From 1c38c39db2ad6789281edb368ae0d53e18cfe2f1 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Mon, 6 Apr 2020 22:02:32 +0200 Subject: [PATCH] Center settings on current screen --- src/core/mainwindow.cpp | 2 +- src/settings/settingsdialog.cpp | 123 ++++++++++++++++++++------------ src/settings/settingsdialog.h | 43 ++++++----- 3 files changed, 102 insertions(+), 66 deletions(-) diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index d3be1a607..4e47f4baa 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -2298,7 +2298,7 @@ void MainWindow::ShowCoverManager() { SettingsDialog *MainWindow::CreateSettingsDialog() { - SettingsDialog *settings_dialog = new SettingsDialog(app_); + SettingsDialog *settings_dialog = new SettingsDialog(app_, this); #ifdef HAVE_GLOBALSHORTCUTS settings_dialog->SetGlobalShortcutManager(global_shortcuts_); #endif diff --git a/src/settings/settingsdialog.cpp b/src/settings/settingsdialog.cpp index 09eac1f88..2eac44e73 100644 --- a/src/settings/settingsdialog.cpp +++ b/src/settings/settingsdialog.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -45,6 +46,8 @@ #include #include #include +#include +#include #include "core/application.h" #include "core/player.h" @@ -73,8 +76,6 @@ #include "ui_settingsdialog.h" -class QShowEvent; - const char *SettingsDialog::kSettingsGroup = "SettingsDialog"; SettingsItemDelegate::SettingsItemDelegate(QObject *parent) @@ -106,8 +107,9 @@ void SettingsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & } -SettingsDialog::SettingsDialog(Application *app, QWidget *parent) +SettingsDialog::SettingsDialog(Application *app, QMainWindow *mainwindow, QDialog *parent) : QDialog(parent), + mainwindow_(mainwindow), app_(app), player_(app_->player()), engine_(app_->player()->engine()), @@ -159,6 +161,56 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent) ui_->buttonBox->button(QDialogButtonBox::Cancel)->setShortcut(QKeySequence::Close); +} + +SettingsDialog::~SettingsDialog() { + delete ui_; +} + +void SettingsDialog::showEvent(QShowEvent *e) { + + LoadGeometry(); + + // Load settings + loading_settings_ = true; + for (const PageData &data : pages_.values()) { + data.page_->Load(); + } + loading_settings_ = false; + + QDialog::showEvent(e); + +} + +void SettingsDialog::closeEvent(QCloseEvent*) { + + SaveGeometry(); + +} + +void SettingsDialog::accept() { + + Save(); + SaveGeometry(); + + QDialog::accept(); + +} + +void SettingsDialog::reject() { + + // Notify each page that user clicks on Cancel + for (const PageData &data : pages_.values()) { + data.page_->Cancel(); + } + SaveGeometry(); + + QDialog::reject(); + +} + +void SettingsDialog::LoadGeometry() { + QSettings s; s.beginGroup(kSettingsGroup); if (s.contains("geometry")) { @@ -166,10 +218,29 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent) } s.endGroup(); -} + // Resize the dialog if it's too big +#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + QScreen *screen = QWidget::screen(); +#else + QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen()); +#endif + if (screen && screen->availableGeometry().height() < height()) { + resize(width(), sizeHint().height()); + } + + // Center the dialog on the same screen as mainwindow. +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) + screen = mainwindow_->screen(); +#else + screen = (mainwindow_->window() && mainwindow_->window()->windowHandle() ? mainwindow_->window()->windowHandle()->screen() : nullptr); +#endif + if (screen) { + const QRect sr = screen->availableGeometry(); + const QRect wr({}, size().boundedTo(sr.size())); + resize(wr.size()); + move(sr.center() - wr.center()); + } -SettingsDialog::~SettingsDialog() { - delete ui_; } void SettingsDialog::SaveGeometry() { @@ -243,23 +314,6 @@ void SettingsDialog::Save() { } -void SettingsDialog::accept() { - Save(); - SaveGeometry(); - QDialog::accept(); -} - -void SettingsDialog::reject() { - - // Notify each page that user clicks on Cancel - for (const PageData &data : pages_.values()) { - data.page_->Cancel(); - } - SaveGeometry(); - - QDialog::reject(); -} - void SettingsDialog::DialogButtonClicked(QAbstractButton *button) { // While we only connect Apply at the moment, this might change in the future @@ -268,29 +322,6 @@ void SettingsDialog::DialogButtonClicked(QAbstractButton *button) { } } -void SettingsDialog::showEvent(QShowEvent *e) { - - // Load settings - loading_settings_ = true; - for (const PageData &data : pages_.values()) { - data.page_->Load(); - } - loading_settings_ = false; - - // Resize the dialog if it's too big -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = QWidget::screen(); -#else - QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen()); -#endif - if (screen->availableGeometry().height() < height()) { - resize(width(), sizeHint().height()); - } - - QDialog::showEvent(e); - -} - void SettingsDialog::OpenAtPage(Page page) { if (!pages_.contains(page)) { diff --git a/src/settings/settingsdialog.h b/src/settings/settingsdialog.h index 5de9c55b7..2875da87a 100644 --- a/src/settings/settingsdialog.h +++ b/src/settings/settingsdialog.h @@ -36,6 +36,7 @@ #include "engine/engine_fwd.h" #include "widgets/osd.h" +class QMainWindow; class QWidget; class QModelIndex; class QPainter; @@ -44,6 +45,7 @@ class QComboBox; class QScrollArea; class QAbstractButton; class QShowEvent; +class QCloseEvent; class Application; class Player; @@ -67,7 +69,7 @@ class SettingsDialog : public QDialog { Q_OBJECT public: - SettingsDialog(Application *app, QWidget *parent = nullptr); + SettingsDialog(Application *app, QMainWindow *mainwindow, QDialog *parent = nullptr); ~SettingsDialog(); enum Page { @@ -104,15 +106,31 @@ class SettingsDialog : public QDialog { void OpenAtPage(Page page); + void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const QString &default_value); + void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const int default_value); + + protected: + void showEvent(QShowEvent *e); + void closeEvent(QCloseEvent*); + + private: + struct PageData { + QTreeWidgetItem *item_; + QScrollArea *scroll_area_; + SettingsPage *page_; + }; + // QDialog void accept(); void reject(); - // QWidget - void showEvent(QShowEvent *e); + void LoadGeometry(); + void SaveGeometry(); - void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const QString &default_value); - void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const int default_value); + QTreeWidgetItem *AddCategory(const QString &name); + void AddPage(Page id, SettingsPage *page, QTreeWidgetItem *parent = nullptr); + + void Save(); signals: void ReloadSettings(); @@ -122,23 +140,10 @@ class SettingsDialog : public QDialog { void CurrentItemChanged(QTreeWidgetItem *item); void DialogButtonClicked(QAbstractButton *button); - private: - struct PageData { - QTreeWidgetItem *item_; - QScrollArea *scroll_area_; - SettingsPage *page_; - }; - - QTreeWidgetItem *AddCategory(const QString &name); - void AddPage(Page id, SettingsPage *page, QTreeWidgetItem *parent = nullptr); - - void Save(); - - void SaveGeometry(); - private: static const char *kSettingsGroup; + QMainWindow *mainwindow_; Application *app_; Player *player_; EngineBase *engine_;