Skip to content

Commit d1a7a00

Browse files
etienneskydakcarto
authored andcommitted
add save/load state and titleRect()
1 parent 234db45 commit d1a7a00

2 files changed

Lines changed: 79 additions & 18 deletions

File tree

‎src/gui/qgscollapsiblegroupbox.cpp‎

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,29 @@
2323
#include <QToolButton>
2424
#include <QMouseEvent>
2525
#include <QStyleOptionGroupBox>
26+
#include <QSettings>
2627

2728
QIcon QgsCollapsibleGroupBox::mCollapseIcon;
2829
QIcon QgsCollapsibleGroupBox::mExpandIcon;
2930

3031
QgsCollapsibleGroupBox::QgsCollapsibleGroupBox( QWidget *parent )
31-
: QGroupBox( parent ), mCollapsed( false )
32+
: QGroupBox( parent ), mCollapsed( false ), mSaveState( true )
3233
{
3334
init();
3435
}
3536

3637
QgsCollapsibleGroupBox::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+
4349
void QgsCollapsibleGroupBox::init()
4450
{
4551
// init icons
@@ -64,7 +70,7 @@ void QgsCollapsibleGroupBox::init()
6470

6571
void 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+
99162
void 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

147203
void 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 );

‎src/gui/qgscollapsiblegroupbox.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
3636
public:
3737
QgsCollapsibleGroupBox( QWidget *parent = 0 );
3838
QgsCollapsibleGroupBox( const QString &title, QWidget *parent = 0 );
39-
39+
~QgsCollapsibleGroupBox();
4040
bool isCollapsed() const { return mCollapsed; }
4141
void setCollapsed( bool collapse );
42+
void setSaveState( bool save ) { mSaveState = save; }
4243

4344
signals:
4445
void collapsedStateChanged( QWidget* );
@@ -48,16 +49,20 @@ class GUI_EXPORT QgsCollapsibleGroupBox : public QGroupBox
4849
void toggleCollapsed();
4950
void updateStyle();
5051

52+
protected slots:
53+
void loadState();
54+
void saveState();
55+
5156
protected:
5257
void init();
5358
void showEvent( QShowEvent *event );
5459
void mouseReleaseEvent( QMouseEvent *event );
60+
QRect titleRect() const;
61+
QString saveKey() const;
5562

56-
private:
5763
bool mCollapsed;
58-
QList< QWidget* > mHiddenWidgets;
64+
bool mSaveState;
5965
QToolButton* mCollapseButton;
60-
QRect mTitleRect;
6166

6267
static QIcon mCollapseIcon;
6368
static QIcon mExpandIcon;

0 commit comments

Comments
 (0)