]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
42a8aff09e63c8a7d4f644cb06b7d12297b7e488
[dolphin.git] / src / dolphintabwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphintabwidget.h"
21
22 #include "dolphintabbar.h"
23 #include "dolphintabpage.h"
24 #include "dolphinviewcontainer.h"
25 #include "dolphin_generalsettings.h"
26 #include "views/draganddrophelper.h"
27
28 #include <QApplication>
29 #include <KConfigGroup>
30 #include <kio/global.h>
31 #include <KRun>
32
33 DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
34 QTabWidget(parent),
35 m_placesSelectorVisible(true)
36 {
37 connect(this, SIGNAL(tabCloseRequested(int)),
38 this, SLOT(closeTab(int)));
39 connect(this, SIGNAL(currentChanged(int)),
40 this, SLOT(currentTabChanged(int)));
41
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)));
49 tabBar->hide();
50
51 setTabBar(tabBar);
52 setDocumentMode(true);
53 setElideMode(Qt::ElideRight);
54 setUsesScrollButtons(true);
55 }
56
57 DolphinTabPage* DolphinTabWidget::currentTabPage() const
58 {
59 return tabPageAt(currentIndex());
60 }
61
62 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
63 {
64 return static_cast<DolphinTabPage*>(widget(index));
65 }
66
67 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
68 {
69 const int tabCount = count();
70 group.writeEntry("Tab Count", tabCount);
71 group.writeEntry("Active Tab Index", currentIndex());
72
73 for (int i = 0; i < tabCount; ++i) {
74 const DolphinTabPage* tabPage = tabPageAt(i);
75 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
76 }
77 }
78
79 void DolphinTabWidget::readProperties(const KConfigGroup& group)
80 {
81 const int tabCount = group.readEntry("Tab Count", 0);
82 for (int i = 0; i < tabCount; ++i) {
83 if (i >= count()) {
84 openNewActivatedTab();
85 }
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);
90 } else {
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);
94 }
95 }
96
97 const int index = group.readEntry("Active Tab Index", 0);
98 setCurrentIndex(index);
99 }
100
101 void DolphinTabWidget::refreshViews()
102 {
103 const int tabCount = count();
104 for (int i = 0; i < tabCount; ++i) {
105 tabPageAt(i)->refreshViews();
106 }
107 }
108
109 void DolphinTabWidget::openNewActivatedTab()
110 {
111 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
112 Q_ASSERT(oldActiveViewContainer);
113
114 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
115
116 openNewActivatedTab(oldActiveViewContainer->url());
117
118 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
119 Q_ASSERT(newActiveViewContainer);
120
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);
125
126 if (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();
130 }
131 }
132
133 void DolphinTabWidget::openNewActivatedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
134 {
135 openNewTab(primaryUrl, secondaryUrl);
136 setCurrentIndex(count() - 1);
137 }
138
139 void DolphinTabWidget::openNewTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
140 {
141 QWidget* focusWidget = QApplication::focusWidget();
142
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));
150
151 if (focusWidget) {
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();
155 }
156 }
157
158 void DolphinTabWidget::openDirectories(const QList<KUrl>& dirs)
159 {
160 const bool hasSplitView = GeneralSettings::splitView();
161
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);
170 } else {
171 openNewTab(primaryUrl);
172 }
173 }
174 }
175
176 void DolphinTabWidget::openFiles(const QList<KUrl>& files)
177 {
178 if (files.isEmpty()) {
179 return;
180 }
181
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()).
185 QList<KUrl> dirs;
186 foreach (const KUrl& url, files) {
187 const KUrl dir(url.directory());
188 if (!dirs.contains(dir)) {
189 dirs.append(dir);
190 }
191 }
192
193 const int oldTabCount = count();
194 openDirectories(dirs);
195 const int tabCount = count();
196
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());
204 }
205 }
206
207 void DolphinTabWidget::closeTab()
208 {
209 closeTab(currentIndex());
210 }
211
212 void DolphinTabWidget::closeTab(const int index)
213 {
214 Q_ASSERT(index >= 0);
215 Q_ASSERT(index < count());
216
217 if (count() < 2) {
218 // Never close the last tab.
219 return;
220 }
221
222 DolphinTabPage* tabPage = tabPageAt(index);
223 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
224
225 removeTab(index);
226 tabPage->deleteLater();
227 }
228
229 void DolphinTabWidget::activateNextTab()
230 {
231 const int index = currentIndex() + 1;
232 setCurrentIndex(index < count() ? index : 0);
233 }
234
235 void DolphinTabWidget::activatePrevTab()
236 {
237 const int index = currentIndex() - 1;
238 setCurrentIndex(index >= 0 ? index : (count() - 1));
239 }
240
241 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
242 {
243 // The places-selector from the URL navigator should only be shown
244 // if the places dock is invisible
245 m_placesSelectorVisible = !visible;
246
247 const int tabCount = count();
248 for (int i = 0; i < tabCount; ++i) {
249 DolphinTabPage* tabPage = tabPageAt(i);
250 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
251 }
252 }
253
254 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
255 {
256 openNewActivatedTab();
257 currentTabPage()->restoreState(state);
258 }
259
260 void DolphinTabWidget::detachTab(int index)
261 {
262 Q_ASSERT(index >= 0);
263
264 const QString separator(QLatin1Char(' '));
265 QString command = QLatin1String("dolphin");
266
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");
272 }
273
274 KRun::runCommand(command, this);
275
276 closeTab(index);
277 }
278
279 void DolphinTabWidget::openNewActivatedTab(int index)
280 {
281 Q_ASSERT(index >= 0);
282 const DolphinTabPage* tabPage = tabPageAt(index);
283 openNewActivatedTab(tabPage->activeViewContainer()->url());
284 }
285
286 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
287 {
288 if (index >= 0) {
289 const DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
290
291 QString error;
292 DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error);
293 if (!error.isEmpty()) {
294 currentTabPage()->activeViewContainer()->showMessage(error, DolphinViewContainer::Error);
295 }
296 }
297 }
298
299 void DolphinTabWidget::tabUrlChanged(const KUrl& url)
300 {
301 const int index = indexOf(qobject_cast<QWidget*>(sender()));
302 if (index >= 0) {
303 tabBar()->setTabText(index, tabName(url));
304 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
305
306 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
307 if (index == currentIndex()) {
308 emit currentUrlChanged(url);
309 }
310 }
311 }
312
313 void DolphinTabWidget::currentTabChanged(int index)
314 {
315 DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer();
316 emit activeViewChanged(viewContainer);
317 emit currentUrlChanged(viewContainer->url());
318 viewContainer->view()->setFocus();
319 }
320
321 void DolphinTabWidget::tabInserted(int index)
322 {
323 QTabWidget::tabInserted(index);
324
325 if (count() > 1) {
326 tabBar()->show();
327 }
328
329 emit tabCountChanged(count());
330 }
331
332 void DolphinTabWidget::tabRemoved(int index)
333 {
334 QTabWidget::tabRemoved(index);
335
336 // If only one tab is left, then remove the tab entry so that
337 // closing the last tab is not possible.
338 if (count() < 2) {
339 tabBar()->hide();
340 }
341
342 emit tabCountChanged(count());
343 }
344
345 QString DolphinTabWidget::tabName(const KUrl& url) const
346 {
347 QString name;
348 if (url.equals(KUrl("file:///"))) {
349 name = '/';
350 } else {
351 name = url.fileName();
352 if (name.isEmpty()) {
353 name = url.protocol();
354 } else {
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('&', "&&");
358 }
359 }
360 return name;
361 }