]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
DolphinPart: provide a way to switch between view modes in konqueror.
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 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 "dolphincolumnview.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphincolumnwidget.h"
24 #include "dolphincontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinmodel.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "dolphinsettings.h"
29
30 #include "dolphin_columnmodesettings.h"
31
32 #include <kcolorutils.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35
36 #include <QAbstractProxyModel>
37 #include <QApplication>
38 #include <QPoint>
39 #include <QScrollBar>
40 #include <QTimer>
41 #include <QTimeLine>
42
43 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
44 QAbstractItemView(parent),
45 m_controller(controller),
46 m_active(false),
47 m_index(-1),
48 m_contentX(0),
49 m_columns(),
50 m_animation(0)
51 {
52 Q_ASSERT(controller != 0);
53
54 setAcceptDrops(true);
55 setDragDropMode(QAbstractItemView::DragDrop);
56 setDropIndicatorShown(false);
57 setSelectionMode(ExtendedSelection);
58
59 connect(this, SIGNAL(entered(const QModelIndex&)),
60 controller, SLOT(emitItemEntered(const QModelIndex&)));
61 connect(this, SIGNAL(viewportEntered()),
62 controller, SLOT(emitViewportEntered()));
63 connect(controller, SIGNAL(zoomIn()),
64 this, SLOT(zoomIn()));
65 connect(controller, SIGNAL(zoomOut()),
66 this, SLOT(zoomOut()));
67 connect(controller, SIGNAL(showHiddenFilesChanged(bool)),
68 this, SLOT(slotShowHiddenFilesChanged(bool)));
69 connect(controller, SIGNAL(showPreviewChanged(bool)),
70 this, SLOT(slotShowPreviewChanged(bool)));
71 connect(controller, SIGNAL(activationChanged(bool)),
72 this, SLOT(updateColumnsBackground(bool)));
73
74 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
75 this, SLOT(moveContentHorizontally(int)));
76
77 m_animation = new QTimeLine(500, this);
78 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
79
80 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, m_controller->url());
81 m_columns.append(column);
82 setActiveColumnIndex(0);
83
84 updateDecorationSize();
85 updateColumnsBackground(true);
86 }
87
88 DolphinColumnView::~DolphinColumnView()
89 {
90 }
91
92 QModelIndex DolphinColumnView::indexAt(const QPoint& point) const
93 {
94 foreach (DolphinColumnWidget* column, m_columns) {
95 const QPoint topLeft = column->frameGeometry().topLeft();
96 const QPoint adjustedPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
97 const QModelIndex index = column->indexAt(adjustedPoint);
98 if (index.isValid()) {
99 return index;
100 }
101 }
102
103 return QModelIndex();
104 }
105
106 void DolphinColumnView::scrollTo(const QModelIndex& index, ScrollHint hint)
107 {
108 activeColumn()->scrollTo(index, hint);
109 }
110
111 QRect DolphinColumnView::visualRect(const QModelIndex& index) const
112 {
113 return activeColumn()->visualRect(index);
114 }
115
116 void DolphinColumnView::invertSelection()
117 {
118 QItemSelectionModel* selectionModel = activeColumn()->selectionModel();
119 const QAbstractItemModel* itemModel = selectionModel->model();
120
121 const QModelIndex topLeft = itemModel->index(0, 0);
122 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
123 itemModel->columnCount() - 1);
124
125 const QItemSelection selection(topLeft, bottomRight);
126 selectionModel->select(selection, QItemSelectionModel::Toggle);
127 }
128
129 void DolphinColumnView::reload()
130 {
131 foreach (DolphinColumnWidget* column, m_columns) {
132 column->reload();
133 }
134 }
135
136 void DolphinColumnView::setRootUrl(const KUrl& url)
137 {
138 removeAllColumns();
139 m_columns[0]->setUrl(url);
140 }
141
142 void DolphinColumnView::setNameFilter(const QString& nameFilter)
143 {
144 foreach (DolphinColumnWidget* column, m_columns) {
145 column->setNameFilter(nameFilter);
146 }
147 }
148
149 QString DolphinColumnView::nameFilter() const
150 {
151 return activeColumn()->nameFilter();
152 }
153
154 KUrl DolphinColumnView::rootUrl() const
155 {
156 return m_columns[0]->url();
157 }
158
159 void DolphinColumnView::showColumn(const KUrl& url)
160 {
161 if (!rootUrl().isParentOf(url)) {
162 setRootUrl(url);
163 return;
164 }
165
166 int columnIndex = 0;
167 foreach (DolphinColumnWidget* column, m_columns) {
168 if (column->url() == url) {
169 // the column represents already the requested URL, hence activate it
170 requestActivation(column);
171 return;
172 } else if (!column->url().isParentOf(url)) {
173 // the column is no parent of the requested URL, hence
174 // just delete all remaining columns
175 if (columnIndex > 0) {
176 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + columnIndex;
177 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
178 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
179 (*it)->deleteLater();
180 }
181 m_columns.erase(start, end);
182
183 const int maxIndex = m_columns.count() - 1;
184 Q_ASSERT(maxIndex >= 0);
185 if (m_index > maxIndex) {
186 m_index = maxIndex;
187 }
188 break;
189 }
190 }
191 ++columnIndex;
192 }
193
194 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
195 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
196 // "c" will be created.
197 const int lastIndex = m_columns.count() - 1;
198 Q_ASSERT(lastIndex >= 0);
199
200 const KUrl& activeUrl = m_columns[lastIndex]->url();
201 Q_ASSERT(activeUrl.isParentOf(url));
202 Q_ASSERT(activeUrl != url);
203
204 QString path = activeUrl.url(KUrl::AddTrailingSlash);
205 const QString targetPath = url.url(KUrl::AddTrailingSlash);
206
207 columnIndex = lastIndex;
208 int slashIndex = path.count('/');
209 bool hasSubPath = (slashIndex >= 0);
210 while (hasSubPath) {
211 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
212 if (subPath.isEmpty()) {
213 hasSubPath = false;
214 } else {
215 path += subPath + '/';
216 ++slashIndex;
217
218 const KUrl childUrl = KUrl(path);
219 m_columns[columnIndex]->setChildUrl(childUrl);
220 columnIndex++;
221
222 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, childUrl);
223 const QString filter = nameFilter();
224 if (!filter.isEmpty()) {
225 column->setNameFilter(filter);
226 }
227 column->setActive(false);
228
229 m_columns.append(column);
230
231 // Before invoking layoutColumns() the column must be set visible temporary.
232 // To prevent a flickering the initial geometry is set to a hidden position.
233 column->setGeometry(QRect(-1, -1, 1, 1));
234 column->show();
235 layoutColumns();
236 updateScrollBar();
237 }
238 }
239
240 // set the last column as active column without modifying the controller
241 // and hence the history
242 activeColumn()->setActive(false);
243 m_index = columnIndex;
244 activeColumn()->setActive(true);
245 assureVisibleActiveColumn();
246 }
247
248 void DolphinColumnView::selectAll()
249 {
250 activeColumn()->selectAll();
251 }
252
253 bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const
254 {
255 Q_UNUSED(index);
256 return false;//activeColumn()->isIndexHidden(index);
257 }
258
259 QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
260 {
261 // Parts of this code have been taken from QColumnView::moveCursor().
262 // Copyright (C) 1992-2007 Trolltech ASA.
263
264 Q_UNUSED(modifiers);
265 if (model() == 0) {
266 return QModelIndex();
267 }
268
269 const QModelIndex current = currentIndex();
270 if (isRightToLeft()) {
271 if (cursorAction == MoveLeft) {
272 cursorAction = MoveRight;
273 } else if (cursorAction == MoveRight) {
274 cursorAction = MoveLeft;
275 }
276 }
277
278 switch (cursorAction) {
279 case MoveLeft:
280 if (m_index > 0) {
281 setActiveColumnIndex(m_index - 1);
282 m_controller->triggerUrlChangeRequest(activeColumn()->url());
283 }
284 break;
285
286 case MoveRight:
287 if (m_index < m_columns.count() - 1) {
288 setActiveColumnIndex(m_index + 1);
289 m_controller->triggerUrlChangeRequest(m_columns[m_index]->url());
290 }
291 break;
292
293 default:
294 break;
295 }
296
297 return QModelIndex();
298 }
299
300 void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags)
301 {
302 Q_UNUSED(rect);
303 Q_UNUSED(flags);
304 //activeColumn()->setSelection(rect, flags);
305 }
306
307 QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const
308 {
309 Q_UNUSED(selection);
310 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
311 }
312
313 int DolphinColumnView::horizontalOffset() const
314 {
315 return -m_contentX;
316 }
317
318 int DolphinColumnView::verticalOffset() const
319 {
320 return 0;
321 }
322
323 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
324 {
325 m_controller->requestActivation();
326 QAbstractItemView::mousePressEvent(event);
327 }
328
329 void DolphinColumnView::resizeEvent(QResizeEvent* event)
330 {
331 QAbstractItemView::resizeEvent(event);
332 layoutColumns();
333 updateScrollBar();
334 }
335
336 void DolphinColumnView::zoomIn()
337 {
338 if (isZoomInPossible()) {
339 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
340 switch (settings->iconSize()) {
341 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
342 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
343 default: Q_ASSERT(false); break;
344 }
345 updateDecorationSize();
346 }
347 }
348
349 void DolphinColumnView::zoomOut()
350 {
351 if (isZoomOutPossible()) {
352 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
353 switch (settings->iconSize()) {
354 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
355 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
356 default: Q_ASSERT(false); break;
357 }
358 updateDecorationSize();
359 }
360 }
361
362 void DolphinColumnView::moveContentHorizontally(int x)
363 {
364 m_contentX = isRightToLeft() ? +x : -x;
365 layoutColumns();
366 }
367
368 void DolphinColumnView::updateDecorationSize()
369 {
370 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
371 const int iconSize = settings->iconSize();
372
373 foreach (QObject* object, viewport()->children()) {
374 if (object->inherits("QListView")) {
375 DolphinColumnWidget* widget = static_cast<DolphinColumnWidget*>(object);
376 widget->setDecorationSize(QSize(iconSize, iconSize));
377 }
378 }
379
380 m_controller->setZoomInPossible(isZoomInPossible());
381 m_controller->setZoomOutPossible(isZoomOutPossible());
382
383 doItemsLayout();
384 }
385
386 void DolphinColumnView::updateColumnsBackground(bool active)
387 {
388 if (active == m_active) {
389 return;
390 }
391
392 m_active = active;
393
394 // dim the background of the viewport
395 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
396 color.setAlpha(150);
397
398 QPalette palette;
399 palette.setColor(viewport()->backgroundRole(), color);
400 viewport()->setPalette(palette);
401
402 foreach (DolphinColumnWidget* column, m_columns) {
403 column->updateBackground();
404 }
405 }
406
407 void DolphinColumnView::slotShowHiddenFilesChanged(bool show)
408 {
409 foreach (DolphinColumnWidget* column, m_columns) {
410 column->setShowHiddenFiles(show);
411 }
412 }
413
414 void DolphinColumnView::slotShowPreviewChanged(bool show)
415 {
416 foreach (DolphinColumnWidget* column, m_columns) {
417 column->setShowPreview(show);
418 }
419 }
420
421 bool DolphinColumnView::isZoomInPossible() const
422 {
423 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
424 return settings->iconSize() < KIconLoader::SizeLarge;
425 }
426
427 bool DolphinColumnView::isZoomOutPossible() const
428 {
429 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
430 return settings->iconSize() > KIconLoader::SizeSmall;
431 }
432
433 void DolphinColumnView::setActiveColumnIndex(int index)
434 {
435 if (m_index == index) {
436 return;
437 }
438
439 const bool hasActiveColumn = (m_index >= 0);
440 if (hasActiveColumn) {
441 m_columns[m_index]->setActive(false);
442 }
443
444 m_index = index;
445 m_columns[m_index]->setActive(true);
446
447 assureVisibleActiveColumn();
448 }
449
450 void DolphinColumnView::layoutColumns()
451 {
452 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
453 const int columnWidth = settings->columnWidth();
454 if (isRightToLeft()) {
455 int x = viewport()->width() - columnWidth + m_contentX;
456 foreach (DolphinColumnWidget* column, m_columns) {
457 column->setGeometry(QRect(x, 0, columnWidth, viewport()->height()));
458 x -= columnWidth;
459 }
460 } else {
461 int x = m_contentX;
462 foreach (DolphinColumnWidget* column, m_columns) {
463 column->setGeometry(QRect(x, 0, columnWidth, viewport()->height()));
464 x += columnWidth;
465 }
466 }
467 }
468
469 void DolphinColumnView::updateScrollBar()
470 {
471 int contentWidth = 0;
472 foreach (DolphinColumnWidget* column, m_columns) {
473 contentWidth += column->width();
474 }
475
476 horizontalScrollBar()->setPageStep(contentWidth);
477 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
478 }
479
480 void DolphinColumnView::assureVisibleActiveColumn()
481 {
482 const int viewportWidth = viewport()->width();
483 const int x = activeColumn()->x();
484 const int width = activeColumn()->width();
485 if (x + width > viewportWidth) {
486 const int newContentX = m_contentX - x - width + viewportWidth;
487 if (isRightToLeft()) {
488 m_animation->setFrameRange(m_contentX, newContentX);
489 } else {
490 m_animation->setFrameRange(-m_contentX, -newContentX);
491 }
492 m_animation->start();
493 } else if (x < 0) {
494 const int newContentX = m_contentX - x;
495 if (isRightToLeft()) {
496 m_animation->setFrameRange(m_contentX, newContentX);
497 } else {
498 m_animation->setFrameRange(-m_contentX, -newContentX);
499 }
500 m_animation->start();
501 }
502 }
503
504 void DolphinColumnView::requestActivation(DolphinColumnWidget* column)
505 {
506 if (column->isActive()) {
507 assureVisibleActiveColumn();
508 } else {
509 int index = 0;
510 foreach (DolphinColumnWidget* currColumn, m_columns) {
511 if (currColumn == column) {
512 setActiveColumnIndex(index);
513 return;
514 }
515 ++index;
516 }
517 }
518 }
519
520 void DolphinColumnView::removeAllColumns()
521 {
522 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + 1;
523 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
524 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
525 (*it)->deleteLater();
526 }
527 m_columns.erase(start, end);
528 m_index = 0;
529 m_columns[0]->setActive(true);
530 assureVisibleActiveColumn();
531 }
532
533 #include "dolphincolumnview.moc"