Improve Features

Support CommandLineParsers "--fix-size" and "--hide-buttons".
This commit is contained in:
忘记、过去 2020-11-21 22:25:20 +08:00
parent 7d82812fc9
commit ea511893ba
7 changed files with 117 additions and 69 deletions

@ -4,22 +4,24 @@
```shell ```shell
用法:./SparkWebAppRuntime [选项] 用法:./spark-webapp-runtime [选项]
描述: 星火网页应用运行时 描述: 星火网页应用运行时
选项: 选项:
-h, --help 显示这个此帮助。 -h, --help 显示这个此帮助。
-v, --version 显示版本信息。 -v, --version 显示版本信息。
-p, --parser 启用参数解析方式. 默认顺序解析方式. -p, --parser 启用参数解析方式。默认顺序解析方式。
-t, --title \<title\> 设置程序的运行标题. 默认是 星火网页应用运行时. -t, --title \<title\> 设置程序的运行标题。默认是 星火网页应用运行环境。
-u, --url \<url\> 设置要打开的目标URL. 默认是空. -u, --url \<url\> 设置要打开的目标 URL。默认为空。
-w, --width \<width\> 设置程序运行的宽度. 默认是 1024. -w, --width \<width\> 设置程序运行的宽度。默认为 1024。
-H, --height \<height\> 设置程序运行的高度. 默认是 768 -H, --height \<height\> 设置程序运行的高度。默认为 768。
-i, --ico \<ico\> 设置程序运行的图标. --fix-size 固定窗口大小。默认关闭该功能。
-d, --desc \<desc\> 设置程序的描述信息. --hide-buttons 隐藏控制按钮。默认关闭该功能。
-c, --cfg \<cfg\> 设置程序运行的配置文件. -i, --ico \<ico\> 设置程序运行的图标。
-r, --root \<root\> 设置内置WebServer的根路径. -d, --desc \<desc\> 设置程序的描述信息。
-P, --port \<port\> 设置内置WebServer的监听端口号. -c, --cfg \<cfg\> 设置程序运行的配置文件。
-r, --root \<root\> 设置内置 WebServer 的根路径。
-P, --port \<port\> 设置内置 WebServer 的监听端口号。
``` ```
这里给大家附送两个例子,帮助大家理解使用 这里给大家附送两个例子,帮助大家理解使用
@ -56,7 +58,7 @@
作为星火系列应用我们的设计哲学是M.I.S.(Make it Simple)没错我就是从KISS抄的 作为星火系列应用我们的设计哲学是M.I.S.(Make it Simple)没错我就是从KISS抄的
配置spark-webapp-runtime环境是非常简单的 配置 spark-webapp-runtime 环境是非常简单的
只需要 只需要

@ -28,6 +28,8 @@
-u, --url <url> 设置要打开的目标 URL。默认为空。 -u, --url <url> 设置要打开的目标 URL。默认为空。
-w, --width <width> 设置应用的窗口宽度。默认为 1024。 -w, --width <width> 设置应用的窗口宽度。默认为 1024。
-H, --height <height> 设置应用的窗口高度。默认为 768。 -H, --height <height> 设置应用的窗口高度。默认为 768。
--fix-size 固定窗口大小。默认关闭该功能。
--hide-buttons 隐藏控制按钮。默认关闭该功能。
-i, --ico <ico> 设置应用的图标。 -i, --ico <ico> 设置应用的图标。
-d, --desc <desc> 设置应用的描述信息。 -d, --desc <desc> 设置应用的描述信息。
-c, --cfg <cfg> 设置应用的配置文件。 -c, --cfg <cfg> 设置应用的配置文件。
@ -61,6 +63,8 @@
-u, --url <url> The target URL. Default is Blank. -u, --url <url> The target URL. Default is Blank.
-w, --width <width> The Width of Application. Default is 1024. -w, --width <width> The Width of Application. Default is 1024.
-H, --height <height> The Height of Application. Default is 768. -H, --height <height> The Height of Application. Default is 768.
--fix-size Fix Window Size. Default is false.
--hide-buttons Hide Control Buttons. Default is false.
-i, --ico <ico> The ICON of Application. -i, --ico <ico> The ICON of Application.
-d, --desc <desc> The Description of Application. -d, --desc <desc> The Description of Application.
-c, --cfg <cfg> The Configuration file of Application. -c, --cfg <cfg> The Configuration file of Application.

