Add SH_ItemView_ScrollMode style hint and use it in QMacStyle

On OS X, the default scrolling mode of item views should be
per pixel instead of per item. We enforce this through a new
style hint. On all other platforms, the behavior remains the
same.

It's still possible to override the style hint by using the
regular scroll mode setters. Any subsequent style change will
result in a no-op once the setters have been called and until
the properties are reset.

Some auto-tests had to be update to to take the new behavior
into account.

[ChangeLog][QtWidgets][Styles] Added SH_ItemView_ScrollMode
style hint.

[ChangeLog][QtWidgets][Item Views] Item views scroll per pixel
on OS X now.

[ChangeLog][QtWidgets][Item Views] QAbstractItemView::verticalScrollMode
and QAbstractItemView::horizontalScrollMode are now resettable.

Change-Id: I3f923275c99aa4389323b52fc1c5455fe71f8d73
Task-number: QTBUG-50102
Reviewed-by: Jake Petroules <jake.petroules@theqtcompany.com>
This commit is contained in:
Gabriel de Dietrich 2016-02-05 16:41:20 -08:00
parent cc2938b5b6
commit 9e8c84aa14
10 changed files with 121 additions and 13 deletions

View File

@ -108,7 +108,9 @@ QAbstractItemViewPrivate::QAbstractItemViewPrivate()
currentIndexSet(false), currentIndexSet(false),
wrapItemText(false), wrapItemText(false),
delayedPendingLayout(true), delayedPendingLayout(true),
moveCursorUpdatedView(false) moveCursorUpdatedView(false),
verticalScrollModeSet(false),
horizontalScrollModeSet(false)
{ {
keyboardInputTime.invalidate(); keyboardInputTime.invalidate();
} }
@ -137,6 +139,9 @@ void QAbstractItemViewPrivate::init()
viewport->setBackgroundRole(QPalette::Base); viewport->setBackgroundRole(QPalette::Base);
q->setAttribute(Qt::WA_InputMethodEnabled); q->setAttribute(Qt::WA_InputMethodEnabled);
verticalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, q, 0));
horizontalScrollMode = static_cast<QAbstractItemView::ScrollMode>(q->style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, q, 0));
} }
void QAbstractItemViewPrivate::setHoverIndex(const QPersistentModelIndex &index) void QAbstractItemViewPrivate::setHoverIndex(const QPersistentModelIndex &index)
@ -1237,12 +1242,14 @@ QAbstractItemView::EditTriggers QAbstractItemView::editTriggers() const
\brief how the view scrolls its contents in the vertical direction \brief how the view scrolls its contents in the vertical direction
This property controls how the view scroll its contents vertically. This property controls how the view scroll its contents vertically.
Scrolling can be done either per pixel or per item. Scrolling can be done either per pixel or per item. Its default value
comes from the style via the QStyle::SH_ItemView_ScrollMode style hint.
*/ */
void QAbstractItemView::setVerticalScrollMode(ScrollMode mode) void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
{ {
Q_D(QAbstractItemView); Q_D(QAbstractItemView);
d->verticalScrollModeSet = true;
if (mode == d->verticalScrollMode) if (mode == d->verticalScrollMode)
return; return;
QModelIndex topLeft = indexAt(QPoint(0, 0)); QModelIndex topLeft = indexAt(QPoint(0, 0));
@ -1261,18 +1268,27 @@ QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode() const
return d->verticalScrollMode; return d->verticalScrollMode;
} }
void QAbstractItemView::resetVerticalScrollMode()
{
auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, this, 0));
setVerticalScrollMode(sm);
d_func()->verticalScrollModeSet = false;
}
/*! /*!
\since 4.2 \since 4.2
\property QAbstractItemView::horizontalScrollMode \property QAbstractItemView::horizontalScrollMode
\brief how the view scrolls its contents in the horizontal direction \brief how the view scrolls its contents in the horizontal direction
This property controls how the view scroll its contents horizontally. This property controls how the view scroll its contents horizontally.
Scrolling can be done either per pixel or per item. Scrolling can be done either per pixel or per item. Its default value
comes from the style via the QStyle::SH_ItemView_ScrollMode style hint.
*/ */
void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode) void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
{ {
Q_D(QAbstractItemView); Q_D(QAbstractItemView);
d->horizontalScrollModeSet = true;
if (mode == d->horizontalScrollMode) if (mode == d->horizontalScrollMode)
return; return;
d->horizontalScrollMode = mode; d->horizontalScrollMode = mode;
@ -1289,6 +1305,13 @@ QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode() const
return d->horizontalScrollMode; return d->horizontalScrollMode;
} }
void QAbstractItemView::resetHorizontalScrollMode()
{
auto sm = static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, this, 0));
setHorizontalScrollMode(sm);
d_func()->horizontalScrollModeSet = false;
}
#ifndef QT_NO_DRAGANDDROP #ifndef QT_NO_DRAGANDDROP
/*! /*!
\since 4.2 \since 4.2
@ -1627,6 +1650,10 @@ bool QAbstractItemView::event(QEvent *event)
break; break;
case QEvent::StyleChange: case QEvent::StyleChange:
doItemsLayout(); doItemsLayout();
if (!d->verticalScrollModeSet)
resetVerticalScrollMode();
if (!d->horizontalScrollModeSet)
resetHorizontalScrollMode();
break; break;
case QEvent::FocusOut: case QEvent::FocusOut:
d->checkPersistentEditorFocus(); d->checkPersistentEditorFocus();

View File

@ -75,8 +75,8 @@ class Q_WIDGETS_EXPORT QAbstractItemView : public QAbstractScrollArea
Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior) Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior WRITE setSelectionBehavior)
Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged) Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode) Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode)
Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode) Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode RESET resetVerticalScrollMode)
Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode WRITE setHorizontalScrollMode) Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode WRITE setHorizontalScrollMode RESET resetHorizontalScrollMode)
public: public:
enum SelectionMode { enum SelectionMode {
@ -147,9 +147,11 @@ public:
void setVerticalScrollMode(ScrollMode mode); void setVerticalScrollMode(ScrollMode mode);
ScrollMode verticalScrollMode() const; ScrollMode verticalScrollMode() const;
void resetVerticalScrollMode();
void setHorizontalScrollMode(ScrollMode mode); void setHorizontalScrollMode(ScrollMode mode);
ScrollMode horizontalScrollMode() const; ScrollMode horizontalScrollMode() const;
void resetHorizontalScrollMode();
void setAutoScroll(bool enable); void setAutoScroll(bool enable);
bool hasAutoScroll() const; bool hasAutoScroll() const;

View File

@ -461,6 +461,10 @@ public:
mutable bool delayedPendingLayout; mutable bool delayedPendingLayout;
bool moveCursorUpdatedView; bool moveCursorUpdatedView;
// Whether scroll mode has been explicitly set or its value come from SH_ItemView_ScrollMode
bool verticalScrollModeSet;
bool horizontalScrollModeSet;
private: private:
mutable QBasicTimer delayedLayout; mutable QBasicTimer delayedLayout;
mutable QBasicTimer fetchMoreTimer; mutable QBasicTimer fetchMoreTimer;

View File

@ -5215,6 +5215,9 @@ int QCommonStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget
case SH_Splitter_OpaqueResize: case SH_Splitter_OpaqueResize:
ret = true; ret = true;
break; break;
case SH_ItemView_ScrollMode:
ret = QAbstractItemView::ScrollPerItem;
break;
default: default:
ret = 0; ret = 0;
break; break;

View File

@ -3043,6 +3043,9 @@ int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w
ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay; ret = [NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay;
} }
break; break;
case SH_ItemView_ScrollMode:
ret = QAbstractItemView::ScrollPerPixel;
break;
default: default:
ret = QCommonStyle::styleHint(sh, opt, w, hret); ret = QCommonStyle::styleHint(sh, opt, w, hret);
break; break;

View File

@ -1978,6 +1978,10 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment,
tab is changed while dragging over the tabbar, in milliseconds. This tab is changed while dragging over the tabbar, in milliseconds. This
enum value has been introduced in Qt 5.4 enum value has been introduced in Qt 5.4
\value SH_ItemView_ScrollMode The default vertical and horizontal scroll mode as specified
by the style. Can be overridden with QAbstractItemView::setVerticalScrollMode() and
QAbstractItemView::setHorizontalScrollMode(). This enum value has been introduced in Qt 5.7.
\sa styleHint() \sa styleHint()
*/ */

