feat: option for set window startup size

This commit is contained in:
Gary Wang 2022-03-11 16:21:56 +08:00
parent 5674230c4c
commit 432266a6f0
9 changed files with 94 additions and 6 deletions

View File

@ -45,8 +45,9 @@ int main(int argc, char *argv[])
if (!urlList.isEmpty()) { if (!urlList.isEmpty()) {
w.showUrls(urlList); w.showUrls(urlList);
w.adjustWindowSizeBySceneRect();
} }
w.initWindowSize();
return a.exec(); return a.exec();
} }

View File

@ -164,8 +164,22 @@ void MainWindow::showUrls(const QList<QUrl> &urls)
m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio); 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() void MainWindow::adjustWindowSizeBySceneRect()
{ {
if (m_pm->count() < 1) return;
QSize sceneSize = m_graphicsView->sceneRect().toRect().size(); QSize sceneSize = m_graphicsView->sceneRect().toRect().size();
QSize sceneSizeWithMargins = sceneSize + QSize(130, 125); QSize sceneSizeWithMargins = sceneSize + QSize(130, 125);

View File

@ -33,6 +33,7 @@ public:
~MainWindow() override; ~MainWindow() override;
void showUrls(const QList<QUrl> &urls); void showUrls(const QList<QUrl> &urls);
void initWindowSize();
void adjustWindowSizeBySceneRect(); void adjustWindowSizeBySceneRect();
QUrl currentImageFileUrl() const; QUrl currentImageFileUrl() const;

View File

@ -118,6 +118,11 @@ int PlaylistManager::indexOf(const QString &filePath)
return m_playlist.indexOf(url); return m_playlist.indexOf(url);
} }
int PlaylistManager::count() const
{
return m_playlist.count();
}
std::tuple<int, QString> PlaylistManager::previousFile() const std::tuple<int, QString> PlaylistManager::previousFile() const
{ {
int count = m_playlist.count(); int count = m_playlist.count();

View File

@ -28,6 +28,8 @@ public:
int appendFile(const QString & filePath); int appendFile(const QString & filePath);
int indexOf(const QString & filePath); int indexOf(const QString & filePath);
int count() const;
std::tuple<int, QString> previousFile() const; std::tuple<int, QString> previousFile() const;
std::tuple<int, QString> nextFile() const; std::tuple<int, QString> nextFile() const;
std::tuple<int, QString> currentFile() const; std::tuple<int, QString> currentFile() const;

View File

@ -21,20 +21,27 @@ bool Settings::stayOnTop()
return m_qsettings->value("stay_on_top", true).toBool(); 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(); QString result = m_qsettings->value("double_click_behavior", "close").toString().toLower();
return stringToDoubleClickBehavior(result); return stringToDoubleClickBehavior(result);
} }
MouseWheelBehavior Settings::mouseWheelBehavior() MouseWheelBehavior Settings::mouseWheelBehavior() const
{ {
QString result = m_qsettings->value("mouse_wheel_behavior", "close").toString().toLower(); QString result = m_qsettings->value("mouse_wheel_behavior", "close").toString().toLower();
return stringToMouseWheelBehavior(result); 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) void Settings::setStayOnTop(bool on)
{ {
m_qsettings->setValue("stay_on_top", on); m_qsettings->setValue("stay_on_top", on);
@ -53,6 +60,12 @@ void Settings::setMouseWheelBehavior(MouseWheelBehavior mwb)
m_qsettings->sync(); 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) QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb)
{ {
static QMap<DoubleClickBehavior, QString> _map { static QMap<DoubleClickBehavior, QString> _map {
@ -74,6 +87,16 @@ QString Settings::mouseWheelBehaviorToString(MouseWheelBehavior mwb)
return _map.value(mwb, "zoom"); return _map.value(mwb, "zoom");
} }
QString Settings::windowSizeBehaviorToString(WindowSizeBehavior wsb)
{
static QMap<WindowSizeBehavior, QString> _map {
{ActionAutoSize, "auto"},
{ActionMaximize, "maximized"}
};
return _map.value(wsb, "zoom");
}
DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str) DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str)
{ {
static QMap<QString, DoubleClickBehavior> _map { static QMap<QString, DoubleClickBehavior> _map {
@ -95,6 +118,16 @@ MouseWheelBehavior Settings::stringToMouseWheelBehavior(QString str)
return _map.value(str, ActionZoomImage); return _map.value(str, ActionZoomImage);
} }
WindowSizeBehavior Settings::stringToWindowSizeBehavior(QString str)
{
static QMap<QString, WindowSizeBehavior> _map {
{"auto", ActionAutoSize},
{"maximized", ActionMaximize}
};
return _map.value(str, ActionAutoSize);
}
Settings::Settings() Settings::Settings()
: QObject(qApp) : QObject(qApp)
{ {

View File

@ -20,6 +20,14 @@ enum MouseWheelBehavior {
MWActionEnd = ActionPrevNextImage MWActionEnd = ActionPrevNextImage
}; };
enum WindowSizeBehavior {
ActionAutoSize,
ActionMaximize,
IWSActionStart = ActionAutoSize,
IWSActionEnd = ActionMaximize
};
class Settings : public QObject class Settings : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -27,17 +35,21 @@ public:
static Settings *instance(); static Settings *instance();
bool stayOnTop(); bool stayOnTop();
DoubleClickBehavior doubleClickBehavior(); DoubleClickBehavior doubleClickBehavior() const;
MouseWheelBehavior mouseWheelBehavior(); MouseWheelBehavior mouseWheelBehavior() const;
WindowSizeBehavior initWindowSizeBehavior() const;
void setStayOnTop(bool on); void setStayOnTop(bool on);
void setDoubleClickBehavior(DoubleClickBehavior dcb); void setDoubleClickBehavior(DoubleClickBehavior dcb);
void setMouseWheelBehavior(MouseWheelBehavior mwb); void setMouseWheelBehavior(MouseWheelBehavior mwb);
void setInitWindowSizeBehavior(WindowSizeBehavior wsb);
static QString doubleClickBehaviorToString(DoubleClickBehavior dcb); static QString doubleClickBehaviorToString(DoubleClickBehavior dcb);
static QString mouseWheelBehaviorToString(MouseWheelBehavior mwb); static QString mouseWheelBehaviorToString(MouseWheelBehavior mwb);
static QString windowSizeBehaviorToString(WindowSizeBehavior wsb);
static DoubleClickBehavior stringToDoubleClickBehavior(QString str); static DoubleClickBehavior stringToDoubleClickBehavior(QString str);
static MouseWheelBehavior stringToMouseWheelBehavior(QString str); static MouseWheelBehavior stringToMouseWheelBehavior(QString str);
static WindowSizeBehavior stringToWindowSizeBehavior(QString str);
private: private:
Settings(); Settings();

View File

@ -12,6 +12,7 @@ SettingsDialog::SettingsDialog(QWidget *parent)
, m_stayOnTop(new QCheckBox) , m_stayOnTop(new QCheckBox)
, m_doubleClickBehavior(new QComboBox) , m_doubleClickBehavior(new QComboBox)
, m_mouseWheelBehavior(new QComboBox) , m_mouseWheelBehavior(new QComboBox)
, m_initWindowSizeBehavior(new QComboBox)
{ {
this->setWindowTitle(tr("Settings")); this->setWindowTitle(tr("Settings"));
@ -28,6 +29,11 @@ SettingsDialog::SettingsDialog(QWidget *parent)
{ ActionPrevNextImage, tr("View next or previous item") } { ActionPrevNextImage, tr("View next or previous item") }
}; };
static QMap<WindowSizeBehavior, QString> _iws_map {
{ ActionAutoSize, "Auto size" },
{ ActionMaximize, "Maximize" }
};
QStringList dcbDropDown; QStringList dcbDropDown;
for (int dcb = DCActionStart; dcb <= DCActionEnd; dcb++) { for (int dcb = DCActionStart; dcb <= DCActionEnd; dcb++) {
dcbDropDown.append(_dc_map.value(static_cast<DoubleClickBehavior>(dcb))); dcbDropDown.append(_dc_map.value(static_cast<DoubleClickBehavior>(dcb)));
@ -38,9 +44,15 @@ SettingsDialog::SettingsDialog(QWidget *parent)
mwbDropDown.append(_mw_map.value(static_cast<MouseWheelBehavior>(mwb))); mwbDropDown.append(_mw_map.value(static_cast<MouseWheelBehavior>(mwb)));
} }
QStringList iwsbDropDown;
for (int iwsb = IWSActionStart; iwsb <= IWSActionEnd; iwsb++) {
iwsbDropDown.append(_iws_map.value(static_cast<WindowSizeBehavior>(iwsb)));
}
settingsForm->addRow(tr("Stay on top when start-up"), m_stayOnTop); settingsForm->addRow(tr("Stay on top when start-up"), m_stayOnTop);
settingsForm->addRow(tr("Double-click behavior"), m_doubleClickBehavior); settingsForm->addRow(tr("Double-click behavior"), m_doubleClickBehavior);
settingsForm->addRow(tr("Mouse wheel behavior"), m_mouseWheelBehavior); settingsForm->addRow(tr("Mouse wheel behavior"), m_mouseWheelBehavior);
settingsForm->addRow("Init window size behavior", m_initWindowSizeBehavior);
m_stayOnTop->setChecked(Settings::instance()->stayOnTop()); m_stayOnTop->setChecked(Settings::instance()->stayOnTop());
m_doubleClickBehavior->setModel(new QStringListModel(dcbDropDown)); m_doubleClickBehavior->setModel(new QStringListModel(dcbDropDown));
@ -49,6 +61,9 @@ SettingsDialog::SettingsDialog(QWidget *parent)
m_mouseWheelBehavior->setModel(new QStringListModel(mwbDropDown)); m_mouseWheelBehavior->setModel(new QStringListModel(mwbDropDown));
MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior(); MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior();
m_mouseWheelBehavior->setCurrentIndex(static_cast<int>(mwb)); m_mouseWheelBehavior->setCurrentIndex(static_cast<int>(mwb));
m_initWindowSizeBehavior->setModel(new QStringListModel(iwsbDropDown));
WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior();
m_initWindowSizeBehavior->setCurrentIndex(static_cast<int>(iwsb));
connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){ connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){
Settings::instance()->setStayOnTop(state == Qt::Checked); Settings::instance()->setStayOnTop(state == Qt::Checked);
@ -62,7 +77,11 @@ SettingsDialog::SettingsDialog(QWidget *parent)
Settings::instance()->setMouseWheelBehavior(static_cast<MouseWheelBehavior>(index)); Settings::instance()->setMouseWheelBehavior(static_cast<MouseWheelBehavior>(index));
}); });
this->setMinimumSize(300, 61); // not sure why it complain "Unable to set geometry" connect(m_initWindowSizeBehavior, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index){
Settings::instance()->setInitWindowSizeBehavior(static_cast<WindowSizeBehavior>(index));
});
this->setMinimumSize(300, 90); // not sure why it complain "Unable to set geometry"
setWindowFlag(Qt::WindowContextHelpButtonHint, false); setWindowFlag(Qt::WindowContextHelpButtonHint, false);
} }

View File

@ -21,6 +21,7 @@ private:
QCheckBox * m_stayOnTop = nullptr; QCheckBox * m_stayOnTop = nullptr;
QComboBox * m_doubleClickBehavior = nullptr; QComboBox * m_doubleClickBehavior = nullptr;
QComboBox * m_mouseWheelBehavior = nullptr; QComboBox * m_mouseWheelBehavior = nullptr;
QComboBox * m_initWindowSizeBehavior = nullptr;
}; };
#endif // SETTINGSDIALOG_H #endif // SETTINGSDIALOG_H