1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

LP-567 store/restore tree expansion state

This commit is contained in:
Philippe Renon 2018-04-07 14:55:30 +02:00
parent 077d1e89d1
commit 6e8f338034
4 changed files with 88 additions and 0 deletions

View File

@ -55,6 +55,17 @@ void UAVObjectBrowser::loadConfiguration(IUAVGadgetConfiguration *config)
m_widget->setSplitterState(m->splitterState());
}
void UAVObjectBrowser::saveState(QSettings &settings) const
{
m_widget->saveState(settings);
}
void UAVObjectBrowser::restoreState(QSettings &settings)
{
m_widget->restoreState(settings);
}
void UAVObjectBrowser::viewOptionsChangedSlot(bool categorized, bool scientific, bool metadata, bool description)
{
if (m_config) {

View File

@ -51,6 +51,9 @@ public:
}
void loadConfiguration(IUAVGadgetConfiguration *config);
void saveState(QSettings &settings) const;
void restoreState(QSettings &settings);
private slots:
void viewOptionsChangedSlot(bool categorized, bool scientific, bool metadata, bool description);
void splitterChanged(QByteArray state);

View File

@ -422,6 +422,68 @@ void UAVObjectBrowserWidget::searchLineChanged(QString searchText)
}
}
QString UAVObjectBrowserWidget::indexToPath(const QModelIndex &index) const
{
QString path = index.data(Qt::DisplayRole).toString();
QModelIndex parent = index.parent();
while (parent.isValid()) {
path = parent.data(Qt::DisplayRole).toString() + "/" + path;
parent = parent.parent();
}
return path;
}
QModelIndex UAVObjectBrowserWidget::indexFromPath(const QString &path) const
{
QStringList list = path.split("/");
QModelIndex index = m_modelProxy->index(0, 0);
foreach(QString name, list) {
QModelIndexList items = m_modelProxy->match(index, Qt::DisplayRole, name, 1, Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
if (!items.isEmpty()) {
index = items.first();
} else {
// bail out
return QModelIndex();
}
}
return index;
}
void UAVObjectBrowserWidget::saveState(QSettings &settings) const
{
QStringList list;
// prepare list
foreach(QModelIndex index, m_modelProxy->getPersistentIndexList()) {
if (m_browser->treeView->isExpanded(index)) {
QString path = indexToPath(index);
list << path;
}
}
// save list
settings.setValue("expandedItems", QVariant::fromValue(list));
}
void UAVObjectBrowserWidget::restoreState(QSettings &settings)
{
// get list
QStringList list = settings.value("expandedItems").toStringList();
foreach(QString path, list) {
QModelIndex index = indexFromPath(path);
if (index.isValid()) {
m_browser->treeView->setExpanded(index, true);
}
}
}
void UAVObjectBrowserWidget::searchTextCleared()
{
m_browser->searchLine->clear();

View File

@ -46,6 +46,12 @@ class TreeSortFilterProxyModel : public QSortFilterProxyModel {
public:
TreeSortFilterProxyModel(QObject *parent);
public:
QModelIndexList getPersistentIndexList() const
{
return persistentIndexList();
}
protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
bool filterAcceptsRowItself(int source_row, const QModelIndex &source_parent) const;
@ -87,6 +93,12 @@ public:
void setViewOptions(bool showCategories, bool showMetadata, bool useScientificNotation, bool showDescription);
void setSplitterState(QByteArray state);
void saveState(QSettings &settings) const;
void restoreState(QSettings &settings);
QString indexToPath(const QModelIndex &index) const;
QModelIndex indexFromPath(const QString &path) const;
public slots:
void showDescription(bool show);