]>
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 #pragma GCC diagnostic push
95 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
96 tabPageAt(i
)->restoreStateV1(state
);
97 #pragma GCC diagnostic pop
101 const int index
= group
.readEntry("Active Tab Index", 0);
102 setCurrentIndex(index
);
105 void DolphinTabWidget::refreshViews()
107 const int tabCount
= count();
108 for (int i
= 0; i
< tabCount
; ++i
) {
109 tabPageAt(i
)->refreshViews();
113 void DolphinTabWidget::openNewActivatedTab()
115 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
116 Q_ASSERT(oldActiveViewContainer
);
118 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
120 openNewActivatedTab(oldActiveViewContainer
->url());
122 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
123 Q_ASSERT(newActiveViewContainer
);
125 // The URL navigator of the new tab should have the same editable state
126 // as the current tab
127 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
128 navigator
->setUrlEditable(isUrlEditable
);
131 // If a new tab is opened and the URL is editable, assure that
132 // the user can edit the URL without manually setting the focus
133 navigator
->setFocus();
137 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
139 openNewTab(primaryUrl
, secondaryUrl
);
140 setCurrentIndex(count() - 1);
143 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
145 QWidget
* focusWidget
= QApplication::focusWidget();
147 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
148 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
149 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
150 this, &DolphinTabWidget::activeViewChanged
);
151 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
152 this, &DolphinTabWidget::tabUrlChanged
);
153 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
156 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
157 // in background, assure that the previous focused widget gets the focus back.
158 focusWidget
->setFocus();
162 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
164 Q_ASSERT(dirs
.size() > 0);
166 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
167 while (it
!= dirs
.constEnd()) {
168 const QUrl
& primaryUrl
= *(it
++);
169 if (splitView
&& (it
!= dirs
.constEnd())) {
170 const QUrl
& secondaryUrl
= *(it
++);
171 openNewTab(primaryUrl
, secondaryUrl
);
173 openNewTab(primaryUrl
);
178 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
180 Q_ASSERT(files
.size() > 0);
182 // Get all distinct directories from 'files' and open a tab
183 // for each directory. If the "split view" option is enabled, two
184 // directories are shown inside one tab (see openDirectories()).
186 foreach (const QUrl
& url
, files
) {
187 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
188 if (!dirs
.contains(dir
)) {
193 const int oldTabCount
= count();
194 openDirectories(dirs
, splitView
);
195 const int tabCount
= count();
197 // Select the files. Although the files can be split between several
198 // tabs, there is no need to split 'files' accordingly, as
199 // the DolphinView will just ignore invalid selections.
200 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
201 DolphinTabPage
* tabPage
= tabPageAt(i
);
202 tabPage
->markUrlsAsSelected(files
);
203 tabPage
->markUrlAsCurrent(files
.first());
207 void DolphinTabWidget::closeTab()
209 closeTab(currentIndex());
212 void DolphinTabWidget::closeTab(const int index
)
214 Q_ASSERT(index
>= 0);
215 Q_ASSERT(index
< count());
218 // Never close the last tab.
222 DolphinTabPage
* tabPage
= tabPageAt(index
);
223 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
226 tabPage
->deleteLater();
229 void DolphinTabWidget::activateNextTab()
231 const int index
= currentIndex() + 1;
232 setCurrentIndex(index
< count() ? index
: 0);
235 void DolphinTabWidget::activatePrevTab()
237 const int index
= currentIndex() - 1;
238 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
241 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
243 // The places-selector from the URL navigator should only be shown
244 // if the places dock is invisible
245 m_placesSelectorVisible
= !visible
;
247 const int tabCount
= count();
248 for (int i
= 0; i
< tabCount
; ++i
) {
249 DolphinTabPage
* tabPage
= tabPageAt(i
);
250 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
254 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
256 openNewActivatedTab();
257 currentTabPage()->restoreState(state
);
260 void DolphinTabWidget::detachTab(int index
)
262 Q_ASSERT(index
>= 0);
266 const DolphinTabPage
* tabPage
= tabPageAt(index
);
267 args
<< tabPage
->primaryViewContainer()->url().url();
268 if (tabPage
->splitViewEnabled()) {
269 args
<< tabPage
->secondaryViewContainer()->url().url();
270 args
<< QStringLiteral("--split");
273 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
274 KRun::runCommand(command
, this);
279 void DolphinTabWidget::openNewActivatedTab(int index
)
281 Q_ASSERT(index
>= 0);
282 const DolphinTabPage
* tabPage
= tabPageAt(index
);
283 openNewActivatedTab(tabPage
->activeViewContainer()->url());
286 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
289 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
290 view
->dropUrls(view
->url(), event
, view
);
294 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
296 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
298 tabBar()->setTabText(index
, tabName(url
));
299 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
301 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
302 if (index
== currentIndex()) {
303 emit
currentUrlChanged(url
);
308 void DolphinTabWidget::currentTabChanged(int index
)
310 // previous tab deactivation
311 if (DolphinTabPage
* tabPage
= tabPageAt(m_previousTab
)) {
312 tabPage
->setActive(false);
314 DolphinTabPage
* tabPage
= tabPageAt(index
);
315 DolphinViewContainer
* viewContainer
= tabPage
->activeViewContainer();
316 emit
activeViewChanged(viewContainer
);
317 emit
currentUrlChanged(viewContainer
->url());
318 tabPage
->setActive(true);
319 m_previousTab
= index
;
322 void DolphinTabWidget::tabInserted(int index
)
324 QTabWidget::tabInserted(index
);
330 emit
tabCountChanged(count());
333 void DolphinTabWidget::tabRemoved(int index
)
335 QTabWidget::tabRemoved(index
);
337 // If only one tab is left, then remove the tab entry so that
338 // closing the last tab is not possible.
343 emit
tabCountChanged(count());
346 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
349 if (url
== QUrl(QStringLiteral("file:///"))) {
352 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
353 if (name
.isEmpty()) {
356 // Make sure that a '&' inside the directory name is displayed correctly
357 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
358 name
.replace('&', QLatin1String("&&"));