|
7 | 7 | #include "dialogs/settingdialog.h"
|
8 | 8 | #include "maincontroller/maincontroller.h"
|
9 | 9 |
|
| 10 | +#include <utils/cooperationutil.h> |
10 | 11 | #include <QScreen>
|
11 | 12 | #include <QUrl>
|
12 | 13 | #include <QApplication>
|
13 | 14 | #include <QDesktopServices>
|
| 15 | +#include <QCheckBox> |
| 16 | +#include <QSystemTrayIcon> |
| 17 | +#include <QMenu> |
| 18 | +#include <QVBoxLayout> |
14 | 19 |
|
15 | 20 | using namespace cooperation_core;
|
16 | 21 |
|
| 22 | +#ifdef __linux__ |
| 23 | +DWIDGET_USE_NAMESPACE |
| 24 | +const char *Kicon = "dde-cooperation"; |
| 25 | +#else |
| 26 | +const char *Kicon = ":/icons/deepin/builtin/icons/dde-cooperation_128px.svg"; |
| 27 | +#endif |
| 28 | + |
17 | 29 | MainWindowPrivate::MainWindowPrivate(MainWindow *qq)
|
18 | 30 | : q(qq)
|
19 | 31 | {
|
@@ -160,9 +172,103 @@ void MainWindow::mousePressEvent(QMouseEvent *event)
|
160 | 172 | {
|
161 | 173 | d->mousePressEvent(event);
|
162 | 174 | }
|
| 175 | +#endif |
163 | 176 |
|
164 | 177 | void MainWindow::closeEvent(QCloseEvent *event)
|
165 | 178 | {
|
166 |
| - QApplication::quit(); |
| 179 | + if (qApp->property("onlyTransfer").toBool()) |
| 180 | + QApplication::quit(); |
| 181 | + |
| 182 | + showCloseDialog(); |
| 183 | + event->ignore(); |
167 | 184 | }
|
| 185 | + |
| 186 | +void MainWindow::showCloseDialog() |
| 187 | +{ |
| 188 | + QString option = CooperationUtil::closeOption(); |
| 189 | + if (option == "Minimise") { |
| 190 | + minimizedAPP(); |
| 191 | + return; |
| 192 | + } |
| 193 | + if (option == "Exit") |
| 194 | + QApplication::quit(); |
| 195 | + |
| 196 | + CooperationDialog dlg; |
| 197 | + |
| 198 | + QVBoxLayout *layout = new QVBoxLayout(); |
| 199 | + QCheckBox *op1 = new QCheckBox(tr("Minimise to system tray")); |
| 200 | + op1->setChecked(true); |
| 201 | + QCheckBox *op2 = new QCheckBox(tr("Exit")); |
| 202 | + |
| 203 | + connect(op1, &QCheckBox::stateChanged, op1, [op2](int state) { |
| 204 | + op2->setChecked(state != Qt::Checked); |
| 205 | + }); |
| 206 | + connect(op2, &QCheckBox::stateChanged, op2, [op1](int state) { |
| 207 | + op1->setChecked(state != Qt::Checked); |
| 208 | + }); |
| 209 | + |
| 210 | + QCheckBox *op3 = new QCheckBox(tr("No more enquiries")); |
| 211 | + |
| 212 | + layout->addWidget(op1); |
| 213 | + layout->addWidget(op2); |
| 214 | + layout->addWidget(op3); |
| 215 | + |
| 216 | +#ifdef __linux__ |
| 217 | + dlg.setIcon(QIcon::fromTheme("dde-cooperation")); |
| 218 | + dlg.addButton(tr("Cancel")); |
| 219 | + dlg.addButton(tr("Confirm"), true, DDialog::ButtonWarning); |
| 220 | + dlg.setTitle(tr("Please select your operation")); |
| 221 | + QWidget *content = new QWidget(); |
| 222 | + |
| 223 | + content->setLayout(layout); |
| 224 | + dlg.addContent(content); |
| 225 | +#else |
| 226 | + dlg.setWindowIcon(QIcon::fromTheme(Kicon)); |
| 227 | + dlg.setWindowTitle(tr("Please select your operation")); |
| 228 | + QPushButton *okButton = new QPushButton(tr("Confirm")); |
| 229 | + QPushButton *cancelButton = new QPushButton(tr("Cancel")); |
| 230 | + |
| 231 | + QObject::connect(okButton, &QPushButton::clicked, &dlg, &QDialog::accept); |
| 232 | + QObject::connect(cancelButton, &QPushButton::clicked, &dlg, &QDialog::reject); |
| 233 | + |
| 234 | + layout->addWidget(okButton); |
| 235 | + layout->addWidget(cancelButton); |
| 236 | + dlg.setLayout(layout); |
| 237 | + dlg.setFixedSize(400, 200); |
168 | 238 | #endif
|
| 239 | + |
| 240 | + int code = dlg.exec(); |
| 241 | + if (code == QDialog::Accepted) { |
| 242 | + bool isExit = op2->checkState() == Qt::Checked; |
| 243 | + if (op3->checkState() == Qt::Checked) { |
| 244 | + CooperationUtil::saveOption(isExit); |
| 245 | + } |
| 246 | + |
| 247 | + if (isExit) |
| 248 | + QApplication::quit(); |
| 249 | + else |
| 250 | + minimizedAPP(); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +void MainWindow::minimizedAPP() |
| 255 | +{ |
| 256 | + this->hide(); |
| 257 | + if (d->trayIcon) |
| 258 | + return; |
| 259 | + d->trayIcon = new QSystemTrayIcon(QIcon::fromTheme(Kicon), this); |
| 260 | + |
| 261 | + QMenu *trayMenu = new QMenu(this); |
| 262 | + QAction *restoreAction = trayMenu->addAction(tr("Restore")); |
| 263 | + QAction *quitAction = trayMenu->addAction(tr("Quit")); |
| 264 | + |
| 265 | + d->trayIcon->setContextMenu(trayMenu); |
| 266 | + d->trayIcon->show(); |
| 267 | + |
| 268 | + QObject::connect(restoreAction, &QAction::triggered, this, &QMainWindow::show); |
| 269 | + QObject::connect(quitAction, &QAction::triggered, qApp, &QApplication::quit); |
| 270 | + QObject::connect(d->trayIcon, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { |
| 271 | + if (reason == QSystemTrayIcon::Trigger) |
| 272 | + this->show(); |
| 273 | + }); |
| 274 | +} |
0 commit comments