forked from xiaoyanLG/Soft-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xyqstringview.cpp
99 lines (92 loc) · 2.35 KB
/
xyqstringview.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
#include "xyqstringview.h"
#include <QEvent>
#include <QMouseEvent>
//#include <QDebug>
XYQStringView::XYQStringView(QWidget *parent)
: QWidget(parent)
{
unitMinWidth = -1;
unitMinHeight = -1;
}
int XYQStringView::getUnitMinWidth() const
{
return unitMinWidth;
}
void XYQStringView::setUnitMinWidth(int value)
{
unitMinWidth = value;
}
int XYQStringView::getUnitMinHeight() const
{
return unitMinHeight;
}
void XYQStringView::setUnitMinHeight(int value)
{
unitMinHeight = value;
}
bool XYQStringView::event(QEvent *event)
{
static bool pressed = false;
static QPoint pressedPoint;
if (event->type() == QEvent::MouseButtonPress)
{
pressed = true;
pressRect = QRect();
QMouseEvent *mouse_event = (QMouseEvent *)event;
pressedPoint = mouse_event->globalPos();
int index = findcontainsMouseRect(mouse_event->pos(), pressRect);
if (index != -1)
{
emit stringPressed(dataStrings.at(index),
mapToGlobal(QPoint(pressRect.x(), pressRect.y())));
update();
}
return true;
}
else if (event->type() == QEvent::MouseButtonRelease)
{
if (pressed)
{
if (!pressRect.isEmpty())
{
int index = dataRects.indexOf(pressRect);
emit clicked(dataStrings.at(index), index);
}
update();
emit stringPressed("", QPoint());
}
pressed = false;
pressRect = QRect();
return true;
}
else if (pressed && event->type() == QEvent::MouseMove)
{
if (pressed)
{
QRect cur_rect;
QMouseEvent *mouse_event = (QMouseEvent *)event;
int index = findcontainsMouseRect(mouse_event->pos(), cur_rect);
if (index == -1)
{
emit stringPressed("", QPoint());
pressed = false;
pressRect = QRect();
update();
}
}
return true;
}
return QWidget::event(event);
}
int XYQStringView::findcontainsMouseRect(const QPoint &mousePos, QRect &rect)
{
for (int i = 0; i < dataRects.size(); ++i)
{
if (dataRects.at(i).contains(mousePos))
{
rect = dataRects.at(i);
return i;
}
}
return -1;
}