Skip to content

Commit c56bc7c

Browse files
author
ciscorn
committed
Merge pull request #10 from dkalachov/master
'Save mp3' item in context menu for audio
2 parents d840cc2 + addcb35 commit c56bc7c

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ For Arch Linux, two packages [ldoce5viewer](https://aur.archlinux.org/packages/l
6363

6464
(for advanced users)
6565

66+
*Homebrew*:
67+
```bash
68+
$ brew install pyqt
69+
$ pip install lxml pyobjc-core pyobjc-framework-Cocoa whoosh py2app
70+
$ # inside ldoce5viewer directory
71+
$ sudo DISTUTILS_DEBUG=1 python setup.py py2app
72+
$ open dist/LDOCE5\ Viewer.app/
73+
```
74+
75+
Or if you are using *MacPorts*:
6676
<ol>
67-
<li><p>Install MacPorts</p></li>
6877
<li><p>Install the following ports:</p>
6978
<ul>
7079
<li>python27 (or python3x)</li>

ldoce5viewer/qtgui/main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,28 @@ def _onTextEdited(self, text):
540540
#----------
541541

542542
def _playbackAudio(self, path):
543+
self._getAudioData(path, lambda data: self._soundplayer.play(data))
544+
545+
def _getAudioData(self, path, callback):
543546
(archive, name) = path.lstrip('/').split('/', 1)
544547
if archive in ('us_hwd_pron', 'gb_hwd_pron', 'exa_pron', 'sfx', 'sound'):
545548
def finished():
546549
if reply.error() == QNetworkReply.NoError:
547-
self._soundplayer.play(reply.readAll())
550+
callback(reply.readAll())
548551

549552
url = QUrl('dict:///{0}/{1}'.format(archive, name))
550553
reply = self._networkAccessManager.get(QNetworkRequest(url))
551554
reply.finished.connect(finished)
552555

556+
def downloadSelectedAudio(self):
557+
path = self._ui.webView.audioUrlToDownload.path()
558+
def showSaveDialog(data):
559+
filename = QFileDialog.getSaveFileName(self, u'Save mp3', '', u'MP3 Files (*.mp3)')
560+
if filename != '':
561+
file = open(filename, "w")
562+
file.write(data)
563+
file.close()
564+
self._getAudioData(path, showSaveDialog)
553565

554566
def _onWebViewLinkClicked(self, url):
555567
scheme = url.scheme()
@@ -1126,6 +1138,7 @@ def act_conn(action, slot):
11261138
act_conn(ui.actionSearchDefinitions, self._onSearchDefinitions)
11271139
act_conn(ui.actionAdvancedSearch, self._onAdvancedSearch)
11281140
act_conn(wv.actionSearchText, self.searchSelectedText)
1141+
act_conn(wv.actionDownloadAudio, self.downloadSelectedAudio)
11291142
act_conn(ui.actionZoomIn, partial(self.setZoom, 1, relative=True))
11301143
act_conn(ui.actionZoomOut, partial(self.setZoom, -1, relative=True))
11311144
act_conn(ui.actionNormalSize, partial(self.setZoom, 0))

ldoce5viewer/qtgui/ui/custom.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def __init__(self, parent):
192192
self._actionCopyPlain.setShortcut(QKeySequence.Copy)
193193
self.page().selectionChanged.connect(self.__onSelectionChanged)
194194
self.__onSelectionChanged()
195+
self._actionDownloadAudio = QAction(u'Download mp3', self)
195196

196197
def _copyAsPlainText(self):
197198
text = self.selectedText().strip()
@@ -205,6 +206,14 @@ def actionSearchText(self):
205206
def actionCopyPlain(self):
206207
return self._actionCopyPlain
207208

209+
@property
210+
def actionDownloadAudio(self):
211+
return self._actionDownloadAudio
212+
213+
@property
214+
def audioUrlToDownload(self):
215+
return self._audioUrlToDownload
216+
208217
def __onSelectionChanged(self):
209218
text = self.selectedText()
210219
self._actionCopyPlain.setEnabled(bool(text))
@@ -213,6 +222,14 @@ def contextMenuEvent(self, event):
213222
page = self.page()
214223
menu = page.createStandardContextMenu()
215224
actions = menu.actions()
225+
226+
# inserts the "Download audio" action
227+
frame = page.frameAt(event.pos())
228+
hit_test_result = frame.hitTestContent(event.pos())
229+
if hit_test_result.linkUrl().scheme() == 'audio':
230+
self._audioUrlToDownload = hit_test_result.linkUrl()
231+
menu.insertAction(actions[0] if actions else None,
232+
self.actionDownloadAudio)
216233

217234
# inserts the "Search for ..." action
218235
text = page.selectedText().strip().lower()

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
import subprocess
34
from distutils.core import setup
45
from ldoce5viewer import __version__
56

@@ -60,6 +61,8 @@ def iter_static():
6061
except ImportError:
6162
pass
6263
else:
64+
qt_plugins_path = subprocess.check_output('qmake -query QT_INSTALL_PLUGINS', shell=True)
65+
qt_plugins_path = qt_plugins_path[0:len(qt_plugins_path)-1] # remove "\n"
6366
extra_options.update(dict(
6467
name='LDOCE5 Viewer',
6568
app=['ldoce5viewer.py'],
@@ -90,7 +93,7 @@ def iter_static():
9093
#]
9194
}},
9295
data_files=[
93-
('qt_plugins/imageformats', ['/opt/local/share/qt4/plugins/imageformats/libqjpeg.dylib']),
96+
('qt_plugins/imageformats', [qt_plugins_path]),
9497
('', ['ldoce5viewer/static']),
9598
],
9699
))

0 commit comments

Comments
 (0)