1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphintabwidget.h"
22 #include "dolphin_generalsettings.h"
23 #include "dolphintabbar.h"
24 #include "dolphintabpage.h"
25 #include "dolphinviewcontainer.h"
27 #include <KConfigGroup>
29 #include <kio/global.h>
30 #include <KIO/CommandLauncherJob>
31 #include <KAcceleratorManager>
33 #include <QApplication>
36 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
38 m_placesSelectorVisible(true),
41 KAcceleratorManager::setNoAccel(this);
43 connect(this, &DolphinTabWidget::tabCloseRequested
,
44 this, QOverload
<int>::of(&DolphinTabWidget::closeTab
));
45 connect(this, &DolphinTabWidget::currentChanged
,
46 this, &DolphinTabWidget::currentTabChanged
);
48 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
49 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
50 this, QOverload
<int>::of(&DolphinTabWidget::openNewActivatedTab
));
51 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
52 this, &DolphinTabWidget::tabDropEvent
);
53 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
54 this, &DolphinTabWidget::detachTab
);
58 setDocumentMode(true);
59 setElideMode(Qt::ElideRight
);
60 setUsesScrollButtons(true);
63 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
65 return tabPageAt(currentIndex());
68 DolphinTabPage
* DolphinTabWidget::nextTabPage() const
70 const int index
= currentIndex() + 1;
71 return tabPageAt(index
< count() ? index
: 0);
74 DolphinTabPage
* DolphinTabWidget::prevTabPage() const
76 const int index
= currentIndex() - 1;
77 return tabPageAt(index
>= 0 ? index
: (count() - 1));
80 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
82 return static_cast<DolphinTabPage
*>(widget(index
));
85 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
87 const int tabCount
= count();
88 group
.writeEntry("Tab Count", tabCount
);
89 group
.writeEntry("Active Tab Index", currentIndex());
91 for (int i
= 0; i
< tabCount
; ++i
) {
92 const DolphinTabPage
* tabPage
= tabPageAt(i
);
93 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
97 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
99 const int tabCount
= group
.readEntry("Tab Count", 0);
100 for (int i
= 0; i
< tabCount
; ++i
) {
102 openNewActivatedTab();
104 if (group
.hasKey("Tab Data " % QString::number(i
))) {
105 // Tab state created with Dolphin > 4.14.x
106 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
107 tabPageAt(i
)->restoreState(state
);
109 // Tab state created with Dolphin <= 4.14.x
110 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
111 tabPageAt(i
)->restoreStateV1(state
);
115 const int index
= group
.readEntry("Active Tab Index", 0);
116 setCurrentIndex(index
);
119 void DolphinTabWidget::refreshViews()
121 // Left-elision is better when showing full paths, since you care most
122 // about the current directory which is on the right
123 if (GeneralSettings::showFullPathInTitlebar()) {
124 setElideMode(Qt::ElideLeft
);
126 setElideMode(Qt::ElideRight
);
129 const int tabCount
= count();
130 for (int i
= 0; i
< tabCount
; ++i
) {
131 tabBar()->setTabText(i
, tabName(tabPageAt(i
)));
132 tabPageAt(i
)->refreshViews();
136 bool DolphinTabWidget::isUrlOpen(const QUrl
&url
) const
138 return indexByUrl(url
).first
>= 0;
141 void DolphinTabWidget::openNewActivatedTab()
143 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
144 Q_ASSERT(oldActiveViewContainer
);
146 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
148 openNewActivatedTab(oldActiveViewContainer
->url());
150 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
151 Q_ASSERT(newActiveViewContainer
);
153 // The URL navigator of the new tab should have the same editable state
154 // as the current tab
155 newActiveViewContainer
->urlNavigator()->setUrlEditable(isUrlEditable
);
157 // Always focus the new tab's view
158 newActiveViewContainer
->view()->setFocus();
161 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
163 openNewTab(primaryUrl
, secondaryUrl
);
164 setCurrentIndex(count() - 1);
167 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
, TabPlacement tabPlacement
)
169 QWidget
* focusWidget
= QApplication::focusWidget();
171 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
172 tabPage
->setActive(false);
173 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
174 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
175 this, &DolphinTabWidget::activeViewChanged
);
176 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
177 this, &DolphinTabWidget::tabUrlChanged
);
178 int newTabIndex
= -1;
179 if (tabPlacement
== AfterCurrentTab
) {
180 newTabIndex
= currentIndex() + 1;
182 insertTab(newTabIndex
, tabPage
, QIcon() /* loaded in tabInserted */, tabName(tabPage
));
185 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
186 // in background, assure that the previous focused widget gets the focus back.
187 focusWidget
->setFocus();
191 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
193 Q_ASSERT(dirs
.size() > 0);
195 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
196 while (it
!= dirs
.constEnd()) {
197 const QUrl
& primaryUrl
= *(it
++);
198 const QPair
<int, bool> indexInfo
= indexByUrl(primaryUrl
);
199 const int index
= indexInfo
.first
;
200 const bool isInPrimaryView
= indexInfo
.second
;
202 setCurrentIndex(index
);
203 const auto tabPage
= tabPageAt(index
);
204 if (isInPrimaryView
) {
205 tabPage
->primaryViewContainer()->setActive(true);
207 tabPage
->secondaryViewContainer()->setActive(true);
210 // Required for updateViewState() call in openFiles() to work as expected
211 // If there is a selection, updateViewState() calls are effectively a no-op
212 tabPage
->activeViewContainer()->view()->clearSelection();
215 if (splitView
&& (it
!= dirs
.constEnd())) {
216 const QUrl
& secondaryUrl
= *(it
++);
217 openNewActivatedTab(primaryUrl
, secondaryUrl
);
219 openNewActivatedTab(primaryUrl
);
224 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
226 Q_ASSERT(files
.size() > 0);
228 // Get all distinct directories from 'files' and open a tab
229 // for each directory. If the "split view" option is enabled, two
230 // directories are shown inside one tab (see openDirectories()).
232 foreach (const QUrl
& url
, files
) {
233 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
234 if (!dirs
.contains(dir
)) {
239 const int oldTabCount
= count();
240 openDirectories(dirs
, splitView
);
241 const int tabCount
= count();
243 // Select the files. Although the files can be split between several
244 // tabs, there is no need to split 'files' accordingly, as
245 // the DolphinView will just ignore invalid selections.
246 for (int i
= 0; i
< tabCount
; ++i
) {
247 DolphinTabPage
* tabPage
= tabPageAt(i
);
248 tabPage
->markUrlsAsSelected(files
);
249 tabPage
->markUrlAsCurrent(files
.first());
250 if (i
< oldTabCount
) {
251 // Force selection of file if directory was already open, BUG: 417230
252 tabPage
->activeViewContainer()->view()->updateViewState();
257 void DolphinTabWidget::closeTab()
259 closeTab(currentIndex());
262 void DolphinTabWidget::closeTab(const int index
)
264 Q_ASSERT(index
>= 0);
265 Q_ASSERT(index
< count());
268 // Close Dolphin when closing the last tab.
269 parentWidget()->close();
273 DolphinTabPage
* tabPage
= tabPageAt(index
);
274 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
277 tabPage
->deleteLater();
280 void DolphinTabWidget::activateTab(const int index
)
282 if (index
< count()) {
283 setCurrentIndex(index
);
287 void DolphinTabWidget::activateLastTab()
289 setCurrentIndex(count() - 1);
292 void DolphinTabWidget::activateNextTab()
294 const int index
= currentIndex() + 1;
295 setCurrentIndex(index
< count() ? index
: 0);
298 void DolphinTabWidget::activatePrevTab()
300 const int index
= currentIndex() - 1;
301 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
304 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
306 // The places-selector from the URL navigator should only be shown
307 // if the places dock is invisible
308 m_placesSelectorVisible
= !visible
;
310 const int tabCount
= count();
311 for (int i
= 0; i
< tabCount
; ++i
) {
312 DolphinTabPage
* tabPage
= tabPageAt(i
);
313 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
317 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
319 openNewActivatedTab();
320 currentTabPage()->restoreState(state
);
323 void DolphinTabWidget::copyToInactiveSplitView()
325 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
326 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
327 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
331 if (tabPage
->primaryViewActive()) {
332 // copy from left panel to right
333 activeViewContainer
->view()->copySelectedItemsToInactiveSplitView(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
335 // copy from right panel to left
336 activeViewContainer
->view()->copySelectedItemsToInactiveSplitView(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
340 void DolphinTabWidget::moveToInactiveSplitView()
342 const DolphinTabPage
* tabPage
= tabPageAt(currentIndex());
343 DolphinViewContainer
* activeViewContainer
= currentTabPage()->activeViewContainer();
344 if (!tabPage
->splitViewEnabled() || activeViewContainer
->view()->selectedItems().isEmpty()) {
348 if (tabPage
->primaryViewActive()) {
349 // move from left panel to right
350 activeViewContainer
->view()->moveSelectedItemsToInactiveSplitView(activeViewContainer
->view()->selectedItems(), tabPage
->secondaryViewContainer()->url());
352 // move from right panel to left
353 activeViewContainer
->view()->moveSelectedItemsToInactiveSplitView(activeViewContainer
->view()->selectedItems(), tabPage
->primaryViewContainer()->url());
357 void DolphinTabWidget::detachTab(int index
)
359 Q_ASSERT(index
>= 0);
363 const DolphinTabPage
* tabPage
= tabPageAt(index
);
364 args
<< tabPage
->primaryViewContainer()->url().url();
365 if (tabPage
->splitViewEnabled()) {
366 args
<< tabPage
->secondaryViewContainer()->url().url();
367 args
<< QStringLiteral("--split");
369 args
<< QStringLiteral("--new-window");
371 KIO::CommandLauncherJob
*job
= new KIO::CommandLauncherJob("dolphin", args
, this);
372 job
->setDesktopName(QStringLiteral("org.kde.dolphin"));
378 void DolphinTabWidget::openNewActivatedTab(int index
)
380 Q_ASSERT(index
>= 0);
381 const DolphinTabPage
* tabPage
= tabPageAt(index
);
382 openNewActivatedTab(tabPage
->activeViewContainer()->url());
385 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
388 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
389 view
->dropUrls(view
->url(), event
, view
);
393 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
395 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
397 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
398 if (tabBar()->isVisible()) {
399 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
401 // Mark as dirty, actually load once the tab bar actually gets shown
402 tabBar()->setTabIcon(index
, QIcon());
405 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
406 if (index
== currentIndex()) {
407 emit
currentUrlChanged(url
);
412 void DolphinTabWidget::currentTabChanged(int index
)
414 // last-viewed tab deactivation
415 if (DolphinTabPage
* tabPage
= tabPageAt(m_lastViewedTab
)) {
416 tabPage
->setActive(false);
418 DolphinTabPage
* tabPage
= tabPageAt(index
);
419 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
420 emit
activeViewChanged(viewContainer
);
421 emit
currentUrlChanged(viewContainer
->url());
422 tabPage
->setActive(true);
423 m_lastViewedTab
= index
;
426 void DolphinTabWidget::tabInserted(int index
)
428 QTabWidget::tabInserted(index
);
431 // Resolve all pending tab icons
432 for (int i
= 0; i
< count(); ++i
) {
433 if (tabBar()->tabIcon(i
).isNull()) {
434 tabBar()->setTabIcon(i
, QIcon::fromTheme(KIO::iconNameForUrl(tabPageAt(i
)->activeViewContainer()->url())));
441 emit
tabCountChanged(count());
444 void DolphinTabWidget::tabRemoved(int index
)
446 QTabWidget::tabRemoved(index
);
448 // If only one tab is left, then remove the tab entry so that
449 // closing the last tab is not possible.
454 emit
tabCountChanged(count());
457 QString
DolphinTabWidget::tabName(DolphinTabPage
* tabPage
) const
462 QString name
= tabPage
->activeViewContainer()->caption();
463 // Make sure that a '&' inside the directory name is displayed correctly
464 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
465 return name
.replace('&', QLatin1String("&&"));
468 QPair
<int, bool> DolphinTabWidget::indexByUrl(const QUrl
& url
) const
470 for (int i
= 0; i
< count(); i
++) {
471 const auto tabPage
= tabPageAt(i
);
472 if (url
== tabPage
->primaryViewContainer()->url()) {
473 return qMakePair(i
, true);
476 if (tabPage
->splitViewEnabled() && url
== tabPage
->secondaryViewContainer()->url()) {
477 return qMakePair(i
, false);
480 return qMakePair(-1, false);