24 #include <QtGui/QAbstractScrollArea> 39 class KViewStateSaverPrivate
48 m_horizontalScrollBarValue(-1),
49 m_verticalScrollBarValue(-1)
57 QStringList getExpandedItems(
const QModelIndex &index)
const;
59 void listenToPendingChanges();
60 void processPendingChanges();
62 inline void restoreScrollBarState()
67 if ( m_horizontalScrollBarValue >= 0 && m_horizontalScrollBarValue <= m_scrollArea->horizontalScrollBar()->maximum() ) {
68 m_scrollArea->horizontalScrollBar()->setValue( m_horizontalScrollBarValue );
69 m_horizontalScrollBarValue = -1;
71 if ( m_verticalScrollBarValue >= 0 && m_verticalScrollBarValue <= m_scrollArea->verticalScrollBar()->maximum() ) {
72 m_scrollArea->verticalScrollBar()->setValue( m_verticalScrollBarValue );
73 m_verticalScrollBarValue = -1;
77 void restoreSelection();
78 void restoreCurrentItem();
79 void restoreExpanded();
81 inline bool hasPendingChanges()
const 83 return !m_pendingCurrent.isEmpty() || !m_pendingExpansions.isEmpty() || !m_pendingSelections.isEmpty();
88 if ( m_selectionModel && m_selectionModel->model() )
89 return m_selectionModel->model();
90 else if ( m_view && m_view->model() )
91 return m_view->model();
95 void rowsInserted(
const QModelIndex &,
int ,
int )
98 processPendingChanges();
100 if ( !hasPendingChanges() )
102 q->disconnect( getModel(), SIGNAL(rowsInserted(QModelIndex,
int,
int)),
103 q, SLOT(rowsInserted(QModelIndex,
int,
int)) );
108 QTreeView *m_treeView;
109 QAbstractItemView *m_view;
111 QAbstractScrollArea *m_scrollArea;
113 int m_horizontalScrollBarValue;
114 int m_verticalScrollBarValue;
117 QString m_pendingCurrent;
121 :
QObject(0), d_ptr( new KViewStateSaverPrivate(this) )
124 qRegisterMetaType<QModelIndex>(
"QModelIndex" );
135 d->m_scrollArea =
view;
137 d->m_selectionModel = view->selectionModel();
138 d->m_treeView = qobject_cast<QTreeView*>(
view);
140 d->m_selectionModel = 0;
155 return d->m_selectionModel;
164 void KViewStateSaverPrivate::listenToPendingChanges()
168 if ( hasPendingChanges() )
173 q->disconnect( model, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
174 q, SLOT(rowsInserted(QModelIndex,
int,
int)) );
175 q->connect( model, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
176 SLOT(rowsInserted(QModelIndex,
int,
int)) );
186 void KViewStateSaverPrivate::processPendingChanges()
190 q->restoreCurrentItem(m_pendingCurrent);
191 q->restoreSelection(m_pendingSelections.toList());
192 q->restoreExpanded(m_pendingExpansions.toList());
193 q->restoreScrollState(m_verticalScrollBarValue, m_horizontalScrollBarValue);
201 QTimer::singleShot(60000,
this, SLOT(deleteLater()));
210 d->processPendingChanges();
211 if (d->hasPendingChanges())
212 d->listenToPendingChanges();
215 QStringList KViewStateSaverPrivate::getExpandedItems(
const QModelIndex &index)
const 219 QStringList expansion;
220 for (
int i = 0; i < m_treeView->model()->rowCount( index ); ++i ) {
221 const QModelIndex child = m_treeView->model()->index( i, 0, index );
224 if ( m_treeView->model()->hasChildren( child ) ) {
225 if ( m_treeView->isExpanded( child ) )
226 expansion << q->indexToConfigString( child );
227 expansion << getExpandedItems( child );
237 if ( d->m_selectionModel )
250 if ( d->m_scrollArea )
258 void KViewStateSaverPrivate::restoreCurrentItem()
262 QModelIndex currentIndex = q->indexFromConfigString(m_selectionModel->model(), m_pendingCurrent);
263 if ( currentIndex.isValid() )
266 m_treeView->setCurrentIndex(currentIndex);
268 m_selectionModel->setCurrentIndex(currentIndex, QItemSelectionModel::NoUpdate);
269 m_pendingCurrent.clear();
276 if (!d->m_selectionModel || !d->m_selectionModel->model())
279 if (indexString.isEmpty())
283 d->m_pendingCurrent = indexString;
284 d->restoreCurrentItem();
286 if (d->hasPendingChanges())
287 d->listenToPendingChanges();
290 void KViewStateSaverPrivate::restoreExpanded()
295 for ( ; it != m_pendingExpansions.end(); )
297 QModelIndex idx = q->indexFromConfigString( m_treeView->model(), *it);
300 m_treeView->expand( idx );
301 it = m_pendingExpansions.erase( it );
311 if (!d->m_treeView || !d->m_treeView->model())
314 if (indexStrings.isEmpty())
317 d->m_pendingExpansions.unite(indexStrings.toSet());
318 d->restoreExpanded();
319 if (d->hasPendingChanges())
320 d->listenToPendingChanges();
327 if ( !d->m_scrollArea )
330 d->m_verticalScrollBarValue = verticalScoll;
331 d->m_horizontalScrollBarValue = horizontalScroll;
333 QTimer::singleShot( 0,
this, SLOT(restoreScrollBarState()) );
336 void KViewStateSaverPrivate::restoreSelection()
341 for ( ; it != m_pendingSelections.end(); )
343 QModelIndex idx = q->indexFromConfigString( m_selectionModel->model(), *it);
346 m_selectionModel->select( idx, QItemSelectionModel::Select );
347 it = m_pendingSelections.erase( it );
358 if (!d->m_selectionModel || !d->m_selectionModel->model())
361 if (indexStrings.isEmpty())
364 d->m_pendingSelections.unite(indexStrings.toSet());
365 d->restoreSelection();
366 if (d->hasPendingChanges())
367 d->listenToPendingChanges();
373 if (!d->m_selectionModel)
381 if (!d->m_treeView || !d->m_treeView->model())
382 return QStringList();
384 return d->getExpandedItems(QModelIndex());
390 if (!d->m_selectionModel)
391 return QStringList();
393 QModelIndexList selectedIndexes = d->m_selectionModel->selectedRows();
394 QStringList selection;
395 foreach (
const QModelIndex &index, selectedIndexes )
404 return qMakePair(d->m_scrollArea->verticalScrollBar()->value(), d->m_scrollArea->horizontalScrollBar()->value());
407 #include "kviewstatesaver.moc"
QItemSelectionModel * selectionModel() const
The QItemSelectionModel whose state is persisted.
QString currentIndexKey() const
Returns a QString describing the current index in the selection model.
static const char * currentKey
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
void restoreCurrentItem(const QString &indexString)
Make the index described by indexString the currentIndex in the selectionModel.
QStringList selectionKeys() const
Returns a QStringList describing the selection in the selectionModel.
KViewStateSaver(QObject *parent=0)
Constructor.
static const char * scrollStateHorizontalKey
~KViewStateSaver()
Destructor.
void restoreScrollState(int verticalScoll, int horizontalScroll)
Restores the scroll state of the QAbstractScrollArea to the verticalScoll and horizontalScroll.
void saveState(KConfigGroup &configGroup)
Saves the state to the configGroup.
void setView(QAbstractItemView *view)
Sets the view whose state is persisted.
QStringList expansionKeys() const
Returns a QStringList representing the expanded indexes in the QTreeView.
static const char * expansionKey
QPair< int, int > scrollState() const
Returns the vertical and horizontal scroll of the QAbstractScrollArea.
void restoreExpanded(const QStringList &indexStrings)
Expand the indexes described by indexStrings in the QTreeView.
static const char * scrollStateVerticalKey
virtual QString indexToConfigString(const QModelIndex &index) const =0
Reimplement to return a unique string for the index.
void setSelectionModel(QItemSelectionModel *selectionModel)
Sets the QItemSelectionModel whose state is persisted.
void restoreSelection(const QStringList &indexStrings)
Select the indexes described by indexStrings.
QAbstractItemView * view() const
The view whose state is persisted.
void restoreState(const KConfigGroup &configGroup)
Restores the state from the configGroup.
static const char * selectionKey
T readEntry(const QString &key, const T &aDefault) const
Object for saving and restoring state in QTreeViews and QItemSelectionModels.