]>
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)
37 connect(this, &DolphinTabWidget::tabCloseRequested
,
38 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab
));
39 connect(this, &DolphinTabWidget::currentChanged
,
40 this, &DolphinTabWidget::currentTabChanged
);
42 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
43 connect(tabBar
, &DolphinTabBar::openNewActivatedTab
,
44 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab
));
45 connect(tabBar
, &DolphinTabBar::tabDropEvent
,
46 this, &DolphinTabWidget::tabDropEvent
);
47 connect(tabBar
, &DolphinTabBar::tabDetachRequested
,
48 this, &DolphinTabWidget::detachTab
);
52 setDocumentMode(true);
53 setElideMode(Qt::ElideRight
);
54 setUsesScrollButtons(true);
57 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
59 return tabPageAt(currentIndex());
62 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
64 return static_cast<DolphinTabPage
*>(widget(index
));
67 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
69 const int tabCount
= count();
70 group
.writeEntry("Tab Count", tabCount
);
71 group
.writeEntry("Active Tab Index", currentIndex());
73 for (int i
= 0; i
< tabCount
; ++i
) {
74 const DolphinTabPage
* tabPage
= tabPageAt(i
);
75 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
79 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
81 const int tabCount
= group
.readEntry("Tab Count", 0);
82 for (int i
= 0; i
< tabCount
; ++i
) {
84 openNewActivatedTab();
86 if (group
.hasKey("Tab Data " % QString::number(i
))) {
87 // Tab state created with Dolphin > 4.14.x
88 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
89 tabPageAt(i
)->restoreState(state
);
91 // Tab state created with Dolphin <= 4.14.x
92 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
93 tabPageAt(i
)->restoreStateV1(state
);
97 const int index
= group
.readEntry("Active Tab Index", 0);
98 setCurrentIndex(index
);
101 void DolphinTabWidget::refreshViews()
103 const int tabCount
= count();
104 for (int i
= 0; i
< tabCount
; ++i
) {
105 tabPageAt(i
)->refreshViews();
109 void DolphinTabWidget::openNewActivatedTab()
111 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
112 Q_ASSERT(oldActiveViewContainer
);
114 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
116 openNewActivatedTab(oldActiveViewContainer
->url());
118 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
119 Q_ASSERT(newActiveViewContainer
);
121 // The URL navigator of the new tab should have the same editable state
122 // as the current tab
123 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
124 navigator
->setUrlEditable(isUrlEditable
);
127 // If a new tab is opened and the URL is editable, assure that
128 // the user can edit the URL without manually setting the focus
129 navigator
->setFocus();
133 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
135 openNewTab(primaryUrl
, secondaryUrl
);
136 setCurrentIndex(count() - 1);
139 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
141 QWidget
* focusWidget
= QApplication::focusWidget();
143 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
144 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
145 connect(tabPage
, &DolphinTabPage::activeViewChanged
,
146 this, &DolphinTabWidget::activeViewChanged
);
147 connect(tabPage
, &DolphinTabPage::activeViewUrlChanged
,
148 this, &DolphinTabWidget::tabUrlChanged
);
149 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
152 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
153 // in background, assure that the previous focused widget gets the focus back.
154 focusWidget
->setFocus();
158 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
160 Q_ASSERT(dirs
.size() > 0);
162 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
163 while (it
!= dirs
.constEnd()) {
164 const QUrl
& primaryUrl
= *(it
++);
165 if (splitView
&& (it
!= dirs
.constEnd())) {
166 const QUrl
& secondaryUrl
= *(it
++);
167 openNewTab(primaryUrl
, secondaryUrl
);
169 openNewTab(primaryUrl
);
174 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
176 Q_ASSERT(files
.size() > 0);
178 // Get all distinct directories from 'files' and open a tab
179 // for each directory. If the "split view" option is enabled, two
180 // directories are shown inside one tab (see openDirectories()).
182 foreach (const QUrl
& url
, files
) {
183 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
184 if (!dirs
.contains(dir
)) {
189 const int oldTabCount
= count();
190 openDirectories(dirs
, splitView
);
191 const int tabCount
= count();
193 // Select the files. Although the files can be split between several
194 // tabs, there is no need to split 'files' accordingly, as
195 // the DolphinView will just ignore invalid selections.
196 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
197 DolphinTabPage
* tabPage
= tabPageAt(i
);
198 tabPage
->markUrlsAsSelected(files
);
199 tabPage
->markUrlAsCurrent(files
.first());
203 void DolphinTabWidget::closeTab()
205 closeTab(currentIndex());
208 void DolphinTabWidget::closeTab(const int index
)
210 Q_ASSERT(index
>= 0);
211 Q_ASSERT(index
< count());
214 // Never close the last tab.
218 DolphinTabPage
* tabPage
= tabPageAt(index
);
219 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
222 tabPage
->deleteLater();
225 void DolphinTabWidget::activateNextTab()
227 const int index
= currentIndex() + 1;
228 setCurrentIndex(index
< count() ? index
: 0);
231 void DolphinTabWidget::activatePrevTab()
233 const int index
= currentIndex() - 1;
234 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
237 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
239 // The places-selector from the URL navigator should only be shown
240 // if the places dock is invisible
241 m_placesSelectorVisible
= !visible
;
243 const int tabCount
= count();
244 for (int i
= 0; i
< tabCount
; ++i
) {
245 DolphinTabPage
* tabPage
= tabPageAt(i
);
246 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
250 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
252 openNewActivatedTab();
253 currentTabPage()->restoreState(state
);
256 void DolphinTabWidget::detachTab(int index
)
258 Q_ASSERT(index
>= 0);
262 const DolphinTabPage
* tabPage
= tabPageAt(index
);
263 args
<< tabPage
->primaryViewContainer()->url().url();
264 if (tabPage
->splitViewEnabled()) {
265 args
<< tabPage
->secondaryViewContainer()->url().url();
266 args
<< QStringLiteral("--split");
269 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
270 KRun::runCommand(command
, this);
275 void DolphinTabWidget::openNewActivatedTab(int index
)
277 Q_ASSERT(index
>= 0);
278 const DolphinTabPage
* tabPage
= tabPageAt(index
);
279 openNewActivatedTab(tabPage
->activeViewContainer()->url());
282 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
285 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
286 view
->dropUrls(view
->url(), event
);
290 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
292 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
294 tabBar()->setTabText(index
, tabName(url
));
295 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
297 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
298 if (index
== currentIndex()) {
299 emit
currentUrlChanged(url
);
304 void DolphinTabWidget::currentTabChanged(int index
)
306 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
307 emit
activeViewChanged(viewContainer
);
308 emit
currentUrlChanged(viewContainer
->url());
309 viewContainer
->view()->setFocus();
312 void DolphinTabWidget::tabInserted(int index
)
314 QTabWidget::tabInserted(index
);
320 emit
tabCountChanged(count());
323 void DolphinTabWidget::tabRemoved(int index
)
325 QTabWidget::tabRemoved(index
);
327 // If only one tab is left, then remove the tab entry so that
328 // closing the last tab is not possible.
333 emit
tabCountChanged(count());
336 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
339 if (url
== QUrl(QStringLiteral("file:///"))) {
342 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
343 if (name
.isEmpty()) {
346 // Make sure that a '&' inside the directory name is displayed correctly
347 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
348 name
.replace('&', QLatin1String("&&"));