]>
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>
28 #include <kio/global.h>
31 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
33 m_placesSelectorVisible(true)
35 connect(this, SIGNAL(tabCloseRequested(int)),
36 this, SLOT(closeTab(int)));
37 connect(this, SIGNAL(currentChanged(int)),
38 this, SLOT(currentTabChanged(int)));
40 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
41 connect(tabBar
, SIGNAL(openNewActivatedTab(int)),
42 this, SLOT(openNewActivatedTab(int)));
43 connect(tabBar
, SIGNAL(tabDropEvent(int,QDropEvent
*)),
44 this, SLOT(tabDropEvent(int,QDropEvent
*)));
45 connect(tabBar
, SIGNAL(tabDetachRequested(int)),
46 this, SLOT(detachTab(int)));
50 setDocumentMode(true);
51 setElideMode(Qt::ElideRight
);
52 setUsesScrollButtons(true);
55 DolphinTabPage
* DolphinTabWidget::currentTabPage() const
57 return tabPageAt(currentIndex());
60 DolphinTabPage
* DolphinTabWidget::tabPageAt(const int index
) const
62 return static_cast<DolphinTabPage
*>(widget(index
));
65 void DolphinTabWidget::saveProperties(KConfigGroup
& group
) const
67 const int tabCount
= count();
68 group
.writeEntry("Tab Count", tabCount
);
69 group
.writeEntry("Active Tab Index", currentIndex());
71 for (int i
= 0; i
< tabCount
; ++i
) {
72 const DolphinTabPage
* tabPage
= tabPageAt(i
);
73 group
.writeEntry("Tab Data " % QString::number(i
), tabPage
->saveState());
77 void DolphinTabWidget::readProperties(const KConfigGroup
& group
)
79 const int tabCount
= group
.readEntry("Tab Count", 0);
80 for (int i
= 0; i
< tabCount
; ++i
) {
82 openNewActivatedTab();
84 if (group
.hasKey("Tab Data " % QString::number(i
))) {
85 // Tab state created with Dolphin > 4.14.x
86 const QByteArray state
= group
.readEntry("Tab Data " % QString::number(i
), QByteArray());
87 tabPageAt(i
)->restoreState(state
);
89 // Tab state created with Dolphin <= 4.14.x
90 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
91 tabPageAt(i
)->restoreStateV1(state
);
95 const int index
= group
.readEntry("Active Tab Index", 0);
96 setCurrentIndex(index
);
99 void DolphinTabWidget::refreshViews()
101 const int tabCount
= count();
102 for (int i
= 0; i
< tabCount
; ++i
) {
103 tabPageAt(i
)->refreshViews();
107 void DolphinTabWidget::openNewActivatedTab()
109 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
110 Q_ASSERT(oldActiveViewContainer
);
112 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
114 openNewActivatedTab(oldActiveViewContainer
->url());
116 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
117 Q_ASSERT(newActiveViewContainer
);
119 // The URL navigator of the new tab should have the same editable state
120 // as the current tab
121 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
122 navigator
->setUrlEditable(isUrlEditable
);
125 // If a new tab is opened and the URL is editable, assure that
126 // the user can edit the URL without manually setting the focus
127 navigator
->setFocus();
131 void DolphinTabWidget::openNewActivatedTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
133 openNewTab(primaryUrl
, secondaryUrl
);
134 setCurrentIndex(count() - 1);
137 void DolphinTabWidget::openNewTab(const QUrl
& primaryUrl
, const QUrl
& secondaryUrl
)
139 QWidget
* focusWidget
= QApplication::focusWidget();
141 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
142 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
143 connect(tabPage
, SIGNAL(activeViewChanged(DolphinViewContainer
*)),
144 this, SIGNAL(activeViewChanged(DolphinViewContainer
*)));
145 connect(tabPage
, SIGNAL(activeViewUrlChanged(QUrl
)),
146 this, SLOT(tabUrlChanged(QUrl
)));
147 addTab(tabPage
, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
150 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
151 // in background, assure that the previous focused widget gets the focus back.
152 focusWidget
->setFocus();
156 void DolphinTabWidget::openDirectories(const QList
<QUrl
>& dirs
, bool splitView
)
158 Q_ASSERT(dirs
.size() > 0);
160 QList
<QUrl
>::const_iterator it
= dirs
.constBegin();
161 while (it
!= dirs
.constEnd()) {
162 const QUrl
& primaryUrl
= *(it
++);
163 if (splitView
&& (it
!= dirs
.constEnd())) {
164 const QUrl
& secondaryUrl
= *(it
++);
165 openNewTab(primaryUrl
, secondaryUrl
);
167 openNewTab(primaryUrl
);
172 void DolphinTabWidget::openFiles(const QList
<QUrl
>& files
, bool splitView
)
174 Q_ASSERT(files
.size() > 0);
176 // Get all distinct directories from 'files' and open a tab
177 // for each directory. If the "split view" option is enabled, two
178 // directories are shown inside one tab (see openDirectories()).
180 foreach (const QUrl
& url
, files
) {
181 const QUrl
dir(url
.adjusted(QUrl::RemoveFilename
));
182 if (!dirs
.contains(dir
)) {
187 const int oldTabCount
= count();
188 openDirectories(dirs
, splitView
);
189 const int tabCount
= count();
191 // Select the files. Although the files can be split between several
192 // tabs, there is no need to split 'files' accordingly, as
193 // the DolphinView will just ignore invalid selections.
194 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
195 DolphinTabPage
* tabPage
= tabPageAt(i
);
196 tabPage
->markUrlsAsSelected(files
);
197 tabPage
->markUrlAsCurrent(files
.first());
201 void DolphinTabWidget::closeTab()
203 closeTab(currentIndex());
206 void DolphinTabWidget::closeTab(const int index
)
208 Q_ASSERT(index
>= 0);
209 Q_ASSERT(index
< count());
212 // Never close the last tab.
216 DolphinTabPage
* tabPage
= tabPageAt(index
);
217 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
220 tabPage
->deleteLater();
223 void DolphinTabWidget::activateNextTab()
225 const int index
= currentIndex() + 1;
226 setCurrentIndex(index
< count() ? index
: 0);
229 void DolphinTabWidget::activatePrevTab()
231 const int index
= currentIndex() - 1;
232 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
235 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
237 // The places-selector from the URL navigator should only be shown
238 // if the places dock is invisible
239 m_placesSelectorVisible
= !visible
;
241 const int tabCount
= count();
242 for (int i
= 0; i
< tabCount
; ++i
) {
243 DolphinTabPage
* tabPage
= tabPageAt(i
);
244 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
248 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
250 openNewActivatedTab();
251 currentTabPage()->restoreState(state
);
254 void DolphinTabWidget::detachTab(int index
)
256 Q_ASSERT(index
>= 0);
258 const QString
separator(QLatin1Char(' '));
259 QString command
= QLatin1String("dolphin");
261 const DolphinTabPage
* tabPage
= tabPageAt(index
);
262 command
+= separator
+ tabPage
->primaryViewContainer()->url().url();
263 if (tabPage
->splitViewEnabled()) {
264 command
+= separator
+ tabPage
->secondaryViewContainer()->url().url();
265 command
+= separator
+ QLatin1String("-split");
268 KRun::runCommand(command
, this);
273 void DolphinTabWidget::openNewActivatedTab(int index
)
275 Q_ASSERT(index
>= 0);
276 const DolphinTabPage
* tabPage
= tabPageAt(index
);
277 openNewActivatedTab(tabPage
->activeViewContainer()->url());
280 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
283 DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
284 view
->dropUrls(view
->url(), event
);
288 void DolphinTabWidget::tabUrlChanged(const QUrl
& url
)
290 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
292 tabBar()->setTabText(index
, tabName(url
));
293 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
295 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
296 if (index
== currentIndex()) {
297 emit
currentUrlChanged(url
);
302 void DolphinTabWidget::currentTabChanged(int index
)
304 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
305 emit
activeViewChanged(viewContainer
);
306 emit
currentUrlChanged(viewContainer
->url());
307 viewContainer
->view()->setFocus();
310 void DolphinTabWidget::tabInserted(int index
)
312 QTabWidget::tabInserted(index
);
318 emit
tabCountChanged(count());
321 void DolphinTabWidget::tabRemoved(int index
)
323 QTabWidget::tabRemoved(index
);
325 // If only one tab is left, then remove the tab entry so that
326 // closing the last tab is not possible.
331 emit
tabCountChanged(count());
334 QString
DolphinTabWidget::tabName(const QUrl
& url
) const
337 if (url
== QUrl("file:///")) {
340 name
= url
.adjusted(QUrl::StripTrailingSlash
).fileName();
341 if (name
.isEmpty()) {
344 // Make sure that a '&' inside the directory name is displayed correctly
345 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
346 name
.replace('&', "&&");