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