Skip to content

Commit 234db45

Browse files
etienneskydakcarto
authored andcommitted
add updateStyle() slot - to call when application style/style sheet changes
1 parent 0cc5923 commit 234db45

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/gui/qgscollapsiblegroupbox.cpp

+41-27
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,25 @@ void QgsCollapsibleGroupBox::init()
4949
mExpandIcon = QgsApplication::getThemeIcon( "/mIconExpand.png" );
5050
}
5151

52-
// customize style sheet
53-
// TODO: move to app stylesheet system, when appropriate
54-
QString ss;
55-
ss += "QgsCollapsibleGroupBox::title {";
56-
ss += " subcontrol-origin: margin;";
57-
ss += " subcontrol-position: top left;";
58-
ss += " margin-left: 20px;"; // offset for disclosure triangle
59-
ss += " margin-right: 5px;"; // a little bit of space on the right, to match space on the left
60-
ss += "}";
61-
setStyleSheet( ss );
62-
6352
// collapse button
6453
mCollapseButton = new QToolButton( this );
6554
mCollapseButton->setObjectName( "collapseButton" );
6655
mCollapseButton->setAutoRaise( true );
6756
mCollapseButton->setFixedSize( 16, 16 );
68-
// TODO set size (as well as margins) depending on theme
57+
// TODO set size (as well as margins) depending on theme, in updateStyle()
6958
mCollapseButton->setIconSize( QSize( 12, 12 ) );
7059
mCollapseButton->setIcon( mCollapseIcon );
7160

72-
// clear toolbutton default background and border
73-
// TODO: move to app stylesheet system, when appropriate
74-
QString ssd;
75-
ssd = QString( "QgsCollapsibleGroupBox > QToolButton#%1 {" ).arg( mCollapseButton->objectName() );
76-
ssd += " background-color: rgba(255, 255, 255, 0); border: none;";
77-
ssd += "}";
78-
mCollapseButton->setStyleSheet( ssd );
79-
8061
connect( mCollapseButton, SIGNAL( clicked() ), this, SLOT( toggleCollapsed() ) );
8162
connect( this, SIGNAL( toggled( bool ) ), this, SLOT( checkToggled( bool ) ) );
8263
}
8364

8465
void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )
8566
{
8667
QGroupBox::showEvent( event );
68+
69+
updateStyle();
70+
8771
// expand if needed - any calls to setCollapsed() before only set mCollapsed
8872
if ( mCollapsed )
8973
{
@@ -102,11 +86,7 @@ void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event )
10286
// catch mouse release over title when non checkable, to collapse/expand
10387
if ( !isCheckable() && event->button() == Qt::LeftButton )
10488
{
105-
QStyleOptionGroupBox box;
106-
initStyleOption( &box );
107-
QRect rect = style()->subControlRect( QStyle::CC_GroupBox, &box,
108-
QStyle::SC_GroupBoxLabel, this );
109-
if ( rect.contains( event->pos() ) )
89+
if ( mTitleRect.contains( event->pos() ) )
11090
{
11191
toggleCollapsed();
11292
return;
@@ -131,6 +111,39 @@ void QgsCollapsibleGroupBox::toggleCollapsed()
131111
setCollapsed( !mCollapsed );
132112
}
133113

114+
void QgsCollapsibleGroupBox::updateStyle()
115+
{
116+
setUpdatesEnabled( false );
117+
118+
// customize style sheet
119+
// TODO: move to app stylesheet system, when appropriate
120+
QString ss;
121+
ss += "QgsCollapsibleGroupBox::title {";
122+
ss += " subcontrol-origin: margin;";
123+
ss += " subcontrol-position: top left;";
124+
ss += " margin-left: 20px;"; // offset for disclosure triangle
125+
ss += " margin-right: 5px;"; // a little bit of space on the right, to match space on the left
126+
ss += "}";
127+
setStyleSheet( ss );
128+
129+
// clear toolbutton default background and border
130+
// TODO: move to app stylesheet system, when appropriate
131+
QString ssd;
132+
ssd = QString( "QgsCollapsibleGroupBox > QToolButton#%1 {" ).arg( mCollapseButton->objectName() );
133+
ssd += " background-color: rgba(255, 255, 255, 0); border: none;";
134+
ssd += "}";
135+
mCollapseButton->setStyleSheet( ssd );
136+
137+
setUpdatesEnabled( true );
138+
139+
// init title rect for later use - should not change during execution
140+
// if it needs updating (e.g. in options dlg), call slot when style changes
141+
QStyleOptionGroupBox box;
142+
initStyleOption( &box );
143+
mTitleRect = style()->subControlRect( QStyle::CC_GroupBox, &box,
144+
QStyle::SC_GroupBoxLabel, this );
145+
}
146+
134147
void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
135148
{
136149
mCollapsed = collapse;
@@ -142,8 +155,9 @@ void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
142155
setFlat( collapse );
143156
// avoid flicker in X11
144157
QApplication::processEvents();
145-
// set maximum height to 25 to hide contents - does this work in all envs?
146-
setMaximumHeight( collapse ? 25 : 16777215 );
158+
// set maximum height to hide contents - does this work in all envs?
159+
// setMaximumHeight( collapse ? 25 : 16777215 );
160+
setMaximumHeight( collapse ? mTitleRect.height() + 2 : 16777215 );
147161
mCollapseButton->setIcon( collapse ? mExpandIcon : mCollapseIcon );
148162

149163
emit collapsedStateChanged( this );

src/gui/qgscollapsiblegroupbox.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
4646
public slots:
4747
void checkToggled( bool ckd );
4848
void toggleCollapsed();
49+
void updateStyle();
4950

5051
protected:
5152
void init();
@@ -56,6 +57,7 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
5657
bool mCollapsed;
5758
QList< QWidget* > mHiddenWidgets;
5859
QToolButton* mCollapseButton;
60+
QRect mTitleRect;
5961

6062
static QIcon mCollapseIcon;
6163
static QIcon mExpandIcon;

0 commit comments

Comments
 (0)