]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Disable keyboard accelerators for the tabs widget
[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 "dolphin_generalsettings.h"
23 #include "dolphintabbar.h"
24 #include "dolphintabpage.h"
25 #include "dolphinviewcontainer.h"
26
27 #include <KConfigGroup>
28 #include <KRun>
29 #include <KShell>
30 #include <kio/global.h>
31 #include <KAcceleratorManager>
32
33 #include <QApplication>
34 #include <QDropEvent>
35
36 DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
37 QTabWidget(parent),
38 m_placesSelectorVisible(true),
39 m_lastViewedTab(0)
40 {
41 KAcceleratorManager::setNoAccel(this);
42
43 connect(this, &DolphinTabWidget::tabCloseRequested,
44 this, QOverload<int>::of(&DolphinTabWidget::closeTab));
45 connect(this, &DolphinTabWidget::currentChanged,
46 this, &DolphinTabWidget::currentTabChanged);
47
48 DolphinTabBar* tabBar = new DolphinTabBar(this);
49 connect(tabBar, &DolphinTabBar::openNewActivatedTab,
50 this, QOverload<int>::of(&DolphinTabWidget::openNewActivatedTab));
51 connect(tabBar, &DolphinTabBar::tabDropEvent,
52 this, &DolphinTabWidget::tabDropEvent);
53 connect(tabBar, &DolphinTabBar::tabDetachRequested,
54 this, &DolphinTabWidget::detachTab);
55 tabBar->hide();
56
57 setTabBar(tabBar);
58 setDocumentMode(true);
59 setElideMode(Qt::ElideRight);
60 setUsesScrollButtons(true);
61 }
62
63 DolphinTabPage* DolphinTabWidget::currentTabPage() const
64 {
65 return tabPageAt(currentIndex());
66 }
67
68 DolphinTabPage* DolphinTabWidget::nextTabPage() const
69 {
70 const int index = currentIndex() + 1;
71 return tabPageAt(index < count() ? index : 0);
72 }
73
74 DolphinTabPage* DolphinTabWidget::prevTabPage() const
75 {
76 const int index = currentIndex() - 1;
77 return tabPageAt(index >= 0 ? index : (count() - 1));
78 }
79
80 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
81 {
82 return static_cast<DolphinTabPage*>(widget(index));
83 }
84
85 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
86 {
87 const int tabCount = count();
88 group.writeEntry("Tab Count", tabCount);
89 group.writeEntry("Active Tab Index", currentIndex());
90
91 for (int i = 0; i < tabCount; ++i) {
92 const DolphinTabPage* tabPage = tabPageAt(i);
93 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
94 }
95 }
96
97 void DolphinTabWidget::readProperties(const KConfigGroup& group)
98 {
99 const int tabCount = group.readEntry("Tab Count", 0);
100 for (int i = 0; i < tabCount; ++i) {
101 if (i >= count()) {
102 openNewActivatedTab();
103 }
104 if (group.hasKey("Tab Data " % QString::number(i))) {
105 // Tab state created with Dolphin > 4.14.x
106 const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
107 tabPageAt(i)->restoreState(state);
108 } else {
109 // Tab state created with Dolphin <= 4.14.x
110 const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray());
111 tabPageAt(i)->restoreStateV1(state);
112 }
113 }
114
115 const int index = group.readEntry("Active Tab Index", 0);
116 setCurrentIndex(index);
117 }
118
119 void DolphinTabWidget::refreshViews()
120 {
121 // Left-elision is better when showing full paths, since you care most
122 // about the current directory which is on the right
123 if (GeneralSettings::showFullPathInTitlebar()) {
124 setElideMode(Qt::ElideLeft);
125 } else {
126 setElideMode(Qt::ElideRight);
127 }
128
129 const int tabCount = count();
130 for (int i = 0; i < tabCount; ++i) {
131 tabBar()->setTabText(i, tabName(tabPageAt(i)));
132 tabPageAt(i)->refreshViews();
133 }
134 }
135
136 bool DolphinTabWidget::isUrlOpen(const QUrl &url) const
137 {
138 return indexByUrl(url).first >= 0;
139 }
140
141 void DolphinTabWidget::openNewActivatedTab()
142 {
143 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
144 Q_ASSERT(oldActiveViewContainer);
145
146 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
147
148 openNewActivatedTab(oldActiveViewContainer->url());
149
150 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
151 Q_ASSERT(newActiveViewContainer);
152
153 // The URL navigator of the new tab should have the same editable state
154 // as the current tab
155 newActiveViewContainer->urlNavigator()->setUrlEditable(isUrlEditable);
156
157 // Always focus the new tab's view
158 newActiveViewContainer->view()->setFocus();
159 }
160
161 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
162 {
163 openNewTab(primaryUrl, secondaryUrl);
164 setCurrentIndex(count() - 1);
165 }
166
167 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl, TabPlacement tabPlacement)
168 {
169 QWidget* focusWidget = QApplication::focusWidget();
170
171 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
172 tabPage->setActive(false);
173 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
174 connect(tabPage, &DolphinTabPage::activeViewChanged,
175 this, &DolphinTabWidget::activeViewChanged);
176 connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
177 this, &DolphinTabWidget::tabUrlChanged);
178 int newTabIndex = -1;
179 if (tabPlacement == AfterCurrentTab) {
180 newTabIndex = currentIndex() + 1;
181 }
182 insertTab(newTabIndex, tabPage, QIcon() /* loaded in tabInserted */, tabName(tabPage));
183
184 if (focusWidget) {
185 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
186 // in background, assure that the previous focused widget gets the focus back.
187 focusWidget->setFocus();
188 }
189 }
190
191 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
192 {
193 Q_ASSERT(dirs.size() > 0);
194
195 QList<QUrl>::const_iterator it = dirs.constBegin();
196 while (it != dirs.constEnd()) {
197 const QUrl& primaryUrl = *(it++);
198 const QPair<int, bool> indexInfo = indexByUrl(primaryUrl);
199 const int index = indexInfo.first;
200 const bool isInPrimaryView = indexInfo.second;
201 if (index >= 0) {
202 setCurrentIndex(index);
203 const auto tabPage = tabPageAt(index);
204 if (isInPrimaryView) {
205 tabPage->primaryViewContainer()->setActive(true);
206 } else {
207 tabPage->secondaryViewContainer()->setActive(true);
208 }
209 continue;
210 }
211 if (splitView && (it != dirs.constEnd())) {
212 const QUrl& secondaryUrl = *(it++);
213 openNewActivatedTab(primaryUrl, secondaryUrl);
214 } else {
215 openNewActivatedTab(primaryUrl);
216 }
217 }
218 }
219
220 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
221 {
222 Q_ASSERT(files.size() > 0);
223
224 // Get all distinct directories from 'files' and open a tab
225 // for each directory. If the "split view" option is enabled, two
226 // directories are shown inside one tab (see openDirectories()).
227 QList<QUrl> dirs;
228 foreach (const QUrl& url, files) {
229 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
230 if (!dirs.contains(dir)) {
231 dirs.append(dir);
232 }
233 }
234
235 const int oldTabCount = count();
236 openDirectories(dirs, splitView);
237 const int tabCount = count();
238
239 // Select the files. Although the files can be split between several
240 // tabs, there is no need to split 'files' accordingly, as
241 // the DolphinView will just ignore invalid selections.
242 for (int i = oldTabCount; i < tabCount; ++i) {
243 DolphinTabPage* tabPage = tabPageAt(i);
244 tabPage->markUrlsAsSelected(files);
245 tabPage->markUrlAsCurrent(files.first());
246 }
247 }
248
249 void DolphinTabWidget::closeTab()
250 {
251 closeTab(currentIndex());
252 }
253
254 void DolphinTabWidget::closeTab(const int index)
255 {
256 Q_ASSERT(index >= 0);
257 Q_ASSERT(index < count());
258
259 if (count() < 2) {
260 // Close Dolphin when closing the last tab.
261 parentWidget()->close();
262 return;
263 }
264
265 DolphinTabPage* tabPage = tabPageAt(index);
266 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
267
268 removeTab(index);
269 tabPage->deleteLater();
270 }
271
272 void DolphinTabWidget::activateTab(const int index)
273 {
274 if (index < count()) {
275 setCurrentIndex(index);
276 }
277 }
278
279 void DolphinTabWidget::activateLastTab()
280 {
281 setCurrentIndex(count() - 1);
282 }
283
284 void DolphinTabWidget::activateNextTab()
285 {
286 const int index = currentIndex() + 1;
287 setCurrentIndex(index < count() ? index : 0);
288 }
289
290 void DolphinTabWidget::activatePrevTab()
291 {
292 const int index = currentIndex() - 1;
293 setCurrentIndex(index >= 0 ? index : (count() - 1));
294 }
295
296 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
297 {
298 // The places-selector from the URL navigator should only be shown
299 // if the places dock is invisible
300 m_placesSelectorVisible = !visible;
301
302 const int tabCount = count();
303 for (int i = 0; i < tabCount; ++i) {
304 DolphinTabPage* tabPage = tabPageAt(i);
305 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
306 }
307 }
308
309 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
310 {
311 openNewActivatedTab();
312 currentTabPage()->restoreState(state);
313 }
314
315 void DolphinTabWidget::detachTab(int index)
316 {
317 Q_ASSERT(index >= 0);
318
319 QStringList args;
320
321 const DolphinTabPage* tabPage = tabPageAt(index);
322 args << tabPage->primaryViewContainer()->url().url();
323 if (tabPage->splitViewEnabled()) {
324 args << tabPage->secondaryViewContainer()->url().url();
325 args << QStringLiteral("--split");
326 }
327 args << QStringLiteral("--new-window");
328
329 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
330 KRun::runCommand(command, this);
331
332 closeTab(index);
333 }
334
335 void DolphinTabWidget::openNewActivatedTab(int index)
336 {
337 Q_ASSERT(index >= 0);
338 const DolphinTabPage* tabPage = tabPageAt(index);
339 openNewActivatedTab(tabPage->activeViewContainer()->url());
340 }
341
342 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
343 {
344 if (index >= 0) {
345 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
346 view->dropUrls(view->url(), event, view);
347 }
348 }
349
350 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
351 {
352 const int index = indexOf(qobject_cast<QWidget*>(sender()));
353 if (index >= 0) {
354 tabBar()->setTabText(index, tabName(tabPageAt(index)));
355 if (tabBar()->isVisible()) {
356 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
357 } else {
358 // Mark as dirty, actually load once the tab bar actually gets shown
359 tabBar()->setTabIcon(index, QIcon());
360 }
361
362 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
363 if (index == currentIndex()) {
364 emit currentUrlChanged(url);
365 }
366 }
367 }
368
369 void DolphinTabWidget::currentTabChanged(int index)
370 {
371 // last-viewed tab deactivation
372 if (DolphinTabPage* tabPage = tabPageAt(m_lastViewedTab)) {
373 tabPage->setActive(false);
374 }
375 DolphinTabPage* tabPage = tabPageAt(index);
376 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
377 emit activeViewChanged(viewContainer);
378 emit currentUrlChanged(viewContainer->url());
379 tabPage->setActive(true);
380 m_lastViewedTab = index;
381 }
382
383 void DolphinTabWidget::tabInserted(int index)
384 {
385 QTabWidget::tabInserted(index);
386
387 if (count() > 1) {
388 // Resolve all pending tab icons
389 for (int i = 0; i < count(); ++i) {
390 if (tabBar()->tabIcon(i).isNull()) {
391 tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(tabPageAt(i)->activeViewContainer()->url())));
392 }
393 }
394
395 tabBar()->show();
396 }
397
398 emit tabCountChanged(count());
399 }
400
401 void DolphinTabWidget::tabRemoved(int index)
402 {
403 QTabWidget::tabRemoved(index);
404
405 // If only one tab is left, then remove the tab entry so that
406 // closing the last tab is not possible.
407 if (count() < 2) {
408 tabBar()->hide();
409 }
410
411 emit tabCountChanged(count());
412 }
413
414 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
415 {
416 if (!tabPage) {
417 return QString();
418 }
419 QString name = tabPage->activeViewContainer()->caption();
420 // Make sure that a '&' inside the directory name is displayed correctly
421 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
422 return name.replace('&', QLatin1String("&&"));
423 }
424
425 QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url) const
426 {
427 for (int i = 0; i < count(); i++) {
428 const auto tabPage = tabPageAt(i);
429 if (url == tabPage->primaryViewContainer()->url()) {
430 return qMakePair(i, true);
431 }
432
433 if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
434 return qMakePair(i, false);
435 }
436 }
437 return qMakePair(-1, false);
438 }