From 4da137b0d5c375e4ae826e84cc5434661dc5c233 Mon Sep 17 00:00:00 2001 From: Denis Dzyubenko Date: Fri, 27 Jun 2008 11:06:16 +0200 Subject: [PATCH] Implements tabBackgroundBrush property for QTabBar Added a tabBackgroundBrush/setTabBackgroundBrush functions that allows to specify a brush that is used to draw the background of the tab. It is a hint to the style, so it isn't used by all styles. For example, on Mac OS X the tabBackgroundBrush isn't used. --- src/gui/styles/qcleanlooksstyle.cpp | 15 +++++++- src/gui/styles/qcommonstyle.cpp | 20 +++++++++- src/gui/styles/qmotifstyle.cpp | 18 +++++++-- src/gui/styles/qplastiquestyle.cpp | 15 +++++++- src/gui/styles/qstyleoption.cpp | 10 +++++ src/gui/styles/qstyleoption.h | 1 + src/gui/styles/qwindowscestyle.cpp | 62 ++++++++++++++++++++++--------- src/gui/styles/qwindowsmobilestyle.cpp | 50 +++++++++++++++++++------ src/gui/styles/qwindowsstyle.cpp | 51 ++++++++++++++++++++------ src/gui/styles/qwindowsxpstyle.cpp | 14 +++++++ src/gui/widgets/qtabbar.cpp | 50 +++++++++++++++++++++++++- src/gui/widgets/qtabbar.h | 3 ++ src/gui/widgets/qtabbar_p.h | 1 + 13 files changed, 257 insertions(+), 53 deletions(-) diff --git a/src/gui/styles/qcleanlooksstyle.cpp b/src/gui/styles/qcleanlooksstyle.cpp index 01f19c6..0dbff0a 100644 --- a/src/gui/styles/qcleanlooksstyle.cpp +++ b/src/gui/styles/qcleanlooksstyle.cpp @@ -2214,6 +2214,11 @@ void QCleanlooksStyle::drawControl(ControlElement element, const QStyleOption *o int y1 = r2.top(); int y2 = r2.bottom(); + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(tab); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + QTransform rotMatrix; bool flip = false; painter->setPen(shadow); @@ -2266,12 +2271,18 @@ void QCleanlooksStyle::drawControl(ControlElement element, const QStyleOption *o else if (selected) { gradient.setColorAt(0, option->palette.background().color().lighter(104)); gradient.setColorAt(1, tabFrameColor); - painter->fillRect(rect.adjusted(0, 2, 0, -1), gradient); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(rect.adjusted(0, 2, 0, -1), gradient); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(rect.adjusted(0, 2, 0, -1), userBrush); } else { y1 += 2; gradient.setColorAt(0, option->palette.background().color()); gradient.setColorAt(1, dark.lighter(120)); - painter->fillRect(rect.adjusted(0, 2, 0, -2), gradient); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(rect.adjusted(0, 2, 0, -2), gradient); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(rect.adjusted(0, 2, 0, -2), userBrush); } // Delete border diff --git a/src/gui/styles/qcommonstyle.cpp b/src/gui/styles/qcommonstyle.cpp index 7b8b75e..50e96ce 100644 --- a/src/gui/styles/qcommonstyle.cpp +++ b/src/gui/styles/qcommonstyle.cpp @@ -1823,6 +1823,12 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, } p->setPen(QPen(tab->palette.foreground(), 0)); + + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(opt); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + if (selected) { p->setBrush(tab->palette.base()); } else { @@ -1861,7 +1867,12 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, QPainterPath path; path.addPolygon(a); - p->drawPath(path); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->drawPath(path); + if (userBrush.style() != Qt::NoBrush) { + p->setBrush(userBrush); + p->drawPath(path); + } break; } case QTabBar::TriangularEast: case QTabBar::TriangularWest: { @@ -1885,7 +1896,12 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, p->translate(0.5, 0); QPainterPath path; path.addPolygon(a); - p->drawPath(path); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->drawPath(path); + if (userBrush.style() != Qt::NoBrush) { + p->setBrush(userBrush); + p->drawPath(path); + } break; } default: break; diff --git a/src/gui/styles/qmotifstyle.cpp b/src/gui/styles/qmotifstyle.cpp index 328b1c9..b200780 100644 --- a/src/gui/styles/qmotifstyle.cpp +++ b/src/gui/styles/qmotifstyle.cpp @@ -973,9 +973,18 @@ void QMotifStyle::drawControl(ControlElement element, const QStyleOption *opt, Q QColor tabLight = opt->palette.light().color(); QColor tabDark = opt->palette.dark().color(); + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(opt); + QBrush brush(tab->palette.background()); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) { + if (tab3->backgroundBrush.isOpaque()) + p->fillRect(opt->rect.adjusted(default_frame, default_frame, + -default_frame, -default_frame), + brush); + brush = tab3->backgroundBrush; + } p->fillRect(opt->rect.adjusted(default_frame, default_frame, -default_frame, -default_frame), - tab->palette.background()); + brush); if(tab->shape == QTabBar::RoundedWest) { tabDark = opt->palette.light().color(); @@ -1011,9 +1020,10 @@ void QMotifStyle::drawControl(ControlElement element, const QStyleOption *opt, Q } if (opt->state & State_Selected) { - p->fillRect(QRect(tabRect.left()+1, tabRect.bottom()-frame_offset, - tabRect.width()-3, 2), - tab->palette.brush(QPalette::Active, QPalette::Background)); + if (!tab3 || tab3->backgroundBrush.style() == Qt::NoBrush) + p->fillRect(QRect(tabRect.left()+1, tabRect.bottom()-frame_offset, + tabRect.width()-3, 2), + tab->palette.brush(QPalette::Active, QPalette::Background)); p->setPen(tab->palette.background().color()); p->drawLine(tabRect.left()+1, tabRect.bottom(), tabRect.left()+1, tabRect.top()+2); diff --git a/src/gui/styles/qplastiquestyle.cpp b/src/gui/styles/qplastiquestyle.cpp index cd0bd0a..27dc3a2 100644 --- a/src/gui/styles/qplastiquestyle.cpp +++ b/src/gui/styles/qplastiquestyle.cpp @@ -2059,6 +2059,11 @@ void QPlastiqueStyle::drawControl(ControlElement element, const QStyleOption *op && !leftCornerWidget; bool reverseShadow = false; + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(tab); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + int borderThickness = proxy()->pixelMetric(PM_TabBarBaseOverlap, tab, widget); int marginLeft = 0; if ((atBeginning && !selected) || (selected && leftCornerWidget && ((tab->position == QStyleOptionTab::Beginning) || onlyTab))) { @@ -2237,7 +2242,10 @@ void QPlastiqueStyle::drawControl(ControlElement element, const QStyleOption *op if (fillRect.isValid()) { if (selected) { fillRect = QRect(startPoint, endPoint + vectorLeft + vectorDown * 3).normalized(); - painter->fillRect(fillRect, option->palette.window()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(fillRect, option->palette.window()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(fillRect, userBrush); // Connect to the base painter->setPen(QPen(option->palette.window(), 0)); @@ -2264,7 +2272,10 @@ void QPlastiqueStyle::drawControl(ControlElement element, const QStyleOption *op buttonGradientBrush = QBrush(buttonGradient); } - painter->fillRect(fillRect, buttonGradientBrush); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(fillRect, buttonGradientBrush); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(fillRect, userBrush); } } } diff --git a/src/gui/styles/qstyleoption.cpp b/src/gui/styles/qstyleoption.cpp index e174370..9d8b33f 100644 --- a/src/gui/styles/qstyleoption.cpp +++ b/src/gui/styles/qstyleoption.cpp @@ -2170,6 +2170,14 @@ QStyleOptionTabV2 &QStyleOptionTabV2::operator=(const QStyleOptionTab &other) */ /*! + \variable QStyleOptionTabV3::backgroundBrush + \brief the brush set by the user that is supposed to be used to + draw background of the tab. + + The default value is QBrush(), i.e. an invalid brush; +*/ + +/*! Constructs a QStyleOptionTabV3. */ @@ -2237,10 +2245,12 @@ QStyleOptionTabV3 &QStyleOptionTabV3::operator=(const QStyleOptionTab &other) if (const QStyleOptionTabV3 *tab = qstyleoption_cast(&other)) { leftButtonSize = tab->leftButtonSize; rightButtonSize = tab->rightButtonSize; + backgroundBrush = tab->backgroundBrush; } else { leftButtonSize = QSize(); rightButtonSize = QSize(); documentMode = false; + backgroundBrush = QBrush(); } return *this; } diff --git a/src/gui/styles/qstyleoption.h b/src/gui/styles/qstyleoption.h index 710c696..86a7f0c 100644 --- a/src/gui/styles/qstyleoption.h +++ b/src/gui/styles/qstyleoption.h @@ -331,6 +331,7 @@ public: bool documentMode; QSize leftButtonSize; QSize rightButtonSize; + QBrush backgroundBrush; QStyleOptionTabV3(); QStyleOptionTabV3(const QStyleOptionTabV3 &other) : QStyleOptionTabV2(Version) { *this = other; } QStyleOptionTabV3(const QStyleOptionTabV2 &other) : QStyleOptionTabV2(Version) { *this = other; } diff --git a/src/gui/styles/qwindowscestyle.cpp b/src/gui/styles/qwindowscestyle.cpp index 997fc72..79bf803 100644 --- a/src/gui/styles/qwindowscestyle.cpp +++ b/src/gui/styles/qwindowscestyle.cpp @@ -810,6 +810,12 @@ void QWindowsCEStyle::drawControl(ControlElement element, const QStyleOption *op int x2 = r2.right(); int y1 = r2.top(); int y2 = r2.bottom(); + + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(tab); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + switch (tab->shape) { default: QCommonStyle::drawControl(element, tab, painter, widget); @@ -821,14 +827,19 @@ void QWindowsCEStyle::drawControl(ControlElement element, const QStyleOption *op x2 -= lastTab ? borderThinkness : 0; } - painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), userBrush); // Delete border if (selected) { - painter->setPen(background); - painter->drawLine(x1, y2 - 1, x2, y2 - 1); - painter->drawLine(x1, y2 + 1, x2, y2 + 1); - painter->drawLine(x1, y2, x2, y2); + if (userBrush.style() == Qt::NoBrush) { + painter->setPen(background); + painter->drawLine(x1, y2 - 1, x2, y2 - 1); + painter->drawLine(x1, y2 + 1, x2, y2 + 1); + painter->drawLine(x1, y2, x2, y2); + } } // Left if (firstTab || selected || onlyOne || !previousSelected) { @@ -867,14 +878,19 @@ void QWindowsCEStyle::drawControl(ControlElement element, const QStyleOption *op x2 -= lastTab ? borderThinkness : 0; } - painter->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - painter->setPen(background); - painter->drawLine(x1, y1 + 1, x2 - 1, y1 + 1); - painter->drawLine(x1, y1 - 1, x2 - 1, y1 - 1); - painter->drawLine(x1, y1, x2 - 1, y1); + if (userBrush.style() == Qt::NoBrush) { + painter->setPen(background); + painter->drawLine(x1, y1 + 1, x2 - 1, y1 + 1); + painter->drawLine(x1, y1 - 1, x2 - 1, y1 - 1); + painter->drawLine(x1, y1, x2 - 1, y1); + } } // Left if (firstTab || selected || onlyOne || !previousSelected) { @@ -911,13 +927,18 @@ void QWindowsCEStyle::drawControl(ControlElement element, const QStyleOption *op y2 -= lastTab ? borderThinkness : 0; } - painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - painter->setPen(background); - painter->drawLine(x2 - 1, y1, x2 - 1, y2); - painter->drawLine(x2, y1, x2, y2); + if (userBrush.style() == Qt::NoBrush) { + painter->setPen(background); + painter->drawLine(x2 - 1, y1, x2 - 1, y2); + painter->drawLine(x2, y1, x2, y2); + } } // Top if (firstTab || selected || onlyOne || !previousSelected) { @@ -958,13 +979,18 @@ void QWindowsCEStyle::drawControl(ControlElement element, const QStyleOption *op y2 -= lastTab ? borderThinkness : 0; } - painter->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - painter->setPen(background); - painter->drawLine(x1 + 1, y1, x1 + 1, y2 - 1); - painter->drawLine(x1, y1, x1, y2 - 1); + if (userBrush.style() == Qt::NoBrush) { + painter->setPen(background); + painter->drawLine(x1 + 1, y1, x1 + 1, y2 - 1); + painter->drawLine(x1, y1, x1, y2 - 1); + } } // Top if (firstTab || selected || onlyOne || !previousSelected) { diff --git a/src/gui/styles/qwindowsmobilestyle.cpp b/src/gui/styles/qwindowsmobilestyle.cpp index c70b4c8..86c63ca 100644 --- a/src/gui/styles/qwindowsmobilestyle.cpp +++ b/src/gui/styles/qwindowsmobilestyle.cpp @@ -1586,6 +1586,10 @@ void QWindowsMobileStyle::drawControl(ControlElement element, const QStyleOption break; case CE_TabBarTabShape: if (const QStyleOptionTab *tab = qstyleoption_cast(option)) { + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(tab); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; if (tab->shape == QTabBar::RoundedNorth || tab->shape == QTabBar::RoundedEast || tab->shape == QTabBar::RoundedSouth || tab->shape == QTabBar::RoundedWest) { @@ -1598,24 +1602,34 @@ void QWindowsMobileStyle::drawControl(ControlElement element, const QStyleOption painter->setPen(pen); } if(tab->shape == QTabBar::RoundedNorth) { - if (tab->state & State_Selected) { - painter->fillRect(tab->rect, tab->palette.light()); + if (tab->state & State_Selected) { + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.light()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.topRight(), tab->rect.bottomRight()); - } - else { - painter->fillRect(tab->rect, tab->palette.button()); + } else { + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.bottomLeft() , tab->rect.bottomRight()); painter->drawLine(tab->rect.topRight(), tab->rect.bottomRight()); } } else if(tab->shape == QTabBar::RoundedSouth) { - if (tab->state & State_Selected) { - painter->fillRect(tab->rect.adjusted(0,-2,0,0), tab->palette.light()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect.adjusted(0,-2,0,0), tab->palette.light()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect.adjusted(0,-2,0,0), userBrush); painter->drawLine(tab->rect.topRight(), tab->rect.bottomRight()); } else { - painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); if (d->doubleControls) painter->drawLine(tab->rect.topLeft() + QPoint(0,1), tab->rect.topRight() + QPoint(0,1)); else @@ -1625,22 +1639,34 @@ void QWindowsMobileStyle::drawControl(ControlElement element, const QStyleOption } else if(tab->shape == QTabBar::RoundedEast) { if (tab->state & State_Selected) { - painter->fillRect(tab->rect, tab->palette.light()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.light()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.topLeft(), tab->rect.topRight()); } else { - painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.topLeft(), tab->rect.bottomLeft()); painter->drawLine(tab->rect.topLeft(), tab->rect.topRight()); } } else if(tab->shape == QTabBar::RoundedWest) { if (tab->state & State_Selected) { - painter->fillRect(tab->rect, tab->palette.light()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.light()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.bottomLeft(), tab->rect.bottomRight()); } else { - painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + painter->fillRect(tab->rect, tab->palette.button()); + if (userBrush.style() != Qt::NoBrush) + painter->fillRect(tab->rect, userBrush); painter->drawLine(tab->rect.topRight(), tab->rect.bottomRight()); painter->drawLine(tab->rect.bottomLeft(), tab->rect.bottomRight()); } diff --git a/src/gui/styles/qwindowsstyle.cpp b/src/gui/styles/qwindowsstyle.cpp index 4c66bbb..667ec5c 100644 --- a/src/gui/styles/qwindowsstyle.cpp +++ b/src/gui/styles/qwindowsstyle.cpp @@ -2026,6 +2026,12 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai int x2 = r2.right(); int y1 = r2.top(); int y2 = r2.bottom(); + + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(tab); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + switch (tab->shape) { default: QCommonStyle::drawControl(ce, tab, p, widget); @@ -2037,12 +2043,18 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai x2 -= onlyOne || lastTab ? borderThinkness : 0; } - p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 2), userBrush); // Delete border if (selected) { - p->fillRect(QRect(x1,y2-1,x2-x1,1), tab->palette.background()); - p->fillRect(QRect(x1,y2,x2-x1,1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush) { + p->fillRect(QRect(x1,y2-1,x2-x1,1), tab->palette.background()); + p->fillRect(QRect(x1,y2,x2-x1,1), tab->palette.background()); + } + // Do I need to do anything if the user set the brush? } // Left if (firstTab || selected || onlyOne || !previousSelected) { @@ -2073,12 +2085,17 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai x2 -= lastTab ? borderThinkness : 0; } - p->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + p->fillRect(QRect(x1 + 1, y1 + 2, (x2 - x1) - 1, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - p->fillRect(QRect(x1, y1 + 1, (x2 - 1)-x1, 1), tab->palette.background()); - p->fillRect(QRect(x1, y1, (x2 - 1)-x1, 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush) { + p->fillRect(QRect(x1, y1 + 1, (x2 - 1)-x1, 1), tab->palette.background()); + p->fillRect(QRect(x1, y1, (x2 - 1)-x1, 1), tab->palette.background()); + } } // Left if (firstTab || selected || onlyOne || !previousSelected) { @@ -2111,12 +2128,17 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai y2 -= lastTab ? borderThinkness : 0; } - p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + p->fillRect(QRect(x1 + 1, y1 + 1, (x2 - x1) - 2, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - p->fillRect(QRect(x2 - 1, y1, 1, y2-y1), tab->palette.background()); - p->fillRect(QRect(x2, y1, 1, y2-y1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush) { + p->fillRect(QRect(x2 - 1, y1, 1, y2-y1), tab->palette.background()); + p->fillRect(QRect(x2, y1, 1, y2-y1), tab->palette.background()); + } } // Top if (firstTab || selected || onlyOne || !previousSelected) { @@ -2149,12 +2171,17 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai y2 -= lastTab ? borderThinkness : 0; } - p->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush || !userBrush.isOpaque()) + p->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), tab->palette.background()); + if (userBrush.style() != Qt::NoBrush) + p->fillRect(QRect(x1 + 2, y1 + 1, (x2 - x1) - 1, (y2 - y1) - 1), userBrush); // Delete border if (selected) { - p->fillRect(QRect(x1 + 1, y1, 1, (y2 - 1)-y1),tab->palette.background()); - p->fillRect(QRect(x1, y1, 1, (y2-1)-y1), tab->palette.background()); + if (userBrush.style() == Qt::NoBrush) { + p->fillRect(QRect(x1 + 1, y1, 1, (y2 - 1)-y1),tab->palette.background()); + p->fillRect(QRect(x1, y1, 1, (y2-1)-y1), tab->palette.background()); + } } // Top if (firstTab || selected || onlyOne || !previousSelected) { diff --git a/src/gui/styles/qwindowsxpstyle.cpp b/src/gui/styles/qwindowsxpstyle.cpp index 4d1fb7a..a163d71 100644 --- a/src/gui/styles/qwindowsxpstyle.cpp +++ b/src/gui/styles/qwindowsxpstyle.cpp @@ -1844,6 +1844,20 @@ case PE_Frame: theme.noContent = noContent; theme.rotate = rotate; d->drawBackground(theme); + if (element == CE_TabBarTabShape) { + QBrush userBrush; + const QStyleOptionTabV3 *tab3 = qstyleoption_cast(option); + if (tab3 && tab3->backgroundBrush.style() != Qt::NoBrush) + userBrush = tab3->backgroundBrush; + + if (userBrush.style() != Qt::NoBrush) { + p->save(); + if (userBrush.isOpaque()) + p->setOpacity(0.7); + p->fillRect(rect, userBrush); + p->restore(); + } + } } /*! diff --git a/src/gui/widgets/qtabbar.cpp b/src/gui/widgets/qtabbar.cpp index 690e624..1ee2c5b 100644 --- a/src/gui/widgets/qtabbar.cpp +++ b/src/gui/widgets/qtabbar.cpp @@ -147,6 +147,7 @@ void QTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex) const optionV3->leftButtonSize = tab.leftWidget ? tab.leftWidget->size() : QSize(); optionV3->rightButtonSize = tab.rightWidget ? tab.rightWidget->size() : QSize(); optionV3->documentMode = d->documentMode; + optionV3->backgroundBrush = tab.backgroundBrush; } if (tabIndex > 0 && tabIndex - 1 == d->currentIndex) @@ -203,7 +204,10 @@ void QTabBar::initStyleOption(QStyleOptionTab *option, int tabIndex) const Each tab can display text in a distinct color. The current text color for a tab can be found with the tabTextColor() function. Set the text - color for a particular tab with setTabTextColor(). + color for a particular tab with setTabTextColor(). The background of + the tab can also be changed with setTabBackground() function. The + current background that is set by user can be found with tabBackground() + function. Tabs are added using addTab(), or inserted at particular positions using insertTab(). The total number of tabs is given by @@ -976,6 +980,8 @@ QColor QTabBar::tabTextColor(int index) const } /*! + \since 4.5 + Sets the color of the text in the tab with the given \a index to the specified \a color. If an invalid color is specified, the tab will use the QTabBar foreground role instead. @@ -992,6 +998,48 @@ void QTabBar::setTabTextColor(int index, const QColor &color) } /*! + \since 4.5 + + Returns the brush that the user set to draw the background of the + tab with the given \a index, or a invalid brush if \a index is out + of range. + + If the user didn't set the background brush the returned brush is + invalid and the tab uses the default background specified by the + style. + + Note that the tab background is a hint to the style and might be + be omitted. For example, on Mac OS X the background is ignored. + + \sa setTabBackground() +*/ +QBrush QTabBar::tabBackgroundBrush(int index) const +{ + Q_D(const QTabBar); + if (const QTabBarPrivate::Tab *tab = d->at(index)) + return tab->backgroundBrush; + return QBrush(); +} + +/*! + Sets the brush that is used to draw the background of the tab with + the given \a index. + + If an invalid brush is specified, the tab will use the default + background as specified by the style. + + \sa tabBackground() +*/ +void QTabBar::setTabBackgroundBrush(int index, const QBrush &brush) +{ + Q_D(QTabBar); + if (QTabBarPrivate::Tab *tab = d->at(index)) { + tab->backgroundBrush = brush; + update(tabRect(index)); + } +} + +/*! Returns the icon of the tab at position \a index, or a null icon if \a index is out of range. */ diff --git a/src/gui/widgets/qtabbar.h b/src/gui/widgets/qtabbar.h index 402f54b..af95bf0 100644 --- a/src/gui/widgets/qtabbar.h +++ b/src/gui/widgets/qtabbar.h @@ -118,6 +118,9 @@ public: QColor tabTextColor(int index) const; void setTabTextColor(int index, const QColor &color); + QBrush tabBackgroundBrush(int index) const; + void setTabBackgroundBrush(int index, const QBrush &brush); + QIcon tabIcon(int index) const; void setTabIcon(int index, const QIcon &icon); diff --git a/src/gui/widgets/qtabbar_p.h b/src/gui/widgets/qtabbar_p.h index b9b9fba..576ac45 100644 --- a/src/gui/widgets/qtabbar_p.h +++ b/src/gui/widgets/qtabbar_p.h @@ -115,6 +115,7 @@ public: QWidget *rightWidget; int lastTab; int dragOffset; + QBrush backgroundBrush; #ifndef QT_NO_ANIMATION ~Tab() { delete animation; } -- 1.6.2.1.307.g91408