From 7d82812fc9dd628b1fafcd62e8c35af44979a137 Mon Sep 17 00:00:00 2001
From: zty199 <1282441920@qq.com>
Date: Sat, 21 Nov 2020 18:52:35 +0800
Subject: [PATCH] Improve Feature

Add options "Fix Size" and "Hide Buttons" in titlebar GUI menu.
P.S. Command Line settings is not included.
---
 spark-webapp-runtime/mainwindow.cpp           |  67 ++++++++++++++++--
 spark-webapp-runtime/mainwindow.h             |  13 ++++
 .../spark-webapp-runtime_zh_CN.qm             | Bin 1664 -> 1778 bytes
 .../spark-webapp-runtime_zh_CN.ts             |  13 ++++
 4 files changed, 88 insertions(+), 5 deletions(-)

diff --git a/spark-webapp-runtime/mainwindow.cpp b/spark-webapp-runtime/mainwindow.cpp
index 0a44216..5d8567b 100644
--- a/spark-webapp-runtime/mainwindow.cpp
+++ b/spark-webapp-runtime/mainwindow.cpp
@@ -2,7 +2,6 @@
 
 #include <DMainWindow>
 #include <DTitlebar>
-#include <DToolButton>
 
 #include <QLayout>
 #include <QFileInfo>
