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 "dolphintabpage.h"
12 #include "dolphinviewcontainer.h"
14 #include <KConfigGroup>
16 #include <kio/global.h>
17 #include <KIO/CommandLauncherJob>
18 #include <KAcceleratorManager>
20 #include <QApplication>
23 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
25 m_placesSelectorVisible(true),
28 KAcceleratorManager::setNoAccel(this);
30 connect(this, &DolphinTabWidget::tabCloseRequested
,
31 this, QOverload
<int>::of(&DolphinTabWidget::closeTab
));
32 connect(this, &DolphinTabWidget::currentChanged
,
33 this, &DolphinTabWidget::currentTabChanged
);
35 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
36 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
37 this, QOverload
<int>::of(&DolphinTabWidget::openNewActivatedTab
));
38 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
39 this, &DolphinTabWidget::tabDropEvent
);
40 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
41 this, &DolphinTabWidget::detachTab
);
45 setDocumentMode(true);
46 setElideMode(Qt::ElideRight
);
47 setUsesScrollButtons(true);
50 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
52 return tabPageAt(currentIndex());
55 DolphinTabPage
* DolphinTabWidget::nextTabPage() const
57 const int index
= currentIndex() + 1;
58 return tabPageAt(index
< count() ? index
: 0);
61 DolphinTabPage
* DolphinTabWidget::prevTabPage() const
63 const int index
= currentIndex() - 1;
64 return tabPageAt(index
>= 0 ? index
: (count() - 1));
67 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
69 return static_cast<DolphinTabPage
*>(widget(index
));
72 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
74 const int tabCount
= count();
75 group
.writeEntry("Tab Count", tabCount
);
76 group
.writeEntry("Active Tab Index", currentIndex());
78 for (int i
= 0; i
< tabCount
; ++i
) {
79 const DolphinTabPage
* tabPage
= tabPageAt(i
);
80 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
84 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
86 const int tabCount
= group
.readEntry("Tab Count", 0);
87 for (int i
= 0; i
< tabCount
; ++i
) {
89 openNewActivatedTab();
91 if (group
.hasKey("Tab Data " % QString::number(i
))) {
92 // Tab state created with Dolphin > 4.14.x
93 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
94 tabPageAt(i
)->restoreState(state
);
96 // Tab state created with Dolphin <= 4.14.x
97 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
98 tabPageAt(i
)->restoreStateV1(state
);
102 const int index
= group
.readEntry("Active Tab Index", 0);
103 setCurrentIndex(index
);
106 void DolphinTabWidget::refreshViews()
108 // Left-elision is better when showing full paths, since you care most
109 // about the current directory which is on the right
110 if (GeneralSettings::showFullPathInTitlebar()) {
111 setElideMode(Qt::ElideLeft
);
113 setElideMode(Qt::ElideRight
);
116 const int tabCount
= count();
117 for (int i
= 0; i
< tabCount
; ++i
) {
118 tabBar()->setTabText(i
, tabName(tabPageAt(i
)));
119 tabPageAt(i
)->refreshViews();
123 bool DolphinTabWidget::isUrlOpen(const QUrl
&url
) const
125 return indexByUrl(url
).first
>= 0;
128 void DolphinTabWidget::openNewActivatedTab()
130 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
131 Q_ASSERT(oldActiveViewContainer
);
133 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
135 openNewActivatedTab(oldActiveViewContainer
->url());
137 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
138 Q_ASSERT(newActiveViewContainer
);
140 // The URL navigator of the new tab should have the same editable state
141 // as the current tab
142 newActiveViewContainer
->urlNavigator()->setUrlEditable(isUrlEditable
);
144 // Always focus the new tab's view
145 newActiveViewContainer
->view()->setFocus();
148 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
150 openNewTab(primaryUrl
, secondaryUrl
);
151 setCurrentIndex(count() - 1);
154 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
, TabPlacement tabPlacement
)
156 QWidget
* focusWidget
= QApplication::focusWidget();
158 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
159 tabPage
->setActive(false);
160 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
161 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
162 this, &DolphinTabWidget::activeViewChanged
);
163 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
164 this, &DolphinTabWidget::tabUrlChanged
);
165 int newTabIndex
= -1;
166 if (tabPlacement
== AfterCurrentTab
) {
167 newTabIndex
= currentIndex() + 1;
169 insertTab(newTabIndex
, tabPage
, QIcon() /* loaded in tabInserted */, tabName(tabPage
));
172 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
173 // in background, assure that the previous focused widget gets the focus back.
174 focusWidget
->setFocus();
178 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
180 Q_ASSERT(dirs
.size() > 0);
182 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
183 while (it
!= dirs
.constEnd()) {
184 const QUrl
& primaryUrl
= *(it
++);
185 const QPair
<int, bool> indexInfo
= indexByUrl(primaryUrl
);
186 const int index
= indexInfo
.first
;
187 const bool isInPrimaryView
= indexInfo
.second
;
189 setCurrentIndex(index
);
190 const auto tabPage
= tabPageAt(index
);
191 if (isInPrimaryView
) {
192 tabPage
->primaryViewContainer()->setActive(true);
194 tabPage
->secondaryViewContainer()->setActive(true);
197 // Required for updateViewState() call in openFiles() to work as expected
198 // If there is a selection, updateViewState() calls are effectively a no-op
199 tabPage
->activeViewContainer()->view()->clearSelection();
202 if (splitView
&& (it
!= dirs
.constEnd())) {
203 const QUrl
& secondaryUrl
= *(it
++);
204 openNewActivatedTab(primaryUrl
, secondaryUrl
);
206 openNewActivatedTab(primaryUrl
);
211 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
213 Q_ASSERT(files
.size() > 0);
215 // Get all distinct directories from 'files' and open a tab
216 // for each directory. If the "split view" option is enabled, two
217 // directories are shown inside one tab (see openDirectories()).
219 foreach (const QUrl
& url
, files
) {
220 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
221 if (!dirs
.contains(dir
)) {
226 const int oldTabCount
= count();
227 openDirectories(dirs
, splitView
);
228 const int tabCount
= count();
230 // Select the files. Although the files can be split between several
231 // tabs, there is no need to split 'files' accordingly, as
232 // the DolphinView will just ignore invalid selections.
233 for (int i
= 0; i
< tabCount
; ++i
) {
234 DolphinTabPage
* tabPage
= tabPageAt(i
);
235 tabPage
->markUrlsAsSelected(files
);
236 tabPage
->markUrlAsCurrent(files
.first());
237 if (i
< oldTabCount
) {
238 // Force selection of file if directory was already open, BUG: 417230
239 tabPage
->activeViewContainer()->view()->updateViewState();
244 void DolphinTabWidget::closeTab()
246 closeTab(currentIndex());
249 void DolphinTabWidget::closeTab(const int index
)
251 Q_ASSERT(index
>= 0);
252 Q_ASSERT(index
< count());
255 // Close Dolphin when closing the last tab.
256 parentWidget()->close();
260 DolphinTabPage
* tabPage
= tabPageAt(index
);
261 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
264 tabPage
->deleteLater();
267 void DolphinTabWidget::activateTab(const int index
)
269 if (index
< count()) {
270 setCurrentIndex(index
);
274 void DolphinTabWidget::activateLastTab()
276 setCurrentIndex(count() - 1);
279 void DolphinTabWidget::activateNextTab()
281 const int index
= currentIndex() + 1;
282 setCurrentIndex(index
< count() ? index
: 0);
285 void DolphinTabWidget::activatePrevTab()
287 const int index
= currentIndex() - 1;
288 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
291 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
293 // The places-selector from the URL navigator should only be shown
294 // if the places dock is invisible
295 m_placesSelectorVisible
= !visible
;
297 const int tabCount
= count();
298 for (int i
= 0; i
< tabCount
; ++i
) {
299 DolphinTabPage
* tabPage
= tabPageAt(i
);
300 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
304 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
306 openNewActivatedTab();
307 currentTabPage()->restoreState(state
);
310 void DolphinTabWidget::copyToInactiveSplitView()
312 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
313 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
314 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
318 if (tabPage
->primaryViewActive()) {
319 // copy from left panel to right
320 activeViewContainer
->view()->copySelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
322 // copy from right panel to left
323 activeViewContainer
->view()->copySelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
327 void DolphinTabWidget::moveToInactiveSplitView()
329 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
330 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
331 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
335 if (tabPage
->primaryViewActive()) {
336 // move from left panel to right
337 activeViewContainer
->view()->moveSelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
339 // move from right panel to left
340 activeViewContainer
->view()->moveSelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
344 void DolphinTabWidget::detachTab(int index
)
346 Q_ASSERT(index
>= 0);
350 const DolphinTabPage
* tabPage
= tabPageAt(index
);
351 args
<< tabPage
->primaryViewContainer()->url().url();
352 if (tabPage
->splitViewEnabled()) {
353 args
<< tabPage
->secondaryViewContainer()->url().url();
354 args
<< QStringLiteral("--split");
356 args
<< QStringLiteral("--new-window");
358 KIO::CommandLauncherJob
*job
= new KIO::CommandLauncherJob("dolphin", args
, this);
359 job
->setDesktopName(QStringLiteral("org.kde.dolphin"));
365 void DolphinTabWidget::openNewActivatedTab(int index
)
367 Q_ASSERT(index
>= 0);
368 const DolphinTabPage
* tabPage
= tabPageAt(index
);
369 openNewActivatedTab(tabPage
->activeViewContainer()->url());
372 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
375 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
376 view
->dropUrls(view
->url(), event
, view
);
380 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
382 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
384 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
385 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
386 if (tabBar()->isVisible()) {
387 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
389 // Mark as dirty, actually load once the tab bar actually gets shown
390 tabBar()->setTabIcon(index
, QIcon());
393 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
394 if (index
== currentIndex()) {
395 emit
currentUrlChanged(url
);
400 void DolphinTabWidget::currentTabChanged(int index
)
402 // last-viewed tab deactivation
403 if (DolphinTabPage
* tabPage
= tabPageAt(m_lastViewedTab
)) {
404 tabPage
->setActive(false);
406 DolphinTabPage
* tabPage
= tabPageAt(index
);
407 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
408 emit
activeViewChanged(viewContainer
);
409 emit
currentUrlChanged(viewContainer
->url());
410 tabPage
->setActive(true);
411 m_lastViewedTab
= index
;
414 void DolphinTabWidget::tabInserted(int index
)
416 QTabWidget::tabInserted(index
);
419 // Resolve all pending tab icons
420 for (int i
= 0; i
< count(); ++i
) {
421 const QUrl url
= tabPageAt(i
)->activeViewContainer()->url();
422 if (tabBar()->tabIcon(i
).isNull()) {
423 tabBar()->setTabIcon(i
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
425 if (tabBar()->tabToolTip(i
).isEmpty()) {
426 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
433 emit
tabCountChanged(count());
436 void DolphinTabWidget::tabRemoved(int index
)
438 QTabWidget::tabRemoved(index
);
440 // If only one tab is left, then remove the tab entry so that
441 // closing the last tab is not possible.
446 emit
tabCountChanged(count());
449 QString
DolphinTabWidget::tabName(DolphinTabPage
* tabPage
) const
454 QString name
= tabPage
->activeViewContainer()->caption();
455 // Make sure that a '&' inside the directory name is displayed correctly
456 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
457 return name
.replace('&', QLatin1String("&&"));
460 QPair
<int, bool> DolphinTabWidget::indexByUrl(const QUrl
& url
) const
462 for (int i
= 0; i
< count(); i
++) {
463 const auto tabPage
= tabPageAt(i
);
464 if (url
== tabPage
->primaryViewContainer()->url()) {
465 return qMakePair(i
, true);
468 if (tabPage
->splitViewEnabled() && url
== tabPage
->secondaryViewContainer()->url()) {
469 return qMakePair(i
, false);
472 return qMakePair(-1, false);