-
Notifications
You must be signed in to change notification settings - Fork 5
/
fancyactionbar.cpp
372 lines (337 loc) · 13.5 KB
/
fancyactionbar.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "fancyactionbar.h"
#include "coreconstants.h"
#include "stylehelper.h"
#include "stringutils.h"
#include <QHBoxLayout>
#include <QPainter>
#include <QPicture>
#include <QVBoxLayout>
#include <QAction>
#include <QStatusBar>
#include <QStyle>
#include <QStyleOption>
#include <QMouseEvent>
#include <QApplication>
#include <QEvent>
#include <QAnimationGroup>
#include <QPropertyAnimation>
#include <QDebug>
using namespace Manhattan;
FancyToolButton::FancyToolButton(QWidget *parent)
: QToolButton(parent), m_fader(0)
{
m_hasForceVisible = false;
setAttribute(Qt::WA_Hover, true);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}
void FancyToolButton::forceVisible(bool visible)
{
m_hasForceVisible = true;
setVisible(visible);
}
bool FancyToolButton::event(QEvent *e)
{
switch(e->type()) {
case QEvent::Enter:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
case QEvent::Leave:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(0.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
default:
return QToolButton::event(e);
}
return false;
}
static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics &fontMetrics,
qreal availableWidth)
{
// split in two lines.
// this looks if full words can be split off at the end of the string,
// to put them in the second line. First line is drawn with ellipsis,
// second line gets ellipsis if it couldn't split off full words.
QVector<QString> splitLines(2);
QRegExp rx(QLatin1String("\\s+"));
int splitPos = -1;
int nextSplitPos = text.length();
do {
nextSplitPos = rx.lastIndexIn(text,
nextSplitPos - text.length() - 1);
if (nextSplitPos != -1) {
int splitCandidate = nextSplitPos + rx.matchedLength();
if (fontMetrics.width(text.mid(splitCandidate)) <= availableWidth) {
splitPos = splitCandidate;
} else {
break;
}
}
} while (nextSplitPos > 0 && fontMetrics.width(text.left(nextSplitPos)) > availableWidth);
// check if we could split at white space at all
if (splitPos < 0) {
splitLines[0] = fontMetrics.elidedText(text, Qt::ElideRight,
availableWidth);
QString common = Manhattan::commonPrefix(QStringList()
<< splitLines[0] << text);
splitLines[1] = text.mid(common.length());
// elide the second line even if it fits, since it is cut off in mid-word
while (fontMetrics.width(QChar(0x2026) /*'...'*/ + splitLines[1]) > availableWidth
&& splitLines[1].length() > 3
/*keep at least three original characters (should not happen)*/) {
splitLines[1].remove(0, 1);
}
splitLines[1] = QChar(0x2026) /*'...'*/ + splitLines[1];
} else {
splitLines[0] = fontMetrics.elidedText(text.left(splitPos).trimmed(), Qt::ElideRight, availableWidth);
splitLines[1] = text.mid(splitPos);
}
return splitLines;
}
void FancyToolButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
// draw borders
bool isTitledAction = defaultAction()->property("titledAction").toBool();
#ifndef Q_OS_MAC // Mac UIs usually don't hover
if (m_fader > 0 && isEnabled() && !isDown() && !isChecked()) {
painter.save();
int fader = int(40 * m_fader);
QLinearGradient grad(rect().topLeft(), rect().topRight());
grad.setColorAt(0, Qt::transparent);
grad.setColorAt(0.5, QColor(255, 255, 255, fader));
grad.setColorAt(1, Qt::transparent);
painter.fillRect(rect(), grad);
painter.setPen(QPen(grad, 1.0));
painter.drawLine(rect().topLeft(), rect().topRight());
painter.drawLine(rect().bottomLeft(), rect().bottomRight());
painter.restore();
} else
#endif
if (isDown() || isChecked()) {
painter.save();
QLinearGradient grad(rect().topLeft(), rect().topRight());
grad.setColorAt(0, Qt::transparent);
grad.setColorAt(0.5, QColor(0, 0, 0, 50));
grad.setColorAt(1, Qt::transparent);
painter.fillRect(rect(), grad);
painter.setPen(QPen(grad, 1.0));
painter.drawLine(rect().topLeft(), rect().topRight());
painter.drawLine(rect().topLeft(), rect().topRight());
painter.drawLine(rect().topLeft() + QPoint(0,1), rect().topRight() + QPoint(0,1));
painter.drawLine(rect().bottomLeft(), rect().bottomRight());
painter.drawLine(rect().bottomLeft(), rect().bottomRight());
painter.drawLine(rect().topLeft() - QPoint(0,1), rect().topRight() - QPoint(0,1));
painter.restore();
}
QPixmap borderPixmap;
QMargins margins;
QRect iconRect(0, 0, Manhattan::Constants::TARGET_ICON_SIZE, Manhattan::Constants::TARGET_ICON_SIZE);
// draw popup texts
if (isTitledAction) {
QFont normalFont(painter.font());
QRect centerRect = rect();
normalFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
QFont boldFont(normalFont);
boldFont.setBold(true);
QFontMetrics fm(normalFont);
QFontMetrics boldFm(boldFont);
int lineHeight = boldFm.height();
int textFlags = Qt::AlignVCenter|Qt::AlignHCenter;
const QString projectName = defaultAction()->property("heading").toString();
if (!projectName.isNull())
centerRect.adjust(0, lineHeight + 4, 0, 0);
centerRect.adjust(0, 0, 0, -lineHeight*2 - 4);
iconRect.moveCenter(centerRect.center());
Utils::StyleHelper::drawIconWithShadow(icon(), iconRect, &painter, isEnabled() ? QIcon::Normal : QIcon::Disabled);
painter.setFont(normalFont);
QPoint textOffset = centerRect.center() - QPoint(iconRect.width()/2, iconRect.height()/2);
textOffset = textOffset - QPoint(0, lineHeight + 4);
QRectF r(0, textOffset.y(), rect().width(), lineHeight);
QColor penColor;
if (isEnabled())
penColor = Qt::white;
else
penColor = Qt::gray;
painter.setPen(penColor);
// draw project name
const int margin = 6;
const qreal availableWidth = r.width() - margin;
QString ellidedProjectName = fm.elidedText(projectName, Qt::ElideMiddle, availableWidth);
if (isEnabled()) {
const QRectF shadowR = r.translated(0, 1);
painter.setPen(QColor(30, 30, 30, 80));
painter.drawText(shadowR, textFlags, ellidedProjectName);
painter.setPen(penColor);
}
painter.drawText(r, textFlags, ellidedProjectName);
// draw build configuration name
textOffset = iconRect.center() + QPoint(iconRect.width()/2, iconRect.height()/2);
QRectF buildConfigRect[2];
buildConfigRect[0] = QRectF(0, textOffset.y() + 5, rect().width(), lineHeight);
buildConfigRect[1] = QRectF(0, textOffset.y() + 5 + lineHeight, rect().width(), lineHeight);
painter.setFont(boldFont);
QVector<QString> splitBuildConfiguration(2);
const QString buildConfiguration = defaultAction()->property("subtitle").toString();
if (boldFm.width(buildConfiguration) <= availableWidth) {
// text fits in one line
splitBuildConfiguration[0] = buildConfiguration;
} else {
splitBuildConfiguration = splitInTwoLines(buildConfiguration, boldFm, availableWidth);
}
// draw the two lines for the build configuration
for (int i = 0; i < 2; ++i) {
if (splitBuildConfiguration[i].isEmpty())
continue;
if (isEnabled()) {
const QRectF shadowR = buildConfigRect[i].translated(0, 1);
painter.setPen(QColor(30, 30, 30, 80));
painter.drawText(shadowR, textFlags, splitBuildConfiguration[i]);
painter.setPen(penColor);
}
painter.drawText(buildConfigRect[i], textFlags, splitBuildConfiguration[i]);
}
// pop up arrow next to icon
if (!icon().isNull()) {
QStyleOption opt;
opt.initFrom(this);
opt.rect = rect().adjusted(rect().width() - 16, 0, -8, 0);
Utils::StyleHelper::drawArrow(QStyle::PE_IndicatorArrowRight, &painter, &opt);
}
} else {
iconRect.moveCenter(rect().center());
Utils::StyleHelper::drawIconWithShadow(icon(), iconRect, &painter, isEnabled() ? QIcon::Normal : QIcon::Disabled);
}
}
void FancyActionBar::setSeparator(SeparatorType type)
{
if (type != m_separator) {
m_separator = type;
update();
}
}
void FancyActionBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
if (m_separator == None)
return;
QPainter painter(this);
QColor light = Utils::StyleHelper::sidebarHighlight();
QColor dark = Utils::StyleHelper::sidebarShadow();
if (m_separator == Top) {
painter.setPen(dark);
painter.drawLine(rect().topLeft(), rect().topRight());
painter.setPen(light);
painter.drawLine(rect().topLeft() + QPoint(1,1), rect().topRight() + QPoint(0,1));
}
else {
painter.setPen(dark);
painter.drawLine(rect().bottomLeft() - QPoint(0,2), rect().bottomRight() - QPoint(0,2));
painter.setPen(light);
painter.drawLine(rect().bottomLeft() - QPoint(1,1), rect().bottomRight() - QPoint(0,1));
}
}
QSize FancyToolButton::sizeHint() const
{
QSizeF buttonSize = iconSize().expandedTo(QSize(64, 38));
if (defaultAction()->property("titledAction").toBool()) {
QFont boldFont(font());
boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
boldFont.setBold(true);
QFontMetrics fm(boldFont);
qreal lineHeight = fm.height();
const QString projectName = defaultAction()->property("heading").toString();
buttonSize += QSizeF(0, 10);
if (!projectName.isEmpty())
buttonSize += QSizeF(0, lineHeight + 2);
buttonSize += QSizeF(0, lineHeight*2 + 2);
}
return buttonSize.toSize();
}
QSize FancyToolButton::minimumSizeHint() const
{
return QSize(8, 8);
}
void FancyToolButton::actionChanged()
{
// the default action changed in some way, e.g. it might got hidden
// since we inherit a tool button we won't get invisible, so do this here
if (!m_hasForceVisible) {
if (QAction* action = defaultAction())
setVisible(action->isVisible());
}
}
FancyActionBar::FancyActionBar(QWidget *parent)
: QWidget(parent)
, m_separator(Top)
{
setObjectName(QLatin1String("actionbar"));
m_actionsLayout = new QVBoxLayout;
QVBoxLayout *spacerLayout = new QVBoxLayout;
spacerLayout->addLayout(m_actionsLayout);
int sbh = 8;
spacerLayout->addSpacing(sbh);
spacerLayout->setMargin(0);
spacerLayout->setSpacing(0);
setLayout(spacerLayout);
setContentsMargins(0,2,0,0);
}
void FancyActionBar::addProjectSelector(QAction *action)
{
FancyToolButton* toolButton = new FancyToolButton(this);
toolButton->setDefaultAction(action);
connect(action, SIGNAL(changed()), toolButton, SLOT(actionChanged()));
m_actionsLayout->insertWidget(0, toolButton);
}
void FancyActionBar::insertAction(int index, QAction *action)
{
FancyToolButton *toolButton = new FancyToolButton(this);
toolButton->setDefaultAction(action);
connect(action, SIGNAL(changed()), toolButton, SLOT(actionChanged()));
m_actionsLayout->insertWidget(index, toolButton);
}
QLayout *FancyActionBar::actionsLayout() const
{
return m_actionsLayout;
}
QSize FancyActionBar::minimumSizeHint() const
{
return sizeHint();
}