]>
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"
26 #include "views/draganddrophelper.h"
28 #include <QApplication>
29 #include <KConfigGroup>
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 " % 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 const QByteArray state
= group
.readEntry("Tab " % QString::number(i
), QByteArray());
87 tabPageAt(i
)->restoreState(state
);
90 const int index
= group
.readEntry("Active Tab Index", 0);
91 setCurrentIndex(index
);
94 void DolphinTabWidget::refreshViews()
96 const int tabCount
= count();
97 for (int i
= 0; i
< tabCount
; ++i
) {
98 tabPageAt(i
)->refreshViews();
102 void DolphinTabWidget::openNewActivatedTab()
104 const DolphinViewContainer
* oldActiveViewContainer
= currentTabPage()->activeViewContainer();
105 Q_ASSERT(oldActiveViewContainer
);
107 const bool isUrlEditable
= oldActiveViewContainer
->urlNavigator()->isUrlEditable();
109 openNewActivatedTab(oldActiveViewContainer
->url());
111 DolphinViewContainer
* newActiveViewContainer
= currentTabPage()->activeViewContainer();
112 Q_ASSERT(newActiveViewContainer
);
114 // The URL navigator of the new tab should have the same editable state
115 // as the current tab
116 KUrlNavigator
* navigator
= newActiveViewContainer
->urlNavigator();
117 navigator
->setUrlEditable(isUrlEditable
);
120 // If a new tab is opened and the URL is editable, assure that
121 // the user can edit the URL without manually setting the focus
122 navigator
->setFocus();
126 void DolphinTabWidget::openNewActivatedTab(const KUrl
& primaryUrl
, const KUrl
& secondaryUrl
)
128 openNewTab(primaryUrl
, secondaryUrl
);
129 setCurrentIndex(count() - 1);
132 void DolphinTabWidget::openNewTab(const KUrl
& primaryUrl
, const KUrl
& secondaryUrl
)
134 QWidget
* focusWidget
= QApplication::focusWidget();
136 DolphinTabPage
* tabPage
= new DolphinTabPage(primaryUrl
, secondaryUrl
, this);
137 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
138 connect(tabPage
, SIGNAL(activeViewChanged(DolphinViewContainer
*)),
139 this, SIGNAL(activeViewChanged(DolphinViewContainer
*)));
140 connect(tabPage
, SIGNAL(activeViewUrlChanged(KUrl
)),
141 this, SLOT(tabUrlChanged(KUrl
)));
142 addTab(tabPage
, KIcon(KMimeType::iconNameForUrl(primaryUrl
)), tabName(primaryUrl
));
145 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
146 // in background, assure that the previous focused widget gets the focus back.
147 focusWidget
->setFocus();
151 void DolphinTabWidget::openDirectories(const QList
<KUrl
>& dirs
)
153 const bool hasSplitView
= GeneralSettings::splitView();
155 // Open each directory inside a new tab. If the "split view" option has been enabled,
156 // always show two directories within one tab.
157 QList
<KUrl
>::const_iterator it
= dirs
.constBegin();
158 while (it
!= dirs
.constEnd()) {
159 const KUrl
& primaryUrl
= *(it
++);
160 if (hasSplitView
&& (it
!= dirs
.constEnd())) {
161 const KUrl
& secondaryUrl
= *(it
++);
162 openNewTab(primaryUrl
, secondaryUrl
);
164 openNewTab(primaryUrl
);
169 void DolphinTabWidget::openFiles(const QList
<KUrl
>& files
)
171 if (files
.isEmpty()) {
175 // Get all distinct directories from 'files' and open a tab
176 // for each directory. If the "split view" option is enabled, two
177 // directories are shown inside one tab (see openDirectories()).
179 foreach (const KUrl
& url
, files
) {
180 const KUrl
dir(url
.directory());
181 if (!dirs
.contains(dir
)) {
186 const int oldTabCount
= count();
187 openDirectories(dirs
);
188 const int tabCount
= count();
190 // Select the files. Although the files can be split between several
191 // tabs, there is no need to split 'files' accordingly, as
192 // the DolphinView will just ignore invalid selections.
193 for (int i
= oldTabCount
; i
< tabCount
; ++i
) {
194 DolphinTabPage
* tabPage
= tabPageAt(i
);
195 tabPage
->markUrlsAsSelected(files
);
196 tabPage
->markUrlAsCurrent(files
.first());
200 void DolphinTabWidget::closeTab()
202 closeTab(currentIndex());
205 void DolphinTabWidget::closeTab(const int index
)
207 Q_ASSERT(index
>= 0);
208 Q_ASSERT(index
< count());
211 // Never close the last tab.
215 DolphinTabPage
* tabPage
= tabPageAt(index
);
216 emit
rememberClosedTab(tabPage
->activeViewContainer()->url(), tabPage
->saveState());
219 tabPage
->deleteLater();
222 void DolphinTabWidget::activateNextTab()
224 const int index
= currentIndex() + 1;
225 setCurrentIndex(index
< count() ? index
: 0);
228 void DolphinTabWidget::activatePrevTab()
230 const int index
= currentIndex() - 1;
231 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
234 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
236 // The places-selector from the URL navigator should only be shown
237 // if the places dock is invisible
238 m_placesSelectorVisible
= !visible
;
240 const int tabCount
= count();
241 for (int i
= 0; i
< tabCount
; ++i
) {
242 DolphinTabPage
* tabPage
= tabPageAt(i
);
243 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
247 void DolphinTabWidget::restoreClosedTab(const QByteArray
& state
)
249 openNewActivatedTab();
250 currentTabPage()->restoreState(state
);
253 void DolphinTabWidget::detachTab(int index
)
255 Q_ASSERT(index
>= 0);
257 const QString
separator(QLatin1Char(' '));
258 QString command
= QLatin1String("dolphin");
260 const DolphinTabPage
* tabPage
= tabPageAt(index
);
261 command
+= separator
+ tabPage
->primaryViewContainer()->url().url();
262 if (tabPage
->splitViewEnabled()) {
263 command
+= separator
+ tabPage
->secondaryViewContainer()->url().url();
264 command
+= separator
+ QLatin1String("-split");
267 KRun::runCommand(command
, this);
272 void DolphinTabWidget::openNewActivatedTab(int index
)
274 Q_ASSERT(index
>= 0);
275 const DolphinTabPage
* tabPage
= tabPageAt(index
);
276 openNewActivatedTab(tabPage
->activeViewContainer()->url());
279 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
282 const DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
285 DragAndDropHelper::dropUrls(view
->rootItem(), view
->url(), event
, error
);
286 if (!error
.isEmpty()) {
287 currentTabPage()->activeViewContainer()->showMessage(error
, DolphinViewContainer::Error
);
292 void DolphinTabWidget::tabUrlChanged(const KUrl
& url
)
294 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
296 tabBar()->setTabText(index
, tabName(url
));
297 tabBar()->setTabIcon(index
, KIcon(KMimeType::iconNameForUrl(url
)));
299 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
300 if (index
== currentIndex()) {
301 emit
currentUrlChanged(url
);
306 void DolphinTabWidget::currentTabChanged(int index
)
308 DolphinViewContainer
* viewContainer
= tabPageAt(index
)->activeViewContainer();
309 emit
activeViewChanged(viewContainer
);
310 emit
currentUrlChanged(viewContainer
->url());
313 void DolphinTabWidget::tabInserted(int index
)
315 QTabWidget::tabInserted(index
);
321 emit
tabCountChanged(count());
324 void DolphinTabWidget::tabRemoved(int index
)
326 QTabWidget::tabRemoved(index
);
328 // If only one tab is left, then remove the tab entry so that
329 // closing the last tab is not possible.
334 emit
tabCountChanged(count());
337 QString
DolphinTabWidget::tabName(const KUrl
& url
) const
340 if (url
.equals(KUrl("file:///"))) {
343 name
= url
.fileName();
344 if (name
.isEmpty()) {
345 name
= url
.protocol();
347 // Make sure that a '&' inside the directory name is displayed correctly
348 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
349 name
.replace('&', "&&");