From 89304c2e170005715b0441993cac6cabd7e8b9a6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 8 Dec 2020 20:24:41 +0530 Subject: [PATCH] More stupid PyQt enums --- src/calibre/gui2/complete2.py | 13 +++++++------ src/calibre/gui2/keyboard.py | 6 +++--- src/calibre/gui2/library/alternate_views.py | 2 +- src/calibre/gui2/library/views.py | 2 +- src/calibre/gui2/search_box.py | 4 ++-- src/calibre/gui2/shortcuts.py | 6 +++--- src/calibre/gui2/tag_mapper.py | 4 ++-- src/calibre/gui2/tweak_book/completion/popup.py | 8 ++++---- src/calibre/gui2/tweak_book/diff/view.py | 16 ++++++++-------- src/calibre/gui2/tweak_book/editor/text.py | 16 ++++++++-------- src/calibre/gui2/tweak_book/search.py | 4 ++-- src/calibre/gui2/viewer/web_view.py | 6 +++--- 12 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/calibre/gui2/complete2.py b/src/calibre/gui2/complete2.py index a0c454d782..951b2a2eed 100644 --- a/src/calibre/gui2/complete2.py +++ b/src/calibre/gui2/complete2.py @@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en' from PyQt5.Qt import ( QLineEdit, QAbstractListModel, Qt, pyqtSignal, QObject, QKeySequence, QAbstractItemView, - QApplication, QListView, QPoint, QModelIndex, + QApplication, QListView, QPoint, QModelIndex, QEvent, QStyleOptionComboBox, QStyle, QComboBox, QTimer) try: from PyQt5 import sip @@ -184,7 +184,7 @@ class Completer(QListView): # {{{ def debug_event(self, ev): from calibre.gui2 import event_type_name print('Event:', event_type_name(ev)) - if ev.type() in (ev.KeyPress, ev.ShortcutOverride, ev.KeyRelease): + if ev.type() in (QEvent.Type.KeyPress, QEvent.Type.ShortcutOverride, QEvent.Type.KeyRelease): print('\tkey:', QKeySequence(ev.key()).toString()) def mouseMoveEvent(self, ev): @@ -206,7 +206,7 @@ class Completer(QListView): # {{{ # self.debug_event(e) - if etype == e.KeyPress: + if etype == QEvent.Type.KeyPress: try: key = e.key() except AttributeError: @@ -253,13 +253,14 @@ class Completer(QListView): # {{{ self.hide() if e.isAccepted(): return True - elif ismacos and etype == e.InputMethodQuery and e.queries() == (Qt.InputMethodQuery.ImHints | Qt.InputMethodQuery.ImEnabled) and self.isVisible(): + elif ismacos and etype == QEvent.Type.InputMethodQuery and e.queries() == ( + Qt.InputMethodQuery.ImHints | Qt.InputMethodQuery.ImEnabled) and self.isVisible(): # In Qt 5 the Esc key causes this event and the line edit does not # handle it, which causes the parent dialog to be closed # See https://bugreports.qt-project.org/browse/QTBUG-41806 e.accept() return True - elif etype == e.MouseButtonPress and hasattr(e, 'globalPos') and not self.rect().contains(self.mapFromGlobal(e.globalPos())): + elif etype == QEvent.Type.MouseButtonPress and hasattr(e, 'globalPos') and not self.rect().contains(self.mapFromGlobal(e.globalPos())): # A click outside the popup, close it if isinstance(widget, QComboBox): # This workaround is needed to ensure clicking on the drop down @@ -274,7 +275,7 @@ class Completer(QListView): # {{{ self.hide() e.accept() return True - elif etype in (e.InputMethod, e.ShortcutOverride): + elif etype in (QEvent.Type.InputMethod, QEvent.Type.ShortcutOverride): QApplication.sendEvent(widget, e) return False # }}} diff --git a/src/calibre/gui2/keyboard.py b/src/calibre/gui2/keyboard.py index 85c7ac6b29..f55264f960 100644 --- a/src/calibre/gui2/keyboard.py +++ b/src/calibre/gui2/keyboard.py @@ -12,7 +12,7 @@ from functools import partial from PyQt5.Qt import (QObject, QKeySequence, QAbstractItemModel, QModelIndex, Qt, QStyledItemDelegate, QTextDocument, QStyle, pyqtSignal, QFrame, QApplication, QSize, QRectF, QWidget, QTreeView, QHBoxLayout, QVBoxLayout, - QGridLayout, QLabel, QRadioButton, QPushButton, QToolButton, QIcon) + QGridLayout, QLabel, QRadioButton, QPushButton, QToolButton, QIcon, QEvent) try: from PyQt5 import sip except ImportError: @@ -490,10 +490,10 @@ class Editor(QFrame): # {{{ def eventFilter(self, obj, event): if self.capture and obj in (self.button1, self.button2): t = event.type() - if t == event.ShortcutOverride: + if t == QEvent.Type.ShortcutOverride: event.accept() return True - if t == event.KeyPress: + if t == QEvent.Type.KeyPress: self.key_press_event(event, 1 if obj is self.button1 else 2) return True return QFrame.eventFilter(self, obj, event) diff --git a/src/calibre/gui2/library/alternate_views.py b/src/calibre/gui2/library/alternate_views.py index 97be85729f..0f45097357 100644 --- a/src/calibre/gui2/library/alternate_views.py +++ b/src/calibre/gui2/library/alternate_views.py @@ -1146,7 +1146,7 @@ class GridView(QListView): return index def selectionCommand(self, index, event): - if event and event.type() == event.KeyPress and event.key() in (Qt.Key.Key_Home, Qt.Key.Key_End) and event.modifiers() & Qt.Modifier.CTRL: + if event and event.type() == QEvent.Type.KeyPress and event.key() in (Qt.Key.Key_Home, Qt.Key.Key_End) and event.modifiers() & Qt.Modifier.CTRL: return QItemSelectionModel.SelectionFlag.ClearAndSelect | QItemSelectionModel.SelectionFlag.Rows return super(GridView, self).selectionCommand(index, event) diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index 8cb6dc6bdb..9db22d1f77 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -1184,7 +1184,7 @@ class BooksView(QTableView): # {{{ return index def selectionCommand(self, index, event): - if event and event.type() == event.KeyPress and event.key() in (Qt.Key.Key_Home, Qt.Key.Key_End) and event.modifiers() & Qt.Modifier.CTRL: + if event and event.type() == QEvent.Type.KeyPress and event.key() in (Qt.Key.Key_Home, Qt.Key.Key_End) and event.modifiers() & Qt.Modifier.CTRL: return QItemSelectionModel.SelectionFlag.ClearAndSelect | QItemSelectionModel.SelectionFlag.Rows return super(BooksView, self).selectionCommand(index, event) diff --git a/src/calibre/gui2/search_box.py b/src/calibre/gui2/search_box.py index 4c06e32ca5..ea2ebe61db 100644 --- a/src/calibre/gui2/search_box.py +++ b/src/calibre/gui2/search_box.py @@ -11,7 +11,7 @@ from functools import partial from PyQt5.Qt import ( - QComboBox, Qt, QLineEdit, pyqtSlot, QDialog, + QComboBox, Qt, QLineEdit, pyqtSlot, QDialog, QEvent, pyqtSignal, QCompleter, QAction, QKeySequence, QTimer, QIcon, QMenu, QApplication, QKeyEvent) @@ -69,7 +69,7 @@ class SearchLineEdit(QLineEdit): # {{{ def paste_and_search(self): self.paste() - ev = QKeyEvent(QKeyEvent.KeyPress, Qt.Key.Key_Enter, Qt.KeyboardModifier.NoModifier) + ev = QKeyEvent(QEvent.Type.KeyPress, Qt.Key.Key_Enter, Qt.KeyboardModifier.NoModifier) self.keyPressEvent(ev) @pyqtSlot() diff --git a/src/calibre/gui2/shortcuts.py b/src/calibre/gui2/shortcuts.py index 9711a6009b..9eafce88ac 100644 --- a/src/calibre/gui2/shortcuts.py +++ b/src/calibre/gui2/shortcuts.py @@ -11,7 +11,7 @@ from functools import partial from PyQt5.Qt import ( QAbstractListModel, Qt, QKeySequence, QListView, QVBoxLayout, QLabel, QHBoxLayout, QWidget, QApplication, QStyledItemDelegate, QStyle, QIcon, - QTextDocument, QRectF, QFrame, QSize, QFont, QKeyEvent, QRadioButton, QPushButton, QToolButton + QTextDocument, QRectF, QFrame, QSize, QFont, QKeyEvent, QRadioButton, QPushButton, QToolButton, QEvent ) from calibre.gui2 import error_dialog @@ -72,10 +72,10 @@ class Customize(QFrame): if self.capture == 0 or obj not in (self.button1, self.button2): return QFrame.eventFilter(self, obj, event) t = event.type() - if t == event.ShortcutOverride: + if t == QEvent.Type.ShortcutOverride: event.accept() return True - if t == event.KeyPress: + if t == QEvent.Type.KeyPress: self.key_press_event(event, 1 if obj is self.button1 else 2) return True return QFrame.eventFilter(self, obj, event) diff --git a/src/calibre/gui2/tag_mapper.py b/src/calibre/gui2/tag_mapper.py index 1e44057fbd..ecb7a25722 100644 --- a/src/calibre/gui2/tag_mapper.py +++ b/src/calibre/gui2/tag_mapper.py @@ -9,7 +9,7 @@ import textwrap from PyQt5.Qt import ( QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QListWidget, QIcon, QDialog, QSize, QComboBox, QLineEdit, QListWidgetItem, QStyledItemDelegate, QAbstractItemView, - QStaticText, Qt, QStyle, QToolButton, QInputDialog, QMenu, pyqtSignal + QStaticText, Qt, QStyle, QToolButton, QInputDialog, QMenu, pyqtSignal, QPalette ) from calibre.ebooks.metadata.tag_mapper import map_tags, compile_pat @@ -256,7 +256,7 @@ class Delegate(QStyledItemDelegate): def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index) pal = option.palette - color = pal.color(pal.HighlightedText if option.state & QStyle.StateFlag.State_Selected else pal.Text).name() + color = pal.color(QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text).name() text = '
%s
' % (color, index.data(RENDER_ROLE)) st = QStaticText(text) st.setTextWidth(option.rect.width()) diff --git a/src/calibre/gui2/tweak_book/completion/popup.py b/src/calibre/gui2/tweak_book/completion/popup.py index 003993b0dd..338c59f88c 100644 --- a/src/calibre/gui2/tweak_book/completion/popup.py +++ b/src/calibre/gui2/tweak_book/completion/popup.py @@ -9,7 +9,7 @@ import textwrap from math import ceil from PyQt5.Qt import ( - QWidget, Qt, QStaticText, QTextOption, QSize, QPainter, QTimer, QPalette) + QWidget, Qt, QStaticText, QTextOption, QSize, QPainter, QTimer, QPalette, QEvent) from calibre import prints, prepare_string_for_xml from calibre.gui2 import error_dialog @@ -109,9 +109,9 @@ class ChoosePopupWidget(QWidget): painter = QPainter(self) painter.setClipRect(ev.rect()) pal = self.palette() - painter.fillRect(self.rect(), pal.color(pal.Text)) + painter.fillRect(self.rect(), pal.color(QPalette.ColorRole.Text)) crect = self.rect().adjusted(1, 1, -1, -1) - painter.fillRect(crect, pal.color(pal.Base)) + painter.fillRect(crect, pal.color(QPalette.ColorRole.Base)) painter.setClipRect(crect) painter.setFont(self.parent().font()) width = self.rect().width() @@ -195,7 +195,7 @@ class ChoosePopupWidget(QWidget): def eventFilter(self, obj, ev): if obj is self.parent() and self.isVisible(): etype = ev.type() - if etype == ev.KeyPress: + if etype == QEvent.Type.KeyPress: ret = self.handle_keypress(ev) if ret: ev.accept() diff --git a/src/calibre/gui2/tweak_book/diff/view.py b/src/calibre/gui2/tweak_book/diff/view.py index b3479ef509..ce289d4acd 100644 --- a/src/calibre/gui2/tweak_book/diff/view.py +++ b/src/calibre/gui2/tweak_book/diff/view.py @@ -122,20 +122,20 @@ class TextBrowser(PlainTextEdit): # {{{ font.setBold(True) theme = get_theme(tprefs['editor_theme']) pal = self.palette() - pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) - pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) - pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg')) - pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg')) - pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg')) + pal.setColor(QPalette.ColorRole.Base, theme_color(theme, 'Normal', 'bg')) + pal.setColor(QPalette.ColorRole.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) + pal.setColor(QPalette.ColorRole.Text, theme_color(theme, 'Normal', 'fg')) + pal.setColor(QPalette.ColorRole.Highlight, theme_color(theme, 'Visual', 'bg')) + pal.setColor(QPalette.ColorRole.HighlightedText, theme_color(theme, 'Visual', 'fg')) self.setPalette(pal) self.viewport().setCursor(Qt.CursorShape.ArrowCursor) self.line_number_area = LineNumbers(self) self.blockCountChanged[int].connect(self.update_line_number_area_width) self.updateRequest.connect(self.update_line_number_area) self.line_number_palette = pal = QPalette() - pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg')) - pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg')) - pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg')) + pal.setColor(QPalette.ColorRole.Base, theme_color(theme, 'LineNr', 'bg')) + pal.setColor(QPalette.ColorRole.Text, theme_color(theme, 'LineNr', 'fg')) + pal.setColor(QPalette.ColorRole.BrightText, theme_color(theme, 'LineNrC', 'fg')) self.line_number_map = LineNumberMap() self.search_header_pos = 0 self.changes, self.headers, self.images = [], [], OrderedDict() diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index ec84891619..f1e46b293f 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -235,19 +235,19 @@ class TextEdit(PlainTextEdit): def apply_theme(self, theme): self.theme = theme pal = self.palette() - pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg')) - pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) - pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg')) + pal.setColor(QPalette.ColorRole.Base, theme_color(theme, 'Normal', 'bg')) + pal.setColor(QPalette.ColorRole.AlternateBase, theme_color(theme, 'CursorLine', 'bg')) + pal.setColor(QPalette.ColorRole.Text, theme_color(theme, 'Normal', 'fg')) pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg')) pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg')) self.setPalette(pal) self.tooltip_palette = pal = QPalette() - pal.setColor(pal.ToolTipBase, theme_color(theme, 'Tooltip', 'bg')) - pal.setColor(pal.ToolTipText, theme_color(theme, 'Tooltip', 'fg')) + pal.setColor(QPalette.ColorRole.ToolTipBase, theme_color(theme, 'Tooltip', 'bg')) + pal.setColor(QPalette.ColorRole.ToolTipText, theme_color(theme, 'Tooltip', 'fg')) self.line_number_palette = pal = QPalette() - pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg')) - pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg')) - pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg')) + pal.setColor(QPalette.ColorRole.Base, theme_color(theme, 'LineNr', 'bg')) + pal.setColor(QPalette.ColorRole.Text, theme_color(theme, 'LineNr', 'fg')) + pal.setColor(QPalette.ColorRole.BrightText, theme_color(theme, 'LineNrC', 'fg')) self.match_paren_format = theme_format(theme, 'MatchParen') font = self.font() ff = tprefs['editor_font_family'] diff --git a/src/calibre/gui2/tweak_book/search.py b/src/calibre/gui2/tweak_book/search.py index c1fc206f2a..c0d4b3af3b 100644 --- a/src/calibre/gui2/tweak_book/search.py +++ b/src/calibre/gui2/tweak_book/search.py @@ -14,7 +14,7 @@ from PyQt5.Qt import ( QGridLayout, QHBoxLayout, QIcon, QItemSelection, QKeySequence, QLabel, QLineEdit, QListView, QMenu, QMimeData, QModelIndex, QPushButton, QScrollArea, QSize, QSizePolicy, QStackedLayout, QStyledItemDelegate, Qt, QTimer, QToolBar, QDialog, - QToolButton, QVBoxLayout, QWidget, pyqtSignal, QAbstractItemView + QToolButton, QVBoxLayout, QWidget, pyqtSignal, QAbstractItemView, QEvent ) from calibre import prepare_string_for_xml @@ -104,7 +104,7 @@ class HistoryBox(HistoryComboBox): self.set_uniform_item_sizes(False) def event(self, ev): - if ev.type() in (ev.ShortcutOverride, ev.KeyPress) and ev.key() == KEY and ev.modifiers() & MODIFIER: + if ev.type() in (QEvent.Type.ShortcutOverride, QEvent.Type.KeyPress) and ev.key() == KEY and ev.modifiers() & MODIFIER: if not self.ignore_snip_expansion: self.ignore_snip_expansion = True expand_template(self.lineEdit()) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 45de56d44e..fc8f6c30f4 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -8,7 +8,7 @@ import shutil import sys from itertools import count from PyQt5.Qt import ( - QT_VERSION, QApplication, QBuffer, QByteArray, QFontDatabase, QFontInfo, + QT_VERSION, QApplication, QBuffer, QByteArray, QFontDatabase, QFontInfo, QPalette, QHBoxLayout, QMimeData, QSize, Qt, QTimer, QUrl, QWidget, pyqtSignal, QIODevice ) from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler @@ -422,8 +422,8 @@ def system_colors(): is_dark_theme = app.is_dark_theme pal = app.palette() ans = { - 'background': pal.color(pal.Base).name(), - 'foreground': pal.color(pal.Text).name(), + 'background': pal.color(QPalette.ColorRole.Base).name(), + 'foreground': pal.color(QPalette.ColorRole.Text).name(), } if is_dark_theme: # only override link colors for dark themes