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>
30 #include <kio/global.h>
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);
211 if (splitView
&& (it
!= dirs
.constEnd())) {
212 const QUrl
& secondaryUrl
= *(it
++);
213 openNewActivatedTab(primaryUrl
, secondaryUrl
);
215 openNewActivatedTab(primaryUrl
);
220 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
222 Q_ASSERT(files
.size() > 0);
224 // Get all distinct directories from 'files' and open a tab
225 // for each directory. If the "split view" option is enabled, two
226 // directories are shown inside one tab (see openDirectories()).
228 foreach (const QUrl
& url
, files
) {
229 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
230 if (!dirs
.contains(dir
)) {
235 const int oldTabCount
= count();
236 openDirectories(dirs
, splitView
);
237 const int tabCount
= count();
239 // Select the files. Although the files can be split between several
240 // tabs, there is no need to split 'files' accordingly, as
241 // the DolphinView will just ignore invalid selections.
242 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
243 DolphinTabPage
* tabPage
= tabPageAt(i
);
244 tabPage
->markUrlsAsSelected(files
);
245 tabPage
->markUrlAsCurrent(files
.first());
249 void DolphinTabWidget::closeTab()
251 closeTab(currentIndex());
254 void DolphinTabWidget::closeTab(const int index
)
256 Q_ASSERT(index
>= 0);
257 Q_ASSERT(index
< count());
260 // Close Dolphin when closing the last tab.
261 parentWidget()->close();
265 DolphinTabPage
* tabPage
= tabPageAt(index
);
266 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
269 tabPage
->deleteLater();
272 void DolphinTabWidget::activateTab(const int index
)
274 if (index
< count()) {
275 setCurrentIndex(index
);
279 void DolphinTabWidget::activateLastTab()
281 setCurrentIndex(count() - 1);
284 void DolphinTabWidget::activateNextTab()
286 const int index
= currentIndex() + 1;
287 setCurrentIndex(index
< count() ? index
: 0);
290 void DolphinTabWidget::activatePrevTab()
292 const int index
= currentIndex() - 1;
293 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
296 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
298 // The places-selector from the URL navigator should only be shown
299 // if the places dock is invisible
300 m_placesSelectorVisible
= !visible
;
302 const int tabCount
= count();
303 for (int i
= 0; i
< tabCount
; ++i
) {
304 DolphinTabPage
* tabPage
= tabPageAt(i
);
305 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
309 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
311 openNewActivatedTab();
312 currentTabPage()->restoreState(state
);
315 void DolphinTabWidget::detachTab(int index
)
317 Q_ASSERT(index
>= 0);
321 const DolphinTabPage
* tabPage
= tabPageAt(index
);
322 args
<< tabPage
->primaryViewContainer()->url().url();
323 if (tabPage
->splitViewEnabled()) {
324 args
<< tabPage
->secondaryViewContainer()->url().url();
325 args
<< QStringLiteral("--split");
327 args
<< QStringLiteral("--new-window");
329 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
330 KRun::runCommand(command
, this);
335 void DolphinTabWidget::openNewActivatedTab(int index
)
337 Q_ASSERT(index
>= 0);
338 const DolphinTabPage
* tabPage
= tabPageAt(index
);
339 openNewActivatedTab(tabPage
->activeViewContainer()->url());
342 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
345 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
346 view
->dropUrls(view
->url(), event
, view
);
350 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
352 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
354 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
355 if (tabBar()->isVisible()) {
356 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
358 // Mark as dirty, actually load once the tab bar actually gets shown
359 tabBar()->setTabIcon(index
, QIcon());
362 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
363 if (index
== currentIndex()) {
364 emit
currentUrlChanged(url
);
369 void DolphinTabWidget::currentTabChanged(int index
)
371 // last-viewed tab deactivation
372 if (DolphinTabPage
* tabPage
= tabPageAt(m_lastViewedTab
)) {
373 tabPage
->setActive(false);
375 DolphinTabPage
* tabPage
= tabPageAt(index
);
376 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
377 emit
activeViewChanged(viewContainer
);
378 emit
currentUrlChanged(viewContainer
->url());
379 tabPage
->setActive(true);
380 m_lastViewedTab
= index
;
383 void DolphinTabWidget::tabInserted(int index
)
385 QTabWidget::tabInserted(index
);
388 // Resolve all pending tab icons
389 for (int i
= 0; i
< count(); ++i
) {
390 if (tabBar()->tabIcon(i
).isNull()) {
391 tabBar()->setTabIcon(i
, QIcon::fromTheme(KIO::iconNameForUrl(tabPageAt(i
)->activeViewContainer()->url())));
398 emit
tabCountChanged(count());
401 void DolphinTabWidget::tabRemoved(int index
)
403 QTabWidget::tabRemoved(index
);
405 // If only one tab is left, then remove the tab entry so that
406 // closing the last tab is not possible.
411 emit
tabCountChanged(count());
414 QString
DolphinTabWidget::tabName(DolphinTabPage
* tabPage
) const
419 QString name
= tabPage
->activeViewContainer()->caption();
420 // Make sure that a '&' inside the directory name is displayed correctly
421 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
422 return name
.replace('&', QLatin1String("&&"));
425 QPair
<int, bool> DolphinTabWidget::indexByUrl(const QUrl
& url
) const
427 for (int i
= 0; i
< count(); i
++) {
428 const auto tabPage
= tabPageAt(i
);
429 if (url
== tabPage
->primaryViewContainer()->url()) {
430 return qMakePair(i
, true);
433 if (tabPage
->splitViewEnabled() && url
== tabPage
->secondaryViewContainer()->url()) {
434 return qMakePair(i
, false);
437 return qMakePair(-1, false);