Commit 6db5a71a authored by loh.tar's avatar loh.tar
Browse files

Remember expand/collapse state of tree nodes

... in GitWidget between updates

Don't track user action, just remember state before update

Use QMap instead of QVector

Fix auto added bad header include

Loop exact over rowcount, avoid valid check

Add again isValid check as requested

Squashed commits
utilities/kate/-/merge_requests/725
parent 7b249cfe
Pipeline #175761 passed with stage
in 12 minutes
......@@ -30,6 +30,7 @@
#include <QHeaderView>
#include <QInputMethodEvent>
#include <QLineEdit>
#include <QMap>
#include <QMenu>
#include <QPainter>
#include <QPlainTextEdit>
......@@ -296,6 +297,7 @@ GitWidget::GitWidget(KateProject *project, KTextEditor::MainWindow *mainWindow,
m_treeView->installEventFilter(this);
m_treeView->setRootIsDecorated(false);
m_treeView->setAllColumnsShowFocus(true);
m_treeView->expandAll();
if (m_treeView->style()) {
auto indent = m_treeView->style()->pixelMetric(QStyle::PM_TreeViewIndentation, nullptr, m_treeView);
......@@ -773,35 +775,45 @@ void GitWidget::treeViewDoubleClicked(const QModelIndex &idx)
handleClick(idx, m_pluginView->plugin()->doubleClickAcion());
}
void GitWidget::hideEmptyTreeNodes()
void GitWidget::parseStatusReady()
{
auto expand = [this](GitStatusModel::ItemType t) {
auto *model = m_treeView->model();
auto index = model->index(t, 0);
if (!index.isValid() || index.data(GitStatusModel::TreeItemType).toInt() == GitStatusModel::NodeUntrack) {
return;
// Remember collapse/expand state
// The default is expanded, so only add here which should be not expanded
QMap<int, bool> nodeIsExpanded;
nodeIsExpanded.insert(GitStatusModel::NodeUntrack, false);
const auto *model = m_treeView->model();
for (int i = 0; i < model->rowCount(); ++i) {
const auto index = model->index(i, 0);
if (!index.isValid()) {
continue;
}
const auto t = index.data(GitStatusModel::TreeItemType).toInt();
nodeIsExpanded[t] = m_treeView->isExpanded(index);
}
// Set new data
GitUtils::GitParsedStatus s = m_gitStatusWatcher.result();
m_model->setStatusItems(std::move(s), m_pluginView->plugin()->showGitStatusWithNumStat());
// Restore collapse/expand state
for (int i = 0; i < model->rowCount(); ++i) {
const auto index = model->index(i, 0);
if (!index.isValid()) {
continue;
}
if (model->rowCount(index) > 0 && !m_treeView->isExpanded(index)) {
const auto t = index.data(GitStatusModel::TreeItemType).toInt();
if (!nodeIsExpanded.contains(t) || nodeIsExpanded[t]) {
m_treeView->expand(index);
} else {
m_treeView->collapse(index);
}
};
expand(GitStatusModel::NodeStage);
expand(GitStatusModel::NodeChanges);
expand(GitStatusModel::NodeConflict);
}
m_treeView->resizeColumnToContents(0);
m_treeView->resizeColumnToContents(1);
}
void GitWidget::parseStatusReady()
{
GitUtils::GitParsedStatus s = m_gitStatusWatcher.result();
m_model->setStatusItems(std::move(s), m_pluginView->plugin()->showGitStatusWithNumStat());
hideEmptyTreeNodes();
}
void GitWidget::branchCompareFiles(const QString &from, const QString &to)
{
if (from.isEmpty() && to.isEmpty()) {
......
......@@ -75,9 +75,9 @@ private:
QToolButton *m_pushBtn;
QToolButton *m_pullBtn;
QToolButton *m_cancelBtn;
KateProject *m_project;
GitWidgetTreeView *m_treeView;
GitStatusModel *m_model;
KateProject *m_project;
QLineEdit *m_filterLineEdit;
/** This ends with "/", always remember this */
QString m_gitPath;
......@@ -113,7 +113,6 @@ private:
QMenu *stashMenu(KActionCollection *pCollection);
QAction *stashMenuAction(KActionCollection *ac, const QString &name, const QString &text, StashMode m);
void hideEmptyTreeNodes();
void treeViewContextMenuEvent(QContextMenuEvent *e);
void selectedContextMenu(QContextMenuEvent *e);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment