Fix header content size when sorting is disabled

Summary: Instead of always adding space for the sorting indicator in item view headers, only add it if sorting is enabled. Without this fix, operations such as QTreeView::resizeColumnToContents(..) will not result in a snug fit when the header section is wider than all items in the column.

Test Plan:
```
#include <QApplication>
#include <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Test model
    QStandardItemModel model(3, 2);
    model.setHorizontalHeaderLabels({ "Header 1", "Header 2" });
    for (int row = 0; row < 3; ++row) {
        for (int column = 0; column < 2; ++column) {
            model.setItem(row, column, new QStandardItem("Foo"));
        }
    }

    // View with sorting disabled
    QTreeView view;
    view.setWindowTitle("Sorting Disabled");
    view.setModel(&model);
    view.show();
    view.resizeColumnToContents(0);

    // View with sorting enabled
    QTreeView viewWithSorting;
    viewWithSorting.setWindowTitle("Sorting Enabled");
    viewWithSorting.setModel(&model);
    viewWithSorting.setSortingEnabled(true);
    viewWithSorting.show();
    viewWithSorting.resizeColumnToContents(0);

    return app.exec();
}
```

Notice how before applying this fix, there's space reserved for the sorting indicator even on the QTreeView that has sorting disabled.

Before the fix:

{F2405867}

After the fix:

{F2405871}

Reviewers: #breeze, hpereiradacosta

Reviewed By: hpereiradacosta

Subscribers: hpereiradacosta, cfeck, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D4406
wilder-pre-rebase
Elvis Stansvik 9 years ago
parent 28ac287925
commit 54e43f695d
  1. 4
      kstyle/breezestyle.cpp

@ -2926,9 +2926,9 @@ namespace Breeze
int contentsHeight( headerOption->fontMetrics.height() );
if( hasIcon ) contentsHeight = qMax( contentsHeight, iconSize.height() );
if( horizontal )
if( horizontal && headerOption->sortIndicator != QStyleOptionHeader::None )
{
// also add space for icon
// also add space for sort indicator
contentsWidth += Metrics::Header_ArrowSize + Metrics::Header_ItemSpacing;
contentsHeight = qMax( contentsHeight, int(Metrics::Header_ArrowSize) );
}

Loading…
Cancel
Save