View File

@ -732,6 +732,7 @@ public:
SH_Menu_SubMenuSloppyCloseTimeout, SH_Menu_SubMenuSloppyCloseTimeout,
SH_Menu_SubMenuResetWhenReenteringParent, SH_Menu_SubMenuResetWhenReenteringParent,
SH_Menu_SubMenuDontStartSloppyOnLeave, SH_Menu_SubMenuDontStartSloppyOnLeave,
SH_ItemView_ScrollMode,
// Add new style hint values here // Add new style hint values here
SH_CustomBase = 0xf0000000 SH_CustomBase = 0xf0000000

View File

@ -50,6 +50,7 @@
#include <qstyleditemdelegate.h> #include <qstyleditemdelegate.h>
#include <qstringlistmodel.h> #include <qstringlistmodel.h>
#include <qsortfilterproxymodel.h> #include <qsortfilterproxymodel.h>
#include <qproxystyle.h>
static inline void setFrameless(QWidget *w) static inline void setFrameless(QWidget *w)
{ {
@ -246,6 +247,7 @@ private slots:
void sizeHintChangeTriggersLayout(); void sizeHintChangeTriggersLayout();
void shiftSelectionAfterChangingModelContents(); void shiftSelectionAfterChangingModelContents();
void QTBUG48968_reentrant_updateEditorGeometries(); void QTBUG48968_reentrant_updateEditorGeometries();
void QTBUG50102_SH_ItemView_ScrollMode();
}; };
class MyAbstractItemDelegate : public QAbstractItemDelegate class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -2018,5 +2020,53 @@ void tst_QAbstractItemView::QTBUG48968_reentrant_updateEditorGeometries()
// No crash, all fine. // No crash, all fine.
} }
class ScrollModeProxyStyle: public QProxyStyle
{
public:
ScrollModeProxyStyle(QAbstractItemView::ScrollMode sm, QStyle *style = 0)
: QProxyStyle(style)
, scrollMode(sm == QAbstractItemView::ScrollPerItem ?
QAbstractItemView::ScrollPerPixel : QAbstractItemView::ScrollPerItem)
{ }
int styleHint(QStyle::StyleHint hint, const QStyleOption *opt, const QWidget *w, QStyleHintReturn *returnData) const override
{
if (hint == SH_ItemView_ScrollMode)
return scrollMode;
return baseStyle()->styleHint(hint, opt, w, returnData);
}
QAbstractItemView::ScrollMode scrollMode;
};
void tst_QAbstractItemView::QTBUG50102_SH_ItemView_ScrollMode()
{
QListView view;
// Default comes from the style
auto styleScrollMode = static_cast<QAbstractItemView::ScrollMode>(view.style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, &view, 0));
QCOMPARE(view.verticalScrollMode(), styleScrollMode);
QCOMPARE(view.horizontalScrollMode(), styleScrollMode);
// Change style, get new value
view.setStyle(new ScrollModeProxyStyle(styleScrollMode));
auto proxyScrollMode = static_cast<QAbstractItemView::ScrollMode>(view.style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, &view, 0));
QVERIFY(styleScrollMode != proxyScrollMode);
QCOMPARE(view.verticalScrollMode(), proxyScrollMode);
QCOMPARE(view.horizontalScrollMode(), proxyScrollMode);
// Explicitly set vertical, same value
view.setVerticalScrollMode(proxyScrollMode);
QCOMPARE(view.verticalScrollMode(), proxyScrollMode);
QCOMPARE(view.horizontalScrollMode(), proxyScrollMode);
// Change style, won't change value for vertical, will change for horizontal
view.setStyle(new ScrollModeProxyStyle(proxyScrollMode));
QCOMPARE(view.verticalScrollMode(), proxyScrollMode);
QCOMPARE(view.horizontalScrollMode(), styleScrollMode);
}
QTEST_MAIN(tst_QAbstractItemView) QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc" #include "tst_qabstractitemview.moc"

