]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
4bb70b4eab62a75151b1f07a9b509113583cd9f6
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 if (tabPage
->splitViewEnabled()) {
217 emit
rememberClosedTab(tabPage
->primaryViewContainer()->url(),
218 tabPage
->secondaryViewContainer()->url());
220 emit
rememberClosedTab(tabPage
->primaryViewContainer()->url(), KUrl());
224 tabPage
->deleteLater();
227 void DolphinTabWidget::activateNextTab()
229 const int index
= currentIndex() + 1;
230 setCurrentIndex(index
< count() ? index
: 0);
233 void DolphinTabWidget::activatePrevTab()
235 const int index
= currentIndex() - 1;
236 setCurrentIndex(index
>= 0 ? index
: (count() - 1));
239 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible
)
241 // The places-selector from the URL navigator should only be shown
242 // if the places dock is invisible
243 m_placesSelectorVisible
= !visible
;
245 const int tabCount
= count();
246 for (int i
= 0; i
< tabCount
; ++i
) {
247 DolphinTabPage
* tabPage
= tabPageAt(i
);
248 tabPage
->setPlacesSelectorVisible(m_placesSelectorVisible
);
252 void DolphinTabWidget::detachTab(int index
)
254 Q_ASSERT(index
>= 0);
256 const QString
separator(QLatin1Char(' '));
257 QString command
= QLatin1String("dolphin");
259 const DolphinTabPage
* tabPage
= tabPageAt(index
);
260 command
+= separator
+ tabPage
->primaryViewContainer()->url().url();
261 if (tabPage
->splitViewEnabled()) {
262 command
+= separator
+ tabPage
->secondaryViewContainer()->url().url();
263 command
+= separator
+ QLatin1String("-split");
266 KRun::runCommand(command
, this);
271 void DolphinTabWidget::openNewActivatedTab(int index
)
273 Q_ASSERT(index
>= 0);
274 const DolphinTabPage
* tabPage
= tabPageAt(index
);
275 openNewActivatedTab(tabPage
->activeViewContainer()->url());
278 void DolphinTabWidget::tabDropEvent(int index
, QDropEvent
* event
)
281 const DolphinView
* view
= tabPageAt(index
)->activeViewContainer()->view();
284 DragAndDropHelper::dropUrls(view
->rootItem(), view
->url(), event
, error
);
285 if (!error
.isEmpty()) {
286 currentTabPage()->activeViewContainer()->showMessage(error
, DolphinViewContainer::Error
);
291 void DolphinTabWidget::tabUrlChanged(const KUrl
& url
)
293 const int index
= indexOf(qobject_cast
<QWidget
*>(sender()));
295 tabBar()->setTabText(index
, tabName(url
));
296 tabBar()->setTabIcon(index
, KIcon(KMimeType::iconNameForUrl(url
)));
300 void DolphinTabWidget::currentTabChanged(int index
)
302 emit
activeViewChanged(tabPageAt(index
)->activeViewContainer());
305 void DolphinTabWidget::tabInserted(int index
)
307 QTabWidget::tabInserted(index
);
313 emit
tabCountChanged(count());
316 void DolphinTabWidget::tabRemoved(int index
)
318 QTabWidget::tabRemoved(index
);
320 // If only one tab is left, then remove the tab entry so that
321 // closing the last tab is not possible.
326 emit
tabCountChanged(count());
329 QString
DolphinTabWidget::tabName(const KUrl
& url
) const
332 if (url
.equals(KUrl("file:///"))) {
335 name
= url
.fileName();
336 if (name
.isEmpty()) {
337 name
= url
.protocol();
339 // Make sure that a '&' inside the directory name is displayed correctly
340 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
341 name
.replace('&', "&&");