]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Merge branch 'Applications/16.04'
[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 <QDropEvent>
28 #include <KConfigGroup>
29 #include <KShell>
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, &DolphinTabWidget::tabCloseRequested,
38 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab));
39 connect(this, &DolphinTabWidget::currentChanged,
40 this, &DolphinTabWidget::currentTabChanged);
41
42 DolphinTabBar* tabBar = new DolphinTabBar(this);
43 connect(tabBar, &DolphinTabBar::openNewActivatedTab,
44 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab));
45 connect(tabBar, &DolphinTabBar::tabDropEvent,
46 this, &DolphinTabWidget::tabDropEvent);
47 connect(tabBar, &DolphinTabBar::tabDetachRequested,
48 this, &DolphinTabWidget::detachTab);
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 QUrl& primaryUrl, const QUrl& secondaryUrl)
134 {
135 openNewTab(primaryUrl, secondaryUrl);
136 setCurrentIndex(count() - 1);
137 }
138
139 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
140 {
141 QWidget* focusWidget = QApplication::focusWidget();
142
143 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
144 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
145 connect(tabPage, &DolphinTabPage::activeViewChanged,
146 this, &DolphinTabWidget::activeViewChanged);
147 connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
148 this, &DolphinTabWidget::tabUrlChanged);
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<QUrl>& dirs, bool splitView)
159 {
160 Q_ASSERT(dirs.size() > 0);
161
162 QList<QUrl>::const_iterator it = dirs.constBegin();
163 while (it != dirs.constEnd()) {
164 const QUrl& primaryUrl = *(it++);
165 if (splitView && (it != dirs.constEnd())) {
166 const QUrl& secondaryUrl = *(it++);
167 openNewTab(primaryUrl, secondaryUrl);
168 } else {
169 openNewTab(primaryUrl);
170 }
171 }
172 }
173
174 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
175 {
176 Q_ASSERT(files.size() > 0);
177
178 // Get all distinct directories from 'files' and open a tab
179 // for each directory. If the "split view" option is enabled, two
180 // directories are shown inside one tab (see openDirectories()).
181 QList<QUrl> dirs;
182 foreach (const QUrl& url, files) {
183 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
184 if (!dirs.contains(dir)) {
185 dirs.append(dir);
186 }
187 }
188
189 const int oldTabCount = count();
190 openDirectories(dirs, splitView);
191 const int tabCount = count();
192
193 // Select the files. Although the files can be split between several
194 // tabs, there is no need to split 'files' accordingly, as
195 // the DolphinView will just ignore invalid selections.
196 for (int i = oldTabCount; i < tabCount; ++i) {
197 DolphinTabPage* tabPage = tabPageAt(i);
198 tabPage->markUrlsAsSelected(files);
199 tabPage->markUrlAsCurrent(files.first());
200 }
201 }
202
203 void DolphinTabWidget::closeTab()
204 {
205 closeTab(currentIndex());
206 }
207
208 void DolphinTabWidget::closeTab(const int index)
209 {
210 Q_ASSERT(index >= 0);
211 Q_ASSERT(index < count());
212
213 if (count() < 2) {
214 // Never close the last tab.
215 return;
216 }
217
218 DolphinTabPage* tabPage = tabPageAt(index);
219 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
220
221 removeTab(index);
222 tabPage->deleteLater();
223 }
224
225 void DolphinTabWidget::activateNextTab()
226 {
227 const int index = currentIndex() + 1;
228 setCurrentIndex(index < count() ? index : 0);
229 }
230
231 void DolphinTabWidget::activatePrevTab()
232 {
233 const int index = currentIndex() - 1;
234 setCurrentIndex(index >= 0 ? index : (count() - 1));
235 }
236
237 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
238 {
239 // The places-selector from the URL navigator should only be shown
240 // if the places dock is invisible
241 m_placesSelectorVisible = !visible;
242
243 const int tabCount = count();
244 for (int i = 0; i < tabCount; ++i) {
245 DolphinTabPage* tabPage = tabPageAt(i);
246 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
247 }
248 }
249
250 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
251 {
252 openNewActivatedTab();
253 currentTabPage()->restoreState(state);
254 }
255
256 void DolphinTabWidget::detachTab(int index)
257 {
258 Q_ASSERT(index >= 0);
259
260 QStringList args;
261
262 const DolphinTabPage* tabPage = tabPageAt(index);
263 args << tabPage->primaryViewContainer()->url().url();
264 if (tabPage->splitViewEnabled()) {
265 args << tabPage->secondaryViewContainer()->url().url();
266 args << QStringLiteral("--split");
267 }
268
269 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
270 KRun::runCommand(command, this);
271
272 closeTab(index);
273 }
274
275 void DolphinTabWidget::openNewActivatedTab(int index)
276 {
277 Q_ASSERT(index >= 0);
278 const DolphinTabPage* tabPage = tabPageAt(index);
279 openNewActivatedTab(tabPage->activeViewContainer()->url());
280 }
281
282 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
283 {
284 if (index >= 0) {
285 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
286 view->dropUrls(view->url(), event);
287 }
288 }
289
290 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
291 {
292 const int index = indexOf(qobject_cast<QWidget*>(sender()));
293 if (index >= 0) {
294 tabBar()->setTabText(index, tabName(url));
295 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
296
297 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
298 if (index == currentIndex()) {
299 emit currentUrlChanged(url);
300 }
301 }
302 }
303
304 void DolphinTabWidget::currentTabChanged(int index)
305 {
306 DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer();
307 emit activeViewChanged(viewContainer);
308 emit currentUrlChanged(viewContainer->url());
309 viewContainer->view()->setFocus();
310 }
311
312 void DolphinTabWidget::tabInserted(int index)
313 {
314 QTabWidget::tabInserted(index);
315
316 if (count() > 1) {
317 tabBar()->show();
318 }
319
320 emit tabCountChanged(count());
321 }
322
323 void DolphinTabWidget::tabRemoved(int index)
324 {
325 QTabWidget::tabRemoved(index);
326
327 // If only one tab is left, then remove the tab entry so that
328 // closing the last tab is not possible.
329 if (count() < 2) {
330 tabBar()->hide();
331 }
332
333 emit tabCountChanged(count());
334 }
335
336 QString DolphinTabWidget::tabName(const QUrl& url) const
337 {
338 QString name;
339 if (url == QUrl(QStringLiteral("file:///"))) {
340 name = '/';
341 } else {
342 name = url.adjusted(QUrl::StripTrailingSlash).fileName();
343 if (name.isEmpty()) {
344 name = url.scheme();
345 } else {
346 // Make sure that a '&' inside the directory name is displayed correctly
347 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
348 name.replace('&', QLatin1String("&&"));
349 }
350 }
351 return name;
352 }