Skip to content

Commit

Permalink
Allow QgsScrollArea to maintain horizontal width of area for
Browse files Browse the repository at this point in the history
child widgets, and apply scroll area to vertical contents only
  • Loading branch information
nyalldawson committed May 2, 2019
1 parent 5a57336 commit ffe02c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/gui/auto_generated/qgsscrollarea.sip.in
Expand Up @@ -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 );


};

Expand Down
17 changes: 17 additions & 0 deletions src/gui/qgsscrollarea.cpp
Expand Up @@ -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 );
Expand All @@ -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 )
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsscrollarea.h
Expand Up @@ -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
Expand Down

0 comments on commit ffe02c3

Please sign in to comment.