From 4a251647b83240fdba1743fde2eb175f331135db Mon Sep 17 00:00:00 2001 From: kas Date: Wed, 29 Mar 2023 11:06:31 +0200 Subject: [PATCH] Fix repo tab name deduplication only working for 2 tabs at most --- src/ui/MainWindow.cpp | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 4e96d060..97b6c09d 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -21,6 +21,7 @@ #include "git/Repository.h" #include "git/Config.h" #include "git/Submodule.h" +#include "qmap.h" #include #include #include @@ -54,6 +55,8 @@ public: QString name() const { return mPath.section('/', -mSections); } void increment() { ++mSections; } + int sections() const { return mSections; } + void setSections(int sections) { mSections = sections; } private: QString mPath; @@ -462,25 +465,31 @@ void MainWindow::warnInvalidRepo(const QString &path) { } void MainWindow::updateTabNames() { - QList names; + TabWidget *tabs = tabWidget(); + QHash> names; + QList fullNames; + for (int i = 0; i < count(); ++i) { TabName name(view(i)->repo().workdir().path()); - auto functor = [&name](const TabName &rhs) { - return (name.name() == rhs.name()); - }; - - QList::iterator it, end = names.end(); - while ((it = std::find_if(names.begin(), end, functor)) != end) { - it->increment(); - name.increment(); - } - - names.append(name); + names[name.name()].append(i); + fullNames.append(name); } - TabWidget *tabs = tabWidget(); - for (int i = 0; i < count(); ++i) - tabs->setTabText(i, names.at(i).name()); + QHash>::key_iterator first; + while ((first = names.keyBegin()) != names.keyEnd()) { + auto key = *first; + auto ids = names.take(key); + + if (ids.count() == 1) { + tabs->setTabText(ids.first(), key); + } else { + for (auto id : ids) { + auto &name = fullNames[id]; + name.increment(); + names[name.name()].append(id); + } + } + } } void MainWindow::updateInterface() {