forked from TuxSH/InputRedirectionClient-Qt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtouchscreen.cpp
240 lines (196 loc) · 6.35 KB
/
touchscreen.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
#include "touchscreen.h"
#include "global.h"
#include <QMenu>
void TouchScreen::setTouchScreenPressed(bool b)
{
touchScreenPressed = b;
}
bool TouchScreen::isTouchScreenPressed()
{
return touchScreenPressed;
}
QSize TouchScreen::getTouchScreenSize()
{
return touchScreenSize;
}
QPoint TouchScreen::getTouchScreenPosition()
{
return touchScreenPosition;
}
TouchScreen::TouchScreen(QWidget *parent) : QWidget(parent)
{
Qt::WindowFlags windowFlags = Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint;
if (QSysInfo::productType() == "windows")
windowFlags |= Qt::MSWindowsFixedSizeDialogHint;
this->setWindowFlags(windowFlags);
this->setWindowTitle(tr("InputRedirectionClient-Qt - Touch screen"));
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(ShowContextMenu(const QPoint&)));
bgLabel = new QLabel(this);
updatePixmap();
setMinimumWidth(TOUCH_SCREEN_WIDTH);
setMinimumHeight(TOUCH_SCREEN_HEIGHT);
bgLabel->setScaledContents(true);
bgLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
ellipNeedDraw = true;
float loadedScale = profileSettings.value(buttonProfile+"/TouchScreen/Scale", 1).toDouble();
this->resize(TOUCH_SCREEN_WIDTH*loadedScale, TOUCH_SCREEN_HEIGHT*loadedScale);
}
void TouchScreen::ShowContextMenu(const QPoint& pos)
{
QMenu* myMenu = new QMenu();
QString strOpenOverlay = "Set Overlay Image...";
QString clearOverlayBtn = "Clear Overlay";
QString strSaveSize = "Save Touch Screen Scale";
QString strPtToBtn = "Set point to button...";
QPoint globalPos = this->mapToGlobal(pos);
myMenu->addAction(strPtToBtn);
myMenu->addSeparator();
myMenu->addAction(strSaveSize);
myMenu->addSeparator();
myMenu->addAction(strOpenOverlay);
myMenu->addAction(clearOverlayBtn);
myMenu->popup(globalPos);
QAction* selectedItem = myMenu->exec(globalPos);
QPoint newPos;
newPos.setX(pos.x());
newPos.setY(pos.y());
//If custom size, scale the point to orig 3ds touchscreen size
if(this->width() != TOUCH_SCREEN_WIDTH ||
this->height() != TOUCH_SCREEN_HEIGHT)
{
newPos.setX((TOUCH_SCREEN_WIDTH*pos.x())/this->width());
newPos.setY((TOUCH_SCREEN_HEIGHT*pos.y())/this->height());
}
//Update the shortcut gui position title text
tsShortcutGui.setCurrentPos(newPos);
if(!selectedItem)
{
myMenu->close();
return;
}
if(selectedItem->text() == strOpenOverlay)
{
QString strPic = QFileDialog::getOpenFileName(this,
tr("Open Touchscreen Image (320x240)"), "MyDocuments",
tr("Image Files (*.jpg *.jpeg *.png *.bmp *.gif *.pbm *.pgm *.ppm *.xbm *.xpm)"));
if(!strPic.isNull())
{
profileSettings.setValue(buttonProfile+"/TouchScreen/Image", strPic);
updatePixmap();
}
}
if(selectedItem->text() == strPtToBtn )
{
if(tsShortcutGui.isVisible())
{
tsShortcutGui.updateTitleText();
}
tsShortcutGui.setVisible(true);
}
if(selectedItem->text() == clearOverlayBtn)
{
clearImage();
}
if(selectedItem->text() == strSaveSize)
{
double scaleToSave = ((double)this->width()/TOUCH_SCREEN_WIDTH);
profileSettings.setValue(buttonProfile+"/TouchScreen/Scale", scaleToSave);
}
myMenu->deleteLater();
}
void TouchScreen::resizeEvent(QResizeEvent* e)
{
QSize newWinSize = e->size();
QSize curWinSize = e->oldSize();
QSize propWinSize = e->size();
if(curWinSize.height() != newWinSize.height())
{
propWinSize.setWidth((TOUCH_SCREEN_WIDTH*newWinSize.height())/TOUCH_SCREEN_HEIGHT);
propWinSize.setHeight(newWinSize.height());
}
if(curWinSize.width() != newWinSize.width())
{
propWinSize.setWidth(newWinSize.width());
propWinSize.setHeight((TOUCH_SCREEN_HEIGHT*newWinSize.width())/TOUCH_SCREEN_WIDTH);
}
touchScreenSize = propWinSize;
this->resize(propWinSize);
bgLabel->setFixedHeight(this->height());
bgLabel->setFixedWidth(this->width());
tsRatio = (double)this->width() / (double)TOUCH_SCREEN_WIDTH;
}
void TouchScreen::mousePressEvent(QMouseEvent *ev)
{
if(ev->button() == Qt::LeftButton)
{
touchScreenPressed = true;
touchScreenPosition = ev->pos();
}
}
void TouchScreen::mouseMoveEvent(QMouseEvent *ev)
{
if(touchScreenPressed && (ev->buttons() & Qt::LeftButton))
{
touchScreenPosition = ev->pos();
}
}
void TouchScreen::mouseReleaseEvent(QMouseEvent *ev)
{
if(ev->button() == Qt::LeftButton)
{
touchScreenPressed = false;
}
}
void TouchScreen::closeEvent(QCloseEvent *ev)
{
touchScreenPressed = false;
ev->accept();
}
void TouchScreen::updatePixmap(void)
{
QString strPic = profileSettings.value(buttonProfile+"/TouchScreen/Image", "").toString();
QPixmap newPic(strPic);
if (newPic.isNull())
{
newPic = QPixmap(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT);
newPic.fill(Qt::transparent);
}
}
void TouchScreen::paintEvent(QPaintEvent* e)
{
QString strPic = profileSettings.value(buttonProfile+"/TouchScreen/Image", "").toString();
QPixmap newPic(strPic);
if (newPic.isNull())
{
newPic = QPixmap(TOUCH_SCREEN_WIDTH, TOUCH_SCREEN_HEIGHT);
newPic.fill(Qt::transparent);
}
QPainter painter;
painter.begin(&newPic);
QPen pen;
pen.setWidth(1);
//painter.setBrush(QBrush(Qt::black));
pen.setColor(Qt::black);
//painter.setRenderHint(QPainter::Antialiasing, true);
for (unsigned i = 0; i < listShortcuts.size(); ++i)
{
ShortCut curShort = listShortcuts[i];
//pen.setColor(curShort.color);
painter.setBrush(QBrush(curShort.color));
painter.setPen(pen);
painter.drawEllipse(QPoint(
TOUCH_SCREEN_WIDTH*((height()*curShort.pos.x())/TOUCH_SCREEN_HEIGHT)/width(),
TOUCH_SCREEN_HEIGHT*((width()*curShort.pos.y())/TOUCH_SCREEN_WIDTH)/height()),
3, 3);
}
bgLabel->setPixmap(newPic);
bgLabel->show();
e->accept();
}
void TouchScreen::clearImage(void)
{
profileSettings.setValue(buttonProfile+"/TouchScreen/Image", "");
updatePixmap();
}