Debian Codegraph @

src/fancytabwidget.cpp

raw symbols SCIP
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation ([email protected])
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "fancytabwidget.h"
31 #include "stylehelper.h"
32
33 #include <QDebug>
34
35 #include <QAnimationGroup>
36 #include <QColorDialog>
37 #include <QHBoxLayout>
38 #include <QMenu>
39 #include <QMouseEvent>
40 #include <QPainter>
41 #include <QPropertyAnimation>
42 #include <QSignalMapper>
43 #include <QSplitter>
44 #include <QStackedLayout>
45 #include <QStyleOptionTabV3>
46 #include <QToolButton>
47 #include <QToolTip>
48 #include <QVBoxLayout>
49 #include <QCommonStyle>
50
51 using namespace Core;
52 using namespace Internal;
53
54 const int FancyTabBar::m_rounding = 22;
55 const int FancyTabBar::m_textPadding = 4;
56
57 void FancyTabProxyStyle::drawControl(
58 ControlElement element, const QStyleOption* option,
59 QPainter* p, const QWidget* widget) const {
60
61 const QStyleOptionTabV3* v_opt = qstyleoption_cast<const QStyleOptionTabV3*>(option);
62
63 if (element != CE_TabBarTab || !v_opt) {
64 QProxyStyle::drawControl(element, option, p, widget);
65 return;
66 }
67
68 const QRect rect = v_opt->rect;
69 const bool selected = v_opt->state & State_Selected;
70 const bool vertical_tabs = v_opt->shape == QTabBar::RoundedWest;
71 const QString text = v_opt->text;
72
73 if (selected) {
74 //background
75 p->save();
76 QLinearGradient grad(rect.topLeft(), rect.topRight());
77 grad.setColorAt(0, QColor(255, 255, 255, 140));
78 grad.setColorAt(1, QColor(255, 255, 255, 210));
79 p->fillRect(rect.adjusted(0, 0, 0, -1), grad);
80 p->restore();
81
82 //shadows
83 p->setPen(QColor(0, 0, 0, 110));
84 p->drawLine(rect.topLeft() + QPoint(1,-1), rect.topRight() - QPoint(0,1));
85 p->drawLine(rect.bottomLeft(), rect.bottomRight());
86 p->setPen(QColor(0, 0, 0, 40));
87 p->drawLine(rect.topLeft(), rect.bottomLeft());
88
89 //highlights
90 p->setPen(QColor(255, 255, 255, 50));
91 p->drawLine(rect.topLeft() + QPoint(0, -2), rect.topRight() - QPoint(0,2));
92 p->drawLine(rect.bottomLeft() + QPoint(0, 1), rect.bottomRight() + QPoint(0,1));
93 p->setPen(QColor(255, 255, 255, 40));
94 p->drawLine(rect.topLeft() + QPoint(0, 0), rect.topRight());
95 p->drawLine(rect.topRight() + QPoint(0, 1), rect.bottomRight() - QPoint(0, 1));
96 p->drawLine(rect.bottomLeft() + QPoint(0,-1), rect.bottomRight()-QPoint(0,1));
97 }
98
99 QTransform m;
100 if (vertical_tabs) {
101 m = QTransform::fromTranslate(rect.left(), rect.bottom());
102 m.rotate(-90);
103 } else {
104 m = QTransform::fromTranslate(rect.left(), rect.top());
105 }
106
107 const QRect draw_rect(QPoint(0, 0), m.mapRect(rect).size());
108
109 p->save();
110 p->setTransform(m);
111
112 QRect icon_rect(QPoint(8, 0), v_opt->iconSize);
113 QRect text_rect(icon_rect.topRight() + QPoint(4, 0), draw_rect.size());
114 text_rect.setRight(draw_rect.width());
115 icon_rect.translate(0, (draw_rect.height() - icon_rect.height()) / 2);
116
117 QFont boldFont(p->font());
118 boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
119 boldFont.setBold(true);
120 p->setFont(boldFont);
121 p->setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
122 int textFlags = Qt::AlignHCenter | Qt::AlignVCenter;
123 p->drawText(text_rect, textFlags, text);
124 p->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor());
125 #ifndef Q_WS_MAC
126 if (widget) {
127 const QString fader_key = "tab_" + text + "_fader";
128 const QString animation_key = "tab_" + text + "_animation";
129
130 const QString tab_hover = widget->property("tab_hover").toString();
131 int fader = widget->property(fader_key.toUtf8().constData()).toInt();
132 QPropertyAnimation* animation = widget->property(animation_key.toUtf8().constData()).value<QPropertyAnimation*>();
133
134 if (!animation) {
135 QWidget* mut_widget = const_cast<QWidget*>(widget);
136 fader = 0;
137 mut_widget->setProperty(fader_key.toUtf8().constData(), fader);
138 animation = new QPropertyAnimation(mut_widget, fader_key.toUtf8(), mut_widget);
139 connect(animation, SIGNAL(valueChanged(QVariant)), mut_widget, SLOT(update()));
140 mut_widget->setProperty(animation_key.toUtf8().constData(), QVariant::fromValue(animation));
141 }
142
143 if (text == tab_hover) {
144 if (animation->state() != QAbstractAnimation::Running && fader != 40) {
145 animation->stop();
146 animation->setDuration(80);
147 animation->setEndValue(40);
148 animation->start();
149 }
150 } else {
151 if (animation->state() != QAbstractAnimation::Running && fader != 0) {
152 animation->stop();
153 animation->setDuration(160);
154 animation->setEndValue(0);
155 animation->start();
156 }
157 }
158
159 if (!selected) {
160 p->save();
161 QLinearGradient grad(draw_rect.topLeft(), vertical_tabs ? draw_rect.bottomLeft() : draw_rect.topRight());
162 grad.setColorAt(0, Qt::transparent);
163 grad.setColorAt(0.5, QColor(255, 255, 255, fader));
164 grad.setColorAt(1, Qt::transparent);
165 p->fillRect(draw_rect, grad);
166 p->setPen(QPen(grad, 1.0));
167 p->drawLine(draw_rect.topLeft(), vertical_tabs ? draw_rect.bottomLeft() : draw_rect.topRight());
168 p->drawLine(draw_rect.bottomRight(), vertical_tabs ? draw_rect.topRight() : draw_rect.bottomLeft());
169 p->restore();
170 }
171 }
172 #endif
173
174 Utils::StyleHelper::drawIconWithShadow(v_opt->icon, icon_rect, p, QIcon::Normal);
175
176 p->drawText(text_rect.translated(0, -1), textFlags, text);
177
178 p->restore();
179 }
180
181 void FancyTabProxyStyle::polish(QWidget* widget) {
182 if (QString(widget->metaObject()->className()) == "QTabBar") {
183 widget->setMouseTracking(true);
184 widget->installEventFilter(this);
185 }
186 QProxyStyle::polish(widget);
187 }
188
189 void FancyTabProxyStyle::polish(QApplication* app) {
190 QProxyStyle::polish(app);
191 }
192
193 void FancyTabProxyStyle::polish(QPalette& palette) {
194 QProxyStyle::polish(palette);
195 }
196
197 bool FancyTabProxyStyle::eventFilter(QObject* o, QEvent* e) {
198 QTabBar* bar = qobject_cast<QTabBar*>(o);
199 if (bar && (e->type() == QEvent::MouseMove || e->type() == QEvent::Leave)) {
200 QMouseEvent* event = static_cast<QMouseEvent*>(e);
201 const QString old_hovered_tab = bar->property("tab_hover").toString();
202 const QString hovered_tab = e->type() == QEvent::Leave ? QString() : bar->tabText(bar->tabAt(event->pos()));
203 bar->setProperty("tab_hover", hovered_tab);
204
205 if (old_hovered_tab != hovered_tab)
206 bar->update();
207 }
208
209 return false;
210 }
211
212 FancyTab::FancyTab(QWidget* tabbar)
213 : QWidget(tabbar), tabbar(tabbar), m_fader(0)
214 {
215 animator.setPropertyName("fader");
216 animator.setTargetObject(this);
217 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
218 }
219
220 void FancyTab::fadeIn()
221 {
222 animator.stop();
223 animator.setDuration(80);
224 animator.setEndValue(40);
225 animator.start();
226 }
227
228 void FancyTab::fadeOut()
229 {
230 animator.stop();
231 animator.setDuration(160);
232 animator.setEndValue(0);
233 animator.start();
234 }
235
236 void FancyTab::setFader(float value)
237 {
238 m_fader = value;
239 tabbar->update();
240 }
241
242 FancyTabBar::FancyTabBar(QWidget *parent)
243 : QWidget(parent)
244 {
245 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
246 setStyle(new QCommonStyle);
247 setMinimumWidth(qMax(2 * m_rounding, 40));
248 setAttribute(Qt::WA_Hover, true);
249 setFocusPolicy(Qt::NoFocus);
250 setMouseTracking(true); // Needed for hover events
251 m_triggerTimer.setSingleShot(true);
252
253 QVBoxLayout* layout = new QVBoxLayout;
254 layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
255 layout->setSpacing(0);
256 layout->setContentsMargins(0, 0, 0, 0);
257 setLayout(layout);
258
259 // We use a zerotimer to keep the sidebar responsive
260 connect(&m_triggerTimer, SIGNAL(timeout()), this, SLOT(emitCurrentIndex()));
261 }
262
263 FancyTabBar::~FancyTabBar()
264 {
265 delete style();
266 }
267
268 QSize FancyTab::sizeHint() const {
269 QFont boldFont(font());
270 boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
271 boldFont.setBold(true);
272 QFontMetrics fm(boldFont);
273 int spacing = 8;
274 int width = 60 + spacing + 2;
275 int iconHeight = 32;
276 QSize ret(width, iconHeight + spacing + fm.height());
277 return ret;
278 }
279
280 QSize FancyTabBar::tabSizeHint(bool minimum) const
281 {
282 QFont boldFont(font());
283 boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
284 boldFont.setBold(true);
285 QFontMetrics fm(boldFont);
286 int spacing = 8;
287 int width = 60 + spacing + 2;
288 int iconHeight = minimum ? 0 : 32;
289 return QSize(width, iconHeight + spacing + fm.height());
290 }
291
292 void FancyTabBar::paintEvent(QPaintEvent *event)
293 {
294 Q_UNUSED(event)
295 QPainter p(this);
296
297 for (int i = 0; i < count(); ++i)
298 if (i != currentIndex())
299 paintTab(&p, i);
300
301 // paint active tab last, since it overlaps the neighbors
302 if (currentIndex() != -1)
303 paintTab(&p, currentIndex());
304 }
305
306 void FancyTab::enterEvent(QEvent*)
307 {
308 fadeIn();
309 }
310
311 void FancyTab::leaveEvent(QEvent*)
312 {
313 fadeOut();
314 }
315
316 QSize FancyTabBar::sizeHint() const
317 {
318 QSize sh = tabSizeHint();
319 return QSize(sh.width(), sh.height() * m_tabs.count());
320 }
321
322 QSize FancyTabBar::minimumSizeHint() const
323 {
324 QSize sh = tabSizeHint(true);
325 return QSize(sh.width(), sh.height() * m_tabs.count());
326 }
327
328 QRect FancyTabBar::tabRect(int index) const
329 {
330 return m_tabs[index]->geometry();
331 }
332
333 QString FancyTabBar::tabToolTip(int index) const {
334 return m_tabs[index]->toolTip();
335 }
336
337 void FancyTabBar::setTabToolTip(int index, const QString& toolTip) {
338 m_tabs[index]->setToolTip(toolTip);
339 }
340
341 // This keeps the sidebar responsive since
342 // we get a repaint before loading the
343 // mode itself
344 void FancyTabBar::emitCurrentIndex()
345 {
346 emit currentChanged(m_currentIndex);
347 }
348
349 void FancyTabBar::mousePressEvent(QMouseEvent *e)
350 {
351 e->accept();
352 for (int index = 0; index < m_tabs.count(); ++index) {
353 if (tabRect(index).contains(e->pos())) {
354 m_currentIndex = index;
355 update();
356 m_triggerTimer.start(0);
357 break;
358 }
359 }
360 }
361
362 void FancyTabBar::addTab(const QIcon& icon, const QString& label) {
363 FancyTab *tab = new FancyTab(this);
364 tab->icon = icon;
365 tab->text = label;
366 m_tabs.append(tab);
367 qobject_cast<QVBoxLayout*>(layout())->insertWidget(layout()->count()-1, tab);
368 }
369
370 void FancyTabBar::addSpacer(int size) {
371 qobject_cast<QVBoxLayout*>(layout())->insertSpacerItem(layout()->count()-1,
372 new QSpacerItem(0, size, QSizePolicy::Fixed, QSizePolicy::Maximum));
373 }
374
375 void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
376 {
377 if (!validIndex(tabIndex)) {
378 qWarning("invalid index");
379 return;
380 }
381 painter->save();
382
383 QRect rect = tabRect(tabIndex);
384 bool selected = (tabIndex == m_currentIndex);
385
386 if (selected) {
387 //background
388 painter->save();
389 QLinearGradient grad(rect.topLeft(), rect.topRight());
390 grad.setColorAt(0, QColor(255, 255, 255, 140));
391 grad.setColorAt(1, QColor(255, 255, 255, 210));
392 painter->fillRect(rect.adjusted(0, 0, 0, -1), grad);
393 painter->restore();
394
395 //shadows
396 painter->setPen(QColor(0, 0, 0, 110));
397 painter->drawLine(rect.topLeft() + QPoint(1,-1), rect.topRight() - QPoint(0,1));
398 painter->drawLine(rect.bottomLeft(), rect.bottomRight());
399 painter->setPen(QColor(0, 0, 0, 40));
400 painter->drawLine(rect.topLeft(), rect.bottomLeft());
401
402 //highlights
403 painter->setPen(QColor(255, 255, 255, 50));
404 painter->drawLine(rect.topLeft() + QPoint(0, -2), rect.topRight() - QPoint(0,2));
405 painter->drawLine(rect.bottomLeft() + QPoint(0, 1), rect.bottomRight() + QPoint(0,1));
406 painter->setPen(QColor(255, 255, 255, 40));
407 painter->drawLine(rect.topLeft() + QPoint(0, 0), rect.topRight());
408 painter->drawLine(rect.topRight() + QPoint(0, 1), rect.bottomRight() - QPoint(0, 1));
409 painter->drawLine(rect.bottomLeft() + QPoint(0,-1), rect.bottomRight()-QPoint(0,1));
410 }
411
412 QString tabText(painter->fontMetrics().elidedText(this->tabText(tabIndex), Qt::ElideRight, width()));
413 QRect tabTextRect(tabRect(tabIndex));
414 QRect tabIconRect(tabTextRect);
415 tabIconRect.adjust(+4, +4, -4, -4);
416 tabTextRect.translate(0, -2);
417 QFont boldFont(painter->font());
418 boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
419 boldFont.setBold(true);
420 painter->setFont(boldFont);
421 painter->setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
422 int textFlags = Qt::AlignCenter | Qt::AlignBottom;
423 painter->drawText(tabTextRect, textFlags, tabText);
424 painter->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor());
425 #ifndef Q_WS_MAC
426 if (!selected) {
427 painter->save();
428 int fader = int(m_tabs[tabIndex]->fader());
429 QLinearGradient grad(rect.topLeft(), rect.topRight());
430 grad.setColorAt(0, Qt::transparent);
431 grad.setColorAt(0.5, QColor(255, 255, 255, fader));
432 grad.setColorAt(1, Qt::transparent);
433 painter->fillRect(rect, grad);
434 painter->setPen(QPen(grad, 1.0));
435 painter->drawLine(rect.topLeft(), rect.topRight());
436 painter->drawLine(rect.bottomLeft(), rect.bottomRight());
437 painter->restore();
438 }
439 #endif
440
441 const int textHeight = painter->fontMetrics().height();
442 tabIconRect.adjust(0, 4, 0, -textHeight);
443 Utils::StyleHelper::drawIconWithShadow(tabIcon(tabIndex), tabIconRect, painter, QIcon::Normal);
444
445 painter->translate(0, -1);
446 painter->drawText(tabTextRect, textFlags, tabText);
447 painter->restore();
448 }
449
450 void FancyTabBar::setCurrentIndex(int index) {
451 m_currentIndex = index;
452 update();
453 emit currentChanged(m_currentIndex);
454 }
455
456
457 //////
458 // FancyColorButton
459 //////
460
461 class FancyColorButton : public QWidget
462 {
463 public:
464 FancyColorButton(QWidget *parent)
465 : m_parent(parent)
466 {
467 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
468 }
469
470 void mousePressEvent(QMouseEvent *ev)
471 {
472 if (ev->modifiers() & Qt::ShiftModifier)
473 Utils::StyleHelper::setBaseColor(QColorDialog::getColor(Utils::StyleHelper::requestedBaseColor(), m_parent));
474 }
475 private:
476 QWidget *m_parent;
477 };
478
479 //////
480 // FancyTabWidget
481 //////
482
483 FancyTabWidget::FancyTabWidget(QWidget* parent)
484 : QWidget(parent),
485 mode_(Mode_None),
486 tab_bar_(NULL),
487 stack_(new QStackedLayout),
488 side_widget_(new QWidget),
489 side_layout_(new QVBoxLayout),
490 top_layout_(new QVBoxLayout),
491 use_background_(false),
492 menu_(NULL),
493 proxy_style_(new FancyTabProxyStyle)
494 {
495 side_layout_->setSpacing(0);
496 side_layout_->setMargin(0);
497 side_layout_->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
498
499 side_widget_->setLayout(side_layout_);
500 side_widget_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
501
502 top_layout_->setMargin(0);
503 top_layout_->setSpacing(0);
504 top_layout_->addLayout(stack_);
505
506 QHBoxLayout* main_layout = new QHBoxLayout;
507 main_layout->setMargin(0);
508 main_layout->setSpacing(1);
509 main_layout->addWidget(side_widget_);
510 main_layout->addLayout(top_layout_);
511 setLayout(main_layout);
512 }
513
514 void FancyTabWidget::AddTab(QWidget* tab, const QIcon& icon, const QString& label) {
515 stack_->addWidget(tab);
516 items_ << Item(icon, label);
517 }
518
519 void FancyTabWidget::AddSpacer(int size) {
520 items_ << Item(size);
521 }
522
523 void FancyTabWidget::SetBackgroundPixmap(const QPixmap& pixmap) {
524 background_pixmap_ = pixmap;
525 update();
526 }
527
528 void FancyTabWidget::paintEvent(QPaintEvent*) {
529 if (!use_background_)
530 return;
531
532 QPainter painter(this);
533
534 QRect rect = side_widget_->rect().adjusted(0, 0, 1, 0);
535 rect = style()->visualRect(layoutDirection(), geometry(), rect);
536 Utils::StyleHelper::verticalGradient(&painter, rect, rect);
537
538 if (!background_pixmap_.isNull()) {
539 QRect pixmap_rect(background_pixmap_.rect());
540 pixmap_rect.moveTo(rect.topLeft());
541
542 while (pixmap_rect.top() < rect.bottom()) {
543 QRect source_rect(pixmap_rect.intersected(rect));
544 source_rect.moveTo(0, 0);
545 painter.drawPixmap(pixmap_rect.topLeft(), background_pixmap_, source_rect);
546 pixmap_rect.moveTop(pixmap_rect.bottom() - 10);
547 }
548 }
549
550 painter.setPen(Utils::StyleHelper::borderColor());
551 painter.drawLine(rect.topRight(), rect.bottomRight());
552
553 QColor light = Utils::StyleHelper::sidebarHighlight();
554 painter.setPen(light);
555 painter.drawLine(rect.bottomLeft(), rect.bottomRight());
556 }
557
558 int FancyTabWidget::current_index() const {
559 return stack_->currentIndex();
560 }
561
562 void FancyTabWidget::SetCurrentIndex(int index) {
563 if (FancyTabBar* bar = qobject_cast<FancyTabBar*>(tab_bar_)) {
564 bar->setCurrentIndex(index);
565 } else if (QTabBar* bar = qobject_cast<QTabBar*>(tab_bar_)) {
566 bar->setCurrentIndex(index);
567 } else {
568 stack_->setCurrentIndex(index);
569 }
570 }
571
572 void FancyTabWidget::ShowWidget(int index) {
573 stack_->setCurrentIndex(index);
574 emit CurrentChanged(index);
575 }
576
577 void FancyTabWidget::AddBottomWidget(QWidget* widget) {
578 top_layout_->addWidget(widget);
579 }
580
581 void FancyTabWidget::SetMode(Mode mode) {
582 // Remove previous tab bar
583 delete tab_bar_;
584 tab_bar_ = NULL;
585
586 use_background_ = false;
587
588 // Create new tab bar
589 switch (mode) {
590 case Mode_None:
591 default:
592 qDebug() << "Unknown fancy tab mode" << mode;
593 // fallthrough
594
595 case Mode_LargeSidebar: {
596 FancyTabBar* bar = new FancyTabBar(this);
597 side_layout_->insertWidget(0, bar);
598 tab_bar_ = bar;
599
600 foreach (const Item& item, items_) {
601 if (item.type_ == Item::Type_Spacer)
602 bar->addSpacer(item.spacer_size_);
603 else
604 bar->addTab(item.tab_icon_, item.tab_label_);
605 }
606
607 bar->setCurrentIndex(stack_->currentIndex());
608 connect(bar, SIGNAL(currentChanged(int)), SLOT(ShowWidget(int)));
609
610 use_background_ = true;
611
612 break;
613 }
614
615 case Mode_Tabs:
616 MakeTabBar(QTabBar::RoundedNorth, true, false, false);
617 break;
618
619 case Mode_IconOnlyTabs:
620 MakeTabBar(QTabBar::RoundedNorth, false, true, false);
621 break;
622
623 case Mode_SmallSidebar:
624 MakeTabBar(QTabBar::RoundedWest, true, true, true);
625 use_background_ = true;
626 break;
627
628 case Mode_PlainSidebar:
629 MakeTabBar(QTabBar::RoundedWest, true, true, false);
630 break;
631 }
632
633 tab_bar_->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
634
635 mode_ = mode;
636 emit ModeChanged(mode);
637 update();
638 }
639
640 void FancyTabWidget::contextMenuEvent(QContextMenuEvent* e) {
641 if (!menu_) {
642 menu_ = new QMenu(this);
643
644 QSignalMapper* mapper = new QSignalMapper(this);
645 QActionGroup* group = new QActionGroup(this);
646 AddMenuItem(mapper, group, tr("Large sidebar"), Mode_LargeSidebar);
647 AddMenuItem(mapper, group, tr("Small sidebar"), Mode_SmallSidebar);
648 AddMenuItem(mapper, group, tr("Plain sidebar"), Mode_PlainSidebar);
649 AddMenuItem(mapper, group, tr("Tabs on top"), Mode_Tabs);
650 AddMenuItem(mapper, group, tr("Icons on top"), Mode_IconOnlyTabs);
651 menu_->addActions(group->actions());
652
653 connect(mapper, SIGNAL(mapped(int)), SLOT(SetMode(int)));
654 }
655
656 menu_->popup(e->globalPos());
657 }
658
659 void FancyTabWidget::AddMenuItem(QSignalMapper* mapper, QActionGroup* group,
660 const QString& text, Mode mode) {
661 QAction* action = group->addAction(text);
662 action->setCheckable(true);
663 mapper->setMapping(action, mode);
664 connect(action, SIGNAL(triggered()), mapper, SLOT(map()));
665
666 if (mode == mode_)
667 action->setChecked(true);
668 }
669
670 void FancyTabWidget::MakeTabBar(QTabBar::Shape shape, bool text, bool icons,
671 bool fancy) {
672 QTabBar* bar = new QTabBar(this);
673 bar->setShape(shape);
674 bar->setDocumentMode(true);
675 bar->setUsesScrollButtons(true);
676
677 if (shape == QTabBar::RoundedWest) {
678 bar->setIconSize(QSize(22, 22));
679 }
680
681 if (fancy) {
682 bar->setStyle(proxy_style_.data());
683 }
684
685 if (shape == QTabBar::RoundedNorth)
686 top_layout_->insertWidget(0, bar);
687 else
688 side_layout_->insertWidget(0, bar);
689
690 foreach (const Item& item, items_) {
691 if (item.type_ != Item::Type_Tab)
692 continue;
693
694 QString label = item.tab_label_;
695 if (shape == QTabBar::RoundedWest) {
696 label = QFontMetrics(font()).elidedText(label, Qt::ElideMiddle, 100);
697 }
698
699 int tab_id = -1;
700 if (icons && text)
701 tab_id = bar->addTab(item.tab_icon_, label);
702 else if (icons)
703 tab_id = bar->addTab(item.tab_icon_, QString());
704 else if (text)
705 tab_id = bar->addTab(label);
706
707 bar->setTabToolTip(tab_id, item.tab_label_);
708 }
709
710 bar->setCurrentIndex(stack_->currentIndex());
711 connect(bar, SIGNAL(currentChanged(int)), SLOT(ShowWidget(int)));
712 tab_bar_ = bar;
713 }