fix: 修复退出全屏模式后菜单状态不同步的问题

重载 resizeEvent,根据窗口状态手动同步菜单状态。

Log: 修复退出全屏模式后菜单状态不同步的问题
This commit is contained in:
ut003880 2021-07-26 15:16:15 +08:00
parent 2f38b9dd76
commit 2453752bd7
6 changed files with 195 additions and 246 deletions

@ -3,18 +3,19 @@
#include <QObject> #include <QObject>
#define DEFAULT_TITLE QObject::tr("SparkWebAppRuntime") #define DEFAULT_TITLE QObject::tr("SparkWebAppRuntime")
#define DEFAULT_URL QString("qrc:/help/help.html") //#define DEFAULT_URL QString("qrc:/help/help.html")
#define DEFAULT_WIDTH (1024) #define DEFAULT_URL QString("https://www.baidu.com")
#define DEFAULT_HEIGHT (768) #define DEFAULT_WIDTH (1024)
#define DEFAULT_HEIGHT (768)
#define DEFAULT_DESC QString() #define DEFAULT_DESC QString()
#define DEFAULT_ICON QString() #define DEFAULT_ICON QString()
#define DEFAULT_CFG QString() #define DEFAULT_CFG QString()
#define DEFAULT_ROOT QString() #define DEFAULT_ROOT QString()
#define DEFAULT_PORT 0 #define DEFAULT_PORT 0
#define CURRENT_VER (1.6) #define CURRENT_VER (1.6)
#endif // GLOBALDEFINE_H #endif // GLOBALDEFINE_H

