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