@ -8,7 +8,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <DApplication> #include <DApplication>
#include <DWidgetUtil>
#include <DMainWindow> #include <DMainWindow>
#include <QCommandLineParser> #include <QCommandLineParser>
@ -89,6 +88,14 @@ int main(int argc, char *argv[])
QString::number(DEFAULT_HEIGHT)); QString::number(DEFAULT_HEIGHT));
parser.addOption(optHeight); parser.addOption(optHeight);
QCommandLineOption optFixSize("fix-size",
QObject::tr("Fix Window Size. Default is false."));
parser.addOption(optFixSize);
QCommandLineOption optHideButtons("hide-buttons",
QObject::tr("Hide Control Buttons. Default is false."));
parser.addOption(optHideButtons);
QCommandLineOption optIcon(QStringList() << "i" << "ico", QCommandLineOption optIcon(QStringList() << "i" << "ico",
QObject::tr("The ICON of Application."), QObject::tr("The ICON of Application."),
"ico", "ico",
@ -134,6 +141,8 @@ int main(int argc, char *argv[])
QString szUrl = DEFAULT_URL; QString szUrl = DEFAULT_URL;
int width = DEFAULT_WIDTH; int width = DEFAULT_WIDTH;
int height = DEFAULT_HEIGHT; int height = DEFAULT_HEIGHT;
bool fixSize = false;
bool hideButtons = false;
QString szIcon = DEFAULT_ICON; QString szIcon = DEFAULT_ICON;
QString szDesc = DEFAULT_DESC; QString szDesc = DEFAULT_DESC;
QString szRootPath = DEFAULT_ROOT; QString szRootPath = DEFAULT_ROOT;
@ -152,18 +161,20 @@ int main(int argc, char *argv[])
if (fi.exists()) if (fi.exists())
{ {
QSettings settings(szCfgFile, QSettings::IniFormat); QSettings settings(szCfgFile, QSettings::IniFormat);
szTitle = settings.value("SpartWebAppRuntime/Title", DEFAULT_TITLE).toString(); szTitle = settings.value("SparkWebAppRuntime/Title", DEFAULT_TITLE).toString();
szUrl = settings.value("SpartWebAppRuntime/URL", DEFAULT_TITLE).toString(); szUrl = settings.value("SparkWebAppRuntime/URL", DEFAULT_TITLE).toString();
width = settings.value("SpartWebAppRuntime/Width", DEFAULT_WIDTH).toUInt(); width = settings.value("SparkWebAppRuntime/Width", DEFAULT_WIDTH).toUInt();
height = settings.value("SpartWebAppRuntime/Height", DEFAULT_HEIGHT).toUInt(); height = settings.value("SparkWebAppRuntime/Height", DEFAULT_HEIGHT).toUInt();
szIcon = settings.value("SpartWebAppRuntime/Ico", DEFAULT_ICON).toString(); fixSize = settings.value("SparkWebAppRunTime/FixSize", false).toBool();
hideButtons = settings.value("SparkWebAppRunTime/HideButtons", false).toBool();
szIcon = settings.value("SparkWebAppRuntime/Ico", DEFAULT_ICON).toString();
szDesc = QString("%1<br/><br/>%2") szDesc = QString("%1<br/><br/>%2")
.arg(settings.value("SpartWebAppRuntime/Desc", QString()).toString()) .arg(settings.value("SparkWebAppRuntime/Desc", QString()).toString())
.arg(szDefaultDesc); .arg(szDefaultDesc);
szRootPath = settings.value("SpartWebAppRuntime/RootPath", QString()).toString(); szRootPath = settings.value("SparkWebAppRuntime/RootPath", QString()).toString();
u16Port = settings.value("SpartWebAppRuntime/Port", 0).toUInt(); u16Port = settings.value("SparkWebAppRuntime/Port", 0).toUInt();
#if SSL_SERVER #if SSL_SERVER
u16sslPort = settings.value("SpartWebAppRuntime/SSLPort", 0).toUInt(); u16sslPort = settings.value("SparkWebAppRuntime/SSLPort", 0).toUInt();
#endif #endif
} }
} }
@ -187,6 +198,15 @@ int main(int argc, char *argv[])
height = parser.value(optHeight).toInt(); height = parser.value(optHeight).toInt();
} }
if (parser.isSet(optFixSize))
{
fixSize = true;
}
if (parser.isSet(optHideButtons))
{
hideButtons = true;
}
if (parser.isSet(optDesc)) if (parser.isSet(optDesc))
{ {
szDesc = QString("%1<br/><br/>%2").arg(parser.value(optDesc)) szDesc = QString("%1<br/><br/>%2").arg(parser.value(optDesc))
@ -230,30 +250,41 @@ int main(int argc, char *argv[])
{ {
height = QString(argv[4]).toInt(); height = QString(argv[4]).toInt();
} }
if (argc > 5) if (argc > 5)
{ {
szIcon = QString(argv[5]); fixSize = true;
} }
if (argc > 6) if (argc > 6)
szDesc = QString("%1<br/><br/>%2").arg(QString(argv[6])) {
.arg(szDefaultDesc);; hideButtons = true;
}
if (argc > 7) if (argc > 7)
{ {
szRootPath = QString(argv[7]); szIcon = QString(argv[7]);
} }
if (argc > 8) if (argc > 8)
{ {
u16Port = QString(argv[8]).toUInt(); szDesc = QString("%1<br/><br/>%2").arg(QString(argv[8])).arg(szDefaultDesc);
} }
#if SSL_SERVER
if (argc > 9) if (argc > 9)
{ {
u16sslPort = QString(argv[9]).toUInt(); szRootPath = QString(argv[9]);
}
if (argc > 10)
{
u16Port = QString(argv[10]).toUInt();
}
#if SSL_SERVER
if (argc > 11)
{
u16sslPort = QString(argv[11]).toUInt();
} }
#endif #endif
} }
MainWindow w(szTitle, szUrl, width, height, dialog); MainWindow w(szTitle, szUrl, width, height, fixSize, hideButtons, dialog);
#if SSL_SERVER #if SSL_SERVER
if (!szRootPath.isEmpty() && u16Port > 0 && u16sslPort > 0) if (!szRootPath.isEmpty() && u16Port > 0 && u16sslPort > 0)
@ -287,7 +318,5 @@ int main(int argc, char *argv[])
} }
w.show(); w.show();
Dtk::Widget::moveToCenter(&w);
return a.exec(); return a.exec();
} }

