-
Notifications
You must be signed in to change notification settings - Fork 7
/
mainwindow.cpp
executable file
·226 lines (183 loc) · 6.37 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "encryptutils.h"
#include <QTimer>
#include <QPainter>
#include <QtNetwork/QNetworkInterface>
#include <QSettings>
#include <QCryptographicHash>
#include <QMessageBox>
#include <QCloseEvent>
#include "fakeinput.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
programIcon(new QIcon(":/images/icon64.png"))
{
ui->setupUi(this);
setPasswordDialog = new SetPasswordDialog(this);
helpIpDialog = new HelpIpDialog(this);
loadSettings();
this->setWindowIcon(*programIcon);
this->setFixedSize(this->geometry().width(),this->geometry().height());
this->setWindowFlags(Qt::Dialog);
this->setWindowFlag(Qt::WindowContextHelpButtonHint, false);
rotatingSquare = new RotatingSquare(0, 0, 0, this->width()/7, 35, 35, 35);
loadSvgs();
startSvgAnimation();
updateServerIp();
createActions();
createTrayIcon();
if(ui->startMinimizedCheck->isChecked() == false)
this->show();
}
MainWindow::~MainWindow()
{
delete rotatingSquare;
delete ui;
delete programIcon;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
saveSettings();
QMainWindow::closeEvent(event);
}
void MainWindow::loadSettings()
{
QSettings settings("WifiMouse", QSettings::NativeFormat);
serverPassword = settings.value("pass","").toByteArray();
if(serverPassword.length() != 16)
serverPassword = EncryptUtils::makeHash16("");
ui->startMinimizedCheck->setChecked( settings.value("startMinimized", false).toBool() );
}
void MainWindow::saveSettings()
{
QSettings settings("WifiMouse", QSettings::NativeFormat);
settings.setValue("pass", serverPassword);
settings.setValue("startMinimized", ui->startMinimizedCheck->isChecked());
}
QByteArray MainWindow::getPassword()
{
return serverPassword;
}
void MainWindow::setPassword(QString newPassword)
{
// Store password as 16 length hash ready to be used as AES key
serverPassword = EncryptUtils::makeHash16(newPassword.toUtf8());
saveSettings();
}
void MainWindow::setClientIp(QString ip)
{
clientIpAction->setText(ip);
if(ip == "Not connected" && statusWidget->getRenderer() == connectedSvg)
statusWidget->setRenderer(listening1Svg);
else if(ip != "Not connected")
statusWidget->setRenderer(connectedSvg);
}
void MainWindow::updateServerIp()
{
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
ui->serverIpLabel->setText("Server IP: "+address.toString());
}
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.translate(rotatingSquare->x, rotatingSquare->y);
p.rotate(rotatingSquare->rotation);
logoSvg->render(&p, QRectF(rotatingSquare->size/-2,rotatingSquare->size/-2,rotatingSquare->size,rotatingSquare->size));
}
void MainWindow::loadSvgs()
{
listening1Svg = new QSvgRenderer(this); listening1Svg->load((QString)":/images/listening1.svg");
listening2Svg = new QSvgRenderer(this); listening2Svg->load((QString)":/images/listening2.svg");
listening3Svg = new QSvgRenderer(this); listening3Svg->load((QString)":/images/listening3.svg");
logoSvg = new QSvgRenderer(this); logoSvg->load((QString)":/images/logo.svg");
connectedSvg = new QSvgRenderer(this); connectedSvg->load((QString)":/images/connected.svg");
statusWidget = new FixedSvgWidget(this);
statusWidget->setRenderer(listening3Svg);
statusWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->statusLayout->addWidget(statusWidget);
}
void MainWindow::updateListeningAnimation()
{
if(this->isVisible() == false)
return;
if(statusWidget->getRenderer() == listening1Svg)
statusWidget->setRenderer(listening2Svg);
else if(statusWidget->getRenderer() == listening2Svg)
statusWidget->setRenderer(listening3Svg);
else if(statusWidget->getRenderer() == listening3Svg)
statusWidget->setRenderer(listening1Svg);
statusWidget->repaint();
updateServerIp();
}
void MainWindow::updateLogoAnimation()
{
if(this->isVisible() == false)
return;
rotatingSquare->update(this->width(), this->height());
this->repaint();
}
void MainWindow::startSvgAnimation()
{
QTimer *listeningTimer = new QTimer(this);
connect(listeningTimer, SIGNAL(timeout()), this, SLOT(updateListeningAnimation()));
listeningTimer->start(1500);
QTimer *logoTimer = new QTimer(this);
connect(logoTimer, SIGNAL(timeout()), this, SLOT(updateLogoAnimation()));
logoTimer->start(15);
}
void MainWindow::clickMaximized()
{
this->show();
this->raise();
}
void MainWindow::clickMinimized()
{
this->hide();
}
void MainWindow::clickQuit()
{
this->close();
}
void MainWindow::clickSetPassword()
{
if(!setPasswordDialog->isVisible())
setPasswordDialog->show();
}
void MainWindow::clickIpHelper()
{
helpIpDialog->show();
}
void MainWindow::createActions()
{
quitAction = new QAction(tr("Quit"),this);
connect(quitAction, SIGNAL(triggered()), this, SLOT(clickQuit()));
connect(ui->minimizeButton, SIGNAL(released()), this, SLOT(clickMinimized()));
maximizeAction = new QAction(tr("Maximize"),this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(clickMaximized()));
passwordAction = new QAction(tr("Set password"),this);
connect(passwordAction, SIGNAL(triggered()), this, SLOT(clickSetPassword()));
connect(ui->passwordButton, SIGNAL(released()), this, SLOT(clickSetPassword()));
connect(ui->startMinimizedCheck, SIGNAL(released()), this, SLOT(saveSettings()));
connect(ui->helpButton, SIGNAL(released()), this, SLOT(clickIpHelper()));
clientTitleAction = new QAction("Client IP", this);
clientTitleAction->setEnabled(false);
clientIpAction = new QAction("Not connected", this);
}
void MainWindow::createTrayIcon()
{
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(*programIcon);
trayIcon->show();
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(clientTitleAction);
trayIconMenu->addAction(clientIpAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(passwordAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
}