2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "uistatesaver.h"
22 #include <KConfigGroup>
24 #include <QtGui/QComboBox>
25 #include <QtGui/QHeaderView>
26 #include <QtGui/QSplitter>
27 #include <QtGui/QTabWidget>
28 #include <QtGui/QTreeView>
33 static void process( QSplitter
*splitter
, KConfigGroup
&config
)
35 if ( splitter
->sizes().count( 0 ) == splitter
->sizes().count() )
37 config
.writeEntry( splitter
->objectName(), splitter
->sizes() );
40 static void process( QTabWidget
*tab
, KConfigGroup
&config
)
42 config
.writeEntry( tab
->objectName(), tab
->currentIndex() );
45 static void process( QTreeView
*tv
, KConfigGroup
&config
)
47 config
.writeEntry( tv
->objectName(), tv
->header()->saveState() );
50 static void process( QComboBox
*cb
, KConfigGroup
&config
)
52 config
.writeEntry( cb
->objectName(), cb
->currentIndex() );
57 static void process( QSplitter
*splitter
, const KConfigGroup
&config
)
59 const QList
<int> sizes
= config
.readEntry( splitter
->objectName(), QList
<int>() );
60 if ( !sizes
.isEmpty() && splitter
->count() == sizes
.count() && sizes
.count() != sizes
.count( 0 ) )
61 splitter
->setSizes( sizes
);
64 static void process( QTabWidget
*tab
, const KConfigGroup
&config
)
66 const int index
= config
.readEntry( tab
->objectName(), -1 );
67 if ( index
>= 0 && index
< tab
->count() )
68 tab
->setCurrentIndex( index
);
71 static void process( QTreeView
*tv
, const KConfigGroup
&config
)
73 const QByteArray state
= config
.readEntry( tv
->objectName(), QByteArray() );
74 if ( !state
.isEmpty() )
75 tv
->header()->restoreState( state
);
78 static void process( QComboBox
*cb
, const KConfigGroup
&config
)
80 const int index
= config
.readEntry( cb
->objectName(), -1 );
81 if ( index
>= 0 && index
< cb
->count() )
82 cb
->setCurrentIndex( index
);
86 #define PROCESS_TYPE( T ) \
88 T *obj = qobject_cast<T*>( w ); \
90 Op::process( obj, config ); \
95 template <typename Op
, typename Config
> static void processWidgets( QWidget
*widget
, Config config
)
97 QList
<QWidget
*> widgets
= widget
->findChildren
<QWidget
*>();
99 foreach ( QWidget
* w
, widgets
) {
100 if ( w
->objectName().isEmpty() )
102 PROCESS_TYPE( QSplitter
);
103 PROCESS_TYPE( QTabWidget
);
104 PROCESS_TYPE( QTreeView
);
105 PROCESS_TYPE( QComboBox
);
111 void UiStateSaver::saveState(QWidget
* widget
, KConfigGroup
& config
)
113 processWidgets
<Saver
, KConfigGroup
&>( widget
, config
);
116 void UiStateSaver::restoreState(QWidget
* widget
, const KConfigGroup
& config
)
118 processWidgets
<Restorer
, const KConfigGroup
&>( widget
, config
);