Replace use of the deprecated QDesktoWidget

It is removed in PyQt6
This commit is contained in:
Kovid Goyal 2021-11-19 22:01:50 +05:30
parent 7d85958b57
commit d9bdbbab7f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
19 changed files with 50 additions and 59 deletions

View File

@ -9,15 +9,15 @@ import signal
import sys import sys
import threading import threading
from contextlib import contextmanager from contextlib import contextmanager
from threading import Lock, RLock
from qt.core import ( from qt.core import (
QT_VERSION, QApplication, QBuffer, QByteArray, QCoreApplication, QDateTime, QT_VERSION, QApplication, QBuffer, QByteArray, QColor, QCoreApplication,
QDesktopServices, QDialog, QEvent, QFileDialog, QFileIconProvider, QFileInfo, QPalette, QDateTime, QDesktopServices, QDialog, QDialogButtonBox, QEvent, QFileDialog,
QFont, QFontDatabase, QFontInfo, QFontMetrics, QIcon, QLocale, QColor, QFileIconProvider, QFileInfo, QFont, QFontDatabase, QFontInfo, QFontMetrics,
QNetworkProxyFactory, QObject, QSettings, QSocketNotifier, QStringListModel, Qt, QGuiApplication, QIcon, QIODevice, QLocale, QNetworkProxyFactory, QObject,
QThread, QTimer, QTranslator, QUrl, pyqtSignal, QIODevice, QDialogButtonBox, QStyle QPalette, QSettings, QSocketNotifier, QStringListModel, QStyle, Qt, QThread,
QTimer, QTranslator, QUrl, pyqtSignal
) )
from threading import Lock, RLock
from calibre import as_unicode, prints from calibre import as_unicode, prints
from calibre.constants import ( from calibre.constants import (
@ -331,13 +331,15 @@ def default_author_link():
def available_heights(): def available_heights():
desktop = QCoreApplication.instance().desktop() return tuple(s.availableSize().height() for s in QGuiApplication.screens())
return list(map(lambda x: x.height(), map(desktop.availableGeometry, range(desktop.screenCount()))))
def available_height(): def available_height():
desktop = QCoreApplication.instance().desktop() return QApplication.instance().primaryScreen().availableSize().height()
return desktop.availableGeometry().height()
def available_width():
return QApplication.instance().primaryScreen().availableSize().width()
def max_available_height(): def max_available_height():
@ -348,11 +350,6 @@ def min_available_height():
return min(available_heights()) return min(available_heights())
def available_width():
desktop = QCoreApplication.instance().desktop()
return desktop.availableGeometry().width()
def get_screen_dpi(): def get_screen_dpi():
d = QApplication.desktop() d = QApplication.desktop()
return (d.logicalDpiX(), d.logicalDpiY()) return (d.logicalDpiX(), d.logicalDpiY())
@ -678,12 +675,16 @@ if not iswindows and not ismacos and 'CALIBRE_NO_NATIVE_FILEDIALOGS' not in os.e
has_linux_file_dialog_helper = check_for_linux_native_dialogs() has_linux_file_dialog_helper = check_for_linux_native_dialogs()
if has_windows_file_dialog_helper: if has_windows_file_dialog_helper:
from calibre.gui2.win_file_dialogs import choose_files, choose_images, choose_dir, choose_save_file from calibre.gui2.win_file_dialogs import (
choose_dir, choose_files, choose_images, choose_save_file
)
elif has_linux_file_dialog_helper: elif has_linux_file_dialog_helper:
choose_dir, choose_files, choose_save_file, choose_images = map( choose_dir, choose_files, choose_save_file, choose_images = map(
linux_native_dialog, 'dir files save_file images'.split()) linux_native_dialog, 'dir files save_file images'.split())
else: else:
from calibre.gui2.qt_file_dialogs import choose_files, choose_images, choose_dir, choose_save_file from calibre.gui2.qt_file_dialogs import (
choose_dir, choose_files, choose_images, choose_save_file
)
choose_files, choose_images, choose_dir, choose_save_file choose_files, choose_images, choose_dir, choose_save_file
@ -1042,6 +1043,7 @@ class Application(QApplication):
def load_builtin_fonts(self, scan_for_fonts=False): def load_builtin_fonts(self, scan_for_fonts=False):
if scan_for_fonts: if scan_for_fonts:
from calibre.utils.fonts.scanner import font_scanner from calibre.utils.fonts.scanner import font_scanner
# Start scanning the users computer for fonts # Start scanning the users computer for fonts
font_scanner font_scanner
@ -1332,6 +1334,7 @@ def ensure_app(headless=True):
if headless and has_headless: if headless and has_headless:
_store_app.headless = True _store_app.headless = True
import traceback import traceback
# This is needed because as of PyQt 5.4 if sys.execpthook == # This is needed because as of PyQt 5.4 if sys.execpthook ==
# sys.__excepthook__ PyQt will abort the application on an # sys.__excepthook__ PyQt will abort the application on an
# unhandled python exception in a slot or virtual method. Since ensure_app() # unhandled python exception in a slot or virtual method. Since ensure_app()
@ -1399,7 +1402,7 @@ def elided_text(text, font=None, width=300, pos='middle'):
rendered, replacing characters from the left, middle or right (as per pos) rendered, replacing characters from the left, middle or right (as per pos)
of the string with an ellipsis. Results in a string much closer to the of the string with an ellipsis. Results in a string much closer to the
limit than Qt's elidedText().''' limit than Qt's elidedText().'''
from qt.core import QFontMetrics, QApplication from qt.core import QApplication, QFontMetrics
if font is None: if font is None:
font = QApplication.instance().font() font = QApplication.instance().font()
fm = (font if isinstance(font, QFontMetrics) else QFontMetrics(font)) fm = (font if isinstance(font, QFontMetrics) else QFontMetrics(font))
@ -1434,6 +1437,7 @@ def form_to_compiled_form(form):
def build_forms(srcdir, info=None, summary=False, check_for_migration=False): def build_forms(srcdir, info=None, summary=False, check_for_migration=False):
import re import re
from PyQt5.uic import compileUi from PyQt5.uic import compileUi
from polyglot.io import PolyglotStringIO from polyglot.io import PolyglotStringIO
forms = find_forms(srcdir) forms = find_forms(srcdir)
if info is None: if info is None:
@ -1495,8 +1499,7 @@ empty_index = empty_model.index(0)
def set_app_uid(val): def set_app_uid(val):
import ctypes import ctypes
from ctypes import wintypes from ctypes import HRESULT, wintypes
from ctypes import HRESULT
try: try:
AppUserModelID = ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID AppUserModelID = ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID
except Exception: # Vista has no app uids except Exception: # Vista has no app uids

View File

@ -143,7 +143,7 @@ class Completer(QListView): # {{{
widget = self.parent() widget = self.parent()
if widget is None: if widget is None:
return return
screen = QApplication.desktop().availableGeometry(widget) screen = widget.screen().availableGeometry()
h = (p.sizeHintForRow(0) * min(self.max_visible_items, m.rowCount()) + 3) + 3 h = (p.sizeHintForRow(0) * min(self.max_visible_items, m.rowCount()) + 3) + 3
hsb = p.horizontalScrollBar() hsb = p.horizontalScrollBar()
if hsb and hsb.isVisible(): if hsb and hsb.isVisible():

View File

@ -6,7 +6,7 @@
import shutil import shutil
from qt.core import ( from qt.core import (
QAbstractListModel, QCheckBox, QComboBox, QCoreApplication, QDialog, QAbstractListModel, QCheckBox, QComboBox, QDialog,
QDialogButtonBox, QFont, QFrame, QGridLayout, QHBoxLayout, QIcon, QLabel, QDialogButtonBox, QFont, QFrame, QGridLayout, QHBoxLayout, QIcon, QLabel,
QListView, QModelIndex, QScrollArea, QSize, QSizePolicy, QSpacerItem, QListView, QModelIndex, QScrollArea, QSize, QSizePolicy, QSpacerItem,
Qt, QTextEdit, QWidget, QApplication Qt, QTextEdit, QWidget, QApplication
@ -179,8 +179,7 @@ class Config(QDialog):
self.buttonBox.rejected.connect(self.reject) self.buttonBox.rejected.connect(self.reject)
def sizeHint(self): def sizeHint(self):
desktop = QCoreApplication.instance().desktop() geom = self.screen().availableSize()
geom = desktop.availableGeometry(self)
nh, nw = max(300, geom.height()-100), max(400, geom.width()-70) nh, nw = max(300, geom.height()-100), max(400, geom.width()-70)
return QSize(nw, nh) return QSize(nw, nh)

View File

@ -19,9 +19,7 @@ from qt.core import (
from calibre.constants import islinux from calibre.constants import islinux
from calibre.ebooks.metadata import rating_to_stars, authors_to_string from calibre.ebooks.metadata import rating_to_stars, authors_to_string
from calibre.gui2 import ( from calibre.gui2 import config, gprefs, rating_font
available_height, available_width, config, gprefs, rating_font
)
from calibre_extensions import pictureflow from calibre_extensions import pictureflow
@ -261,7 +259,8 @@ class CBDialog(QDialog):
geom = gprefs.get('cover_browser_dialog_geometry', None) geom = gprefs.get('cover_browser_dialog_geometry', None)
if not geom or not QApplication.instance().safe_restore_geometry(self, geom): if not geom or not QApplication.instance().safe_restore_geometry(self, geom):
h, w = available_height()-60, int(available_width()/1.5) sz = self.screen().availableSize()
h, w = sz.height()-60, int(sz.width()/1.5)
self.resize(w, h) self.resize(w, h)
self.action_fs_toggle = a = QAction(self) self.action_fs_toggle = a = QAction(self)
self.addAction(a) self.addAction(a)
@ -497,7 +496,6 @@ def test():
app = QApplication([]) app = QApplication([])
w = QMainWindow() w = QMainWindow()
cf = CoverFlow() cf = CoverFlow()
cf.resize(int(available_width()/1.5), available_height()-60)
w.resize(cf.size()+QSize(30, 20)) w.resize(cf.size()+QSize(30, 20))
model = DummyImageList() model = DummyImageList()
cf.setImages(model) cf.setImages(model)
@ -518,7 +516,6 @@ if __name__ == '__main__':
app = QApplication([]) app = QApplication([])
w = QMainWindow() w = QMainWindow()
cf = CoverFlow() cf = CoverFlow()
cf.resize(int(available_width()/1.5), available_height()-60)
w.resize(cf.size()+QSize(30, 20)) w.resize(cf.size()+QSize(30, 20))
path = sys.argv[1] path = sys.argv[1]
model = FileSystemImages(sys.argv[1]) model = FileSystemImages(sys.argv[1])

View File

@ -4,7 +4,7 @@
import textwrap import textwrap
from qt.core import ( from qt.core import (
QAction, QApplication, QBrush, QCheckBox, QCoreApplication, QDialog, QGridLayout, QAction, QApplication, QBrush, QCheckBox, QDialog, QGridLayout,
QHBoxLayout, QIcon, QKeySequence, QLabel, QListView, QModelIndex, QPalette, QHBoxLayout, QIcon, QKeySequence, QLabel, QListView, QModelIndex, QPalette,
QPixmap, QPushButton, QShortcut, QSize, QSplitter, Qt, QTimer, QToolButton, QPixmap, QPushButton, QShortcut, QSize, QSplitter, Qt, QTimer, QToolButton,
QVBoxLayout, QWidget, pyqtSignal, QDialogButtonBox QVBoxLayout, QWidget, pyqtSignal, QDialogButtonBox
@ -198,7 +198,7 @@ class BookInfo(QDialog):
self.previous_button.setToolTip(_('Previous [%s]')% self.previous_button.setToolTip(_('Previous [%s]')%
str(self.ps.key().toString(QKeySequence.SequenceFormat.NativeText))) str(self.ps.key().toString(QKeySequence.SequenceFormat.NativeText)))
geom = QCoreApplication.instance().desktop().availableGeometry(self) geom = self.screen().availableSize()
screen_height = geom.height() - 100 screen_height = geom.height() - 100
screen_width = geom.width() - 100 screen_width = geom.width() - 100
self.resize(max(int(screen_width/2), 700), screen_height) self.resize(max(int(screen_width/2), 700), screen_height)

View File

@ -11,7 +11,7 @@ import os
import sys import sys
import weakref import weakref
from qt.core import ( from qt.core import (
QApplication, QCoreApplication, QDialog, QDialogButtonBox, QScrollArea, QSize QApplication, QDialog, QDialogButtonBox, QScrollArea, QSize
) )
from calibre.customize import PluginInstallationType from calibre.customize import PluginInstallationType
@ -138,14 +138,12 @@ class Catalog(QDialog, Ui_Dialog):
QApplication.instance().safe_restore_geometry(self, bytes(geom)) QApplication.instance().safe_restore_geometry(self, bytes(geom))
else: else:
self.resize(self.sizeHint()) self.resize(self.sizeHint())
d = QCoreApplication.instance().desktop() g = self.screen().availableSize()
g = d.availableGeometry(d.screenNumber(self))
self.setMaximumWidth(g.width() - 50) self.setMaximumWidth(g.width() - 50)
self.setMaximumHeight(g.height() - 50) self.setMaximumHeight(g.height() - 50)
def sizeHint(self): def sizeHint(self):
desktop = QCoreApplication.instance().desktop() geom = self.screen().availableSize()
geom = desktop.availableGeometry(self)
nh, nw = max(300, geom.height()-50), max(400, geom.width()-70) nh, nw = max(300, geom.height()-50), max(400, geom.width()-70)
return QSize(nw, nh) return QSize(nw, nh)

View File

@ -8,7 +8,7 @@ import regex
from collections import defaultdict, namedtuple from collections import defaultdict, namedtuple
from io import BytesIO from io import BytesIO
from qt.core import ( from qt.core import (
QApplication, QComboBox, QCompleter, QCoreApplication, QDateTime, QDialog, QApplication, QComboBox, QCompleter, QDateTime, QDialog,
QDialogButtonBox, QFont, QGridLayout, QInputDialog, QLabel, QLineEdit, QDialogButtonBox, QFont, QGridLayout, QInputDialog, QLabel, QLineEdit,
QProgressBar, QSize, Qt, QVBoxLayout, pyqtSignal QProgressBar, QSize, Qt, QVBoxLayout, pyqtSignal
) )
@ -579,8 +579,7 @@ class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
self.exec() self.exec()
def sizeHint(self): def sizeHint(self):
desktop = QCoreApplication.instance().desktop() geom = self.screen().availableSize()
geom = desktop.availableGeometry(self)
nh, nw = max(300, geom.height()-50), max(400, geom.width()-70) nh, nw = max(300, geom.height()-50), max(400, geom.width()-70)
return QSize(nw, nh) return QSize(nw, nh)

View File

@ -601,8 +601,7 @@ class ChooseTheme(Dialog):
self.dialog_closed = True self.dialog_closed = True
def sizeHint(self): def sizeHint(self):
desktop = QApplication.instance().desktop() h = self.screen().availableSize().height()
h = desktop.availableGeometry(self).height()
return QSize(900, h - 75) return QSize(900, h - 75)
def setup_ui(self): def setup_ui(self):

View File

@ -101,8 +101,7 @@ class ImageView(QDialog):
self.maximized_at_last_fullscreen = False self.maximized_at_last_fullscreen = False
self.setWindowFlag(Qt.WindowType.WindowMinimizeButtonHint) self.setWindowFlag(Qt.WindowType.WindowMinimizeButtonHint)
self.setWindowFlag(Qt.WindowType.WindowMaximizeButtonHint) self.setWindowFlag(Qt.WindowType.WindowMaximizeButtonHint)
dw = QApplication.instance().desktop() self.avail_geom = self.screen().availableGeometry()
self.avail_geom = dw.availableGeometry(parent if parent is not None else self)
self.current_img = current_img self.current_img = current_img
self.current_url = current_url self.current_url = current_url
self.factor = 1.0 self.factor = 1.0

View File

@ -41,7 +41,7 @@ CACHE_FORMAT = 'PPM'
def auto_height(widget): def auto_height(widget):
# On some broken systems, availableGeometry() returns tiny values, we need # On some broken systems, availableGeometry() returns tiny values, we need
# a value of at least 1000 for 200 DPI systems. # a value of at least 1000 for 200 DPI systems.
return max(1000, QApplication.instance().desktop().availableGeometry(widget).height()) / 5.0 return max(1000, widget.screen().availableSize().height()) / 5.0
class EncodeError(ValueError): class EncodeError(ValueError):

View File

@ -169,8 +169,7 @@ class Main(MainWindow, Ui_MainWindow):
scrollbar_adjust = min(s.width(), s.height()) scrollbar_adjust = min(s.width(), s.height())
self.graphics_view.resize_for(width+scrollbar_adjust, height+scrollbar_adjust) self.graphics_view.resize_for(width+scrollbar_adjust, height+scrollbar_adjust)
desktop = QCoreApplication.instance().desktop() screen_height = self.screen().availableSize().height() - 25
screen_height = desktop.availableGeometry(self).height() - 25
height = min(screen_height, height+hdelta+scrollbar_adjust) height = min(screen_height, height+hdelta+scrollbar_adjust)
self.resize(width+scrollbar_adjust, height) self.resize(width+scrollbar_adjust, height)
self.setWindowTitle(self.renderer.lrf.metadata.title + ' - ' + __appname__) self.setWindowTitle(self.renderer.lrf.metadata.title + ' - ' + __appname__)

View File

@ -680,8 +680,7 @@ class CompareMany(QDialog):
self.next_item(True) self.next_item(True)
desktop = QApplication.instance().desktop() geom = (parent or self).screen().availableSize()
geom = desktop.availableGeometry(parent or self)
width = max(700, min(950, geom.width()-50)) width = max(700, min(950, geom.width()-50))
height = max(650, min(1000, geom.height()-100)) height = max(650, min(1000, geom.height()-100))
self.resize(QSize(width, height)) self.resize(QSize(width, height))

View File

@ -11,7 +11,7 @@ import os
from datetime import datetime from datetime import datetime
from functools import partial from functools import partial
from qt.core import ( from qt.core import (
QApplication, QCoreApplication, QDialog, QDialogButtonBox, QFont, QFrame, QApplication, QDialog, QDialogButtonBox, QFont, QFrame,
QGridLayout, QGroupBox, QHBoxLayout, QIcon, QInputDialog, QKeySequence, QMenu, QGridLayout, QGroupBox, QHBoxLayout, QIcon, QInputDialog, QKeySequence, QMenu,
QPushButton, QScrollArea, QShortcut, QSize, QSizePolicy, QSpacerItem, QSplitter, QPushButton, QScrollArea, QShortcut, QSize, QSizePolicy, QSpacerItem, QSplitter,
Qt, QTabWidget, QToolButton, QVBoxLayout, QWidget, pyqtSignal Qt, QTabWidget, QToolButton, QVBoxLayout, QWidget, pyqtSignal
@ -131,8 +131,7 @@ class MetadataSingleDialogBase(QDialog):
# }}} # }}}
def sizeHint(self): def sizeHint(self):
desktop = QCoreApplication.instance().desktop() geom = self.screen().availableSize()
geom = desktop.availableGeometry(self)
nh, nw = max(300, geom.height()-50), max(400, geom.width()-70) nh, nw = max(300, geom.height()-50), max(400, geom.width()-70)
return QSize(nw, nh) return QSize(nw, nh)

View File

@ -1034,7 +1034,7 @@ class TOCEditor(QDialog): # {{{
self.explode_done.connect(self.read_toc, type=Qt.ConnectionType.QueuedConnection) self.explode_done.connect(self.read_toc, type=Qt.ConnectionType.QueuedConnection)
self.writing_done.connect(self.really_accept, type=Qt.ConnectionType.QueuedConnection) self.writing_done.connect(self.really_accept, type=Qt.ConnectionType.QueuedConnection)
r = QApplication.desktop().availableGeometry(self) r = self.screen().availableSize()
self.resize(r.width() - 100, r.height() - 100) self.resize(r.width() - 100, r.height() - 100)
geom = self.prefs.get('toc_editor_window_geom', None) geom = self.prefs.get('toc_editor_window_geom', None)
if geom is not None: if geom is not None:

View File

@ -222,7 +222,7 @@ class Diff(Dialog):
self.view.line_activated.connect(self.line_activated) self.view.line_activated.connect(self.line_activated)
def sizeHint(self): def sizeHint(self):
geom = QApplication.instance().desktop().availableGeometry(self) geom = self.screen().availableSize()
return QSize(int(0.9 * geom.width()), int(0.8 * geom.height())) return QSize(int(0.9 * geom.width()), int(0.8 * geom.height()))
def setup_ui(self): def setup_ui(self):

View File

@ -688,7 +688,7 @@ class ThemeEditor(Dialog):
self.show_theme() self.show_theme()
def sizeHint(self): def sizeHint(self):
g = QApplication.instance().desktop().availableGeometry(self.parent() or self) g = self.screen().availableSize()
return QSize(min(1500, g.width() - 25), 650) return QSize(min(1500, g.width() - 25), 650)
# }}} # }}}

View File

@ -403,7 +403,7 @@ class WebView(RestartingWebEngineView, OpenWithHandler):
def __init__(self, parent=None): def __init__(self, parent=None):
RestartingWebEngineView.__init__(self, parent) RestartingWebEngineView.__init__(self, parent)
self.inspector = Inspector(self) self.inspector = Inspector(self)
w = QApplication.instance().desktop().availableGeometry(self).width() w = self.screen().availableSize().width()
self._size_hint = QSize(int(w/3), int(w/2)) self._size_hint = QSize(int(w/3), int(w/2))
self._page = WebPage(self) self._page = WebPage(self)
self.setPage(self._page) self.setPage(self._page)

View File

@ -374,7 +374,7 @@ class Main(MainWindow):
self.status_bar.addWidget(la) self.status_bar.addWidget(la)
self.boss(self) self.boss(self)
g = QApplication.instance().desktop().availableGeometry(self) g = self.screen().availableSize()
self.resize(g.width()-50, g.height()-50) self.resize(g.width()-50, g.height()-50)
self.apply_settings() self.apply_settings()

View File

@ -494,7 +494,7 @@ class WebView(RestartingWebEngineView):
self.tts.event_received.connect(self.tts_event_received) self.tts.event_received.connect(self.tts_event_received)
self.dead_renderer_error_shown = False self.dead_renderer_error_shown = False
self.render_process_failed.connect(self.render_process_died) self.render_process_failed.connect(self.render_process_died)
w = QApplication.instance().desktop().availableGeometry(self).width() w = self.screen().availableSize().width()
QApplication.instance().palette_changed.connect(self.palette_changed) QApplication.instance().palette_changed.connect(self.palette_changed)
self.show_home_page_on_ready = True self.show_home_page_on_ready = True
self._size_hint = QSize(int(w/3), int(w/2)) self._size_hint = QSize(int(w/3), int(w/2))