]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Fixing bugs in new folders in tabs feature
[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 void DolphinTabWidget::openNewActivatedTab()
134 {
135 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
136 Q_ASSERT(oldActiveViewContainer);
137
138 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
139
140 openNewActivatedTab(oldActiveViewContainer->url());
141
142 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
143 Q_ASSERT(newActiveViewContainer);
144
145 // The URL navigator of the new tab should have the same editable state
146 // as the current tab
147 newActiveViewContainer->urlNavigator()->setUrlEditable(isUrlEditable);
148
149 // Always focus the new tab's view
150 newActiveViewContainer->view()->setFocus();
151 }
152
153 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
154 {
155 openNewTab(primaryUrl, secondaryUrl);
156 setCurrentIndex(count() - 1);
157 }
158
159 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement)
160 {
161 QWidget* focusWidget = QApplication::focusWidget();
162
163 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
164 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
165 connect(tabPage, &DolphinTabPage::activeViewChanged,
166 this, &DolphinTabWidget::activeViewChanged);
167 connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
168 this, &DolphinTabWidget::tabUrlChanged);
169 int newTabIndex = -1;
170 if (tabPlacement == AfterCurrentTab) {
171 newTabIndex = currentIndex() + 1;
172 }
173 insertTab(newTabIndex, tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(tabPage));
174
175 if (focusWidget) {
176 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
177 // in background, assure that the previous focused widget gets the focus back.
178 focusWidget->setFocus();
179 }
180 }
181
182 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
183 {
184 Q_ASSERT(dirs.size() > 0);
185
186 QList<QUrl>::const_iterator it = dirs.constBegin();
187 while (it != dirs.constEnd()) {
188 const QUrl& primaryUrl = *(it++);
189 const QPair<int, bool> viewLocation = getIndexByUrl(primaryUrl);
190 if (viewLocation.first >= 0) {
191 setCurrentIndex(viewLocation.first);
192 const auto tabPage = tabPageAt(viewLocation.first);
193 if (viewLocation.second) {
194 tabPage->primaryViewContainer()->setActive(true);
195 } else {
196 tabPage->secondaryViewContainer()->setActive(true);
197 }
198 continue;
199 }
200 if (splitView && (it != dirs.constEnd())) {
201 const QUrl& secondaryUrl = *(it++);
202 openNewActivatedTab(primaryUrl, secondaryUrl);
203 } else {
204 openNewActivatedTab(primaryUrl);
205 }
206 }
207 }
208
209 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
210 {
211 Q_ASSERT(files.size() > 0);
212
213 // Get all distinct directories from 'files' and open a tab
214 // for each directory. If the "split view" option is enabled, two
215 // directories are shown inside one tab (see openDirectories()).
216 QList<QUrl> dirs;
217 foreach (const QUrl& url, files) {
218 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
219 if (!dirs.contains(dir)) {
220 dirs.append(dir);
221 }
222 }
223
224 const int oldTabCount = count();
225 openDirectories(dirs, splitView);
226 const int tabCount = count();
227
228 // Select the files. Although the files can be split between several
229 // tabs, there is no need to split 'files' accordingly, as
230 // the DolphinView will just ignore invalid selections.
231 for (int i = oldTabCount; i < tabCount; ++i) {
232 DolphinTabPage* tabPage = tabPageAt(i);
233 tabPage->markUrlsAsSelected(files);
234 tabPage->markUrlAsCurrent(files.first());
235 }
236 }
237
238 void DolphinTabWidget::closeTab()
239 {
240 closeTab(currentIndex());
241 }
242
243 void DolphinTabWidget::closeTab(const int index)
244 {
245 Q_ASSERT(index >= 0);
246 Q_ASSERT(index < count());
247
248 if (count() < 2) {
249 // Close Dolphin when closing the last tab.
250 parentWidget()->close();
251 return;
252 }
253
254 DolphinTabPage* tabPage = tabPageAt(index);
255 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
256
257 removeTab(index);
258 tabPage->deleteLater();
259 }
260
261 void DolphinTabWidget::activateNextTab()
262 {
263 const int index = currentIndex() + 1;
264 setCurrentIndex(index < count() ? index : 0);
265 }
266
267 void DolphinTabWidget::activatePrevTab()
268 {
269 const int index = currentIndex() - 1;
270 setCurrentIndex(index >= 0 ? index : (count() - 1));
271 }
272
273 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
274 {
275 // The places-selector from the URL navigator should only be shown
276 // if the places dock is invisible
277 m_placesSelectorVisible = !visible;
278
279 const int tabCount = count();
280 for (int i = 0; i < tabCount; ++i) {
281 DolphinTabPage* tabPage = tabPageAt(i);
282 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
283 }
284 }
285
286 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
287 {
288 openNewActivatedTab();
289 currentTabPage()->restoreState(state);
290 }
291
292 void DolphinTabWidget::detachTab(int index)
293 {
294 Q_ASSERT(index >= 0);
295
296 QStringList args;
297
298 const DolphinTabPage* tabPage = tabPageAt(index);
299 args << tabPage->primaryViewContainer()->url().url();
300 if (tabPage->splitViewEnabled()) {
301 args << tabPage->secondaryViewContainer()->url().url();
302 args << QStringLiteral("--split");
303 }
304 args << QStringLiteral("--new-window");
305
306 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
307 KRun::runCommand(command, this);
308
309 closeTab(index);
310 }
311
312 void DolphinTabWidget::openNewActivatedTab(int index)
313 {
314 Q_ASSERT(index >= 0);
315 const DolphinTabPage* tabPage = tabPageAt(index);
316 openNewActivatedTab(tabPage->activeViewContainer()->url());
317 }
318
319 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
320 {
321 if (index >= 0) {
322 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
323 view->dropUrls(view->url(), event, view);
324 }
325 }
326
327 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
328 {
329 const int index = indexOf(qobject_cast<QWidget*>(sender()));
330 if (index >= 0) {
331 tabBar()->setTabText(index, tabName(tabPageAt(index)));
332 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
333
334 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
335 if (index == currentIndex()) {
336 emit currentUrlChanged(url);
337 }
338 }
339 }
340
341 void DolphinTabWidget::currentTabChanged(int index)
342 {
343 // last-viewed tab deactivation
344 if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) {
345 tabPage->setActive(false);
346 }
347 DolphinTabPage* tabPage = tabPageAt(index);
348 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
349 emit activeViewChanged(viewContainer);
350 emit currentUrlChanged(viewContainer->url());
351 tabPage->setActive(true);
352 m_lastViewedTab = index;
353 }
354
355 void DolphinTabWidget::tabInserted(int index)
356 {
357 QTabWidget::tabInserted(index);
358
359 if (count() > 1) {
360 tabBar()->show();
361 }
362
363 emit tabCountChanged(count());
364 }
365
366 void DolphinTabWidget::tabRemoved(int index)
367 {
368 QTabWidget::tabRemoved(index);
369
370 // If only one tab is left, then remove the tab entry so that
371 // closing the last tab is not possible.
372 if (count() < 2) {
373 tabBar()->hide();
374 }
375
376 emit tabCountChanged(count());
377 }
378
379 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
380 {
381 if (!tabPage) {
382 return QString();
383 }
384 QString name = tabPage->activeViewContainer()->caption();
385 // Make sure that a '&' inside the directory name is displayed correctly
386 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
387 return name.replace('&', QLatin1String("&&"));
388 }
389
390 QPair<int, bool> DolphinTabWidget::getIndexByUrl(const QUrl& url) const
391 {
392 for (int i = 0; i < count(); i++) {
393 const auto tabPage = tabPageAt(i);
394 if (url == tabPage->primaryViewContainer()->url()) {
395 return qMakePair(i, true);
396 }
397
398 if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
399 return qMakePair(i, false);
400 }
401 }
402 return qMakePair(-1, false);
403 }