View File

@ -279,6 +279,18 @@ public:
mutable bool wrongIndex; mutable bool wrongIndex;
}; };
class ScrollPerItemListView : public QListView
{
public:
explicit ScrollPerItemListView(QWidget *parent = Q_NULLPTR)
: QListView(parent)
{
// Force per item scroll mode since it comes from the style by default
setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
}
};
void tst_QListView::initTestCase() void tst_QListView::initTestCase()
{ {
#ifdef Q_OS_WINCE //disable magic for WindowsCE #ifdef Q_OS_WINCE //disable magic for WindowsCE
@ -823,7 +835,7 @@ void tst_QListView::setCurrentIndex()
{ {
QStringListModel model(generateList(QLatin1String("item "), 20)); QStringListModel model(generateList(QLatin1String("item "), 20));
QListView view; ScrollPerItemListView view;
view.setModel(&model); view.setModel(&model);
view.resize(220,182); view.resize(220,182);
@ -1153,7 +1165,7 @@ void tst_QListView::scrollTo()
{ {
QWidget topLevel; QWidget topLevel;
setFrameless(&topLevel); setFrameless(&topLevel);
QListView lv(&topLevel); ScrollPerItemListView lv(&topLevel);
QStringListModel model(&lv); QStringListModel model(&lv);
QStringList list; QStringList list;
list << "Short item 1"; list << "Short item 1";
@ -1189,6 +1201,7 @@ void tst_QListView::scrollTo()
model.setStringList(list); model.setStringList(list);
lv.setModel(&model); lv.setModel(&model);
lv.setFixedSize(110, 200); lv.setFixedSize(110, 200);
topLevel.show(); topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
@ -1262,7 +1275,7 @@ void tst_QListView::scrollBarRanges()
const int rowHeight = 20; const int rowHeight = 20;
QWidget topLevel; QWidget topLevel;
QListView lv(&topLevel); ScrollPerItemListView lv(&topLevel);
QStringListModel model(&lv); QStringListModel model(&lv);
QStringList list; QStringList list;
for (int i = 0; i < rowCount; ++i) for (int i = 0; i < rowCount; ++i)
@ -1271,6 +1284,7 @@ void tst_QListView::scrollBarRanges()
model.setStringList(list); model.setStringList(list);
lv.setModel(&model); lv.setModel(&model);
lv.resize(250, 130); lv.resize(250, 130);
TestDelegate *delegate = new TestDelegate(&lv); TestDelegate *delegate = new TestDelegate(&lv);
delegate->m_sizeHint = QSize(100, rowHeight); delegate->m_sizeHint = QSize(100, rowHeight);
lv.setItemDelegate(delegate); lv.setItemDelegate(delegate);
@ -1814,7 +1828,7 @@ void tst_QListView::taskQTBUG_2233_scrollHiddenItems()
QWidget topLevel; QWidget topLevel;
setFrameless(&topLevel); setFrameless(&topLevel);
QListView view(&topLevel); ScrollPerItemListView view(&topLevel);
QStringListModel model(&view); QStringListModel model(&view);
QStringList list; QStringList list;
for (int i = 0; i < rowCount; ++i) for (int i = 0; i < rowCount; ++i)
@ -2060,7 +2074,7 @@ void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems()
{ {
QFETCH(int, flow); QFETCH(int, flow);
QListView lv; ScrollPerItemListView lv;
lv.setUniformItemSizes(true); lv.setUniformItemSizes(true);
lv.setFlow(static_cast<QListView::Flow>(flow)); lv.setFlow(static_cast<QListView::Flow>(flow));
@ -2155,7 +2169,7 @@ void tst_QListView::taskQTBUG_21804_hiddenItemsAndScrollingWithKeys()
model.setStringList(list); model.setStringList(list);
// create listview // create listview
QListView lv; ScrollPerItemListView lv;
lv.setFlow(static_cast<QListView::Flow>(flow)); lv.setFlow(static_cast<QListView::Flow>(flow));
lv.setSpacing(spacing); lv.setSpacing(spacing);
lv.setModel(&model); lv.setModel(&model);
@ -2227,7 +2241,7 @@ void tst_QListView::spacing()
model.setStringList(list); model.setStringList(list);
// create listview // create listview
QListView lv; ScrollPerItemListView lv;
lv.setFlow(static_cast<QListView::Flow>(flow)); lv.setFlow(static_cast<QListView::Flow>(flow));
lv.setModel(&model); lv.setModel(&model);
lv.setSpacing(spacing); lv.setSpacing(spacing);

View File

@ -503,7 +503,7 @@ void tst_QTreeView::construction()
QCOMPARE(view.sizeHintForRow(1), -1); QCOMPARE(view.sizeHintForRow(1), -1);
QVERIFY(!view.tabKeyNavigation()); QVERIFY(!view.tabKeyNavigation());
QCOMPARE(view.textElideMode(), Qt::ElideRight); QCOMPARE(view.textElideMode(), Qt::ElideRight);
QCOMPARE(view.verticalScrollMode(), QAbstractItemView::ScrollPerItem); QCOMPARE(static_cast<int>(view.verticalScrollMode()), view.style()->styleHint(QStyle::SH_ItemView_ScrollMode, 0, &view));
QCOMPARE(view.visualRect(QModelIndex()), QRect()); QCOMPARE(view.visualRect(QModelIndex()), QRect());
// QTreeView properties // QTreeView properties