2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "dolphintabwidget.h"
9 #include "dolphin_generalsettings.h"
10 #include "dolphintabbar.h"
11 #include "dolphinviewcontainer.h"
12 #include "views/draganddrophelper.h"
14 #include <KAcceleratorManager>
15 #include <KConfigGroup>
16 #include <KIO/CommandLauncherJob>
17 #include <KLocalizedString>
19 #include <KStringHandler>
20 #include <kio/global.h>
22 #include <QApplication>
24 #include <QStackedWidget>
26 DolphinTabWidget::DolphinTabWidget(DolphinNavigatorsWidgetAction
*navigatorsWidget
, QWidget
*parent
)
28 , m_lastViewedTab(nullptr)
29 , m_navigatorsWidget
{navigatorsWidget
}
31 KAcceleratorManager::setNoAccel(this);
33 connect(this, &DolphinTabWidget::tabCloseRequested
, this, QOverload
<int>::of(&DolphinTabWidget::closeTab
));
34 connect(this, &DolphinTabWidget::currentChanged
, this, &DolphinTabWidget::currentTabChanged
);
36 DolphinTabBar
*tabBar
= new DolphinTabBar(this);
37 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
, this, QOverload
<int>::of(&DolphinTabWidget::openNewActivatedTab
));
38 connect(tabBar
, &DolphinTabBar::tabDragMoveEvent
, this, &DolphinTabWidget::tabDragMoveEvent
);
39 connect(tabBar
, &DolphinTabBar::tabDropEvent
, this, &DolphinTabWidget::tabDropEvent
);
40 connect(tabBar
, &DolphinTabBar::tabDetachRequested
, this, &DolphinTabWidget::detachTab
);
43 setDocumentMode(true);
44 setElideMode(Qt::ElideRight
);
45 setUsesScrollButtons(true);
46 setTabBarAutoHide(true);
48 auto stackWidget
{findChild
<QStackedWidget
*>()};
49 // i18n: This accessible name will be announced any time the user moves keyboard focus e.g. from the toolbar or the places panel towards the main working
50 // area of Dolphin. It gives structure. This container does not only contain the main view but also the status bar, the search panel, filter, and selection
51 // mode bars, so calling it just a "View" is a bit wrong, but hopefully still gets the point across.
52 stackWidget
->setAccessibleName(i18nc("accessible name of Dolphin's view container", "Location View")); // Without this call, the non-descript Qt provided
53 // "Layered Pane" role is announced.
56 DolphinTabPage
*DolphinTabWidget::currentTabPage() const
58 return tabPageAt(currentIndex());
61 DolphinTabPage
*DolphinTabWidget::nextTabPage() const
63 const int index
= currentIndex() + 1;
64 return tabPageAt(index
< count() ? index
: 0);
67 DolphinTabPage
*DolphinTabWidget::prevTabPage() const
69 const int index
= currentIndex() - 1;
70 return tabPageAt(index
>= 0 ? index
: (count() - 1));
73 DolphinTabPage
*DolphinTabWidget::tabPageAt(const int index
) const
75 return static_cast<DolphinTabPage
*>(widget(index
));
78 void DolphinTabWidget::saveProperties(KConfigGroup
&group
) const
80 const int tabCount
= count();
81 group
.writeEntry("Tab Count", tabCount
);
82 group
.writeEntry("Active Tab Index", currentIndex());
84 for (int i
= 0; i
< tabCount
; ++i
) {
85 const DolphinTabPage
*tabPage
= tabPageAt(i
);
86 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
90 void DolphinTabWidget::readProperties(const KConfigGroup
&group
)
92 const int tabCount
= group
.readEntry("Tab Count", 0);
93 for (int i
= 0; i
< tabCount
; ++i
) {
95 openNewActivatedTab();
97 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
98 tabPageAt(i
)->restoreState(state
);
101 const int index
= group
.readEntry("Active Tab Index", 0);
102 setCurrentIndex(index
);
105 void DolphinTabWidget::refreshViews()
107 // Left-elision is better when showing full paths, since you care most
108 // about the current directory which is on the right
109 if (GeneralSettings::showFullPathInTitlebar()) {
110 setElideMode(Qt::ElideLeft
);
112 setElideMode(Qt::ElideRight
);
115 const int tabCount
= count();
116 for (int i
= 0; i
< tabCount
; ++i
) {
118 tabPageAt(i
)->refreshViews();
122 void DolphinTabWidget::updateTabName(int index
)
124 Q_ASSERT(index
>= 0);
125 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
128 bool DolphinTabWidget::isUrlOpen(const QUrl
&url
) const
130 return viewOpenAtDirectory(url
).has_value();
133 bool DolphinTabWidget::isItemVisibleInAnyView(const QUrl
&urlOfItem
) const
135 return viewShowingItem(urlOfItem
).has_value();
138 void DolphinTabWidget::openNewActivatedTab()
140 std::unique_ptr
<DolphinUrlNavigator::VisualState
> oldNavigatorState
;
141 if (currentTabPage()->primaryViewActive() || !m_navigatorsWidget
->secondaryUrlNavigator()) {
142 oldNavigatorState
= m_navigatorsWidget
->primaryUrlNavigator()->visualState();
144 oldNavigatorState
= m_navigatorsWidget
->secondaryUrlNavigator()->visualState();
147 const DolphinViewContainer
*oldActiveViewContainer
= currentTabPage()->activeViewContainer();
148 Q_ASSERT(oldActiveViewContainer
);
150 openNewActivatedTab(oldActiveViewContainer
->url());
152 DolphinViewContainer
*newActiveViewContainer
= currentTabPage()->activeViewContainer();
153 Q_ASSERT(newActiveViewContainer
);
155 // The URL navigator of the new tab should have the same editable state
156 // as the current tab
157 newActiveViewContainer
->urlNavigator()->setVisualState(*oldNavigatorState
.get());
159 // Always focus the new tab's view
160 newActiveViewContainer
->view()->setFocus();
163 void DolphinTabWidget::openNewActivatedTab(const QUrl
&primaryUrl
, const QUrl
&secondaryUrl
)
165 openNewTab(primaryUrl
, secondaryUrl
);
166 if (GeneralSettings::openNewTabAfterLastTab()) {
167 setCurrentIndex(count() - 1);
169 setCurrentIndex(currentIndex() + 1);
173 void DolphinTabWidget::openNewTab(const QUrl
&primaryUrl
, const QUrl
&secondaryUrl
, DolphinTabWidget::NewTabPosition position
)
175 QWidget
*focusWidget
= QApplication::focusWidget();
177 DolphinTabPage
*tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
178 tabPage
->setActive(false);
179 connect(tabPage
, &DolphinTabPage::activeViewChanged
, this, &DolphinTabWidget::activeViewChanged
);
180 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
, this, &DolphinTabWidget::tabUrlChanged
);
181 connect(tabPage
->activeViewContainer(), &DolphinViewContainer::captionChanged
, this, [this, tabPage
]() {
182 updateTabName(indexOf(tabPage
));
185 if (position
== NewTabPosition::FollowSetting
) {
186 if (GeneralSettings::openNewTabAfterLastTab()) {
187 position
= NewTabPosition::AtEnd
;
189 position
= NewTabPosition::AfterCurrent
;
193 int newTabIndex
= -1;
194 if (position
== NewTabPosition::AfterCurrent
|| (position
== NewTabPosition::FollowSetting
&& !GeneralSettings::openNewTabAfterLastTab())) {
195 newTabIndex
= currentIndex() + 1;
198 insertTab(newTabIndex
, tabPage
, QIcon() /* loaded in tabInserted */, tabName(tabPage
));
201 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
202 // in background, assure that the previous focused widget gets the focus back.
203 focusWidget
->setFocus();
207 void DolphinTabWidget::openDirectories(const QList
<QUrl
> &dirs
, bool splitView
)
209 Q_ASSERT(dirs
.size() > 0);
211 bool somethingWasAlreadyOpen
= false;
213 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
214 while (it
!= dirs
.constEnd()) {
215 const QUrl
&primaryUrl
= *(it
++);
216 const std::optional
<ViewIndex
> viewIndexAtDirectory
= viewOpenAtDirectory(primaryUrl
);
218 // When the user asks for a URL that's already open,
219 // activate it instead of opening a new tab
220 if (viewIndexAtDirectory
.has_value()) {
221 somethingWasAlreadyOpen
= true;
222 activateViewContainerAt(viewIndexAtDirectory
.value());
223 } else if (splitView
&& (it
!= dirs
.constEnd())) {
224 const QUrl
&secondaryUrl
= *(it
++);
225 if (somethingWasAlreadyOpen
) {
226 openNewTab(primaryUrl
, secondaryUrl
);
228 openNewActivatedTab(primaryUrl
, secondaryUrl
);
231 if (somethingWasAlreadyOpen
) {
232 openNewTab(primaryUrl
);
234 openNewActivatedTab(primaryUrl
);
240 void DolphinTabWidget::openFiles(const QList
<QUrl
> &files
, bool splitView
)
242 Q_ASSERT(files
.size() > 0);
244 // Get all distinct directories from 'files'.
245 QList
<QUrl
> dirsThatNeedToBeOpened
;
246 QList
<QUrl
> dirsThatWereAlreadyOpen
;
247 for (const QUrl
&file
: files
) {
248 const QUrl
dir(file
.adjusted(QUrl::RemoveFilename
| QUrl::StripTrailingSlash
));
249 if (dirsThatNeedToBeOpened
.contains(dir
) || dirsThatWereAlreadyOpen
.contains(dir
)) {
253 // The selecting of files that we do later will not work in views that already have items selected.
254 // So we check if dir is already open and clear the selection if it is. BUG: 417230
255 // We also make sure the view will be activated.
256 auto viewIndex
= viewShowingItem(file
);
257 if (viewIndex
.has_value()) {
258 viewContainerAt(viewIndex
.value())->view()->clearSelection();
259 activateViewContainerAt(viewIndex
.value());
260 dirsThatWereAlreadyOpen
.append(dir
);
262 dirsThatNeedToBeOpened
.append(dir
);
266 const int oldTabCount
= count();
267 // Open a tab for each directory. If the "split view" option is enabled,
268 // two directories are shown inside one tab (see openDirectories()).
269 if (dirsThatNeedToBeOpened
.size() > 0) {
270 openDirectories(dirsThatNeedToBeOpened
, splitView
);
272 const int tabCount
= count();
274 // Select the files. Although the files can be split between several
275 // tabs, there is no need to split 'files' accordingly, as
276 // the DolphinView will just ignore invalid selections.
277 for (int i
= 0; i
< tabCount
; ++i
) {
278 DolphinTabPage
*tabPage
= tabPageAt(i
);
279 tabPage
->markUrlsAsSelected(files
);
280 tabPage
->markUrlAsCurrent(files
.first());
281 if (i
< oldTabCount
) {
282 // Force selection of file if directory was already open, BUG: 417230
283 tabPage
->activeViewContainer()->view()->updateViewState();
288 void DolphinTabWidget::closeTab()
290 closeTab(currentIndex());
293 void DolphinTabWidget::closeTab(const int index
)
295 Q_ASSERT(index
>= 0);
296 Q_ASSERT(index
< count());
299 // Close Dolphin when closing the last tab.
300 parentWidget()->close();
304 DolphinTabPage
*tabPage
= tabPageAt(index
);
305 Q_EMIT
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
308 tabPage
->deleteLater();
311 void DolphinTabWidget::activateTab(const int index
)
313 if (index
< count()) {
314 setCurrentIndex(index
);
318 void DolphinTabWidget::activateLastTab()
320 setCurrentIndex(count() - 1);
323 void DolphinTabWidget::activateNextTab()
325 const int index
= currentIndex() + 1;
326 setCurrentIndex(index
< count() ? index
: 0);
329 void DolphinTabWidget::activatePrevTab()
331 const int index
= currentIndex() - 1;
332 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
335 void DolphinTabWidget::restoreClosedTab(const QByteArray
&state
)
337 openNewActivatedTab();
338 currentTabPage()->restoreState(state
);
341 void DolphinTabWidget::copyToInactiveSplitView()
343 const DolphinTabPage
*tabPage
= currentTabPage();
344 if (!tabPage
->splitViewEnabled()) {
348 const KFileItemList selectedItems
= tabPage
->activeViewContainer()->view()->selectedItems();
349 if (selectedItems
.isEmpty()) {
353 DolphinView
*const inactiveView
= tabPage
->inactiveViewContainer()->view();
354 inactiveView
->copySelectedItems(selectedItems
, inactiveView
->url());
357 void DolphinTabWidget::moveToInactiveSplitView()
359 const DolphinTabPage
*tabPage
= currentTabPage();
360 if (!tabPage
->splitViewEnabled()) {
364 const KFileItemList selectedItems
= tabPage
->activeViewContainer()->view()->selectedItems();
365 if (selectedItems
.isEmpty()) {
369 DolphinView
*const inactiveView
= tabPage
->inactiveViewContainer()->view();
370 inactiveView
->moveSelectedItems(selectedItems
, inactiveView
->url());
373 void DolphinTabWidget::detachTab(int index
)
375 Q_ASSERT(index
>= 0);
379 const DolphinTabPage
*tabPage
= tabPageAt(index
);
380 args
<< tabPage
->primaryViewContainer()->url().url();
381 if (tabPage
->splitViewEnabled()) {
382 args
<< tabPage
->secondaryViewContainer()->url().url();
383 args
<< QStringLiteral("--split");
385 args
<< QStringLiteral("--new-window");
387 KIO::CommandLauncherJob
*job
= new KIO::CommandLauncherJob("dolphin", args
, this);
388 job
->setDesktopName(QStringLiteral("org.kde.dolphin"));
394 void DolphinTabWidget::openNewActivatedTab(int index
)
396 Q_ASSERT(index
>= 0);
397 const DolphinTabPage
*tabPage
= tabPageAt(index
);
398 openNewActivatedTab(tabPage
->activeViewContainer()->url());
401 void DolphinTabWidget::tabDragMoveEvent(int index
, QDragMoveEvent
*event
)
404 DolphinView
*view
= tabPageAt(index
)->activeViewContainer()->view();
405 DragAndDropHelper::updateDropAction(event
, view
->url());
409 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
*event
)
412 DolphinView
*view
= tabPageAt(index
)->activeViewContainer()->view();
413 view
->dropUrls(view
->url(), event
, view
);
415 const auto urls
= event
->mimeData()->urls();
417 for (const QUrl
&url
: urls
) {
418 auto *job
= KIO::stat(url
, KIO::StatJob::SourceSide
, KIO::StatDetail::StatBasic
, KIO::JobFlag::HideProgressInfo
);
419 connect(job
, &KJob::result
, this, [this, job
]() {
420 if (!job
->error() && job
->statResult().isDir()) {
421 openNewTab(job
->url(), QUrl(), NewTabPosition::AtEnd
);
428 void DolphinTabWidget::tabUrlChanged(const QUrl
&url
)
430 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
432 updateTabName(index
);
433 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
434 if (tabBar()->isVisible()) {
435 // ensure the path url ends with a slash to have proper folder icon for remote folders
436 const QUrl pathUrl
= QUrl(url
.adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/"));
437 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl
)));
439 // Mark as dirty, actually load once the tab bar actually gets shown
440 tabBar()->setTabIcon(index
, QIcon());
443 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
444 if (index
== currentIndex()) {
445 Q_EMIT
currentUrlChanged(url
);
448 Q_EMIT
urlChanged(url
);
452 void DolphinTabWidget::currentTabChanged(int index
)
454 DolphinTabPage
*tabPage
= tabPageAt(index
);
455 if (tabPage
== m_lastViewedTab
) {
458 if (m_lastViewedTab
) {
459 m_lastViewedTab
->disconnectNavigators();
460 m_lastViewedTab
->setActive(false);
462 if (tabPage
->splitViewEnabled() && !m_navigatorsWidget
->secondaryUrlNavigator()) {
463 m_navigatorsWidget
->createSecondaryUrlNavigator();
465 DolphinViewContainer
*viewContainer
= tabPage
->activeViewContainer();
466 Q_EMIT
activeViewChanged(viewContainer
);
467 Q_EMIT
currentUrlChanged(viewContainer
->url());
468 tabPage
->setActive(true);
469 tabPage
->connectNavigators(m_navigatorsWidget
);
470 m_navigatorsWidget
->setSecondaryNavigatorVisible(tabPage
->splitViewEnabled());
471 m_lastViewedTab
= tabPage
;
474 void DolphinTabWidget::tabInserted(int index
)
476 QTabWidget::tabInserted(index
);
478 if (tabBar()->isVisible()) {
479 // Resolve all pending tab icons
480 for (int i
= 0; i
< count(); ++i
) {
481 const QUrl url
= tabPageAt(i
)->activeViewContainer()->url();
482 if (tabBar()->tabIcon(i
).isNull()) {
483 // ensure the path url ends with a slash to have proper folder icon for remote folders
484 const QUrl pathUrl
= QUrl(url
.adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/"));
485 tabBar()->setTabIcon(i
, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl
)));
487 if (tabBar()->tabToolTip(i
).isEmpty()) {
488 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
493 Q_EMIT
tabCountChanged(count());
496 void DolphinTabWidget::tabRemoved(int index
)
498 QTabWidget::tabRemoved(index
);
500 Q_EMIT
tabCountChanged(count());
503 QString
DolphinTabWidget::tabName(DolphinTabPage
*tabPage
) const
510 if (tabPage
->splitViewEnabled()) {
511 if (tabPage
->primaryViewActive()) {
512 // i18n: %1 is the primary view and %2 the secondary view. For left to right languages the primary view is on the left so we also want it to be on the
513 // left in the tab name. In right to left languages the primary view would be on the right so the tab name should match.
514 name
= i18nc("@title:tab Active primary view | (Inactive secondary view)", "%1 | (%2)", tabPage
->primaryViewContainer()->caption(), tabPage
->secondaryViewContainer()->caption());
516 // i18n: %1 is the primary view and %2 the secondary view. For left to right languages the primary view is on the left so we also want it to be on the
517 // left in the tab name. In right to left languages the primary view would be on the right so the tab name should match.
518 name
= i18nc("@title:tab (Inactive primary view) | Active secondary view", "(%1) | %2", tabPage
->primaryViewContainer()->caption(), tabPage
->secondaryViewContainer()->caption());
521 name
= tabPage
->activeViewContainer()->caption();
525 // Make sure that a '&' inside the directory name is displayed correctly
526 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
527 return KStringHandler::rsqueeze(name
.replace('&', QLatin1String("&&")), 40 /* default maximum visible folder name visible */);
530 DolphinViewContainer
*DolphinTabWidget::viewContainerAt(DolphinTabWidget::ViewIndex viewIndex
) const
532 const auto tabPage
= tabPageAt(viewIndex
.tabIndex
);
536 return viewIndex
.isInPrimaryView
? tabPage
->primaryViewContainer() : tabPage
->secondaryViewContainer();
539 DolphinViewContainer
*DolphinTabWidget::activateViewContainerAt(DolphinTabWidget::ViewIndex viewIndex
)
541 activateTab(viewIndex
.tabIndex
);
542 auto viewContainer
= viewContainerAt(viewIndex
);
543 if (!viewContainer
) {
546 viewContainer
->setActive(true);
547 return viewContainer
;
550 const std::optional
<const DolphinTabWidget::ViewIndex
> DolphinTabWidget::viewOpenAtDirectory(const QUrl
&directory
) const
552 int i
= currentIndex();
556 // loop over the tabs starting from the current one
558 const auto tabPage
= tabPageAt(i
);
559 if (tabPage
->primaryViewContainer()->url() == directory
) {
560 return std::optional(ViewIndex
{i
, true});
563 if (tabPage
->splitViewEnabled() && tabPage
->secondaryViewContainer()->url() == directory
) {
564 return std::optional(ViewIndex
{i
, false});
567 i
= (i
+ 1) % count();
568 } while (i
!= currentIndex());
573 const std::optional
<const DolphinTabWidget::ViewIndex
> DolphinTabWidget::viewShowingItem(const QUrl
&item
) const
575 // The item might not be loaded yet even though it exists. So instead
576 // we check if the folder containing the item is showing its contents.
577 const QUrl
dirContainingItem(item
.adjusted(QUrl::RemoveFilename
| QUrl::StripTrailingSlash
));
579 // The dirContainingItem is either open directly or expanded in a tree-style view mode.
580 // Is dirContainingitem the base url of a view?
581 auto viewOpenAtContainingDirectory
= viewOpenAtDirectory(dirContainingItem
);
582 if (viewOpenAtContainingDirectory
.has_value()) {
583 return viewOpenAtContainingDirectory
;
586 // Is dirContainingItem expanded in some tree-style view?
587 // The rest of this method is about figuring this out.
589 int i
= currentIndex();
593 // loop over the tabs starting from the current one
595 const auto tabPage
= tabPageAt(i
);
596 if (tabPage
->primaryViewContainer()->url().isParentOf(item
)) {
597 const KFileItem fileItemContainingItem
= tabPage
->primaryViewContainer()->view()->items().findByUrl(dirContainingItem
);
598 if (!fileItemContainingItem
.isNull() && tabPage
->primaryViewContainer()->view()->isExpanded(fileItemContainingItem
)) {
599 return std::optional(ViewIndex
{i
, true});
603 if (tabPage
->splitViewEnabled() && tabPage
->secondaryViewContainer()->url().isParentOf(item
)) {
604 const KFileItem fileItemContainingItem
= tabPage
->secondaryViewContainer()->view()->items().findByUrl(dirContainingItem
);
605 if (!fileItemContainingItem
.isNull() && tabPage
->secondaryViewContainer()->view()->isExpanded(fileItemContainingItem
)) {
606 return std::optional(ViewIndex
{i
, false});
610 i
= (i
+ 1) % count();
611 } while (i
!= currentIndex());
616 #include "moc_dolphintabwidget.cpp"