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>
26
#include <QHeaderView>
34
static void process( QSplitter *splitter, KConfigGroup &config )
36
if ( splitter->sizes().count( 0 ) == splitter->sizes().count() )
38
config.writeEntry( splitter->objectName(), splitter->sizes() );
41
static void process( QTabWidget *tab, KConfigGroup &config )
43
config.writeEntry( tab->objectName(), tab->currentIndex() );
46
static void process( QTreeView *tv, KConfigGroup &config )
48
config.writeEntry( tv->objectName(), tv->header()->saveState() );
51
static void process( QComboBox *cb, KConfigGroup &config )
53
config.writeEntry( cb->objectName(), cb->currentIndex() );
58
static void process( QSplitter *splitter, const KConfigGroup &config )
60
const QList<int> sizes = config.readEntry( splitter->objectName(), QList<int>() );
61
if ( !sizes.isEmpty() && splitter->count() == sizes.count() && sizes.count() != sizes.count( 0 ) )
62
splitter->setSizes( sizes );
65
static void process( QTabWidget *tab, const KConfigGroup &config )
67
const int index = config.readEntry( tab->objectName(), -1 );
68
if ( index >= 0 && index < tab->count() )
69
tab->setCurrentIndex( index );
72
static void process( QTreeView *tv, const KConfigGroup &config )
74
const QByteArray state = config.readEntry( tv->objectName(), QByteArray() );
75
if ( !state.isEmpty() )
76
tv->header()->restoreState( state );
79
static void process( QComboBox *cb, const KConfigGroup &config )
81
const int index = config.readEntry( cb->objectName(), -1 );
82
if ( index >= 0 && index < cb->count() )
83
cb->setCurrentIndex( index );
87
#define PROCESS_TYPE( T ) \
89
T *obj = qobject_cast<T*>( w ); \
91
Op::process( obj, config ); \
96
template <typename Op, typename Config> static void processWidgets( QWidget *widget, Config config )
98
QList<QWidget*> widgets = widget->findChildren<QWidget*>();
100
foreach ( QWidget* w, widgets ) {
101
if ( w->objectName().isEmpty() )
103
PROCESS_TYPE( QSplitter );
104
PROCESS_TYPE( QTabWidget );
105
PROCESS_TYPE( QTreeView );
106
PROCESS_TYPE( QComboBox );
112
void UiStateSaver::saveState(QWidget * widget, KConfigGroup & config)
114
processWidgets<Saver, KConfigGroup&>( widget, config );
117
void UiStateSaver::restoreState(QWidget * widget, const KConfigGroup & config)
119
processWidgets<Restorer, const KConfigGroup&>( widget, config );