@@ -19,6 +19,7 @@ MainWindow::MainWindow(QString szTitle,
19
19
QString szUrl,
20
20
int nWidth,
21
21
int nHeight,
22
+ bool tray,
22
23
bool nFullScreen,
23
24
bool nFixSize,
24
25
bool nHideButtons,
@@ -27,19 +28,27 @@ MainWindow::MainWindow(QString szTitle,
27
28
: DMainWindow(parent)
28
29
, m_widget(new Widget(szUrl))
29
30
, m_dialog(dialog)
31
+ , m_tray(new QSystemTrayIcon)
30
32
, btnBack(new DToolButton(titlebar()))
31
33
, btnForward(new DToolButton(titlebar()))
32
34
, btnRefresh(new DToolButton(titlebar()))
33
35
, m_menu(new QMenu)
34
36
, m_fullScreen(new QAction(tr(" Full Screen" )))
35
37
, m_fixSize(new QAction(tr(" Fix Size" )))
36
38
, m_hideButtons(new QAction(tr(" Hide Buttons" )))
39
+ , t_menu(new QMenu)
40
+ , t_show(new QAction(tr(" Show MainWindow" )))
41
+ , t_about(new QAction(tr(" About" )))
42
+ , t_exit(new QAction(tr(" Exit" )))
37
43
, bar(new DProgressBar)
38
44
, message(new DFloatingMessage(DFloatingMessage::ResidentType))
39
45
, process(new QProcess)
46
+ , mtray(tray)
47
+ , mFixSize(nFixSize)
40
48
, m_width(nWidth)
41
49
, m_height(nHeight)
42
50
{
51
+ /* 初始化 MainWindow */
43
52
setCentralWidget (m_widget);
44
53
centralWidget ()->layout ()->setContentsMargins (0 , 0 , 0 , 0 );
45
54
@@ -72,17 +81,36 @@ MainWindow::MainWindow(QString szTitle,
72
81
m_hideButtons->setCheckable (true );
73
82
m_hideButtons->setChecked (nHideButtons);
74
83
m_hideButtons->setDisabled (nHideButtons);
75
- m_menu->addAction (m_fullScreen);
76
- m_menu->addAction (m_fixSize);
77
- m_menu->addAction (m_hideButtons);
84
+ /* 命令行设置参数后 GUI 中隐藏对应选项 */
85
+ if (!nFixSize)
86
+ {
87
+ m_menu->addAction (m_fullScreen);
88
+ m_menu->addAction (m_fixSize);
89
+ }
90
+ if (!nHideButtons)
91
+ {
92
+ m_menu->addAction (m_hideButtons);
93
+ }
78
94
titlebar ()->setMenu (m_menu);
79
95
80
96
titlebar ()->setAutoHideOnFullscreen (true );
81
97
82
- fullScreen ();
83
98
fixSize ();
84
99
hideButtons ();
85
100
101
+ /* 初始化 TrayIcon */
102
+ t_menu->addAction (t_show);
103
+ t_menu->addAction (t_about);
104
+ t_menu->addAction (t_exit);
105
+ m_tray->setContextMenu (t_menu);
106
+ m_tray->setToolTip (szTitle);
107
+ m_tray->setIcon (QIcon (" :/images/spark-webapp-runtime.svg" ));
108
+
109
+ if (tray)
110
+ {
111
+ m_tray->show (); // 启用托盘时显示
112
+ }
113
+
86
114
connect (btnBack, &DToolButton::clicked, this , [&]()
87
115
{
88
116
m_widget->goBack ();
@@ -109,6 +137,22 @@ MainWindow::MainWindow(QString szTitle,
109
137
hideButtons ();
110
138
});
111
139
140
+ connect (t_show, &QAction::triggered, this , [=]()
141
+ {
142
+ this ->activateWindow ();
143
+ fixSize ();
144
+ });
145
+ connect (t_about, &QAction::triggered, this , [=]()
146
+ {
147
+ m_dialog->activateWindow ();
148
+ m_dialog->show ();
149
+ });
150
+ connect (t_exit, &QAction::triggered, this , [=]()
151
+ {
152
+ exit (0 );
153
+ });
154
+ connect (m_tray, &QSystemTrayIcon::activated, this , &MainWindow::trayIconActivated);
155
+
112
156
connect (m_widget->getPage ()->profile (), &QWebEngineProfile::downloadRequested, this , &MainWindow::on_downloadStart);
113
157
}
114
158
@@ -117,6 +161,7 @@ MainWindow::~MainWindow()
117
161
emit sigQuit ();
118
162
delete m_widget;
119
163
delete m_dialog;
164
+ delete m_tray;
120
165
}
121
166
122
167
void MainWindow::setIcon (QString szIconPath)
@@ -126,6 +171,7 @@ void MainWindow::setIcon(QString szIconPath)
126
171
{
127
172
titlebar ()->setIcon (QIcon (szIconPath));
128
173
setWindowIcon (QIcon (szIconPath));
174
+ m_tray->setIcon (QIcon (szIconPath));
129
175
qDebug () << szIconPath << " is Set!" ;
130
176
}
131
177
else
@@ -138,11 +184,19 @@ void MainWindow::fullScreen()
138
184
{
139
185
if (m_fullScreen->isChecked ())
140
186
{
187
+ m_fixSize->setChecked (false );
188
+ m_fixSize->setDisabled (true );
189
+ m_menu->update ();
141
190
showFullScreen ();
142
191
DMessageManager::instance ()->sendMessage (this , QIcon::fromTheme (" dialog-information" ).pixmap (64 , 64 ), QString (tr (" %1Fullscreen Mode" )).arg (" " ));
143
192
}
144
193
else
145
194
{
195
+ if (!mFixSize )
196
+ {
197
+ m_fixSize->setDisabled (false ); // 命令行参数没有固定窗口大小时,窗口模式下允许手动选择固定窗口大小
198
+ }
199
+ m_menu->update ();
146
200
showNormal ();
147
201
DMessageManager::instance ()->sendMessage (this , QIcon::fromTheme (" dialog-information" ).pixmap (64 , 64 ), QString (tr (" %1Windowed Mode" )).arg (" " ));
148
202
}
@@ -152,13 +206,20 @@ void MainWindow::fixSize()
152
206
{
153
207
if (m_fixSize->isChecked ())
154
208
{
155
- setFixedSize (this ->width (), this ->height ());
209
+ m_fullScreen->setChecked (false );
210
+ m_fullScreen->setDisabled (true );
211
+ m_menu->update ();
212
+ setFixedSize (this ->size ());
213
+ /* 存在 BUG: 启用托盘图标后,若手动选择固定窗口大小,并且关闭窗口,再次打开时会丢失最大化按钮,且无法恢复。 */
156
214
}
157
215
else
158
216
{
217
+ m_fullScreen->setDisabled (false );
218
+ m_menu->update ();
159
219
setMinimumSize (m_width, m_height);
160
220
setMaximumSize (QSize (QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
161
221
}
222
+ fullScreen ();
162
223
}
163
224
164
225
void MainWindow::hideButtons ()
@@ -182,24 +243,23 @@ QString MainWindow::saveAs(QString fileName)
182
243
QString saveFile = QFileDialog::getSaveFileName (this , tr (" Save As" ), QDir::homePath () + " /Downloads/" + fileName);
183
244
if (!saveFile.isEmpty ())
184
245
{
185
- // 判断上层目录是否可写入
186
- if (QFileInfo (QFileInfo (saveFile).absolutePath ()).permissions ().testFlag (QFile::WriteUser))
246
+ if (QFileInfo (QFileInfo (saveFile).absolutePath ()).permissions ().testFlag (QFile::WriteUser)) // 判断上层目录是否可写入
187
247
{
188
248
return saveFile;
189
249
}
190
250
else
191
251
{
192
- saveAs (fileName);
252
+ return saveAs (fileName);
193
253
}
194
254
}
195
255
return nullptr ;
196
256
}
197
257
198
258
void MainWindow::keyPressEvent (QKeyEvent *event)
199
259
{
200
- if (m_fixSize->isEnabled ())
260
+ if (! m_fixSize->isChecked ()) // 固定窗口大小时禁止全屏
201
261
{
202
- if (event->key () == Qt::Key_F11)
262
+ if (event->key () == Qt::Key_F11) // 绑定键盘快捷键 F11
203
263
{
204
264
m_fullScreen->trigger ();
205
265
m_menu->update ();
@@ -210,23 +270,41 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
210
270
211
271
void MainWindow::closeEvent (QCloseEvent *event)
212
272
{
213
- m_dialog->close ();
273
+ if (!mtray)
274
+ {
275
+ m_dialog->close (); // 不启用托盘时,关闭主窗口则关闭关于窗口
276
+ }
214
277
event->accept ();
215
278
}
216
279
280
+ void MainWindow::trayIconActivated (QSystemTrayIcon::ActivationReason reason)
281
+ {
282
+ switch (reason)
283
+ {
284
+ /* 响应托盘点击事件 */
285
+ case QSystemTrayIcon::Trigger:
286
+ this ->activateWindow ();
287
+ fixSize ();
288
+ break ;
289
+ default :
290
+ break ;
291
+ }
292
+ }
293
+
217
294
void MainWindow::on_downloadStart (QWebEngineDownloadItem *item)
218
295
219
296
{
220
297
QString fileName = QFileInfo (item->path ()).fileName ();
221
- if (saveAs (fileName).isEmpty ())
298
+ QString filePath = saveAs (fileName);
299
+ if (filePath.isEmpty ())
222
300
{
223
301
return ;
224
302
}
225
- item->setPath (saveAs (fileName) );
226
- QString filePath = QFileInfo (item->path ()).absoluteFilePath ();
303
+ item->setPath (filePath );
304
+ filePath = QFileInfo (item->path ()).absoluteFilePath ();
227
305
228
306
connect (item, &QWebEngineDownloadItem::downloadProgress, this , &MainWindow::on_downloadProgress);
229
- connect (item, &QWebEngineDownloadItem::finished, this , [=]
307
+ connect (item, &QWebEngineDownloadItem::finished, this , [=]()
230
308
{
231
309
on_downloadFinish (filePath);
232
310
});
@@ -263,7 +341,7 @@ void MainWindow::on_downloadFinish(QString filePath)
263
341
message->setWidget (button);
264
342
DMessageManager::instance ()->sendMessage (this , message);
265
343
266
- connect (button, &DPushButton::clicked, this , [=]
344
+ connect (button, &DPushButton::clicked, this , [=]()
267
345
{
268
346
process->start (" dde-file-manager --show-item " + filePath);
269
347
message->hide ();
0 commit comments