forked from DFRobot/Mindplus-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameter_Time_Form.cpp
More file actions
183 lines (163 loc) · 4.87 KB
/
Parameter_Time_Form.cpp
File metadata and controls
183 lines (163 loc) · 4.87 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
#include "Parameter_Time_Form.h"
#include <QPainter>
#include <QRegExp>
#include <QDebug>
#include <QTime>
#include <QKeyEvent>
#include "Animation.h"
Parameter_Time_Form::Parameter_Time_Form(const QRect rect, QWidget *parent)
: QWidget(parent)
, min_(0)
, sec_(0)
, msec_(0)
, pParent_(parent)
, rect_(rect)
{
setupUi(this);
initSetting();
initGui();
}
void Parameter_Time_Form::setValue(const QString &value)
{
QTime time(0,0,0);
int msec = value.toInt();
time = time.addMSecs(msec);
QString tmp = time.toString("m-s-z");
QStringList tmpList = tmp.split("-");
lineEditMin->setText(tmpList[0]);
lineEditSec->setText(tmpList[1]);
lineEditMsec->setText(tmpList[2]);
}
void Parameter_Time_Form::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
void Parameter_Time_Form::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
bool Parameter_Time_Form::eventFilter(QObject *target, QEvent *event)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if(lineEditMin == target || lineEditSec == target || lineEditMsec == target || this == target)
{
if(QEvent::KeyPress == event->type())
{
if(Qt::Key_Enter == keyEvent->key()
|| Qt::Key_Return == keyEvent->key())
{
this->on_pushButton_clicked();
}
else
{
return QWidget::eventFilter(target, event);
}
}
else
{
return QWidget::eventFilter(target, event);
}
}
else
{
return QWidget::eventFilter(target, event);
}
}
void Parameter_Time_Form::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QWidget *pParent_ = static_cast<QWidget *>(this->parent());
if(NULL == pParent_)
{
return;
}
offset_ = event->globalPos() - pParent_->frameGeometry().topLeft();
}
}
void Parameter_Time_Form::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
{
return;
}
if ((event->pos() - offset_).manhattanLength()
< QApplication::startDragDistance())
{
return;
}
pParent_->move(event->globalPos() - offset_);
}
void Parameter_Time_Form::showEvent(QShowEvent *event)
{
this->setGeometry(rect_);
}
void Parameter_Time_Form::initSetting()
{
//用正则表达式限定输入范围, 呵呵现学现用
//QRegExp regExp("0\.[0-9]*|[0-9]+\.?[0-9]*");
//lineEditMin->setValidator(new QRegExpValidator(regExp));
//lineEditSec->setValidator(new QRegExpValidator(regExp));
//lineEditMsec->setValidator(new QIntValidator(0, 1000));
lineEditMin->installEventFilter(this);
lineEditSec->installEventFilter(this);
lineEditMsec->installEventFilter(this);
this->installEventFilter(this);
lineEditMsec->setFocus();
lineEditMin->setValidator(new QDoubleValidator(0, 65535, 2));
lineEditSec->setValidator(new QDoubleValidator(0, 65535, 3));
lineEditMsec->setValidator(new QIntValidator(0, 100000));
this->setAttribute(Qt::WA_TranslucentBackground, true);
//恩原来是tool
this->setWindowFlags(Qt::FramelessWindowHint);
//connect(lineEdit, SIGNAL(textEdited(QString)), this, SLOT(slotTextEdited(QString)));
pushButton->setWindowFlags(Qt::FramelessWindowHint);
pushButton->setAttribute(Qt::WA_TranslucentBackground);
}
void Parameter_Time_Form::initGui()
{
this->move(1, 1);
Animation::opacityAnimation(pushButton, 150, 0, 1, QEasingCurve::OutQuart);
Animation::posAnimation(pushButton, 150, pushButton->pos() + QPoint(50, 0) , pushButton->pos(), QEasingCurve::OutQuart);
}
void Parameter_Time_Form::changeValue()
{
float min = lineEditMin->text().toFloat();
int sec = lineEditSec->text().toFloat();
int msec = lineEditMsec->text().toInt();
if(lineEditSec->text().indexOf(".") != -1)
{//处理小数点的情况
QString tmp = lineEditSec->text();
tmp = tr("0") + tmp.right(tmp.size() - tmp.indexOf(".") );
qDebug()<<"小数点" << tmp.toFloat();
//用float的话会有精度损失....fuck
msec += (tmp.toDouble()*1000);
}
sec += msec/1000;
msec %= 1000;
min += sec / 60;
sec %= 60;
min_ = min;
sec_ = sec;
msec_ = msec;
lineEditMin->setText(QString::number(min_));
lineEditSec->setText(QString::number(sec_));
lineEditMsec->setText(QString::number(msec_));
}
void Parameter_Time_Form::on_pushButton_clicked()
{
changeValue();
int value = min_*60*1000 + sec_*1000 + msec_;
emit signalValueChange(QString::number(value));
close();
}