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