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