Stylesheet: Handle tabs with elide mode set correctly

In order for SE_TabBarTabText to correctly calculate the space available
for the text it needs to get the rectangle of the tab directly instead
of relying on the option's rectangle as this may have been modified
before this point. Therefore we introduce QStyleOptionTabV4 to be able
to store the index as part of the option so it can be queried directly.

[ChangeLog][QtWidgets] Added QStyleOptionTabV4 as a subclass of
QStyleOptionTab so that the tab's index information can be obtained.

Fixes: QTBUG-50637
Change-Id: If705f5069fdd14eeccf06bc63dba4e8d2e704359
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Andy Shaw 2020-01-03 13:39:04 +01:00
parent aa542be4e0
commit 24b8b2cb6c
10 changed files with 127 additions and 8 deletions

View File

@ -1470,6 +1470,22 @@ QStyleOptionTab::QStyleOptionTab(int version)
The default value is QSize(-1, -1), i.e. an invalid size;
*/
/*!
Constructs a QStyleOptionTabV4 object, initializing the members
variables to their default values.
*/
QStyleOptionTabV4::QStyleOptionTabV4() : QStyleOptionTab(QStyleOptionTabV4::Version)
{
}
/*!
\variable QStyleOptionTabV4::tabIndex
\brief the index for the tab being represented.
The default value is -1, i.e. a tab not on a tabbar;
*/
#endif // QT_CONFIG(tabbar)
/*!

View File

@ -296,6 +296,14 @@ protected:
QStyleOptionTab(int version);
};
class Q_WIDGETS_EXPORT QStyleOptionTabV4 : public QStyleOptionTab
{
public:
enum StyleOptionVersion { Version = 4 };
QStyleOptionTabV4();
int tabIndex = -1;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QStyleOptionTab::CornerWidgets)
typedef Q_DECL_DEPRECATED QStyleOptionTab QStyleOptionTabV2;

View File

@ -6009,6 +6009,16 @@ QRect QStyleSheetStyle::subElementRect(SubElement se, const QStyleOption *opt, c
case SE_TabBarTabRightButton: {
QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
if (subRule.hasBox() || !subRule.hasNativeBorder()) {
if (se == SE_TabBarTabText) {
if (const QStyleOptionTabV4 *tab = qstyleoption_cast<const QStyleOptionTabV4 *>(opt)) {
const QTabBar *bar = qobject_cast<const QTabBar *>(w);
const QRect optRect = bar && tab->tabIndex != -1 ? bar->tabRect(tab->tabIndex) : opt->rect;
const QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, optRect, opt->direction);
QStyleOptionTabV4 tabCopy(*tab);
tabCopy.rect = subRule.contentsRect(r);
return ParentStyle::subElementRect(se, &tabCopy, w);
}
}
return ParentStyle::subElementRect(se, opt, w);
}
break;

View File

@ -229,6 +229,8 @@ void QTabBarPrivate::initBasicStyleOption(QStyleOptionTab *option, int tabIndex)
option->cornerWidgets |= QStyleOptionTab::RightCornerWidget;
}
#endif
if (QStyleOptionTabV4 *optv4 = qstyleoption_cast<QStyleOptionTabV4 *>(option))
optv4->tabIndex = tabIndex;
}
/*!
@ -628,7 +630,7 @@ QRect QTabBarPrivate::normalizedScrollRect(int index)
// tab bar itself is in a different orientation.
Q_Q(QTabBar);
QStyleOptionTab opt;
QStyleOptionTabV4 opt;
q->initStyleOption(&opt, currentIndex);
opt.rect = q->rect();
@ -757,7 +759,7 @@ void QTabBarPrivate::layoutTab(int index)
if (!(tab.leftWidget || tab.rightWidget))
return;
QStyleOptionTab opt;
QStyleOptionTabV4 opt;
q->initStyleOption(&opt, index);
if (tab.leftWidget) {
QRect rect = q->style()->subElementRect(QStyle::SE_TabBarTabLeftButton, &opt, q);
@ -1003,7 +1005,7 @@ int QTabBar::insertTab(int index, const QIcon& icon, const QString &text)
}
if (d->closeButtonOnTabs) {
QStyleOptionTab opt;
QStyleOptionTabV4 opt;
initStyleOption(&opt, index);
ButtonPosition closeSide = (ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, nullptr, this);
QAbstractButton *closeButton = new CloseButton(this);
@ -1574,7 +1576,7 @@ QSize QTabBar::tabSizeHint(int index) const
//Note: this must match with the computations in QCommonStylePrivate::tabLayout
Q_D(const QTabBar);
if (const QTabBarPrivate::Tab *tab = d->at(index)) {
QStyleOptionTab opt;
QStyleOptionTabV4 opt;
d->initBasicStyleOption(&opt, index);
opt.text = d->tabList.at(index).text;
QSize iconSize = tab->icon.isNull() ? QSize(0, 0) : opt.iconSize;
@ -1819,7 +1821,7 @@ void QTabBar::paintEvent(QPaintEvent *)
for (int i = 0; i < d->tabList.count(); ++i) {
if (!d->at(i)->visible)
continue;
QStyleOptionTab tab;
QStyleOptionTabV4 tab;
initStyleOption(&tab, i);
if (d->paintWithOffsets && d->tabList[i].dragOffset != 0) {
if (vertical) {
@ -1859,7 +1861,7 @@ void QTabBar::paintEvent(QPaintEvent *)
// Draw the selected tab last to get it "on top"
if (selected >= 0) {
QStyleOptionTab tab;
QStyleOptionTabV4 tab;
initStyleOption(&tab, selected);
if (d->paintWithOffsets && d->tabList[selected].dragOffset != 0) {
if (vertical)
@ -2209,7 +2211,7 @@ void QTabBarPrivate::setupMovableTab()
grabImage.fill(Qt::transparent);
QStylePainter p(&grabImage, q);
QStyleOptionTab tab;
QStyleOptionTabV4 tab;
q->initStyleOption(&tab, pressedIndex);
tab.position = QStyleOptionTab::OnlyOneTab;
if (verticalTabs(shape))

View File

@ -0,0 +1 @@
SUBDIRS = stylesheet

View File

@ -0,0 +1,71 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
// This test is for checking that when there is padding set on the stylesheet and the elide mode is
// set that it is correctly shown as elided and not clipped.
#include <QApplication>
#include <QTabBar>
#include <QIcon>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setStyleSheet("QTabBar::tab { padding-left: 20px; }\n");
QIcon icon(":/v.ico");
QTabBar b;
b.setElideMode(Qt::ElideRight);
b.addTab(icon, "some text");
b.resize(80,32);
b.show();
return app.exec();
}

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>v.ico</file>
</qresource>
</RCC>

View File

@ -0,0 +1,5 @@
QT += widgets
TEMPLATE = app
TARGET = stylesheet
RESOURCES += res.qrc
SOURCES += main.cpp

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -2,4 +2,5 @@ TEMPLATE = subdirs
SUBDIRS = bigmenucreator \
defaultUpMenuBar \
multiscreen-menus \
qtoolbutton/menuOnMultiScreens
qtoolbutton/menuOnMultiScreens \
qtabbar