]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Now that we talk about the column view => smooth scrolling
[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 "dolphincontroller.h"
24 #include "dolphinsettings.h"
25
26 #include "dolphin_columnmodesettings.h"
27
28 #include <kcolorutils.h>
29 #include <kcolorscheme.h>
30 #include <kdirlister.h>
31
32 #include <QAbstractProxyModel>
33 #include <QApplication>
34 #include <QPoint>
35 #include <QScrollBar>
36 #include <QTimeLine>
37
38 /**
39 * Represents one column inside the DolphinColumnView and has been
40 * extended to respect view options and hovering information.
41 */
42 class ColumnWidget : public QListView
43 {
44 public:
45 ColumnWidget(QWidget* parent,
46 DolphinColumnView* columnView,
47 const KUrl& url);
48 virtual ~ColumnWidget();
49
50 /** Sets the size of the icons. */
51 void setDecorationSize(const QSize& size);
52
53 /**
54 * An active column is defined as column, which shows the same URL
55 * as indicated by the URL navigator. The active column is usually
56 * drawn in a lighter color. All operations are applied to this column.
57 */
58 void setActive(bool active);
59 bool isActive() const;
60
61 /**
62 * Sets the directory URL of the child column that is shown next to
63 * this column. This property is only used for a visual indication
64 * of the shown directory, it does not trigger a loading of the model.
65 */
66 void setChildUrl(const KUrl& url);
67 const KUrl& childUrl() const;
68
69 /** Sets the directory URL that is shown inside the column widget. */
70 void setUrl(const KUrl& url);
71
72 /** Returns the directory URL that is shown inside the column widget. */
73 inline const KUrl& url() const;
74
75 protected:
76 virtual QStyleOptionViewItem viewOptions() const;
77 virtual void dragEnterEvent(QDragEnterEvent* event);
78 virtual void dragLeaveEvent(QDragLeaveEvent* event);
79 virtual void dragMoveEvent(QDragMoveEvent* event);
80 virtual void dropEvent(QDropEvent* event);
81 virtual void paintEvent(QPaintEvent* event);
82 virtual void mousePressEvent(QMouseEvent* event);
83 virtual void keyPressEvent(QKeyEvent* event);
84 virtual void contextMenuEvent(QContextMenuEvent* event);
85 virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
86
87 private:
88 /** Used by ColumnWidget::setActive(). */
89 void activate();
90
91 /** Used by ColumnWidget::setActive(). */
92 void deactivate();
93
94 private:
95 bool m_active;
96 DolphinColumnView* m_view;
97 KUrl m_url; // URL of the directory that is shown
98 KUrl m_childUrl; // URL of the next column that is shown
99 QStyleOptionViewItem m_viewOptions;
100
101 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
102 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
103 };
104
105 ColumnWidget::ColumnWidget(QWidget* parent,
106 DolphinColumnView* columnView,
107 const KUrl& url) :
108 QListView(parent),
109 m_active(true),
110 m_view(columnView),
111 m_url(url),
112 m_childUrl(),
113 m_dragging(false),
114 m_dropRect()
115 {
116 setMouseTracking(true);
117 viewport()->setAttribute(Qt::WA_Hover);
118 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
119 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
120 setSelectionBehavior(SelectItems);
121 setSelectionMode(QAbstractItemView::ExtendedSelection);
122 setDragDropMode(QAbstractItemView::DragDrop);
123 setDropIndicatorShown(false);
124 setFocusPolicy(Qt::NoFocus);
125
126 // apply the column mode settings to the widget
127 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
128 Q_ASSERT(settings != 0);
129
130 m_viewOptions = QListView::viewOptions();
131
132 QFont font(settings->fontFamily(), settings->fontSize());
133 font.setItalic(settings->italicFont());
134 font.setBold(settings->boldFont());
135 m_viewOptions.font = font;
136
137 const int iconSize = settings->iconSize();
138 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
139
140 KFileItemDelegate* delegate = new KFileItemDelegate(this);
141 setItemDelegate(delegate);
142
143 activate();
144
145 connect(this, SIGNAL(entered(const QModelIndex&)),
146 m_view->m_controller, SLOT(emitItemEntered(const QModelIndex&)));
147 connect(this, SIGNAL(viewportEntered()),
148 m_view->m_controller, SLOT(emitViewportEntered()));
149 }
150
151 ColumnWidget::~ColumnWidget()
152 {
153 }
154
155 void ColumnWidget::setDecorationSize(const QSize& size)
156 {
157 m_viewOptions.decorationSize = size;
158 doItemsLayout();
159 }
160
161 void ColumnWidget::setActive(bool active)
162 {
163 if (m_active == active) {
164 return;
165 }
166
167 m_active = active;
168
169 if (active) {
170 activate();
171 } else {
172 deactivate();
173 }
174 }
175
176 inline bool ColumnWidget::isActive() const
177 {
178 return m_active;
179 }
180
181 inline void ColumnWidget::setChildUrl(const KUrl& url)
182 {
183 m_childUrl = url;
184 }
185
186 inline const KUrl& ColumnWidget::childUrl() const
187 {
188 return m_childUrl;
189 }
190
191 inline void ColumnWidget::setUrl(const KUrl& url)
192 {
193 m_url = url;
194 }
195
196 const KUrl& ColumnWidget::url() const
197 {
198 return m_url;
199 }
200
201 QStyleOptionViewItem ColumnWidget::viewOptions() const
202 {
203 return m_viewOptions;
204 }
205
206 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
207 {
208 if (event->mimeData()->hasUrls()) {
209 event->acceptProposedAction();
210 }
211
212 m_dragging = true;
213 }
214
215 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
216 {
217 QListView::dragLeaveEvent(event);
218
219 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
220 m_dragging = false;
221 setDirtyRegion(m_dropRect);
222 }
223
224 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
225 {
226 QListView::dragMoveEvent(event);
227
228 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
229 const QModelIndex index = indexAt(event->pos());
230 setDirtyRegion(m_dropRect);
231 m_dropRect = visualRect(index);
232 setDirtyRegion(m_dropRect);
233 }
234
235 void ColumnWidget::dropEvent(QDropEvent* event)
236 {
237 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
238 if (!urls.isEmpty()) {
239 event->acceptProposedAction();
240 m_view->m_controller->indicateDroppedUrls(urls,
241 url(),
242 indexAt(event->pos()),
243 event->source());
244 }
245 QListView::dropEvent(event);
246 m_dragging = false;
247 }
248
249 void ColumnWidget::paintEvent(QPaintEvent* event)
250 {
251 if (!m_childUrl.isEmpty()) {
252 // indicate the shown URL of the next column by highlighting the shown folder item
253 const QModelIndex dirIndex = m_view->m_dolphinModel->indexForUrl(m_childUrl);
254 const QModelIndex proxyIndex = m_view->m_proxyModel->mapFromSource(dirIndex);
255 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
256 const QRect itemRect = visualRect(proxyIndex);
257 QPainter painter(viewport());
258 painter.save();
259
260 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
261 color.setAlpha(32);
262 painter.setPen(Qt::NoPen);
263 painter.setBrush(color);
264 painter.drawRect(itemRect);
265
266 painter.restore();
267 }
268 }
269
270 QListView::paintEvent(event);
271
272 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
273 if (m_dragging) {
274 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
275 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
276 }
277 }
278
279 void ColumnWidget::mousePressEvent(QMouseEvent* event)
280 {
281 if (!m_active) {
282 m_view->requestActivation(this);
283 }
284
285 QListView::mousePressEvent(event);
286 }
287
288 void ColumnWidget::keyPressEvent(QKeyEvent* event)
289 {
290 QListView::keyPressEvent(event);
291
292 const QItemSelectionModel* selModel = selectionModel();
293 const QModelIndex currentIndex = selModel->currentIndex();
294 const bool triggerItem = currentIndex.isValid()
295 && (event->key() == Qt::Key_Return)
296 && (selModel->selectedIndexes().count() <= 1);
297 if (triggerItem) {
298 m_view->m_controller->triggerItem(currentIndex);
299 }
300 }
301
302 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
303 {
304 if (!m_active) {
305 m_view->requestActivation(this);
306 }
307
308 QListView::contextMenuEvent(event);
309
310 const QModelIndex index = indexAt(event->pos());
311 if (index.isValid() || m_active) {
312 // Only open a context menu above an item or if the mouse is above
313 // the active column.
314 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
315 m_view->m_controller->triggerContextMenuRequest(pos);
316 }
317 }
318
319 void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
320 {
321 QListView::selectionChanged(selected, deselected);
322
323 QItemSelectionModel* selModel = m_view->selectionModel();
324 selModel->select(selected, QItemSelectionModel::Select);
325 selModel->select(deselected, QItemSelectionModel::Deselect);
326 }
327
328 void ColumnWidget::activate()
329 {
330 if (m_view->hasFocus()) {
331 setFocus(Qt::OtherFocusReason);
332 }
333 m_view->setFocusProxy(this);
334
335 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
336 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
337 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
338 if (KGlobalSettings::singleClick()) {
339 connect(this, SIGNAL(clicked(const QModelIndex&)),
340 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
341 } else {
342 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
343 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
344 }
345
346 const QColor bgColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
347 QPalette palette = viewport()->palette();
348 palette.setColor(viewport()->backgroundRole(), bgColor);
349 viewport()->setPalette(palette);
350
351 if (!m_childUrl.isEmpty()) {
352 // assure that the current index is set on the index that represents
353 // the child URL
354 const QModelIndex dirIndex = m_view->m_dolphinModel->indexForUrl(m_childUrl);
355 const QModelIndex proxyIndex = m_view->m_proxyModel->mapFromSource(dirIndex);
356 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current);
357 }
358
359 update();
360 }
361
362 void ColumnWidget::deactivate()
363 {
364 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
365 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
366 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
367 if (KGlobalSettings::singleClick()) {
368 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
369 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
370 } else {
371 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
372 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
373 }
374
375 const QPalette palette = m_view->viewport()->palette();
376 viewport()->setPalette(palette);
377
378 selectionModel()->clear();
379 update();
380 }
381
382 // ---
383
384 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
385 QAbstractItemView(parent),
386 m_controller(controller),
387 m_restoreActiveColumnFocus(false),
388 m_index(-1),
389 m_contentX(0),
390 m_columns(),
391 m_animation(0),
392 m_dolphinModel(0),
393 m_proxyModel(0)
394 {
395 Q_ASSERT(controller != 0);
396
397 setAcceptDrops(true);
398 setDragDropMode(QAbstractItemView::DragDrop);
399 setDropIndicatorShown(false);
400 setSelectionMode(ExtendedSelection);
401
402 connect(this, SIGNAL(entered(const QModelIndex&)),
403 controller, SLOT(emitItemEntered(const QModelIndex&)));
404 connect(this, SIGNAL(viewportEntered()),
405 controller, SLOT(emitViewportEntered()));
406 connect(controller, SIGNAL(zoomIn()),
407 this, SLOT(zoomIn()));
408 connect(controller, SIGNAL(zoomOut()),
409 this, SLOT(zoomOut()));
410 connect(controller, SIGNAL(urlChanged(const KUrl&)),
411 this, SLOT(showColumn(const KUrl&)));
412
413 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
414 this, SLOT(moveContentHorizontally(int)));
415
416 ColumnWidget* column = new ColumnWidget(viewport(), this, m_controller->url());
417 column->setVerticalScrollMode(ColumnWidget::ScrollPerPixel);
418 column->setHorizontalScrollMode(ColumnWidget::ScrollPerPixel);
419 m_columns.append(column);
420 setActiveColumnIndex(0);
421
422 updateDecorationSize();
423
424 m_animation = new QTimeLine(500, this);
425 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
426
427 // dim the background of the viewport
428 QColor bgColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
429 const QColor fgColor = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
430 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
431
432 QPalette palette = viewport()->palette();
433 palette.setColor(viewport()->backgroundRole(), bgColor);
434 viewport()->setPalette(palette);
435 }
436
437 DolphinColumnView::~DolphinColumnView()
438 {
439 }
440
441 QModelIndex DolphinColumnView::indexAt(const QPoint& point) const
442 {
443 foreach (ColumnWidget* column, m_columns) {
444 const QPoint topLeft = column->frameGeometry().topLeft();
445 const QPoint adjustedPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
446 const QModelIndex index = column->indexAt(adjustedPoint);
447 if (index.isValid()) {
448 return index;
449 }
450 }
451
452 return QModelIndex();
453 }
454
455 void DolphinColumnView::scrollTo(const QModelIndex& index, ScrollHint hint)
456 {
457 activeColumn()->scrollTo(index, hint);
458 }
459
460 QRect DolphinColumnView::visualRect(const QModelIndex& index) const
461 {
462 return activeColumn()->visualRect(index);
463 }
464
465 void DolphinColumnView::setModel(QAbstractItemModel* model)
466 {
467 if (m_dolphinModel != 0) {
468 m_dolphinModel->disconnect(this);
469 }
470
471 m_proxyModel = static_cast<QAbstractProxyModel*>(model);
472 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
473 connect(m_dolphinModel, SIGNAL(expand(const QModelIndex&)),
474 this, SLOT(triggerReloadColumns(const QModelIndex&)));
475
476 activeColumn()->setModel(model);
477 QAbstractItemView::setModel(model);
478 }
479
480 void DolphinColumnView::reload()
481 {
482 // Due to the reloading of the model all columns will be reset to show
483 // the same content as the first column. As this is not wanted, all columns
484 // except of the first column are temporary hidden until the root index can
485 // be updated again.
486 m_restoreActiveColumnFocus = false;
487 QList<ColumnWidget*>::iterator start = m_columns.begin() + 1;
488 QList<ColumnWidget*>::iterator end = m_columns.end();
489 for (QList<ColumnWidget*>::iterator it = start; it != end; ++it) {
490 ColumnWidget* column = (*it);
491 if (column->isActive() && column->hasFocus()) {
492 // because of hiding the column, it will lose the focus
493 // -> remember that the focus should be restored after reloading
494 m_restoreActiveColumnFocus = true;
495 }
496 column->hide();
497 }
498
499 // all columns are hidden, now reload the directory lister
500 KDirLister* dirLister = m_dolphinModel->dirLister();
501 connect(dirLister, SIGNAL(completed()),
502 this, SLOT(expandToActiveUrl()));
503 const KUrl rootUrl = m_columns[0]->url();
504 dirLister->openUrl(rootUrl, false, true);
505 }
506
507 void DolphinColumnView::showColumn(const KUrl& url)
508 {
509 const KUrl& rootUrl = m_columns[0]->url();
510 if (!rootUrl.isParentOf(url)) {
511 // the URL is no child URL of the column view, hence clear all columns
512 // and reset the root column
513 QList<ColumnWidget*>::iterator start = m_columns.begin() + 1;
514 QList<ColumnWidget*>::iterator end = m_columns.end();
515 for (QList<ColumnWidget*>::iterator it = start; it != end; ++it) {
516 (*it)->deleteLater();
517 }
518 m_columns.erase(start, end);
519 m_index = 0;
520 m_columns[0]->setActive(true);
521 m_columns[0]->setUrl(url);
522 assureVisibleActiveColumn();
523 return;
524 }
525
526 KDirLister* dirLister = m_dolphinModel->dirLister();
527 const KUrl dirListerUrl = dirLister->url();
528 if (dirListerUrl != rootUrl) {
529 // It is possible that root URL of the directory lister is adjusted
530 // after creating the column widget (e. g. when restoring the history
531 // having a different root URL than the controller indicates).
532 m_columns[0]->setUrl(dirListerUrl);
533 }
534
535 int columnIndex = 0;
536 foreach (ColumnWidget* column, m_columns) {
537 if (column->url() == url) {
538 // the column represents already the requested URL, hence activate it
539 requestActivation(column);
540 return;
541 } else if (!column->url().isParentOf(url)) {
542 // the column is no parent of the requested URL, hence
543 // just delete all remaining columns
544 if (columnIndex > 0) {
545 QList<ColumnWidget*>::iterator start = m_columns.begin() + columnIndex;
546 QList<ColumnWidget*>::iterator end = m_columns.end();
547 for (QList<ColumnWidget*>::iterator it = start; it != end; ++it) {
548 (*it)->deleteLater();
549 }
550 m_columns.erase(start, end);
551
552 const int maxIndex = m_columns.count() - 1;
553 Q_ASSERT(maxIndex >= 0);
554 if (m_index > maxIndex) {
555 m_index = maxIndex;
556 }
557 break;
558 }
559 }
560 ++columnIndex;
561 }
562
563 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
564 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
565 // "c" will be created.
566 const int lastIndex = m_columns.count() - 1;
567 Q_ASSERT(lastIndex >= 0);
568
569 const KUrl& activeUrl = m_columns[lastIndex]->url();
570 Q_ASSERT(activeUrl.isParentOf(url));
571 Q_ASSERT(activeUrl != url);
572
573 QString path = activeUrl.url(KUrl::AddTrailingSlash);
574 const QString targetPath = url.url(KUrl::AddTrailingSlash);
575
576 columnIndex = lastIndex;
577 int slashIndex = path.count('/');
578 bool hasSubPath = (slashIndex >= 0);
579 while (hasSubPath) {
580 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
581 if (subPath.isEmpty()) {
582 hasSubPath = false;
583 } else {
584 path += subPath + '/';
585 ++slashIndex;
586
587 const KUrl childUrl = KUrl(path);
588 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(KUrl(path));
589 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
590
591 m_columns[columnIndex]->setChildUrl(childUrl);
592 columnIndex++;
593
594 ColumnWidget* column = new ColumnWidget(viewport(), this, childUrl);
595 column->setVerticalScrollMode(ColumnWidget::ScrollPerPixel);
596 column->setHorizontalScrollMode(ColumnWidget::ScrollPerPixel);
597 column->setModel(model());
598 column->setRootIndex(proxyIndex);
599 column->setActive(false);
600
601 m_columns.append(column);
602
603 // Before invoking layoutColumns() the column must be set visible temporary.
604 // To prevent a flickering the initial geometry is set to a hidden position.
605 column->setGeometry(QRect(-1, -1, 1, 1));
606 column->show();
607 layoutColumns();
608 updateScrollBar();
609
610 // the layout is finished, now let the column be invisible until it
611 // gets a valid root index due to expandToActiveUrl()
612 column->hide();
613 }
614 }
615
616 // set the last column as active column without modifying the controller
617 // and hence the history
618 activeColumn()->setActive(false);
619 m_index = columnIndex;
620 activeColumn()->setActive(true);
621
622 expandToActiveUrl();
623 }
624
625 bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const
626 {
627 Q_UNUSED(index);
628 return false;//activeColumn()->isIndexHidden(index);
629 }
630
631 QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
632 {
633 // Parts of this code have been taken from QColumnView::moveCursor().
634 // Copyright (C) 1992-2007 Trolltech ASA.
635
636 Q_UNUSED(modifiers);
637 if (model() == 0) {
638 return QModelIndex();
639 }
640
641 const QModelIndex current = currentIndex();
642 if (isRightToLeft()) {
643 if (cursorAction == MoveLeft) {
644 cursorAction = MoveRight;
645 } else if (cursorAction == MoveRight) {
646 cursorAction = MoveLeft;
647 }
648 }
649
650 switch (cursorAction) {
651 case MoveLeft:
652 if (m_index > 0) {
653 setActiveColumnIndex(m_index - 1);
654 }
655 break;
656
657 case MoveRight:
658 if (m_index < m_columns.count() - 1) {
659 setActiveColumnIndex(m_index + 1);
660 }
661 break;
662
663 default:
664 break;
665 }
666
667 return QModelIndex();
668 }
669
670 void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags)
671 {
672 Q_UNUSED(rect);
673 Q_UNUSED(flags);
674 //activeColumn()->setSelection(rect, flags);
675 }
676
677 QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const
678 {
679 Q_UNUSED(selection);
680 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
681 }
682
683 int DolphinColumnView::horizontalOffset() const
684 {
685 return -m_contentX;
686 }
687
688 int DolphinColumnView::verticalOffset() const
689 {
690 return 0;
691 }
692
693 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
694 {
695 m_controller->triggerActivation();
696 QAbstractItemView::mousePressEvent(event);
697 }
698
699 void DolphinColumnView::resizeEvent(QResizeEvent* event)
700 {
701 QAbstractItemView::resizeEvent(event);
702 layoutColumns();
703 updateScrollBar();
704 }
705
706 void DolphinColumnView::zoomIn()
707 {
708 if (isZoomInPossible()) {
709 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
710 // TODO: get rid of K3Icon sizes
711 switch (settings->iconSize()) {
712 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
713 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
714 default: Q_ASSERT(false); break;
715 }
716 updateDecorationSize();
717 }
718 }
719
720 void DolphinColumnView::zoomOut()
721 {
722 if (isZoomOutPossible()) {
723 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
724 // TODO: get rid of K3Icon sizes
725 switch (settings->iconSize()) {
726 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
727 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
728 default: Q_ASSERT(false); break;
729 }
730 updateDecorationSize();
731 }
732 }
733
734 void DolphinColumnView::moveContentHorizontally(int x)
735 {
736 m_contentX = -x;
737 layoutColumns();
738 }
739
740 void DolphinColumnView::updateDecorationSize()
741 {
742 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
743 const int iconSize = settings->iconSize();
744
745 foreach (QObject* object, viewport()->children()) {
746 if (object->inherits("QListView")) {
747 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
748 widget->setDecorationSize(QSize(iconSize, iconSize));
749 }
750 }
751
752 m_controller->setZoomInPossible(isZoomInPossible());
753 m_controller->setZoomOutPossible(isZoomOutPossible());
754
755 doItemsLayout();
756 }
757
758 void DolphinColumnView::expandToActiveUrl()
759 {
760 const int lastIndex = m_columns.count() - 1;
761 Q_ASSERT(lastIndex >= 0);
762 const KUrl& activeUrl = m_columns[lastIndex]->url();
763 const KUrl rootUrl = m_dolphinModel->dirLister()->url();
764 const bool expand = rootUrl.isParentOf(activeUrl)
765 && !rootUrl.equals(activeUrl, KUrl::CompareWithoutTrailingSlash);
766 if (expand) {
767 m_dolphinModel->expandToUrl(activeUrl);
768 reloadColumns();
769 }
770 }
771
772 void DolphinColumnView::triggerReloadColumns(const QModelIndex& index)
773 {
774 Q_UNUSED(index);
775 // the reloading of the columns may not be done in the context of this slot
776 QMetaObject::invokeMethod(this, "reloadColumns", Qt::QueuedConnection);
777 }
778
779 void DolphinColumnView::reloadColumns()
780 {
781 const int end = m_columns.count() - 2; // next to last column
782 for (int i = 0; i <= end; ++i) {
783 ColumnWidget* nextColumn = m_columns[i + 1];
784
785 KDirLister* dirLister = m_dolphinModel->dirLister();
786 dirLister->updateDirectory(nextColumn->url());
787
788 const QModelIndex rootIndex = nextColumn->rootIndex();
789 if (rootIndex.isValid()) {
790 nextColumn->show();
791 } else {
792 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_columns[i]->childUrl());
793 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
794 if (proxyIndex.isValid()) {
795 nextColumn->setRootIndex(proxyIndex);
796 nextColumn->show();
797 if (nextColumn->isActive() && m_restoreActiveColumnFocus) {
798 nextColumn->setFocus();
799 m_restoreActiveColumnFocus = false;
800 }
801 }
802 }
803 }
804 assureVisibleActiveColumn();
805 }
806
807 bool DolphinColumnView::isZoomInPossible() const
808 {
809 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
810 return settings->iconSize() < K3Icon::SizeLarge;
811 }
812
813 bool DolphinColumnView::isZoomOutPossible() const
814 {
815 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
816 return settings->iconSize() > K3Icon::SizeSmall;
817 }
818
819 void DolphinColumnView::setActiveColumnIndex(int index)
820 {
821 if (m_index == index) {
822 return;
823 }
824
825 const bool hasActiveColumn = (m_index >= 0);
826 if (hasActiveColumn) {
827 m_columns[m_index]->setActive(false);
828 }
829
830 m_index = index;
831 m_columns[m_index]->setActive(true);
832
833 m_controller->setUrl(m_columns[m_index]->url());
834 }
835
836 void DolphinColumnView::layoutColumns()
837 {
838 int x = m_contentX;
839 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
840 const int columnWidth = settings->columnWidth();
841 foreach (ColumnWidget* column, m_columns) {
842 column->setGeometry(QRect(x, 0, columnWidth, viewport()->height()));
843 x += columnWidth;
844 }
845 }
846
847 void DolphinColumnView::updateScrollBar()
848 {
849 int contentWidth = 0;
850 foreach (ColumnWidget* column, m_columns) {
851 contentWidth += column->width();
852 }
853
854 horizontalScrollBar()->setPageStep(contentWidth);
855 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
856 }
857
858 void DolphinColumnView::assureVisibleActiveColumn()
859 {
860 const int viewportWidth = viewport()->width();
861 const int x = activeColumn()->x();
862 const int width = activeColumn()->width();
863 if (x + width > viewportWidth) {
864 int newContentX = m_contentX - x - width + viewportWidth;
865 if (newContentX > 0) {
866 newContentX = 0;
867 }
868 m_animation->setFrameRange(-m_contentX, -newContentX);
869 m_animation->start();
870 } else if (x < 0) {
871 const int newContentX = m_contentX - x;
872 m_animation->setFrameRange(-m_contentX, -newContentX);
873 m_animation->start();
874 }
875 }
876
877 void DolphinColumnView::requestActivation(ColumnWidget* column)
878 {
879 if (column->isActive()) {
880 assureVisibleActiveColumn();
881 } else {
882 int index = 0;
883 foreach (ColumnWidget* currColumn, m_columns) {
884 if (currColumn == column) {
885 setActiveColumnIndex(index);
886 assureVisibleActiveColumn();
887 return;
888 }
889 ++index;
890 }
891 }
892 }
893
894 #include "dolphincolumnview.moc"