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