Center settings on current screen

This commit is contained in:
Jonas Kvinge
2020-04-06 22:02:32 +02:00
parent 4a0235c2ed
commit 1c38c39db2
3 changed files with 102 additions and 66 deletions

View File

@@ -2298,7 +2298,7 @@ void MainWindow::ShowCoverManager() {
SettingsDialog *MainWindow::CreateSettingsDialog() { SettingsDialog *MainWindow::CreateSettingsDialog() {
SettingsDialog *settings_dialog = new SettingsDialog(app_); SettingsDialog *settings_dialog = new SettingsDialog(app_, this);
#ifdef HAVE_GLOBALSHORTCUTS #ifdef HAVE_GLOBALSHORTCUTS
settings_dialog->SetGlobalShortcutManager(global_shortcuts_); settings_dialog->SetGlobalShortcutManager(global_shortcuts_);
#endif #endif

View File

@@ -23,6 +23,7 @@
#include <QtGlobal> #include <QtGlobal>
#include <QDialog> #include <QDialog>
#include <QWidget> #include <QWidget>
#include <QMainWindow>
#include <QScreen> #include <QScreen>
#include <QWindow> #include <QWindow>
#include <QAbstractItemModel> #include <QAbstractItemModel>
@@ -45,6 +46,8 @@
#include <QLayout> #include <QLayout>
#include <QStackedWidget> #include <QStackedWidget>
#include <QSettings> #include <QSettings>
#include <QShowEvent>
#include <QCloseEvent>
#include "core/application.h" #include "core/application.h"
#include "core/player.h" #include "core/player.h"
@@ -73,8 +76,6 @@
#include "ui_settingsdialog.h" #include "ui_settingsdialog.h"
class QShowEvent;
const char *SettingsDialog::kSettingsGroup = "SettingsDialog"; const char *SettingsDialog::kSettingsGroup = "SettingsDialog";
SettingsItemDelegate::SettingsItemDelegate(QObject *parent) 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), : QDialog(parent),
mainwindow_(mainwindow),
app_(app), app_(app),
player_(app_->player()), player_(app_->player()),
engine_(app_->player()->engine()), engine_(app_->player()->engine()),
@@ -159,6 +161,56 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent)
ui_->buttonBox->button(QDialogButtonBox::Cancel)->setShortcut(QKeySequence::Close); 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; QSettings s;
s.beginGroup(kSettingsGroup); s.beginGroup(kSettingsGroup);
if (s.contains("geometry")) { if (s.contains("geometry")) {
@@ -166,10 +218,29 @@ SettingsDialog::SettingsDialog(Application *app, QWidget *parent)
} }
s.endGroup(); 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() { 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) { void SettingsDialog::DialogButtonClicked(QAbstractButton *button) {
// While we only connect Apply at the moment, this might change in the future // 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) { void SettingsDialog::OpenAtPage(Page page) {
if (!pages_.contains(page)) { if (!pages_.contains(page)) {

View File

@@ -36,6 +36,7 @@
#include "engine/engine_fwd.h" #include "engine/engine_fwd.h"
#include "widgets/osd.h" #include "widgets/osd.h"
class QMainWindow;
class QWidget; class QWidget;
class QModelIndex; class QModelIndex;
class QPainter; class QPainter;
@@ -44,6 +45,7 @@ class QComboBox;
class QScrollArea; class QScrollArea;
class QAbstractButton; class QAbstractButton;
class QShowEvent; class QShowEvent;
class QCloseEvent;
class Application; class Application;
class Player; class Player;
@@ -67,7 +69,7 @@ class SettingsDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
SettingsDialog(Application *app, QWidget *parent = nullptr); SettingsDialog(Application *app, QMainWindow *mainwindow, QDialog *parent = nullptr);
~SettingsDialog(); ~SettingsDialog();
enum Page { enum Page {
@@ -104,15 +106,31 @@ class SettingsDialog : public QDialog {
void OpenAtPage(Page page); 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 // QDialog
void accept(); void accept();
void reject(); void reject();
// QWidget void LoadGeometry();
void showEvent(QShowEvent *e); void SaveGeometry();
void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const QString &default_value); QTreeWidgetItem *AddCategory(const QString &name);
void ComboBoxLoadFromSettings(const QSettings &s, QComboBox *combobox, const QString &setting, const int default_value); void AddPage(Page id, SettingsPage *page, QTreeWidgetItem *parent = nullptr);
void Save();
signals: signals:
void ReloadSettings(); void ReloadSettings();
@@ -122,23 +140,10 @@ class SettingsDialog : public QDialog {
void CurrentItemChanged(QTreeWidgetItem *item); void CurrentItemChanged(QTreeWidgetItem *item);
void DialogButtonClicked(QAbstractButton *button); 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: private:
static const char *kSettingsGroup; static const char *kSettingsGroup;
QMainWindow *mainwindow_;
Application *app_; Application *app_;
Player *player_; Player *player_;
EngineBase *engine_; EngineBase *engine_;