-
Notifications
You must be signed in to change notification settings - Fork 9
/
fancytabbar.cpp
466 lines (403 loc) · 16.5 KB
/
fancytabbar.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/****************************************************************************
**
** Copyright (C) 2012 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 "fancytabbar.h"
#include <stylehelper.h>
#include <QMouseEvent>
#include <QPainter>
#include <QColor>
#include <QStackedLayout>
#include <QToolTip>
const int FancyTabBar::m_rounding = 22;
const int FancyTabBar::m_textPadding = 4;
FancyTabBar::FancyTabBar(const TabBarPosition position, QWidget *parent)
: QWidget(parent), mPosition(position)
{
mHoverIndex = -1;
mCurrentIndex = -1;
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
{
setMinimumHeight(qMax(2 * m_rounding, 40));
setMaximumHeight(tabSizeHint(false).height());
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}
else
{
setMinimumWidth(qMax(2 * m_rounding, 40));
setMaximumWidth(tabSizeHint(false).width());
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
}
setAttribute(Qt::WA_Hover, true);
setFocusPolicy(Qt::NoFocus);
setMouseTracking(true); // Needed for hover events
mTimerTriggerChangedSignal.setSingleShot(true);
// We use a zerotimer to keep the sidebar responsive
connect(&mTimerTriggerChangedSignal, SIGNAL(timeout()), this, SLOT(emitCurrentIndex()));
}
FancyTabBar::~FancyTabBar()
{
}
QSize FancyTabBar::tabSizeHint(bool minimum) const
{
QFont boldFont(font());
boldFont.setPointSizeF(StyleHelper::sidebarFontSize());
boldFont.setBold(true);
QFontMetrics fm(boldFont);
int spacing = 8;
int width = 60 + spacing + 2;
int maxLabelwidth = 0;
for (int tab=0 ; tab<count() ;++tab) {
int width = fm.width(tabText(tab));
if (width > maxLabelwidth)
maxLabelwidth = width;
}
int iconHeight = minimum ? 0 : 40;
return QSize(qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height());
}
QPoint FancyTabBar::getCorner(const QRect& rect, const Corner corner) const
{
if(mPosition == TabBarPosition::Above)
{
if(corner == Corner::OutsideBeginning) return rect.topLeft();
if(corner == Corner::OutsideEnd) return rect.topRight();
if(corner == Corner::InsideBeginning) return rect.bottomLeft();
if(corner == Corner::InsideEnd) return rect.bottomRight();
}
else if(mPosition == TabBarPosition::Below)
{
if(corner == Corner::OutsideBeginning) return rect.bottomLeft();
if(corner == Corner::OutsideEnd) return rect.bottomRight();
if(corner == Corner::InsideBeginning) return rect.topLeft();
if(corner == Corner::InsideEnd) return rect.topRight();
}
else if(mPosition == TabBarPosition::Left)
{
if(corner == Corner::OutsideBeginning) return rect.topLeft();
if(corner == Corner::OutsideEnd) return rect.bottomLeft();
if(corner == Corner::InsideBeginning) return rect.topRight();
if(corner == Corner::InsideEnd) return rect.bottomRight();
}
else if(mPosition == TabBarPosition::Right)
{
if(corner == Corner::OutsideBeginning) return rect.topRight();
if(corner == Corner::OutsideEnd) return rect.bottomRight();
if(corner == Corner::InsideBeginning) return rect.topLeft();
if(corner == Corner::InsideEnd) return rect.bottomLeft();
}
Q_ASSERT("that's impossible!");
return QPoint();
}
QRect FancyTabBar::adjustRect(const QRect& rect, const qint8 offsetOutside, const qint8 offsetInside, const qint8 offsetBeginning, const qint8 offsetEnd) const
{
if(mPosition == TabBarPosition::Above) return rect.adjusted(-offsetBeginning, -offsetOutside, offsetEnd, offsetInside);
else if(mPosition == TabBarPosition::Below) return rect.adjusted(-offsetBeginning, -offsetInside, -offsetBeginning, offsetOutside);
else if(mPosition == TabBarPosition::Left) return rect.adjusted(-offsetOutside, -offsetBeginning, offsetInside, offsetEnd);
else if(mPosition == TabBarPosition::Right) return rect.adjusted(-offsetInside, -offsetBeginning, offsetOutside, offsetEnd);
Q_ASSERT("that's impossible!");
return QRect();
}
// Same with a point: + means towards Outside/End, - means towards Inside/Beginning
QPoint FancyTabBar::adjustPoint(const QPoint& point, const qint8 offsetInsideOutside, const qint8 offsetBeginningEnd) const
{
if(mPosition == TabBarPosition::Above) return point + QPoint(offsetBeginningEnd, -offsetInsideOutside);
else if(mPosition == TabBarPosition::Below) return point + QPoint(offsetBeginningEnd, offsetInsideOutside);
else if(mPosition == TabBarPosition::Left) return point + QPoint(-offsetInsideOutside, offsetBeginningEnd);
else if(mPosition == TabBarPosition::Right) return point + QPoint(offsetInsideOutside, offsetBeginningEnd);
Q_ASSERT("that's impossible!");
return QPoint();
}
void FancyTabBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
// paint background
QRect rectangle = adjustRect(rect(), 0, -1, 0, 0);
QLinearGradient lg;
lg.setStart(getCorner(rectangle, Corner::OutsideBeginning));
lg.setFinalStop(getCorner(rectangle, Corner::InsideBeginning));
lg.setColorAt(0.0, QColor(64, 64, 64, 255));
lg.setColorAt(1.0, QColor(130, 130, 130, 255));
painter.fillRect(rectangle, lg);
// draw dark widget bordert on inner inside (e.g. bottom if the widget position is top)
painter.setPen(StyleHelper::borderColor());
painter.drawLine(adjustPoint(getCorner(rectangle, Corner::InsideBeginning), -1, 0), adjustPoint(getCorner(rectangle, Corner::InsideEnd), -1, 0));
// draw bright widget border on outer inside (e.g. bottom if the widget position is top)
painter.setPen(StyleHelper::sidebarHighlight());
painter.drawLine(getCorner(rectangle, Corner::InsideBeginning), getCorner(rectangle, Corner::InsideEnd));
// paint inactive tabs
for (int i = 0; i < count(); ++i)
if (i != currentIndex())
paintTab(&painter, i);
// paint active tab last, since it overlaps the neighbors
if (currentIndex() != -1)
paintTab(&painter, currentIndex());
}
// Handle hover events for mouse fade ins
void FancyTabBar::mouseMoveEvent(QMouseEvent *e)
{
int newHover = -1;
for (int i = 0; i < count(); ++i)
{
QRect area = tabRect(i);
if (area.contains(e->pos())) {
newHover = i;
break;
}
}
if (newHover == mHoverIndex)
return;
if (validIndex(mHoverIndex))
mAttachedTabs[mHoverIndex]->fadeOut();
mHoverIndex = newHover;
if (validIndex(mHoverIndex)) {
mAttachedTabs[mHoverIndex]->fadeIn();
mHoverRect = tabRect(mHoverIndex);
}
}
bool FancyTabBar::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip) {
if (validIndex(mHoverIndex)) {
QString tt = tabToolTip(mHoverIndex);
if (!tt.isEmpty()) {
QToolTip::showText(static_cast<QHelpEvent*>(event)->globalPos(), tt, this);
return true;
}
}
}
return QWidget::event(event);
}
// Resets hover animation on mouse enter
void FancyTabBar::enterEvent(QEvent *e)
{
Q_UNUSED(e)
mHoverRect = QRect();
mHoverIndex = -1;
}
// Resets hover animation on mouse leave
void FancyTabBar::leaveEvent(QEvent *e)
{
Q_UNUSED(e)
mHoverIndex = -1;
mHoverRect = QRect();
for (int i = 0 ; i < mAttachedTabs.count() ; ++i) {
mAttachedTabs[i]->fadeOut();
}
}
QSize FancyTabBar::sizeHint() const
{
QSize sh = tabSizeHint();
// return QSize(sh.width(), sh.height() * mAttachedTabs.count());
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
return QSize(sh.width() * mAttachedTabs.count(), sh.height());
else
return QSize(sh.width(), sh.height() * mAttachedTabs.count());
}
QSize FancyTabBar::minimumSizeHint() const
{
QSize sh = tabSizeHint(true);
// return QSize(sh.width(), sh.height() * mAttachedTabs.count());
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
return QSize(sh.width() * mAttachedTabs.count(), sh.height());
else
return QSize(sh.width(), sh.height() * mAttachedTabs.count());
}
QRect FancyTabBar::tabRect(int index) const
{
QSize sh = tabSizeHint();
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
{
if (sh.width() * mAttachedTabs.count() > width())
sh.setWidth(width() / mAttachedTabs.count());
return QRect(index * sh.width(), 0, sh.width(), sh.height());
}
else
{
if (sh.height() * mAttachedTabs.count() > height())
sh.setHeight(height() / mAttachedTabs.count());
return QRect(0, index * sh.height(), sh.width(), sh.height());
}
}
// This keeps the sidebar responsive since
// we get a repaint before loading the
// mode itself
void FancyTabBar::emitCurrentIndex()
{
emit currentChanged(mCurrentIndex);
}
void FancyTabBar::mousePressEvent(QMouseEvent *e)
{
e->accept();
for (int index = 0; index < mAttachedTabs.count(); ++index)
{
if (tabRect(index).contains(e->pos()))
{
if (isTabEnabled(index))
{
mCurrentIndex = index;
update();
mTimerTriggerChangedSignal.start(0);
}
break;
}
}
}
void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
{
if (!validIndex(tabIndex))
{
qWarning("invalid index");
return;
}
painter->save();
QRect rect = tabRect(tabIndex);
bool selected = (tabIndex == mCurrentIndex);
bool enabled = isTabEnabled(tabIndex);
if(selected)
{
// background
painter->save();
QLinearGradient grad(getCorner(rect, Corner::OutsideBeginning), getCorner(rect, Corner::InsideBeginning));
grad.setColorAt(0, QColor(255, 255, 255, 140));
grad.setColorAt(1, QColor(255, 255, 255, 210));
painter->fillRect(adjustRect(rect, 0, 0, 0, -1), grad);
painter->restore();
// shadows (the black lines immediately before/after (active && selected)-backgrounds)
painter->setPen(QColor(0, 0, 0, 110));
painter->drawLine(adjustPoint(getCorner(rect, Corner::OutsideBeginning), 0, -1), adjustPoint(getCorner(rect, Corner::InsideBeginning), 0, -1));
painter->drawLine(getCorner(rect, Corner::OutsideEnd), getCorner(rect, Corner::InsideEnd));
// thin shadow on the outside of active tab
painter->setPen(QColor(0, 0, 0, 40));
painter->drawLine(getCorner(rect, Corner::OutsideBeginning), getCorner(rect, Corner::OutsideEnd));
// highlights
painter->setPen(QColor(255, 255, 255, 50));
painter->drawLine(adjustPoint(getCorner(rect, Corner::OutsideBeginning), 0, -2), adjustPoint(getCorner(rect, Corner::InsideBeginning), 0, -2));
painter->drawLine(adjustPoint(getCorner(rect, Corner::OutsideEnd), 0, 1), adjustPoint(getCorner(rect, Corner::InsideEnd), 0, 1));
painter->setPen(QColor(255, 255, 255, 40));
// thin white line towards beginning
painter->drawLine(adjustPoint(getCorner(rect, Corner::OutsideBeginning), 0, 0), adjustPoint(getCorner(rect, Corner::InsideBeginning), 0, 0));
// thin white line on inside border
painter->drawLine(adjustPoint(getCorner(rect, Corner::InsideBeginning), 0, 1), adjustPoint(getCorner(rect, Corner::InsideEnd), 0, -1));
// thin white line towards end
painter->drawLine(adjustPoint(getCorner(rect, Corner::OutsideEnd), 0, -1), adjustPoint(getCorner(rect, Corner::InsideEnd), 0, -1));
}
QString tabText(this->tabText(tabIndex));
QRect tabTextRect(rect);
const bool drawIcon = rect.height() > 36;
QRect tabIconRect(tabTextRect);
tabTextRect.translate(0, drawIcon ? -2 : 1);
QFont boldFont(painter->font());
boldFont.setPointSizeF(StyleHelper::sidebarFontSize());
boldFont.setBold(true);
painter->setFont(boldFont);
painter->setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
const int textFlags = Qt::AlignCenter | (drawIcon ? Qt::AlignBottom : Qt::AlignVCenter) | Qt::TextWordWrap;
if (enabled) {
painter->drawText(tabTextRect, textFlags, tabText);
painter->setPen(selected ? QColor(60, 60, 60) : StyleHelper::panelTextColor());
} else {
painter->setPen(selected ? StyleHelper::panelTextColor() : QColor(255, 255, 255, 120));
}
#if defined(Q_OS_MAC)
bool isMac=true;
#else
bool isMac = false;
#endif
// hover
if(!isMac && !selected && enabled)
{
painter->save();
int fader = int(mAttachedTabs[tabIndex]->fader());
QLinearGradient grad(getCorner(rect, Corner::OutsideBeginning), getCorner(rect, Corner::InsideBeginning));
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));
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
{
painter->drawLine(rect.topLeft(), rect.bottomLeft());
painter->drawLine(rect.topRight(), rect.bottomRight());
}
else
{
painter->drawLine(rect.topLeft(), rect.topRight());
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
}
painter->restore();
}
if (!enabled)
painter->setOpacity(0.7);
if (drawIcon) {
int textHeight = painter->fontMetrics().boundingRect(QRect(0, 0, width(), height()), Qt::TextWordWrap, tabText).height();
tabIconRect.adjust(0, 4, 0, -textHeight);
StyleHelper::drawIconWithShadow(tabIcon(tabIndex), tabIconRect, painter, enabled ? QIcon::Normal : QIcon::Disabled);
}
painter->translate(0, -1);
painter->drawText(tabTextRect, textFlags, tabText);
painter->restore();
}
void FancyTabBar::setCurrentIndex(int index) {
if (isTabEnabled(index)) {
mCurrentIndex = index;
update();
emit currentChanged(mCurrentIndex);
}
}
void FancyTabBar::setTabEnabled(int index, bool enable)
{
Q_ASSERT(index < mAttachedTabs.size());
Q_ASSERT(index >= 0);
if (index < mAttachedTabs.size() && index >= 0) {
mAttachedTabs[index]->enabled = enable;
/* During constructor there was no tabs attached which caused a limit in tab dimensions by the call
to setMaximumHeight and setMaximumWidth.
when attached tabs have texts demanding more space than the previous limit setup in constructor,
this caused the text to be truncated.
By updating setMaximumWidth and setMaximumHeight during setTabEnabled calls will keep space
allocation dynamic enough to grow an shrink tabbar as needed.
And beware that QFontMetrics doesn't take into account \n characters inside the strings. */
if(mPosition == TabBarPosition::Above || mPosition == TabBarPosition::Below)
{
setMaximumHeight(tabSizeHint(false).height());
}
else
{
setMaximumWidth(tabSizeHint(false).width());
}
update(tabRect(index));
}
}
bool FancyTabBar::isTabEnabled(int index) const
{
Q_ASSERT(index < mAttachedTabs.size());
Q_ASSERT(index >= 0);
if (index < mAttachedTabs.size() && index >= 0)
return mAttachedTabs[index]->enabled;
return false;
}