@@ -17,10 +16,18 @@ MainWindow::MainWindow(QString szTitle,
     : DMainWindow(parent)
     , m_widget(new Widget(szUrl))
     , m_dialog(dialog)
+    , btnBackward(new DToolButton(titlebar()))
+    , btnForward(new DToolButton(titlebar()))
+    , btnRefresh(new DToolButton(titlebar()))
+    , m_menu(new QMenu)
+    , m_fixSize(new QAction(tr("Fix Size")))
+    , m_hideButtons(new QAction(tr("Hide Buttons")))
+    , m_width(nWidth)
+    , m_height(nHeight)
 {
     //  setFixedSize(nWidth, nHeight);
     //  应 shenmo 要求改成设置最小尺寸试试效果
-    setMinimumSize(nWidth, nHeight);
+    setMinimumSize(m_width, m_height);
 
     setCentralWidget(m_widget);
     centralWidget()->layout()->setContentsMargins(0, 0, 0, 0);
@@ -30,13 +37,10 @@ MainWindow::MainWindow(QString szTitle,
     titlebar()->setTitle(szTitle);
     titlebar()->setIcon(QIcon(":/images/spark-webapp-runtime.svg"));
 
-    DToolButton *btnBackward = new DToolButton(titlebar());
     btnBackward->setIcon(QIcon(":/images/go-previous-24.svg"));
     btnBackward->setIconSize(QSize(36, 36));
-    DToolButton *btnForward = new DToolButton(titlebar());
     btnForward->setIcon(QIcon(":/images/go-next-24.svg"));
     btnForward->setIconSize(QSize(36, 36));
-    DToolButton *btnRefresh = new DToolButton(titlebar());
     btnRefresh->setIcon(QIcon(":/images/view-refresh.svg"));
     btnRefresh->setIconSize(QSize(36, 36));
 
@@ -44,6 +48,14 @@ MainWindow::MainWindow(QString szTitle,
     titlebar()->addWidget(btnForward, Qt::AlignLeft);
     titlebar()->addWidget(btnRefresh, Qt::AlignLeft);
 
+    m_fixSize->setCheckable(true);
+    m_fixSize->setChecked(false);
+    m_hideButtons->setCheckable(true);
+    m_hideButtons->setChecked(false);
+    m_menu->addAction(m_fixSize);
+    m_menu->addAction(m_hideButtons);
+    titlebar()->setMenu(m_menu);
+
     connect(btnBackward, &DToolButton::clicked, this, [&]()
     {
         if (m_widget)
@@ -65,6 +77,15 @@ MainWindow::MainWindow(QString szTitle,
             m_widget->refresh();
         }
     });
+
+    connect(m_fixSize, &QAction::triggered, this, [=]()
+    {
+        fixSize();
+    });
+    connect(m_hideButtons, &QAction::triggered, this, [=]()
+    {
+        hideButtons();
+    });
 }
 
 MainWindow::~MainWindow()
@@ -97,6 +118,42 @@ void MainWindow::setIcon(QString szIconPath)
     }
 }
 
+void MainWindow::fixSize()
+{
+    if(m_fixSize->isChecked())
+    {
+        setFixedSize(this->width(), this->height());    // setFixedSize() 等同于同时设置 MaximumSize 和 MinimumSize
+        resize(this->width(), this->height());
+
+        /*
+         * 尝试固定窗口大小后禁用最大化按钮,但是取消勾选后无法恢复
+         * titlebar()->setDisableFlags(Qt::WindowMaximizeButtonHint);
+         */
+    }
+    else
+    {
+        setMinimumSize(m_width, m_height);
+        setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
+        resize(this->width(), this->height());
+    }
+}
+
+void MainWindow::hideButtons()
+{
+    if(m_hideButtons->isChecked())
+    {
+        titlebar()->removeWidget(btnBackward);
+        titlebar()->removeWidget(btnForward);
+        titlebar()->removeWidget(btnRefresh);
+    }
+    else
+    {
+        titlebar()->addWidget(btnBackward, Qt::AlignLeft);
+        titlebar()->addWidget(btnForward, Qt::AlignLeft);
+        titlebar()->addWidget(btnRefresh, Qt::AlignLeft);
+    }
+}
+
 void MainWindow::closeEvent(QCloseEvent *event)
 {
     m_dialog->close();
diff --git a/spark-webapp-runtime/mainwindow.h b/spark-webapp-runtime/mainwindow.h
index 2e5fba4..892ff6f 100644
--- a/spark-webapp-runtime/mainwindow.h
+++ b/spark-webapp-runtime/mainwindow.h
@@ -3,6 +3,7 @@
 
 #include <DMainWindow>
 #include <DAboutDialog>
+#include <DToolButton>
 
 #include "widget.h"
 #include "globaldefine.h"
@@ -23,6 +24,8 @@ public:
     ~MainWindow();
 
     void setIcon(QString);
+    void fixSize();
+    void hideButtons();
 
 signals:
     void sigQuit();
@@ -31,6 +34,16 @@ private:
     Widget *m_widget;
     DAboutDialog *m_dialog;
 
+    DToolButton *btnBackward;
+    DToolButton *btnForward;
+    DToolButton *btnRefresh;
+
+    QMenu *m_menu;
+    QAction *m_fixSize;
+    QAction *m_hideButtons;
+
+    int m_width, m_height;
+
     void closeEvent(QCloseEvent *event);
 
 };
diff --git a/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.qm b/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.qm
index eddef58a8dcf83cf1e72461529a01ecbe49ebb43..35296ca170db2d6fe9343a734606bf427fe7b10f 100644
GIT binary patch
delta 269
zcmZqR{lq&#q<#j2>;^vu1}091rAx0e00Cp(Vm$^1W@g5%i&BBSLKeo|{}>opoLF3B
z@_^FQSq`rT@|iPPcOLQw$}_R<>bnP|H?w}c^NxXm)r^BV=_vyPn<NKE|3jeqB%YHC
z_<-V{cvXajfqZ>FId`D>Oc8vGx36PhsAt;Gcj4nXpatvsK8n2q8hoBVpg$R?VR|Oe
z6bELY7LKrA(X%4eWB56M90oR!h+AfbLU3kPDm#$F<(rt97oM4yl3&gURz7V)>-l8P
fDeK_Mc|0;xQWcy^OG@(dicz&Nl{0Q!bdMDPAwxvF

delta 155
zcmeyw+rT?Pq`reec7q=S1LJbWyv2G93``3dw=PNr^7&a9cmHExV1B^jB9q6!z+lR9
zcr}R6&bsrEKajtGbywd#pt?ZTk9XcNFtA+VU`~3<z`(klgQNc;P@aM3<N`h*e*&MJ
xJJ4LlZ+wfluLGJN%Xi`9IiPx9zK>$>7#R4ObNK`MlYt6NGl9gt%_5BVSOGQtD)s;X

diff --git a/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.ts b/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.ts
index c58f15a..cd25dbc 100644
--- a/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.ts
+++ b/spark-webapp-runtime/translations/spark-webapp-runtime_zh_CN.ts
@@ -1,6 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE TS>
 <TS version="2.1" language="zh_CN">
+<context>
+    <name>MainWindow</name>
+    <message>
+        <location filename="../mainwindow.cpp" line="23"/>
+        <source>Fix Size</source>
+        <translation>固定大小</translation>
+    </message>
+    <message>
+        <location filename="../mainwindow.cpp" line="24"/>
+        <source>Hide Buttons</source>
+        <translation>隐藏按钮</translation>
+    </message>
+</context>
 <context>
     <name>QObject</name>
     <message>