More stupid PyQt enums

This commit is contained in:
Kovid Goyal 2020-12-08 20:24:41 +05:30
parent 23a324c643
commit 89304c2e17
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
12 changed files with 44 additions and 43 deletions

View File

@ -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
# }}}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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 = '<div style="color:%s">%s</div>' % (color, index.data(RENDER_ROLE))
st = QStaticText(text)
st.setTextWidth(option.rect.width())

View File

@ -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()

View File

@ -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()

View File

@ -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']

View File

@ -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())

View File

@ -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