forked from shadowsocks/shadowsocks-qt5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrcodecapturer.cpp
83 lines (76 loc) · 2.4 KB
/
qrcodecapturer.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
#include "qrcodecapturer.h"
#include "urihelper.h"
#include <QApplication>
#include <QMoveEvent>
#include <QResizeEvent>
#include <QDesktopWidget>
#include <QScreen>
QRCodeCapturer::QRCodeCapturer(QWidget *parent) :
QMainWindow(parent)
{
#ifdef Q_OS_WIN
/*
* On Windows, it requires Qt::FramelessWindowHint to be set to make
* translucent background work, but we need a window with borders.
* Therefore, we set the entire window semi-transparent so that
* users are still able to see the region below while moving the
* capturer above the QR code image.
*/
this->setWindowOpacity(0.75);
#else
this->setAttribute(Qt::WA_TranslucentBackground, true);
#endif
this->setWindowTitle(tr("QR Capturer"));
this->setMinimumSize(400, 400);
}
QRCodeCapturer::~QRCodeCapturer()
{}
QString QRCodeCapturer::scanEntireScreen()
{
QString uri;
QList<QScreen *> screens = qApp->screens();
for (QList<QScreen *>::iterator sc = screens.begin();
sc != screens.end();
++sc) {
QImage raw_sc = (*sc)->grabWindow(qApp->desktop()->winId()).toImage();
QString result = URIHelper::decodeImage(raw_sc);
if (!result.isNull()) {
uri = result;
}
}
return uri;
}
void QRCodeCapturer::moveEvent(QMoveEvent *e)
{
QMainWindow::moveEvent(e);
decodeCurrentRegion();
}
void QRCodeCapturer::resizeEvent(QResizeEvent *e)
{
QMainWindow::resizeEvent(e);
decodeCurrentRegion();
}
void QRCodeCapturer::closeEvent(QCloseEvent *e)
{
QMainWindow::closeEvent(e);
emit closed();
}
void QRCodeCapturer::decodeCurrentRegion()
{
QScreen *sc = qApp->screens().at(qApp->desktop()->screenNumber(this));
QRect geometry = this->geometry();
QImage raw_sc = sc->grabWindow(qApp->desktop()->winId(),
geometry.x(),
geometry.y(),
geometry.width(),
geometry.height()).toImage();
QString result = URIHelper::decodeImage(raw_sc);
if (!result.isNull()) {
this->close();
// moveEvent and resizeEvent both happen quite frequent
// it's very likely this signal would be emitted multiple times
// the solution is to use Qt::DirectConnection signal-slot connection
// and disconnect such a connection in the slot function
emit qrCodeFound(result);
}
}