#include "dolphintabwidget.h"
+#include "dolphin_generalsettings.h"
#include "dolphintabbar.h"
#include "dolphintabpage.h"
#include "dolphinviewcontainer.h"
-#include <QApplication>
-#include <QDropEvent>
#include <KConfigGroup>
+#include <KRun>
#include <KShell>
#include <kio/global.h>
-#include <KRun>
+#include <KAcceleratorManager>
+
+#include <QApplication>
+#include <QDropEvent>
DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
QTabWidget(parent),
m_placesSelectorVisible(true),
- m_previousTab(-1)
+ m_lastViewedTab(0)
{
+ KAcceleratorManager::setNoAccel(this);
+
connect(this, &DolphinTabWidget::tabCloseRequested,
- this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab));
+ this, QOverload<int>::of(&DolphinTabWidget::closeTab));
connect(this, &DolphinTabWidget::currentChanged,
this, &DolphinTabWidget::currentTabChanged);
DolphinTabBar* tabBar = new DolphinTabBar(this);
connect(tabBar, &DolphinTabBar::openNewActivatedTab,
- this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab));
+ this, QOverload<int>::of(&DolphinTabWidget::openNewActivatedTab));
connect(tabBar, &DolphinTabBar::tabDropEvent,
this, &DolphinTabWidget::tabDropEvent);
connect(tabBar, &DolphinTabBar::tabDetachRequested,
return tabPageAt(currentIndex());
}
+DolphinTabPage* DolphinTabWidget::nextTabPage() const
+{
+ const int index = currentIndex() + 1;
+ return tabPageAt(index < count() ? index : 0);
+}
+
+DolphinTabPage* DolphinTabWidget::prevTabPage() const
+{
+ const int index = currentIndex() - 1;
+ return tabPageAt(index >= 0 ? index : (count() - 1));
+}
+
DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
{
return static_cast<DolphinTabPage*>(widget(index));
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)));
tabPageAt(i)->refreshViews();
}
}
+bool DolphinTabWidget::isUrlOpen(const QUrl &url) const
+{
+ return indexByUrl(url).first >= 0;
+}
+
void DolphinTabWidget::openNewActivatedTab()
{
const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
// 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)
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->setActive(false);
tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
connect(tabPage, &DolphinTabPage::activeViewChanged,
this, &DolphinTabWidget::activeViewChanged);
connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
this, &DolphinTabWidget::tabUrlChanged);
- addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(primaryUrl));
+ 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
QList<QUrl>::const_iterator it = dirs.constBegin();
while (it != dirs.constEnd()) {
const QUrl& primaryUrl = *(it++);
+ const QPair<int, bool> 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);
+ }
+ continue;
+ }
if (splitView && (it != dirs.constEnd())) {
const QUrl& secondaryUrl = *(it++);
- openNewTab(primaryUrl, secondaryUrl);
+ openNewActivatedTab(primaryUrl, secondaryUrl);
} else {
- openNewTab(primaryUrl);
+ openNewActivatedTab(primaryUrl);
}
}
}
Q_ASSERT(index < count());
if (count() < 2) {
- // Never close the last tab.
+ // Close Dolphin when closing the last tab.
+ parentWidget()->close();
return;
}
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;
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);
{
if (index >= 0) {
DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
- view->dropUrls(view->url(), event);
+ view->dropUrls(view->url(), event, view);
}
}
{
const int index = indexOf(qobject_cast<QWidget*>(sender()));
if (index >= 0) {
- tabBar()->setTabText(index, tabName(url));
- tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
+ tabBar()->setTabText(index, tabName(tabPageAt(index)));
+ 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()) {
void DolphinTabWidget::currentTabChanged(int index)
{
- DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer();
- viewContainer->setActive(true);
+ // last-viewed tab deactivation
+ if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) {
+ tabPage->setActive(false);
+ }
+ DolphinTabPage* tabPage = tabPageAt(index);
+ DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
emit activeViewChanged(viewContainer);
emit currentUrlChanged(viewContainer->url());
- viewContainer->view()->setFocus();
-
- if (tabPageAt(m_previousTab)) {
- tabPageAt(m_previousTab)->activeViewContainer()->setActive(false);
- }
- m_previousTab = index;
+ tabPage->setActive(true);
+ m_lastViewedTab = index;
}
void DolphinTabWidget::tabInserted(int index)
QTabWidget::tabInserted(index);
if (count() > 1) {
+ // Resolve all pending tab icons
+ for (int i = 0; i < count(); ++i) {
+ if (tabBar()->tabIcon(i).isNull()) {
+ tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(tabPageAt(i)->activeViewContainer()->url())));
+ }
+ }
+
tabBar()->show();
}
emit tabCountChanged(count());
}
-QString DolphinTabWidget::tabName(const QUrl& url) const
+QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
{
- QString name;
- if (url == QUrl(QStringLiteral("file:///"))) {
- name = '/';
- } else {
- name = url.adjusted(QUrl::StripTrailingSlash).fileName();
- if (name.isEmpty()) {
- name = url.scheme();
- } else {
- // Make sure that a '&' inside the directory name is displayed correctly
- // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
- name.replace('&', QLatin1String("&&"));
+ if (!tabPage) {
+ return QString();
+ }
+ QString name = tabPage->activeViewContainer()->caption();
+ // Make sure that a '&' inside the directory name is displayed correctly
+ // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
+ return name.replace('&', QLatin1String("&&"));
+}
+
+QPair<int, bool> 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 name;
+ return qMakePair(-1, false);
}