@ -22,7 +22,7 @@ int main(int argc, char *argv[])
a.setAttribute(Qt::AA_UseHighDpiPixmaps); a.setAttribute(Qt::AA_UseHighDpiPixmaps);
a.setApplicationVersion(QString::number(CURRENT_VER)); a.setApplicationVersion(QString::number(CURRENT_VER));
a.setOrganizationName("spark-union"); // 添加组织名称,和商店主体的文件夹同在 ~/.local/share/spark-union 文件夹下 a.setOrganizationName("spark-union"); // 添加组织名称,和商店主体的文件夹同在 ~/.local/share/spark-union 文件夹下
a.setApplicationName("SparkWebAppRuntime"); // 这里不要翻译,否则 ~/.local/share 中文件夹名也会被翻译 a.setApplicationName("SparkWebAppRuntime"); // 这里不要翻译,否则 ~/.local/share 中文件夹名也会被翻译
DAboutDialog *dialog = new DAboutDialog; DAboutDialog *dialog = new DAboutDialog;
@ -41,15 +41,15 @@ int main(int argc, char *argv[])
// Description // Description
QString szDefaultDesc = QString("<a href='https://gitee.com/deepin-community-store/spark-web-app-runtime'><span style='font-size:12pt;font-weight:500;'>%1</span></a><br/>" QString szDefaultDesc = QString("<a href='https://gitee.com/deepin-community-store/spark-web-app-runtime'><span style='font-size:12pt;font-weight:500;'>%1</span></a><br/>"
"<span style='font-size:12pt;'>%2</span>") "<span style='font-size:12pt;'>%2</span>")
.arg(DEFAULT_TITLE) .arg(DEFAULT_TITLE)
.arg(QObject::tr("Presented By Spark developers # HadesStudio")); .arg(QObject::tr("Presented By Spark developers # HadesStudio"));
dialog->setDescription(szDefaultDesc); dialog->setDescription(szDefaultDesc);
// WebsiteName // WebsiteName
dialog->setWebsiteName("Spark Project"); dialog->setWebsiteName("Spark Project");
// WebsiteLink // WebsiteLink
dialog->setWebsiteLink("https://gitee.com/deepin-community-store/"); dialog->setWebsiteLink("https://gitee.com/deepin-community-store/");
// License // License
dialog->setLicense(QObject::tr("This program is open source under GPLv3")); // 本程序按GPL第三版开源 dialog->setLicense(QObject::tr("This program is open source under GPLv3")); // 本程序按GPL第三版开源
QCommandLineParser parser; QCommandLineParser parser;
@ -57,40 +57,46 @@ int main(int argc, char *argv[])
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
QCommandLineOption optParser(QStringList() << "p" << "parser", QCommandLineOption optParser(QStringList() << "p"
<< "parser",
QObject::tr("Enable CommandLineParser. Default is false.")); QObject::tr("Enable CommandLineParser. Default is false."));
parser.addOption(optParser); parser.addOption(optParser);
QCommandLineOption optTitle(QStringList() << "t" << "title", QCommandLineOption optTitle(QStringList() << "t"
<< "title",
QObject::tr("The Title of Application. Default is %1.").arg(DEFAULT_TITLE), QObject::tr("The Title of Application. Default is %1.").arg(DEFAULT_TITLE),
"title", "title",
DEFAULT_TITLE); DEFAULT_TITLE);
parser.addOption(optTitle); parser.addOption(optTitle);
QCommandLineOption optUrl(QStringList() << "u" << "url", QCommandLineOption optUrl(QStringList() << "u"
<< "url",
QObject::tr("The target URL. Default is Blank."), QObject::tr("The target URL. Default is Blank."),
"url", "url",
DEFAULT_URL); DEFAULT_URL);
parser.addOption(optUrl); parser.addOption(optUrl);
QCommandLineOption optWidth(QStringList() << "w" << "width", QCommandLineOption optWidth(QStringList() << "w"
<< "width",
QObject::tr("The Width of Application. Default is %1.").arg(DEFAULT_WIDTH), QObject::tr("The Width of Application. Default is %1.").arg(DEFAULT_WIDTH),
"width", "width",
QString::number(DEFAULT_WIDTH)); QString::number(DEFAULT_WIDTH));
parser.addOption(optWidth); parser.addOption(optWidth);
QCommandLineOption optHeight(QStringList() << "H" << "height", QCommandLineOption optHeight(QStringList() << "H"
<< "height",
QObject::tr("The Height of Application. Default is %1.").arg(DEFAULT_HEIGHT), QObject::tr("The Height of Application. Default is %1.").arg(DEFAULT_HEIGHT),
"height", "height",
QString::number(DEFAULT_HEIGHT)); QString::number(DEFAULT_HEIGHT));
parser.addOption(optHeight); parser.addOption(optHeight);
QCommandLineOption optTray(QStringList() << "T" << "tray", QCommandLineOption optTray(QStringList() << "T"
QObject::tr("Enable Tray Icon. Default is false.")); << "tray",
QObject::tr("Enable Tray Icon. Default is false."));
parser.addOption(optTray); parser.addOption(optTray);
QCommandLineOption optFullScreen("full-screen", QCommandLineOption optFullScreen("full-screen",
QObject::tr("Run in Fullscreen Mode. Default is false.")); QObject::tr("Run in Fullscreen Mode. Default is false."));
parser.addOption(optFullScreen); parser.addOption(optFullScreen);
QCommandLineOption optFixSize("fix-size", QCommandLineOption optFixSize("fix-size",
@ -98,42 +104,47 @@ int main(int argc, char *argv[])
parser.addOption(optFixSize); parser.addOption(optFixSize);
QCommandLineOption optHideButtons("hide-buttons", QCommandLineOption optHideButtons("hide-buttons",
QObject::tr("Hide Control Buttons. Default is false.")); QObject::tr("Hide Control Buttons. Default is false."));
parser.addOption(optHideButtons); 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",
DEFAULT_ICON); DEFAULT_ICON);
parser.addOption(optIcon); parser.addOption(optIcon);
QCommandLineOption optDesc(QStringList() << "d" << "desc", QCommandLineOption optDesc(QStringList() << "d"
<< "desc",
QObject::tr("The Description of Application."), QObject::tr("The Description of Application."),
"desc", "desc",
DEFAULT_DESC); DEFAULT_DESC);
parser.addOption(optDesc); parser.addOption(optDesc);
QCommandLineOption optCfgFile(QStringList() << "c" << "cfg", QCommandLineOption optCfgFile(QStringList() << "c"
<< "cfg",
QObject::tr("The Configuration file of Application."), QObject::tr("The Configuration file of Application."),
"cfg", "cfg",
DEFAULT_CFG); DEFAULT_CFG);
parser.addOption(optCfgFile); parser.addOption(optCfgFile);
QCommandLineOption optRootPath(QStringList() << "r" << "root", QCommandLineOption optRootPath(QStringList() << "r"
<< "root",
QObject::tr("The root path of the program web service."), QObject::tr("The root path of the program web service."),
"root", "root",
DEFAULT_ROOT); DEFAULT_ROOT);
parser.addOption(optRootPath); parser.addOption(optRootPath);
QCommandLineOption optPort(QStringList() << "P"
QCommandLineOption optPort(QStringList() << "P" << "port", << "port",
QObject::tr("The port number of the program web service."), QObject::tr("The port number of the program web service."),
"port", "port",
DEFAULT_PORT); DEFAULT_PORT);
parser.addOption(optPort); parser.addOption(optPort);
#if SSL_SERVER #if SSL_SERVER
QCommandLineOption optSSLPort(QStringList() << "s" << "sslport", QCommandLineOption optSSLPort(QStringList() << "s"
<< "sslport",
QObject::tr("The ssl port number of the program web service."), QObject::tr("The ssl port number of the program web service."),
"sslport", "sslport",
DEFAULT_PORT); DEFAULT_PORT);
@ -159,13 +170,10 @@ int main(int argc, char *argv[])
#endif #endif
QString szCfgFile = DEFAULT_CFG; QString szCfgFile = DEFAULT_CFG;
if (parser.isSet(optCfgFile)) if (parser.isSet(optCfgFile)) {
{
szCfgFile = parser.value(optCfgFile); szCfgFile = parser.value(optCfgFile);
if (!szCfgFile.isEmpty()) if (!szCfgFile.isEmpty()) {
{ if (QFileInfo(szCfgFile).exists()) {
if (QFileInfo(szCfgFile).exists())
{
QSettings settings(szCfgFile, QSettings::IniFormat); QSettings settings(szCfgFile, QSettings::IniFormat);
szTitle = settings.value("SparkWebAppRuntime/Title", DEFAULT_TITLE).toString(); szTitle = settings.value("SparkWebAppRuntime/Title", DEFAULT_TITLE).toString();
szUrl = settings.value("SparkWebAppRuntime/URL", DEFAULT_TITLE).toString(); szUrl = settings.value("SparkWebAppRuntime/URL", DEFAULT_TITLE).toString();
@ -177,8 +185,8 @@ int main(int argc, char *argv[])
hideButtons = settings.value("SparkWebAppRunTime/HideButtons", false).toBool(); hideButtons = settings.value("SparkWebAppRunTime/HideButtons", false).toBool();
szIcon = settings.value("SparkWebAppRuntime/Ico", DEFAULT_ICON).toString(); szIcon = settings.value("SparkWebAppRuntime/Ico", DEFAULT_ICON).toString();
szDesc = QString("%1<br/><br/>%2") szDesc = QString("%1<br/><br/>%2")
.arg(settings.value("SparkWebAppRuntime/Desc", QString()).toString()) .arg(settings.value("SparkWebAppRuntime/Desc", QString()).toString())
.arg(szDefaultDesc); .arg(szDefaultDesc);
szRootPath = settings.value("SparkWebAppRuntime/RootPath", QString()).toString(); szRootPath = settings.value("SparkWebAppRuntime/RootPath", QString()).toString();
u16Port = settings.value("SparkWebAppRuntime/Port", 0).toUInt(); u16Port = settings.value("SparkWebAppRuntime/Port", 0).toUInt();
#if SSL_SERVER #if SSL_SERVER
@ -189,162 +197,130 @@ int main(int argc, char *argv[])
} }
// 命令行级别优先, 覆盖配置文件的设置 // 命令行级别优先, 覆盖配置文件的设置
if (parser.isSet(optTitle)) if (parser.isSet(optTitle)) {
{
szTitle = parser.value(optTitle); szTitle = parser.value(optTitle);
} }
if (parser.isSet(optUrl)) if (parser.isSet(optUrl)) {
{
szUrl = parser.value(optUrl); szUrl = parser.value(optUrl);
} }
if (parser.isSet(optWidth)) if (parser.isSet(optWidth)) {
{
width = parser.value(optWidth).toInt(); width = parser.value(optWidth).toInt();
} }
if (parser.isSet(optHeight)) if (parser.isSet(optHeight)) {
{
height = parser.value(optHeight).toInt(); height = parser.value(optHeight).toInt();
} }
if (parser.isSet(optTray)) if (parser.isSet(optTray)) {
{
tray = true; tray = true;
} }
if (parser.isSet(optFullScreen)) if (parser.isSet(optFullScreen)) {
{
fullScreen = true; fullScreen = true;
} }
if (parser.isSet(optFixSize)) if (parser.isSet(optFixSize)) {
{
fixSize = true; fixSize = true;
} }
if (parser.isSet(optHideButtons)) if (parser.isSet(optHideButtons)) {
{
hideButtons = true; hideButtons = true;
} }
if (parser.isSet(optDesc)) if (parser.isSet(optDesc)) {
{ szDesc = QString("%1<br/><br/>%2").arg(parser.value(optDesc)).arg(szDefaultDesc);
szDesc = QString("%1<br/><br/>%2").arg(parser.value(optDesc))
.arg(szDefaultDesc);
} }
if (parser.isSet(optRootPath)) if (parser.isSet(optRootPath)) {
{
szRootPath = parser.value(optRootPath); szRootPath = parser.value(optRootPath);
} }
if (parser.isSet(optPort)) if (parser.isSet(optPort)) {
{
u16Port = parser.value(optPort).toUInt(); u16Port = parser.value(optPort).toUInt();
} }
#if SSL_SERVER #if SSL_SERVER
if (parser.isSet(optSSLPort)) if (parser.isSet(optSSLPort)) {
{
u16sslPort = parser.value(optSSLPort).toUInt(); u16sslPort = parser.value(optSSLPort).toUInt();
} }
#endif #endif
// 没设置 -p 并且参数个数 > 1 并且第一个参数不是 - 开始的 // 没设置 -p 并且参数个数 > 1 并且第一个参数不是 - 开始的
if (!parser.isSet(optParser) && argc > 1 && !QString(argv[1]).startsWith("-")) if (!parser.isSet(optParser) && argc > 1 && !QString(argv[1]).startsWith("-")) {
{
// 按照固定顺序级别最优先 // 按照固定顺序级别最优先
if (argc > 1) if (argc > 1) {
{
szTitle = argv[1]; szTitle = argv[1];
} }
if (argc > 2) if (argc > 2) {
{
szUrl = argv[2]; szUrl = argv[2];
} }
if (argc > 3) if (argc > 3) {
{
width = QString(argv[3]).toInt(); width = QString(argv[3]).toInt();
} }
if (argc > 4) if (argc > 4) {
{
height = QString(argv[4]).toInt(); height = QString(argv[4]).toInt();
} }
if (argc > 5) if (argc > 5) {
{
tray = true; tray = true;
} }
if (argc > 6) if (argc > 6) {
{
fullScreen = true; fullScreen = true;
} }
if (argc > 7) if (argc > 7) {
{
fixSize = true; fixSize = true;
} }
if (argc > 8) if (argc > 8) {
{
hideButtons = true; hideButtons = true;
} }
if (argc > 9) if (argc > 9) {
{
szIcon = QString(argv[9]); szIcon = QString(argv[9]);
} }
if (argc > 10) if (argc > 10) {
{
szDesc = QString("%1<br/><br/>%2").arg(QString(argv[10])).arg(szDefaultDesc); szDesc = QString("%1<br/><br/>%2").arg(QString(argv[10])).arg(szDefaultDesc);
} }
if (argc > 11) if (argc > 11) {
{
szRootPath = QString(argv[11]); szRootPath = QString(argv[11]);
} }
if (argc > 12) if (argc > 12) {
{
u16Port = QString(argv[12]).toUInt(); u16Port = QString(argv[12]).toUInt();
} }
#if SSL_SERVER #if SSL_SERVER
if (argc > 13) if (argc > 13) {
{
u16sslPort = QString(argv[13]).toUInt(); u16sslPort = QString(argv[13]).toUInt();
} }
#endif #endif
} }
if(fixSize) if (fixSize) {
{ fullScreen = false; // 固定窗口大小时禁用全屏模式,避免标题栏按钮 BUG
fullScreen = false; // 固定窗口大小时禁用全屏模式,避免标题栏按钮 BUG
} }
a.setQuitOnLastWindowClosed(!tray); // 启用托盘时,退出程序后服务不终止 a.setQuitOnLastWindowClosed(!tray); // 启用托盘时,退出程序后服务不终止
MainWindow w(szTitle, szUrl, width, height, tray, fullScreen, fixSize, hideButtons, dialog); MainWindow w(szTitle, szUrl, width, height, tray, fullScreen, fixSize, hideButtons, dialog);
// qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-features=UseModernMediaControls");
#if SSL_SERVER #if SSL_SERVER
if (!szRootPath.isEmpty() && u16Port > 0 && u16sslPort > 0) if (!szRootPath.isEmpty() && u16Port > 0 && u16sslPort > 0) {
{
HttpD httpd(szRootPath, u16Port, u16sslPort); HttpD httpd(szRootPath, u16Port, u16sslPort);
httpd.start(); httpd.start();
} }
#else #else
if (!szRootPath.isEmpty() && u16Port > 0) if (!szRootPath.isEmpty() && u16Port > 0) {
{
static HttpD httpd(szRootPath, u16Port); static HttpD httpd(szRootPath, u16Port);
QObject::connect(&w, &MainWindow::sigQuit, &httpd, &HttpD::stop); QObject::connect(&w, &MainWindow::sigQuit, &httpd, &HttpD::stop);
httpd.start(); httpd.start();
} }
#endif #endif
if (parser.isSet(optIcon)) if (parser.isSet(optIcon)) {
{
szIcon = parser.value(optIcon); szIcon = parser.value(optIcon);
} }
if (!szIcon.isEmpty()) if (!szIcon.isEmpty()) {
{
dialog->setWindowIcon(QIcon(szIcon)); dialog->setWindowIcon(QIcon(szIcon));
dialog->setProductIcon(QIcon(szIcon)); dialog->setProductIcon(QIcon(szIcon));
w.setIcon(szIcon); w.setIcon(szIcon);
} }
if (!szDesc.isEmpty()) if (!szDesc.isEmpty()) {
{
dialog->setDescription(szDesc); dialog->setDescription(szDesc);
} }

@ -76,7 +76,7 @@ MainWindow::MainWindow(QString szTitle,
m_fullScreen->setCheckable(true); m_fullScreen->setCheckable(true);
m_fullScreen->setChecked(nFullScreen); m_fullScreen->setChecked(nFullScreen);
m_fullScreen->setDisabled(nFixSize); // 固定窗口大小时禁用全屏模式,避免标题栏按钮 BUG m_fullScreen->setDisabled(nFixSize); // 固定窗口大小时禁用全屏模式,避免标题栏按钮 BUG
m_fixSize->setCheckable(true); m_fixSize->setCheckable(true);
m_fixSize->setChecked(nFixSize); m_fixSize->setChecked(nFixSize);
m_fixSize->setDisabled(nFixSize); m_fixSize->setDisabled(nFixSize);
@ -84,13 +84,11 @@ MainWindow::MainWindow(QString szTitle,
m_hideButtons->setChecked(nHideButtons); m_hideButtons->setChecked(nHideButtons);
m_hideButtons->setDisabled(nHideButtons); m_hideButtons->setDisabled(nHideButtons);
/* 命令行设置参数后 GUI 中隐藏对应选项 */ /* 命令行设置参数后 GUI 中隐藏对应选项 */
if(!nFixSize) if (!nFixSize) {
{
m_menu->addAction(m_fullScreen); m_menu->addAction(m_fullScreen);
m_menu->addAction(m_fixSize); m_menu->addAction(m_fixSize);
} }
if(!nHideButtons) if (!nHideButtons) {
{
m_menu->addAction(m_hideButtons); m_menu->addAction(m_hideButtons);
} }
titlebar()->setMenu(m_menu); titlebar()->setMenu(m_menu);
@ -108,8 +106,7 @@ MainWindow::MainWindow(QString szTitle,
m_tray->setToolTip(szTitle); m_tray->setToolTip(szTitle);
m_tray->setIcon(QIcon(":/images/spark-webapp-runtime.svg")); m_tray->setIcon(QIcon(":/images/spark-webapp-runtime.svg"));
if(tray) if (tray) {
{
m_tray->show(); // 启用托盘时显示 m_tray->show(); // 启用托盘时显示
} }
@ -124,52 +121,42 @@ MainWindow::MainWindow(QString szTitle,
message->setIcon(QIcon::fromTheme("deepin-download").pixmap(64, 64)); message->setIcon(QIcon::fromTheme("deepin-download").pixmap(64, 64));
message->setWidget(downloadProgressBar); message->setWidget(downloadProgressBar);
connect(btnBack, &DToolButton::clicked, this, [&]() connect(btnBack, &DToolButton::clicked, this, [&]() {
{
m_widget->goBack(); m_widget->goBack();
}); });
connect(btnForward, &DToolButton::clicked, this, [&]() connect(btnForward, &DToolButton::clicked, this, [&]() {
{
m_widget->goForward(); m_widget->goForward();
}); });
connect(btnRefresh, &DToolButton::clicked, this, [&]() connect(btnRefresh, &DToolButton::clicked, this, [&]() {
{
m_widget->refresh(); m_widget->refresh();
}); });
connect(m_fullScreen, &QAction::triggered, this, [=]() connect(m_fullScreen, &QAction::triggered, this, [=]() {
{
fullScreen(); fullScreen();
}); });
connect(m_fixSize, &QAction::triggered, this, [=]() connect(m_fixSize, &QAction::triggered, this, [=]() {
{
fixSize(); fixSize();
}); });
connect(m_hideButtons, &QAction::triggered, this, [=]() connect(m_hideButtons, &QAction::triggered, this, [=]() {
{
hideButtons(); hideButtons();
}); });
connect(t_show, &QAction::triggered, this, [=]() connect(t_show, &QAction::triggered, this, [=]() {
{
this->activateWindow(); this->activateWindow();
fixSize(); fixSize();
}); });
connect(t_about, &QAction::triggered, this, [=]() connect(t_about, &QAction::triggered, this, [=]() {
{
m_dialog->activateWindow(); m_dialog->activateWindow();
m_dialog->show(); m_dialog->show();
}); });
connect(t_exit, &QAction::triggered, this, [=]() connect(t_exit, &QAction::triggered, this, [=]() {
{
exit(0); exit(0);
}); });
connect(m_tray, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated); connect(m_tray, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated);
connect(m_widget->getPage()->profile(), &QWebEngineProfile::downloadRequested, this, &MainWindow::on_downloadStart); connect(m_widget->getPage()->profile(), &QWebEngineProfile::downloadRequested, this, &MainWindow::on_downloadStart);
connect(m_widget->getPage(), &QWebEnginePage::windowCloseRequested, this, [=]() connect(m_widget->getPage(), &QWebEnginePage::windowCloseRequested, this, [=]() {
{
this->close(); this->close();
}); });
} }
@ -184,29 +171,58 @@ MainWindow::~MainWindow()
void MainWindow::setIcon(QString szIconPath) void MainWindow::setIcon(QString szIconPath)
{ {
if (QFileInfo(szIconPath).exists()) if (QFileInfo(szIconPath).exists()) {
{
titlebar()->setIcon(QIcon(szIconPath)); titlebar()->setIcon(QIcon(szIconPath));
setWindowIcon(QIcon(szIconPath)); setWindowIcon(QIcon(szIconPath));
m_tray->setIcon(QIcon(szIconPath)); m_tray->setIcon(QIcon(szIconPath));
} }
} }
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (!m_fixSize->isChecked()) // 固定窗口大小时禁止全屏
{
if (event->key() == Qt::Key_F11) // 绑定键盘快捷键 F11
{
m_fullScreen->trigger();
m_menu->update();
}
}
event->accept();
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
if (this->isFullScreen()) {
m_fullScreen->setChecked(true);
} else {
m_fullScreen->setChecked(false);
if (!mFixSize) {
m_fixSize->setEnabled(true); // 命令行参数没有固定窗口大小时,窗口模式下允许手动选择固定窗口大小
}
}
DMainWindow::resizeEvent(event);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (!mtray) {
m_dialog->close(); // 不启用托盘时,关闭主窗口则关闭关于窗口
}
event->accept();
}
void MainWindow::fullScreen() void MainWindow::fullScreen()
{ {
if(m_fullScreen->isChecked()) if (m_fullScreen->isChecked()) {
{
m_fixSize->setChecked(false); m_fixSize->setChecked(false);
m_fixSize->setDisabled(true); m_fixSize->setDisabled(true);
m_menu->update(); m_menu->update();
showFullScreen(); showFullScreen();
// DMessageManager::instance()->sendMessage(this, QIcon::fromTheme("dialog-information").pixmap(64, 64), QString(tr("%1Fullscreen Mode")).arg(" ")); // DMessageManager::instance()->sendMessage(this, QIcon::fromTheme("dialog-information").pixmap(64, 64), QString(tr("%1Fullscreen Mode")).arg(" "));
} } else {
else if (!mFixSize) {
{ m_fixSize->setDisabled(false); // 命令行参数没有固定窗口大小时,窗口模式下允许手动选择固定窗口大小
if(!mFixSize)
{
m_fixSize->setDisabled(false); // 命令行参数没有固定窗口大小时,窗口模式下允许手动选择固定窗口大小
} }
m_menu->update(); m_menu->update();
showNormal(); showNormal();
@ -216,16 +232,13 @@ void MainWindow::fullScreen()
void MainWindow::fixSize() void MainWindow::fixSize()
{ {
if(m_fixSize->isChecked()) if (m_fixSize->isChecked()) {
{
m_fullScreen->setChecked(false); m_fullScreen->setChecked(false);
m_fullScreen->setDisabled(true); m_fullScreen->setDisabled(true);
m_menu->update(); m_menu->update();
setFixedSize(this->size()); setFixedSize(this->size());
/* 存在 BUG: 启用托盘图标后,若手动选择固定窗口大小,并且关闭窗口,再次打开时会丢失最大化按钮,且无法恢复。 */ /* 存在 BUG: 启用托盘图标后,若手动选择固定窗口大小,并且关闭窗口,再次打开时会丢失最大化按钮,且无法恢复。 */
} } else {
else
{
m_fullScreen->setDisabled(false); m_fullScreen->setDisabled(false);
m_menu->update(); m_menu->update();
setMinimumSize(m_width, m_height); setMinimumSize(m_width, m_height);
@ -236,14 +249,11 @@ void MainWindow::fixSize()
void MainWindow::hideButtons() void MainWindow::hideButtons()
{ {
if(m_hideButtons->isChecked()) if (m_hideButtons->isChecked()) {
{
btnBack->hide(); btnBack->hide();
btnForward->hide(); btnForward->hide();
btnRefresh->hide(); btnRefresh->hide();
} } else {
else
{
btnBack->show(); btnBack->show();
btnForward->show(); btnForward->show();
btnRefresh->show(); btnRefresh->show();
@ -253,65 +263,37 @@ void MainWindow::hideButtons()
QString MainWindow::saveAs(QString fileName) QString MainWindow::saveAs(QString fileName)
{ {
QString saveFile = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::homePath() + "/Downloads/" + fileName); QString saveFile = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::homePath() + "/Downloads/" + fileName);
if(!saveFile.isEmpty()) if (!saveFile.isEmpty()) {
{ if (QFileInfo(QFileInfo(saveFile).absolutePath()).permissions().testFlag(QFile::WriteUser)) // 判断上层目录是否可写入
if(QFileInfo(QFileInfo(saveFile).absolutePath()).permissions().testFlag(QFile::WriteUser)) // 判断上层目录是否可写入
{ {
return saveFile; return saveFile;
} } else {
else
{
return saveAs(fileName); return saveAs(fileName);
} }
} }
return nullptr; return nullptr;
} }
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(!m_fixSize->isChecked()) // 固定窗口大小时禁止全屏
{
if(event->key() == Qt::Key_F11) // 绑定键盘快捷键 F11
{
m_fullScreen->trigger();
m_menu->update();
}
event->accept();
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(!mtray)
{
m_dialog->close(); // 不启用托盘时,关闭主窗口则关闭关于窗口
}
event->accept();
}
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason) void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{ {
switch(reason) switch (reason) {
{ /* 响应托盘点击事件 */
/* 响应托盘点击事件 */ case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::Trigger: this->activateWindow();
this->activateWindow(); fixSize();
fixSize(); break;
break; default:
default: break;
break;
} }
} }
void MainWindow::on_downloadStart(QWebEngineDownloadItem *item) void MainWindow::on_downloadStart(QWebEngineDownloadItem *item)
{ {
/* 尝试加锁互斥量,禁止同时下载多个文件 */ /* 尝试加锁互斥量,禁止同时下载多个文件 */
if(mutex.tryLock()) if (mutex.tryLock()) {
{
QString fileName = QFileInfo(item->path()).fileName(); QString fileName = QFileInfo(item->path()).fileName();
QString filePath = saveAs(fileName); QString filePath = saveAs(fileName);
if(filePath.isEmpty()) if (filePath.isEmpty()) {
{
mutex.unlock(); mutex.unlock();
return; return;
} }
@ -319,21 +301,17 @@ void MainWindow::on_downloadStart(QWebEngineDownloadItem *item)
filePath = QFileInfo(item->path()).absoluteFilePath(); filePath = QFileInfo(item->path()).absoluteFilePath();
connect(item, &QWebEngineDownloadItem::downloadProgress, this, &MainWindow::on_downloadProgress); connect(item, &QWebEngineDownloadItem::downloadProgress, this, &MainWindow::on_downloadProgress);
connect(item, &QWebEngineDownloadItem::finished, this, [=]() connect(item, &QWebEngineDownloadItem::finished, this, [=]() {
{
on_downloadFinish(filePath); on_downloadFinish(filePath);
}); });
connect(pause, &DPushButton::clicked, this, [=]() connect(pause, &DPushButton::clicked, this, [=]() {
{
on_downloadPause(item); on_downloadPause(item);
}); });
connect(resume, &DPushButton::clicked, this, [=]() connect(resume, &DPushButton::clicked, this, [=]() {
{
on_downloadResume(item); on_downloadResume(item);
}); });
connect(cancel, &DPushButton::clicked, this, [=]() connect(cancel, &DPushButton::clicked, this, [=]() {
{
on_downloadCancel(item); on_downloadCancel(item);
}); });
@ -348,10 +326,8 @@ void MainWindow::on_downloadStart(QWebEngineDownloadItem *item)
isCanceled = false; isCanceled = false;
resume->hide(); resume->hide();
pause->show(); pause->show();
this->message->show(); // 上一次下载完成后隐藏了进度条,这里要重新显示 this->message->show(); // 上一次下载完成后隐藏了进度条,这里要重新显示
} } else {
else
{
DMessageManager::instance()->sendMessage(this, QIcon::fromTheme("dialog-cancel").pixmap(64, 64), QString(tr("%1Wait for previous download to complete!")).arg(" ")); DMessageManager::instance()->sendMessage(this, QIcon::fromTheme("dialog-cancel").pixmap(64, 64), QString(tr("%1Wait for previous download to complete!")).arg(" "));
} }
} }
@ -372,7 +348,7 @@ void MainWindow::on_downloadFinish(QString filePath)
message->hide(); message->hide();
if(!isCanceled) // 下载完成显示提示信息 if (!isCanceled) // 下载完成显示提示信息
{ {
DPushButton *button = new DPushButton(tr("Open")); DPushButton *button = new DPushButton(tr("Open"));
@ -382,8 +358,7 @@ void MainWindow::on_downloadFinish(QString filePath)
message->setWidget(button); message->setWidget(button);
DMessageManager::instance()->sendMessage(this, message); DMessageManager::instance()->sendMessage(this, message);
connect(button, &DPushButton::clicked, this, [=]() connect(button, &DPushButton::clicked, this, [=]() {
{
DDesktopServices::showFileItem(filePath); DDesktopServices::showFileItem(filePath);
message->hide(); message->hide();
}); });
@ -410,7 +385,7 @@ void MainWindow::on_downloadResume(QWebEngineDownloadItem *item)
void MainWindow::on_downloadCancel(QWebEngineDownloadItem *item) void MainWindow::on_downloadCancel(QWebEngineDownloadItem *item)
{ {
isCanceled = true; // 取消下载 isCanceled = true; // 取消下载
item->cancel(); item->cancel();
mutex.unlock(); mutex.unlock();

@ -36,6 +36,11 @@ public:
void setIcon(QString szIconPath); void setIcon(QString szIconPath);
protected:
void keyPressEvent(QKeyEvent *event);
void resizeEvent(QResizeEvent *event);
void closeEvent(QCloseEvent *event);
private: private:
Widget *m_widget; Widget *m_widget;
DAboutDialog *m_dialog; DAboutDialog *m_dialog;
@ -63,8 +68,8 @@ private:
QHBoxLayout *progress; QHBoxLayout *progress;
DFloatingMessage *message; DFloatingMessage *message;
QMutex mutex; // 通过 Mutex 互斥量禁止同时下载多个文件(使用简单的 bool 变量应该也可以实现该功能?) QMutex mutex; // 通过 Mutex 互斥量禁止同时下载多个文件(使用简单的 bool 变量应该也可以实现该功能?)
bool isCanceled; // 判断是否为取消下载 bool isCanceled; // 判断是否为取消下载
bool mtray, mFixSize; bool mtray, mFixSize;
int m_width, m_height; int m_width, m_height;
@ -75,9 +80,6 @@ private:
QString saveAs(QString fileName); QString saveAs(QString fileName);
void keyPressEvent(QKeyEvent *event);
void closeEvent(QCloseEvent *event);
private slots: private slots:
void trayIconActivated(QSystemTrayIcon::ActivationReason reason); void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
@ -90,7 +92,6 @@ private slots:
signals: signals:
void sigQuit(); void sigQuit();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

@ -49,47 +49,45 @@
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="198"/>
<source>%1Fullscreen Mode</source> <source>%1Fullscreen Mode</source>
<translation>%1</translation> <translation type="vanished">%1</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="208"/>
<source>%1Windowed Mode</source> <source>%1Windowed Mode</source>
<translation>%1</translation> <translation type="vanished">%1</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="250"/> <location filename="../mainwindow.cpp" line="265"/>
<source>Save As</source> <source>Save As</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="336"/> <location filename="../mainwindow.cpp" line="320"/>
<source>%1Start downloading %2</source> <source>%1Start downloading %2</source>
<translation>%1 %2</translation> <translation>%1 %2</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="349"/> <location filename="../mainwindow.cpp" line="331"/>
<source>%1Wait for previous download to complete!</source> <source>%1Wait for previous download to complete!</source>
<translation>%1</translation> <translation>%1</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="371"/> <location filename="../mainwindow.cpp" line="353"/>
<source>Open</source> <source>Open</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="375"/> <location filename="../mainwindow.cpp" line="357"/>
<source>download finished.</source> <source>download finished.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="375"/> <location filename="../mainwindow.cpp" line="357"/>
<source>Show in file manager?</source> <source>Show in file manager?</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="413"/> <location filename="../mainwindow.cpp" line="394"/>
<source>%1Download canceled!</source> <source>%1Download canceled!</source>
<translation>%1</translation> <translation>%1</translation>
</message> </message>
@ -117,77 +115,77 @@
<translation>%1</translation> <translation>%1</translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="61"/> <location filename="../main.cpp" line="62"/>
<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="65"/> <location filename="../main.cpp" line="67"/>
<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="71"/> <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="77"/> <location filename="../main.cpp" line="81"/>
<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="83"/> <location filename="../main.cpp" line="88"/>
<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="89"/> <location filename="../main.cpp" line="95"/>
<source>Enable Tray Icon. Default is false.</source> <source>Enable Tray Icon. Default is false.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="93"/> <location filename="../main.cpp" line="99"/>
<source>Run in Fullscreen Mode. Default is false.</source> <source>Run in Fullscreen Mode. Default is false.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="97"/> <location filename="../main.cpp" line="103"/>
<source>Fix Window Size. Default is false.</source> <source>Fix Window Size. Default is false.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="101"/> <location filename="../main.cpp" line="107"/>
<source>Hide Control Buttons. Default is false.</source> <source>Hide Control Buttons. Default is false.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../main.cpp" line="105"/> <location filename="../main.cpp" line="112"/>
<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="111"/> <location filename="../main.cpp" line="119"/>
<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="117"/> <location filename="../main.cpp" line="126"/>
<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="123"/> <location filename="../main.cpp" line="133"/>
<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="130"/> <location filename="../main.cpp" line="140"/>
<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="137"/> <location filename="../main.cpp" line="148"/>
<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>

@ -14,14 +14,13 @@ Widget::Widget(QString szUrl, QWidget *parent)
m_webEngineView->setObjectName(QStringLiteral("webEngineView")); m_webEngineView->setObjectName(QStringLiteral("webEngineView"));
m_webEngineView->setEnabled(true); m_webEngineView->setEnabled(true);
m_webEngineView->setAutoFillBackground(false); m_webEngineView->setAutoFillBackground(false);
m_webEngineView->setZoomFactor(1); m_webEngineView->setZoomFactor(1.0);
QWebEnginePage *page = new QWebEnginePage; QWebEnginePage *page = new QWebEnginePage;
m_webEngineView->setPage(page); m_webEngineView->setPage(page);
m_webEngineView->setUrl(QUrl(nullptr)); m_webEngineView->setUrl(QUrl(nullptr));
if (!m_szUrl.isEmpty()) if (!m_szUrl.isEmpty()) {
{
m_webEngineView->setUrl(QUrl(m_szUrl)); m_webEngineView->setUrl(QUrl(m_szUrl));
} }
@ -56,8 +55,7 @@ void Widget::refresh()
void Widget::clearLayout(QLayout *layout) void Widget::clearLayout(QLayout *layout)
{ {
QLayoutItem *item; QLayoutItem *item;
while ((item = layout->takeAt(0)) != nullptr) while ((item = layout->takeAt(0)) != nullptr) {
{
delete item; delete item;
} }
} }