Skip to content

Commit a645f43

Browse files
committedFeb 19, 2013
Updates to QgsCollapsibleGroupBoxBasic
- Use title instead of checkbox to define QgsCollapsibleGroupBox offset - Fix error when group box has form layout with QPushButtons that refuse to fully hide - Update vertical offsets for Ubuntu - Add control-click modifier and tool tip for toggling all collapsible group boxes - Ensure disclosure triangle mirrors enabled state of group box
1 parent a86622b commit a645f43

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed
 

‎src/gui/qgscollapsiblegroupbox.cpp

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <QToolButton>
2424
#include <QMouseEvent>
25+
#include <QPushButton>
2526
#include <QStyleOptionGroupBox>
2627
#include <QSettings>
2728
#include <QScrollArea>
@@ -47,7 +48,6 @@ QgsCollapsibleGroupBoxBasic::~QgsCollapsibleGroupBoxBasic()
4748
//QgsDebugMsg( "Entered" );
4849
}
4950

50-
5151
void QgsCollapsibleGroupBoxBasic::init()
5252
{
5353
//QgsDebugMsg( "Entered" );
@@ -126,6 +126,10 @@ void QgsCollapsibleGroupBoxBasic::showEvent( QShowEvent * event )
126126
// emit signal for connections using collapsed state
127127
emit collapsedStateChanged( isCollapsed() );
128128
}
129+
130+
// verify triangle mirrors groupbox's enabled state
131+
mCollapseButton->setEnabled( isEnabled() );
132+
129133
// set mShown after first setCollapsed call or expanded groupboxes
130134
// will scroll scroll areas when first shown
131135
mShown = true;
@@ -149,7 +153,7 @@ void QgsCollapsibleGroupBoxBasic::mousePressEvent( QMouseEvent *event )
149153

150154
void QgsCollapsibleGroupBoxBasic::mouseReleaseEvent( QMouseEvent *event )
151155
{
152-
mAltDown = ( event->modifiers() & Qt::AltModifier );
156+
mAltDown = ( event->modifiers() & ( Qt::AltModifier | Qt::ControlModifier ) );
153157
mTitleClicked = ( titleRect().contains( event->pos() ) );
154158

155159
// sync group when title is alt-clicked
@@ -177,6 +181,19 @@ void QgsCollapsibleGroupBoxBasic::changeEvent( QEvent *event )
177181
mCollapseButton->setEnabled( true );
178182
}
179183

184+
void QgsCollapsibleGroupBoxBasic::setSyncGroup( QString grp )
185+
{
186+
mSyncGroup = grp;
187+
if ( !grp.isEmpty() )
188+
{
189+
mCollapseButton->setToolTip( tr( "Ctrl(or Alt)-click to toggle all" ) );
190+
}
191+
else
192+
{
193+
mCollapseButton->setToolTip( QString( "" ) );
194+
}
195+
}
196+
180197
QRect QgsCollapsibleGroupBoxBasic::titleRect() const
181198
{
182199
QStyleOptionGroupBox box;
@@ -255,20 +272,34 @@ void QgsCollapsibleGroupBoxBasic::updateStyle()
255272
{
256273
setUpdatesEnabled( false );
257274

275+
QSettings settings;
276+
// NOTE: QGIS-Style groupbox styled in app stylesheet
277+
bool usingQgsStyle = settings.value( "qgis/stylesheet/groupBoxCustom", QVariant( false ) ).toBool();
278+
279+
QStyleOptionGroupBox box;
280+
initStyleOption( &box );
281+
QRect rectFrame = style()->subControlRect( QStyle::CC_GroupBox, &box,
282+
QStyle::SC_GroupBoxFrame, this );
283+
QRect rectTitle = titleRect();
284+
258285
// margin/offset defaults
259286
int marginLeft = 20; // title margin for disclosure triangle
260287
int marginRight = 5; // a little bit of space on the right, to match space on the left
261288
int offsetLeft = 0; // offset for oxygen theme
262-
int offsetTop = 0;
263-
// int offsetTop2 = 0; // offset for triangle
264-
265-
// starting top offset for custom groupboxes in app stylesheet
266-
QStyleOptionGroupBox box;
267-
initStyleOption( &box );
268-
QRect rectCheckBox = style()->subControlRect( QStyle::CC_GroupBox, &box,
269-
QStyle::SC_GroupBoxCheckBox, this );
270-
int offsetTop2 = rectCheckBox.top(); // offset for triangle
289+
int offsetStyle = QApplication::style()->objectName().contains( "macintosh" ) ? 1 : 0;
290+
int topBuffer = ( usingQgsStyle ? 3 : 1 ) + offsetStyle; // space between top of title or triangle and widget above
291+
int offsetTop = topBuffer;
292+
int offsetTopTri = topBuffer; // offset for triangle
271293

294+
if ( mCollapseButton->height() < rectTitle.height() ) // triangle's height > title text's, offset triangle
295+
{
296+
offsetTopTri += ( rectTitle.height() - mCollapseButton->height() ) / 2 ;
297+
// offsetTopTri += rectTitle.top();
298+
}
299+
else if ( rectTitle.height() < mCollapseButton->height() ) // title text's height < triangle's, offset title
300+
{
301+
offsetTop += ( mCollapseButton->height() - rectTitle.height() ) / 2;
302+
}
272303

273304
// calculate offset if frame overlaps triangle (oxygen theme)
274305
// using an offset of 6 pixels from frame border
@@ -289,22 +320,27 @@ void QgsCollapsibleGroupBoxBasic::updateStyle()
289320
// if is checkable align with checkbox
290321
offsetTop = ( rectCheckBox.height() / 2 ) -
291322
( mCollapseButton->height() / 2 ) + rectCheckBox.top();
292-
offsetTop2 = offsetTop + 1;
323+
offsetTopTri = offsetTop + 1;
293324
}
294325
else
295326
{
296327
offsetTop = 6 + rectFrame.top();
297-
offsetTop2 = offsetTop;
328+
offsetTopTri = offsetTop;
298329
}
299330
}
300331
}
301332

