From fa87f762989286d7afeb7cfad63e9c811aaa2a80 Mon Sep 17 00:00:00 2001 From: Andrew Senetar Date: Wed, 13 May 2020 23:07:14 -0500 Subject: [PATCH] Initial work on #653 - Update both qt/directories_dialog.py and qt/result_window.py to have the same resizing on out of bounds. - Update moveToScreenCenter to do most of the checks needed - Add additional preference to help track maximized position of results window --- qt/directories_dialog.py | 8 ++++++-- qt/preferences.py | 5 +++-- qt/result_window.py | 29 ++++++++++++++--------------- qtlib/util.py | 20 ++++++++++++++++---- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/qt/directories_dialog.py b/qt/directories_dialog.py index 5b0c1ea4..c26879ee 100644 --- a/qt/directories_dialog.py +++ b/qt/directories_dialog.py @@ -206,8 +206,6 @@ class DirectoriesDialog(QMainWindow): if self.app.prefs.directoriesWindowRect is not None: self.setGeometry(self.app.prefs.directoriesWindowRect) - else: - moveToScreenCenter(self) def _setupColumns(self): header = self.treeView.header() @@ -326,3 +324,9 @@ class DirectoriesDialog(QMainWindow): def selectionChanged(self, selected, deselected): self._updateRemoveButton() + + def showEvent(self, event): + if self.isMaximized() is False: + # have to do this here as the frameGeometry is not correct until shown + moveToScreenCenter(self) + super().showEvent(event) diff --git a/qt/preferences.py b/qt/preferences.py index c9691cca..1534ede4 100644 --- a/qt/preferences.py +++ b/qt/preferences.py @@ -30,7 +30,7 @@ class Preferences(PreferencesBase): self.language = trans.installed_lang self.tableFontSize = get("TableFontSize", self.tableFontSize) - self.reference_bold_font = get('ReferenceBoldFont', self.reference_bold_font) + self.reference_bold_font = get("ReferenceBoldFont", self.reference_bold_font) self.resultWindowIsMaximized = get( "ResultWindowIsMaximized", self.resultWindowIsMaximized ) @@ -68,6 +68,7 @@ class Preferences(PreferencesBase): self.tableFontSize = QApplication.font().pointSize() self.reference_bold_font = True self.resultWindowIsMaximized = False + self.resultWindowMaximizeLocation = None self.resultWindowRect = None self.directoriesWindowRect = None self.recentResults = [] @@ -99,7 +100,7 @@ class Preferences(PreferencesBase): set_("Language", self.language) set_("TableFontSize", self.tableFontSize) - set_('ReferenceBoldFont', self.reference_bold_font) + set_("ReferenceBoldFont", self.reference_bold_font) set_("ResultWindowIsMaximized", self.resultWindowIsMaximized) self.set_rect("ResultWindowRect", self.resultWindowRect) self.set_rect("DirectoriesWindowRect", self.directoriesWindowRect) diff --git a/qt/result_window.py b/qt/result_window.py index 705ee912..c8c525e2 100644 --- a/qt/result_window.py +++ b/qt/result_window.py @@ -20,7 +20,6 @@ from PyQt5.QtWidgets import ( QDialog, QPushButton, QCheckBox, - QDesktopWidget, ) from hscommon.trans import trget @@ -344,21 +343,12 @@ class ResultWindow(QMainWindow): self.statusLabel = QLabel(self) self.statusbar.addPermanentWidget(self.statusLabel, 1) + if self.app.prefs.resultWindowRect is not None: + self.setGeometry(self.app.prefs.resultWindowRect) if self.app.prefs.resultWindowIsMaximized: + if self.app.prefs.resultWindowMaximizeLocation is not None: + self.move(self.app.prefs.resultWindowMaximizeLocation) self.setWindowState(self.windowState() | Qt.WindowMaximized) - else: - if self.app.prefs.resultWindowRect is not None: - self.setGeometry(self.app.prefs.resultWindowRect) - # if not on any screen move to center of default screen - # moves to center of closest screen if partially off screen - frame = self.frameGeometry() - if QDesktopWidget().screenNumber(self) == -1: - moveToScreenCenter(self) - elif QDesktopWidget().availableGeometry(self).contains(frame) is False: - frame.moveCenter(QDesktopWidget().availableGeometry(self).center()) - self.move(frame.topLeft()) - else: - moveToScreenCenter(self) # --- Private def _update_column_actions_status(self): @@ -452,7 +442,10 @@ class ResultWindow(QMainWindow): def appWillSavePrefs(self): prefs = self.app.prefs prefs.resultWindowIsMaximized = self.isMaximized() - prefs.resultWindowRect = self.geometry() + if self.isMaximized(): + prefs.resultWindowMaximizeLocation = self.geometry().topLeft() + else: + prefs.resultWindowRect = self.geometry() def columnToggled(self, action): index = action.item_index @@ -478,3 +471,9 @@ class ResultWindow(QMainWindow): def closeEvent(self, event): # this saves the location of the results window when it is closed self.appWillSavePrefs() + + def showEvent(self, event): + if self.isMaximized() is False: + # have to do this here as the frameGeometry is not correct until shown + moveToScreenCenter(self) + super().showEvent(event) diff --git a/qtlib/util.py b/qtlib/util.py index 8ac8d268..c76a2647 100644 --- a/qtlib/util.py +++ b/qtlib/util.py @@ -15,9 +15,8 @@ import logging from hscommon.util import first from PyQt5.QtCore import QStandardPaths -from PyQt5.QtGui import QPixmap, QIcon +from PyQt5.QtGui import QPixmap, QIcon, QGuiApplication from PyQt5.QtWidgets import ( - QDesktopWidget, QSpacerItem, QSizePolicy, QAction, @@ -27,8 +26,21 @@ from PyQt5.QtWidgets import ( def moveToScreenCenter(widget): frame = widget.frameGeometry() - frame.moveCenter(QDesktopWidget().availableGeometry().center()) - widget.move(frame.topLeft()) + if QGuiApplication.screenAt(frame.center()) is None: + # if center not on any screen use default screen + screen = QGuiApplication.screens[0].availableGeometry() + else: + screen = QGuiApplication.screenAt(frame.center()).availableGeometry() + # moves to center of screen if partially off screen + if screen.contains(frame) is False: + # make sure the frame is not larger than screen + # resize does not seem to take frame size into account (move does) + widget.resize( + frame.size().boundedTo(screen.size() - (frame.size() - widget.size())) + ) + frame = widget.frameGeometry() + frame.moveCenter(screen.center()) + widget.move(frame.topLeft()) def verticalSpacer(size=None):