481 lines
14 KiB
C++
481 lines
14 KiB
C++
/**************************************************************************
|
|
**
|
|
** This file is part of Qt Creator
|
|
**
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
**
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
**
|
|
** Commercial Usage
|
|
**
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
** a written agreement between you and Nokia.
|
|
**
|
|
** 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.
|
|
**
|
|
** If you are unsure which license is appropriate for your use, please
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
**
|
|
**************************************************************************/
|
|
|
|
#include "fancytabwidget.h"
|
|
#include "stylehelper.h"
|
|
#include "styledbar.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QtGui/QColorDialog>
|
|
#include <QtGui/QHBoxLayout>
|
|
#include <QtGui/QVBoxLayout>
|
|
#include <QtGui/QMouseEvent>
|
|
#include <QtGui/QWindowsStyle>
|
|
#include <QtGui/QPainter>
|
|
#include <QtGui/QSplitter>
|
|
#include <QtGui/QStackedLayout>
|
|
#include <QtGui/QStatusBar>
|
|
#include <QtGui/QToolButton>
|
|
#include <QtGui/QToolTip>
|
|
#include <QtCore/QAnimationGroup>
|
|
#include <QtCore/QPropertyAnimation>
|
|
|
|
using namespace Core;
|
|
using namespace Internal;
|
|
|
|
const int FancyTabBar::m_rounding = 22;
|
|
const int FancyTabBar::m_textPadding = 4;
|
|
|
|
FancyTab::FancyTab(QWidget* tabbar)
|
|
: QWidget(tabbar), tabbar(tabbar), m_fader(0)
|
|
{
|
|
animator.setPropertyName("fader");
|
|
animator.setTargetObject(this);
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
|
|
}
|
|
|
|
void FancyTab::fadeIn()
|
|
{
|
|
animator.stop();
|
|
animator.setDuration(80);
|
|
animator.setEndValue(40);
|
|
animator.start();
|
|
}
|
|
|
|
void FancyTab::fadeOut()
|
|
{
|
|
animator.stop();
|
|
animator.setDuration(160);
|
|
animator.setEndValue(0);
|
|
animator.start();
|
|
}
|
|
|
|
void FancyTab::setFader(float value)
|
|
{
|
|
m_fader = value;
|
|
tabbar->update();
|
|
}
|
|
|
|
FancyTabBar::FancyTabBar(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
|
|
setStyle(new QWindowsStyle);
|
|
setMinimumWidth(qMax(2 * m_rounding, 40));
|
|
setAttribute(Qt::WA_Hover, true);
|
|
setFocusPolicy(Qt::NoFocus);
|
|
setMouseTracking(true); // Needed for hover events
|
|
m_triggerTimer.setSingleShot(true);
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout;
|
|
layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
|
|
layout->setSpacing(0);
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
setLayout(layout);
|
|
|
|
// We use a zerotimer to keep the sidebar responsive
|
|
connect(&m_triggerTimer, SIGNAL(timeout()), this, SLOT(emitCurrentIndex()));
|
|
}
|
|
|
|
FancyTabBar::~FancyTabBar()
|
|
{
|
|
delete style();
|
|
}
|
|
|
|
QSize FancyTab::sizeHint() const {
|
|
QFont boldFont(font());
|
|
boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
|
|
boldFont.setBold(true);
|
|
QFontMetrics fm(boldFont);
|
|
int spacing = 8;
|
|
int width = 60 + spacing + 2;
|
|
int iconHeight = 32;
|
|
QSize ret(width, iconHeight + spacing + fm.height());
|
|
return ret;
|
|
}
|
|
|
|
QSize FancyTabBar::tabSizeHint(bool minimum) const
|
|
{
|
|
QFont boldFont(font());
|
|
boldFont.setPointSizeF(Utils::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(m_tabs[tab]->text);
|
|
if (width > maxLabelwidth)
|
|
maxLabelwidth = width;
|
|
}
|
|
int iconHeight = minimum ? 0 : 32;
|
|
return QSize(qMax(width, maxLabelwidth + 4), iconHeight + spacing + fm.height());
|
|
}
|
|
|
|
void FancyTabBar::paintEvent(QPaintEvent *event)
|
|
{
|
|
Q_UNUSED(event)
|
|
QPainter p(this);
|
|
|
|
for (int i = 0; i < count(); ++i)
|
|
if (i != currentIndex())
|
|
paintTab(&p, i);
|
|
|
|
// paint active tab last, since it overlaps the neighbors
|
|
if (currentIndex() != -1)
|
|
paintTab(&p, currentIndex());
|
|
}
|
|
|
|
// Resets hover animation on mouse enter
|
|
void FancyTab::enterEvent(QEvent*)
|
|
{
|
|
fadeIn();
|
|
}
|
|
|
|
// Resets hover animation on mouse enter
|
|
void FancyTab::leaveEvent(QEvent*)
|
|
{
|
|
fadeOut();
|
|
}
|
|
|
|
QSize FancyTabBar::sizeHint() const
|
|
{
|
|
QSize sh = tabSizeHint();
|
|
return QSize(sh.width(), sh.height() * m_tabs.count());
|
|
}
|
|
|
|
QSize FancyTabBar::minimumSizeHint() const
|
|
{
|
|
QSize sh = tabSizeHint(true);
|
|
return QSize(sh.width(), sh.height() * m_tabs.count());
|
|
}
|
|
|
|
QRect FancyTabBar::tabRect(int index) const
|
|
{
|
|
return m_tabs[index]->geometry();
|
|
}
|
|
|
|
QString FancyTabBar::tabToolTip(int index) const {
|
|
return m_tabs[index]->toolTip();
|
|
}
|
|
|
|
void FancyTabBar::setTabToolTip(int index, const QString& toolTip) {
|
|
m_tabs[index]->setToolTip(toolTip);
|
|
}
|
|
|
|
// This keeps the sidebar responsive since
|
|
// we get a repaint before loading the
|
|
// mode itself
|
|
void FancyTabBar::emitCurrentIndex()
|
|
{
|
|
emit currentChanged(m_currentIndex);
|
|
}
|
|
|
|
void FancyTabBar::mousePressEvent(QMouseEvent *e)
|
|
{
|
|
e->accept();
|
|
for (int index = 0; index < m_tabs.count(); ++index) {
|
|
if (tabRect(index).contains(e->pos())) {
|
|
m_currentIndex = index;
|
|
update();
|
|
m_triggerTimer.start(0);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void FancyTabBar::addTab(const QIcon& icon, const QString& label) {
|
|
FancyTab *tab = new FancyTab(this);
|
|
tab->icon = icon;
|
|
tab->text = label;
|
|
m_tabs.append(tab);
|
|
qobject_cast<QVBoxLayout*>(layout())->insertWidget(layout()->count()-1, tab);
|
|
}
|
|
|
|
void FancyTabBar::addSpacer(int size) {
|
|
qobject_cast<QVBoxLayout*>(layout())->insertSpacerItem(layout()->count()-1,
|
|
new QSpacerItem(0, size, QSizePolicy::Fixed, QSizePolicy::Maximum));
|
|
}
|
|
|
|
void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
|
|
{
|
|
if (!validIndex(tabIndex)) {
|
|
qWarning("invalid index");
|
|
return;
|
|
}
|
|
painter->save();
|
|
|
|
QRect rect = tabRect(tabIndex);
|
|
bool selected = (tabIndex == m_currentIndex);
|
|
|
|
if (selected) {
|
|
//background
|
|
painter->save();
|
|
QLinearGradient grad(rect.topLeft(), rect.topRight());
|
|
grad.setColorAt(0, QColor(255, 255, 255, 140));
|
|
grad.setColorAt(1, QColor(255, 255, 255, 210));
|
|
painter->fillRect(rect.adjusted(0, 0, 0, -1), grad);
|
|
painter->restore();
|
|
|
|
//shadows
|
|
painter->setPen(QColor(0, 0, 0, 110));
|
|
painter->drawLine(rect.topLeft() + QPoint(1,-1), rect.topRight() - QPoint(0,1));
|
|
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
|
|
painter->setPen(QColor(0, 0, 0, 40));
|
|
painter->drawLine(rect.topLeft(), rect.bottomLeft());
|
|
|
|
//highlights
|
|
painter->setPen(QColor(255, 255, 255, 50));
|
|
painter->drawLine(rect.topLeft() + QPoint(0, -2), rect.topRight() - QPoint(0,2));
|
|
painter->drawLine(rect.bottomLeft() + QPoint(0, 1), rect.bottomRight() + QPoint(0,1));
|
|
painter->setPen(QColor(255, 255, 255, 40));
|
|
painter->drawLine(rect.topLeft() + QPoint(0, 0), rect.topRight());
|
|
painter->drawLine(rect.topRight() + QPoint(0, 1), rect.bottomRight() - QPoint(0, 1));
|
|
painter->drawLine(rect.bottomLeft() + QPoint(0,-1), rect.bottomRight()-QPoint(0,1));
|
|
}
|
|
|
|
QString tabText(this->tabText(tabIndex));
|
|
QRect tabTextRect(tabRect(tabIndex));
|
|
QRect tabIconRect(tabTextRect);
|
|
tabIconRect.adjust(+4, +4, -4, -4);
|
|
tabTextRect.translate(0, -2);
|
|
QFont boldFont(painter->font());
|
|
boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
|
|
boldFont.setBold(true);
|
|
painter->setFont(boldFont);
|
|
painter->setPen(selected ? QColor(255, 255, 255, 160) : QColor(0, 0, 0, 110));
|
|
int textFlags = Qt::AlignCenter | Qt::AlignBottom | Qt::TextWordWrap;
|
|
painter->drawText(tabTextRect, textFlags, tabText);
|
|
painter->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor());
|
|
#ifndef Q_WS_MAC
|
|
if (!selected) {
|
|
painter->save();
|
|
int fader = int(m_tabs[tabIndex]->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();
|
|
}
|
|
#endif
|
|
|
|
int textHeight = painter->fontMetrics().boundingRect(QRect(0, 0, width(), height()), Qt::TextWordWrap, tabText).height();
|
|
tabIconRect.adjust(0, 4, 0, -textHeight);
|
|
Utils::StyleHelper::drawIconWithShadow(tabIcon(tabIndex), tabIconRect, painter, QIcon::Normal);
|
|
|
|
painter->translate(0, -1);
|
|
painter->drawText(tabTextRect, textFlags, tabText);
|
|
painter->restore();
|
|
}
|
|
|
|
void FancyTabBar::setCurrentIndex(int index) {
|
|
m_currentIndex = index;
|
|
update();
|
|
emit currentChanged(m_currentIndex);
|
|
}
|
|
|
|
|
|
//////
|
|
// FancyColorButton
|
|
//////
|
|
|
|
class FancyColorButton : public QWidget
|
|
{
|
|
public:
|
|
FancyColorButton(QWidget *parent)
|
|
: m_parent(parent)
|
|
{
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
|
|
}
|
|
|
|
void mousePressEvent(QMouseEvent *ev)
|
|
{
|
|
if (ev->modifiers() & Qt::ShiftModifier)
|
|
Utils::StyleHelper::setBaseColor(QColorDialog::getColor(Utils::StyleHelper::requestedBaseColor(), m_parent));
|
|
}
|
|
private:
|
|
QWidget *m_parent;
|
|
};
|
|
|
|
//////
|
|
// FancyTabWidget
|
|
//////
|
|
|
|
FancyTabWidget::FancyTabWidget(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
m_tabBar = new FancyTabBar(this);
|
|
|
|
m_selectionWidget = new QWidget(this);
|
|
QVBoxLayout *selectionLayout = new QVBoxLayout;
|
|
selectionLayout->setSpacing(0);
|
|
selectionLayout->setMargin(0);
|
|
|
|
Utils::StyledBar *bar = new Utils::StyledBar;
|
|
QHBoxLayout *layout = new QHBoxLayout(bar);
|
|
layout->setMargin(0);
|
|
layout->setSpacing(0);
|
|
layout->addWidget(new FancyColorButton(this));
|
|
selectionLayout->addWidget(bar);
|
|
|
|
selectionLayout->addWidget(m_tabBar, 1);
|
|
m_selectionWidget->setLayout(selectionLayout);
|
|
m_selectionWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
|
|
|
m_cornerWidgetContainer = new QWidget(this);
|
|
m_cornerWidgetContainer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
|
|
m_cornerWidgetContainer->setAutoFillBackground(false);
|
|
|
|
QVBoxLayout *cornerWidgetLayout = new QVBoxLayout;
|
|
cornerWidgetLayout->setSpacing(0);
|
|
cornerWidgetLayout->setMargin(0);
|
|
cornerWidgetLayout->addStretch();
|
|
m_cornerWidgetContainer->setLayout(cornerWidgetLayout);
|
|
|
|
selectionLayout->addWidget(m_cornerWidgetContainer, 0);
|
|
|
|
m_modesStack = new QStackedLayout;
|
|
m_statusBar = new QStatusBar;
|
|
m_statusBar->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
|
|
|
|
QVBoxLayout *vlayout = new QVBoxLayout;
|
|
vlayout->setMargin(0);
|
|
vlayout->setSpacing(0);
|
|
vlayout->addLayout(m_modesStack);
|
|
vlayout->addWidget(m_statusBar);
|
|
|
|
QHBoxLayout *mainLayout = new QHBoxLayout;
|
|
mainLayout->setMargin(0);
|
|
mainLayout->setSpacing(1);
|
|
mainLayout->addWidget(m_selectionWidget);
|
|
mainLayout->addLayout(vlayout);
|
|
setLayout(mainLayout);
|
|
|
|
connect(m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(showWidget(int)));
|
|
}
|
|
|
|
void FancyTabWidget::addTab(QWidget *tab, const QIcon &icon, const QString &label)
|
|
{
|
|
m_modesStack->addWidget(tab);
|
|
m_tabBar->addTab(icon, label);
|
|
}
|
|
|
|
void FancyTabWidget::addSpacer(int size) {
|
|
m_tabBar->addSpacer(size);
|
|
}
|
|
|
|
void FancyTabWidget::removeTab(int index)
|
|
{
|
|
m_modesStack->removeWidget(m_modesStack->widget(index));
|
|
m_tabBar->removeTab(index);
|
|
}
|
|
|
|
void FancyTabWidget::setBackgroundPixmap(const QPixmap& pixmap)
|
|
{
|
|
m_backgroundPixmap = pixmap;
|
|
update();
|
|
}
|
|
|
|
void FancyTabWidget::paintEvent(QPaintEvent *event)
|
|
{
|
|
Q_UNUSED(event)
|
|
QPainter painter(this);
|
|
|
|
QRect rect = m_selectionWidget->rect().adjusted(0, 0, 1, 0);
|
|
rect = style()->visualRect(layoutDirection(), geometry(), rect);
|
|
Utils::StyleHelper::verticalGradient(&painter, rect, rect);
|
|
|
|
if (!m_backgroundPixmap.isNull()) {
|
|
QRect pixmap_rect(m_backgroundPixmap.rect());
|
|
pixmap_rect.moveTo(rect.topLeft());
|
|
|
|
while (pixmap_rect.top() < rect.bottom()) {
|
|
QRect source_rect(pixmap_rect.intersected(rect));
|
|
source_rect.moveTo(0, 0);
|
|
painter.drawPixmap(pixmap_rect.topLeft(), m_backgroundPixmap, source_rect);
|
|
pixmap_rect.moveTop(pixmap_rect.bottom() - 10);
|
|
}
|
|
}
|
|
|
|
painter.setPen(Utils::StyleHelper::borderColor());
|
|
painter.drawLine(rect.topRight(), rect.bottomRight());
|
|
|
|
QColor light = Utils::StyleHelper::sidebarHighlight();
|
|
painter.setPen(light);
|
|
painter.drawLine(rect.bottomLeft(), rect.bottomRight());
|
|
}
|
|
|
|
void FancyTabWidget::insertCornerWidget(int pos, QWidget *widget)
|
|
{
|
|
QVBoxLayout *layout = static_cast<QVBoxLayout *>(m_cornerWidgetContainer->layout());
|
|
layout->insertWidget(pos, widget);
|
|
}
|
|
|
|
int FancyTabWidget::cornerWidgetCount() const
|
|
{
|
|
return m_cornerWidgetContainer->layout()->count();
|
|
}
|
|
|
|
void FancyTabWidget::addCornerWidget(QWidget *widget)
|
|
{
|
|
m_cornerWidgetContainer->layout()->addWidget(widget);
|
|
}
|
|
|
|
int FancyTabWidget::currentIndex() const
|
|
{
|
|
return m_tabBar->currentIndex();
|
|
}
|
|
|
|
QStatusBar *FancyTabWidget::statusBar() const
|
|
{
|
|
return m_statusBar;
|
|
}
|
|
|
|
void FancyTabWidget::setCurrentIndex(int index)
|
|
{
|
|
m_tabBar->setCurrentIndex(index);
|
|
}
|
|
|
|
void FancyTabWidget::showWidget(int index)
|
|
{
|
|
emit currentAboutToShow(index);
|
|
m_modesStack->setCurrentIndex(index);
|
|
emit currentChanged(index);
|
|
}
|
|
|
|
void FancyTabWidget::setTabToolTip(int index, const QString &toolTip)
|
|
{
|
|
m_tabBar->setTabToolTip(index, toolTip);
|
|
}
|