From ffe02c37b6c5993a410f0dc709dd80211d2414e6 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 2 May 2019 09:37:02 +1000 Subject: [PATCH] Allow QgsScrollArea to maintain horizontal width of area for child widgets, and apply scroll area to vertical contents only --- python/gui/auto_generated/qgsscrollarea.sip.in | 12 ++++++++++++ src/gui/qgsscrollarea.cpp | 17 +++++++++++++++++ src/gui/qgsscrollarea.h | 13 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/python/gui/auto_generated/qgsscrollarea.sip.in b/python/gui/auto_generated/qgsscrollarea.sip.in index 15c022a65f7..44b0e214def 100644 --- a/python/gui/auto_generated/qgsscrollarea.sip.in +++ b/python/gui/auto_generated/qgsscrollarea.sip.in @@ -45,11 +45,23 @@ QScrollArea itself or its child viewport(). %Docstring Returns ``True`` if a scroll recently occurred within the QScrollArea or its child viewport() +%End + + void setVerticalOnly( bool verticalOnly ); +%Docstring +Sets whether the scroll area only applies vertical. + +If set to ``True``, then scroll area children will resize horizontally to match the width of +the scroll area widget. + +.. versionadded:: 3.8 %End protected: virtual void wheelEvent( QWheelEvent *event ); + virtual void resizeEvent( QResizeEvent *event ); + }; diff --git a/src/gui/qgsscrollarea.cpp b/src/gui/qgsscrollarea.cpp index 77bb50bfdab..110e23131c5 100644 --- a/src/gui/qgsscrollarea.cpp +++ b/src/gui/qgsscrollarea.cpp @@ -34,6 +34,13 @@ void QgsScrollArea::wheelEvent( QWheelEvent *e ) QScrollArea::wheelEvent( e ); } +void QgsScrollArea::resizeEvent( QResizeEvent *event ) +{ + if ( mVerticalOnly && widget() ) + widget()->setFixedWidth( event->size().width() ); + QScrollArea::resizeEvent( event ); +} + void QgsScrollArea::scrollOccurred() { mTimer.setSingleShot( true ); @@ -45,6 +52,16 @@ bool QgsScrollArea::hasScrolled() const return mTimer.isActive(); } +void QgsScrollArea::setVerticalOnly( bool verticalOnly ) +{ + mVerticalOnly = verticalOnly; + if ( mVerticalOnly ) + setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + + if ( mVerticalOnly && widget() ) + widget()->setFixedWidth( size().width() ); +} + ///@cond PRIVATE ScrollAreaFilter::ScrollAreaFilter( QgsScrollArea *parent, QWidget *viewPort ) diff --git a/src/gui/qgsscrollarea.h b/src/gui/qgsscrollarea.h index 5485a21eecd..3286f3f1bec 100644 --- a/src/gui/qgsscrollarea.h +++ b/src/gui/qgsscrollarea.h @@ -61,12 +61,25 @@ class GUI_EXPORT QgsScrollArea : public QScrollArea */ bool hasScrolled() const; + /** + * Sets whether the scroll area only applies vertical. + * + * If set to TRUE, then scroll area children will resize horizontally to match the width of + * the scroll area widget. + * + * \since QGIS 3.8 + */ + void setVerticalOnly( bool verticalOnly ); + protected: void wheelEvent( QWheelEvent *event ) override; + void resizeEvent( QResizeEvent *event ) override; private: QTimer mTimer; ScrollAreaFilter *mFilter = nullptr; + bool mVerticalOnly = false; + }; #ifndef SIP_RUN