]>
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>
27 #include <KConfigGroup>
29 #include <kio/global.h>
32 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
34 m_placesSelectorVisible(true)
36 connect(this, SIGNAL(tabCloseRequested(int)),
37 this, SLOT(closeTab(int)));
38 connect(this, SIGNAL(currentChanged(int)),
39 this, SLOT(currentTabChanged(int)));
41 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
42 connect(tabBar
, SIGNAL(openNewActivatedTab(int)),
43 this, SLOT(openNewActivatedTab(int)));
44 connect(tabBar
, SIGNAL(tabDropEvent(int,QDropEvent
*)),
45 this, SLOT(tabDropEvent(int,QDropEvent
*)));
46 connect(tabBar
, SIGNAL(tabDetachRequested(int)),
47 this, SLOT(detachTab(int)));
51 setDocumentMode(true);
52 setElideMode(Qt::ElideRight
);
53 setUsesScrollButtons(true);
56 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
58 return tabPageAt(currentIndex());
61 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
63 return static_cast<DolphinTabPage
*>(widget(index
));
66 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
68 const int tabCount
= count();
69 group
.writeEntry("Tab Count", tabCount
);
70 group
.writeEntry("Active Tab Index", currentIndex());
72 for (int i
= 0; i
< tabCount
; ++i
) {
73 const DolphinTabPage
* tabPage
= tabPageAt(i
);
74 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
78 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
80 const int tabCount
= group
.readEntry("Tab Count", 0);
81 for (int i
= 0; i
< tabCount
; ++i
) {
83 openNewActivatedTab();
85 if (group
.hasKey("Tab Data " % QString::number(i
))) {
86 // Tab state created with Dolphin > 4.14.x
87 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
88 tabPageAt(i
)->restoreState(state
);
90 // Tab state created with Dolphin <= 4.14.x
91 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
92 tabPageAt(i
)->restoreStateV1(state
);
96 const int index
= group
.readEntry("Active Tab Index", 0);
97 setCurrentIndex(index
);
100 void DolphinTabWidget::refreshViews()
102 const int tabCount
= count();
103 for (int i
= 0; i
< tabCount
; ++i
) {
104 tabPageAt(i
)->refreshViews();
108 void DolphinTabWidget::openNewActivatedTab()
110 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
111 Q_ASSERT(oldActiveViewContainer
);
113 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
115 openNewActivatedTab(oldActiveViewContainer
->url());
117 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
118 Q_ASSERT(newActiveViewContainer
);
120 // The URL navigator of the new tab should have the same editable state
121 // as the current tab
122 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
123 navigator
->setUrlEditable(isUrlEditable
);
126 // If a new tab is opened and the URL is editable, assure that
127 // the user can edit the URL without manually setting the focus
128 navigator
->setFocus();
132 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
134 openNewTab(primaryUrl
, secondaryUrl
);
135 setCurrentIndex(count() - 1);
138 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
140 QWidget
* focusWidget
= QApplication::focusWidget();
142 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
143 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
144 connect(tabPage
, SIGNAL(activeViewChanged(DolphinViewContainer
*)),
145 this, SIGNAL(activeViewChanged(DolphinViewContainer
*)));
146 connect(tabPage
, SIGNAL(activeViewUrlChanged(QUrl
)),
147 this, SLOT(tabUrlChanged(QUrl
)));
148 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
151 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
152 // in background, assure that the previous focused widget gets the focus back.
153 focusWidget
->setFocus();
157 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
159 Q_ASSERT(dirs
.size() > 0);
161 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
162 while (it
!= dirs
.constEnd()) {
163 const QUrl
& primaryUrl
= *(it
++);
164 if (splitView
&& (it
!= dirs
.constEnd())) {
165 const QUrl
& secondaryUrl
= *(it
++);
166 openNewTab(primaryUrl
, secondaryUrl
);
168 openNewTab(primaryUrl
);
173 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
175 Q_ASSERT(files
.size() > 0);
177 // Get all distinct directories from 'files' and open a tab
178 // for each directory. If the "split view" option is enabled, two
179 // directories are shown inside one tab (see openDirectories()).
181 foreach (const QUrl
& url
, files
) {
182 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
183 if (!dirs
.contains(dir
)) {
188 const int oldTabCount
= count();
189 openDirectories(dirs
, splitView
);
190 const int tabCount
= count();
192 // Select the files. Although the files can be split between several
193 // tabs, there is no need to split 'files' accordingly, as
194 // the DolphinView will just ignore invalid selections.
195 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
196 DolphinTabPage
* tabPage
= tabPageAt(i
);
197 tabPage
->markUrlsAsSelected(files
);
198 tabPage
->markUrlAsCurrent(files
.first());
202 void DolphinTabWidget::closeTab()
204 closeTab(currentIndex());
207 void DolphinTabWidget::closeTab(const int index
)
209 Q_ASSERT(index
>= 0);
210 Q_ASSERT(index
< count());
213 // Never close the last tab.
217 DolphinTabPage
* tabPage
= tabPageAt(index
);
218 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
221 tabPage
->deleteLater();
224 void DolphinTabWidget::activateNextTab()
226 const int index
= currentIndex() + 1;
227 setCurrentIndex(index
< count() ? index
: 0);
230 void DolphinTabWidget::activatePrevTab()
232 const int index
= currentIndex() - 1;
233 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
236 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
238 // The places-selector from the URL navigator should only be shown
239 // if the places dock is invisible
240 m_placesSelectorVisible
= !visible
;
242 const int tabCount
= count();
243 for (int i
= 0; i
< tabCount
; ++i
) {
244 DolphinTabPage
* tabPage
= tabPageAt(i
);
245 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
249 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
251 openNewActivatedTab();
252 currentTabPage()->restoreState(state
);
255 void DolphinTabWidget::detachTab(int index
)
257 Q_ASSERT(index
>= 0);
261 const DolphinTabPage
* tabPage
= tabPageAt(index
);
262 args
<< tabPage
->primaryViewContainer()->url().url();
263 if (tabPage
->splitViewEnabled()) {
264 args
<< tabPage
->secondaryViewContainer()->url().url();
265 args
<< QStringLiteral("--split");
268 const QString command
= QStringLiteral("dolphin %1").arg(KShell::joinArgs(args
));
269 KRun::runCommand(command
, this);
274 void DolphinTabWidget::openNewActivatedTab(int index
)
276 Q_ASSERT(index
>= 0);
277 const DolphinTabPage
* tabPage
= tabPageAt(index
);
278 openNewActivatedTab(tabPage
->activeViewContainer()->url());
281 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
284 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
285 view
->dropUrls(view
->url(), event
);
289 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
291 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
293 tabBar()->setTabText(index
, tabName(url
));
294 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
296 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
297 if (index
== currentIndex()) {
298 emit
currentUrlChanged(url
);
303 void DolphinTabWidget::currentTabChanged(int index
)
305 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
306 emit
activeViewChanged(viewContainer
);
307 emit
currentUrlChanged(viewContainer
->url());
308 viewContainer
->view()->setFocus();
311 void DolphinTabWidget::tabInserted(int index
)
313 QTabWidget::tabInserted(index
);
319 emit
tabCountChanged(count());
322 void DolphinTabWidget::tabRemoved(int index
)
324 QTabWidget::tabRemoved(index
);
326 // If only one tab is left, then remove the tab entry so that
327 // closing the last tab is not possible.
332 emit
tabCountChanged(count());
335 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
338 if (url
== QUrl("file:///")) {
341 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
342 if (name
.isEmpty()) {
345 // Make sure that a '&' inside the directory name is displayed correctly
346 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
347 name
.replace('&', "&&");