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