-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmibitlineedit.cpp
194 lines (169 loc) · 5.91 KB
/
mibitlineedit.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
/***************************************************************************
* Copyright (C) 2009 by Miguel Chavez Gamboa *
* *
* This is based on the KLineEdit class *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "mibitlineedit.h"
#include <QtGui>
#include <QTimer>
MibitLineEdit::MibitLineEdit( QWidget *parent )
: QLineEdit( parent )
{
qDebug()<<"creating MibitLineEdit";
drawEmptyMsg = false;
actualColor = 0;
timer = new QTimer(this);
shakeTimeToLive = 0;
par = false; parTimes = 0;
shakeTimer = new QTimer(this);
shakeTimer->setInterval(20);
timer->setInterval(30);
emptyMessage = "Type here..."; //what about localization?
connect(this, SIGNAL(textEdited( const QString & ) ), SLOT(onTextChange(const QString &)) );
connect(timer, SIGNAL(timeout()), this, SLOT(stepColors()));
connect(shakeTimer,SIGNAL(timeout()), this, SLOT(shakeIt()));
}
MibitLineEdit::~MibitLineEdit ()
{
}
QString MibitLineEdit::getEmptyMessage() const
{
return emptyMessage;
}
void MibitLineEdit::setEmptyMessage( const QString &msg )
{
emptyMessage = msg;
drawEmptyMsg = text().isEmpty();
update();
}
void MibitLineEdit::paintEvent( QPaintEvent *ev )
{
QLineEdit::paintEvent( ev );
if ( text().isEmpty() ) {
QPainter p(this);
QFont f = font();
f.setItalic(true);
p.setFont(f);
QColor color(palette().color(foregroundRole()));
color.setAlphaF(0.5);
p.setPen(color);
//FIXME: fugly alert!
// qlineedit uses an internal qstyleoption set to figure this out
// and then adds a hardcoded 2 pixel interior to that.
// probably requires fixes to Qt itself to do this cleanly
// see define horizontalMargin 2 in qlineedit.cpp
QStyleOptionFrame opt;
initStyleOption(&opt);
QRect cr = style()->subElementRect(QStyle::SE_LineEditContents, &opt, this);
cr.setLeft(cr.left() + 2);
cr.setRight(cr.right() - 2);
p.drawText(cr, Qt::AlignLeft|Qt::AlignVCenter, emptyMessage);
}
}
void MibitLineEdit::setError( const QString& msg )
{
timer->start(); //timer for colors
//set tooltip
setToolTip(msg);
if (autoClear) {
//create a timer to clear the error.
QTimer::singleShot(5000,this,SLOT(clearError()));
}
}
void MibitLineEdit::stepColors()
{
if (actualColor > 199) {
actualColor=0;
timer->stop();
return;
} else {
actualColor=actualColor+2;
}
setStyleSheet(QString(" QLineEdit { background: rgb(255,%1,0); color:white; font-weight: bold;}").arg(actualColor));
}
void MibitLineEdit::clearError( )
{
setStyleSheet("");
setToolTip("");
}
///This function needs to be called before the setError() one... to enable the Qtimer::singleShot
void MibitLineEdit::setAutoClearError( const bool& state )
{
autoClear = state;
}
void MibitLineEdit::onTextChange(const QString &)
{
//clear the error
clearError();
}
void MibitLineEdit::focusInEvent( QFocusEvent *ev )
{
if ( drawEmptyMsg )
{
drawEmptyMsg = false;
update();
}
QLineEdit::focusInEvent( ev );
}
void MibitLineEdit::focusOutEvent( QFocusEvent *ev )
{
if ( text().isEmpty() )
{
drawEmptyMsg = true;
update();
}
QLineEdit::focusOutEvent( ev );
}
void MibitLineEdit::keyPressEvent ( QKeyEvent * event )
{
//check for our special keys +,Enter (specific for lemonPOS)
if ( event->key() == Qt::Key_Plus || event->key() == Qt::Key_Enter )
emit plusKeyPressed();
//anyway we must send enter and + key events...
QLineEdit::keyPressEvent(event);
}
void MibitLineEdit::shake()
{
shakeTimer->start();
QTimer::singleShot(3000,shakeTimer,SLOT(stop()));
}
void MibitLineEdit::shakeIt()
{
if (par) {
if (parTimes < 5) {
if ( parTimes % 2 == 0 )
setGeometry(geometry().x()-3, geometry().y()+3, geometry().width(), geometry().height());
else
setGeometry(geometry().x()+3, geometry().y()+3, geometry().width(), geometry().height());
}
parTimes++;
if (parTimes >39) {
parTimes = 0;
}
}
else {
if (parTimes < 5) {
if ( parTimes % 2 == 0 )
setGeometry(geometry().x()+3, geometry().y()-3, geometry().width(), geometry().height());
else
setGeometry(geometry().x()-3, geometry().y()-3, geometry().width(), geometry().height());
}
}
par = !par;
}