-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
384 lines (333 loc) · 11 KB
/
mainwindow.cpp
File metadata and controls
384 lines (333 loc) · 11 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "capturescreen.h"
#include "about.h"
#include "loading.h"
//#include "drawwidget.h"
#include "draw.h"
#include <QFileDialog>
#include <QClipboard>
#include <QAction>
#include <QSystemTrayIcon>
#include <QTime>
#include <QDebug>
#include <QMessageBox>
#include <QShortcut>
//SystemTray
#include <QDesktopServices>
#include <QCloseEvent>
#include <QHideEvent>
#include <QKeyEvent>
#include <QShortcut>
#include <Qt>
#include <QScreen>
//NetWork
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
//JSON
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <qhotkey.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Version = "1.3.2";
tv1 = 1, tv2 = 3, tv3 = 2;
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
/*Init Status-bar*/
statusHead = " Status: ";
this->ui->status->setText(statusHead+"Ready");
/*Init System tray*/
systemTrayIcon.setIcon(QIcon::fromTheme("folder",QIcon(":/SystemTray/LanGongIconMode.PNG")));
systemTrayIcon.setToolTip("LanGong ScreenShot-L");
/*Init System tray actions*/
QAction *showwindow = new QAction("Show Window");
QAction *menucapture = new QAction("Capture", &menu);
QAction *gethelp = new QAction("Help / About");
QAction *exitapp = new QAction("Quit");
auto startcap_hotkey = new QHotkey(QKeySequence("ctrl+shift+p"), true, this);//The hotkey will be automatically registered
auto startcap_hotkey2 = new QHotkey(QKeySequence("ctrl+alt+p"), true, this);
if (!startcap_hotkey->isRegistered())
QMessageBox::warning(NULL, "Error", "Hotkey (shortcut) 'ctrl+shift+p' is not available!\nYou can use 'ctrl+alt+p'");
QObject::connect(startcap_hotkey, &QHotkey::activated, this, [&](){
this->startcap();
});
QObject::connect(startcap_hotkey2, &QHotkey::activated, this, [&](){
this->startcap();
});
showwindow->setIcon(QIcon(":/SystemTray/LanGongIconMode.PNG"));
/*Init System tray menu*/
menu.addAction(showwindow);
menu.addAction(menucapture);
menu.addAction(gethelp);
menu.addSeparator();
menu.addAction(exitapp);
/*Connect System tray*/
connect(&systemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
connect(menucapture, SIGNAL(triggered()), this, SLOT(on_start_clicked()));
connect(exitapp, SIGNAL(triggered()), this, SLOT(slotActionExitApp()));
connect(gethelp, SIGNAL(triggered()), this, SLOT(on_about_clicked()));
connect(showwindow, SIGNAL(triggered()), this, SLOT(slotShowwindow()));
/*Init System tray icon and title*/
systemTrayIcon.setContextMenu(&menu);
systemTrayIcon.showMessage("title", "message", QSystemTrayIcon::MessageIcon::Information, 3000);
systemTrayIcon.show();
if(!systemTrayIcon.isVisible())
{
QMessageBox::information(NULL, "ScreenShot-L", "ScreenShot-L's system tray started error!");
}
exitwindow = new QShortcut(QKeySequence(tr("ctrl+q")), this);
connect(exitwindow, SIGNAL(activated()), this, SLOT(slotActionExitApp()));
}
void MainWindow::slotActionExitApp()
{
systemTrayIcon.hide();
exit(0);
}
void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
startcap();
break;
case QSystemTrayIcon::DoubleClick:
this->show();
break;
default:
break;
}
}
void MainWindow::slotShowwindow(){
this->show();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(systemTrayIcon.isVisible())
{
hide(); //隐藏窗口
event->ignore(); //忽略事件
}
}
//void MainWindow::hideEvent(QHideEvent *event)
//{
// if(systemTrayIcon.isVisible())
// {
// hide(); //隐藏窗口
// event->ignore(); //忽略事件
// }
//}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startcap(){
this->ui->status->setText(statusHead+"Starting Capturer");
this->hide();
QTime _Timer = QTime::currentTime().addMSecs(750);
while (QTime::currentTime() < _Timer)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
QScreen *screen = QGuiApplication::primaryScreen();
QPixmap map = screen->grabWindow(0);
CaptureScreen* captureHelper = new CaptureScreen(map);
connect(captureHelper, SIGNAL(signalCompleteCature(QPixmap)), this, SLOT(onCompleteCature(QPixmap)));
captureHelper->show();
this->show();
}
void MainWindow::on_start_clicked()
{
startcap();
}
void MainWindow::onCompleteCature(QPixmap captureImage)
{
// this->ui>show->setPixmap(captureImage);
// this->setFixedSize(100,100);
// this->show();
// int nowwidth=this->ui->show->width()/2;
// QSize picsize(nowwidth, this->ui->show->heightForWidth(nowwidth));
// captureImage.scaled(this->size(), Qt::KeepAspectRatio);
// captureImage.scaled(picsize, Qt::KeepAspectRatio);
//ui->show->setScaledContents(true);
/*
DrawWidget *drawWidget =new DrawWidget(this); //创建一个新的修改界面呢文件
//把这个设置为控件的中心窗体
this->setCentralWidget(drawWidget);
//创建一个工具栏
createToolBar(); //创建一个工具栏
//设置主窗体初始最小大小
setMinimumSize(600, 400);
ShowStyle(); //显示选择的风格,初始化风格,设置控件中的当前值作为选项
drawWidget->setWidth(widthSpinBox->value()); //初始化画笔宽度
drawWidget->setColor(Qt::black); //初始画笔为黑色
*/
// this->hide();
// QPixmap cp = captureImage;
// Draw *dw = new Draw(cp, this);
// connect (dw, SIGNAL(oksig(QPixmap)), this, SLOT(doneedit(QPixmap)));
// dw->show();
this->ui->status->setText(statusHead+"Ready");
nowimage=captureImage;
path="Unsaved";
putimg(nowimage);
// this->ui->path->setText(path);
}
void MainWindow::doneedit(QPixmap px)
{
nowimage = px;
putimg(px);
}
void MainWindow::putimg(QPixmap img)
{
int with = ui->show->width();
int height = ui->show->height();
QPixmap fitpixmap = img.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->show->setAlignment(Qt::AlignCenter);
ui->show->setPixmap(fitpixmap);
}
void MainWindow::on_save_clicked()
{
this->ui->status->setText(statusHead+"Waiting QFileDialog");
QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)"));
if(!filename1.isEmpty()&& !filename1.isNull()){
// functionA
// ui->show->pixmap()->save(filename1);
this->ui->status->setText(statusHead+"Writing to file");
nowimage.save(filename1);
path=filename1;
// this->ui->path->setText(path);
}
this->ui->status->setText(statusHead+"Ready");
}
void MainWindow::on_copy_clicked()
{
QClipboard *clipb=QApplication::clipboard();
//clipb->setImage(ui->show->pixmap()->toImage());
clipb->setImage(nowimage.toImage());
}
void MainWindow::on_about_clicked()
{
About* about = new About();
about->show();
}
void MainWindow::on_refresh_clicked()
{
putimg(nowimage);
// this->ui->path->setText(path);
}
//void MainWindow::on_delete_btn_clicked()
//{
// this->ui->show->clear();
// nowimage=QPixmap::fromImage(this->ui->show->pixmap()->toImage());
//}
void MainWindow::on_getupdate_clicked()
{
this->ui->status->setText(statusHead + "Getting updates");
Stopupd = false;
QString requrl = "https://langong-dev.github.io/ScreenShot-L/versions.json";
requrl = "http://victorwoo.synology.me:670/screenshot.json";
Loading *videodlg = new Loading("Loading...","Fetching for updates...",this);
connect(videodlg,SIGNAL(sendsignal()),this,SLOT(reshow()));
connect(this, SIGNAL(doneupd()), videodlg, SLOT(clo()));
videodlg->show();//子界面出现
QNetworkRequest quest;
quest.setUrl(QUrl(requrl));
quest.setHeader(QNetworkRequest::UserAgentHeader,"RT-Thread ART");
manager->get(quest);
}
void MainWindow::reshow(){
Stopupd = true;
this->ui->status->setText(statusHead+"Ready");
return;
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
if (Stopupd){
return;
}
QString str = reply->readAll();
parse_UpdateJSON(str);
reply->deleteLater();
}
int MainWindow::parse_UpdateJSON(QString str)
{
QJsonParseError err_rpt;
QJsonDocument root_Doc = QJsonDocument::fromJson(str.toUtf8(),&err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::critical(this, "Error", "Cannot get the infomations!");
emit doneupd();
return -1;
}
if(root_Doc.isObject())
{
QString url;
QJsonObject root_Obj = root_Doc.object();
QString topver = root_Obj.value("version").toString().trimmed();
QJsonObject PulseValue = root_Obj.value(topver).toObject();
int v1 = PulseValue.value("v1").toInt();
int v2 = PulseValue.value("v2").toInt();
int v3 = PulseValue.value("v3").toInt();
QJsonObject urlObj = PulseValue.value("url").toObject();
int software = 0;
#if defined (Q_OS_LINUX)
software = 1;
url = urlObj.value("linux").toString().trimmed();
#endif
#if defined (Q_OS_WIN)
software = 2;
url = urlObj.value("windows").toString().trimmed();
#endif
#if defined (Q_OS_OSX)
software = 3;
url = urlObj.value("mac").toString().trimmed();
#endif
if (software == 0)
{
QMessageBox::information(this, "Error", "Cannot get your system id!");
}
QString verison = topver;
QString UpdateTime = PulseValue.value("updtime").toString();
QString ReleaseNote = PulseValue.value("new").toString();
if (Stopupd) return 1;
emit doneupd();
bool upd = false;
if (v1>tv1)
{
upd = true;
}
else if (v1 == tv1)
{
if (v2>tv2)
{
upd = true;
}
else if (v2 == tv2)
{
if (v3 > tv3)
{
upd = true;
}
}
}
if(upd)
{
QString warningStr = "We had got a new version!\nVersion: " + verison + "\n" + "Update time: " + UpdateTime + "\n" + "Infomation:\n" + ReleaseNote;
int ret = QMessageBox::warning(this, "Get Update", warningStr, "Update", "Miss");
if(ret == 0)
{
QDesktopServices::openUrl(QUrl(url));
}
}
else{
QString str = "This is the latest version!\nYour version: ScreenShot-L@"+Version;
QMessageBox::information(this, "Get Update", str);
}
}
this->ui->status->setText(statusHead+"Ready");
return 0;
}