]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Open new tab placement 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 "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 if (GeneralSettings::openNewTabAfterLastTab()) {
156 setCurrentIndex(count() - 1);
157 } else {
158 setCurrentIndex(currentIndex() + 1);
159 }
160 }
161
162 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
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 (!GeneralSettings::openNewTabAfterLastTab()) {
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 if (tabPage->splitViewEnabled() && !m_navigatorsWidget->secondaryUrlNavigator()) {
405 m_navigatorsWidget->createSecondaryUrlNavigator();
406 }
407 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
408 Q_EMIT activeViewChanged(viewContainer);
409 Q_EMIT currentUrlChanged(viewContainer->url());
410 tabPage->setActive(true);
411 tabPage->connectNavigators(m_navigatorsWidget);
412 m_navigatorsWidget->setSecondaryNavigatorVisible(tabPage->splitViewEnabled());
413 m_lastViewedTab = tabPage;
414 }
415
416 void DolphinTabWidget::tabInserted(int index)
417 {
418 QTabWidget::tabInserted(index);
419
420 if (count() > 1) {
421 // Resolve all pending tab icons
422 for (int i = 0; i < count(); ++i) {
423 const QUrl url = tabPageAt(i)->activeViewContainer()->url();
424 if (tabBar()->tabIcon(i).isNull()) {
425 tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(url)));
426 }
427 if (tabBar()->tabToolTip(i).isEmpty()) {
428 tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile));
429 }
430 }
431
432 tabBar()->show();
433 }
434
435 Q_EMIT tabCountChanged(count());
436 }
437
438 void DolphinTabWidget::tabRemoved(int index)
439 {
440 QTabWidget::tabRemoved(index);
441
442 // If only one tab is left, then remove the tab entry so that
443 // closing the last tab is not possible.
444 if (count() < 2) {
445 tabBar()->hide();
446 }
447
448 Q_EMIT tabCountChanged(count());
449 }
450
451 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
452 {
453 if (!tabPage) {
454 return QString();
455 }
456 QString name = tabPage->activeViewContainer()->caption();
457 // Make sure that a '&' inside the directory name is displayed correctly
458 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
459 return name.replace('&', QLatin1String("&&"));
460 }
461
462 QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url) const
463 {
464 for (int i = 0; i < count(); i++) {
465 const auto tabPage = tabPageAt(i);
466 if (url == tabPage->primaryViewContainer()->url()) {
467 return qMakePair(i, true);
468 }
469
470 if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
471 return qMakePair(i, false);
472 }
473 }
474 return qMakePair(-1, false);
475 }