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