Skip to content
Permalink
Browse files
Add shift-click modifier to QgsCollapsibleGroupBox
- Expands current group box on shift-click, then collapses any others in sync group
  • Loading branch information
dakcarto committed Feb 26, 2013
1 parent 37208df commit e60891c28c738757d89a400aba02b3302d07e2cd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
@@ -4,6 +4,7 @@
* A groupbox that collapses/expands when toggled.
* Basic class QgsCollapsibleGroupBoxBasic does not auto-save collapsed or checked state
* Holding Alt modifier key when toggling collapsed state will synchronize the toggling across other collapsible group boxes with the same syncGroup QString value
* Holding Shift modifier key when attempting to toggle collapsed state will expand current group box, then collapse any others with the same syncGroup QString value
* @note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
* bool collapsed, QString syncGroup
*/
@@ -51,6 +52,7 @@ class QgsCollapsibleGroupBoxBasic : QGroupBox
* A groupbox that collapses/expands when toggled and can save its collapsed and checked states.
* By default, it auto-saves only its collapsed state to the global settings based on the widget and it's parent names.
* Holding Alt modifier key when toggling collapsed state will synchronize the toggling across other collapsible group boxes with the same syncGroup QString value
* Holding Shift modifier key when attempting to toggle collapsed state will expand current group box, then collapse any others with the same syncGroup QString value
* @see basic class QgsCollapsibleGroupBoxBasic which does not auto-save states
* @note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
* bool collapsed, bool saveCollapsedState, bool saveCheckedState, QString syncGroup
@@ -61,6 +61,7 @@ void QgsCollapsibleGroupBoxBasic::init()
mSyncParent = 0;
mSyncGroup = "";
mAltDown = false;
mShiftDown = false;
mTitleClicked = false;

// init icons
@@ -138,8 +139,8 @@ void QgsCollapsibleGroupBoxBasic::showEvent( QShowEvent * event )

