]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Let the DolphinController be aware on which QAbstractItemView instance he is working...
[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 "dolphincolumnwidget.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsettings.h"
25
26 #include "dolphin_columnmodesettings.h"
27
28 #include <QPoint>
29 #include <QScrollBar>
30 #include <QTimeLine>
31
32 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
33 QAbstractItemView(parent),
34 m_controller(controller),
35 m_active(false),
36 m_index(-1),
37 m_contentX(0),
38 m_columns(),
39 m_emptyViewport(0),
40 m_animation(0),
41 m_nameFilter()
42 {
43 Q_ASSERT(controller != 0);
44
45 setAcceptDrops(true);
46 setDragDropMode(QAbstractItemView::DragDrop);
47 setDropIndicatorShown(false);
48 setSelectionMode(ExtendedSelection);
49 setFocusPolicy(Qt::NoFocus);
50 setFrameShape(QFrame::NoFrame);
51
52 connect(this, SIGNAL(viewportEntered()),
53 controller, SLOT(emitViewportEntered()));
54 connect(controller, SIGNAL(zoomIn()),
55 this, SLOT(zoomIn()));
56 connect(controller, SIGNAL(zoomOut()),
57 this, SLOT(zoomOut()));
58 connect(controller, SIGNAL(activationChanged(bool)),
59 this, SLOT(updateColumnsBackground(bool)));
60
61 const DolphinView* view = controller->dolphinView();
62 connect(view, SIGNAL(sortingChanged(DolphinView::Sorting)),
63 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
64 connect(view, SIGNAL(sortOrderChanged(Qt::SortOrder)),
65 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
66 connect(view, SIGNAL(showHiddenFilesChanged()),
67 this, SLOT(slotShowHiddenFilesChanged()));
68 connect(view, SIGNAL(showPreviewChanged()),
69 this, SLOT(slotShowPreviewChanged()));
70
71 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
72 this, SLOT(moveContentHorizontally(int)));
73
74 m_animation = new QTimeLine(500, this);
75 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
76
77 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, m_controller->url());
78 m_columns.append(column);
79 setActiveColumnIndex(0);
80
81 m_emptyViewport = new QFrame(viewport());
82 m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
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 if (nameFilter != m_nameFilter) {
145 m_nameFilter = nameFilter;
146 foreach (DolphinColumnWidget* column, m_columns) {
147 column->setNameFilter(nameFilter);
148 }
149 }
150 }
151
152 QString DolphinColumnView::nameFilter() const
153 {
154 return m_nameFilter;
155 }
156
157 KUrl DolphinColumnView::rootUrl() const
158 {
159 return m_columns[0]->url();
160 }
161
162 void DolphinColumnView::showColumn(const KUrl& url)
163 {
164 if (!rootUrl().isParentOf(url)) {
165 setRootUrl(url);
166 return;
167 }
168
169 int columnIndex = 0;
170 foreach (DolphinColumnWidget* column, m_columns) {
171 if (column->url() == url) {
172 // the column represents already the requested URL, hence activate it
173 requestActivation(column);
174 layoutColumns();
175 return;
176 } else if (!column->url().isParentOf(url)) {
177 // the column is no parent of the requested URL, hence
178 // just delete all remaining columns
179 if (columnIndex > 0) {
180 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + columnIndex;
181 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
182 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
183 (*it)->deleteLater();
184 }
185 m_columns.erase(start, end);
186
187 const int maxIndex = m_columns.count() - 1;
188 Q_ASSERT(maxIndex >= 0);
189 if (m_index > maxIndex) {
190 m_index = maxIndex;
191 }
192 break;
193 }
194 }
195 ++columnIndex;
196 }
197
198 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
199 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
200 // "c" will be created.
201 const int lastIndex = m_columns.count() - 1;
202 Q_ASSERT(lastIndex >= 0);
203
204 const KUrl& activeUrl = m_columns[lastIndex]->url();
205 Q_ASSERT(activeUrl.isParentOf(url));
206 Q_ASSERT(activeUrl != url);
207
208 QString path = activeUrl.url(KUrl::AddTrailingSlash);
209 const QString targetPath = url.url(KUrl::AddTrailingSlash);
210
211 columnIndex = lastIndex;
212 int slashIndex = path.count('/');
213 bool hasSubPath = (slashIndex >= 0);
214 while (hasSubPath) {
215 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
216 if (subPath.isEmpty()) {
217 hasSubPath = false;
218 } else {
219 path += subPath + '/';
220 ++slashIndex;
221
222 const KUrl childUrl = KUrl(path);
223 m_columns[columnIndex]->setChildUrl(childUrl);
224 columnIndex++;
225
226 DolphinColumnWidget* column = new DolphinColumnWidget(viewport(), this, childUrl);
227 const QString filter = nameFilter();
228 if (!filter.isEmpty()) {
229 column->setNameFilter(filter);
230 }
231 column->setActive(false);
232
233 m_columns.append(column);
234
235 // Before invoking layoutColumns() the column must be set visible temporary.
236 // To prevent a flickering the initial geometry is set to a hidden position.
237 column->setGeometry(QRect(-1, -1, 1, 1));
238 column->show();
239 layoutColumns();
240 updateScrollBar();
241 }
242 }
243
244 // set the last column as active column without modifying the controller
245 // and hence the history
246 activeColumn()->setActive(false);
247 m_index = columnIndex;
248 activeColumn()->setActive(true);
249 assureVisibleActiveColumn();
250 }
251
252 void DolphinColumnView::selectAll()
253 {
254 activeColumn()->selectAll();
255 }
256
257 bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const
258 {
259 Q_UNUSED(index);
260 return false;//activeColumn()->isIndexHidden(index);
261 }
262
263 QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
264 {
265 // Parts of this code have been taken from QColumnView::moveCursor().
266 // Copyright (C) 1992-2007 Trolltech ASA.
267
268 Q_UNUSED(modifiers);
269 if (model() == 0) {
270 return QModelIndex();
271 }
272
273 const QModelIndex current = currentIndex();
274 if (isRightToLeft()) {
275 if (cursorAction == MoveLeft) {
276 cursorAction = MoveRight;
277 } else if (cursorAction == MoveRight) {
278 cursorAction = MoveLeft;
279 }
280 }
281
282 switch (cursorAction) {
283 case MoveLeft:
284 if (m_index > 0) {
285 setActiveColumnIndex(m_index - 1);
286 m_controller->triggerUrlChangeRequest(activeColumn()->url());
287 }
288 break;
289
290 case MoveRight:
291 if (m_index < m_columns.count() - 1) {
292 setActiveColumnIndex(m_index + 1);
293 m_controller->triggerUrlChangeRequest(m_columns[m_index]->url());
294 }
295 break;
296
297 default:
298 break;
299 }
300
301 return QModelIndex();
302 }
303
304 void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags)
305 {
306 Q_UNUSED(rect);
307 Q_UNUSED(flags);
308 //activeColumn()->setSelection(rect, flags);
309 }
310
311 QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const
312 {
313 Q_UNUSED(selection);
314 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
315 }
316
317 int DolphinColumnView::horizontalOffset() const
318 {
319 return -m_contentX;
320 }
321
322 int DolphinColumnView::verticalOffset() const
323 {
324 return 0;
325 }
326
327 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
328 {
329 m_controller->requestActivation();
330 QAbstractItemView::mousePressEvent(event);
331 }
332
333 void DolphinColumnView::resizeEvent(QResizeEvent* event)
334 {
335 QAbstractItemView::resizeEvent(event);
336 layoutColumns();
337 updateScrollBar();
338 assureVisibleActiveColumn();
339 }
340
341 void DolphinColumnView::wheelEvent(QWheelEvent* event)
342 {
343 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
344 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
345 event->ignore();
346 return;
347 }
348 QAbstractItemView::wheelEvent(event);
349 }
350
351 void DolphinColumnView::zoomIn()
352 {
353 if (isZoomInPossible()) {
354 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
355 switch (settings->iconSize()) {
356 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
357 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
358 default: Q_ASSERT(false); break;
359 }
360 updateDecorationSize();
361 }
362 }
363
364 void DolphinColumnView::zoomOut()
365 {
366 if (isZoomOutPossible()) {
367 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
368 switch (settings->iconSize()) {
369 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
370 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
371 default: Q_ASSERT(false); break;
372 }
373 updateDecorationSize();
374 }
375 }
376
377 void DolphinColumnView::moveContentHorizontally(int x)
378 {
379 m_contentX = isRightToLeft() ? +x : -x;
380 layoutColumns();
381 }
382
383 void DolphinColumnView::updateDecorationSize()
384 {
385 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
386 const int iconSize = settings->iconSize();
387 const QSize size(iconSize, iconSize);
388 setIconSize(size);
389
390 foreach (QObject* object, viewport()->children()) {
391 if (object->inherits("QListView")) {
392 DolphinColumnWidget* widget = static_cast<DolphinColumnWidget*>(object);
393 widget->setDecorationSize(size);
394 }
395 }
396
397 m_controller->setZoomInPossible(isZoomInPossible());
398 m_controller->setZoomOutPossible(isZoomOutPossible());
399
400 doItemsLayout();
401 }
402
403 void DolphinColumnView::updateColumnsBackground(bool active)
404 {
405 if (active == m_active) {
406 return;
407 }
408
409 m_active = active;
410
411 // dim the background of the viewport
412 const QPalette::ColorRole role = viewport()->backgroundRole();
413 QColor background = viewport()->palette().color(role);
414 background.setAlpha(0); // make background transparent
415
416 QPalette palette = viewport()->palette();
417 palette.setColor(role, background);
418 viewport()->setPalette(palette);
419
420 foreach (DolphinColumnWidget* column, m_columns) {
421 column->updateBackground();
422 }
423 }
424
425 void DolphinColumnView::slotSortingChanged(DolphinView::Sorting sorting)
426 {
427 foreach (DolphinColumnWidget* column, m_columns) {
428 column->setSorting(sorting);
429 }
430 }
431
432 void DolphinColumnView::slotSortOrderChanged(Qt::SortOrder order)
433 {
434 foreach (DolphinColumnWidget* column, m_columns) {
435 column->setSortOrder(order);
436 }
437 }
438
439 void DolphinColumnView::slotShowHiddenFilesChanged()
440 {
441 const bool show = m_controller->dolphinView()->showHiddenFiles();
442 foreach (DolphinColumnWidget* column, m_columns) {
443 column->setShowHiddenFiles(show);
444 }
445 }
446
447 void DolphinColumnView::slotShowPreviewChanged()
448 {
449 const bool show = m_controller->dolphinView()->showPreview();
450 foreach (DolphinColumnWidget* column, m_columns) {
451 column->setShowPreview(show);
452 }
453 }
454
455 bool DolphinColumnView::isZoomInPossible() const
456 {
457 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
458 return settings->iconSize() < KIconLoader::SizeLarge;
459 }
460
461 bool DolphinColumnView::isZoomOutPossible() const
462 {
463 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
464 return settings->iconSize() > KIconLoader::SizeSmall;
465 }
466
467 void DolphinColumnView::setActiveColumnIndex(int index)
468 {
469 if (m_index == index) {
470 return;
471 }
472
473 const bool hasActiveColumn = (m_index >= 0);
474 if (hasActiveColumn) {
475 m_columns[m_index]->setActive(false);
476 }
477
478 m_index = index;
479 m_columns[m_index]->setActive(true);
480
481 assureVisibleActiveColumn();
482 }
483
484 void DolphinColumnView::layoutColumns()
485 {
486 const int gap = 4;
487
488 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
489 const int columnWidth = settings->columnWidth();
490
491 QRect emptyViewportRect;
492 if (isRightToLeft()) {
493 int x = viewport()->width() - columnWidth + m_contentX;
494 foreach (DolphinColumnWidget* column, m_columns) {
495 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
496 x -= columnWidth;
497 }
498 emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height());
499 } else {
500 int x = m_contentX;
501 foreach (DolphinColumnWidget* column, m_columns) {
502 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
503 x += columnWidth;
504 }
505 emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height());
506 }
507
508 if (emptyViewportRect.isValid()) {
509 m_emptyViewport->show();
510 m_emptyViewport->setGeometry(emptyViewportRect);
511 } else {
512 m_emptyViewport->hide();
513 }
514 }
515
516 void DolphinColumnView::updateScrollBar()
517 {
518 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
519 const int contentWidth = m_columns.count() * settings->columnWidth();
520
521 horizontalScrollBar()->setPageStep(contentWidth);
522 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
523 }
524
525 void DolphinColumnView::assureVisibleActiveColumn()
526 {
527 const int viewportWidth = viewport()->width();
528 const int x = activeColumn()->x();
529
530 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
531 const int width = settings->columnWidth();
532
533 if (x + width > viewportWidth) {
534 const int newContentX = m_contentX - x - width + viewportWidth;
535 if (isRightToLeft()) {
536 m_animation->setFrameRange(m_contentX, newContentX);
537 } else {
538 m_animation->setFrameRange(-m_contentX, -newContentX);
539 }
540 if (m_animation->state() != QTimeLine::Running) {
541 m_animation->start();
542 }
543 } else if (x < 0) {
544 const int newContentX = m_contentX - x;
545 if (isRightToLeft()) {
546 m_animation->setFrameRange(m_contentX, newContentX);
547 } else {
548 m_animation->setFrameRange(-m_contentX, -newContentX);
549 }
550 if (m_animation->state() != QTimeLine::Running) {
551 m_animation->start();
552 }
553 }
554 }
555
556 void DolphinColumnView::requestActivation(DolphinColumnWidget* column)
557 {
558 m_controller->setItemView(column);
559 if (column->isActive()) {
560 assureVisibleActiveColumn();
561 } else {
562 int index = 0;
563 foreach (DolphinColumnWidget* currColumn, m_columns) {
564 if (currColumn == column) {
565 setActiveColumnIndex(index);
566 return;
567 }
568 ++index;
569 }
570 }
571 }
572
573 void DolphinColumnView::removeAllColumns()
574 {
575 QList<DolphinColumnWidget*>::iterator start = m_columns.begin() + 1;
576 QList<DolphinColumnWidget*>::iterator end = m_columns.end();
577 for (QList<DolphinColumnWidget*>::iterator it = start; it != end; ++it) {
578 (*it)->deleteLater();
579 }
580 m_columns.erase(start, end);
581 m_index = 0;
582 m_columns[0]->setActive(true);
583 assureVisibleActiveColumn();
584 }
585
586 #include "dolphincolumnview.moc"