]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphincolumnviewcontainer.cpp
Sourcecode hierarchy cleanup: Move further files from src to src/views
[dolphin.git] / src / views / dolphincolumnviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 by Peter Penz <peter.penz@gmx.at> *
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 "dolphincolumnviewcontainer.h"
21
22 #include "dolphin_columnmodesettings.h"
23
24 #include "dolphincolumnview.h"
25 #include "dolphinviewcontroller.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "settings/dolphinsettings.h"
29 #include "viewmodecontroller.h"
30
31 #include <QPoint>
32 #include <QScrollBar>
33 #include <QTimeLine>
34 #include <QTimer>
35
36 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget* parent,
37 DolphinViewController* dolphinViewController,
38 const ViewModeController* viewModeController) :
39 QScrollArea(parent),
40 m_dolphinViewController(dolphinViewController),
41 m_viewModeController(viewModeController),
42 m_active(false),
43 m_index(-1),
44 m_contentX(0),
45 m_columns(),
46 m_emptyViewport(0),
47 m_animation(0),
48 m_dragSource(0),
49 m_activeUrlTimer(0)
50 {
51 Q_ASSERT(dolphinViewController != 0);
52 Q_ASSERT(viewModeController != 0);
53
54 setAcceptDrops(true);
55 setFocusPolicy(Qt::NoFocus);
56 setFrameShape(QFrame::NoFrame);
57 setLayoutDirection(Qt::LeftToRight);
58
59 connect(viewModeController, SIGNAL(activationChanged(bool)),
60 this, SLOT(updateColumnsBackground(bool)));
61
62 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
63 this, SLOT(moveContentHorizontally(int)));
64
65 m_animation = new QTimeLine(500, this);
66 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
67
68 m_activeUrlTimer = new QTimer(this);
69 m_activeUrlTimer->setSingleShot(true);
70 m_activeUrlTimer->setInterval(200);
71 connect(m_activeUrlTimer, SIGNAL(timeout()),
72 this, SLOT(updateActiveUrl()));
73
74 DolphinColumnView* column = new DolphinColumnView(viewport(), this, viewModeController->url());
75 m_columns.append(column);
76 requestActivation(column);
77
78 m_emptyViewport = new QFrame(viewport());
79 m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
80
81 updateColumnsBackground(true);
82
83 }
84
85 DolphinColumnViewContainer::~DolphinColumnViewContainer()
86 {
87 delete m_dragSource;
88 m_dragSource = 0;
89 }
90
91 KUrl DolphinColumnViewContainer::rootUrl() const
92 {
93 return m_columns[0]->url();
94 }
95
96 QAbstractItemView* DolphinColumnViewContainer::activeColumn() const
97 {
98 return m_columns[m_index];
99 }
100
101 void DolphinColumnViewContainer::showColumn(const KUrl& url)
102 {
103 if (!rootUrl().isParentOf(url)) {
104 removeAllColumns();
105 m_columns[0]->setUrl(url);
106 return;
107 }
108
109 int columnIndex = 0;
110 foreach (DolphinColumnView* column, m_columns) {
111 if (column->url() == url) {
112 // the column represents already the requested URL, hence activate it
113 requestActivation(column);
114 layoutColumns();
115 return;
116 } else if (!column->url().isParentOf(url)) {
117 // the column is no parent of the requested URL, hence
118 // just delete all remaining columns
119 if (columnIndex > 0) {
120 QList<DolphinColumnView*>::iterator start = m_columns.begin() + columnIndex;
121 QList<DolphinColumnView*>::iterator end = m_columns.end();
122 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
123 deleteColumn(*it);
124 }
125 m_columns.erase(start, end);
126
127 const int maxIndex = m_columns.count() - 1;
128 Q_ASSERT(maxIndex >= 0);
129 if (m_index > maxIndex) {
130 m_index = maxIndex;
131 }
132 break;
133 }
134 }
135 ++columnIndex;
136 }
137
138 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
139 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
140 // "c" will be created.
141 const int lastIndex = m_columns.count() - 1;
142 Q_ASSERT(lastIndex >= 0);
143
144 const KUrl& activeUrl = m_columns[lastIndex]->url();
145 Q_ASSERT(activeUrl.isParentOf(url));
146 Q_ASSERT(activeUrl != url);
147
148 QString path = activeUrl.url(KUrl::AddTrailingSlash);
149 const QString targetPath = url.url(KUrl::AddTrailingSlash);
150
151 columnIndex = lastIndex;
152 int slashIndex = path.count('/');
153 bool hasSubPath = (slashIndex >= 0);
154 while (hasSubPath) {
155 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
156 if (subPath.isEmpty()) {
157 hasSubPath = false;
158 } else {
159 path += subPath + '/';
160 ++slashIndex;
161
162 const KUrl childUrl = KUrl(path);
163 m_columns[columnIndex]->setChildUrl(childUrl);
164 columnIndex++;
165
166 DolphinColumnView* column = new DolphinColumnView(viewport(), this, childUrl);
167 m_columns.append(column);
168
169 // Before invoking layoutColumns() the column must be set visible temporary.
170 // To prevent a flickering the initial geometry is set to a hidden position.
171 column->setGeometry(QRect(-1, -1, 1, 1));
172 column->show();
173 layoutColumns();
174 updateScrollBar();
175 }
176 }
177
178 requestActivation(m_columns[columnIndex]);
179 }
180
181 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent* event)
182 {
183 m_dolphinViewController->requestActivation();
184 QScrollArea::mousePressEvent(event);
185 }
186
187 void DolphinColumnViewContainer::keyPressEvent(QKeyEvent* event)
188 {
189 if (event->key() == Qt::Key_Left) {
190 if (m_index > 0) {
191 requestActivation(m_columns[m_index - 1]);
192 }
193 } else {
194 QScrollArea::keyPressEvent(event);
195 }
196 }
197
198 void DolphinColumnViewContainer::resizeEvent(QResizeEvent* event)
199 {
200 QScrollArea::resizeEvent(event);
201 layoutColumns();
202 updateScrollBar();
203 assureVisibleActiveColumn();
204 }
205
206 void DolphinColumnViewContainer::wheelEvent(QWheelEvent* event)
207 {
208 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
209 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
210 event->ignore();
211 } else {
212 QScrollArea::wheelEvent(event);
213 }
214 }
215
216 void DolphinColumnViewContainer::moveContentHorizontally(int x)
217 {
218 m_contentX = isRightToLeft() ? +x : -x;
219 layoutColumns();
220 }
221
222 void DolphinColumnViewContainer::updateColumnsBackground(bool active)
223 {
224 if (active == m_active) {
225 return;
226 }
227
228 m_active = active;
229
230 // dim the background of the viewport
231 const QPalette::ColorRole role = viewport()->backgroundRole();
232 QColor background = viewport()->palette().color(role);
233 background.setAlpha(0); // make background transparent
234
235 QPalette palette = viewport()->palette();
236 palette.setColor(role, background);
237 viewport()->setPalette(palette);
238
239 foreach (DolphinColumnView* column, m_columns) {
240 column->updateBackground();
241 }
242 }
243
244 void DolphinColumnViewContainer::updateActiveUrl()
245 {
246 const KUrl activeUrl = m_columns[m_index]->url();
247 m_dolphinViewController->requestUrlChange(activeUrl);
248 }
249
250 void DolphinColumnViewContainer::layoutColumns()
251 {
252 const int gap = 4;
253
254 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
255 const int columnWidth = settings->columnWidth();
256
257 QRect emptyViewportRect;
258 if (isRightToLeft()) {
259 int x = viewport()->width() - columnWidth + m_contentX;
260 foreach (DolphinColumnView* column, m_columns) {
261 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
262 x -= columnWidth;
263 }
264 emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height());
265 } else {
266 int x = m_contentX;
267 foreach (DolphinColumnView* column, m_columns) {
268 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
269 x += columnWidth;
270 }
271 emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height());
272 }
273
274 if (emptyViewportRect.isValid()) {
275 m_emptyViewport->show();
276 m_emptyViewport->setGeometry(emptyViewportRect);
277 } else {
278 m_emptyViewport->hide();
279 }
280 }
281
282 void DolphinColumnViewContainer::updateScrollBar()
283 {
284 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
285 const int contentWidth = m_columns.count() * settings->columnWidth();
286
287 horizontalScrollBar()->setPageStep(contentWidth);
288 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
289 }
290
291 void DolphinColumnViewContainer::assureVisibleActiveColumn()
292 {
293 const int viewportWidth = viewport()->width();
294 const int x = activeColumn()->x();
295
296 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
297 const int width = settings->columnWidth();
298
299 if (x + width > viewportWidth) {
300 const int newContentX = m_contentX - x - width + viewportWidth;
301 if (isRightToLeft()) {
302 m_animation->setFrameRange(m_contentX, newContentX);
303 } else {
304 m_animation->setFrameRange(-m_contentX, -newContentX);
305 }
306 if (m_animation->state() != QTimeLine::Running) {
307 m_animation->start();
308 }
309 } else if (x < 0) {
310 const int newContentX = m_contentX - x;
311 if (isRightToLeft()) {
312 m_animation->setFrameRange(m_contentX, newContentX);
313 } else {
314 m_animation->setFrameRange(-m_contentX, -newContentX);
315 }
316 if (m_animation->state() != QTimeLine::Running) {
317 m_animation->start();
318 }
319 }
320 }
321
322 void DolphinColumnViewContainer::requestActivation(DolphinColumnView* column)
323 {
324 if (m_dolphinViewController->itemView() != column) {
325 m_dolphinViewController->setItemView(column);
326 }
327 if (focusProxy() != column) {
328 setFocusProxy(column);
329 }
330
331 if (column->isActive()) {
332 assureVisibleActiveColumn();
333 } else {
334 // Deactivate the currently active column
335 if (m_index >= 0) {
336 m_columns[m_index]->setActive(false);
337 }
338
339 // Get the index of the column that should get activated
340 int index = 0;
341 foreach (DolphinColumnView* currColumn, m_columns) {
342 if (currColumn == column) {
343 break;
344 }
345 ++index;
346 }
347
348 Q_ASSERT(index != m_index);
349 Q_ASSERT(index < m_columns.count());
350
351 // Activate the requested column
352 m_index = index;
353 m_columns[m_index]->setActive(true);
354
355 assureVisibleActiveColumn();
356 m_activeUrlTimer->start(); // calls slot updateActiveUrl()
357 }
358 }
359
360 void DolphinColumnViewContainer::removeAllColumns()
361 {
362 QList<DolphinColumnView*>::iterator start = m_columns.begin() + 1;
363 QList<DolphinColumnView*>::iterator end = m_columns.end();
364 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
365 deleteColumn(*it);
366 }
367 m_columns.erase(start, end);
368 m_index = 0;
369 m_columns[0]->setActive(true);
370 assureVisibleActiveColumn();
371 }
372
373 QPoint DolphinColumnViewContainer::columnPosition(DolphinColumnView* column, const QPoint& point) const
374 {
375 const QPoint topLeft = column->frameGeometry().topLeft();
376 return QPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
377 }
378
379 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView* column)
380 {
381 if (column == 0) {
382 return;
383 }
384
385 if (m_dolphinViewController->itemView() == column) {
386 m_dolphinViewController->setItemView(0);
387 }
388 // deleteWhenNotDragSource(column) does not necessarily delete column,
389 // and we want its preview generator destroyed immediately.
390 column->hide();
391 // Prevent automatic destruction of column when this DolphinColumnViewContainer
392 // is destroyed.
393 column->setParent(0);
394 column->disconnect();
395
396 if (DragAndDropHelper::instance().isDragSource(column)) {
397 // The column is a drag source (the feature "Open folders
398 // during drag operations" is used). Deleting the view
399 // during an ongoing drag operation is not allowed, so
400 // this will postponed.
401 if (m_dragSource != 0) {
402 // the old stored view is obviously not the drag source anymore
403 m_dragSource->deleteLater();
404 m_dragSource = 0;
405 }
406 column->hide();
407 column->setParent(0);
408 column->disconnect();
409
410 m_dragSource = column;
411 } else {
412 column->deleteLater();
413 }
414 }
415
416 #include "dolphincolumnviewcontainer.moc"