@ -1,6 +1,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <DMainWindow> #include <DMainWindow>
#include <DWidgetUtil>
#include <DTitlebar> #include <DTitlebar>
#include <QLayout> #include <QLayout>
@ -11,6 +12,8 @@ MainWindow::MainWindow(QString szTitle,
QString szUrl, QString szUrl,
int nWidth, int nWidth,
int nHeight, int nHeight,
bool nFixSize,
bool nHideButtons,
DAboutDialog *dialog, DAboutDialog *dialog,
QWidget *parent) QWidget *parent)
: DMainWindow(parent) : DMainWindow(parent)
@ -25,13 +28,13 @@ MainWindow::MainWindow(QString szTitle,
, m_width(nWidth) , m_width(nWidth)
, m_height(nHeight) , m_height(nHeight)
{ {
// setFixedSize(nWidth, nHeight);
// 应 shenmo 要求改成设置最小尺寸试试效果
setMinimumSize(m_width, m_height);
setCentralWidget(m_widget); setCentralWidget(m_widget);
centralWidget()->layout()->setContentsMargins(0, 0, 0, 0); centralWidget()->layout()->setContentsMargins(0, 0, 0, 0);
resize(m_width, m_height);
moveToCenter(this);
setWindowIcon(QIcon(":/images/spark-webapp-runtime.svg")); setWindowIcon(QIcon(":/images/spark-webapp-runtime.svg"));
titlebar()->setTitle(szTitle); titlebar()->setTitle(szTitle);
@ -49,13 +52,18 @@ MainWindow::MainWindow(QString szTitle,
titlebar()->addWidget(btnRefresh, Qt::AlignLeft); titlebar()->addWidget(btnRefresh, Qt::AlignLeft);
m_fixSize->setCheckable(true); m_fixSize->setCheckable(true);
m_fixSize->setChecked(false); m_fixSize->setChecked(nFixSize);
m_fixSize->setDisabled(nFixSize); // 建议使用命令行参数 --fix-size 或者 --hide-buttons 时直接禁止在 GUI 修改选项,主要因为最大化按钮无法刷新存在状态,干脆都禁用了......
m_hideButtons->setCheckable(true); m_hideButtons->setCheckable(true);
m_hideButtons->setChecked(false); m_hideButtons->setChecked(nHideButtons);
m_hideButtons->setDisabled(nHideButtons);
m_menu->addAction(m_fixSize); m_menu->addAction(m_fixSize);
m_menu->addAction(m_hideButtons); m_menu->addAction(m_hideButtons);
titlebar()->setMenu(m_menu); titlebar()->setMenu(m_menu);
fixSize();
hideButtons();
connect(btnBackward, &DToolButton::clicked, this, [&]() connect(btnBackward, &DToolButton::clicked, this, [&]()
{ {
if (m_widget) if (m_widget)
@ -122,19 +130,12 @@ void MainWindow::fixSize()
{ {
if(m_fixSize->isChecked()) if(m_fixSize->isChecked())
{ {
setFixedSize(this->width(), this->height()); // setFixedSize() 等同于同时设置 MaximumSize 和 MinimumSize setFixedSize(this->width(), this->height());
resize(this->width(), this->height());
/*
*
* titlebar()->setDisableFlags(Qt::WindowMaximizeButtonHint);
*/
} }
else else
{ {
setMinimumSize(m_width, m_height); setMinimumSize(m_width, m_height);
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
resize(this->width(), this->height());
} }
} }
@ -142,15 +143,15 @@ void MainWindow::hideButtons()
{ {
if(m_hideButtons->isChecked()) if(m_hideButtons->isChecked())
{ {
titlebar()->removeWidget(btnBackward); btnBackward->hide();
titlebar()->removeWidget(btnForward); btnForward->hide();
titlebar()->removeWidget(btnRefresh); btnRefresh->hide();
} }
else else
{ {
titlebar()->addWidget(btnBackward, Qt::AlignLeft); btnBackward->show();
titlebar()->addWidget(btnForward, Qt::AlignLeft); btnForward->show();
titlebar()->addWidget(btnRefresh, Qt::AlignLeft); btnRefresh->show();
} }
} }

@ -19,6 +19,8 @@ public:
QString szUrl = DEFAULT_URL, QString szUrl = DEFAULT_URL,
int nWidth = DEFAULT_WIDTH, int nWidth = DEFAULT_WIDTH,
int nHeight = DEFAULT_HEIGHT, int nHeight = DEFAULT_HEIGHT,
bool nFixSize = false,
bool nHideButtons = false,
DAboutDialog *dialog = nullptr, DAboutDialog *dialog = nullptr,
QWidget *parent = nullptr); QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();

@ -4,12 +4,12 @@
<context> <context>
<name>MainWindow</name> <name>MainWindow</name>
<message> <message>
<location filename="../mainwindow.cpp" line="23"/> <location filename="../mainwindow.cpp" line="28"/>
<source>Fix Size</source> <source>Fix Size</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="24"/> <location filename="../mainwindow.cpp" line="29"/>
<source>Hide Buttons</source> <source>Hide Buttons</source>
<translation></translation> <translation></translation>
</message> </message>
@ -17,77 +17,87 @@
<context> <context>
<name>QObject</name> <name>QObject</name>
<message> <message>
<location filename="../main.cpp" line="49"/> <location filename="../main.cpp" line="48"/>
<source>Presented By Spark developers # HadesStudio</source> <source>Presented By Spark developers # HadesStudio</source>
<translation> @ </translation> <translation> @ </translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="56"/> <location filename="../main.cpp" line="55"/>
<source>This program is open source under GPLv3</source> <source>This program is open source under GPLv3</source>
<translation>GPL第三版开源</translation> <translation>GPL第三版开源</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="42"/> <location filename="../main.cpp" line="41"/>
<source>Version</source> <source>Version</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="60"/> <location filename="../main.cpp" line="59"/>
<source>Description: %1</source> <source>Description: %1</source>
<translation>%1</translation> <translation>%1</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="65"/> <location filename="../main.cpp" line="64"/>
<source>Enable CommandLineParser. Default is false.</source> <source>Enable CommandLineParser. Default is false.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="69"/> <location filename="../main.cpp" line="68"/>
<source>The Title of Application. Default is %1.</source> <source>The Title of Application. Default is %1.</source>
<translation> %1</translation> <translation> %1</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="75"/> <location filename="../main.cpp" line="74"/>
<source>The target URL. Default is Blank.</source> <source>The target URL. Default is Blank.</source>
<translation> URL</translation> <translation> URL</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="81"/> <location filename="../main.cpp" line="80"/>
<source>The Width of Application. Default is %1.</source> <source>The Width of Application. Default is %1.</source>
<translation> %1</translation> <translation> %1</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="87"/> <location filename="../main.cpp" line="86"/>
<source>The Height of Application. Default is %1.</source> <source>The Height of Application. Default is %1.</source>
<translation> %1</translation> <translation> %1</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="93"/> <location filename="../main.cpp" line="92"/>
<source>Fix Window Size. Default is false.</source>
<translation></translation>
</message>
<message>
<location filename="../main.cpp" line="96"/>
<source>Hide Control Buttons. Default is false.</source>
<translation></translation>
</message>
<message>
<location filename="../main.cpp" line="100"/>
<source>The ICON of Application.</source> <source>The ICON of Application.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="99"/> <location filename="../main.cpp" line="106"/>
<source>The Description of Application.</source> <source>The Description of Application.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="105"/> <location filename="../main.cpp" line="112"/>
<source>The Configuration file of Application.</source> <source>The Configuration file of Application.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="111"/> <location filename="../main.cpp" line="118"/>
<source>The root path of the program web service.</source> <source>The root path of the program web service.</source>
<translation> WebServer </translation> <translation> WebServer </translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="118"/> <location filename="../main.cpp" line="125"/>
<source>The port number of the program web service.</source> <source>The port number of the program web service.</source>
<translation> WebServer </translation> <translation> WebServer </translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="125"/> <location filename="../main.cpp" line="132"/>
<source>The ssl port number of the program web service.</source> <source>The ssl port number of the program web service.</source>
<translation> WebServer SSL </translation> <translation> WebServer SSL </translation>
</message> </message>