X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/f7c2bd34104c592e4111255821432ef83ed4242c..ad5d3367c7fef4c3c11188a768f21a2ee2b3c025:/src/dolphintabwidget.cpp diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index 0cbe4529d..3ce8229f9 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -1,49 +1,39 @@ -/*************************************************************************** - * 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 * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "dolphintabwidget.h" +#include "dolphin_generalsettings.h" #include "dolphintabbar.h" #include "dolphintabpage.h" #include "dolphinviewcontainer.h" #include -#include #include #include +#include +#include #include #include DolphinTabWidget::DolphinTabWidget(QWidget* parent) : QTabWidget(parent), - m_placesSelectorVisible(true), m_lastViewedTab(0) { + KAcceleratorManager::setNoAccel(this); + connect(this, &DolphinTabWidget::tabCloseRequested, - this, static_cast(&DolphinTabWidget::closeTab)); + this, QOverload::of(&DolphinTabWidget::closeTab)); connect(this, &DolphinTabWidget::currentChanged, this, &DolphinTabWidget::currentTabChanged); DolphinTabBar* tabBar = new DolphinTabBar(this); connect(tabBar, &DolphinTabBar::openNewActivatedTab, - this, static_cast(&DolphinTabWidget::openNewActivatedTab)); + this, QOverload::of(&DolphinTabWidget::openNewActivatedTab)); connect(tabBar, &DolphinTabBar::tabDropEvent, this, &DolphinTabWidget::tabDropEvent); connect(tabBar, &DolphinTabBar::tabDetachRequested, @@ -114,6 +104,14 @@ void DolphinTabWidget::readProperties(const KConfigGroup& group) void DolphinTabWidget::refreshViews() { + // Left-elision is better when showing full paths, since you care most + // about the current directory which is on the right + if (GeneralSettings::showFullPathInTitlebar()) { + setElideMode(Qt::ElideLeft); + } else { + setElideMode(Qt::ElideRight); + } + const int tabCount = count(); for (int i = 0; i < tabCount; ++i) { tabBar()->setTabText(i, tabName(tabPageAt(i))); @@ -121,6 +119,11 @@ void DolphinTabWidget::refreshViews() } } +bool DolphinTabWidget::isUrlOpen(const QUrl &url) const +{ + return indexByUrl(url).first >= 0; +} + void DolphinTabWidget::openNewActivatedTab() { const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); @@ -135,14 +138,10 @@ void DolphinTabWidget::openNewActivatedTab() // The URL navigator of the new tab should have the same editable state // as the current tab - KUrlNavigator* navigator = newActiveViewContainer->urlNavigator(); - navigator->setUrlEditable(isUrlEditable); + newActiveViewContainer->urlNavigator()->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(); - } + // Always focus the new tab's view + newActiveViewContainer->view()->setFocus(); } void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl) @@ -151,17 +150,21 @@ void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& s setCurrentIndex(count() - 1); } -void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl) +void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement) { QWidget* focusWidget = QApplication::focusWidget(); DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this); - tabPage->setPlacesSelectorVisible(m_placesSelectorVisible); + tabPage->setActive(false); connect(tabPage, &DolphinTabPage::activeViewChanged, this, &DolphinTabWidget::activeViewChanged); connect(tabPage, &DolphinTabPage::activeViewUrlChanged, this, &DolphinTabWidget::tabUrlChanged); - addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(tabPage)); + int newTabIndex = -1; + if (tabPlacement == AfterCurrentTab) { + newTabIndex = currentIndex() + 1; + } + insertTab(newTabIndex, tabPage, QIcon() /* loaded in tabInserted */, tabName(tabPage)); if (focusWidget) { // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened @@ -177,11 +180,28 @@ void DolphinTabWidget::openDirectories(const QList& dirs, bool splitView) QList::const_iterator it = dirs.constBegin(); while (it != dirs.constEnd()) { const QUrl& primaryUrl = *(it++); + const QPair indexInfo = indexByUrl(primaryUrl); + const int index = indexInfo.first; + const bool isInPrimaryView = indexInfo.second; + if (index >= 0) { + setCurrentIndex(index); + const auto tabPage = tabPageAt(index); + if (isInPrimaryView) { + tabPage->primaryViewContainer()->setActive(true); + } else { + tabPage->secondaryViewContainer()->setActive(true); + } + // BUG: 417230 + // Required for updateViewState() call in openFiles() to work as expected + // If there is a selection, updateViewState() calls are effectively a no-op + tabPage->activeViewContainer()->view()->clearSelection(); + continue; + } if (splitView && (it != dirs.constEnd())) { const QUrl& secondaryUrl = *(it++); - openNewTab(primaryUrl, secondaryUrl); + openNewActivatedTab(primaryUrl, secondaryUrl); } else { - openNewTab(primaryUrl); + openNewActivatedTab(primaryUrl); } } } @@ -194,7 +214,7 @@ void DolphinTabWidget::openFiles(const QList& files, bool splitView) // for each directory. If the "split view" option is enabled, two // directories are shown inside one tab (see openDirectories()). QList dirs; - foreach (const QUrl& url, files) { + for (const QUrl& url : files) { const QUrl dir(url.adjusted(QUrl::RemoveFilename)); if (!dirs.contains(dir)) { dirs.append(dir); @@ -208,10 +228,14 @@ void DolphinTabWidget::openFiles(const QList& files, bool splitView) // 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) { + for (int i = 0; i < tabCount; ++i) { DolphinTabPage* tabPage = tabPageAt(i); tabPage->markUrlsAsSelected(files); tabPage->markUrlAsCurrent(files.first()); + if (i < oldTabCount) { + // Force selection of file if directory was already open, BUG: 417230 + tabPage->activeViewContainer()->view()->updateViewState(); + } } } @@ -232,12 +256,24 @@ void DolphinTabWidget::closeTab(const int index) } DolphinTabPage* tabPage = tabPageAt(index); - emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); + Q_EMIT rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); removeTab(index); tabPage->deleteLater(); } +void DolphinTabWidget::activateTab(const int index) +{ + if (index < count()) { + setCurrentIndex(index); + } +} + +void DolphinTabWidget::activateLastTab() +{ + setCurrentIndex(count() - 1); +} + void DolphinTabWidget::activateNextTab() { const int index = currentIndex() + 1; @@ -250,23 +286,44 @@ void DolphinTabWidget::activatePrevTab() setCurrentIndex(index >= 0 ? index : (count() - 1)); } -void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible) +void DolphinTabWidget::restoreClosedTab(const QByteArray& state) { - // The places-selector from the URL navigator should only be shown - // if the places dock is invisible - m_placesSelectorVisible = !visible; + openNewActivatedTab(); + currentTabPage()->restoreState(state); +} - const int tabCount = count(); - for (int i = 0; i < tabCount; ++i) { - DolphinTabPage* tabPage = tabPageAt(i); - tabPage->setPlacesSelectorVisible(m_placesSelectorVisible); +void DolphinTabWidget::copyToInactiveSplitView() +{ + const DolphinTabPage* tabPage = tabPageAt(currentIndex()); + DolphinViewContainer* activeViewContainer = currentTabPage()->activeViewContainer(); + if (!tabPage->splitViewEnabled() || activeViewContainer->view()->selectedItems().isEmpty()) { + return; + } + + if (tabPage->primaryViewActive()) { + // copy from left panel to right + activeViewContainer->view()->copySelectedItems(activeViewContainer->view()->selectedItems(), tabPage->secondaryViewContainer()->url()); + } else { + // copy from right panel to left + activeViewContainer->view()->copySelectedItems(activeViewContainer->view()->selectedItems(), tabPage->primaryViewContainer()->url()); } } -void DolphinTabWidget::restoreClosedTab(const QByteArray& state) +void DolphinTabWidget::moveToInactiveSplitView() { - openNewActivatedTab(); - currentTabPage()->restoreState(state); + const DolphinTabPage* tabPage = tabPageAt(currentIndex()); + DolphinViewContainer* activeViewContainer = currentTabPage()->activeViewContainer(); + if (!tabPage->splitViewEnabled() || activeViewContainer->view()->selectedItems().isEmpty()) { + return; + } + + if (tabPage->primaryViewActive()) { + // move from left panel to right + activeViewContainer->view()->moveSelectedItems(activeViewContainer->view()->selectedItems(), tabPage->secondaryViewContainer()->url()); + } else { + // move from right panel to left + activeViewContainer->view()->moveSelectedItems(activeViewContainer->view()->selectedItems(), tabPage->primaryViewContainer()->url()); + } } void DolphinTabWidget::detachTab(int index) @@ -281,9 +338,11 @@ void DolphinTabWidget::detachTab(int index) args << tabPage->secondaryViewContainer()->url().url(); args << QStringLiteral("--split"); } + args << QStringLiteral("--new-window"); - const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); - KRun::runCommand(command, this); + KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob("dolphin", args, this); + job->setDesktopName(QStringLiteral("org.kde.dolphin")); + job->start(); closeTab(index); } @@ -308,11 +367,17 @@ void DolphinTabWidget::tabUrlChanged(const QUrl& url) const int index = indexOf(qobject_cast(sender())); if (index >= 0) { tabBar()->setTabText(index, tabName(tabPageAt(index))); - tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); + tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile)); + if (tabBar()->isVisible()) { + tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); + } else { + // Mark as dirty, actually load once the tab bar actually gets shown + tabBar()->setTabIcon(index, QIcon()); + } // Emit the currentUrlChanged signal if the url of the current tab has been changed. if (index == currentIndex()) { - emit currentUrlChanged(url); + Q_EMIT currentUrlChanged(url); } } } @@ -325,8 +390,8 @@ void DolphinTabWidget::currentTabChanged(int index) } DolphinTabPage* tabPage = tabPageAt(index); DolphinViewContainer* viewContainer = tabPage->activeViewContainer(); - emit activeViewChanged(viewContainer); - emit currentUrlChanged(viewContainer->url()); + Q_EMIT activeViewChanged(viewContainer); + Q_EMIT currentUrlChanged(viewContainer->url()); tabPage->setActive(true); m_lastViewedTab = index; } @@ -336,10 +401,21 @@ void DolphinTabWidget::tabInserted(int index) QTabWidget::tabInserted(index); if (count() > 1) { + // Resolve all pending tab icons + for (int i = 0; i < count(); ++i) { + const QUrl url = tabPageAt(i)->activeViewContainer()->url(); + if (tabBar()->tabIcon(i).isNull()) { + tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(url))); + } + if (tabBar()->tabToolTip(i).isEmpty()) { + tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile)); + } + } + tabBar()->show(); } - emit tabCountChanged(count()); + Q_EMIT tabCountChanged(count()); } void DolphinTabWidget::tabRemoved(int index) @@ -352,7 +428,7 @@ void DolphinTabWidget::tabRemoved(int index) tabBar()->hide(); } - emit tabCountChanged(count()); + Q_EMIT tabCountChanged(count()); } QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const @@ -365,3 +441,18 @@ QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText() return name.replace('&', QLatin1String("&&")); } + +QPair DolphinTabWidget::indexByUrl(const QUrl& url) const +{ + for (int i = 0; i < count(); i++) { + const auto tabPage = tabPageAt(i); + if (url == tabPage->primaryViewContainer()->url()) { + return qMakePair(i, true); + } + + if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) { + return qMakePair(i, false); + } + } + return qMakePair(-1, false); +}