]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
42a8aff09e63c8a7d4f644cb06b7d12297b7e488
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"
26 #include "views/draganddrophelper.h"
28 #include <QApplication>
29 #include <KConfigGroup>
30 #include <kio/global.h>
33 DolphinTabWidget::DolphinTabWidget(QWidget
* parent
) :
35 m_placesSelectorVisible(true)
37 connect(this, SIGNAL(tabCloseRequested(int)),
38 this, SLOT(closeTab(int)));
39 connect(this, SIGNAL(currentChanged(int)),
40 this, SLOT(currentTabChanged(int)));
42 DolphinTabBar
* tabBar
= new DolphinTabBar(this);
43 connect(tabBar
, SIGNAL(openNewActivatedTab(int)),
44 this, SLOT(openNewActivatedTab(int)));
45 connect(tabBar
, SIGNAL(tabDropEvent(int,QDropEvent
*)),
46 this, SLOT(tabDropEvent(int,QDropEvent
*)));
47 connect(tabBar
, SIGNAL(tabDetachRequested(int)),
48 this, SLOT(detachTab(int)));
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 KUrl
& primaryUrl
, const KUrl
& secondaryUrl
)
135 openNewTab(primaryUrl
, secondaryUrl
);
136 setCurrentIndex(count() - 1);
139 void DolphinTabWidget::openNewTab(const KUrl
& primaryUrl
, const KUrl
& secondaryUrl
)
141 QWidget
* focusWidget
= QApplication::focusWidget();
143 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
144 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
145 connect(tabPage
, SIGNAL(activeViewChanged(DolphinViewContainer
*)),
146 this, SIGNAL(activeViewChanged(DolphinViewContainer
*)));
147 connect(tabPage
, SIGNAL(activeViewUrlChanged(KUrl
)),
148 this, SLOT(tabUrlChanged(KUrl
)));
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
<KUrl
>& dirs
)
160 const bool hasSplitView
= GeneralSettings::splitView();
162 // Open each directory inside a new tab. If the "split view" option has been enabled,
163 // always show two directories within one tab.
164 QList
<KUrl
>::const_iterator it
= dirs
.constBegin();
165 while (it
!= dirs
.constEnd()) {
166 const KUrl
& primaryUrl
= *(it
++);
167 if (hasSplitView
&& (it
!= dirs
.constEnd())) {
168 const KUrl
& secondaryUrl
= *(it
++);
169 openNewTab(primaryUrl
, secondaryUrl
);
171 openNewTab(primaryUrl
);
176 void DolphinTabWidget::openFiles(const QList
<KUrl
>& files
)
178 if (files
.isEmpty()) {
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 KUrl
& url
, files
) {
187 const KUrl
dir(url
.directory());
188 if (!dirs
.contains(dir
)) {
193 const int oldTabCount
= count();
194 openDirectories(dirs
);
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);
264 const QString
separator(QLatin1Char(' '));
265 QString command
= QLatin1String("dolphin");
267 const DolphinTabPage
* tabPage
= tabPageAt(index
);
268 command
+= separator
+ tabPage
->primaryViewContainer()->url().url();
269 if (tabPage
->splitViewEnabled()) {
270 command
+= separator
+ tabPage
->secondaryViewContainer()->url().url();
271 command
+= separator
+ QLatin1String("-split");
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 const DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
292 DragAndDropHelper::dropUrls(view
->rootItem(), view
->url(), event
, error
);
293 if (!error
.isEmpty()) {
294 currentTabPage()->activeViewContainer()->showMessage(error
, DolphinViewContainer::Error
);
299 void DolphinTabWidget::tabUrlChanged(const KUrl
& url
)
301 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
303 tabBar()->setTabText(index
, tabName(url
));
304 tabBar()->setTabIcon(index
, QIcon::fromTheme(KIO::iconNameForUrl(url
)));
306 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
307 if (index
== currentIndex()) {
308 emit
currentUrlChanged(url
);
313 void DolphinTabWidget::currentTabChanged(int index
)
315 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
316 emit
activeViewChanged(viewContainer
);
317 emit
currentUrlChanged(viewContainer
->url());
318 viewContainer
->view()->setFocus();
321 void DolphinTabWidget::tabInserted(int index
)
323 QTabWidget::tabInserted(index
);
329 emit
tabCountChanged(count());
332 void DolphinTabWidget::tabRemoved(int index
)
334 QTabWidget::tabRemoved(index
);
336 // If only one tab is left, then remove the tab entry so that
337 // closing the last tab is not possible.
342 emit
tabCountChanged(count());
345 QString
DolphinTabWidget::tabName(const KUrl
& url
) const
348 if (url
.equals(KUrl("file:///"))) {
351 name
= url
.fileName();
352 if (name
.isEmpty()) {
353 name
= url
.protocol();
355 // Make sure that a '&' inside the directory name is displayed correctly
356 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
357 name
.replace('&', "&&");