From 61ccb1a585e6d64c658df8f7fc9dea184c56d341 Mon Sep 17 00:00:00 2001 From: Jens Bache-Wiig <jbache@trolltech.com> Date: Tue, 16 Feb 2010 15:36:56 +0100 Subject: [PATCH] Add api to enable and disable sidebar tabs --- src/plugins/coreplugin/fancytabwidget.cpp | 58 +++++++++++++++++++---- src/plugins/coreplugin/fancytabwidget.h | 8 ++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/plugins/coreplugin/fancytabwidget.cpp b/src/plugins/coreplugin/fancytabwidget.cpp index 647def0006b..7c79bf026db 100644 --- a/src/plugins/coreplugin/fancytabwidget.cpp +++ b/src/plugins/coreplugin/fancytabwidget.cpp @@ -205,6 +205,7 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const bool selected = (tabIndex == m_currentIndex); bool hover = (tabIndex == m_hoverIndex); + bool enabled = isTabEnabled(tabIndex); #ifdef Q_WS_MAC hover = false; // Do not hover on Mac @@ -233,7 +234,7 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const painter->drawLine(rect.bottomLeft(), rect.bottomRight()); } else { painter->fillRect(rect, background); - if (hover) + if (hover && enabled) painter->fillRect(rect, hoverColor); painter->setPen(QPen(light, 0)); painter->drawLine(rect.topLeft(), rect.topRight()); @@ -250,25 +251,55 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const painter->setFont(boldFont); painter->setPen(selected ? Utils::StyleHelper::panelTextColor() : QColor(30, 30, 30, 80)); int textFlags = Qt::AlignCenter | Qt::AlignBottom | Qt::ElideRight | Qt::TextWordWrap; - painter->drawText(tabTextRect, textFlags, tabText); - painter->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor()); + if (enabled) { + painter->drawText(tabTextRect, textFlags, tabText); + painter->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor()); + } else { + painter->setPen(selected ? Utils::StyleHelper::panelTextColor() : QColor(255, 255, 255, 120)); + } + int textHeight = painter->fontMetrics().boundingRect(QRect(0, 0, width(), height()), Qt::TextWordWrap, tabText).height(); tabIconRect.adjust(0, 4, 0, -textHeight); int iconSize = qMin(tabIconRect.width(), tabIconRect.height()); if (iconSize > 4) style()->drawItemPixmap(painter, tabIconRect, Qt::AlignCenter | Qt::AlignVCenter, - tabIcon(tabIndex).pixmap(tabIconRect.size())); + tabIcon(tabIndex).pixmap(tabIconRect.size(), enabled ? QIcon::Normal : QIcon::Disabled)); painter->translate(0, -1); painter->drawText(tabTextRect, textFlags, tabText); painter->restore(); } void FancyTabBar::setCurrentIndex(int index) { - m_currentIndex = index; - update(); - emit currentChanged(index); + if (isTabEnabled(index)) { + m_currentIndex = index; + update(); + emit currentChanged(index); + } } +void FancyTabBar::setTabEnabled(int index, bool enable) +{ + Q_ASSERT(index < m_tabs.size()); + Q_ASSERT(index >= 0); + + if (index < m_tabs.size() && index >= 0) { + m_tabs[index].enabled = enable; + update(tabRect(index)); + } +} + +bool FancyTabBar::isTabEnabled(int index) const +{ + Q_ASSERT(index < m_tabs.size()); + Q_ASSERT(index >= 0); + + if (index < m_tabs.size() && index >= 0) + return m_tabs[index].enabled; + + return false; +} + + ////// // FancyColorButton ////// @@ -410,7 +441,8 @@ QStatusBar *FancyTabWidget::statusBar() const void FancyTabWidget::setCurrentIndex(int index) { - m_tabBar->setCurrentIndex(index); + if (m_tabBar->isTabEnabled(index)) + m_tabBar->setCurrentIndex(index); } void FancyTabWidget::showWidget(int index) @@ -424,3 +456,13 @@ void FancyTabWidget::setTabToolTip(int index, const QString &toolTip) { m_tabBar->setTabToolTip(index, toolTip); } + +void FancyTabWidget::setTabEnabled(int index, bool enable) +{ + m_tabBar->setTabEnabled(index, enable); +} + +bool FancyTabWidget::isTabEnabled(int index) const +{ + return m_tabBar->isTabEnabled(index); +} diff --git a/src/plugins/coreplugin/fancytabwidget.h b/src/plugins/coreplugin/fancytabwidget.h index a9f8485d63f..22163ce3a58 100644 --- a/src/plugins/coreplugin/fancytabwidget.h +++ b/src/plugins/coreplugin/fancytabwidget.h @@ -48,6 +48,7 @@ namespace Internal { QIcon icon; QString text; QString toolTip; + bool enabled; }; class FancyTabBar : public QWidget @@ -70,8 +71,12 @@ public: QSize sizeHint() const; QSize minimumSizeHint() const; + void setTabEnabled(int index, bool enable); + bool isTabEnabled(int index) const; + void insertTab(int index, const QIcon &icon, const QString &label) { FancyTab tab; + tab.enabled = true; tab.icon = icon; tab.text = label; m_tabs.insert(index, tab); @@ -130,6 +135,9 @@ public: int currentIndex() const; QStatusBar *statusBar() const; + void setTabEnabled(int index, bool enable); + bool isTabEnabled(int index) const; + signals: void currentAboutToShow(int index); void currentChanged(int index); -- GitLab