Saves window geometry

This commit is contained in:
Putta Khunchalee 2022-09-17 15:44:02 +07:00
parent 8f17ad8b7b
commit 1a8919bea3
4 changed files with 44 additions and 7 deletions

View File

@ -1,19 +1,24 @@
#include "main_window.hpp"
#include "game_models.hpp"
#include "settings.hpp"
#include <QAction>
#include <QCoreApplication>
#include <QCloseEvent>
#include <QGuiApplication>
#include <QListView>
#include <QMenuBar>
#include <QMessageBox>
#include <QSettings>
MainWindow::MainWindow(GameListModel *games)
{
restoreGeometry();
// Setup File menu.
auto file = menuBar()->addMenu("&File");
auto quit = new QAction("&Quit", this);
connect(quit, &QAction::triggered, this, &MainWindow::quit);
connect(quit, &QAction::triggered, this, &MainWindow::close);
file->addAction(quit);
@ -23,13 +28,16 @@ MainWindow::MainWindow(GameListModel *games)
m_games->setModel(games);
setCentralWidget(m_games);
// Setup status bar.
statusBar();
}
MainWindow::~MainWindow()
{
}
void MainWindow::quit()
void MainWindow::closeEvent(QCloseEvent *event)
{
// Ask user to confirm.
QMessageBox confirm(this);
@ -41,9 +49,33 @@ void MainWindow::quit()
confirm.setIcon(QMessageBox::Warning);
if (confirm.exec() != QMessageBox::Yes) {
event->ignore();
return;
}
// Exit Qt.
QCoreApplication::instance()->exit();
// Save gometry.
QSettings settings;
settings.beginGroup(SettingGroups::mainWindow);
settings.setValue("size", size());
if (qGuiApp->platformName() != "wayland") {
// Wayland does not allow application to position itself.
settings.setValue("pos", pos());
}
QMainWindow::closeEvent(event);
}
void MainWindow::restoreGeometry()
{
QSettings settings;
settings.beginGroup(SettingGroups::mainWindow);
resize(settings.value("size", QSize(800, 800)).toSize());
if (qGuiApp->platformName() != "wayland") {
move(settings.value("pos", QPoint(200, 200)).toPoint());
}
}

View File

@ -10,8 +10,11 @@ public:
MainWindow(GameListModel *games);
~MainWindow();
private slots:
void quit();
protected:
void closeEvent(QCloseEvent *event) override;
private:
void restoreGeometry();
private:
QListView *m_games;

View File

@ -34,4 +34,5 @@ void writeGamesDirectorySetting(const QString &v)
namespace SettingGroups {
const QString user("user");
const QString mainWindow("mainWindow");
}

View File

@ -10,4 +10,5 @@ void writeGamesDirectorySetting(const QString &v);
// Group registry for QSettings.
namespace SettingGroups {
extern const QString user;
extern const QString mainWindow;
}