]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Fix typo in comment
[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 #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::detachTab(int index)
324 {
325 Q_ASSERT(index >= 0);
326
327 QStringList args;
328
329 const DolphinTabPage* tabPage = tabPageAt(index);
330 args << tabPage->primaryViewContainer()->url().url();
331 if (tabPage->splitViewEnabled()) {
332 args << tabPage->secondaryViewContainer()->url().url();
333 args << QStringLiteral("--split");
334 }
335 args << QStringLiteral("--new-window");
336
337 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
338 KRun::runCommand(command, this);
339
340 closeTab(index);
341 }
342
343 void DolphinTabWidget::openNewActivatedTab(int index)
344 {
345 Q_ASSERT(index >= 0);
346 const DolphinTabPage* tabPage = tabPageAt(index);
347 openNewActivatedTab(tabPage->activeViewContainer()->url());
348 }
349
350 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
351 {
352 if (index >= 0) {
353 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
354 view->dropUrls(view->url(), event, view);
355 }
356 }
357
358 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
359 {
360 const int index = indexOf(qobject_cast<QWidget*>(sender()));
361 if (index >= 0) {
362 tabBar()->setTabText(index, tabName(tabPageAt(index)));
363 if (tabBar()->isVisible()) {
364 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
365 } else {
366 // Mark as dirty, actually load once the tab bar actually gets shown
367 tabBar()->setTabIcon(index, QIcon());
368 }
369
370 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
371 if (index == currentIndex()) {
372 emit currentUrlChanged(url);
373 }
374 }
375 }
376
377 void DolphinTabWidget::currentTabChanged(int index)
378 {
379 // last-viewed tab deactivation
380 if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) {
381 tabPage->setActive(false);
382 }
383 DolphinTabPage* tabPage = tabPageAt(index);
384 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
385 emit activeViewChanged(viewContainer);
386 emit currentUrlChanged(viewContainer->url());
387 tabPage->setActive(true);
388 m_lastViewedTab = index;
389 }
390
391 void DolphinTabWidget::tabInserted(int index)
392 {
393 QTabWidget::tabInserted(index);
394
395 if (count() > 1) {
396 // Resolve all pending tab icons
397 for (int i = 0; i < count(); ++i) {
398 if (tabBar()->tabIcon(i).isNull()) {
399 tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(tabPageAt(i)->activeViewContainer()->url())));
400 }
401 }
402
403 tabBar()->show();
404 }
405
406 emit tabCountChanged(count());
407 }
408
409 void DolphinTabWidget::tabRemoved(int index)
410 {
411 QTabWidget::tabRemoved(index);
412
413 // If only one tab is left, then remove the tab entry so that
414 // closing the last tab is not possible.
415 if (count() < 2) {
416 tabBar()->hide();
417 }
418
419 emit tabCountChanged(count());
420 }
421
422 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
423 {
424 if (!tabPage) {
425 return QString();
426 }
427 QString name = tabPage->activeViewContainer()->caption();
428 // Make sure that a '&' inside the directory name is displayed correctly
429 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
430 return name.replace('&', QLatin1String("&&"));
431 }
432
433 QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url) const
434 {
435 for (int i = 0; i < count(); i++) {
436 const auto tabPage = tabPageAt(i);
437 if (url == tabPage->primaryViewContainer()->url()) {
438 return qMakePair(i, true);
439 }
440
441 if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
442 return qMakePair(i, false);
443 }
444 }
445 return qMakePair(-1, false);
446 }