]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Fixing bug where split view opens with no URLs
[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 int index = getIndexByUrl(primaryUrl);
190 if (index >= 0) {
191 setCurrentIndex(index);
192 continue;
193 }
194 if (splitView && (it != dirs.constEnd())) {
195 const QUrl& secondaryUrl = *(it++);
196 openNewActivatedTab(primaryUrl, secondaryUrl);
197 } else {
198 openNewActivatedTab(primaryUrl);
199 }
200 }
201 }
202
203 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
204 {
205 Q_ASSERT(files.size() > 0);
206
207 // Get all distinct directories from 'files' and open a tab
208 // for each directory. If the "split view" option is enabled, two
209 // directories are shown inside one tab (see openDirectories()).
210 QList<QUrl> dirs;
211 foreach (const QUrl& url, files) {
212 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
213 if (!dirs.contains(dir)) {
214 dirs.append(dir);
215 }
216 }
217
218 const int oldTabCount = count();
219 openDirectories(dirs, splitView);
220 const int tabCount = count();
221
222 // Select the files. Although the files can be split between several
223 // tabs, there is no need to split 'files' accordingly, as
224 // the DolphinView will just ignore invalid selections.
225 for (int i = oldTabCount; i < tabCount; ++i) {
226 DolphinTabPage* tabPage = tabPageAt(i);
227 tabPage->markUrlsAsSelected(files);
228 tabPage->markUrlAsCurrent(files.first());
229 }
230 }
231
232 void DolphinTabWidget::closeTab()
233 {
234 closeTab(currentIndex());
235 }
236
237 void DolphinTabWidget::closeTab(const int index)
238 {
239 Q_ASSERT(index >= 0);
240 Q_ASSERT(index < count());
241
242 if (count() < 2) {
243 // Close Dolphin when closing the last tab.
244 parentWidget()->close();
245 return;
246 }
247
248 DolphinTabPage* tabPage = tabPageAt(index);
249 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
250
251 removeTab(index);
252 tabPage->deleteLater();
253 }
254
255 void DolphinTabWidget::activateNextTab()
256 {
257 const int index = currentIndex() + 1;
258 setCurrentIndex(index < count() ? index : 0);
259 }
260
261 void DolphinTabWidget::activatePrevTab()
262 {
263 const int index = currentIndex() - 1;
264 setCurrentIndex(index >= 0 ? index : (count() - 1));
265 }
266
267 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
268 {
269 // The places-selector from the URL navigator should only be shown
270 // if the places dock is invisible
271 m_placesSelectorVisible = !visible;
272
273 const int tabCount = count();
274 for (int i = 0; i < tabCount; ++i) {
275 DolphinTabPage* tabPage = tabPageAt(i);
276 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
277 }
278 }
279
280 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
281 {
282 openNewActivatedTab();
283 currentTabPage()->restoreState(state);
284 }
285
286 void DolphinTabWidget::detachTab(int index)
287 {
288 Q_ASSERT(index >= 0);
289
290 QStringList args;
291
292 const DolphinTabPage* tabPage = tabPageAt(index);
293 args << tabPage->primaryViewContainer()->url().url();
294 if (tabPage->splitViewEnabled()) {
295 args << tabPage->secondaryViewContainer()->url().url();
296 args << QStringLiteral("--split");
297 }
298 args << QStringLiteral("--new-window");
299
300 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
301 KRun::runCommand(command, this);
302
303 closeTab(index);
304 }
305
306 void DolphinTabWidget::openNewActivatedTab(int index)
307 {
308 Q_ASSERT(index >= 0);
309 const DolphinTabPage* tabPage = tabPageAt(index);
310 openNewActivatedTab(tabPage->activeViewContainer()->url());
311 }
312
313 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
314 {
315 if (index >= 0) {
316 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
317 view->dropUrls(view->url(), event, view);
318 }
319 }
320
321 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
322 {
323 const int index = indexOf(qobject_cast<QWidget*>(sender()));
324 if (index >= 0) {
325 tabBar()->setTabText(index, tabName(tabPageAt(index)));
326 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
327
328 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
329 if (index == currentIndex()) {
330 emit currentUrlChanged(url);
331 }
332 }
333 }
334
335 void DolphinTabWidget::currentTabChanged(int index)
336 {
337 // last-viewed tab deactivation
338 if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) {
339 tabPage->setActive(false);
340 }
341 DolphinTabPage* tabPage = tabPageAt(index);
342 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
343 emit activeViewChanged(viewContainer);
344 emit currentUrlChanged(viewContainer->url());
345 tabPage->setActive(true);
346 m_lastViewedTab = index;
347 }
348
349 void DolphinTabWidget::tabInserted(int index)
350 {
351 QTabWidget::tabInserted(index);
352
353 if (count() > 1) {
354 tabBar()->show();
355 }
356
357 emit tabCountChanged(count());
358 }
359
360 void DolphinTabWidget::tabRemoved(int index)
361 {
362 QTabWidget::tabRemoved(index);
363
364 // If only one tab is left, then remove the tab entry so that
365 // closing the last tab is not possible.
366 if (count() < 2) {
367 tabBar()->hide();
368 }
369
370 emit tabCountChanged(count());
371 }
372
373 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
374 {
375 if (!tabPage) {
376 return QString();
377 }
378 QString name = tabPage->activeViewContainer()->caption();
379 // Make sure that a '&' inside the directory name is displayed correctly
380 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
381 return name.replace('&', QLatin1String("&&"));
382 }
383
384 int DolphinTabWidget::getIndexByUrl(const QUrl& url) const
385 {
386 for (int i = 0; i < count(); i++) {
387 // Conversion to display string is necessary to deal with the '~' alias.
388 // i.e. to acknowledge that ~/ is equivalent to /home/user/
389 const QUrl tabUrl = tabPageAt(i)->activeViewContainer()->url();
390 if (url == tabUrl ||
391 url.toDisplayString(QUrl::StripTrailingSlash) == tabUrl.toDisplayString(QUrl::StripTrailingSlash)) {
392 return i;
393 }
394 }
395 return -1;
396 }