]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Simplify startup split view handling
[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
26 #include <QApplication>
27 #include <KConfigGroup>
28 #include <kio/global.h>
29 #include <KRun>
30
31 DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
32 QTabWidget(parent),
33 m_placesSelectorVisible(true)
34 {
35 connect(this, SIGNAL(tabCloseRequested(int)),
36 this, SLOT(closeTab(int)));
37 connect(this, SIGNAL(currentChanged(int)),
38 this, SLOT(currentTabChanged(int)));
39
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)));
47 tabBar->hide();
48
49 setTabBar(tabBar);
50 setDocumentMode(true);
51 setElideMode(Qt::ElideRight);
52 setUsesScrollButtons(true);
53 }
54
55 DolphinTabPage* DolphinTabWidget::currentTabPage() const
56 {
57 return tabPageAt(currentIndex());
58 }
59
60 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
61 {
62 return static_cast<DolphinTabPage*>(widget(index));
63 }
64
65 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
66 {
67 const int tabCount = count();
68 group.writeEntry("Tab Count", tabCount);
69 group.writeEntry("Active Tab Index", currentIndex());
70
71 for (int i = 0; i < tabCount; ++i) {
72 const DolphinTabPage* tabPage = tabPageAt(i);
73 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
74 }
75 }
76
77 void DolphinTabWidget::readProperties(const KConfigGroup& group)
78 {
79 const int tabCount = group.readEntry("Tab Count", 0);
80 for (int i = 0; i < tabCount; ++i) {
81 if (i >= count()) {
82 openNewActivatedTab();
83 }
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);
88 } else {
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);
92 }
93 }
94
95 const int index = group.readEntry("Active Tab Index", 0);
96 setCurrentIndex(index);
97 }
98
99 void DolphinTabWidget::refreshViews()
100 {
101 const int tabCount = count();
102 for (int i = 0; i < tabCount; ++i) {
103 tabPageAt(i)->refreshViews();
104 }
105 }
106
107 void DolphinTabWidget::openNewActivatedTab()
108 {
109 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
110 Q_ASSERT(oldActiveViewContainer);
111
112 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
113
114 openNewActivatedTab(oldActiveViewContainer->url());
115
116 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
117 Q_ASSERT(newActiveViewContainer);
118
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);
123
124 if (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();
128 }
129 }
130
131 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
132 {
133 openNewTab(primaryUrl, secondaryUrl);
134 setCurrentIndex(count() - 1);
135 }
136
137 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
138 {
139 QWidget* focusWidget = QApplication::focusWidget();
140
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));
148
149 if (focusWidget) {
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();
153 }
154 }
155
156 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
157 {
158 Q_ASSERT(dirs.size() > 0);
159
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);
166 } else {
167 openNewTab(primaryUrl);
168 }
169 }
170 }
171
172 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
173 {
174 Q_ASSERT(files.size() > 0);
175
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()).
179 QList<QUrl> dirs;
180 foreach (const QUrl& url, files) {
181 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
182 if (!dirs.contains(dir)) {
183 dirs.append(dir);
184 }
185 }
186
187 const int oldTabCount = count();
188 openDirectories(dirs, splitView);
189 const int tabCount = count();
190
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());
198 }
199 }
200
201 void DolphinTabWidget::closeTab()
202 {
203 closeTab(currentIndex());
204 }
205
206 void DolphinTabWidget::closeTab(const int index)
207 {
208 Q_ASSERT(index >= 0);
209 Q_ASSERT(index < count());
210
211 if (count() < 2) {
212 // Never close the last tab.
213 return;
214 }
215
216 DolphinTabPage* tabPage = tabPageAt(index);
217 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
218
219 removeTab(index);
220 tabPage->deleteLater();
221 }
222
223 void DolphinTabWidget::activateNextTab()
224 {
225 const int index = currentIndex() + 1;
226 setCurrentIndex(index < count() ? index : 0);
227 }
228
229 void DolphinTabWidget::activatePrevTab()
230 {
231 const int index = currentIndex() - 1;
232 setCurrentIndex(index >= 0 ? index : (count() - 1));
233 }
234
235 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
236 {
237 // The places-selector from the URL navigator should only be shown
238 // if the places dock is invisible
239 m_placesSelectorVisible = !visible;
240
241 const int tabCount = count();
242 for (int i = 0; i < tabCount; ++i) {
243 DolphinTabPage* tabPage = tabPageAt(i);
244 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
245 }
246 }
247
248 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
249 {
250 openNewActivatedTab();
251 currentTabPage()->restoreState(state);
252 }
253
254 void DolphinTabWidget::detachTab(int index)
255 {
256 Q_ASSERT(index >= 0);
257
258 const QString separator(QLatin1Char(' '));
259 QString command = QLatin1String("dolphin");
260
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");
266 }
267
268 KRun::runCommand(command, this);
269
270 closeTab(index);
271 }
272
273 void DolphinTabWidget::openNewActivatedTab(int index)
274 {
275 Q_ASSERT(index >= 0);
276 const DolphinTabPage* tabPage = tabPageAt(index);
277 openNewActivatedTab(tabPage->activeViewContainer()->url());
278 }
279
280 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
281 {
282 if (index >= 0) {
283 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
284 view->dropUrls(view->url(), event);
285 }
286 }
287
288 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
289 {
290 const int index = indexOf(qobject_cast<QWidget*>(sender()));
291 if (index >= 0) {
292 tabBar()->setTabText(index, tabName(url));
293 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
294
295 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
296 if (index == currentIndex()) {
297 emit currentUrlChanged(url);
298 }
299 }
300 }
301
302 void DolphinTabWidget::currentTabChanged(int index)
303 {
304 DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer();
305 emit activeViewChanged(viewContainer);
306 emit currentUrlChanged(viewContainer->url());
307 viewContainer->view()->setFocus();
308 }
309
310 void DolphinTabWidget::tabInserted(int index)
311 {
312 QTabWidget::tabInserted(index);
313
314 if (count() > 1) {
315 tabBar()->show();
316 }
317
318 emit tabCountChanged(count());
319 }
320
321 void DolphinTabWidget::tabRemoved(int index)
322 {
323 QTabWidget::tabRemoved(index);
324
325 // If only one tab is left, then remove the tab entry so that
326 // closing the last tab is not possible.
327 if (count() < 2) {
328 tabBar()->hide();
329 }
330
331 emit tabCountChanged(count());
332 }
333
334 QString DolphinTabWidget::tabName(const QUrl& url) const
335 {
336 QString name;
337 if (url == QUrl("file:///")) {
338 name = '/';
339 } else {
340 name = url.adjusted(QUrl::StripTrailingSlash).fileName();
341 if (name.isEmpty()) {
342 name = url.scheme();
343 } else {
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('&', "&&");
347 }
348 }
349 return name;
350 }