forked from linuxdeepin/dde-cooperation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
327 lines (272 loc) · 8.32 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "mainwindow.h"
#include "mainwindow_p.h"
#include "dialogs/settingdialog.h"
#include "utils/cooperationutil.h"
#include "widgets/cooperationstatewidget.h"
#include <QScreen>
#include <QUrl>
#include <QApplication>
#include <QDesktopServices>
#include <QCheckBox>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QVBoxLayout>
#include <QStackedLayout>
using namespace cooperation_core;
#ifdef __linux__
DWIDGET_USE_NAMESPACE
const char *Kicon = "dde-cooperation";
#else
const char *Kicon = ":/icons/deepin/builtin/icons/dde-cooperation_128px.svg";
#endif
MainWindowPrivate::MainWindowPrivate(MainWindow *qq)
: q(qq)
{
}
MainWindowPrivate::~MainWindowPrivate()
{
}
void MainWindowPrivate::initConnect()
{
connect(workspaceWidget, &WorkspaceWidget::search, q, &MainWindow::onFindDevice);
connect(workspaceWidget, &WorkspaceWidget::refresh, q, &MainWindow::onLookingForDevices);
}
void MainWindowPrivate::moveCenter()
{
QScreen *cursorScreen = nullptr;
const QPoint &cursorPos = QCursor::pos();
QList<QScreen *> screens = qApp->screens();
QList<QScreen *>::const_iterator it = screens.begin();
for (; it != screens.end(); ++it) {
if ((*it)->geometry().contains(cursorPos)) {
cursorScreen = *it;
break;
}
}
if (!cursorScreen)
cursorScreen = qApp->primaryScreen();
if (!cursorScreen)
return;
int x = (cursorScreen->availableGeometry().width() - q->width()) / 2;
int y = (cursorScreen->availableGeometry().height() - q->height()) / 2;
q->move(QPoint(x, y) + cursorScreen->geometry().topLeft());
}
void MainWindowPrivate::handleSettingMenuTriggered(int action)
{
switch (static_cast<MenuAction>(action)) {
case MenuAction::kSettings: {
if (q->property("SettingDialogShown").toBool()) {
return;
}
SettingDialog *dialog = new SettingDialog(q);
dialog->show();
dialog->setAttribute(Qt::WA_DeleteOnClose);
q->setProperty("SettingDialogShown", true);
QObject::connect(dialog, &SettingDialog::finished, [=] {
q->setProperty("SettingDialogShown", false);
});
} break;
case MenuAction::kDownloadWindowClient:
QDesktopServices::openUrl(QUrl("https://www.chinauos.com/resource/assistant"));
break;
}
}
void MainWindowPrivate::setIP(const QString &ip)
{
bottomLabel->setIp(ip);
}
MainWindow::MainWindow(QWidget *parent)
: CooperationMainWindow(parent),
d(new MainWindowPrivate(this))
{
d->initWindow();
d->initTitleBar();
d->moveCenter();
d->initConnect();
}
MainWindow::~MainWindow()
{
}
// DeviceInfoPointer MainWindow::findDeviceInfo(const QString &ip)
// {
// return d->workspaceWidget->findDeviceInfo(ip);
// }
void MainWindow::closeEvent(QCloseEvent *event)
{
if (qApp->property("onlyTransfer").toBool())
QApplication::quit();
showCloseDialog();
event->ignore();
}
void MainWindow::onlineStateChanged(const QString &validIP)
{
bool offline = validIP.isEmpty();
if (offline) {
d->workspaceWidget->clear();
d->workspaceWidget->switchWidget(WorkspaceWidget::kNoNetworkWidget);
d->setIP("---");
} else {
d->setIP(validIP);
}
}
void MainWindow::setFirstTipVisible()
{
d->workspaceWidget->setFirstStartTip(true);
}
void MainWindow::onLookingForDevices()
{
_userAction = true;
emit refreshDevices();
d->workspaceWidget->clear();
d->workspaceWidget->switchWidget(WorkspaceWidget::kLookignForDeviceWidget);
}
void MainWindow::onSwitchMode(CooperationMode mode)
{
d->stackedLayout->setCurrentIndex(mode);
d->bottomLabel->onSwitchMode(mode);
}
void MainWindow::onFindDevice(const QString &ip)
{
_userAction = true;
emit searchDevice(ip);
}
void MainWindow::onDiscoveryFinished(bool hasFound)
{
if (!hasFound && _userAction) {
d->workspaceWidget->switchWidget(WorkspaceWidget::kNoResultWidget);
}
_userAction = false;
}
void MainWindow::addDevice(const QList<DeviceInfoPointer> &infoList)
{
d->workspaceWidget->switchWidget(WorkspaceWidget::kDeviceListWidget);
d->workspaceWidget->addDeviceInfos(infoList);
_userAction = false;
}
#ifdef ENABLE_PHONE
void MainWindow::addMobileDevice(const DeviceInfoPointer info)
{
onSwitchMode(kMobile);
d->phoneWidget->setDeviceInfo(info);
}
void MainWindow::disconnectMobile()
{
d->phoneWidget->switchWidget(PhoneWidget::PageName::kQRCodeWidget);
}
void MainWindow::onSetQRCode(const QString &code)
{
d->phoneWidget->onSetQRcodeInfo(code);
}
void MainWindow::addMobileOperation(const QVariantMap &map)
{
d->phoneWidget->addOperation(map);
}
#endif
void MainWindow::removeDevice(const QString &ip)
{
d->workspaceWidget->removeDeviceInfos(ip);
}
void MainWindow::onRegistOperations(const QVariantMap &map)
{
d->workspaceWidget->addDeviceOperation(map);
}
#if defined(_WIN32) || defined(_WIN64)
void MainWindow::paintEvent(QPaintEvent *event)
{
d->paintEvent(event);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
d->mouseMoveEvent(event);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
d->mousePressEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
d->mousePressEvent(event);
}
#endif
void MainWindow::showCloseDialog()
{
QString option = CooperationUtil::closeOption();
if (option == "Minimise") {
minimizedAPP();
return;
}
if (option == "Exit")
QApplication::quit();
CooperationDialog dlg(this);
QVBoxLayout *layout = new QVBoxLayout();
QCheckBox *op1 = new QCheckBox(tr("Minimise to system tray"));
op1->setChecked(true);
QCheckBox *op2 = new QCheckBox(tr("Exit"));
connect(op1, &QCheckBox::stateChanged, op1, [op2](int state) {
op2->setChecked(state != Qt::Checked);
});
connect(op2, &QCheckBox::stateChanged, op2, [op1](int state) {
op1->setChecked(state != Qt::Checked);
});
QCheckBox *op3 = new QCheckBox(tr("No more enquiries"));
layout->addWidget(op1);
layout->addWidget(op2);
layout->addWidget(op3);
#ifdef __linux__
dlg.setIcon(QIcon::fromTheme("dde-cooperation"));
dlg.addButton(tr("Cancel"));
dlg.addButton(tr("Confirm"), true, DDialog::ButtonWarning);
dlg.setTitle(tr("Please select your operation"));
QWidget *content = new QWidget();
content->setLayout(layout);
dlg.addContent(content);
#else
dlg.setWindowIcon(QIcon::fromTheme(Kicon));
dlg.setWindowTitle(tr("Please select your operation"));
QPushButton *okButton = new QPushButton(tr("Confirm"));
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
QObject::connect(okButton, &QPushButton::clicked, &dlg, &QDialog::accept);
QObject::connect(cancelButton, &QPushButton::clicked, &dlg, &QDialog::reject);
layout->addWidget(okButton);
layout->addWidget(cancelButton);
dlg.setLayout(layout);
dlg.setFixedSize(400, 200);
#endif
// get the center position of parent window and move to center
QRect parentRect = this->window()->frameGeometry();
QRect dialogRect = dlg.frameGeometry();
QPoint centerPoint = parentRect.center() - dialogRect.center();
dlg.move(centerPoint);
int code = dlg.exec();
if (code == QDialog::Accepted) {
bool isExit = op2->checkState() == Qt::Checked;
if (op3->checkState() == Qt::Checked) {
CooperationUtil::saveOption(isExit);
}
if (isExit)
QApplication::quit();
else
minimizedAPP();
}
}
void MainWindow::minimizedAPP()
{
this->hide();
if (d->trayIcon)
return;
d->trayIcon = new QSystemTrayIcon(QIcon::fromTheme(Kicon), this);
QMenu *trayMenu = new QMenu(this);
QAction *restoreAction = trayMenu->addAction(tr("Restore"));
QAction *quitAction = trayMenu->addAction(tr("Quit"));
d->trayIcon->setContextMenu(trayMenu);
d->trayIcon->show();
QObject::connect(restoreAction, &QAction::triggered, this, &QMainWindow::show);
QObject::connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
QObject::connect(d->trayIcon, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger)
this->show();
});
}