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