Make the default value of QTreeView::indentation() be style dependent
Add a new PM_TreeViewIndentaion enum value to QStyle and get the corresponding pixel metric in QTreeView. [ChangeLog][QtWidgets][QTreeView] Indentation is now style-dependent by default. [ChangeLog][QtWidgets][QTreeView] Added resetIndentation(). Change-Id: Ifad7987b8f3c6cd32987b89d95390f33043d8f19 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Adam Majer <adamm@zombino.com>
This commit is contained in:
parent
0c748fb7b1
commit
256df2484c
@ -371,7 +371,9 @@ void QTreeView::setAutoExpandDelay(int delay)
|
||||
horizontal distance from the viewport edge to the items in the first column;
|
||||
for child items, it specifies their indentation from their parent items.
|
||||
|
||||
By default, this property has a value of 20.
|
||||
By default, the value of this property is style dependent. Thus, when the style
|
||||
changes, this property updates from it. Calling setIndentation() stops the updates,
|
||||
calling resetIndentation() will restore default behavior.
|
||||
*/
|
||||
int QTreeView::indentation() const
|
||||
{
|
||||
@ -382,12 +384,22 @@ int QTreeView::indentation() const
|
||||
void QTreeView::setIndentation(int i)
|
||||
{
|
||||
Q_D(QTreeView);
|
||||
if (i != d->indent) {
|
||||
if (!d->customIndent || (i != d->indent)) {
|
||||
d->indent = i;
|
||||
d->customIndent = true;
|
||||
d->viewport->update();
|
||||
}
|
||||
}
|
||||
|
||||
void QTreeView::resetIndentation()
|
||||
{
|
||||
Q_D(QTreeView);
|
||||
if (d->customIndent) {
|
||||
d->updateIndentationFromStyle();
|
||||
d->customIndent = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\property QTreeView::rootIsDecorated
|
||||
\brief whether to show controls for expanding and collapsing top-level items
|
||||
@ -2081,6 +2093,12 @@ QModelIndex QTreeView::indexBelow(const QModelIndex &index) const
|
||||
void QTreeView::doItemsLayout()
|
||||
{
|
||||
Q_D(QTreeView);
|
||||
if (!d->customIndent) {
|
||||
// ### Qt 6: move to event()
|
||||
// QAbstractItemView calls this method in case of a style change,
|
||||
// so update the indentation here if it wasn't set manually.
|
||||
d->updateIndentationFromStyle();
|
||||
}
|
||||
if (d->hasRemovedItems) {
|
||||
//clean the QSet that may contains old (and this invalid) indexes
|
||||
d->hasRemovedItems = false;
|
||||
@ -3025,6 +3043,8 @@ bool QTreeView::isIndexHidden(const QModelIndex &index) const
|
||||
void QTreeViewPrivate::initialize()
|
||||
{
|
||||
Q_Q(QTreeView);
|
||||
|
||||
updateIndentationFromStyle();
|
||||
updateStyledFrameWidths();
|
||||
q->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
q->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
@ -3911,6 +3931,12 @@ int QTreeViewPrivate::accessibleTree2Index(const QModelIndex &index) const
|
||||
return (q->visualIndex(index) + (q->header() ? 1 : 0)) * index.model()->columnCount() + index.column();
|
||||
}
|
||||
|
||||
void QTreeViewPrivate::updateIndentationFromStyle()
|
||||
{
|
||||
Q_Q(const QTreeView);
|
||||
indent = q->style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, q);
|
||||
}
|
||||
|
||||
/*!
|
||||
\reimp
|
||||
*/
|
||||
|
@ -56,7 +56,7 @@ class Q_WIDGETS_EXPORT QTreeView : public QAbstractItemView
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int autoExpandDelay READ autoExpandDelay WRITE setAutoExpandDelay)
|
||||
Q_PROPERTY(int indentation READ indentation WRITE setIndentation)
|
||||
Q_PROPERTY(int indentation READ indentation WRITE setIndentation RESET resetIndentation)
|
||||
Q_PROPERTY(bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated)
|
||||
Q_PROPERTY(bool uniformRowHeights READ uniformRowHeights WRITE setUniformRowHeights)
|
||||
Q_PROPERTY(bool itemsExpandable READ itemsExpandable WRITE setItemsExpandable)
|
||||
@ -83,6 +83,7 @@ public:
|
||||
|
||||
int indentation() const;
|
||||
void setIndentation(int i);
|
||||
void resetIndentation();
|
||||
|
||||
bool rootIsDecorated() const;
|
||||
void setRootIsDecorated(bool show);
|
||||
|
@ -89,7 +89,7 @@ public:
|
||||
uniformRowHeights(false), rootDecoration(true),
|
||||
itemsExpandable(true), sortingEnabled(false),
|
||||
expandsOnDoubleClick(true),
|
||||
allColumnsShowFocus(false), current(0), spanning(false),
|
||||
allColumnsShowFocus(false), customIndent(false), current(0), spanning(false),
|
||||
animationsEnabled(false), columnResizeTimerID(0),
|
||||
autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false),
|
||||
treePosition(0) {}
|
||||
@ -191,6 +191,7 @@ public:
|
||||
bool sortingEnabled;
|
||||
bool expandsOnDoubleClick;
|
||||
bool allColumnsShowFocus;
|
||||
bool customIndent;
|
||||
|
||||
// used for drawing
|
||||
mutable QPair<int,int> leftAndRight;
|
||||
@ -241,6 +242,8 @@ public:
|
||||
|
||||
int accessibleTree2Index(const QModelIndex &index) const;
|
||||
|
||||
void updateIndentationFromStyle();
|
||||
|
||||
// used for spanning rows
|
||||
QVector<QPersistentModelIndex> spanningIndexes;
|
||||
|
||||
|
@ -4695,6 +4695,9 @@ int QCommonStyle::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWid
|
||||
case PM_SubMenuOverlap:
|
||||
ret = -proxy()->pixelMetric(QStyle::PM_MenuPanelWidth, opt, widget);
|
||||
break;
|
||||
case PM_TreeViewIndentation:
|
||||
ret = int(QStyleHelper::dpiScaled(20.));
|
||||
break;
|
||||
default:
|
||||
ret = 0;
|
||||
break;
|
||||
|
@ -1470,6 +1470,9 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment,
|
||||
\value PM_TabCloseIndicatorWidth The default width of a close button on a tab in a tab bar.
|
||||
\value PM_TabCloseIndicatorHeight The default height of a close button on a tab in a tab bar.
|
||||
|
||||
\value PM_TreeViewIndentation The indentation of items in a tree view.
|
||||
This enum value has been introduced in Qt 5.4.
|
||||
|
||||
\value PM_CustomBase Base value for custom pixel metrics. Custom
|
||||
values must be greater than this value.
|
||||
|
||||
|
@ -556,6 +556,7 @@ public:
|
||||
PM_ScrollView_ScrollBarSpacing,
|
||||
PM_ScrollView_ScrollBarOverlap,
|
||||
PM_SubMenuOverlap,
|
||||
PM_TreeViewIndentation,
|
||||
|
||||
// do not add any values below/greater than this
|
||||
PM_CustomBase = 0xf0000000
|
||||
|
@ -212,6 +212,7 @@ private slots:
|
||||
void rowSizeHint();
|
||||
void setSortingEnabled();
|
||||
void headerHidden();
|
||||
void indentation();
|
||||
|
||||
void selection();
|
||||
void removeAndInsertExpandedCol0();
|
||||
@ -439,7 +440,8 @@ void tst_QTreeView::getSetCheck()
|
||||
|
||||
// int QTreeView::indentation()
|
||||
// void QTreeView::setIndentation(int)
|
||||
QCOMPARE(obj1.indentation(), 20);
|
||||
const int styledIndentation = obj1.style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, &obj1);
|
||||
QCOMPARE(obj1.indentation(), styledIndentation);
|
||||
obj1.setIndentation(0);
|
||||
QCOMPARE(obj1.indentation(), 0);
|
||||
obj1.setIndentation(INT_MIN);
|
||||
@ -554,7 +556,8 @@ void tst_QTreeView::construction()
|
||||
QCOMPARE(view.columnWidth(0), 0);
|
||||
QCOMPARE(view.columnWidth(1), 0);
|
||||
QVERIFY(view.header());
|
||||
QCOMPARE(view.indentation(), 20);
|
||||
const int styledIndentation = view.style()->pixelMetric(QStyle::PM_TreeViewIndentation, 0, &view);
|
||||
QCOMPARE(view.indentation(), styledIndentation);
|
||||
QCOMPARE(view.indexAbove(QModelIndex()), QModelIndex());
|
||||
QCOMPARE(view.indexBelow(QModelIndex()), QModelIndex());
|
||||
QVERIFY(!view.isAnimated());
|
||||
@ -2576,6 +2579,42 @@ void tst_QTreeView::headerHidden()
|
||||
QCOMPARE(view.header()->isHidden(), true);
|
||||
}
|
||||
|
||||
class TestTreeViewStyle : public QProxyStyle
|
||||
{
|
||||
public:
|
||||
TestTreeViewStyle() : indentation(20) {}
|
||||
int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const Q_DECL_OVERRIDE
|
||||
{
|
||||
if (metric == QStyle::PM_TreeViewIndentation)
|
||||
return indentation;
|
||||
else
|
||||
return QProxyStyle::pixelMetric(metric, option, widget);
|
||||
}
|
||||
int indentation;
|
||||
};
|
||||
|
||||
void tst_QTreeView::indentation()
|
||||
{
|
||||
TestTreeViewStyle style1;
|
||||
TestTreeViewStyle style2;
|
||||
style1.indentation = 20;
|
||||
style2.indentation = 30;
|
||||
|
||||
QTreeView view;
|
||||
view.setStyle(&style1);
|
||||
QCOMPARE(view.indentation(), style1.indentation);
|
||||
view.setStyle(&style2);
|
||||
QCOMPARE(view.indentation(), style2.indentation);
|
||||
view.setIndentation(70);
|
||||
QCOMPARE(view.indentation(), 70);
|
||||
view.setStyle(&style1);
|
||||
QCOMPARE(view.indentation(), 70);
|
||||
view.resetIndentation();
|
||||
QCOMPARE(view.indentation(), style1.indentation);
|
||||
view.setStyle(&style2);
|
||||
QCOMPARE(view.indentation(), style2.indentation);
|
||||
}
|
||||
|
||||
// From Task 145199 (crash when column 0 having at least one expanded item is removed and then
|
||||
// inserted). The test passes simply if it doesn't crash, hence there are no calls
|
||||
// to QCOMPARE() or QVERIFY().
|
||||
|
Loading…
x
Reference in New Issue
Block a user