Definitions dialog functional
This commit is contained in:
2
TODO
2
TODO
@@ -48,7 +48,7 @@ TODO
|
||||
✓ Editing: Name
|
||||
✓ Deletion
|
||||
✓ Cache next and previous images
|
||||
Set context menu for definitions and the like
|
||||
✓ Set context menu for definitions and the like
|
||||
Search document using QTextCursor?
|
||||
Comic view keyboard shortcuts
|
||||
Filetypes:
|
||||
|
@@ -392,8 +392,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
||||
self.tabWidget.widget(self.tabWidget.currentIndex()).add_bookmark()
|
||||
|
||||
def test_function(self):
|
||||
# print('Caesar si viveret, ad remum dareris')
|
||||
self.definitionDialog.find_definition('draft')
|
||||
print('Caesar si viveret, ad remum dareris')
|
||||
|
||||
def resizeEvent(self, event=None):
|
||||
if event:
|
||||
|
@@ -17,7 +17,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import requests
|
||||
from PyQt5 import QtWidgets, QtCore, QtGui
|
||||
from PyQt5 import QtWidgets, QtCore, QtGui, QtMultimedia
|
||||
|
||||
from resources import definitions
|
||||
|
||||
@@ -56,6 +56,11 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
||||
self.root_url += self.language + '/'
|
||||
self.define_url += self.language + '/'
|
||||
|
||||
self.pronunciation_mp3 = None
|
||||
|
||||
self.okButton.clicked.connect(self.hide)
|
||||
self.pronounceButton.clicked.connect(self.play_pronunciation)
|
||||
|
||||
def api_call(self, url, word):
|
||||
url = url + word.lower()
|
||||
|
||||
@@ -72,17 +77,27 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
||||
def find_definition(self, word):
|
||||
word_root_json = self.api_call(self.root_url, word)
|
||||
if not word_root_json:
|
||||
self.set_text(word, None, None, True)
|
||||
return
|
||||
|
||||
word_root = word_root_json['results'][0]['lexicalEntries'][0]['inflectionOf'][0]['id']
|
||||
self.pronounceButton.setToolTip(f'Pronounce "{word_root}"')
|
||||
|
||||
definition_json = self.api_call(self.define_url, word_root)
|
||||
if not definition_json:
|
||||
return
|
||||
|
||||
definitions = {}
|
||||
for i in definition_json['results'][0]['lexicalEntries']:
|
||||
category = i['lexicalCategory']
|
||||
|
||||
try:
|
||||
self.pronunciation_mp3 = i['pronunciations'][0]['audioFile']
|
||||
except KeyError:
|
||||
self.pronounceButton.setEnabled(False)
|
||||
|
||||
this_sense = i['entries'][0]['senses']
|
||||
for j in this_sense:
|
||||
|
||||
try:
|
||||
this_definition = j['definitions'][0].capitalize()
|
||||
except KeyError:
|
||||
@@ -97,15 +112,19 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
||||
|
||||
self.set_text(word, word_root, definitions)
|
||||
|
||||
def set_text(self, word, word_root, definitions):
|
||||
def set_text(self, word, word_root, definitions, nothing_found=False):
|
||||
html_string = ''
|
||||
|
||||
# Word heading
|
||||
html_string += f'<h2><em><strong>{word}</strong></em></h2>\n'
|
||||
|
||||
if nothing_found:
|
||||
html_string += f'<p><em>No definitions found<em></p>\n'
|
||||
else:
|
||||
# Word root
|
||||
html_string += f'<p><em>Word root: <em>{word_root}</p>\n'
|
||||
|
||||
# Definitions per category as an ordered list
|
||||
for i in definitions.items():
|
||||
category = i[0]
|
||||
html_string += f'<p><strong>{category}</strong>:</p>\n<ol>\n'
|
||||
@@ -117,3 +136,14 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
||||
|
||||
self.definitionView.setHtml(html_string)
|
||||
self.show()
|
||||
|
||||
def play_pronunciation(self):
|
||||
if not self.pronunciation_mp3:
|
||||
return
|
||||
|
||||
media_content = QtMultimedia.QMediaContent(
|
||||
QtCore.QUrl(self.pronunciation_mp3))
|
||||
|
||||
player = QtMultimedia.QMediaPlayer(self)
|
||||
player.setMedia(media_content)
|
||||
player.play()
|
||||
|
25
widgets.py
25
widgets.py
@@ -579,6 +579,9 @@ class PliantQTextBrowser(QtWidgets.QTextBrowser):
|
||||
self, self.main_window)
|
||||
self.verticalScrollBar().sliderMoved.connect(self.record_scroll_position)
|
||||
self.setMouseTracking(True)
|
||||
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
|
||||
self.customContextMenuRequested.connect(
|
||||
self.generate_textbrowser_context_menu)
|
||||
|
||||
def wheelEvent(self, event):
|
||||
self.record_scroll_position()
|
||||
@@ -620,6 +623,28 @@ class PliantQTextBrowser(QtWidgets.QTextBrowser):
|
||||
else:
|
||||
self.parent.metadata['position']['last_visible_text'] = visible_text
|
||||
|
||||
def generate_textbrowser_context_menu(self, position):
|
||||
selected_word = self.textCursor().selection()
|
||||
selected_word = selected_word.toPlainText()
|
||||
|
||||
context_menu = QtWidgets.QMenu()
|
||||
|
||||
defineAction = 'Caesar si viveret, ad remum dareris'
|
||||
if selected_word and selected_word != '':
|
||||
selected_word = selected_word.split()[0]
|
||||
defineAction = context_menu.addAction(
|
||||
QtGui.QIcon.fromTheme('view-readermode'), f'Define "{selected_word}"')
|
||||
|
||||
searchAction = context_menu.addAction(
|
||||
QtGui.QIcon.fromTheme('search'), 'Search')
|
||||
|
||||
action = context_menu.exec_(self.sender().mapToGlobal(position))
|
||||
|
||||
if action == defineAction:
|
||||
self.window().definitionDialog.find_definition(selected_word)
|
||||
if action == searchAction:
|
||||
self.window().bookToolBar.searchBar.setFocus()
|
||||
|
||||
def closeEvent(self, *args):
|
||||
self.main_window.closeEvent()
|
||||
|
||||
|
Reference in New Issue
Block a user