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