2323#include < QToolButton>
2424#include < QMouseEvent>
2525#include < QStyleOptionGroupBox>
26+ #include < QSettings>
2627
2728QIcon QgsCollapsibleGroupBox::mCollapseIcon ;
2829QIcon QgsCollapsibleGroupBox::mExpandIcon ;
2930
3031QgsCollapsibleGroupBox::QgsCollapsibleGroupBox ( QWidget *parent )
31- : QGroupBox( parent ), mCollapsed( false )
32+ : QGroupBox( parent ), mCollapsed( false ), mSaveState( true )
3233{
3334 init ();
3435}
3536
3637QgsCollapsibleGroupBox::QgsCollapsibleGroupBox ( const QString &title,
3738 QWidget *parent )
38- : QGroupBox( title, parent ), mCollapsed( false )
39+ : QGroupBox( title, parent ), mCollapsed( false ), mSaveState( true )
3940{
4041 init ();
4142}
4243
44+ QgsCollapsibleGroupBox::~QgsCollapsibleGroupBox ()
45+ {
46+ saveState ();
47+ }
48+
4349void QgsCollapsibleGroupBox::init ()
4450{
4551 // init icons
@@ -64,7 +70,7 @@ void QgsCollapsibleGroupBox::init()
6470
6571void QgsCollapsibleGroupBox::showEvent ( QShowEvent * event )
6672{
67- QGroupBox::showEvent ( event );
73+ loadState ( );
6874
6975 updateStyle ();
7076
@@ -79,14 +85,15 @@ void QgsCollapsibleGroupBox::showEvent( QShowEvent * event )
7985 still emit signal for connections using expanded state */
8086 emit collapsedStateChanged ( this );
8187 }
88+ event->accept ();
8289}
8390
84- void QgsCollapsibleGroupBox::mouseReleaseEvent ( QMouseEvent *event )
91+ void QgsCollapsibleGroupBox::mouseReleaseEvent ( QMouseEvent *event )
8592{
8693 // catch mouse release over title when non checkable, to collapse/expand
87- if ( !isCheckable () && event->button () == Qt::LeftButton )
94+ if ( !isCheckable () && event->button () == Qt::LeftButton )
8895 {
89- if ( mTitleRect .contains ( event->pos () ) )
96+ if ( titleRect () .contains ( event->pos () ) )
9097 {
9198 toggleCollapsed ();
9299 return ;
@@ -96,6 +103,62 @@ void QgsCollapsibleGroupBox::mouseReleaseEvent( QMouseEvent *event )
96103 QGroupBox::mouseReleaseEvent ( event );
97104}
98105
106+ QRect QgsCollapsibleGroupBox::titleRect () const
107+ {
108+ QStyleOptionGroupBox box;
109+ initStyleOption ( &box );
110+ return style ()->subControlRect ( QStyle::CC_GroupBox, &box,
111+ QStyle::SC_GroupBoxLabel, this );
112+ }
113+
114+ QString QgsCollapsibleGroupBox::saveKey () const
115+ {
116+ // save key for load/save state
117+ // currently QgsCollapsibleGroupBox/window()/object
118+ QString saveKey = " /" + objectName ();
119+ // QObject* parentWidget = parent();
120+ // while ( parentWidget != NULL )
121+ // {
122+ // saveKey = "/" + parentWidget->objectName() + saveKey;
123+ // parentWidget = parentWidget->parent();
124+ // }
125+ // if ( parent() != NULL )
126+ // saveKey = "/" + parent()->objectName() + saveKey;
127+ saveKey = " /" + window ()->objectName () + saveKey;
128+ saveKey = " QgsCollapsibleGroupBox" + saveKey;
129+ return saveKey;
130+ }
131+
132+ void QgsCollapsibleGroupBox::loadState ()
133+ {
134+ if ( ! mSaveState )
135+ return ;
136+
137+ setUpdatesEnabled ( false );
138+
139+ QSettings settings;
140+ QString key = saveKey ();
141+ QVariant val = settings.value ( key + " /checked" );
142+ if ( ! val.isNull () )
143+ setChecked ( val.toBool () );
144+ val = settings.value ( key + " /collapsed" );
145+ if ( ! val.isNull () )
146+ setCollapsed ( val.toBool () );
147+
148+ setUpdatesEnabled ( true );
149+ }
150+
151+ void QgsCollapsibleGroupBox::saveState ()
152+ {
153+ if ( ! mSaveState )
154+ return ;
155+ QgsDebugMsg ( " key = " + saveKey () + " objectName = " + objectName () );
156+ QSettings settings;
157+ QString key = saveKey ();
158+ settings.setValue ( key + " /checked" , isChecked () );
159+ settings.setValue ( key + " /collapsed" , isCollapsed () );
160+ }
161+
99162void QgsCollapsibleGroupBox::checkToggled ( bool chkd )
100163{
101164 mCollapseButton ->setEnabled ( true ); // always keep enabled
@@ -135,13 +198,6 @@ void QgsCollapsibleGroupBox::updateStyle()
135198 mCollapseButton ->setStyleSheet ( ssd );
136199
137200 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 );
145201}
146202
147203void QgsCollapsibleGroupBox::setCollapsed ( bool collapse )
@@ -157,7 +213,7 @@ void QgsCollapsibleGroupBox::setCollapsed( bool collapse )
157213 QApplication::processEvents ();
158214 // set maximum height to hide contents - does this work in all envs?
159215 // setMaximumHeight( collapse ? 25 : 16777215 );
160- setMaximumHeight ( collapse ? mTitleRect . height () + 2 : 16777215 );
216+ setMaximumHeight ( collapse ? titleRect (). bottom () + 2 : 16777215 );
161217 mCollapseButton ->setIcon ( collapse ? mExpandIcon : mCollapseIcon );
162218
163219 emit collapsedStateChanged ( this );
0 commit comments