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 "dolphintabbar.h"
23 #include "dolphintabpage.h"
24 #include "dolphinviewcontainer.h"
26 #include <KConfigGroup>
29 #include <kio/global.h>
31 #include <QApplication>
34 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
36 m_placesSelectorVisible(true),
39 connect(this, &DolphinTabWidget::tabCloseRequested
,
40 this, QOverload
<int>::of(&DolphinTabWidget::closeTab
));
41 connect(this, &DolphinTabWidget::currentChanged
,
42 this, &DolphinTabWidget::currentTabChanged
);
44 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
45 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
46 this, QOverload
<int>::of(&DolphinTabWidget::openNewActivatedTab
));
47 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
48 this, &DolphinTabWidget::tabDropEvent
);
49 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
50 this, &DolphinTabWidget::detachTab
);
54 setDocumentMode(true);
55 setElideMode(Qt::ElideRight
);
56 setUsesScrollButtons(true);
59 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
61 return tabPageAt(currentIndex());
64 DolphinTabPage
* DolphinTabWidget::nextTabPage() const
66 const int index
= currentIndex() + 1;
67 return tabPageAt(index
< count() ? index
: 0);
70 DolphinTabPage
* DolphinTabWidget::prevTabPage() const
72 const int index
= currentIndex() - 1;
73 return tabPageAt(index
>= 0 ? index
: (count() - 1));
76 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
78 return static_cast<DolphinTabPage
*>(widget(index
));
81 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
83 const int tabCount
= count();
84 group
.writeEntry("Tab Count", tabCount
);
85 group
.writeEntry("Active Tab Index", currentIndex());
87 for (int i
= 0; i
< tabCount
; ++i
) {
88 const DolphinTabPage
* tabPage
= tabPageAt(i
);
89 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
93 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
95 const int tabCount
= group
.readEntry("Tab Count", 0);
96 for (int i
= 0; i
< tabCount
; ++i
) {
98 openNewActivatedTab();
100 if (group
.hasKey("Tab Data " % QString::number(i
))) {
101 // Tab state created with Dolphin > 4.14.x
102 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
103 tabPageAt(i
)->restoreState(state
);
105 // Tab state created with Dolphin <= 4.14.x
106 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
107 tabPageAt(i
)->restoreStateV1(state
);
111 const int index
= group
.readEntry("Active Tab Index", 0);
112 setCurrentIndex(index
);
115 void DolphinTabWidget::refreshViews()
117 const int tabCount
= count();
118 for (int i
= 0; i
< tabCount
; ++i
) {
119 tabBar()->setTabText(i
, tabName(tabPageAt(i
)));
120 tabPageAt(i
)->refreshViews();
124 void DolphinTabWidget::openNewActivatedTab()
126 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
127 Q_ASSERT(oldActiveViewContainer
);
129 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
131 openNewActivatedTab(oldActiveViewContainer
->url());
133 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
134 Q_ASSERT(newActiveViewContainer
);
136 // The URL navigator of the new tab should have the same editable state
137 // as the current tab
138 newActiveViewContainer
->urlNavigator()->setUrlEditable(isUrlEditable
);
140 // Always focus the new tab's view
141 newActiveViewContainer
->view()->setFocus();
144 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
146 openNewTab(primaryUrl
, secondaryUrl
);
147 setCurrentIndex(count() - 1);
150 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
, TabPlacement tabPlacement
)
152 QWidget
* focusWidget
= QApplication::focusWidget();
154 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
155 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
156 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
157 this, &DolphinTabWidget::activeViewChanged
);
158 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
159 this, &DolphinTabWidget::tabUrlChanged
);
160 int newTabIndex
= -1;
161 if (tabPlacement
== AfterCurrentTab
) {
162 newTabIndex
= currentIndex() + 1;
164 insertTab(newTabIndex
, tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(tabPage
));
167 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
168 // in background, assure that the previous focused widget gets the focus back.
169 focusWidget
->setFocus();
173 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
175 Q_ASSERT(dirs
.size() > 0);
177 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
178 while (it
!= dirs
.constEnd()) {
179 const QUrl
& primaryUrl
= *(it
++);
180 if (splitView
&& (it
!= dirs
.constEnd())) {
181 const QUrl
& secondaryUrl
= *(it
++);
182 openNewTab(primaryUrl
, secondaryUrl
);
184 openNewTab(primaryUrl
);
189 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
191 Q_ASSERT(files
.size() > 0);
193 // Get all distinct directories from 'files' and open a tab
194 // for each directory. If the "split view" option is enabled, two
195 // directories are shown inside one tab (see openDirectories()).
197 foreach (const QUrl
& url
, files
) {
198 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
199 if (!dirs
.contains(dir
)) {
204 const int oldTabCount
= count();
205 openDirectories(dirs
, splitView
);
206 const int tabCount
= count();
208 // Select the files. Although the files can be split between several
209 // tabs, there is no need to split 'files' accordingly, as
210 // the DolphinView will just ignore invalid selections.
211 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
212 DolphinTabPage
* tabPage
= tabPageAt(i
);
213 tabPage
->markUrlsAsSelected(files
);
214 tabPage
->markUrlAsCurrent(files
.first());
218 void DolphinTabWidget::closeTab()
220 closeTab(currentIndex());
223 void DolphinTabWidget::closeTab(const int index
)
225 Q_ASSERT(index
>= 0);
226 Q_ASSERT(index
< count());
229 // Close Dolphin when closing the last tab.
230 parentWidget()->close();
234 DolphinTabPage
* tabPage
= tabPageAt(index
);
235 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
238 tabPage
->deleteLater();
241 void DolphinTabWidget::activateNextTab()
243 const int index
= currentIndex() + 1;
244 setCurrentIndex(index
< count() ? index
: 0);
247 void DolphinTabWidget::activatePrevTab()
249 const int index
= currentIndex() - 1;
250 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
253 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
255 // The places-selector from the URL navigator should only be shown
256 // if the places dock is invisible
257 m_placesSelectorVisible
= !visible
;
259 const int tabCount
= count();
260 for (int i
= 0; i
< tabCount
; ++i
) {
261 DolphinTabPage
* tabPage
= tabPageAt(i
);
262 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
266 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
268 openNewActivatedTab();
269 currentTabPage()->restoreState(state
);
272 void DolphinTabWidget::detachTab(int index
)
274 Q_ASSERT(index
>= 0);
278 const DolphinTabPage
* tabPage
= tabPageAt(index
);
279 args
<< tabPage
->primaryViewContainer()->url().url();
280 if (tabPage
->splitViewEnabled()) {
281 args
<< tabPage
->secondaryViewContainer()->url().url();
282 args
<< QStringLiteral("--split");
285 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
286 KRun::runCommand(command
, this);
291 void DolphinTabWidget::openNewActivatedTab(int index
)
293 Q_ASSERT(index
>= 0);
294 const DolphinTabPage
* tabPage
= tabPageAt(index
);
295 openNewActivatedTab(tabPage
->activeViewContainer()->url());
298 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
301 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
302 view
->dropUrls(view
->url(), event
, view
);
306 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
308 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
310 tabBar()->setTabText(index
, tabName(tabPageAt(index
)));
311 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
313 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
314 if (index
== currentIndex()) {
315 emit
currentUrlChanged(url
);
320 void DolphinTabWidget::currentTabChanged(int index
)
322 // last-viewed tab deactivation
323 if (DolphinTabPage
* tabPage
= tabPageAt(m_lastViewedTab
)) {
324 tabPage
->setActive(false);
326 DolphinTabPage
* tabPage
= tabPageAt(index
);
327 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
328 emit
activeViewChanged(viewContainer
);
329 emit
currentUrlChanged(viewContainer
->url());
330 tabPage
->setActive(true);
331 m_lastViewedTab
= index
;
334 void DolphinTabWidget::tabInserted(int index
)
336 QTabWidget::tabInserted(index
);
342 emit
tabCountChanged(count());
345 void DolphinTabWidget::tabRemoved(int index
)
347 QTabWidget::tabRemoved(index
);
349 // If only one tab is left, then remove the tab entry so that
350 // closing the last tab is not possible.
355 emit
tabCountChanged(count());
358 QString
DolphinTabWidget::tabName(DolphinTabPage
* tabPage
) const
363 QString name
= tabPage
->activeViewContainer()->caption();
364 // Make sure that a '&' inside the directory name is displayed correctly
365 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
366 return name
.replace('&', QLatin1String("&&"));