20 #include "entitytreeviewstatesaver.h"
22 #include <akonadi/collection.h>
23 #include <akonadi/entitytreemodel.h>
24 #include <akonadi/item.h>
26 #include <KConfigGroup>
48 class EntityTreeViewStateSaverPrivate
51 explicit EntityTreeViewStateSaverPrivate(EntityTreeViewStateSaver *parent)
54 , horizontalScrollBarValue(-1)
55 , verticalScrollBarValue(-1)
59 inline bool hasChanges()
const
61 return !pendingCollectionChanges.isEmpty() || !pendingItemChanges.isEmpty();
78 const QString cfgKey = key(index);
79 if (view->selectionModel()->isSelected(index)) {
82 if (view->isExpanded(index)) {
85 for (
int i = 0; i < view->model()->rowCount(index); ++i) {
87 saveState(child, selection, expansion);
91 inline void restoreState(
const QModelIndex &index,
const State &state)
94 view->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
97 view->setExpanded(index,
true);
99 if (state.currentIndex) {
100 view->setCurrentIndex(index);
109 if (pendingCollectionChanges.contains(c.id())) {
110 restoreState(index, pendingCollectionChanges.value(c.id()));
111 pendingCollectionChanges.remove(c.id());
115 if (pendingItemChanges.contains(itemId)) {
116 restoreState(index, pendingItemChanges.value(itemId));
117 pendingItemChanges.remove(itemId);
120 for (
int i = 0; i < view->model()->rowCount(index) && hasChanges(); ++i) {
126 inline void restoreScrollBarState()
128 if (horizontalScrollBarValue >= 0 && horizontalScrollBarValue <= view->horizontalScrollBar()->maximum()) {
129 view->horizontalScrollBar()->setValue(horizontalScrollBarValue);
130 horizontalScrollBarValue = -1;
132 if (verticalScrollBarValue >= 0 && verticalScrollBarValue <= view->verticalScrollBar()->maximum()) {
133 view->verticalScrollBar()->setValue(verticalScrollBarValue);
134 verticalScrollBarValue = -1;
138 void rowsInserted(
const QModelIndex &index,
int start,
int end)
146 for (
int i = start; i <= end && hasChanges(); ++i) {
152 EntityTreeViewStateSaver *q;
155 int horizontalScrollBarValue, verticalScrollBarValue;
160 , d(new EntityTreeViewStateSaverPrivate(this))
172 if (!d->view->model()) {
176 configGroup.deleteGroup();
178 const int rowCount = d->view->model()->rowCount();
179 for (
int i = 0; i < rowCount; ++i) {
181 d->saveState(index, selection, expansion);
184 const QString currentIndex = d->key(d->view->selectionModel()->currentIndex());
186 configGroup.writeEntry(
"Selection", selection);
187 configGroup.writeEntry(
"Expansion", expansion);
188 configGroup.writeEntry(
"CurrentIndex", currentIndex);
189 configGroup.writeEntry(
"ScrollBarHorizontal", d->view->horizontalScrollBar()->value());
190 configGroup.writeEntry(
"ScrollBarVertical", d->view->verticalScrollBar()->value());
195 if (!d->view->model()) {
200 foreach (
const QString &key, selection) {
206 d->pendingCollectionChanges[id].selected =
true;
208 d->pendingItemChanges[id].selected =
true;
213 foreach (
const QString &key, expansion) {
219 d->pendingCollectionChanges[id].expanded =
true;
221 d->pendingItemChanges[id].expanded =
true;
225 const QString key = configGroup.readEntry(
"CurrentIndex",
QString());
229 d->pendingCollectionChanges[id].currentIndex =
true;
231 d->pendingItemChanges[id].currentIndex =
true;
235 d->horizontalScrollBarValue = configGroup.readEntry(
"ScrollBarHorizontal", -1);
236 d->verticalScrollBarValue = configGroup.readEntry(
"ScrollBarVertical", -1);
239 for (
int i = 0; i < d->view->model()->rowCount() && d->hasChanges(); ++i) {
241 d->restoreState(index);
243 d->restoreScrollBarState();
246 if (d->hasChanges()) {
254 #include "moc_entitytreeviewstatesaver.cpp"
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
~EntityTreeViewStateSaver()
Destroys this state saver.
qint64 Id
Describes the unique id type.
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void append(const T &value)
EntityTreeViewStateSaver(QTreeView *view)
Creates a new state saver, for saving or restoring.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void restoreState(const KConfigGroup &configGroup) const
Restore the state stored in configGroup as soon as the corresponding entities become available in the...
const QAbstractItemModel * model() const
QString mid(int position, int n) const
QVariant data(int role) const
QString fromLatin1(const char *str, int size)
void saveState(KConfigGroup &configGroup) const
Stores the current state in the given config group.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
qlonglong toLongLong(bool *ok, int base) const