forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extlineedit.cc
197 lines (164 loc) · 4.62 KB
/
extlineedit.cc
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
/* This file is (c) 2012 Tvangeste <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "extlineedit.hh"
#include <QPainter>
#if QT_VERSION >= 0x040600
#include <QPropertyAnimation>
#endif
ExtLineEdit::ExtLineEdit(QWidget *parent) :
QLineEdit(parent)
{
for (int i = 0; i < 2; ++i) {
iconButtons[i] = new IconButton(this);
iconButtons[i]->installEventFilter(this);
iconButtons[i]->hide();
iconButtons[i]->setAutoHide(false);
iconEnabled[i] = false;
}
ensurePolished();
updateMargins();
connect(iconButtons[Left], SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(iconButtons[Right], SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(this, SIGNAL( textChanged( QString ) ), this, SLOT( updateButtons( QString ) ) );
}
ExtLineEdit::~ExtLineEdit()
{
}
void ExtLineEdit::setButtonVisible(Side side, bool visible)
{
iconButtons[side]->setVisible(visible);
iconEnabled[side] = visible;
updateMargins();
}
bool ExtLineEdit::isButtonVisible(Side side) const
{
return iconEnabled[side];
}
void ExtLineEdit::setButtonAutoHide(Side side, bool autohide)
{
iconButtons[side]->setAutoHide(autohide);
if (autohide)
{
iconButtons[side]->setOpacity( text().isEmpty() ? 0.0 : 1.0 );
}
else
{
iconButtons[side]->setOpacity( 1.0 );
}
}
void ExtLineEdit::updateButtons(QString text)
{
if ( oldText.isEmpty() || text.isEmpty() ) {
for (int i = 0; i < 2; ++i) {
if ( iconButtons[i]->isAutoHide() )
{
iconButtons[i]->animate( !text.isEmpty() );
}
}
oldText = text;
}
}
void ExtLineEdit::iconClicked()
{
IconButton * button = qobject_cast<IconButton *>( sender() );
int index = -1;
for (int i = 0; i < 2; ++i)
if (iconButtons[i] == button)
index = i;
if (index == -1)
return;
if (index == Left)
emit leftButtonClicked();
else if (index == Right)
emit rightButtonClicked();
}
void ExtLineEdit::updateMargins()
{
bool leftToRight = (layoutDirection() == Qt::LeftToRight);
Side realLeft = (leftToRight ? Left : Right);
Side realRight = (leftToRight ? Right : Left);
int leftMargin = iconButtons[realLeft]->pixmap().width() + 8;
int rightMargin = iconButtons[realRight]->pixmap().width() + 8;
setTextMargins(
(iconEnabled[realLeft] ? leftMargin : 0), 1,
(iconEnabled[realRight] ? rightMargin : 0), 1);
}
void ExtLineEdit::updateButtonPositions()
{
QRect contentRect = rect();
for (int i = 0; i < 2; ++i) {
Side iconPos = (Side)i;
if (layoutDirection() == Qt::RightToLeft)
iconPos = (iconPos == Left ? Right : Left);
if (iconPos == ExtLineEdit::Right) {
int right;
getTextMargins(0, 0, &right, 0);
const int iconoffset = right + 4;
iconButtons[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
int left;
getTextMargins(&left, 0, 0, 0);
const int iconoffset = left + 4;
iconButtons[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
}
}
}
void ExtLineEdit::resizeEvent(QResizeEvent *)
{
updateButtonPositions();
}
void ExtLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
{
iconButtons[side]->setPixmap(buttonPixmap);
updateMargins();
updateButtonPositions();
update();
}
QPixmap ExtLineEdit::buttonPixmap(Side side) const
{
return pixmaps[side];
}
void ExtLineEdit::setButtonToolTip(Side side, const QString &tip)
{
iconButtons[side]->setToolTip(tip);
}
void ExtLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
{
iconButtons[side]->setFocusPolicy(policy);
}
IconButton::IconButton(QWidget *parent)
: QAbstractButton(parent)
{
setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::NoFocus);
setFocusProxy(parent);
}
void IconButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
pixmapRect.moveCenter(rect().center());
if (m_autohide)
{
painter.setOpacity(m_opacity);
}
painter.drawPixmap(pixmapRect, m_pixmap);
}
void IconButton::animate(bool visible)
{
#if QT_VERSION >= 0x040600
QPropertyAnimation *animation = new QPropertyAnimation(this, "opacity");
animation->setDuration(250);
if (visible)
{
animation->setEndValue(1.0);
}
else
{
animation->setEndValue(0.0);
}
animation->start(QAbstractAnimation::DeleteWhenStopped);
#else
setOpacity(visible ? 1.0 : 0.0);
#endif
}