Add api to enable and disable sidebar tabs

This commit is contained in:
Jens Bache-Wiig
2010-02-16 15:36:56 +01:00
parent ced28c7090
commit 61ccb1a585
2 changed files with 58 additions and 8 deletions

View File

@@ -205,6 +205,7 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
bool selected = (tabIndex == m_currentIndex); bool selected = (tabIndex == m_currentIndex);
bool hover = (tabIndex == m_hoverIndex); bool hover = (tabIndex == m_hoverIndex);
bool enabled = isTabEnabled(tabIndex);
#ifdef Q_WS_MAC #ifdef Q_WS_MAC
hover = false; // Do not hover on 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()); painter->drawLine(rect.bottomLeft(), rect.bottomRight());
} else { } else {
painter->fillRect(rect, background); painter->fillRect(rect, background);
if (hover) if (hover && enabled)
painter->fillRect(rect, hoverColor); painter->fillRect(rect, hoverColor);
painter->setPen(QPen(light, 0)); painter->setPen(QPen(light, 0));
painter->drawLine(rect.topLeft(), rect.topRight()); painter->drawLine(rect.topLeft(), rect.topRight());
@@ -250,25 +251,55 @@ void FancyTabBar::paintTab(QPainter *painter, int tabIndex) const
painter->setFont(boldFont); painter->setFont(boldFont);
painter->setPen(selected ? Utils::StyleHelper::panelTextColor() : QColor(30, 30, 30, 80)); painter->setPen(selected ? Utils::StyleHelper::panelTextColor() : QColor(30, 30, 30, 80));
int textFlags = Qt::AlignCenter | Qt::AlignBottom | Qt::ElideRight | Qt::TextWordWrap; int textFlags = Qt::AlignCenter | Qt::AlignBottom | Qt::ElideRight | Qt::TextWordWrap;
painter->drawText(tabTextRect, textFlags, tabText); if (enabled) {
painter->setPen(selected ? QColor(60, 60, 60) : Utils::StyleHelper::panelTextColor()); 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(); int textHeight = painter->fontMetrics().boundingRect(QRect(0, 0, width(), height()), Qt::TextWordWrap, tabText).height();
tabIconRect.adjust(0, 4, 0, -textHeight); tabIconRect.adjust(0, 4, 0, -textHeight);
int iconSize = qMin(tabIconRect.width(), tabIconRect.height()); int iconSize = qMin(tabIconRect.width(), tabIconRect.height());
if (iconSize > 4) if (iconSize > 4)
style()->drawItemPixmap(painter, tabIconRect, Qt::AlignCenter | Qt::AlignVCenter, 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->translate(0, -1);
painter->drawText(tabTextRect, textFlags, tabText); painter->drawText(tabTextRect, textFlags, tabText);
painter->restore(); painter->restore();
} }
void FancyTabBar::setCurrentIndex(int index) { void FancyTabBar::setCurrentIndex(int index) {
m_currentIndex = index; if (isTabEnabled(index)) {
update(); m_currentIndex = index;
emit currentChanged(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 // FancyColorButton
////// //////
@@ -410,7 +441,8 @@ QStatusBar *FancyTabWidget::statusBar() const
void FancyTabWidget::setCurrentIndex(int index) void FancyTabWidget::setCurrentIndex(int index)
{ {
m_tabBar->setCurrentIndex(index); if (m_tabBar->isTabEnabled(index))
m_tabBar->setCurrentIndex(index);
} }
void FancyTabWidget::showWidget(int index) void FancyTabWidget::showWidget(int index)
@@ -424,3 +456,13 @@ void FancyTabWidget::setTabToolTip(int index, const QString &toolTip)
{ {
m_tabBar->setTabToolTip(index, 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);
}

View File

@@ -48,6 +48,7 @@ namespace Internal {
QIcon icon; QIcon icon;
QString text; QString text;
QString toolTip; QString toolTip;
bool enabled;
}; };
class FancyTabBar : public QWidget class FancyTabBar : public QWidget
@@ -70,8 +71,12 @@ public:
QSize sizeHint() const; QSize sizeHint() const;
QSize minimumSizeHint() 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) { void insertTab(int index, const QIcon &icon, const QString &label) {
FancyTab tab; FancyTab tab;
tab.enabled = true;
tab.icon = icon; tab.icon = icon;
tab.text = label; tab.text = label;
m_tabs.insert(index, tab); m_tabs.insert(index, tab);
@@ -130,6 +135,9 @@ public:
int currentIndex() const; int currentIndex() const;
QStatusBar *statusBar() const; QStatusBar *statusBar() const;
void setTabEnabled(int index, bool enable);
bool isTabEnabled(int index) const;
signals: signals:
void currentAboutToShow(int index); void currentAboutToShow(int index);
void currentChanged(int index); void currentChanged(int index);