]>
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 #pragma GCC diagnostic push
96 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
97 tabPageAt(i
)->restoreStateV1(state
);
98 #pragma GCC diagnostic pop
102 const int index
= group
.readEntry("Active Tab Index", 0);
103 setCurrentIndex(index
);
106 void DolphinTabWidget::refreshViews()
108 const int tabCount
= count();
109 for (int i
= 0; i
< tabCount
; ++i
) {
110 tabPageAt(i
)->refreshViews();
114 void DolphinTabWidget::openNewActivatedTab()
116 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
117 Q_ASSERT(oldActiveViewContainer
);
119 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
121 openNewActivatedTab(oldActiveViewContainer
->url());
123 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
124 Q_ASSERT(newActiveViewContainer
);
126 // The URL navigator of the new tab should have the same editable state
127 // as the current tab
128 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
129 navigator
->setUrlEditable(isUrlEditable
);
132 // If a new tab is opened and the URL is editable, assure that
133 // the user can edit the URL without manually setting the focus
134 navigator
->setFocus();
138 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
140 openNewTab(primaryUrl
, secondaryUrl
);
141 setCurrentIndex(count() - 1);
144 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
146 QWidget
* focusWidget
= QApplication::focusWidget();
148 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
149 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
150 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
151 this, &DolphinTabWidget::activeViewChanged
);
152 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
153 this, &DolphinTabWidget::tabUrlChanged
);
154 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
157 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
158 // in background, assure that the previous focused widget gets the focus back.
159 focusWidget
->setFocus();
163 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
165 Q_ASSERT(dirs
.size() > 0);
167 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
168 while (it
!= dirs
.constEnd()) {
169 const QUrl
& primaryUrl
= *(it
++);
170 if (splitView
&& (it
!= dirs
.constEnd())) {
171 const QUrl
& secondaryUrl
= *(it
++);
172 openNewTab(primaryUrl
, secondaryUrl
);
174 openNewTab(primaryUrl
);
179 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
181 Q_ASSERT(files
.size() > 0);
183 // Get all distinct directories from 'files' and open a tab
184 // for each directory. If the "split view" option is enabled, two
185 // directories are shown inside one tab (see openDirectories()).
187 foreach (const QUrl
& url
, files
) {
188 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
189 if (!dirs
.contains(dir
)) {
194 const int oldTabCount
= count();
195 openDirectories(dirs
, splitView
);
196 const int tabCount
= count();
198 // Select the files. Although the files can be split between several
199 // tabs, there is no need to split 'files' accordingly, as
200 // the DolphinView will just ignore invalid selections.
201 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
202 DolphinTabPage
* tabPage
= tabPageAt(i
);
203 tabPage
->markUrlsAsSelected(files
);
204 tabPage
->markUrlAsCurrent(files
.first());
208 void DolphinTabWidget::closeTab()
210 closeTab(currentIndex());
213 void DolphinTabWidget::closeTab(const int index
)
215 Q_ASSERT(index
>= 0);
216 Q_ASSERT(index
< count());
219 // Never close the last tab.
223 DolphinTabPage
* tabPage
= tabPageAt(index
);
224 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
227 tabPage
->deleteLater();
230 void DolphinTabWidget::activateNextTab()
232 const int index
= currentIndex() + 1;
233 setCurrentIndex(index
< count() ? index
: 0);
236 void DolphinTabWidget::activatePrevTab()
238 const int index
= currentIndex() - 1;
239 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
242 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
244 // The places-selector from the URL navigator should only be shown
245 // if the places dock is invisible
246 m_placesSelectorVisible
= !visible
;
248 const int tabCount
= count();
249 for (int i
= 0; i
< tabCount
; ++i
) {
250 DolphinTabPage
* tabPage
= tabPageAt(i
);
251 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
255 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
257 openNewActivatedTab();
258 currentTabPage()->restoreState(state
);
261 void DolphinTabWidget::detachTab(int index
)
263 Q_ASSERT(index
>= 0);
267 const DolphinTabPage
* tabPage
= tabPageAt(index
);
268 args
<< tabPage
->primaryViewContainer()->url().url();
269 if (tabPage
->splitViewEnabled()) {
270 args
<< tabPage
->secondaryViewContainer()->url().url();
271 args
<< QStringLiteral("--split");
274 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
275 KRun::runCommand(command
, this);
280 void DolphinTabWidget::openNewActivatedTab(int index
)
282 Q_ASSERT(index
>= 0);
283 const DolphinTabPage
* tabPage
= tabPageAt(index
);
284 openNewActivatedTab(tabPage
->activeViewContainer()->url());
287 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
290 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
291 view
->dropUrls(view
->url(), event
, view
);
295 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
297 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
299 tabBar()->setTabText(index
, tabName(url
));
300 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
302 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
303 if (index
== currentIndex()) {
304 emit
currentUrlChanged(url
);
309 void DolphinTabWidget::currentTabChanged(int index
)
311 // previous tab deactivation
312 if (DolphinTabPage
* tabPage
= tabPageAt(m_previousTab
)) {
313 tabPage
->setActive(false);
315 DolphinTabPage
* tabPage
= tabPageAt(index
);
316 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
317 emit
activeViewChanged(viewContainer
);
318 emit
currentUrlChanged(viewContainer
->url());
319 tabPage
->setActive(true);
320 m_previousTab
= index
;
323 void DolphinTabWidget::tabInserted(int index
)
325 QTabWidget::tabInserted(index
);
331 emit
tabCountChanged(count());
334 void DolphinTabWidget::tabRemoved(int index
)
336 QTabWidget::tabRemoved(index
);
338 // If only one tab is left, then remove the tab entry so that
339 // closing the last tab is not possible.
344 emit
tabCountChanged(count());
347 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
350 if (url
== QUrl(QStringLiteral("file:///"))) {
353 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
354 if (name
.isEmpty()) {
357 // Make sure that a '&' inside the directory name is displayed correctly
358 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
359 name
.replace('&', QLatin1String("&&"));