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