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