void QgsCollapsibleGroupBoxBasic::mousePressEvent( QMouseEvent *event )
{
// avoid leaving checkbox in pressed state if alt-clicking
if ( event->modifiers() & Qt::AltModifier
// avoid leaving checkbox in pressed state if alt- or shift-clicking
if ( event->modifiers() & ( Qt::AltModifier | Qt::ControlModifier | Qt::ShiftModifier )
&& titleRect().contains( event->pos() )
&& isCheckable() )
{
@@ -154,12 +155,14 @@ void QgsCollapsibleGroupBoxBasic::mousePressEvent( QMouseEvent *event )
void QgsCollapsibleGroupBoxBasic::mouseReleaseEvent( QMouseEvent *event )
{
mAltDown = ( event->modifiers() & ( Qt::AltModifier | Qt::ControlModifier ) );
mShiftDown = ( event->modifiers() & Qt::ShiftModifier );
mTitleClicked = ( titleRect().contains( event->pos() ) );

// sync group when title is alt-clicked
// collapse/expand when title is clicked and non-checkable
// expand current and collapse others on shift-click
if ( event->button() == Qt::LeftButton && mTitleClicked &&
( mAltDown || !isCheckable() ) )
( mAltDown || mShiftDown || !isCheckable() ) )
{
toggleCollapsed();
return;
@@ -184,14 +187,12 @@ void QgsCollapsibleGroupBoxBasic::changeEvent( QEvent *event )
void QgsCollapsibleGroupBoxBasic::setSyncGroup( QString grp )
{
mSyncGroup = grp;
QString tipTxt = QString( "" );
if ( !grp.isEmpty() )
{
mCollapseButton->setToolTip( tr( "Ctrl(or Alt)-click to toggle all" ) );
}
else
{
mCollapseButton->setToolTip( QString( "" ) );
tipTxt = tr( "Ctrl(or Alt)-click to toggle all" ) + "\n" + tr( "Shift-click to expand, then collapse others" );
}
mCollapseButton->setToolTip( tipTxt );
}

QRect QgsCollapsibleGroupBoxBasic::titleRect() const
@@ -202,6 +203,14 @@ QRect QgsCollapsibleGroupBoxBasic::titleRect() const
QStyle::SC_GroupBoxLabel, this );
}

void QgsCollapsibleGroupBoxBasic::clearModifiers()
{
mCollapseButton->setAltDown( false );
mCollapseButton->setShiftDown( false );
mAltDown = false;
mShiftDown = false;
}

void QgsCollapsibleGroupBoxBasic::checkToggled( bool chkd )
{
mCollapseButton->setEnabled( true ); // always keep enabled
@@ -219,13 +228,15 @@ void QgsCollapsibleGroupBoxBasic::toggleCollapsed()
QgsGroupBoxCollapseButton* collBtn = qobject_cast<QgsGroupBoxCollapseButton*>( QObject::sender() );
senderCollBtn = ( collBtn && collBtn == mCollapseButton );

mAltDown = ( mAltDown || mCollapseButton->altDown() );
mShiftDown = ( mShiftDown || mCollapseButton->shiftDown() );

// find any sync group siblings and toggle them
if ((( senderCollBtn && mCollapseButton->altDown() ) || ( mTitleClicked && mAltDown ) )
if (( senderCollBtn || mTitleClicked )
&& ( mAltDown || mShiftDown )
&& !mSyncGroup.isEmpty() )
{
mCollapseButton->setAltDown( false );
mAltDown = false;
QgsDebugMsg( "Alt key down, syncing group" );
QgsDebugMsg( "Alt or Shift key down, syncing group" );
// get pointer to parent or grandparent widget
if ( parentWidget() )
{
@@ -253,10 +264,19 @@ void QgsCollapsibleGroupBoxBasic::toggleCollapsed()
{
if ( grpbox->syncGroup() == syncGroup() && grpbox->isEnabled() )
{
grpbox->setCollapsed( !thisCollapsed );
if ( mShiftDown && grpbox == dynamic_cast<QgsCollapsibleGroupBoxBasic *>( this ) )
{
// expand current group box on shift-click
setCollapsed( false );
}
else
{
grpbox->setCollapsed( mShiftDown ? true : !thisCollapsed );
}
}
}

clearModifiers();
return;
}
else
@@ -265,7 +285,17 @@ void QgsCollapsibleGroupBoxBasic::toggleCollapsed()
}
}

setCollapsed( !mCollapsed );
// expand current group box on shift-click, even if no sync group
if ( mShiftDown )
{
setCollapsed( false );
}
else
{
setCollapsed( !mCollapsed );
}

clearModifiers();
}

void QgsCollapsibleGroupBoxBasic::updateStyle()
@@ -35,29 +35,35 @@ class QgsGroupBoxCollapseButton: public QToolButton

public:
QgsGroupBoxCollapseButton( QWidget *parent = 0 )
: QToolButton( parent ), mAltDown( false ) {}
: QToolButton( parent ), mAltDown( false ), mShiftDown( false ) {}

~QgsGroupBoxCollapseButton() {}

bool altDown() const { return mAltDown; }
void setAltDown( bool updown ) { mAltDown = updown; }

bool shiftDown() const { return mShiftDown; }
void setShiftDown( bool shiftdown ) { mShiftDown = shiftdown; }

protected:
void mouseReleaseEvent( QMouseEvent *event )
{
mAltDown = ( event->modifiers() & ( Qt::AltModifier | Qt::ControlModifier ) );
mShiftDown = ( event->modifiers() & Qt::ShiftModifier );
QToolButton::mouseReleaseEvent( event );
}

private:
bool mAltDown;
bool mShiftDown;
};

/** \ingroup gui
* \class QgsCollapsibleGroupBoxBasic
* A groupbox that collapses/expands when toggled.
* Basic class QgsCollapsibleGroupBoxBasic does not auto-save collapsed or checked state
* Holding Alt modifier key when toggling collapsed state will synchronize the toggling across other collapsible group boxes with the same syncGroup QString value
* Holding Shift modifier key when attempting to toggle collapsed state will expand current group box, then collapse any others with the same syncGroup QString value
* @note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
* bool collapsed, QString syncGroup
*/
@@ -101,6 +107,7 @@ class GUI_EXPORT QgsCollapsibleGroupBoxBasic : public QGroupBox

void updateStyle();
QRect titleRect() const;
void clearModifiers();

bool mCollapsed;
bool mInitFlat;
@@ -112,6 +119,7 @@ class GUI_EXPORT QgsCollapsibleGroupBoxBasic : public QGroupBox
QWidget* mSyncParent;
QString mSyncGroup;
bool mAltDown;
bool mShiftDown;
bool mTitleClicked;

static QIcon mCollapseIcon;
@@ -123,6 +131,7 @@ class GUI_EXPORT QgsCollapsibleGroupBoxBasic : public QGroupBox
* A groupbox that collapses/expands when toggled and can save its collapsed and checked states.
* By default, it auto-saves only its collapsed state to the global settings based on the widget and it's parent names.
* Holding Alt modifier key when toggling collapsed state will synchronize the toggling across other collapsible group boxes with the same syncGroup QString value
* Holding Shift modifier key when attempting to toggle collapsed state will expand current group box, then collapse any others with the same syncGroup QString value
* @see basic class QgsCollapsibleGroupBoxBasic which does not auto-save states
* @note To add Collapsible properties in promoted QtDesigner widgets, you can add the following "Dynamic properties" by clicking on the green + in the propreties palette:
* bool collapsed, bool saveCollapsedState, bool saveCheckedState, QString syncGroup

0 comments on commit e60891c

Please sign in to comment.