KDE 4.7 PyKDE API Reference

- KDE's Python API

# KViewStateSaver Class Reference

`from PyKDE4.kdeui import *`

Inherits: QObject

## Detailed Description

Object for saving and restoring state in QTreeViews and QItemSelectionModels

Implement the indexFromConfigString and indexToConfigString methods to handle the model in the view whose state is being saved. These implementations can be quite trivial:

    QModelIndex DynamicTreeStateSaver.indexFromConfigString(const QAbstractItemModel* model, const QString& key) const
    {
    QModelIndexList list = model->match(model->index(0, 0), DynamicTreeModel.DynamicTreeModelId, key.toInt(), 1, Qt.MatchRecursive);
    if (list.isEmpty())
    return QModelIndex();
    return list.first();
    }

    QString DynamicTreeStateSaver.indexToConfigString(const QModelIndex& index) const
    {
    return index.data(DynamicTreeModel.DynamicTreeModelId).toString();
    }

It is possible to restore the state of a QTreeView (that is, the expanded state and selected state of all indexes as well as the horizontal and vertical scroll state) by using setTreeView.

If there is no tree view state to restore (for example if using QML), the selection state of a QItemSelectionModel can be saved or restored instead.

The state of any QAbstractScrollArea can also be saved and restored.

A KViewStateSaver should be created on the stack when saving and on the heap when restoring. The model may be populated dynamically between several event loops, so it may not be immediate for the indexes that should be selected to be in the model. The saver should *not* be persisted as a member. The saver will destroy itself when it has completed the restoration specified in the config group, or a small amount of time has elapsed.

    MyWidget.MyWidget(Qobject *parent)
    : QWidget(parent)
    {
    ...

    m_view = new QTreeView(splitter);
    m_view->setModel(model);

    connect( model, SIGNAL(modelAboutToBeReset()), SLOT(saveState()) );
    connect( model, SIGNAL(modelReset()), SLOT(restoreState()) );
    connect( qApp, SIGNAL(aboutToQuit()), SLOT(saveState()) );

    restoreState();
    }

    void StateSaverWidget.saveState()
    {
    ConcreteStateSaver saver;
    saver.setTreeView(m_view);

    KConfigGroup cfg( KGlobal.config(), "ExampleViewState" );
    saver.saveState( cfg );
    cfg.sync();
    }

    void StateSaverWidget.restoreState()
    {
    // Will delete itself.
    ConcreteTreeStateSaver *saver = new ConcreteStateSaver();
    saver->setTreeView(m_view);
    KConfigGroup cfg( KGlobal.config(), "ExampleViewState" );
    saver->restoreState( cfg );
    }

After creating a saver, the state can be saved using a KConfigGroup.

It is also possible to save and restore state directly by using the restoreSelection, restoreExpanded etc methods. Note that the implementation of these methods should return strings that the indexFromConfigString implementation can handle.

    class DynamicTreeStateSaver : public KViewStateSaver
    {
    Q_OBJECT
    public:
    // ...

    void selectItems(const QList<qint64> &items)
    {
    QStringList itemStrings;
    foreach(qint64 item, items)
    itemStrings << QString.number(item);
    restoreSelection(itemStrings);
    }

    void expandItems(const QList<qint64> &items)
    {
    QStringList itemStrings;
    foreach(qint64 item, items)
    itemStrings << QString.number(item);
    restoreSelection(itemStrings);
    }

    };

Note that a single instance of this class should be used with only one widget. That is don't do this:

    saver->setTreeView(treeView1);
    saver->setSelectionModel(treeView2->selectionModel());
    saver->setScrollArea(treeView3);

To save the state of 3 different widgets, use three savers, even if they operate on the same root model.

    saver1->setTreeView(treeView1);
    saver2->setSelectionModel(treeView2->selectionModel());
    saver3->setScrollArea(treeView3);

Note:
  The KViewStateSaver does not take ownership of any widgets set on it.

It is recommended to restore the state on application startup and after the model has been reset, and to save the state on application close and before the model has been reset.

See also:
  QAbstractItemModel.modelAboutToBeReset QAbstractItemModel.modelReset

Author:
  Stephen Kelly <stephen@kdab.com>

Since:
  4.5

## Methods

| __init__ (self, QObject parent=0)
QString | currentIndexKey (self)
QStringList | expansionKeys (self)
QModelIndex | indexFromConfigString (self, QAbstractItemModel model, QString key)
QString | indexToConfigString (self, QModelIndex index)
| restoreCurrentItem (self, QString indexString)
| restoreExpanded (self, QStringList indexStrings)
| restoreScrollState (self, int verticalScoll, int horizontalScroll)
| restoreSelection (self, QStringList indexStrings)
| restoreState (self, KConfigGroup configGroup)
| saveState (self, KConfigGroup configGroup)
(int,int) | scrollState (self)
QStringList | selectionKeys (self)
QItemSelectionModel | selectionModel (self)
| setSelectionModel (self, QItemSelectionModel selectionModel)
| setView (self, QAbstractItemView view)
QAbstractItemView | view (self)

## Method Documentation

__init__ | ( | self, |
|  | QObject | parent=0
| ) |  |  |

Constructor

QString currentIndexKey | ( |  | self ) |

Returns a QString describing the current index in the selection model.

QStringList expansionKeys | ( |  | self ) |

Returns a QStringList representing the expanded indexes in the QTreeView.

QModelIndex indexFromConfigString | ( | self, |
|  | QAbstractItemModel | model,
|  | QString | key
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Reimplement to return an index in the model described by the unique key key

QString indexToConfigString | ( | self, |
|  | QModelIndex | index
| ) |  |  |

Abstract method:
  This method is abstract and can be overridden but not called directly.

Reimplement to return a unique string for the index.

restoreCurrentItem | ( | self, |
|  | QString | indexString
| ) |  |  |

Make the index described by indexString the currentIndex in the selectionModel.

restoreExpanded | ( | self, |
|  | QStringList | indexStrings
| ) |  |  |

Expand the indexes described by indexStrings in the QTreeView.

restoreScrollState | ( | self, |
|  | int | verticalScoll,
|  | int | horizontalScroll
| ) |  |  |

Restores the scroll state of the QAbstractScrollArea to the verticalScoll and horizontalScroll

restoreSelection | ( | self, |
|  | QStringList | indexStrings
| ) |  |  |

Select the indexes described by indexStrings

restoreState | ( | self, |
|  | KConfigGroup | configGroup
| ) |  |  |

Restores the state from the configGroup

saveState | ( | self, |
|  | KConfigGroup | configGroup
| ) |  |  |

Saves the state to the configGroup

(int,int) scrollState | ( |  | self ) |

Returns the vertical and horizontal scroll of the QAbstractScrollArea.

QStringList selectionKeys | ( |  | self ) |

Returns a QStringList describing the selection in the selectionModel.

QItemSelectionModel selectionModel | ( |  | self ) |

The QItemSelectionModel whose state is persisted.

setSelectionModel | ( | self, |
|  | QItemSelectionModel | selectionModel
| ) |  |  |

Sets the QItemSelectionModel whose state is persisted.

setView | ( | self, |
|  | QAbstractItemView | view
| ) |  |  |

Sets the view whose state is persisted.

QAbstractItemView view | ( |  | self ) |

The view whose state is persisted.