302333
QgsDebugMsg( QString( "groupbox: %1 style: %2 offset: left=%3 top=%4 top2=%5" ).arg(
303-
objectName() ).arg( QApplication::style()->objectName() ).arg( offsetLeft ).arg( offsetTop ).arg( offsetTop2 ) );
334+
objectName() ).arg( QApplication::style()->objectName() ).arg( offsetLeft ).arg( offsetTop ).arg( offsetTopTri ) );
304335

305336
// customize style sheet for collapse/expand button and force left-aligned title
306-
// TODO: move to app stylesheet system, when appropriate
307337
QString ss;
338+
if ( usingQgsStyle || QApplication::style()->objectName().contains( "macintosh" ) )
339+
{
340+
ss += "QgsCollapsibleGroupBoxBasic, QgsCollapsibleGroupBox {";
341+
ss += QString( " margin-top: %1px;" ).arg( topBuffer + ( usingQgsStyle ? rectTitle.height() + 5 : rectFrame.top() ) );
342+
ss += "}";
343+
}
308344
ss += "QgsCollapsibleGroupBoxBasic::title, QgsCollapsibleGroupBox::title {";
309345
ss += " subcontrol-origin: margin;";
310346
ss += " subcontrol-position: top left;";
@@ -321,8 +357,8 @@ void QgsCollapsibleGroupBoxBasic::updateStyle()
321357
ssd += " background-color: rgba(255, 255, 255, 0); border: none;";
322358
ssd += "}";
323359
mCollapseButton->setStyleSheet( ssd );
324-
if ( offsetLeft != 0 || offsetTop2 != 0 )
325-
mCollapseButton->move( offsetLeft, offsetTop2 );
360+
if ( offsetLeft != 0 || offsetTopTri != 0 )
361+
mCollapseButton->move( offsetLeft, offsetTopTri );
326362

327363
setUpdatesEnabled( true );
328364
}
@@ -341,6 +377,31 @@ void QgsCollapsibleGroupBoxBasic::setCollapsed( bool collapse )
341377
// avoid flicker in X11
342378
QApplication::processEvents();
343379

380+
// handle QPushButtons in form layouts that stay visible on collapse (Qt bug)
381+
// set flat on collapse for fix, but preserve button's flat value when expanding
382+
if ( collapse )
383+
{
384+
foreach ( QPushButton* pBtn, findChildren<QPushButton *>() )
385+
{
386+
if ( ! pBtn->isFlat() )
387+
{
388+
if ( ! pBtn->property( "PushButtonFlat" ).isValid() )
389+
pBtn->setProperty( "PushButtonFlat", QVariant( false ) );
390+
391+
pBtn->setFlat( true );
392+
}
393+
}
394+
}
395+
else
396+
{
397+
foreach ( QPushButton* pBtn, findChildren<QPushButton *>() )
398+
{
399+
// restore any previous flat value
400+
if ( pBtn->property( "PushButtonFlat" ).isValid() )
401+
pBtn->setFlat( false );
402+
}
403+
}
404+
344405
// set maximum height to hide contents - does this work in all envs?
345406
// setMaximumHeight( collapse ? 25 : 16777215 );
346407
setMaximumHeight( collapse ? titleRect().bottom() + 6 : 16777215 );

‎src/gui/qgscollapsiblegroupbox.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class QgsGroupBoxCollapseButton: public QToolButton
4545
protected:
4646
void mouseReleaseEvent( QMouseEvent *event )
4747
{
48-
mAltDown = ( event->modifiers() & Qt::AltModifier );
48+
mAltDown = ( event->modifiers() & ( Qt::AltModifier | Qt::ControlModifier ) );
4949
QToolButton::mouseReleaseEvent( event );
5050
}
5151

@@ -79,7 +79,7 @@ class GUI_EXPORT QgsCollapsibleGroupBoxBasic : public QGroupBox
7979

8080
/** Named group which synchronizes collapsing action when triangle is clicked while holding alt modifier key */
8181
QString syncGroup() const { return mSyncGroup; }
82-
void setSyncGroup( QString grp ) { mSyncGroup = grp; }
82+
void setSyncGroup( QString grp );
8383

8484
//! set this to false to not automatically scroll parent QScrollArea to this widget's contents when expanded
8585
void setScrollOnExpand( bool scroll ) { mScrollOnExpand = scroll; }

0 commit comments

Comments
 (0)
Please sign in to comment.