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 *settings_dialog = new SettingsDialog(app_);
SettingsDialog *settings_dialog = new SettingsDialog(app_, this);
#ifdef HAVE_GLOBALSHORTCUTS
settings_dialog->SetGlobalShortcutManager(global_shortcuts_);
#endif

View File

@ -23,6 +23,7 @@
#include <QtGlobal>
#include <QDialog>
#include <QWidget>
#include <QMainWindow>
#include <QScreen>
#include <QWindow>
#include <QAbstractItemModel>
@ -45,6 +46,8 @@
#include <QLayout>
#include <QStackedWidget>
#include <QSettings>
#include <QShowEvent>
#include <QCloseEvent>
#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)) {

View File

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