From 38381bc6a2999b1d8a0b8a6e2b8d703faa0944d9 Mon Sep 17 00:00:00 2001 From: Emmanuel Pescosta Date: Sun, 10 Aug 2014 18:52:06 +0200 Subject: Implemented DolphinTabWidget class to encapsulate the tab handling from DolphinMainWindow. REVIEW: 119115 --- src/dolphintabwidget.cpp | 345 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 src/dolphintabwidget.cpp (limited to 'src/dolphintabwidget.cpp') diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp new file mode 100644 index 000000000..4bb70b4ea --- /dev/null +++ b/src/dolphintabwidget.cpp @@ -0,0 +1,345 @@ +/*************************************************************************** + * Copyright (C) 2014 by Emmanuel Pescosta * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#include "dolphintabwidget.h" + +#include "dolphintabbar.h" +#include "dolphintabpage.h" +#include "dolphinviewcontainer.h" +#include "dolphin_generalsettings.h" +#include "views/draganddrophelper.h" + +#include +#include +#include +#include + +DolphinTabWidget::DolphinTabWidget(QWidget* parent) : + QTabWidget(parent), + m_placesSelectorVisible(true) +{ + connect(this, SIGNAL(tabCloseRequested(int)), + this, SLOT(closeTab(int))); + connect(this, SIGNAL(currentChanged(int)), + this, SLOT(currentTabChanged(int))); + + DolphinTabBar* tabBar = new DolphinTabBar(this); + connect(tabBar, SIGNAL(openNewActivatedTab(int)), + this, SLOT(openNewActivatedTab(int))); + connect(tabBar, SIGNAL(tabDropEvent(int,QDropEvent*)), + this, SLOT(tabDropEvent(int,QDropEvent*))); + connect(tabBar, SIGNAL(tabDetachRequested(int)), + this, SLOT(detachTab(int))); + tabBar->hide(); + + setTabBar(tabBar); + setDocumentMode(true); + setElideMode(Qt::ElideRight); + setUsesScrollButtons(true); +} + +DolphinTabPage* DolphinTabWidget::currentTabPage() const +{ + return tabPageAt(currentIndex()); +} + +DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const +{ + return static_cast(widget(index)); +} + +void DolphinTabWidget::saveProperties(KConfigGroup& group) const +{ + const int tabCount = count(); + group.writeEntry("Tab Count", tabCount); + group.writeEntry("Active Tab Index", currentIndex()); + + for (int i = 0; i < tabCount; ++i) { + const DolphinTabPage* tabPage = tabPageAt(i); + group.writeEntry("Tab " % QString::number(i), tabPage->saveState()); + } +} + +void DolphinTabWidget::readProperties(const KConfigGroup& group) +{ + const int tabCount = group.readEntry("Tab Count", 0); + for (int i = 0; i < tabCount; ++i) { + if (i >= count()) { + openNewActivatedTab(); + } + const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray()); + tabPageAt(i)->restoreState(state); + } + + const int index = group.readEntry("Active Tab Index", 0); + setCurrentIndex(index); +} + +void DolphinTabWidget::refreshViews() +{ + const int tabCount = count(); + for (int i = 0; i < tabCount; ++i) { + tabPageAt(i)->refreshViews(); + } +} + +void DolphinTabWidget::openNewActivatedTab() +{ + const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); + Q_ASSERT(oldActiveViewContainer); + + const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable(); + + openNewActivatedTab(oldActiveViewContainer->url()); + + DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer(); + Q_ASSERT(newActiveViewContainer); + + // The URL navigator of the new tab should have the same editable state + // as the current tab + KUrlNavigator* navigator = newActiveViewContainer->urlNavigator(); + navigator->setUrlEditable(isUrlEditable); + + if (isUrlEditable) { + // If a new tab is opened and the URL is editable, assure that + // the user can edit the URL without manually setting the focus + navigator->setFocus(); + } +} + +void DolphinTabWidget::openNewActivatedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl) +{ + openNewTab(primaryUrl, secondaryUrl); + setCurrentIndex(count() - 1); +} + +void DolphinTabWidget::openNewTab(const KUrl& primaryUrl, const KUrl& secondaryUrl) +{ + QWidget* focusWidget = QApplication::focusWidget(); + + DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this); + tabPage->setPlacesSelectorVisible(m_placesSelectorVisible); + connect(tabPage, SIGNAL(activeViewChanged(DolphinViewContainer*)), + this, SIGNAL(activeViewChanged(DolphinViewContainer*))); + connect(tabPage, SIGNAL(activeViewUrlChanged(KUrl)), + this, SLOT(tabUrlChanged(KUrl))); + addTab(tabPage, KIcon(KMimeType::iconNameForUrl(primaryUrl)), tabName(primaryUrl)); + + if (focusWidget) { + // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened + // in background, assure that the previous focused widget gets the focus back. + focusWidget->setFocus(); + } +} + +void DolphinTabWidget::openDirectories(const QList& dirs) +{ + const bool hasSplitView = GeneralSettings::splitView(); + + // Open each directory inside a new tab. If the "split view" option has been enabled, + // always show two directories within one tab. + QList::const_iterator it = dirs.constBegin(); + while (it != dirs.constEnd()) { + const KUrl& primaryUrl = *(it++); + if (hasSplitView && (it != dirs.constEnd())) { + const KUrl& secondaryUrl = *(it++); + openNewTab(primaryUrl, secondaryUrl); + } else { + openNewTab(primaryUrl); + } + } +} + +void DolphinTabWidget::openFiles(const QList& files) +{ + if (files.isEmpty()) { + return; + } + + // Get all distinct directories from 'files' and open a tab + // for each directory. If the "split view" option is enabled, two + // directories are shown inside one tab (see openDirectories()). + QList dirs; + foreach (const KUrl& url, files) { + const KUrl dir(url.directory()); + if (!dirs.contains(dir)) { + dirs.append(dir); + } + } + + const int oldTabCount = count(); + openDirectories(dirs); + const int tabCount = count(); + + // Select the files. Although the files can be split between several + // tabs, there is no need to split 'files' accordingly, as + // the DolphinView will just ignore invalid selections. + for (int i = oldTabCount; i < tabCount; ++i) { + DolphinTabPage* tabPage = tabPageAt(i); + tabPage->markUrlsAsSelected(files); + tabPage->markUrlAsCurrent(files.first()); + } +} + +void DolphinTabWidget::closeTab() +{ + closeTab(currentIndex()); +} + +void DolphinTabWidget::closeTab(const int index) +{ + Q_ASSERT(index >= 0); + Q_ASSERT(index < count()); + + if (count() < 2) { + // Never close the last tab. + return; + } + + DolphinTabPage* tabPage = tabPageAt(index); + if (tabPage->splitViewEnabled()) { + emit rememberClosedTab(tabPage->primaryViewContainer()->url(), + tabPage->secondaryViewContainer()->url()); + } else { + emit rememberClosedTab(tabPage->primaryViewContainer()->url(), KUrl()); + } + + removeTab(index); + tabPage->deleteLater(); +} + +void DolphinTabWidget::activateNextTab() +{ + const int index = currentIndex() + 1; + setCurrentIndex(index < count() ? index : 0); +} + +void DolphinTabWidget::activatePrevTab() +{ + const int index = currentIndex() - 1; + setCurrentIndex(index >= 0 ? index : (count() - 1)); +} + +void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible) +{ + // The places-selector from the URL navigator should only be shown + // if the places dock is invisible + m_placesSelectorVisible = !visible; + + const int tabCount = count(); + for (int i = 0; i < tabCount; ++i) { + DolphinTabPage* tabPage = tabPageAt(i); + tabPage->setPlacesSelectorVisible(m_placesSelectorVisible); + } +} + +void DolphinTabWidget::detachTab(int index) +{ + Q_ASSERT(index >= 0); + + const QString separator(QLatin1Char(' ')); + QString command = QLatin1String("dolphin"); + + const DolphinTabPage* tabPage = tabPageAt(index); + command += separator + tabPage->primaryViewContainer()->url().url(); + if (tabPage->splitViewEnabled()) { + command += separator + tabPage->secondaryViewContainer()->url().url(); + command += separator + QLatin1String("-split"); + } + + KRun::runCommand(command, this); + + closeTab(index); +} + +void DolphinTabWidget::openNewActivatedTab(int index) +{ + Q_ASSERT(index >= 0); + const DolphinTabPage* tabPage = tabPageAt(index); + openNewActivatedTab(tabPage->activeViewContainer()->url()); +} + +void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) +{ + if (index >= 0) { + const DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); + + QString error; + DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error); + if (!error.isEmpty()) { + currentTabPage()->activeViewContainer()->showMessage(error, DolphinViewContainer::Error); + } + } +} + +void DolphinTabWidget::tabUrlChanged(const KUrl& url) +{ + const int index = indexOf(qobject_cast(sender())); + if (index >= 0) { + tabBar()->setTabText(index, tabName(url)); + tabBar()->setTabIcon(index, KIcon(KMimeType::iconNameForUrl(url))); + } +} + +void DolphinTabWidget::currentTabChanged(int index) +{ + emit activeViewChanged(tabPageAt(index)->activeViewContainer()); +} + +void DolphinTabWidget::tabInserted(int index) +{ + QTabWidget::tabInserted(index); + + if (count() > 1) { + tabBar()->show(); + } + + emit tabCountChanged(count()); +} + +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) { + tabBar()->hide(); + } + + emit tabCountChanged(count()); +} + +QString DolphinTabWidget::tabName(const KUrl& url) const +{ + QString name; + if (url.equals(KUrl("file:///"))) { + name = '/'; + } else { + name = url.fileName(); + if (name.isEmpty()) { + name = url.protocol(); + } else { + // Make sure that a '&' inside the directory name is displayed correctly + // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText() + name.replace('&', "&&"); + } + } + return name; +} -- cgit v1.3 From 62418c58a58fac668e713655552b1c614b226298 Mon Sep 17 00:00:00 2001 From: Emmanuel Pescosta Date: Sun, 10 Aug 2014 20:36:44 +0200 Subject: Use DolphinTabPage saveState/restoreState to remember and re-open closed tabs. REVIEW: 118968 --- src/dolphinmainwindow.cpp | 8 ++++---- src/dolphinrecenttabsmenu.cpp | 19 ++++++------------- src/dolphinrecenttabsmenu.h | 4 ++-- src/dolphintabwidget.cpp | 13 +++++++------ src/dolphintabwidget.h | 8 +++++++- 5 files changed, 26 insertions(+), 26 deletions(-) (limited to 'src/dolphintabwidget.cpp') diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 0882bac71..588bfda64 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -1058,10 +1058,10 @@ void DolphinMainWindow::setupActions() DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this); actionCollection()->addAction("closed_tabs", recentTabsMenu); - connect(m_tabWidget, SIGNAL(rememberClosedTab(KUrl,KUrl)), - recentTabsMenu, SLOT(rememberClosedTab(KUrl,KUrl))); - connect(recentTabsMenu, SIGNAL(restoreClosedTab(KUrl,KUrl)), - this, SLOT(openNewActivatedTab(KUrl,KUrl))); + connect(m_tabWidget, SIGNAL(rememberClosedTab(KUrl,QByteArray)), + recentTabsMenu, SLOT(rememberClosedTab(KUrl,QByteArray))); + connect(recentTabsMenu, SIGNAL(restoreClosedTab(QByteArray)), + m_tabWidget, SLOT(restoreClosedTab(QByteArray))); connect(recentTabsMenu, SIGNAL(closedTabsCountChanged(uint)), this, SLOT(closedTabsCountChanged(uint))); diff --git a/src/dolphinrecenttabsmenu.cpp b/src/dolphinrecenttabsmenu.cpp index 2335f1bf4..fa3eaf166 100644 --- a/src/dolphinrecenttabsmenu.cpp +++ b/src/dolphinrecenttabsmenu.cpp @@ -40,19 +40,14 @@ DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject* parent) : this, SLOT(handleAction(QAction*))); } -void DolphinRecentTabsMenu::rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl) +void DolphinRecentTabsMenu::rememberClosedTab(const KUrl& url, const QByteArray& state) { QAction* action = new QAction(menu()); - action->setText(primaryUrl.path()); - - const QString iconName = KMimeType::iconNameForUrl(primaryUrl); + action->setText(url.path()); + action->setData(state); + const QString iconName = KMimeType::iconNameForUrl(url); action->setIcon(KIcon(iconName)); - KUrl::List urls; - urls << primaryUrl; - urls << secondaryUrl; - action->setData(QVariant::fromValue(urls)); - // Add the closed tab menu entry after the separator and // "Empty Recently Closed Tabs" entry if (menu()->actions().size() == 2) { @@ -88,13 +83,11 @@ void DolphinRecentTabsMenu::handleAction(QAction* action) } emit closedTabsCountChanged(0); } else { - const KUrl::List urls = action->data().value(); + const QByteArray state = action->data().value(); removeAction(action); delete action; action = 0; - if (urls.count() == 2) { - emit restoreClosedTab(urls.first(), urls.last()); - } + emit restoreClosedTab(state); emit closedTabsCountChanged(menu()->actions().size() - 2); } diff --git a/src/dolphinrecenttabsmenu.h b/src/dolphinrecenttabsmenu.h index b5acc735e..910e564a1 100644 --- a/src/dolphinrecenttabsmenu.h +++ b/src/dolphinrecenttabsmenu.h @@ -34,11 +34,11 @@ public: explicit DolphinRecentTabsMenu(QObject* parent); public slots: - void rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl); + void rememberClosedTab(const KUrl& url, const QByteArray& state); void undoCloseTab(); signals: - void restoreClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl); + void restoreClosedTab(const QByteArray& state); void closedTabsCountChanged(unsigned int count); private slots: diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index 4bb70b4ea..ea71b4856 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -213,12 +213,7 @@ void DolphinTabWidget::closeTab(const int index) } DolphinTabPage* tabPage = tabPageAt(index); - if (tabPage->splitViewEnabled()) { - emit rememberClosedTab(tabPage->primaryViewContainer()->url(), - tabPage->secondaryViewContainer()->url()); - } else { - emit rememberClosedTab(tabPage->primaryViewContainer()->url(), KUrl()); - } + emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); removeTab(index); tabPage->deleteLater(); @@ -249,6 +244,12 @@ void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible) } } +void DolphinTabWidget::restoreClosedTab(const QByteArray& state) +{ + openNewActivatedTab(); + currentTabPage()->restoreState(state); +} + void DolphinTabWidget::detachTab(int index) { Q_ASSERT(index >= 0); diff --git a/src/dolphintabwidget.h b/src/dolphintabwidget.h index a9bef11be..aaadbc997 100644 --- a/src/dolphintabwidget.h +++ b/src/dolphintabwidget.h @@ -70,7 +70,7 @@ signals: /** * Is emitted when a tab has been closed. */ - void rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl); + void rememberClosedTab(const KUrl& url, const QByteArray& state); public slots: /** @@ -133,6 +133,12 @@ public slots: */ void slotPlacesPanelVisibilityChanged(bool visible); + /** + * Is called when the user wants to reopen a previously closed tab from + * 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 -- cgit v1.3 From 39d8fb12c1552ec708b5fc1846d7aa9828329417 Mon Sep 17 00:00:00 2001 From: Emmanuel Pescosta Date: Wed, 13 Aug 2014 22:06:28 +0200 Subject: React on the redirection signal from DolphinView to properly update the tab and window titles. REVIEW: 119697 BUG: 305721 --- src/dolphinmainwindow.cpp | 37 ++++++++++++++++++------------------- src/dolphinmainwindow.h | 12 ++++++------ src/dolphintabpage.cpp | 13 +++++++++++++ src/dolphintabpage.h | 7 +++++++ src/dolphintabwidget.cpp | 9 ++++++++- src/dolphintabwidget.h | 6 ++++++ 6 files changed, 58 insertions(+), 26 deletions(-) (limited to 'src/dolphintabwidget.cpp') diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 588bfda64..95b08af96 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -136,6 +136,8 @@ DolphinMainWindow::DolphinMainWindow() : this, SLOT(activeViewChanged(DolphinViewContainer*))); connect(m_tabWidget, SIGNAL(tabCountChanged(int)), this, SLOT(tabCountChanged(int))); + connect(m_tabWidget, SIGNAL(currentUrlChanged(KUrl)), + this, SLOT(setUrlAsCaption(KUrl))); setCentralWidget(m_tabWidget); setupActions(); @@ -236,7 +238,6 @@ void DolphinMainWindow::changeUrl(const KUrl& url) updatePasteAction(); updateViewActions(); updateGoActions(); - setUrlAsCaption(url); emit urlChanged(url); } @@ -945,8 +946,6 @@ void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer) updateGoActions(); const KUrl url = viewContainer->url(); - setUrlAsCaption(url); - emit urlChanged(url); } @@ -958,6 +957,22 @@ void DolphinMainWindow::tabCountChanged(int count) actionCollection()->action("activate_prev_tab")->setEnabled(enableTabActions); } +void DolphinMainWindow::setUrlAsCaption(const KUrl& url) +{ + QString caption; + if (!url.isLocalFile()) { + caption.append(url.protocol() + " - "); + if (url.hasHost()) { + caption.append(url.host() + " - "); + } + } + + const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName(); + caption.append(fileName); + + setCaption(caption); +} + void DolphinMainWindow::setupActions() { // setup 'File' menu @@ -1471,22 +1486,6 @@ bool DolphinMainWindow::isKompareInstalled() const return installed; } -void DolphinMainWindow::setUrlAsCaption(const KUrl& url) -{ - QString caption; - if (!url.isLocalFile()) { - caption.append(url.protocol() + " - "); - if (url.hasHost()) { - caption.append(url.host() + " - "); - } - } - - const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName(); - caption.append(fileName); - - setCaption(caption); -} - void DolphinMainWindow::createPanelAction(const KIcon& icon, const QKeySequence& shortcut, QAction* dockAction, diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 7bce7f13e..9d4c003af 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -429,6 +429,12 @@ private slots: */ void tabCountChanged(int count); + /** + * Sets the window caption to url.fileName() if this is non-empty, + * "/" if the URL is "file:///", and url.protocol() otherwise. + */ + void setUrlAsCaption(const KUrl& url); + private: void setupActions(); void setupDockWidgets(); @@ -464,12 +470,6 @@ private: bool isKompareInstalled() const; - /** - * Sets the window caption to url.fileName() if this is non-empty, - * "/" if the URL is "file:///", and url.protocol() otherwise. - */ - void setUrlAsCaption(const KUrl& url); - /** * Creates an action for showing/hiding a panel, that is accessible * in "Configure toolbars..." and "Configure shortcuts...". This is necessary diff --git a/src/dolphintabpage.cpp b/src/dolphintabpage.cpp index 23e33c958..3d1ba5a3e 100644 --- a/src/dolphintabpage.cpp +++ b/src/dolphintabpage.cpp @@ -41,6 +41,8 @@ DolphinTabPage::DolphinTabPage(const KUrl& primaryUrl, const KUrl& secondaryUrl, m_primaryViewContainer = createViewContainer(primaryUrl); connect(m_primaryViewContainer->view(), SIGNAL(urlChanged(KUrl)), this, SIGNAL(activeViewUrlChanged(KUrl))); + connect(m_primaryViewContainer->view(), SIGNAL(redirection(KUrl,KUrl)), + this, SLOT(slotViewUrlRedirection(KUrl,KUrl))); m_splitter->addWidget(m_primaryViewContainer); m_primaryViewContainer->show(); @@ -245,14 +247,25 @@ void DolphinTabPage::slotViewActivated() if (newActiveView != oldActiveView) { disconnect(oldActiveView, SIGNAL(urlChanged(KUrl)), this, SIGNAL(activeViewUrlChanged(KUrl))); + disconnect(oldActiveView, SIGNAL(redirection(KUrl,KUrl)), + this, SLOT(slotViewUrlRedirection(KUrl,KUrl))); connect(newActiveView, SIGNAL(urlChanged(KUrl)), this, SIGNAL(activeViewUrlChanged(KUrl))); + connect(newActiveView, SIGNAL(redirection(KUrl,KUrl)), + this, SLOT(slotViewUrlRedirection(KUrl,KUrl))); } emit activeViewUrlChanged(activeViewContainer()->url()); emit activeViewChanged(activeViewContainer()); } +void DolphinTabPage::slotViewUrlRedirection(const KUrl& oldUrl, const KUrl& newUrl) +{ + Q_UNUSED(oldUrl); + + emit activeViewUrlChanged(newUrl); +} + DolphinViewContainer* DolphinTabPage::createViewContainer(const KUrl& url) const { DolphinViewContainer* container = new DolphinViewContainer(url, m_splitter); diff --git a/src/dolphintabpage.h b/src/dolphintabpage.h index 278524792..de5a58915 100644 --- a/src/dolphintabpage.h +++ b/src/dolphintabpage.h @@ -133,6 +133,13 @@ private slots: */ void slotViewActivated(); + /** + * Handles the view url redirection event. + * + * It emits the activeViewUrlChanged signal with the url \a newUrl. + */ + void slotViewUrlRedirection(const KUrl& oldUrl, const KUrl& newUrl); + private: /** * Creates a new view container and does the default initialization. diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index ea71b4856..76d4b8d48 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -295,12 +295,19 @@ void DolphinTabWidget::tabUrlChanged(const KUrl& url) if (index >= 0) { tabBar()->setTabText(index, tabName(url)); tabBar()->setTabIcon(index, KIcon(KMimeType::iconNameForUrl(url))); + + // Emit the currentUrlChanged signal if the url of the current tab has been changed. + if (index == currentIndex()) { + emit currentUrlChanged(url); + } } } void DolphinTabWidget::currentTabChanged(int index) { - emit activeViewChanged(tabPageAt(index)->activeViewContainer()); + DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer(); + emit activeViewChanged(viewContainer); + emit currentUrlChanged(viewContainer->url()); } void DolphinTabWidget::tabInserted(int index) diff --git a/src/dolphintabwidget.h b/src/dolphintabwidget.h index aaadbc997..98bcd985a 100644 --- a/src/dolphintabwidget.h +++ b/src/dolphintabwidget.h @@ -72,6 +72,12 @@ signals: */ void rememberClosedTab(const KUrl& url, const QByteArray& state); + /** + * Is emitted when the url of the current tab has been changed. This signal + * is also emitted when the active view has been changed. + */ + void currentUrlChanged(const KUrl& url); + public slots: /** * Opens a new view with the current URL that is part of a tab and activates -- cgit v1.3 From 2d2d55f3df09614e6b7cf267771b52a04dcb5e28 Mon Sep 17 00:00:00 2001 From: Emmanuel Pescosta Date: Wed, 20 Aug 2014 23:06:39 +0200 Subject: Save the view states in addition to the view urls and splitter state in DolphinTabPage. Also added version numbers to view and tab state. REVIEW: 119792 --- src/dolphintabpage.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ src/dolphintabpage.h | 9 ++++++++ src/dolphintabwidget.cpp | 13 ++++++++--- src/views/dolphinview.cpp | 10 +++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) (limited to 'src/dolphintabwidget.cpp') diff --git a/src/dolphintabpage.cpp b/src/dolphintabpage.cpp index 3d1ba5a3e..f7000ea66 100644 --- a/src/dolphintabpage.cpp +++ b/src/dolphintabpage.cpp @@ -171,14 +171,18 @@ QByteArray DolphinTabPage::saveState() const QByteArray state; QDataStream stream(&state, QIODevice::WriteOnly); + stream << quint32(2); // Tab state version + stream << m_splitViewEnabled; stream << m_primaryViewContainer->url(); stream << m_primaryViewContainer->urlNavigator()->isUrlEditable(); + m_primaryViewContainer->view()->saveState(stream); if (m_splitViewEnabled) { stream << m_secondaryViewContainer->url(); stream << m_secondaryViewContainer->urlNavigator()->isUrlEditable(); + m_secondaryViewContainer->view()->saveState(stream); } stream << m_primaryViewActive; @@ -196,6 +200,58 @@ void DolphinTabPage::restoreState(const QByteArray& state) QByteArray sd = state; QDataStream stream(&sd, QIODevice::ReadOnly); + // Read the version number of the tab state and check if the version is supported. + quint32 version = 0; + stream >> version; + if (version != 2) { + // The version of the tab state isn't supported, we can't restore it. + return; + } + + bool isSplitViewEnabled = false; + stream >> isSplitViewEnabled; + setSplitViewEnabled(isSplitViewEnabled); + + KUrl primaryUrl; + stream >> primaryUrl; + m_primaryViewContainer->setUrl(primaryUrl); + bool primaryUrlEditable; + stream >> primaryUrlEditable; + m_primaryViewContainer->urlNavigator()->setUrlEditable(primaryUrlEditable); + m_primaryViewContainer->view()->restoreState(stream); + + if (isSplitViewEnabled) { + KUrl secondaryUrl; + stream >> secondaryUrl; + m_secondaryViewContainer->setUrl(secondaryUrl); + bool secondaryUrlEditable; + stream >> secondaryUrlEditable; + m_secondaryViewContainer->urlNavigator()->setUrlEditable(secondaryUrlEditable); + m_secondaryViewContainer->view()->restoreState(stream); + } + + stream >> m_primaryViewActive; + if (m_primaryViewActive) { + m_primaryViewContainer->setActive(true); + } else { + Q_ASSERT(m_splitViewEnabled); + m_secondaryViewContainer->setActive(true); + } + + QByteArray splitterState; + stream >> splitterState; + m_splitter->restoreState(splitterState); +} + +void DolphinTabPage::restoreStateV1(const QByteArray& state) +{ + if (state.isEmpty()) { + return; + } + + QByteArray sd = state; + QDataStream stream(&sd, QIODevice::ReadOnly); + bool isSplitViewEnabled = false; stream >> isSplitViewEnabled; setSplitViewEnabled(isSplitViewEnabled); diff --git a/src/dolphintabpage.h b/src/dolphintabpage.h index de5a58915..2a406f4a9 100644 --- a/src/dolphintabpage.h +++ b/src/dolphintabpage.h @@ -120,6 +120,15 @@ public: */ void restoreState(const QByteArray& state); + /** + * Restores all tab related properties (urls, splitter layout, ...) from + * the given \a state. + * + * @deprecated The first tab state version has no version number, we keep + * this method to restore old states (<= Dolphin 4.14.x). + */ + void restoreStateV1(const QByteArray& state); + signals: void activeViewChanged(DolphinViewContainer* viewContainer); void activeViewUrlChanged(const KUrl& url); diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index 76d4b8d48..b1b2d858f 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -72,7 +72,7 @@ void DolphinTabWidget::saveProperties(KConfigGroup& group) const for (int i = 0; i < tabCount; ++i) { const DolphinTabPage* tabPage = tabPageAt(i); - group.writeEntry("Tab " % QString::number(i), tabPage->saveState()); + group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState()); } } @@ -83,8 +83,15 @@ void DolphinTabWidget::readProperties(const KConfigGroup& group) if (i >= count()) { openNewActivatedTab(); } - const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray()); - tabPageAt(i)->restoreState(state); + if (group.hasKey("Tab Data " % QString::number(i))) { + // Tab state created with Dolphin > 4.14.x + const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray()); + tabPageAt(i)->restoreState(state); + } else { + // Tab state created with Dolphin <= 4.14.x + const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray()); + tabPageAt(i)->restoreStateV1(state); + } } const int index = group.readEntry("Active Tab Index", 0); diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 02b8815e0..1de973bd5 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -1167,6 +1167,14 @@ bool DolphinView::itemsExpandable() const void DolphinView::restoreState(QDataStream& stream) { + // Read the version number of the view state and check if the version is supported. + quint32 version = 0; + stream >> version; + if (version != 1) { + // The version of the view state isn't supported, we can't restore it. + return; + } + // Restore the current item that had the keyboard focus stream >> m_currentItemUrl; @@ -1181,6 +1189,8 @@ void DolphinView::restoreState(QDataStream& stream) void DolphinView::saveState(QDataStream& stream) { + stream << quint32(1); // View state version + // Save the current item that has the keyboard focus const int currentIndex = m_container->controller()->selectionManager()->currentItem(); if (currentIndex != -1) { -- cgit v1.3