]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
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, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab
));
41 connect(this, &DolphinTabWidget::currentChanged
,
42 this, &DolphinTabWidget::currentTabChanged
);
44 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
45 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
46 this, static_cast<void (DolphinTabWidget::*)(int)>(&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::tabPageAt(const int index
) const
66 return static_cast<DolphinTabPage
*>(widget(index
));
69 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
71 const int tabCount
= count();
72 group
.writeEntry("Tab Count", tabCount
);
73 group
.writeEntry("Active Tab Index", currentIndex());
75 for (int i
= 0; i
< tabCount
; ++i
) {
76 const DolphinTabPage
* tabPage
= tabPageAt(i
);
77 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
81 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
83 const int tabCount
= group
.readEntry("Tab Count", 0);
84 for (int i
= 0; i
< tabCount
; ++i
) {
86 openNewActivatedTab();
88 if (group
.hasKey("Tab Data " % QString::number(i
))) {
89 // Tab state created with Dolphin > 4.14.x
90 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
91 tabPageAt(i
)->restoreState(state
);
93 // Tab state created with Dolphin <= 4.14.x
94 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
95 tabPageAt(i
)->restoreStateV1(state
);
99 const int index
= group
.readEntry("Active Tab Index", 0);
100 setCurrentIndex(index
);
103 void DolphinTabWidget::refreshViews()
105 const int tabCount
= count();
106 for (int i
= 0; i
< tabCount
; ++i
) {
107 tabPageAt(i
)->refreshViews();
111 void DolphinTabWidget::openNewActivatedTab()
113 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
114 Q_ASSERT(oldActiveViewContainer
);
116 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
118 openNewActivatedTab(oldActiveViewContainer
->url());
120 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
121 Q_ASSERT(newActiveViewContainer
);
123 // The URL navigator of the new tab should have the same editable state
124 // as the current tab
125 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
126 navigator
->setUrlEditable(isUrlEditable
);
129 // If a new tab is opened and the URL is editable, assure that
130 // the user can edit the URL without manually setting the focus
131 navigator
->setFocus();
135 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
137 openNewTab(primaryUrl
, secondaryUrl
);
138 setCurrentIndex(count() - 1);
141 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
143 QWidget
* focusWidget
= QApplication::focusWidget();
145 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
146 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
147 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
148 this, &DolphinTabWidget::activeViewChanged
);
149 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
150 this, &DolphinTabWidget::tabUrlChanged
);
151 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
154 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
155 // in background, assure that the previous focused widget gets the focus back.
156 focusWidget
->setFocus();
160 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
162 Q_ASSERT(dirs
.size() > 0);
164 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
165 while (it
!= dirs
.constEnd()) {
166 const QUrl
& primaryUrl
= *(it
++);
167 if (splitView
&& (it
!= dirs
.constEnd())) {
168 const QUrl
& secondaryUrl
= *(it
++);
169 openNewTab(primaryUrl
, secondaryUrl
);
171 openNewTab(primaryUrl
);
176 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
178 Q_ASSERT(files
.size() > 0);
180 // Get all distinct directories from 'files' and open a tab
181 // for each directory. If the "split view" option is enabled, two
182 // directories are shown inside one tab (see openDirectories()).
184 foreach (const QUrl
& url
, files
) {
185 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
186 if (!dirs
.contains(dir
)) {
191 const int oldTabCount
= count();
192 openDirectories(dirs
, splitView
);
193 const int tabCount
= count();
195 // Select the files. Although the files can be split between several
196 // tabs, there is no need to split 'files' accordingly, as
197 // the DolphinView will just ignore invalid selections.
198 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
199 DolphinTabPage
* tabPage
= tabPageAt(i
);
200 tabPage
->markUrlsAsSelected(files
);
201 tabPage
->markUrlAsCurrent(files
.first());
205 void DolphinTabWidget::closeTab()
207 closeTab(currentIndex());
210 void DolphinTabWidget::closeTab(const int index
)
212 Q_ASSERT(index
>= 0);
213 Q_ASSERT(index
< count());
216 // Never close the last tab.
220 DolphinTabPage
* tabPage
= tabPageAt(index
);
221 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
224 tabPage
->deleteLater();
227 void DolphinTabWidget::activateNextTab()
229 const int index
= currentIndex() + 1;
230 setCurrentIndex(index
< count() ? index
: 0);
233 void DolphinTabWidget::activatePrevTab()
235 const int index
= currentIndex() - 1;
236 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
239 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
241 // The places-selector from the URL navigator should only be shown
242 // if the places dock is invisible
243 m_placesSelectorVisible
= !visible
;
245 const int tabCount
= count();
246 for (int i
= 0; i
< tabCount
; ++i
) {
247 DolphinTabPage
* tabPage
= tabPageAt(i
);
248 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
252 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
254 openNewActivatedTab();
255 currentTabPage()->restoreState(state
);
258 void DolphinTabWidget::detachTab(int index
)
260 Q_ASSERT(index
>= 0);
264 const DolphinTabPage
* tabPage
= tabPageAt(index
);
265 args
<< tabPage
->primaryViewContainer()->url().url();
266 if (tabPage
->splitViewEnabled()) {
267 args
<< tabPage
->secondaryViewContainer()->url().url();
268 args
<< QStringLiteral("--split");
271 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
272 KRun::runCommand(command
, this);
277 void DolphinTabWidget::openNewActivatedTab(int index
)
279 Q_ASSERT(index
>= 0);
280 const DolphinTabPage
* tabPage
= tabPageAt(index
);
281 openNewActivatedTab(tabPage
->activeViewContainer()->url());
284 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
287 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
288 view
->dropUrls(view
->url(), event
, view
);
292 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
294 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
296 tabBar()->setTabText(index
, tabName(url
));
297 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
299 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
300 if (index
== currentIndex()) {
301 emit
currentUrlChanged(url
);
306 void DolphinTabWidget::currentTabChanged(int index
)
308 // previous tab deactivation
309 if (DolphinTabPage
* tabPage
= tabPageAt(m_previousTab
)) {
310 tabPage
->setActive(false);
312 DolphinTabPage
* tabPage
= tabPageAt(index
);
313 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
314 emit
activeViewChanged(viewContainer
);
315 emit
currentUrlChanged(viewContainer
->url());
316 tabPage
->setActive(true);
317 m_previousTab
= index
;
320 void DolphinTabWidget::tabInserted(int index
)
322 QTabWidget::tabInserted(index
);
328 emit
tabCountChanged(count());
331 void DolphinTabWidget::tabRemoved(int index
)
333 QTabWidget::tabRemoved(index
);
335 // If only one tab is left, then remove the tab entry so that
336 // closing the last tab is not possible.
341 emit
tabCountChanged(count());
344 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
347 if (url
== QUrl(QStringLiteral("file:///"))) {
350 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
351 if (name
.isEmpty()) {
354 // Make sure that a '&' inside the directory name is displayed correctly
355 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
356 name
.replace('&', QLatin1String("&&"));