Skip to content
Permalink
Browse files

add updateStyle() slot - to call when application style/style sheet c…

…hanges
  • Loading branch information
etiennesky authored and dakcarto committed Sep 11, 2012
1 parent 0cc5923 commit 234db453c797a57e51cd2fec30cf6b886a1e0d3d
Showing with 43 additions and 27 deletions.
  1. +41 −27 src/gui/qgscollapsiblegroupbox.cpp
  2. +2 −0 src/gui/qgscollapsiblegroupbox.h
@@ -49,41 +49,25 @@ void QgsCollapsibleGroupBox::init()
mExpandIcon = QgsApplication::getThemeIcon( "/mIconExpand.png" );
}

// customize style sheet
// TODO: move to app stylesheet system, when appropriate
QString ss;
ss += "QgsCollapsibleGroupBox::title {";
ss += " subcontrol-origin: margin;";
ss += " subcontrol-position: top left;";
ss += " margin-left: 20px;"; // offset for disclosure triangle
ss += " margin-right: 5px;"; // a little bit of space on the right, to match space on the left
ss += "}";
setStyleSheet( ss );

// collapse button
mCollapseButton = new QToolButton( this );
mCollapseButton->setObjectName( "collapseButton" );
mCollapseButton->setAutoRaise( true );
mCollapseButton->setFixedSize( 16, 16 );
// TODO set size (as well as margins) depending on theme
// TODO set size (as well as margins) depending on theme, in updateStyle()
mCollapseButton->setIconSize( QSize( 12, 12 ) );
mCollapseButton->setIcon( mCollapseIcon );

// clear toolbutton default background and border
// TODO: move to app stylesheet system, when appropriate
QString ssd;
ssd = QString( "QgsCollapsibleGroupBox > QToolButton#%1 {" ).arg( mCollapseButton->objectName() );
ssd += " background-color: rgba(255, 255, 255, 0); border: none;";
ssd += "}";
mCollapseButton->setStyleSheet( ssd );

connect( mCollapseButton, SIGNAL( clicked() ), this, SLOT( toggleCollapsed() ) );
connect( this, SIGNAL( toggled( bool ) ), this, SLOT( checkToggled( bool ) ) );
}

void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )
{
QGroupBox::showEvent( event );

updateStyle();

// expand if needed - any calls to setCollapsed() before only set mCollapsed
if ( mCollapsed )
{
@@ -102,11 +86,7 @@ void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event )
// catch mouse release over title when non checkable, to collapse/expand
if ( !isCheckable() && event->button() == Qt::LeftButton )
{
QStyleOptionGroupBox box;
initStyleOption( &box );
QRect rect = style()->subControlRect( QStyle::CC_GroupBox, &box,
QStyle::SC_GroupBoxLabel, this );
if ( rect.contains( event->pos() ) )
if ( mTitleRect.contains( event->pos() ) )
{
toggleCollapsed();
return;
@@ -131,6 +111,39 @@ void QgsCollapsibleGroupBox::toggleCollapsed()
setCollapsed( !mCollapsed );
}

void QgsCollapsibleGroupBox::updateStyle()
{
setUpdatesEnabled( false );

// customize style sheet
// TODO: move to app stylesheet system, when appropriate
QString ss;
ss += "QgsCollapsibleGroupBox::title {";
ss += " subcontrol-origin: margin;";
ss += " subcontrol-position: top left;";
ss += " margin-left: 20px;"; // offset for disclosure triangle
ss += " margin-right: 5px;"; // a little bit of space on the right, to match space on the left
ss += "}";
setStyleSheet( ss );

// clear toolbutton default background and border
// TODO: move to app stylesheet system, when appropriate
QString ssd;
ssd = QString( "QgsCollapsibleGroupBox > QToolButton#%1 {" ).arg( mCollapseButton->objectName() );
ssd += " background-color: rgba(255, 255, 255, 0); border: none;";
ssd += "}";
mCollapseButton->setStyleSheet( ssd );

setUpdatesEnabled( true );

// init title rect for later use - should not change during execution
// if it needs updating (e.g. in options dlg), call slot when style changes
QStyleOptionGroupBox box;
initStyleOption( &box );
mTitleRect = style()->subControlRect( QStyle::CC_GroupBox, &box,
QStyle::SC_GroupBoxLabel, this );
}

void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
{
mCollapsed = collapse;
@@ -142,8 +155,9 @@ void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
setFlat( collapse );
// avoid flicker in X11
QApplication::processEvents();
// set maximum height to 25 to hide contents - does this work in all envs?
setMaximumHeight( collapse ? 25 : 16777215 );
// set maximum height to hide contents - does this work in all envs?
// setMaximumHeight( collapse ? 25 : 16777215 );
setMaximumHeight( collapse ? mTitleRect.height() + 2 : 16777215 );
mCollapseButton->setIcon( collapse ? mExpandIcon : mCollapseIcon );

emit collapsedStateChanged( this );
@@ -46,6 +46,7 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
public slots:
void checkToggled( bool ckd );
void toggleCollapsed();
void updateStyle();

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

static QIcon mCollapseIcon;
static QIcon mExpandIcon;

0 comments on commit 234db45

Please sign in to comment.
You can’t perform that action at this time.