remember collapsed nodes

This commit is contained in:
Jussi Kuokkanen 2023-11-24 19:39:14 +02:00
parent b922186489
commit 6c17201595
5 changed files with 54 additions and 5 deletions

View File

@ -53,6 +53,7 @@ DeviceModel::DeviceModel(TC::TreeNode<TCDBus::DeviceNode> root, QObject *parent)
QList<QStandardItem *> rowItems;
auto nameItem = new QStandardItem;
nameItem->setText(nodeName);
nameItem->setData(node.value().path, NodePathRole);
rowItems.append(nameItem);
p::match(node.value().interface)(

View File

@ -38,7 +38,8 @@ public:
ConnectionRole, // Data about the connection
DynamicReadableProxyRole,
InterfaceTypeRole, // InterfaceType
NodeNameRole //
NodeNameRole,
NodePathRole // DBus path
};
enum InterfaceFlag {

View File

@ -27,10 +27,6 @@ DeviceBrowser::DeviceBrowser(DeviceModel &model, QWidget *parent)
m_treeView = new DeviceTreeView;
m_treeView->setModel(m_proxyModel);
// Expand all nodes
// TODO: remember collapsed nodes
m_treeView->expandAll();
m_flagLabel = new QLabel(_("Showing:"));
m_apply = new QPushButton(_("Apply changes"));
m_apply->setEnabled(true);

View File

@ -8,6 +8,7 @@
#include <QCheckBox>
#include <QDebug>
#include <QHeaderView>
#include <QSettings>
#include <Utils.hpp>
using namespace mpark::patterns;
@ -27,11 +28,16 @@ DeviceTreeView::DeviceTreeView(QWidget *parent) : QTreeView(parent) {
connect(this, &QTreeView::collapsed, [=](auto &index) {
// Traverse model, recursively suspend everything
suspendChildren(index);
// TODO: only save on exit
// Difficulty has been recognizing 'exit' when a bunch of stuff isn't NULL
saveCollapsed(model());
});
connect(this, &QTreeView::expanded, [=](auto &index) {
// Traverse model, recursively resuming expanded nodes
resumeChildren(index);
// TODO: only save on exit
saveCollapsed(model());
});
// Semi-hack: don't try to copy assignable settings from indices when loading profile
@ -60,6 +66,10 @@ DeviceTreeView::DeviceTreeView(QWidget *parent) : QTreeView(parent) {
}
void DeviceTreeView::suspendChildren(const QModelIndex &index) {
// Somehow can be true when we collapse nodes from cache
if (!Globals::g_deviceModel)
return;
// TODO: readables aren't suspended when hidden and a connection using it stops
QVector<QString> connectedReadablePaths;
for (auto &conn : Globals::g_deviceModel->activeConnections())
@ -164,3 +174,41 @@ void DeviceTreeView::dataChanged(
}
QTreeView::dataChanged(topLeft, bottomRight, roles);
}
void DeviceTreeView::saveCollapsed(QAbstractItemModel *model) {
QStringList collapsed;
auto cb = [&](auto *model, auto &index, int row) -> std::optional<QModelIndex> {
if (!isExpanded(index)) {
auto pathV = index.data(DeviceModel::NodePathRole);
if (pathV.isValid())
collapsed.append(pathV.toString());
}
return model->index(row, DeviceModel::NameColumn, index);
};
Utils::traverseModel(cb, model, QModelIndex{});
QSettings cache{Utils::cacheFilePath(), QSettings::NativeFormat};
cache.setValue("collapsedNodes", collapsed);
}
void DeviceTreeView::restoreCollapsed(QAbstractItemModel *model) {
QSettings cache{Utils::cacheFilePath(), QSettings::NativeFormat};
auto collapsed = qvariant_cast<QStringList>(cache.value("collapsedNodes"));
expandAll();
auto cb = [=](auto *model, auto &index, int row) -> std::optional<QModelIndex> {
auto pathV = index.data(DeviceModel::NodePathRole);
if (pathV.isValid() && collapsed.contains(pathV.toString())) {
this->collapse(index);
}
return model->index(row, DeviceModel::NameColumn, index);
};
Utils::traverseModel(cb, model, QModelIndex{});
}
void DeviceTreeView::setModel(QAbstractItemModel *model) {
QTreeView::setModel(model);
restoreCollapsed(model);
}

View File

@ -14,6 +14,7 @@
class DeviceTreeView : public QTreeView {
public:
DeviceTreeView(QWidget *parent = nullptr);
void setModel(QAbstractItemModel *) override;
// Accessor method for connecting everything in the browser
// const DeviceModel &deviceModel() {return m_deviceModel;}
// TODO: make this more generalized
@ -39,6 +40,8 @@ protected:
EditTriggers editTriggers() { return QAbstractItemView::AllEditTriggers; }
void dataChanged(const QModelIndex &, const QModelIndex &, const QVector<int> &) override;
private:
void saveCollapsed(QAbstractItemModel *model);
void restoreCollapsed(QAbstractItemModel *model);
// Suspend/resume readable updates
void suspendChildren(const QModelIndex &);
void resumeChildren(const QModelIndex &);