]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Merge branch 'Applications/18.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 <KConfigGroup>
27 #include <KRun>
28 #include <KShell>
29 #include <kio/global.h>
30
31 #include <QApplication>
32 #include <QDropEvent>
33
34 DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
35 QTabWidget(parent),
36 m_placesSelectorVisible(true),
37 m_previousTab(0)
38 {
39 connect(this, &DolphinTabWidget::tabCloseRequested,
40 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab));
41 connect(this, &DolphinTabWidget::currentChanged,
42 this, &DolphinTabWidget::currentTabChanged);
43
44 DolphinTabBar* tabBar = new DolphinTabBar(this);
45 connect(tabBar, &DolphinTabBar::openNewActivatedTab,
46 this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab));
47 connect(tabBar, &DolphinTabBar::tabDropEvent,
48 this, &DolphinTabWidget::tabDropEvent);
49 connect(tabBar, &DolphinTabBar::tabDetachRequested,
50 this, &DolphinTabWidget::detachTab);
51 tabBar->hide();
52
53 setTabBar(tabBar);
54 setDocumentMode(true);
55 setElideMode(Qt::ElideRight);
56 setUsesScrollButtons(true);
57 }
58
59 DolphinTabPage* DolphinTabWidget::currentTabPage() const
60 {
61 return tabPageAt(currentIndex());
62 }
63
64 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
65 {
66 return static_cast<DolphinTabPage*>(widget(index));
67 }
68
69 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
70 {
71 const int tabCount = count();
72 group.writeEntry("Tab Count", tabCount);
73 group.writeEntry("Active Tab Index", currentIndex());
74
75 for (int i = 0; i < tabCount; ++i) {
76 const DolphinTabPage* tabPage = tabPageAt(i);
77 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
78 }
79 }
80
81 void DolphinTabWidget::readProperties(const KConfigGroup& group)
82 {
83 const int tabCount = group.readEntry("Tab Count", 0);
84 for (int i = 0; i < tabCount; ++i) {
85 if (i >= count()) {
86 openNewActivatedTab();
87 }
88 if (group.hasKey("Tab Data " % QString::number(i))) {
89 // Tab state created with Dolphin > 4.14.x
90 const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
91 tabPageAt(i)->restoreState(state);
92 } else {
93 // Tab state created with Dolphin <= 4.14.x
94 const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray());
95 tabPageAt(i)->restoreStateV1(state);
96 }
97 }
98
99 const int index = group.readEntry("Active Tab Index", 0);
100 setCurrentIndex(index);
101 }
102
103 void DolphinTabWidget::refreshViews()
104 {
105 const int tabCount = count();
106 for (int i = 0; i < tabCount; ++i) {
107 tabPageAt(i)->refreshViews();
108 }
109 }
110
111 void DolphinTabWidget::openNewActivatedTab()
112 {
113 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
114 Q_ASSERT(oldActiveViewContainer);
115
116 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
117
118 openNewActivatedTab(oldActiveViewContainer->url());
119
120 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
121 Q_ASSERT(newActiveViewContainer);
122
123 // The URL navigator of the new tab should have the same editable state
124 // as the current tab
125 KUrlNavigator* navigator = newActiveViewContainer->urlNavigator();
126 navigator->setUrlEditable(isUrlEditable);
127
128 if (isUrlEditable) {
129 // If a new tab is opened and the URL is editable, assure that
130 // the user can edit the URL without manually setting the focus
131 navigator->setFocus();
132 }
133 }
134
135 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
136 {
137 openNewTab(primaryUrl, secondaryUrl);
138 setCurrentIndex(count() - 1);
139 }
140
141 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
142 {
143 QWidget* focusWidget = QApplication::focusWidget();
144
145 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
146 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
147 connect(tabPage, &DolphinTabPage::activeViewChanged,
148 this, &DolphinTabWidget::activeViewChanged);
149 connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
150 this, &DolphinTabWidget::tabUrlChanged);
151 addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(primaryUrl));
152
153 if (focusWidget) {
154 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
155 // in background, assure that the previous focused widget gets the focus back.
156 focusWidget->setFocus();
157 }
158 }
159
160 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
161 {
162 Q_ASSERT(dirs.size() > 0);
163
164 QList<QUrl>::const_iterator it = dirs.constBegin();
165 while (it != dirs.constEnd()) {
166 const QUrl& primaryUrl = *(it++);
167 if (splitView && (it != dirs.constEnd())) {
168 const QUrl& secondaryUrl = *(it++);
169 openNewTab(primaryUrl, secondaryUrl);
170 } else {
171 openNewTab(primaryUrl);
172 }
173 }
174 }
175
176 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
177 {
178 Q_ASSERT(files.size() > 0);
179
180 // Get all distinct directories from 'files' and open a tab
181 // for each directory. If the "split view" option is enabled, two
182 // directories are shown inside one tab (see openDirectories()).
183 QList<QUrl> dirs;
184 foreach (const QUrl& url, files) {
185 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
186 if (!dirs.contains(dir)) {
187 dirs.append(dir);
188 }
189 }
190
191 const int oldTabCount = count();
192 openDirectories(dirs, splitView);
193 const int tabCount = count();
194
195 // Select the files. Although the files can be split between several
196 // tabs, there is no need to split 'files' accordingly, as
197 // the DolphinView will just ignore invalid selections.
198 for (int i = oldTabCount; i < tabCount; ++i) {
199 DolphinTabPage* tabPage = tabPageAt(i);
200 tabPage->markUrlsAsSelected(files);
201 tabPage->markUrlAsCurrent(files.first());
202 }
203 }
204
205 void DolphinTabWidget::closeTab()
206 {
207 closeTab(currentIndex());
208 }
209
210 void DolphinTabWidget::closeTab(const int index)
211 {
212 Q_ASSERT(index >= 0);
213 Q_ASSERT(index < count());
214
215 if (count() < 2) {
216 // Never close the last tab.
217 return;
218 }
219
220 DolphinTabPage* tabPage = tabPageAt(index);
221 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
222
223 removeTab(index);
224 tabPage->deleteLater();
225 }
226
227 void DolphinTabWidget::activateNextTab()
228 {
229 const int index = currentIndex() + 1;
230 setCurrentIndex(index < count() ? index : 0);
231 }
232
233 void DolphinTabWidget::activatePrevTab()
234 {
235 const int index = currentIndex() - 1;
236 setCurrentIndex(index >= 0 ? index : (count() - 1));
237 }
238
239 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
240 {
241 // The places-selector from the URL navigator should only be shown
242 // if the places dock is invisible
243 m_placesSelectorVisible = !visible;
244
245 const int tabCount = count();
246 for (int i = 0; i < tabCount; ++i) {
247 DolphinTabPage* tabPage = tabPageAt(i);
248 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
249 }
250 }
251
252 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
253 {
254 openNewActivatedTab();
255 currentTabPage()->restoreState(state);
256 }
257
258 void DolphinTabWidget::detachTab(int index)
259 {
260 Q_ASSERT(index >= 0);
261
262 QStringList args;
263
264 const DolphinTabPage* tabPage = tabPageAt(index);
265 args << tabPage->primaryViewContainer()->url().url();
266 if (tabPage->splitViewEnabled()) {
267 args << tabPage->secondaryViewContainer()->url().url();
268 args << QStringLiteral("--split");
269 }
270
271 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
272 KRun::runCommand(command, this);
273
274 closeTab(index);
275 }
276
277 void DolphinTabWidget::openNewActivatedTab(int index)
278 {
279 Q_ASSERT(index >= 0);
280 const DolphinTabPage* tabPage = tabPageAt(index);
281 openNewActivatedTab(tabPage->activeViewContainer()->url());
282 }
283
284 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
285 {
286 if (index >= 0) {
287 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
288 view->dropUrls(view->url(), event, view);
289 }
290 }
291
292 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
293 {
294 const int index = indexOf(qobject_cast<QWidget*>(sender()));
295 if (index >= 0) {
296 tabBar()->setTabText(index, tabName(url));
297 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
298
299 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
300 if (index == currentIndex()) {
301 emit currentUrlChanged(url);
302 }
303 }
304 }
305
306 void DolphinTabWidget::currentTabChanged(int index)
307 {
308 // previous tab deactivation
309 if (DolphinTabPage* tabPage = tabPageAt(m_previousTab)) {
310 tabPage->setActive(false);
311 }
312 DolphinTabPage* tabPage = tabPageAt(index);
313 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
314 emit activeViewChanged(viewContainer);
315 emit currentUrlChanged(viewContainer->url());
316 tabPage->setActive(true);
317 m_previousTab = index;
318 }
319
320 void DolphinTabWidget::tabInserted(int index)
321 {
322 QTabWidget::tabInserted(index);
323
324 if (count() > 1) {
325 tabBar()->show();
326 }
327
328 emit tabCountChanged(count());
329 }
330
331 void DolphinTabWidget::tabRemoved(int index)
332 {
333 QTabWidget::tabRemoved(index);
334
335 // If only one tab is left, then remove the tab entry so that
336 // closing the last tab is not possible.
337 if (count() < 2) {
338 tabBar()->hide();
339 }
340
341 emit tabCountChanged(count());
342 }
343
344 QString DolphinTabWidget::tabName(const QUrl& url) const
345 {
346 QString name;
347 if (url == QUrl(QStringLiteral("file:///"))) {
348 name = '/';
349 } else {
350 name = url.adjusted(QUrl::StripTrailingSlash).fileName();
351 if (name.isEmpty()) {
352 name = url.scheme();
353 } else {
354 // Make sure that a '&' inside the directory name is displayed correctly
355 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
356 name.replace('&', QLatin1String("&&"));
357 }
358 }
359 return name;
360 }