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