]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabpage.cpp
Merge remote-tracking branch 'origin/master' into frameworks
[dolphin.git] / src / dolphintabpage.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 "dolphintabpage.h"
21
22 #include "dolphinviewcontainer.h"
23 #include "dolphin_generalsettings.h"
24
25 #include <QSplitter>
26
27 DolphinTabPage::DolphinTabPage(const KUrl& primaryUrl, const KUrl& secondaryUrl, QWidget* parent) :
28 QWidget(parent),
29 m_primaryViewActive(true),
30 m_splitViewEnabled(false)
31 {
32 QVBoxLayout* layout = new QVBoxLayout(this);
33 layout->setSpacing(0);
34 layout->setMargin(0);
35
36 m_splitter = new QSplitter(Qt::Horizontal, this);
37 m_splitter->setChildrenCollapsible(false);
38 layout->addWidget(m_splitter);
39
40 // Create a new primary view
41 m_primaryViewContainer = createViewContainer(primaryUrl);
42 connect(m_primaryViewContainer->view(), SIGNAL(urlChanged(KUrl)),
43 this, SIGNAL(activeViewUrlChanged(KUrl)));
44 connect(m_primaryViewContainer->view(), SIGNAL(redirection(KUrl,KUrl)),
45 this, SLOT(slotViewUrlRedirection(KUrl,KUrl)));
46
47 m_splitter->addWidget(m_primaryViewContainer);
48 m_primaryViewContainer->show();
49
50 if (secondaryUrl.isValid() || GeneralSettings::splitView()) {
51 // Provide a secondary view, if the given secondary url is valid or if the
52 // startup settings are set this way (use the url of the primary view).
53 m_splitViewEnabled = true;
54 const KUrl& url = secondaryUrl.isValid() ? secondaryUrl : primaryUrl;
55 m_secondaryViewContainer = createViewContainer(url);
56 m_splitter->addWidget(m_secondaryViewContainer);
57 m_secondaryViewContainer->show();
58 }
59
60 m_primaryViewContainer->setActive(true);
61 }
62
63 bool DolphinTabPage::primaryViewActive() const
64 {
65 return m_primaryViewActive;
66 }
67
68 bool DolphinTabPage::splitViewEnabled() const
69 {
70 return m_splitViewEnabled;
71 }
72
73 void DolphinTabPage::setSplitViewEnabled(bool enabled)
74 {
75 if (m_splitViewEnabled != enabled) {
76 m_splitViewEnabled = enabled;
77
78 if (enabled) {
79 const KUrl& url = m_primaryViewContainer->url();
80 m_secondaryViewContainer = createViewContainer(url);
81
82 const bool placesSelectorVisible = m_primaryViewContainer->urlNavigator()->isPlacesSelectorVisible();
83 m_secondaryViewContainer->urlNavigator()->setPlacesSelectorVisible(placesSelectorVisible);
84
85 m_splitter->addWidget(m_secondaryViewContainer);
86 m_secondaryViewContainer->show();
87 m_secondaryViewContainer->setActive(true);
88 } else {
89 // Close the view which is active.
90 DolphinViewContainer* view = activeViewContainer();
91 if (m_primaryViewActive) {
92 // If the primary view is active, we have to swap the pointers
93 // because the secondary view will be the new primary view.
94 qSwap(m_primaryViewContainer, m_secondaryViewContainer);
95 }
96 m_primaryViewContainer->setActive(true);
97 view->close();
98 view->deleteLater();
99 }
100 }
101 }
102
103 DolphinViewContainer* DolphinTabPage::primaryViewContainer() const
104 {
105 return m_primaryViewContainer;
106 }
107
108 DolphinViewContainer* DolphinTabPage::secondaryViewContainer() const
109 {
110 return m_secondaryViewContainer;
111 }
112
113 DolphinViewContainer* DolphinTabPage::activeViewContainer() const
114 {
115 return m_primaryViewActive ? m_primaryViewContainer :
116 m_secondaryViewContainer;
117 }
118
119 KFileItemList DolphinTabPage::selectedItems() const
120 {
121 KFileItemList items = m_primaryViewContainer->view()->selectedItems();
122 if (m_splitViewEnabled) {
123 items += m_secondaryViewContainer->view()->selectedItems();
124 }
125 return items;
126 }
127
128 int DolphinTabPage::selectedItemsCount() const
129 {
130 int selectedItemsCount = m_primaryViewContainer->view()->selectedItemsCount();
131 if (m_splitViewEnabled) {
132 selectedItemsCount += m_secondaryViewContainer->view()->selectedItemsCount();
133 }
134 return selectedItemsCount;
135 }
136
137 void DolphinTabPage::markUrlsAsSelected(const QList<KUrl>& urls)
138 {
139 m_primaryViewContainer->view()->markUrlsAsSelected(urls);
140 if (m_splitViewEnabled) {
141 m_secondaryViewContainer->view()->markUrlsAsSelected(urls);
142 }
143 }
144
145 void DolphinTabPage::markUrlAsCurrent(const KUrl& url)
146 {
147 m_primaryViewContainer->view()->markUrlAsCurrent(url);
148 if (m_splitViewEnabled) {
149 m_secondaryViewContainer->view()->markUrlAsCurrent(url);
150 }
151 }
152
153 void DolphinTabPage::setPlacesSelectorVisible(bool visible)
154 {
155 m_primaryViewContainer->urlNavigator()->setPlacesSelectorVisible(visible);
156 if (m_splitViewEnabled) {
157 m_secondaryViewContainer->urlNavigator()->setPlacesSelectorVisible(visible);
158 }
159 }
160
161 void DolphinTabPage::refreshViews()
162 {
163 m_primaryViewContainer->readSettings();
164 if (m_splitViewEnabled) {
165 m_secondaryViewContainer->readSettings();
166 }
167 }
168
169 QByteArray DolphinTabPage::saveState() const
170 {
171 QByteArray state;
172 QDataStream stream(&state, QIODevice::WriteOnly);
173
174 stream << quint32(2); // Tab state version
175
176 stream << m_splitViewEnabled;
177
178 stream << m_primaryViewContainer->url();
179 stream << m_primaryViewContainer->urlNavigator()->isUrlEditable();
180 m_primaryViewContainer->view()->saveState(stream);
181
182 if (m_splitViewEnabled) {
183 stream << m_secondaryViewContainer->url();
184 stream << m_secondaryViewContainer->urlNavigator()->isUrlEditable();
185 m_secondaryViewContainer->view()->saveState(stream);
186 }
187
188 stream << m_primaryViewActive;
189 stream << m_splitter->saveState();
190
191 return state;
192 }
193
194 void DolphinTabPage::restoreState(const QByteArray& state)
195 {
196 if (state.isEmpty()) {
197 return;
198 }
199
200 QByteArray sd = state;
201 QDataStream stream(&sd, QIODevice::ReadOnly);
202
203 // Read the version number of the tab state and check if the version is supported.
204 quint32 version = 0;
205 stream >> version;
206 if (version != 2) {
207 // The version of the tab state isn't supported, we can't restore it.
208 return;
209 }
210
211 bool isSplitViewEnabled = false;
212 stream >> isSplitViewEnabled;
213 setSplitViewEnabled(isSplitViewEnabled);
214
215 KUrl primaryUrl;
216 stream >> primaryUrl;
217 m_primaryViewContainer->setUrl(primaryUrl);
218 bool primaryUrlEditable;
219 stream >> primaryUrlEditable;
220 m_primaryViewContainer->urlNavigator()->setUrlEditable(primaryUrlEditable);
221 m_primaryViewContainer->view()->restoreState(stream);
222
223 if (isSplitViewEnabled) {
224 KUrl secondaryUrl;
225 stream >> secondaryUrl;
226 m_secondaryViewContainer->setUrl(secondaryUrl);
227 bool secondaryUrlEditable;
228 stream >> secondaryUrlEditable;
229 m_secondaryViewContainer->urlNavigator()->setUrlEditable(secondaryUrlEditable);
230 m_secondaryViewContainer->view()->restoreState(stream);
231 }
232
233 stream >> m_primaryViewActive;
234 if (m_primaryViewActive) {
235 m_primaryViewContainer->setActive(true);
236 } else {
237 Q_ASSERT(m_splitViewEnabled);
238 m_secondaryViewContainer->setActive(true);
239 }
240
241 QByteArray splitterState;
242 stream >> splitterState;
243 m_splitter->restoreState(splitterState);
244 }
245
246 void DolphinTabPage::restoreStateV1(const QByteArray& state)
247 {
248 if (state.isEmpty()) {
249 return;
250 }
251
252 QByteArray sd = state;
253 QDataStream stream(&sd, QIODevice::ReadOnly);
254
255 bool isSplitViewEnabled = false;
256 stream >> isSplitViewEnabled;
257 setSplitViewEnabled(isSplitViewEnabled);
258
259 KUrl primaryUrl;
260 stream >> primaryUrl;
261 m_primaryViewContainer->setUrl(primaryUrl);
262 bool primaryUrlEditable;
263 stream >> primaryUrlEditable;
264 m_primaryViewContainer->urlNavigator()->setUrlEditable(primaryUrlEditable);
265
266 if (isSplitViewEnabled) {
267 KUrl secondaryUrl;
268 stream >> secondaryUrl;
269 m_secondaryViewContainer->setUrl(secondaryUrl);
270 bool secondaryUrlEditable;
271 stream >> secondaryUrlEditable;
272 m_secondaryViewContainer->urlNavigator()->setUrlEditable(secondaryUrlEditable);
273 }
274
275 stream >> m_primaryViewActive;
276 if (m_primaryViewActive) {
277 m_primaryViewContainer->setActive(true);
278 } else {
279 Q_ASSERT(m_splitViewEnabled);
280 m_secondaryViewContainer->setActive(true);
281 }
282
283 QByteArray splitterState;
284 stream >> splitterState;
285 m_splitter->restoreState(splitterState);
286 }
287
288 void DolphinTabPage::slotViewActivated()
289 {
290 const DolphinView* oldActiveView = activeViewContainer()->view();
291
292 // Set the view, which was active before, to inactive
293 // and update the active view type.
294 if (m_splitViewEnabled) {
295 activeViewContainer()->setActive(false);
296 m_primaryViewActive = !m_primaryViewActive;
297 } else {
298 m_primaryViewActive = true;
299 }
300
301 const DolphinView* newActiveView = activeViewContainer()->view();
302
303 if (newActiveView != oldActiveView) {
304 disconnect(oldActiveView, SIGNAL(urlChanged(KUrl)),
305 this, SIGNAL(activeViewUrlChanged(KUrl)));
306 disconnect(oldActiveView, SIGNAL(redirection(KUrl,KUrl)),
307 this, SLOT(slotViewUrlRedirection(KUrl,KUrl)));
308 connect(newActiveView, SIGNAL(urlChanged(KUrl)),
309 this, SIGNAL(activeViewUrlChanged(KUrl)));
310 connect(newActiveView, SIGNAL(redirection(KUrl,KUrl)),
311 this, SLOT(slotViewUrlRedirection(KUrl,KUrl)));
312 }
313
314 emit activeViewUrlChanged(activeViewContainer()->url());
315 emit activeViewChanged(activeViewContainer());
316 }
317
318 void DolphinTabPage::slotViewUrlRedirection(const KUrl& oldUrl, const KUrl& newUrl)
319 {
320 Q_UNUSED(oldUrl);
321
322 emit activeViewUrlChanged(newUrl);
323 }
324
325 DolphinViewContainer* DolphinTabPage::createViewContainer(const KUrl& url) const
326 {
327 DolphinViewContainer* container = new DolphinViewContainer(url, m_splitter);
328 container->setActive(false);
329
330 const DolphinView* view = container->view();
331 connect(view, SIGNAL(activated()),
332 this, SLOT(slotViewActivated()));
333
334 return container;
335 }