]>
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 <QApplication>
28 #include <KConfigGroup>
30 #include <kio/global.h>
33 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
35 m_placesSelectorVisible(true),
38 connect(this, &DolphinTabWidget::tabCloseRequested
,
39 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab
));
40 connect(this, &DolphinTabWidget::currentChanged
,
41 this, &DolphinTabWidget::currentTabChanged
);
43 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
44 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
45 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab
));
46 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
47 this, &DolphinTabWidget::tabDropEvent
);
48 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
49 this, &DolphinTabWidget::detachTab
);
53 setDocumentMode(true);
54 setElideMode(Qt::ElideRight
);
55 setUsesScrollButtons(true);
58 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
60 return tabPageAt(currentIndex());
63 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
65 return static_cast<DolphinTabPage
*>(widget(index
));
68 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
70 const int tabCount
= count();
71 group
.writeEntry("Tab Count", tabCount
);
72 group
.writeEntry("Active Tab Index", currentIndex());
74 for (int i
= 0; i
< tabCount
; ++i
) {
75 const DolphinTabPage
* tabPage
= tabPageAt(i
);
76 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
80 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
82 const int tabCount
= group
.readEntry("Tab Count", 0);
83 for (int i
= 0; i
< tabCount
; ++i
) {
85 openNewActivatedTab();
87 if (group
.hasKey("Tab Data " % QString::number(i
))) {
88 // Tab state created with Dolphin > 4.14.x
89 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
90 tabPageAt(i
)->restoreState(state
);
92 // Tab state created with Dolphin <= 4.14.x
93 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
94 tabPageAt(i
)->restoreStateV1(state
);
98 const int index
= group
.readEntry("Active Tab Index", 0);
99 setCurrentIndex(index
);
102 void DolphinTabWidget::refreshViews()
104 const int tabCount
= count();
105 for (int i
= 0; i
< tabCount
; ++i
) {
106 tabPageAt(i
)->refreshViews();
110 void DolphinTabWidget::openNewActivatedTab()
112 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
113 Q_ASSERT(oldActiveViewContainer
);
115 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
117 openNewActivatedTab(oldActiveViewContainer
->url());
119 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
120 Q_ASSERT(newActiveViewContainer
);
122 // The URL navigator of the new tab should have the same editable state
123 // as the current tab
124 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
125 navigator
->setUrlEditable(isUrlEditable
);
128 // If a new tab is opened and the URL is editable, assure that
129 // the user can edit the URL without manually setting the focus
130 navigator
->setFocus();
134 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
136 openNewTab(primaryUrl
, secondaryUrl
);
137 setCurrentIndex(count() - 1);
140 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
142 QWidget
* focusWidget
= QApplication::focusWidget();
144 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
145 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
146 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
147 this, &DolphinTabWidget::activeViewChanged
);
148 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
149 this, &DolphinTabWidget::tabUrlChanged
);
150 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
153 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
154 // in background, assure that the previous focused widget gets the focus back.
155 focusWidget
->setFocus();
159 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
161 Q_ASSERT(dirs
.size() > 0);
163 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
164 while (it
!= dirs
.constEnd()) {
165 const QUrl
& primaryUrl
= *(it
++);
166 if (splitView
&& (it
!= dirs
.constEnd())) {
167 const QUrl
& secondaryUrl
= *(it
++);
168 openNewTab(primaryUrl
, secondaryUrl
);
170 openNewTab(primaryUrl
);
175 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
177 Q_ASSERT(files
.size() > 0);
179 // Get all distinct directories from 'files' and open a tab
180 // for each directory. If the "split view" option is enabled, two
181 // directories are shown inside one tab (see openDirectories()).
183 foreach (const QUrl
& url
, files
) {
184 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
185 if (!dirs
.contains(dir
)) {
190 const int oldTabCount
= count();
191 openDirectories(dirs
, splitView
);
192 const int tabCount
= count();
194 // Select the files. Although the files can be split between several
195 // tabs, there is no need to split 'files' accordingly, as
196 // the DolphinView will just ignore invalid selections.
197 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
198 DolphinTabPage
* tabPage
= tabPageAt(i
);
199 tabPage
->markUrlsAsSelected(files
);
200 tabPage
->markUrlAsCurrent(files
.first());
204 void DolphinTabWidget::closeTab()
206 closeTab(currentIndex());
209 void DolphinTabWidget::closeTab(const int index
)
211 Q_ASSERT(index
>= 0);
212 Q_ASSERT(index
< count());
215 // Never close the last tab.
219 DolphinTabPage
* tabPage
= tabPageAt(index
);
220 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
223 tabPage
->deleteLater();
226 void DolphinTabWidget::activateNextTab()
228 const int index
= currentIndex() + 1;
229 setCurrentIndex(index
< count() ? index
: 0);
232 void DolphinTabWidget::activatePrevTab()
234 const int index
= currentIndex() - 1;
235 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
238 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
240 // The places-selector from the URL navigator should only be shown
241 // if the places dock is invisible
242 m_placesSelectorVisible
= !visible
;
244 const int tabCount
= count();
245 for (int i
= 0; i
< tabCount
; ++i
) {
246 DolphinTabPage
* tabPage
= tabPageAt(i
);
247 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
251 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
253 openNewActivatedTab();
254 currentTabPage()->restoreState(state
);
257 void DolphinTabWidget::detachTab(int index
)
259 Q_ASSERT(index
>= 0);
263 const DolphinTabPage
* tabPage
= tabPageAt(index
);
264 args
<< tabPage
->primaryViewContainer()->url().url();
265 if (tabPage
->splitViewEnabled()) {
266 args
<< tabPage
->secondaryViewContainer()->url().url();
267 args
<< QStringLiteral("--split");
270 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
271 KRun::runCommand(command
, this);
276 void DolphinTabWidget::openNewActivatedTab(int index
)
278 Q_ASSERT(index
>= 0);
279 const DolphinTabPage
* tabPage
= tabPageAt(index
);
280 openNewActivatedTab(tabPage
->activeViewContainer()->url());
283 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
286 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
287 view
->dropUrls(view
->url(), event
, view
);
291 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
293 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
295 tabBar()->setTabText(index
, tabName(url
));
296 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
298 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
299 if (index
== currentIndex()) {
300 emit
currentUrlChanged(url
);
305 void DolphinTabWidget::currentTabChanged(int index
)
307 // previous tab deactivation
308 if (DolphinTabPage
* tabPage
= tabPageAt(m_previousTab
)) {
309 tabPage
->setActive(false);
311 DolphinTabPage
* tabPage
= tabPageAt(index
);
312 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
313 emit
activeViewChanged(viewContainer
);
314 emit
currentUrlChanged(viewContainer
->url());
315 tabPage
->setActive(true);
316 m_previousTab
= index
;
319 void DolphinTabWidget::tabInserted(int index
)
321 QTabWidget::tabInserted(index
);
327 emit
tabCountChanged(count());
330 void DolphinTabWidget::tabRemoved(int index
)
332 QTabWidget::tabRemoved(index
);
334 // If only one tab is left, then remove the tab entry so that
335 // closing the last tab is not possible.
340 emit
tabCountChanged(count());
343 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
346 if (url
== QUrl(QStringLiteral("file:///"))) {
349 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
350 if (name
.isEmpty()) {
353 // Make sure that a '&' inside the directory name is displayed correctly
354 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
355 name
.replace('&', QLatin1String("&&"));