]>
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"
25 #include "dolphin_generalsettings.h"
27 #include <QApplication>
28 #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
)
159 const bool hasSplitView
= GeneralSettings::splitView();
161 // Open each directory inside a new tab. If the "split view" option has been enabled,
162 // always show two directories within one tab.
163 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
164 while (it
!= dirs
.constEnd()) {
165 const QUrl
& primaryUrl
= *(it
++);
166 if (hasSplitView
&& (it
!= dirs
.constEnd())) {
167 const QUrl
& secondaryUrl
= *(it
++);
168 openNewTab(primaryUrl
, secondaryUrl
);
170 openNewTab(primaryUrl
);
175 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
)
177 if (files
.isEmpty()) {
181 // Get all distinct directories from 'files' and open a tab
182 // for each directory. If the "split view" option is enabled, two
183 // directories are shown inside one tab (see openDirectories()).
185 foreach (const QUrl
& url
, files
) {
186 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
187 if (!dirs
.contains(dir
)) {
192 const int oldTabCount
= count();
193 openDirectories(dirs
);
194 const int tabCount
= count();
196 // Select the files. Although the files can be split between several
197 // tabs, there is no need to split 'files' accordingly, as
198 // the DolphinView will just ignore invalid selections.
199 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
200 DolphinTabPage
* tabPage
= tabPageAt(i
);
201 tabPage
->markUrlsAsSelected(files
);
202 tabPage
->markUrlAsCurrent(files
.first());
206 void DolphinTabWidget::closeTab()
208 closeTab(currentIndex());
211 void DolphinTabWidget::closeTab(const int index
)
213 Q_ASSERT(index
>= 0);
214 Q_ASSERT(index
< count());
217 // Never close the last tab.
221 DolphinTabPage
* tabPage
= tabPageAt(index
);
222 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
225 tabPage
->deleteLater();
228 void DolphinTabWidget::activateNextTab()
230 const int index
= currentIndex() + 1;
231 setCurrentIndex(index
< count() ? index
: 0);
234 void DolphinTabWidget::activatePrevTab()
236 const int index
= currentIndex() - 1;
237 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
240 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
242 // The places-selector from the URL navigator should only be shown
243 // if the places dock is invisible
244 m_placesSelectorVisible
= !visible
;
246 const int tabCount
= count();
247 for (int i
= 0; i
< tabCount
; ++i
) {
248 DolphinTabPage
* tabPage
= tabPageAt(i
);
249 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
253 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
255 openNewActivatedTab();
256 currentTabPage()->restoreState(state
);
259 void DolphinTabWidget::detachTab(int index
)
261 Q_ASSERT(index
>= 0);
263 const QString
separator(QLatin1Char(' '));
264 QString command
= QLatin1String("dolphin");
266 const DolphinTabPage
* tabPage
= tabPageAt(index
);
267 command
+= separator
+ tabPage
->primaryViewContainer()->url().url();
268 if (tabPage
->splitViewEnabled()) {
269 command
+= separator
+ tabPage
->secondaryViewContainer()->url().url();
270 command
+= separator
+ QLatin1String("-split");
273 KRun::runCommand(command
, this);
278 void DolphinTabWidget::openNewActivatedTab(int index
)
280 Q_ASSERT(index
>= 0);
281 const DolphinTabPage
* tabPage
= tabPageAt(index
);
282 openNewActivatedTab(tabPage
->activeViewContainer()->url());
285 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
288 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
289 view
->dropUrls(view
->url(), event
);
293 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
295 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
297 tabBar()->setTabText(index
, tabName(url
));
298 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
300 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
301 if (index
== currentIndex()) {
302 emit
currentUrlChanged(url
);
307 void DolphinTabWidget::currentTabChanged(int index
)
309 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
310 emit
activeViewChanged(viewContainer
);
311 emit
currentUrlChanged(viewContainer
->url());
312 viewContainer
->view()->setFocus();
315 void DolphinTabWidget::tabInserted(int index
)
317 QTabWidget::tabInserted(index
);
323 emit
tabCountChanged(count());
326 void DolphinTabWidget::tabRemoved(int index
)
328 QTabWidget::tabRemoved(index
);
330 // If only one tab is left, then remove the tab entry so that
331 // closing the last tab is not possible.
336 emit
tabCountChanged(count());
339 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
342 if (url
== QUrl("file:///")) {
345 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
346 if (name
.isEmpty()) {
349 // Make sure that a '&' inside the directory name is displayed correctly
350 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
351 name
.replace('&', "&&");