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"
13 #include <KConfigGroup>
15 #include <kio/global.h>
16 #include <KIO/CommandLauncherJob>
17 #include <KAcceleratorManager>
19 #include <QApplication>
22 DolphinTabWidget::DolphinTabWidget(DolphinNavigatorsWidgetAction
*navigatorsWidget
, QWidget
* parent
) :
24 m_lastViewedTab(nullptr),
25 m_navigatorsWidget
{navigatorsWidget
}
27 KAcceleratorManager::setNoAccel(this);
29 connect(this, &DolphinTabWidget::tabCloseRequested
,
30 this, QOverload
<int>::of(&DolphinTabWidget::closeTab
));
31 connect(this, &DolphinTabWidget::currentChanged
,
32 this, &DolphinTabWidget::currentTabChanged
);
34 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
35 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
36 this, QOverload
<int>::of(&DolphinTabWidget::openNewActivatedTab
));
37 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
38 this, &DolphinTabWidget::tabDropEvent
);
39 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
40 this, &DolphinTabWidget::detachTab
);
44 setDocumentMode(true);
45 setElideMode(Qt::ElideRight
);
46 setUsesScrollButtons(true);
49 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
51 return tabPageAt(currentIndex());
54 DolphinTabPage
* DolphinTabWidget::nextTabPage() const
56 const int index
= currentIndex() + 1;
57 return tabPageAt(index
< count() ? index
: 0);
60 DolphinTabPage
* DolphinTabWidget::prevTabPage() const
62 const int index
= currentIndex() - 1;
63 return tabPageAt(index
>= 0 ? index
: (count() - 1));
66 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
68 return static_cast<DolphinTabPage
*>(widget(index
));
71 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
73 const int tabCount
= count();
74 group
.writeEntry("Tab Count", tabCount
);
75 group
.writeEntry("Active Tab Index", currentIndex());
77 for (int i
= 0; i
< tabCount
; ++i
) {
78 const DolphinTabPage
* tabPage
= tabPageAt(i
);
79 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
83 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
85 const int tabCount
= group
.readEntry("Tab Count", 0);
86 for (int i
= 0; i
< tabCount
; ++i
) {
88 openNewActivatedTab();
90 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
91 tabPageAt(i
)->restoreState(state
);
94 const int index
= group
.readEntry("Active Tab Index", 0);
95 setCurrentIndex(index
);
98 void DolphinTabWidget::refreshViews()
100 // Left-elision is better when showing full paths, since you care most
101 // about the current directory which is on the right
102 if (GeneralSettings::showFullPathInTitlebar()) {
103 setElideMode(Qt::ElideLeft
);
105 setElideMode(Qt::ElideRight
);
108 const int tabCount
= count();
109 for (int i
= 0; i
< tabCount
; ++i
) {
110 tabBar()->setTabText(i
, tabName(tabPageAt(i
)));
111 tabPageAt(i
)->refreshViews();
115 bool DolphinTabWidget::isUrlOpen(const QUrl
&url
) const
117 return indexByUrl(url
).first
>= 0;
120 void DolphinTabWidget::openNewActivatedTab()
122 std::unique_ptr
<DolphinUrlNavigator::VisualState
> oldNavigatorState
;
123 if (currentTabPage()->primaryViewActive() || !m_navigatorsWidget
->secondaryUrlNavigator()) {
124 oldNavigatorState
= m_navigatorsWidget
->primaryUrlNavigator()->visualState();
126 oldNavigatorState
= m_navigatorsWidget
->secondaryUrlNavigator()->visualState();
129 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
130 Q_ASSERT(oldActiveViewContainer
);
132 openNewActivatedTab(oldActiveViewContainer
->url());
134 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
135 Q_ASSERT(newActiveViewContainer
);
137 // The URL navigator of the new tab should have the same editable state
138 // as the current tab
139 newActiveViewContainer
->urlNavigator()->setVisualState(*oldNavigatorState
.get());
141 // Always focus the new tab's view
142 newActiveViewContainer
->view()->setFocus();
145 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
147 openNewTab(primaryUrl
, secondaryUrl
);
148 if (GeneralSettings::openNewTabAfterLastTab()) {
149 setCurrentIndex(count() - 1);
151 setCurrentIndex(currentIndex() + 1);
155 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
157 QWidget
* focusWidget
= QApplication::focusWidget();
159 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
160 tabPage
->setActive(false);
161 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
162 this, &DolphinTabWidget::activeViewChanged
);
163 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
164 this, &DolphinTabWidget::tabUrlChanged
);
165 int newTabIndex
= -1;
166 if (!GeneralSettings::openNewTabAfterLastTab()) {
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 bool somethingWasAlreadyOpen
= false;
184 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
185 while (it
!= dirs
.constEnd()) {
186 const QUrl
& primaryUrl
= *(it
++);
187 const QPair
<int, bool> indexInfo
= indexByUrl(primaryUrl
);
188 const int index
= indexInfo
.first
;
189 const bool isInPrimaryView
= indexInfo
.second
;
191 // When the user asks for a URL that's already open, activate it instead
192 // of opening a second copy
194 somethingWasAlreadyOpen
= true;
196 const auto tabPage
= tabPageAt(index
);
197 if (isInPrimaryView
) {
198 tabPage
->primaryViewContainer()->setActive(true);
200 tabPage
->secondaryViewContainer()->setActive(true);
203 // Required for updateViewState() call in openFiles() to work as expected
204 // If there is a selection, updateViewState() calls are effectively a no-op
205 tabPage
->activeViewContainer()->view()->clearSelection();
206 } else if (splitView
&& (it
!= dirs
.constEnd())) {
207 const QUrl
& secondaryUrl
= *(it
++);
208 if (somethingWasAlreadyOpen
) {
209 openNewTab(primaryUrl
, secondaryUrl
);
211 openNewActivatedTab(primaryUrl
, secondaryUrl
);
214 if (somethingWasAlreadyOpen
) {
215 openNewTab(primaryUrl
);
217 openNewActivatedTab(primaryUrl
);
223 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
225 Q_ASSERT(files
.size() > 0);
227 // Get all distinct directories from 'files' and open a tab
228 // for each directory. If the "split view" option is enabled, two
229 // directories are shown inside one tab (see openDirectories()).
231 for (const QUrl
& url
: files
) {
232 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
233 if (!dirs
.contains(dir
)) {
238 const int oldTabCount
= count();
239 openDirectories(dirs
, splitView
);
240 const int tabCount
= count();
242 // Select the files. Although the files can be split between several
243 // tabs, there is no need to split 'files' accordingly, as
244 // the DolphinView will just ignore invalid selections.
245 for (int i
= 0; i
< tabCount
; ++i
) {
246 DolphinTabPage
* tabPage
= tabPageAt(i
);
247 tabPage
->markUrlsAsSelected(files
);
248 tabPage
->markUrlAsCurrent(files
.first());
249 if (i
< oldTabCount
) {
250 // Force selection of file if directory was already open, BUG: 417230
251 tabPage
->activeViewContainer()->view()->updateViewState();
256 void DolphinTabWidget::closeTab()
258 closeTab(currentIndex());
261 void DolphinTabWidget::closeTab(const int index
)
263 Q_ASSERT(index
>= 0);
264 Q_ASSERT(index
< count());
267 // Close Dolphin when closing the last tab.
268 parentWidget()->close();
272 DolphinTabPage
* tabPage
= tabPageAt(index
);
273 Q_EMIT
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
276 tabPage
->deleteLater();
279 void DolphinTabWidget::activateTab(const int index
)
281 if (index
< count()) {
282 setCurrentIndex(index
);
286 void DolphinTabWidget::activateLastTab()
288 setCurrentIndex(count() - 1);
291 void DolphinTabWidget::activateNextTab()
293 const int index
= currentIndex() + 1;
294 setCurrentIndex(index
< count() ? index
: 0);
297 void DolphinTabWidget::activatePrevTab()
299 const int index
= currentIndex() - 1;
300 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
303 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
305 openNewActivatedTab();
306 currentTabPage()->restoreState(state
);
309 void DolphinTabWidget::copyToInactiveSplitView()
311 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
312 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
313 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
317 if (tabPage
->primaryViewActive()) {
318 // copy from left panel to right
319 activeViewContainer
->view()->copySelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
321 // copy from right panel to left
322 activeViewContainer
->view()->copySelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
326 void DolphinTabWidget::moveToInactiveSplitView()
328 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
329 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
330 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
334 if (tabPage
->primaryViewActive()) {
335 // move from left panel to right
336 activeViewContainer
->view()->moveSelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
338 // move from right panel to left
339 activeViewContainer
->view()->moveSelectedItems(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
343 void DolphinTabWidget::detachTab(int index
)
345 Q_ASSERT(index
>= 0);
349 const DolphinTabPage
* tabPage
= tabPageAt(index
);
350 args
<< tabPage
->primaryViewContainer()->url().url();
351 if (tabPage
->splitViewEnabled()) {
352 args
<< tabPage
->secondaryViewContainer()->url().url();
353 args
<< QStringLiteral("--split");
355 args
<< QStringLiteral("--new-window");
357 KIO::CommandLauncherJob
*job
= new KIO::CommandLauncherJob("dolphin", args
, this);
358 job
->setDesktopName(QStringLiteral("org.kde.dolphin"));
364 void DolphinTabWidget::openNewActivatedTab(int index
)
366 Q_ASSERT(index
>= 0);
367 const DolphinTabPage
* tabPage
= tabPageAt(index
);
368 openNewActivatedTab(tabPage
->activeViewContainer()->url());
371 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
374 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
375 view
->dropUrls(view
->url(), event
, view
);
379 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
381 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
383 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
384 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
385 if (tabBar()->isVisible()) {
386 // ensure the path url ends with a slash to have proper folder icon for remote folders
387 const QUrl pathUrl
= QUrl(url
.adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/"));
388 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl
)));
390 // Mark as dirty, actually load once the tab bar actually gets shown
391 tabBar()->setTabIcon(index
, QIcon());
394 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
395 if (index
== currentIndex()) {
396 Q_EMIT
currentUrlChanged(url
);
401 void DolphinTabWidget::currentTabChanged(int index
)
403 DolphinTabPage
*tabPage
= tabPageAt(index
);
404 if (tabPage
== m_lastViewedTab
) {
407 if (m_lastViewedTab
) {
408 m_lastViewedTab
->disconnectNavigators();
409 m_lastViewedTab
->setActive(false);
411 if (tabPage
->splitViewEnabled() && !m_navigatorsWidget
->secondaryUrlNavigator()) {
412 m_navigatorsWidget
->createSecondaryUrlNavigator();
414 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
415 Q_EMIT
activeViewChanged(viewContainer
);
416 Q_EMIT
currentUrlChanged(viewContainer
->url());
417 tabPage
->setActive(true);
418 tabPage
->connectNavigators(m_navigatorsWidget
);
419 m_navigatorsWidget
->setSecondaryNavigatorVisible(tabPage
->splitViewEnabled());
420 m_lastViewedTab
= tabPage
;
423 void DolphinTabWidget::tabInserted(int index
)
425 QTabWidget::tabInserted(index
);
428 // Resolve all pending tab icons
429 for (int i
= 0; i
< count(); ++i
) {
430 const QUrl url
= tabPageAt(i
)->activeViewContainer()->url();
431 if (tabBar()->tabIcon(i
).isNull()) {
432 // ensure the path url ends with a slash to have proper folder icon for remote folders
433 const QUrl pathUrl
= QUrl(url
.adjusted(QUrl::StripTrailingSlash
).toString(QUrl::FullyEncoded
).append("/"));
434 tabBar()->setTabIcon(i
, QIcon::fromTheme(KIO::iconNameForUrl(pathUrl
)));
436 if (tabBar()->tabToolTip(i
).isEmpty()) {
437 tabBar()->setTabToolTip(index
, url
.toDisplayString(QUrl::PreferLocalFile
));
444 Q_EMIT
tabCountChanged(count());
447 void DolphinTabWidget::tabRemoved(int index
)
449 QTabWidget::tabRemoved(index
);
451 // If only one tab is left, then remove the tab entry so that
452 // closing the last tab is not possible.
457 Q_EMIT
tabCountChanged(count());
460 QString
DolphinTabWidget::tabName(DolphinTabPage
* tabPage
) const
465 QString name
= tabPage
->activeViewContainer()->caption();
466 // Make sure that a '&' inside the directory name is displayed correctly
467 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
468 return name
.replace('&', QLatin1String("&&"));
471 QPair
<int, bool> DolphinTabWidget::indexByUrl(const QUrl
& url
) const
473 for (int i
= 0; i
< count(); i
++) {
474 const auto tabPage
= tabPageAt(i
);
475 if (url
== tabPage
->primaryViewContainer()->url()) {
476 return qMakePair(i
, true);
479 if (tabPage
->splitViewEnabled() && url
== tabPage
->secondaryViewContainer()->url()) {
480 return qMakePair(i
, false);
483 return qMakePair(-1, false);