]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabpage.cpp
Merge branch 'KDE/4.14'
[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 << m_splitViewEnabled;
175
176 stream << m_primaryViewContainer->url();
177 stream << m_primaryViewContainer->urlNavigator()->isUrlEditable();
178
179 if (m_splitViewEnabled) {
180 stream << m_secondaryViewContainer->url();
181 stream << m_secondaryViewContainer->urlNavigator()->isUrlEditable();
182 }
183
184 stream << m_primaryViewActive;
185 stream << m_splitter->saveState();
186
187 return state;
188 }
189
190 void DolphinTabPage::restoreState(const QByteArray& state)
191 {
192 if (state.isEmpty()) {
193 return;
194 }
195
196 QByteArray sd = state;
197 QDataStream stream(&sd, QIODevice::ReadOnly);
198
199 bool isSplitViewEnabled = false;
200 stream >> isSplitViewEnabled;
201 setSplitViewEnabled(isSplitViewEnabled);
202
203 KUrl primaryUrl;
204 stream >> primaryUrl;
205 m_primaryViewContainer->setUrl(primaryUrl);
206 bool primaryUrlEditable;
207 stream >> primaryUrlEditable;
208 m_primaryViewContainer->urlNavigator()->setUrlEditable(primaryUrlEditable);
209
210 if (isSplitViewEnabled) {
211 KUrl secondaryUrl;
212 stream >> secondaryUrl;
213 m_secondaryViewContainer->setUrl(secondaryUrl);
214 bool secondaryUrlEditable;
215 stream >> secondaryUrlEditable;
216 m_secondaryViewContainer->urlNavigator()->setUrlEditable(secondaryUrlEditable);
217 }
218
219 stream >> m_primaryViewActive;
220 if (m_primaryViewActive) {
221 m_primaryViewContainer->setActive(true);
222 } else {
223 Q_ASSERT(m_splitViewEnabled);
224 m_secondaryViewContainer->setActive(true);
225 }
226
227 QByteArray splitterState;
228 stream >> splitterState;
229 m_splitter->restoreState(splitterState);
230 }
231
232 void DolphinTabPage::slotViewActivated()
233 {
234 const DolphinView* oldActiveView = activeViewContainer()->view();
235
236 // Set the view, which was active before, to inactive
237 // and update the active view type.
238 if (m_splitViewEnabled) {
239 activeViewContainer()->setActive(false);
240 m_primaryViewActive = !m_primaryViewActive;
241 } else {
242 m_primaryViewActive = true;
243 }
244
245 const DolphinView* newActiveView = activeViewContainer()->view();
246
247 if (newActiveView != oldActiveView) {
248 disconnect(oldActiveView, SIGNAL(urlChanged(KUrl)),
249 this, SIGNAL(activeViewUrlChanged(KUrl)));
250 disconnect(oldActiveView, SIGNAL(redirection(KUrl,KUrl)),
251 this, SLOT(slotViewUrlRedirection(KUrl,KUrl)));
252 connect(newActiveView, SIGNAL(urlChanged(KUrl)),
253 this, SIGNAL(activeViewUrlChanged(KUrl)));
254 connect(newActiveView, SIGNAL(redirection(KUrl,KUrl)),
255 this, SLOT(slotViewUrlRedirection(KUrl,KUrl)));
256 }
257
258 emit activeViewUrlChanged(activeViewContainer()->url());
259 emit activeViewChanged(activeViewContainer());
260 }
261
262 void DolphinTabPage::slotViewUrlRedirection(const KUrl& oldUrl, const KUrl& newUrl)
263 {
264 Q_UNUSED(oldUrl);
265
266 emit activeViewUrlChanged(newUrl);
267 }
268
269 DolphinViewContainer* DolphinTabPage::createViewContainer(const KUrl& url) const
270 {
271 DolphinViewContainer* container = new DolphinViewContainer(url, m_splitter);
272 container->setActive(false);
273
274 const DolphinView* view = container->view();
275 connect(view, SIGNAL(activated()),
276 this, SLOT(slotViewActivated()));
277
278 return container;
279 }