diff --git a/app/main.cpp b/app/main.cpp index 0796711..ec6840f 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -45,8 +45,9 @@ int main(int argc, char *argv[]) if (!urlList.isEmpty()) { w.showUrls(urlList); - w.adjustWindowSizeBySceneRect(); } + w.initWindowSize(); + return a.exec(); } diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index b7f8855..624dad2 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -164,8 +164,22 @@ void MainWindow::showUrls(const QList &urls) m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio); } +void MainWindow::initWindowSize() +{ + switch (Settings::instance()->initWindowSizeBehavior()) { + case ActionAutoSize: + adjustWindowSizeBySceneRect(); + break; + case ActionMaximize: + showMaximized(); + break; + } +} + void MainWindow::adjustWindowSizeBySceneRect() { + if (m_pm->count() < 1) return; + QSize sceneSize = m_graphicsView->sceneRect().toRect().size(); QSize sceneSizeWithMargins = sceneSize + QSize(130, 125); diff --git a/app/mainwindow.h b/app/mainwindow.h index 3017d0b..ccfda33 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -33,6 +33,7 @@ public: ~MainWindow() override; void showUrls(const QList &urls); + void initWindowSize(); void adjustWindowSizeBySceneRect(); QUrl currentImageFileUrl() const; diff --git a/app/playlistmanager.cpp b/app/playlistmanager.cpp index b25bd3a..1652d04 100644 --- a/app/playlistmanager.cpp +++ b/app/playlistmanager.cpp @@ -118,6 +118,11 @@ int PlaylistManager::indexOf(const QString &filePath) return m_playlist.indexOf(url); } +int PlaylistManager::count() const +{ + return m_playlist.count(); +} + std::tuple PlaylistManager::previousFile() const { int count = m_playlist.count(); diff --git a/app/playlistmanager.h b/app/playlistmanager.h index 0711629..964e6cb 100644 --- a/app/playlistmanager.h +++ b/app/playlistmanager.h @@ -28,6 +28,8 @@ public: int appendFile(const QString & filePath); int indexOf(const QString & filePath); + int count() const; + std::tuple previousFile() const; std::tuple nextFile() const; std::tuple currentFile() const; diff --git a/app/settings.cpp b/app/settings.cpp index d28b358..7fa7cba 100644 --- a/app/settings.cpp +++ b/app/settings.cpp @@ -21,20 +21,27 @@ bool Settings::stayOnTop() return m_qsettings->value("stay_on_top", true).toBool(); } -DoubleClickBehavior Settings::doubleClickBehavior() +DoubleClickBehavior Settings::doubleClickBehavior() const { QString result = m_qsettings->value("double_click_behavior", "close").toString().toLower(); return stringToDoubleClickBehavior(result); } -MouseWheelBehavior Settings::mouseWheelBehavior() +MouseWheelBehavior Settings::mouseWheelBehavior() const { QString result = m_qsettings->value("mouse_wheel_behavior", "close").toString().toLower(); return stringToMouseWheelBehavior(result); } +WindowSizeBehavior Settings::initWindowSizeBehavior() const +{ + QString result = m_qsettings->value("init_window_size_behavior", "auto").toString().toLower(); + + return stringToWindowSizeBehavior(result); +} + void Settings::setStayOnTop(bool on) { m_qsettings->setValue("stay_on_top", on); @@ -53,6 +60,12 @@ void Settings::setMouseWheelBehavior(MouseWheelBehavior mwb) m_qsettings->sync(); } +void Settings::setInitWindowSizeBehavior(WindowSizeBehavior wsb) +{ + m_qsettings->setValue("init_window_size_behavior", windowSizeBehaviorToString(wsb)); + m_qsettings->sync(); +} + QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb) { static QMap _map { @@ -74,6 +87,16 @@ QString Settings::mouseWheelBehaviorToString(MouseWheelBehavior mwb) return _map.value(mwb, "zoom"); } +QString Settings::windowSizeBehaviorToString(WindowSizeBehavior wsb) +{ + static QMap _map { + {ActionAutoSize, "auto"}, + {ActionMaximize, "maximized"} + }; + + return _map.value(wsb, "zoom"); +} + DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str) { static QMap _map { @@ -95,6 +118,16 @@ MouseWheelBehavior Settings::stringToMouseWheelBehavior(QString str) return _map.value(str, ActionZoomImage); } +WindowSizeBehavior Settings::stringToWindowSizeBehavior(QString str) +{ + static QMap _map { + {"auto", ActionAutoSize}, + {"maximized", ActionMaximize} + }; + + return _map.value(str, ActionAutoSize); +} + Settings::Settings() : QObject(qApp) { diff --git a/app/settings.h b/app/settings.h index 6ba49c9..5fe9b3e 100644 --- a/app/settings.h +++ b/app/settings.h @@ -20,6 +20,14 @@ enum MouseWheelBehavior { MWActionEnd = ActionPrevNextImage }; +enum WindowSizeBehavior { + ActionAutoSize, + ActionMaximize, + + IWSActionStart = ActionAutoSize, + IWSActionEnd = ActionMaximize +}; + class Settings : public QObject { Q_OBJECT @@ -27,17 +35,21 @@ public: static Settings *instance(); bool stayOnTop(); - DoubleClickBehavior doubleClickBehavior(); - MouseWheelBehavior mouseWheelBehavior(); + DoubleClickBehavior doubleClickBehavior() const; + MouseWheelBehavior mouseWheelBehavior() const; + WindowSizeBehavior initWindowSizeBehavior() const; void setStayOnTop(bool on); void setDoubleClickBehavior(DoubleClickBehavior dcb); void setMouseWheelBehavior(MouseWheelBehavior mwb); + void setInitWindowSizeBehavior(WindowSizeBehavior wsb); static QString doubleClickBehaviorToString(DoubleClickBehavior dcb); static QString mouseWheelBehaviorToString(MouseWheelBehavior mwb); + static QString windowSizeBehaviorToString(WindowSizeBehavior wsb); static DoubleClickBehavior stringToDoubleClickBehavior(QString str); static MouseWheelBehavior stringToMouseWheelBehavior(QString str); + static WindowSizeBehavior stringToWindowSizeBehavior(QString str); private: Settings(); diff --git a/app/settingsdialog.cpp b/app/settingsdialog.cpp index 4a6e484..1415e9b 100644 --- a/app/settingsdialog.cpp +++ b/app/settingsdialog.cpp @@ -12,6 +12,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) , m_stayOnTop(new QCheckBox) , m_doubleClickBehavior(new QComboBox) , m_mouseWheelBehavior(new QComboBox) + , m_initWindowSizeBehavior(new QComboBox) { this->setWindowTitle(tr("Settings")); @@ -28,6 +29,11 @@ SettingsDialog::SettingsDialog(QWidget *parent) { ActionPrevNextImage, tr("View next or previous item") } }; + static QMap _iws_map { + { ActionAutoSize, "Auto size" }, + { ActionMaximize, "Maximize" } + }; + QStringList dcbDropDown; for (int dcb = DCActionStart; dcb <= DCActionEnd; dcb++) { dcbDropDown.append(_dc_map.value(static_cast(dcb))); @@ -38,9 +44,15 @@ SettingsDialog::SettingsDialog(QWidget *parent) mwbDropDown.append(_mw_map.value(static_cast(mwb))); } + QStringList iwsbDropDown; + for (int iwsb = IWSActionStart; iwsb <= IWSActionEnd; iwsb++) { + iwsbDropDown.append(_iws_map.value(static_cast(iwsb))); + } + settingsForm->addRow(tr("Stay on top when start-up"), m_stayOnTop); settingsForm->addRow(tr("Double-click behavior"), m_doubleClickBehavior); settingsForm->addRow(tr("Mouse wheel behavior"), m_mouseWheelBehavior); + settingsForm->addRow("Init window size behavior", m_initWindowSizeBehavior); m_stayOnTop->setChecked(Settings::instance()->stayOnTop()); m_doubleClickBehavior->setModel(new QStringListModel(dcbDropDown)); @@ -49,6 +61,9 @@ SettingsDialog::SettingsDialog(QWidget *parent) m_mouseWheelBehavior->setModel(new QStringListModel(mwbDropDown)); MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior(); m_mouseWheelBehavior->setCurrentIndex(static_cast(mwb)); + m_initWindowSizeBehavior->setModel(new QStringListModel(iwsbDropDown)); + WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior(); + m_initWindowSizeBehavior->setCurrentIndex(static_cast(iwsb)); connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){ Settings::instance()->setStayOnTop(state == Qt::Checked); @@ -62,7 +77,11 @@ SettingsDialog::SettingsDialog(QWidget *parent) Settings::instance()->setMouseWheelBehavior(static_cast(index)); }); - this->setMinimumSize(300, 61); // not sure why it complain "Unable to set geometry" + connect(m_initWindowSizeBehavior, QOverload::of(&QComboBox::currentIndexChanged), this, [ = ](int index){ + Settings::instance()->setInitWindowSizeBehavior(static_cast(index)); + }); + + this->setMinimumSize(300, 90); // not sure why it complain "Unable to set geometry" setWindowFlag(Qt::WindowContextHelpButtonHint, false); } diff --git a/app/settingsdialog.h b/app/settingsdialog.h index 9892f1c..5c2de26 100644 --- a/app/settingsdialog.h +++ b/app/settingsdialog.h @@ -21,6 +21,7 @@ private: QCheckBox * m_stayOnTop = nullptr; QComboBox * m_doubleClickBehavior = nullptr; QComboBox * m_mouseWheelBehavior = nullptr; + QComboBox * m_initWindowSizeBehavior = nullptr; }; #endif // SETTINGSDIALOG_H