-
Notifications
You must be signed in to change notification settings - Fork 0
/
QTopMenuGridGroupPopup.cpp
199 lines (168 loc) · 4.57 KB
/
QTopMenuGridGroupPopup.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
195
196
197
198
199
/*
* This file is part of Escain QTopMenu library
*
* QTopMenu library is free software: you can redistribute it and/or modify
* it under ther terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* Escain Documentor 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*
* Author: Adrian Maire escain (at) gmail.com
*/
#include "QTopMenuGridGroupPopup.hpp"
#include <QPainter>
#include <QPaintEvent>
using namespace Escain;
QTopMenuGridGroupPopup::QTopMenuGridGroupPopup( QWidget* parent )
: QWidget(parent)
{
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed);
QTextOption textOption;
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
textOption.setAlignment(Qt::AlignCenter | Qt::AlignBaseline);
m_staticText.setTextFormat(Qt::TextFormat::PlainText);
m_staticText.setTextOption(textOption);
}
void QTopMenuGridGroupPopup::initStyleOption(QTopMenuGridGroupPopupStyleOptions& opt) const
{
opt.isEnabled = isEnabled();
if (opt.isEnabled)
{
const QPoint mousePos = mapFromGlobal(QCursor::pos());
opt.isHover = rect().contains(mousePos);
}
opt.palette = QWidget::palette();
opt.widgetRect = rect();
opt.staticText = &m_staticText;
opt.direction = direction();
opt.margin = m_margin;
}
const std::string& QTopMenuGridGroupPopup::label() const
{
return m_label;
}
void QTopMenuGridGroupPopup::label( const std::string_view& label )
{
if (m_label != label)
{
m_label = label;
prepareText();
update();
}
}
DisplaySide QTopMenuGridGroupPopup::direction() const
{
return m_direction;
}
void QTopMenuGridGroupPopup::direction( const DisplaySide d )
{
if (m_direction != d)
{
m_direction = d;
update();
}
}
qreal QTopMenuGridGroupPopup::margin() const
{
return m_margin;
}
void QTopMenuGridGroupPopup::margin( qreal margin )
{
if (margin != m_margin)
{
m_margin = margin;
update();
}
}
void QTopMenuGridGroupPopup::prepareText()
{
auto qLabel = QString::fromUtf8(m_label.c_str());
QFont font;
setupFontForLabel(font);
QFontMetrics metrics(font);
QString elidedLabel;
if (direction()==DisplaySide::Top)
{
elidedLabel = metrics.elidedText(qLabel, Qt::TextElideMode::ElideMiddle, width()-2.0*m_margin);
}
else
{
elidedLabel = metrics.elidedText(qLabel, Qt::TextElideMode::ElideMiddle, height()-2.0*m_margin);
}
m_staticText.setText(elidedLabel);
m_staticText.prepare(QTransform(), font);
}
void QTopMenuGridGroupPopup::setupFontForLabel( QFont& f)
{
f.setWeight(QFont::ExtraBold);
f.setPointSizeF(f.pointSizeF() *0.75);
}
void QTopMenuGridGroupPopup::closeEvent(QCloseEvent* e)
{
fade();
}
QRect QTopMenuGridGroupPopup::textRect(const QRect& widgetRect, qreal margin, const QStaticText& staticText, DisplaySide dir)
{
QSizeF textSize = staticText.size();
if (dir == DisplaySide::Top)
{
QPoint pos((widgetRect.width()-textSize.width())/2,
widgetRect.height()-margin-textSize.height()*1.5);
return QRect(pos, textSize.toSize());
}
QPoint pos((widgetRect.height()-textSize.width())/2,
widgetRect.width()-margin-textSize.height()*1.5);
return QRect(pos, textSize.toSize());
}
void QTopMenuGridGroupPopup::staticDrawControl( const QTopMenuGridGroupPopupStyleOptions& opt, QPainter& p)
{
// Gather color for state/role
QPaletteExt pal = opt.palette;
if (!opt.isEnabled)
{
pal.setCurrentColorGroup(QPalette::Disabled);
}
ColorRoleExt roleText = ColorRoleExt::PlaceholderText_Normal;
if (opt.isEnabled)
{
if (opt.isHover)
{
roleText = ColorRoleExt::PlaceholderText_Hover;
}
}
// Draw label
p.setPen(pal.color(roleText));
QFont font;
setupFontForLabel(font);
p.setFont(font);
if (opt.direction == DisplaySide::Left)
{
p.translate(opt.widgetRect.left(), opt.widgetRect.top()+opt.widgetRect.height());
p.rotate(-90.0);
}
if (opt.staticText)
{
const auto& labelRect = textRect(opt.widgetRect, opt.margin, *opt.staticText, opt.direction);
p.drawStaticText( labelRect.topLeft(), *opt.staticText);
}
}
void QTopMenuGridGroupPopup::paintEvent(QPaintEvent* e)
{
QWidget::paintEvent(e);
QTopMenuGridGroupPopupStyleOptions opt;
initStyleOption(opt);
QPainter p(this);
p.setClipRect(e->rect());
p.setRenderHint(QPainter::Antialiasing );
drawControl(opt, p);
}
void QTopMenuGridGroupPopup::resizeEvent(QResizeEvent*)
{
prepareText();
}