diff --git a/src/dolphintabbar.h b/src/dolphintabbar.h --- a/src/dolphintabbar.h +++ b/src/dolphintabbar.h @@ -33,6 +33,7 @@ void openNewActivatedTab(int index); void tabDropEvent(int index, QDropEvent* event); void tabDetachRequested(int index); + void tabRenamed(int index, const QString &name); protected: void dragEnterEvent(QDragEnterEvent* event) override; diff --git a/src/dolphintabbar.cpp b/src/dolphintabbar.cpp --- a/src/dolphintabbar.cpp +++ b/src/dolphintabbar.cpp @@ -25,6 +25,7 @@ #include #include #include +#include DolphinTabBar::DolphinTabBar(QWidget* parent) : QTabBar(parent), @@ -138,6 +139,7 @@ // Tab context menu QMenu menu(this); + QAction* renameTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename Tab")); QAction* newTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "New Tab")); QAction* detachTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-detach")), i18nc("@action:inmenu", "Detach Tab")); QAction* closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-close-other")), i18nc("@action:inmenu", "Close Other Tabs")); @@ -158,6 +160,14 @@ } } else if (selectedAction == closeTabAction) { emit tabCloseRequested(index); + } else if (selectedAction == renameTabAction) { + bool renamed = false; + const QString newTabName = QInputDialog::getText(this, + i18n("Rename Tab"), i18n("New tab name:"), QLineEdit::Normal, tabText(index), &renamed + ); + if (renamed) { + emit tabRenamed(index, newTabName); + } } return; diff --git a/src/dolphintabpage.h b/src/dolphintabpage.h --- a/src/dolphintabpage.h +++ b/src/dolphintabpage.h @@ -23,6 +23,7 @@ #include #include #include +#include class QSplitter; class DolphinViewContainer; @@ -134,6 +135,12 @@ * */ void setActive(bool active); + + /** + * Returns UUID of the tab. + * UUIDs are generated during construction. + */ + QUuid uuid() const; signals: void activeViewChanged(DolphinViewContainer* viewContainer); @@ -172,6 +179,7 @@ bool m_primaryViewActive; bool m_splitViewEnabled; bool m_active; + const QUuid m_uuid; }; #endif // DOLPHIN_TAB_PAGE_H diff --git a/src/dolphintabpage.cpp b/src/dolphintabpage.cpp --- a/src/dolphintabpage.cpp +++ b/src/dolphintabpage.cpp @@ -29,7 +29,8 @@ QWidget(parent), m_primaryViewActive(true), m_splitViewEnabled(false), - m_active(true) + m_active(true), + m_uuid(QUuid::createUuid()) { QVBoxLayout* layout = new QVBoxLayout(this); layout->setSpacing(0); @@ -379,3 +380,8 @@ return container; } + +QUuid DolphinTabPage::uuid() const +{ + return m_uuid; +} diff --git a/src/dolphintabwidget.h b/src/dolphintabwidget.h --- a/src/dolphintabwidget.h +++ b/src/dolphintabwidget.h @@ -22,6 +22,7 @@ #include #include +#include class DolphinViewContainer; class DolphinTabPage; @@ -71,7 +72,7 @@ void saveProperties(KConfigGroup& group) const; void readProperties(const KConfigGroup& group); - + /** * Refreshes the views of the main window by recreating them according to * the given Dolphin settings. @@ -172,7 +173,7 @@ * the recent tabs menu. */ void restoreClosedTab(const QByteArray& state); - + private slots: /** * Opens the tab with the index \a index in a new Dolphin instance and closes @@ -199,7 +200,13 @@ void tabUrlChanged(const QUrl& url); void currentTabChanged(int index); - + + /** + * Call to rename tab at index \a index to name \a name. + * Tab is stored in map as its uuid. + */ + void renameTab(int index, const QString& name); + protected: void tabInserted(int index) override; void tabRemoved(int index) override; @@ -216,6 +223,7 @@ bool m_placesSelectorVisible; int m_lastViewedTab; + QMap m_tabNames; }; #endif diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -48,6 +48,7 @@ this, &DolphinTabWidget::tabDropEvent); connect(tabBar, &DolphinTabBar::tabDetachRequested, this, &DolphinTabWidget::detachTab); + connect(tabBar, &DolphinTabBar::tabRenamed, this, &DolphinTabWidget::renameTab); tabBar->hide(); setTabBar(tabBar); @@ -116,7 +117,12 @@ { const int tabCount = count(); for (int i = 0; i < tabCount; ++i) { - tabBar()->setTabText(i, tabName(tabPageAt(i))); + auto uuid = tabPageAt(i)->uuid(); + if (m_tabNames.contains(uuid)) { + tabBar()->setTabText(i, m_tabNames.value(uuid)); + } else { + tabBar()->setTabText(i, tabName(tabPageAt(i))); + } tabPageAt(i)->refreshViews(); } } @@ -233,8 +239,9 @@ DolphinTabPage* tabPage = tabPageAt(index); emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); - + removeTab(index); + m_tabNames.remove(tabPage->uuid()); tabPage->deleteLater(); } @@ -269,19 +276,32 @@ currentTabPage()->restoreState(state); } +void DolphinTabWidget::renameTab(int index, const QString& name) +{ + auto uuid = tabPageAt(index)->uuid(); + if (name.isEmpty()) { + m_tabNames.remove(uuid); + tabBar()->setTabText(index, tabName(tabPageAt(index))); + } else { + tabBar()->setTabText(index, name); + m_tabNames.insert(uuid, name); + } +} + void DolphinTabWidget::detachTab(int index) { Q_ASSERT(index >= 0); QStringList args; - + const DolphinTabPage* tabPage = tabPageAt(index); args << tabPage->primaryViewContainer()->url().url(); if (tabPage->splitViewEnabled()) { args << tabPage->secondaryViewContainer()->url().url(); args << QStringLiteral("--split"); } - + + m_tabNames.remove(tabPage->uuid()); const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); KRun::runCommand(command, this); @@ -306,8 +326,11 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url) { const int index = indexOf(qobject_cast(sender())); + auto uuid = tabPageAt(index)->uuid(); if (index >= 0) { - tabBar()->setTabText(index, tabName(tabPageAt(index))); + if (!m_tabNames.contains(uuid)) { + tabBar()->setTabText(index, tabName(tabPageAt(index))); + } tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); // Emit the currentUrlChanged signal if the url of the current tab has been changed. @@ -334,7 +357,7 @@ void DolphinTabWidget::tabInserted(int index) { QTabWidget::tabInserted(index); - + if (count() > 1) { tabBar()->show(); } @@ -345,7 +368,7 @@ void DolphinTabWidget::tabRemoved(int index) { QTabWidget::tabRemoved(index); - + // If only one tab is left, then remove the tab entry so that // closing the last tab is not possible. if (count() < 2) {