Repository navigation
Expand file tree
/
Copy pathMyLineEdit.cpp
More file actions
321 lines (279 loc) · 8.55 KB
/
Copy pathMyLineEdit.cpp
File metadata and controls
321 lines (279 loc) · 8.55 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "MyLineEdit.h"
#include <QApplication>
#include <QFocusEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QTimer>
MyLineEdit::MyLineEdit(QWidget *parent):
QLineEdit (parent),
m_contextSecondary(false),
m_contextChipWidth(0),
m_contextAreaWidth(0),
m_contextCursorTimer(new QTimer(this)),
m_contextCursorVisible(false)
{
connect(m_contextCursorTimer, &QTimer::timeout, this, [this]()
{
m_contextCursorVisible = !m_contextCursorVisible;
update();
});
}
void MyLineEdit::setInputContext(const QString &name, const QString &toolTip, const QIcon &icon)
{
m_contextName = name;
m_contextIcon = icon;
setToolTip(toolTip);
updateContextLayout();
startContextCursor();
update();
}
void MyLineEdit::setInputContextSecondary(bool secondary)
{
if (m_contextSecondary == secondary)
{
return;
}
m_contextSecondary = secondary;
startContextCursor();
updateContextLayout();
update();
}
void MyLineEdit::clearInputContext()
{
if (m_contextName.isEmpty())
{
return;
}
m_contextName.clear();
m_contextIcon = QIcon();
m_contextSecondary = false;
m_contextChipWidth = 0;
m_contextAreaWidth = 0;
m_contextCursorTimer->stop();
m_contextCursorVisible = false;
setTextMargins(0, 0, 0, 0);
setToolTip(QString());
update();
}
void MyLineEdit::keyPressEvent(QKeyEvent *me)
{
bool handled = false;
if (selectionStart() == -1)
{
switch (me->key())
{
case Qt::Key_Backspace:
if (isAtEndOfSeparator())
{
backspace();
backspace();
backspace();
handled = true;
}
break;
case Qt::Key_Delete:
if (isAtStartOfSeparator())
{
del();
del();
del();
handled = true;
}
break;
case Qt::Key_Left:
if (isAtEndOfSeparator())
{
cursorBackward(false, 3);
handled = true;
}
break;
case Qt::Key_Right:
if (isAtStartOfSeparator())
{
cursorForward(false,3);
handled = true;
}
}
}
if (handled)
{
return;
}
else
{
QLineEdit::keyPressEvent(me);
}
}
void MyLineEdit::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton &&
!m_contextName.isEmpty() &&
contextCloseRect().contains(event->pos()))
{
emit sigInputContextRemove();
event->accept();
return;
}
if (event->button() == Qt::LeftButton)
{
emit clicked();
}
QLineEdit::mousePressEvent(event);
}
void MyLineEdit::paintEvent(QPaintEvent *event)
{
QLineEdit::paintEvent(event);
if (m_contextName.isEmpty())
{
return;
}
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QFont contextFont = font();
if (contextFont.pointSize() > 0)
{
contextFont.setPointSize(qMax(10, contextFont.pointSize() - 2));
}
contextFont.setWeight(QFont::Medium);
painter.setFont(contextFont);
QFontMetrics contextMetrics(contextFont);
const int chipHeight = qMin(38, height() - 12);
const int chipTop = (height() - chipHeight) / 2;
const QRect chipRect(8, chipTop, m_contextChipWidth, chipHeight);
QPalette currentPalette = palette();
QColor contextColor = currentPalette.color(QPalette::Text);
QColor borderColor = contextColor;
QColor backgroundColor = contextColor;
borderColor.setAlpha(55);
backgroundColor.setAlpha(18);
painter.setPen(borderColor);
painter.setBrush(backgroundColor);
painter.drawRoundedRect(chipRect, 6, 6);
const int iconSize = qMin(24, chipHeight - 10);
const QRect iconRect(chipRect.left() + 7,
chipRect.top() + (chipRect.height() - iconSize) / 2,
iconSize,
iconSize);
m_contextIcon.paint(&painter, iconRect);
const QRect closeRect = contextCloseRect();
const int textLeft = iconRect.right() + 7;
const int textRight = closeRect.left() - 6;
const QRect textRect(textLeft, chipRect.top(), textRight - textLeft, chipRect.height());
QString contextText = contextMetrics.elidedText(m_contextName, Qt::ElideMiddle, textRect.width());
painter.setPen(currentPalette.color(QPalette::Text));
painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, contextText);
QColor closeColor = currentPalette.color(QPalette::Text);
closeColor.setAlpha(150);
QPen closePen(closeColor);
closePen.setWidthF(1.5);
painter.setPen(closePen);
const QRect closeLineRect = closeRect.adjusted(4, 4, -4, -4);
painter.drawLine(closeLineRect.topLeft(), closeLineRect.bottomRight());
painter.drawLine(closeLineRect.topRight(), closeLineRect.bottomLeft());
if (m_contextSecondary)
{
QColor arrowColor = currentPalette.color(QPalette::Text);
arrowColor.setAlpha(165);
painter.setPen(arrowColor);
const QRect arrowRect(chipRect.right() + 8,
chipRect.top(),
m_contextAreaWidth - m_contextChipWidth - 8,
chipRect.height());
painter.drawText(arrowRect, Qt::AlignCenter, separatorChar());
}
else if (hasFocus() && m_contextCursorVisible)
{
const int cursorX = chipRect.right() + 8;
const int cursorHeight = qMin(contextMetrics.height(), chipRect.height() - 8);
const int cursorTop = (height() - cursorHeight) / 2;
painter.setPen(currentPalette.color(QPalette::Text));
painter.drawLine(cursorX, cursorTop, cursorX, cursorTop + cursorHeight);
}
}
void MyLineEdit::resizeEvent(QResizeEvent *event)
{
QLineEdit::resizeEvent(event);
updateContextLayout();
}
void MyLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
startContextCursor();
}
void MyLineEdit::focusOutEvent(QFocusEvent *event)
{
QLineEdit::focusOutEvent(event);
m_contextCursorTimer->stop();
m_contextCursorVisible = false;
update();
}
QChar MyLineEdit::separatorChar() const
{
QFontMetrics met = fontMetrics();
QChar arrow(0x25ba);
if (met.inFont(arrow))
return arrow;
else
return QChar('|');
}
QString MyLineEdit::separatorText() const
{
return QString(" ") + separatorChar() + " ";
}
bool MyLineEdit::isAtStartOfSeparator() const
{
return text().mid(cursorPosition(), 3) == separatorText();
}
bool MyLineEdit::isAtEndOfSeparator() const
{
return text().mid(cursorPosition() - 3, 3) == separatorText();
}
QRect MyLineEdit::contextCloseRect() const
{
const int chipHeight = qMin(38, height() - 12);
const int chipTop = (height() - chipHeight) / 2;
const QRect chipRect(8, chipTop, m_contextChipWidth, chipHeight);
const int closeSize = 16;
return QRect(chipRect.right() - closeSize - 6,
chipRect.top() + (chipRect.height() - closeSize) / 2,
closeSize,
closeSize);
}
void MyLineEdit::startContextCursor()
{
if (m_contextName.isEmpty() || m_contextSecondary || !hasFocus())
{
m_contextCursorTimer->stop();
m_contextCursorVisible = false;
return;
}
m_contextCursorVisible = true;
m_contextCursorTimer->start(qMax(200, QApplication::cursorFlashTime() / 2));
}
void MyLineEdit::updateContextLayout()
{
if (m_contextName.isEmpty())
{
return;
}
const int iconWidth = 24;
const int chipPadding = 42;
QFont contextFont = font();
if (contextFont.pointSize() > 0)
{
contextFont.setPointSize(qMax(10, contextFont.pointSize() - 2));
}
contextFont.setWeight(QFont::Medium);
QFontMetrics contextMetrics(contextFont);
const int desiredChipWidth = iconWidth + chipPadding + contextMetrics.horizontalAdvance(m_contextName);
const int maxContextWidth = qMin(280, width() * 40 / 100);
const int arrowAreaWidth = m_contextSecondary ? 28 : 0;
const int minChipWidth = iconWidth + chipPadding + contextMetrics.horizontalAdvance(QStringLiteral("..."));
m_contextChipWidth = qMax(minChipWidth,
qMin(desiredChipWidth, maxContextWidth - arrowAreaWidth));
m_contextAreaWidth = m_contextChipWidth + arrowAreaWidth;
setTextMargins(m_contextAreaWidth + 8, 0, 0, 0);
}