Skip to content

Commit 6e8f338

Browse files
committed
LP-567 store/restore tree expansion state
1 parent 077d1e8 commit 6e8f338

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

ground/gcs/src/plugins/uavobjectbrowser/uavobjectbrowser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ void UAVObjectBrowser::loadConfiguration(IUAVGadgetConfiguration *config)
5555
m_widget->setSplitterState(m->splitterState());
5656
}
5757

58+
void UAVObjectBrowser::saveState(QSettings &settings) const
59+
{
60+
m_widget->saveState(settings);
61+
}
62+
63+
64+
void UAVObjectBrowser::restoreState(QSettings &settings)
65+
{
66+
m_widget->restoreState(settings);
67+
}
68+
5869
void UAVObjectBrowser::viewOptionsChangedSlot(bool categorized, bool scientific, bool metadata, bool description)
5970
{
6071
if (m_config) {

ground/gcs/src/plugins/uavobjectbrowser/uavobjectbrowser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class UAVObjectBrowser : public Core::IUAVGadget {
5151
}
5252
void loadConfiguration(IUAVGadgetConfiguration *config);
5353

54+
void saveState(QSettings &settings) const;
55+
void restoreState(QSettings &settings);
56+
5457
private slots:
5558
void viewOptionsChangedSlot(bool categorized, bool scientific, bool metadata, bool description);
5659
void splitterChanged(QByteArray state);

ground/gcs/src/plugins/uavobjectbrowser/uavobjectbrowserwidget.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,68 @@ void UAVObjectBrowserWidget::searchLineChanged(QString searchText)
422422
}
423423
}
424424

425+
QString UAVObjectBrowserWidget::indexToPath(const QModelIndex &index) const
426+
{
427+
QString path = index.data(Qt::DisplayRole).toString();
428+
429+
QModelIndex parent = index.parent();
430+
431+
while (parent.isValid()) {
432+
path = parent.data(Qt::DisplayRole).toString() + "/" + path;
433+
parent = parent.parent();
434+
}
435+
return path;
436+
}
437+
438+
QModelIndex UAVObjectBrowserWidget::indexFromPath(const QString &path) const
439+
{
440+
QStringList list = path.split("/");
441+
442+
QModelIndex index = m_modelProxy->index(0, 0);
443+
444+
foreach(QString name, list) {
445+
QModelIndexList items = m_modelProxy->match(index, Qt::DisplayRole, name, 1, Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
446+
447+
if (!items.isEmpty()) {
448+
index = items.first();
449+
} else {
450+
// bail out
451+
return QModelIndex();
452+
}
453+
}
454+
return index;
455+
}
456+
457+
void UAVObjectBrowserWidget::saveState(QSettings &settings) const
458+
{
459+
QStringList list;
460+
461+
// prepare list
462+
foreach(QModelIndex index, m_modelProxy->getPersistentIndexList()) {
463+
if (m_browser->treeView->isExpanded(index)) {
464+
QString path = indexToPath(index);
465+
list << path;
466+
}
467+
}
468+
469+
// save list
470+
settings.setValue("expandedItems", QVariant::fromValue(list));
471+
}
472+
473+
void UAVObjectBrowserWidget::restoreState(QSettings &settings)
474+
{
475+
// get list
476+
QStringList list = settings.value("expandedItems").toStringList();
477+
478+
foreach(QString path, list) {
479+
QModelIndex index = indexFromPath(path);
480+
481+
if (index.isValid()) {
482+
m_browser->treeView->setExpanded(index, true);
483+
}
484+
}
485+
}
486+
425487
void UAVObjectBrowserWidget::searchTextCleared()
426488
{
427489
m_browser->searchLine->clear();

ground/gcs/src/plugins/uavobjectbrowser/uavobjectbrowserwidget.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class TreeSortFilterProxyModel : public QSortFilterProxyModel {
4646
public:
4747
TreeSortFilterProxyModel(QObject *parent);
4848

49+
public:
50+
QModelIndexList getPersistentIndexList() const
51+
{
52+
return persistentIndexList();
53+
}
54+
4955
protected:
5056
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
5157
bool filterAcceptsRowItself(int source_row, const QModelIndex &source_parent) const;
@@ -87,6 +93,12 @@ class UAVObjectBrowserWidget : public QWidget {
8793
void setViewOptions(bool showCategories, bool showMetadata, bool useScientificNotation, bool showDescription);
8894
void setSplitterState(QByteArray state);
8995

96+
void saveState(QSettings &settings) const;
97+
void restoreState(QSettings &settings);
98+
99+
QString indexToPath(const QModelIndex &index) const;
100+
QModelIndex indexFromPath(const QString &path) const;
101+
90102
public slots:
91103
void showDescription(bool show);
92104

0 commit comments

Comments
 (0)