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