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