-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLabel.cpp
169 lines (151 loc) · 3.94 KB
/
Label.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
#include "Label.h"
#include <KDE/Plasma/Theme>
Label::Label(QWidget* parent)
{
Q_UNUSED(parent);
QFont* font = new QFont();
font->setPointSize(font->pointSize()+6);
this->setFont(*font);
delete font;
}
Label::~Label()
{}
void Label::setText(QString text)
{
text = text.remove('\n');
if (text != this->text())
{
QLabel::setText(text);
emit textChanged(text);
}
}
void Label::appendText(QString text)
{
this->setText(this->text() + text);
}
QString Label::completion()
{
return this->m_completionText;
}
void Label::setCompletion(QString string)
{
this->m_completionText = string;
this->update();
}
QString Label::preEdit()
{
return this->m_preEditText;
}
void Label::setPreEdit(QString preEdit)
{
this->m_preEditText = preEdit;
this->update();
}
void Label::paintEvent(QPaintEvent*)
{
if (this->text().isEmpty())
return;
m_painter = new QPainter(this);
m_painter->setFont(this->font());
m_backPen.setBrush(QBrush());
m_frontPen.setBrush(QBrush());
m_preEditPen.setBrush(QBrush());
m_preEditPen.setColor(Plasma::Theme().color(Plasma::Theme::HighlightColor));
QColor color = Plasma::Theme().color(Plasma::Theme::TextColor);
m_frontPen.setColor(color);
color.setAlpha(100);
m_backPen.setColor(color);
int lFront = this->text().length();
int lBack = this->m_completionText.length();
m_position = this->contentsRect().width()/2;
int length = QFontMetrics(this->font()).width(this->text());
bool drawComletion = false;
int offset = this->m_completionText.indexOf(this->text(), 0, Qt::CaseInsensitive);
m_position -= length/2;
int advance;
if (length >= this->contentsRect().width()-60)
m_position = -(length-this->width()+30);
if (offset >= 0)
{
drawComletion = true;
m_position -= QFontMetrics(this->font()).width(this->m_completionText.left(offset));
}
else
{
offset = 0;
}
m_backPen = makeGradient(m_backPen);
m_frontPen = makeGradient(m_frontPen);
for (int index = 0; index < max(lFront, lBack); index++)
{
QChar ch;
if (drawComletion)
{
ch = this->m_completionText.at(index);
if (index-offset < 0 or index-offset >= lFront)
advance = paintText(ch, m_backPen);
else
advance = paintText(ch, m_frontPen);
}
else
{
if (index-offset >= lFront)
break;
ch = this->text().at(index);
advance = paintText(ch, m_frontPen);
}
if (!m_preEditText.isEmpty())
{
if (index-lFront > 0 and index-lFront < this->m_preEditText.length())
{
ch = this->m_preEditText.at(index-lFront);
paintText(ch, m_preEditPen);
}
}
m_position += advance;
}
delete m_painter;
m_painter = 0;
}
int Label::paintText(QChar ch, QPen pen)
{
int width = QFontMetrics(this->font()).width(ch);
if (m_position+width >= 0 and m_position <= this->contentsRect().width())
{
m_painter->setPen(pen);
QRect rect = this->contentsRect();
rect.setLeft(m_position);
m_painter->drawText(rect, QString(ch));
}
return width;
}
QPen Label::makeGradient(QPen pen)
{
QColor color = pen.color();
color.setAlpha(0);
QGradient gradient;
gradient = QRadialGradient(this->contentsRect().center(), this->contentsRect().width()/2);
gradient.setColorAt(1, color);
gradient.setColorAt(0.8, pen.color());
pen.setBrush(QBrush(gradient));
return pen;
}
namespace
{
int max(int i1, int i2)
{
if (i1 > i2)
return i1;
else
return i2;
}
QString longest(QString str1, QString str2)
{
if (str1.length() > str2.length())
return str1;
else
return str2;
}
};
#include "Label.moc"
// kate: indent-mode cstyle; space-indent on; indent-width 4;