QListView: Fix viewport size when checking scroll bar visibility
Subtract the viewport margins from the contentsRect in QCommonListViewBase::updateHorizontal/VerticalScrollBar(). This affects list views in icon mode and list mode / ScrollPerPixel. Task-number: QTBUG-61383 Change-Id: I6f2f7951ac9344ac21cef1eba061780d130e2467 Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
parent
9bec818ccc
commit
db404fea64
@ -1871,6 +1871,11 @@ void QCommonListViewBase::paintDragDrop(QPainter *painter)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
QSize QListModeViewBase::viewportSize(const QAbstractItemView *v)
|
||||||
|
{
|
||||||
|
return v->contentsRect().marginsRemoved(v->viewportMargins()).size();
|
||||||
|
}
|
||||||
|
|
||||||
void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
|
void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
|
||||||
{
|
{
|
||||||
horizontalScrollBar()->d_func()->itemviewChangeSingleStep(step.width() + spacing());
|
horizontalScrollBar()->d_func()->itemviewChangeSingleStep(step.width() + spacing());
|
||||||
@ -1883,7 +1888,7 @@ void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
|
|||||||
const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
|
const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
|
||||||
qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
|
qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
|
||||||
|
|
||||||
const QSize viewportSize = qq->contentsRect().size();
|
const QSize viewportSize = QListModeViewBase::viewportSize(qq);
|
||||||
|
|
||||||
bool verticalWantsToShow = contentsSize.height() > viewportSize.height();
|
bool verticalWantsToShow = contentsSize.height() > viewportSize.height();
|
||||||
bool horizontalWantsToShow;
|
bool horizontalWantsToShow;
|
||||||
@ -1913,7 +1918,7 @@ void QCommonListViewBase::updateVerticalScrollBar(const QSize &step)
|
|||||||
const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
|
const bool bothScrollBarsAuto = qq->verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
|
||||||
qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
|
qq->horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded;
|
||||||
|
|
||||||
const QSize viewportSize = qq->contentsRect().size();
|
const QSize viewportSize = QListModeViewBase::viewportSize(qq);
|
||||||
|
|
||||||
bool horizontalWantsToShow = contentsSize.width() > viewportSize.width();
|
bool horizontalWantsToShow = contentsSize.width() > viewportSize.width();
|
||||||
bool verticalWantsToShow;
|
bool verticalWantsToShow;
|
||||||
|
@ -225,6 +225,7 @@ public:
|
|||||||
QRect mapToViewport(const QRect &rect) const override;
|
QRect mapToViewport(const QRect &rect) const override;
|
||||||
int horizontalOffset() const override;
|
int horizontalOffset() const override;
|
||||||
int verticalOffset() const override;
|
int verticalOffset() const override;
|
||||||
|
inline static QSize viewportSize(const QAbstractItemView *v);
|
||||||
void updateHorizontalScrollBar(const QSize &step) override;
|
void updateHorizontalScrollBar(const QSize &step) override;
|
||||||
void updateVerticalScrollBar(const QSize &step) override;
|
void updateVerticalScrollBar(const QSize &step) override;
|
||||||
|
|
||||||
|
@ -60,6 +60,10 @@ static inline HWND getHWNDForWidget(const QWidget *widget)
|
|||||||
}
|
}
|
||||||
#endif // Q_OS_WIN
|
#endif // Q_OS_WIN
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(QAbstractItemView::ScrollMode)
|
||||||
|
Q_DECLARE_METATYPE(QMargins)
|
||||||
|
Q_DECLARE_METATYPE(QSize)
|
||||||
|
|
||||||
// Make a widget frameless to prevent size constraints of title bars
|
// Make a widget frameless to prevent size constraints of title bars
|
||||||
// from interfering (Windows).
|
// from interfering (Windows).
|
||||||
static inline void setFrameless(QWidget *w)
|
static inline void setFrameless(QWidget *w)
|
||||||
@ -902,10 +906,11 @@ class PublicListView : public QListView
|
|||||||
class TestDelegate : public QItemDelegate
|
class TestDelegate : public QItemDelegate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TestDelegate(QObject *parent) : QItemDelegate(parent), m_sizeHint(50,50) {}
|
explicit TestDelegate(QObject *parent, const QSize &sizeHint = QSize(50,50))
|
||||||
|
: QItemDelegate(parent), m_sizeHint(sizeHint) {}
|
||||||
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return m_sizeHint; }
|
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return m_sizeHint; }
|
||||||
|
|
||||||
QSize m_sizeHint;
|
const QSize m_sizeHint;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef QList<int> IntList;
|
typedef QList<int> IntList;
|
||||||
@ -1250,9 +1255,7 @@ void tst_QListView::scrollBarRanges()
|
|||||||
lv.setModel(&model);
|
lv.setModel(&model);
|
||||||
lv.resize(250, 130);
|
lv.resize(250, 130);
|
||||||
|
|
||||||
TestDelegate *delegate = new TestDelegate(&lv);
|
lv.setItemDelegate(new TestDelegate(&lv, QSize(100, rowHeight)));
|
||||||
delegate->m_sizeHint = QSize(100, rowHeight);
|
|
||||||
lv.setItemDelegate(delegate);
|
|
||||||
topLevel.show();
|
topLevel.show();
|
||||||
|
|
||||||
for (int h = 30; h <= 210; ++h) {
|
for (int h = 30; h <= 210; ++h) {
|
||||||
@ -1268,14 +1271,18 @@ void tst_QListView::scrollBarAsNeeded_data()
|
|||||||
{
|
{
|
||||||
QTest::addColumn<QSize>("size");
|
QTest::addColumn<QSize>("size");
|
||||||
QTest::addColumn<int>("itemCount");
|
QTest::addColumn<int>("itemCount");
|
||||||
|
QTest::addColumn<QAbstractItemView::ScrollMode>("verticalScrollMode");
|
||||||
|
QTest::addColumn<QMargins>("viewportMargins");
|
||||||
|
QTest::addColumn<QSize>("delegateSize");
|
||||||
QTest::addColumn<int>("flow");
|
QTest::addColumn<int>("flow");
|
||||||
QTest::addColumn<bool>("horizontalScrollBarVisible");
|
QTest::addColumn<bool>("horizontalScrollBarVisible");
|
||||||
QTest::addColumn<bool>("verticalScrollBarVisible");
|
QTest::addColumn<bool>("verticalScrollBarVisible");
|
||||||
|
|
||||||
|
|
||||||
QTest::newRow("TopToBottom, count:0")
|
QTest::newRow("TopToBottom, count:0")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 0
|
<< 0
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::TopToBottom)
|
<< int(QListView::TopToBottom)
|
||||||
<< false
|
<< false
|
||||||
<< false;
|
<< false;
|
||||||
@ -1283,6 +1290,8 @@ void tst_QListView::scrollBarAsNeeded_data()
|
|||||||
QTest::newRow("TopToBottom, count:1")
|
QTest::newRow("TopToBottom, count:1")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 1
|
<< 1
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::TopToBottom)
|
<< int(QListView::TopToBottom)
|
||||||
<< false
|
<< false
|
||||||
<< false;
|
<< false;
|
||||||
@ -1290,13 +1299,46 @@ void tst_QListView::scrollBarAsNeeded_data()
|
|||||||
QTest::newRow("TopToBottom, count:20")
|
QTest::newRow("TopToBottom, count:20")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 20
|
<< 20
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::TopToBottom)
|
<< int(QListView::TopToBottom)
|
||||||
<< false
|
<< false
|
||||||
<< true;
|
<< true;
|
||||||
|
|
||||||
|
QTest::newRow("TopToBottom, fixed size, count:4")
|
||||||
|
<< QSize(200, 200)
|
||||||
|
<< 4
|
||||||
|
<< QListView::ScrollPerPixel
|
||||||
|
<< QMargins() << QSize(40, 40)
|
||||||
|
<< int(QListView::TopToBottom)
|
||||||
|
<< false
|
||||||
|
<< false;
|
||||||
|
|
||||||
|
// QTBUG-61383, vertical case: take viewport margins into account
|
||||||
|
QTest::newRow("TopToBottom, fixed size, vertical margins, count:4")
|
||||||
|
<< QSize(200, 200)
|
||||||
|
<< 4
|
||||||
|
<< QListView::ScrollPerPixel
|
||||||
|
<< QMargins(0, 50, 0, 50) << QSize(40, 40)
|
||||||
|
<< int(QListView::TopToBottom)
|
||||||
|
<< false
|
||||||
|
<< true;
|
||||||
|
|
||||||
|
// QTBUG-61383, horizontal case: take viewport margins into account
|
||||||
|
QTest::newRow("TopToBottom, fixed size, horizontal margins, count:4")
|
||||||
|
<< QSize(200, 200)
|
||||||
|
<< 4
|
||||||
|
<< QListView::ScrollPerPixel
|
||||||
|
<< QMargins(50, 0, 50, 0) << QSize(120, 40)
|
||||||
|
<< int(QListView::TopToBottom)
|
||||||
|
<< true
|
||||||
|
<< false;
|
||||||
|
|
||||||
QTest::newRow("LeftToRight, count:0")
|
QTest::newRow("LeftToRight, count:0")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 0
|
<< 0
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::LeftToRight)
|
<< int(QListView::LeftToRight)
|
||||||
<< false
|
<< false
|
||||||
<< false;
|
<< false;
|
||||||
@ -1304,6 +1346,8 @@ void tst_QListView::scrollBarAsNeeded_data()
|
|||||||
QTest::newRow("LeftToRight, count:1")
|
QTest::newRow("LeftToRight, count:1")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 1
|
<< 1
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::LeftToRight)
|
<< int(QListView::LeftToRight)
|
||||||
<< false
|
<< false
|
||||||
<< false;
|
<< false;
|
||||||
@ -1311,17 +1355,31 @@ void tst_QListView::scrollBarAsNeeded_data()
|
|||||||
QTest::newRow("LeftToRight, count:20")
|
QTest::newRow("LeftToRight, count:20")
|
||||||
<< QSize(200, 100)
|
<< QSize(200, 100)
|
||||||
<< 20
|
<< 20
|
||||||
|
<< QListView::ScrollPerItem
|
||||||
|
<< QMargins() << QSize()
|
||||||
<< int(QListView::LeftToRight)
|
<< int(QListView::LeftToRight)
|
||||||
<< true
|
<< true
|
||||||
<< false;
|
<< false;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ScrollBarTestListView : public QListView
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit ScrollBarTestListView(QWidget *p) : QListView(p) {}
|
||||||
|
|
||||||
|
using QAbstractScrollArea::setViewportMargins;
|
||||||
|
};
|
||||||
|
|
||||||
void tst_QListView::scrollBarAsNeeded()
|
void tst_QListView::scrollBarAsNeeded()
|
||||||
{
|
{
|
||||||
|
|
||||||
QFETCH(QSize, size);
|
QFETCH(QSize, size);
|
||||||
QFETCH(int, itemCount);
|
QFETCH(int, itemCount);
|
||||||
|
QFETCH(QAbstractItemView::ScrollMode, verticalScrollMode);
|
||||||
|
QFETCH(QMargins, viewportMargins);
|
||||||
|
QFETCH(QSize, delegateSize);
|
||||||
QFETCH(int, flow);
|
QFETCH(int, flow);
|
||||||
QFETCH(bool, horizontalScrollBarVisible);
|
QFETCH(bool, horizontalScrollBarVisible);
|
||||||
QFETCH(bool, verticalScrollBarVisible);
|
QFETCH(bool, verticalScrollBarVisible);
|
||||||
@ -1330,10 +1388,17 @@ void tst_QListView::scrollBarAsNeeded()
|
|||||||
const int rowCounts[3] = {0, 1, 20};
|
const int rowCounts[3] = {0, 1, 20};
|
||||||
|
|
||||||
QWidget topLevel;
|
QWidget topLevel;
|
||||||
QListView lv(&topLevel);
|
topLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QStringLiteral("::")
|
||||||
|
+ QLatin1String(QTest::currentDataTag()));
|
||||||
|
ScrollBarTestListView lv(&topLevel);
|
||||||
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||||
lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||||
|
lv.setVerticalScrollMode(verticalScrollMode);
|
||||||
|
lv.setViewportMargins(viewportMargins);
|
||||||
lv.setFlow((QListView::Flow)flow);
|
lv.setFlow((QListView::Flow)flow);
|
||||||
|
if (!delegateSize.isEmpty())
|
||||||
|
lv.setItemDelegate(new TestDelegate(&lv, delegateSize));
|
||||||
|
|
||||||
QStringListModel model(&lv);
|
QStringListModel model(&lv);
|
||||||
lv.setModel(&model);
|
lv.setModel(&model);
|
||||||
lv.resize(size);
|
lv.resize(size);
|
||||||
@ -2380,9 +2445,7 @@ void tst_QListView::horizontalScrollingByVerticalWheelEvents()
|
|||||||
QListView lv;
|
QListView lv;
|
||||||
lv.setWrapping(true);
|
lv.setWrapping(true);
|
||||||
|
|
||||||
TestDelegate *delegate = new TestDelegate(&lv);
|
lv.setItemDelegate(new TestDelegate(&lv, QSize(100, 100)));
|
||||||
delegate->m_sizeHint = QSize(100, 100);
|
|
||||||
lv.setItemDelegate(delegate);
|
|
||||||
|
|
||||||
QtTestModel model;
|
QtTestModel model;
|
||||||
model.colCount = 1;
|
model.colCount = 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user