Definitions dialog functional
This commit is contained in:
2
TODO
2
TODO
@@ -48,7 +48,7 @@ TODO
|
|||||||
✓ Editing: Name
|
✓ Editing: Name
|
||||||
✓ Deletion
|
✓ Deletion
|
||||||
✓ Cache next and previous images
|
✓ 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?
|
Search document using QTextCursor?
|
||||||
Comic view keyboard shortcuts
|
Comic view keyboard shortcuts
|
||||||
Filetypes:
|
Filetypes:
|
||||||
|
@@ -392,8 +392,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
self.tabWidget.widget(self.tabWidget.currentIndex()).add_bookmark()
|
self.tabWidget.widget(self.tabWidget.currentIndex()).add_bookmark()
|
||||||
|
|
||||||
def test_function(self):
|
def test_function(self):
|
||||||
# print('Caesar si viveret, ad remum dareris')
|
print('Caesar si viveret, ad remum dareris')
|
||||||
self.definitionDialog.find_definition('draft')
|
|
||||||
|
|
||||||
def resizeEvent(self, event=None):
|
def resizeEvent(self, event=None):
|
||||||
if event:
|
if event:
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from PyQt5 import QtWidgets, QtCore, QtGui
|
from PyQt5 import QtWidgets, QtCore, QtGui, QtMultimedia
|
||||||
|
|
||||||
from resources import definitions
|
from resources import definitions
|
||||||
|
|
||||||
@@ -56,6 +56,11 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
|||||||
self.root_url += self.language + '/'
|
self.root_url += self.language + '/'
|
||||||
self.define_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):
|
def api_call(self, url, word):
|
||||||
url = url + word.lower()
|
url = url + word.lower()
|
||||||
|
|
||||||
@@ -72,17 +77,27 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
|||||||
def find_definition(self, word):
|
def find_definition(self, word):
|
||||||
word_root_json = self.api_call(self.root_url, word)
|
word_root_json = self.api_call(self.root_url, word)
|
||||||
if not word_root_json:
|
if not word_root_json:
|
||||||
|
self.set_text(word, None, None, True)
|
||||||
return
|
return
|
||||||
|
|
||||||
word_root = word_root_json['results'][0]['lexicalEntries'][0]['inflectionOf'][0]['id']
|
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)
|
definition_json = self.api_call(self.define_url, word_root)
|
||||||
|
if not definition_json:
|
||||||
|
return
|
||||||
|
|
||||||
definitions = {}
|
definitions = {}
|
||||||
for i in definition_json['results'][0]['lexicalEntries']:
|
for i in definition_json['results'][0]['lexicalEntries']:
|
||||||
category = i['lexicalCategory']
|
category = i['lexicalCategory']
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.pronunciation_mp3 = i['pronunciations'][0]['audioFile']
|
||||||
|
except KeyError:
|
||||||
|
self.pronounceButton.setEnabled(False)
|
||||||
|
|
||||||
this_sense = i['entries'][0]['senses']
|
this_sense = i['entries'][0]['senses']
|
||||||
for j in this_sense:
|
for j in this_sense:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
this_definition = j['definitions'][0].capitalize()
|
this_definition = j['definitions'][0].capitalize()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@@ -97,23 +112,38 @@ class DefinitionsUI(QtWidgets.QDialog, definitions.Ui_Dialog):
|
|||||||
|
|
||||||
self.set_text(word, word_root, definitions)
|
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 = ''
|
html_string = ''
|
||||||
|
|
||||||
# Word heading
|
# Word heading
|
||||||
html_string += f'<h2><em><strong>{word}</strong></em></h2>\n'
|
html_string += f'<h2><em><strong>{word}</strong></em></h2>\n'
|
||||||
|
|
||||||
# Word root
|
if nothing_found:
|
||||||
html_string += f'<p><em>Word root: <em>{word_root}</p>\n'
|
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'
|
||||||
|
|
||||||
for i in definitions.items():
|
# Definitions per category as an ordered list
|
||||||
category = i[0]
|
for i in definitions.items():
|
||||||
html_string += f'<p><strong>{category}</strong>:</p>\n<ol>\n'
|
category = i[0]
|
||||||
|
html_string += f'<p><strong>{category}</strong>:</p>\n<ol>\n'
|
||||||
|
|
||||||
for j in i[1]:
|
for j in i[1]:
|
||||||
html_string += f'<li>{j}</li>\n'
|
html_string += f'<li>{j}</li>\n'
|
||||||
|
|
||||||
html_string += '</ol>\n'
|
html_string += '</ol>\n'
|
||||||
|
|
||||||
self.definitionView.setHtml(html_string)
|
self.definitionView.setHtml(html_string)
|
||||||
self.show()
|
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, self.main_window)
|
||||||
self.verticalScrollBar().sliderMoved.connect(self.record_scroll_position)
|
self.verticalScrollBar().sliderMoved.connect(self.record_scroll_position)
|
||||||
self.setMouseTracking(True)
|
self.setMouseTracking(True)
|
||||||
|
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
|
||||||
|
self.customContextMenuRequested.connect(
|
||||||
|
self.generate_textbrowser_context_menu)
|
||||||
|
|
||||||
def wheelEvent(self, event):
|
def wheelEvent(self, event):
|
||||||
self.record_scroll_position()
|
self.record_scroll_position()
|
||||||
@@ -620,6 +623,28 @@ class PliantQTextBrowser(QtWidgets.QTextBrowser):
|
|||||||
else:
|
else:
|
||||||
self.parent.metadata['position']['last_visible_text'] = visible_text
|
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):
|
def closeEvent(self, *args):
|
||||||
self.main_window.closeEvent()
|
self.main_window.closeEvent()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user