]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
fix filtering issues for views that show a directory hierarchy like a tree view or...
[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::showColumn(const KUrl& url)
507 {
508 const KUrl& rootUrl = m_columns[0]->url();
509 if (!rootUrl.isParentOf(url)) {
510 // the URL is no child URL of the column view, hence clear all columns
511 // and reset the root column
512 QList<ColumnWidget*>::iterator start = m_columns.begin() + 1;
513 QList<ColumnWidget*>::iterator end = m_columns.end();
514 for (QList<ColumnWidget*>::iterator it = start; it != end; ++it) {
515 (*it)->deleteLater();
516 }
517 m_columns.erase(start, end);
518 m_index = 0;
519 m_columns[0]->setActive(true);
520 m_columns[0]->setUrl(url);
521 assureVisibleActiveColumn();
522 return;
523 }
524
525 KDirLister* dirLister = m_dolphinModel->dirLister();
526 const KUrl dirListerUrl = dirLister->url();
527 if (dirListerUrl != rootUrl) {
528 // It is possible that root URL of the directory lister is adjusted
529 // after creating the column widget (e. g. when restoring the history
530 // having a different root URL than the controller indicates).
531 m_columns[0]->setUrl(dirListerUrl);
532 }
533
534 int columnIndex = 0;
535 foreach (ColumnWidget* column, m_columns) {
536 if (column->url() == url) {
537 // the column represents already the requested URL, hence activate it
538 requestActivation(column);
539 return;
540 } else if (!column->url().isParentOf(url)) {
541 // the column is no parent of the requested URL, hence
542 // just delete all remaining columns
543 if (columnIndex > 0) {
544 QList<ColumnWidget*>::iterator start = m_columns.begin() + columnIndex;
545 QList<ColumnWidget*>::iterator end = m_columns.end();
546 for (QList<ColumnWidget*>::iterator it = start; it != end; ++it) {
547 (*it)->deleteLater();
548 }
549 m_columns.erase(start, end);
550
551 const int maxIndex = m_columns.count() - 1;
552 Q_ASSERT(maxIndex >= 0);
553 if (m_index > maxIndex) {
554 m_index = maxIndex;
555 }
556 break;
557 }
558 }
559 ++columnIndex;
560 }
561
562 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
563 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
564 // "c" will be created.
565 const int lastIndex = m_columns.count() - 1;
566 Q_ASSERT(lastIndex >= 0);
567
568 const KUrl& activeUrl = m_columns[lastIndex]->url();
569 Q_ASSERT(activeUrl.isParentOf(url));
570 Q_ASSERT(activeUrl != url);
571
572 QString path = activeUrl.url(KUrl::AddTrailingSlash);
573 const QString targetPath = url.url(KUrl::AddTrailingSlash);
574
575 columnIndex = lastIndex;
576 int slashIndex = path.count('/');
577 bool hasSubPath = (slashIndex >= 0);
578 while (hasSubPath) {
579 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
580 if (subPath.isEmpty()) {
581 hasSubPath = false;
582 } else {
583 path += subPath + '/';
584 ++slashIndex;
585
586 const KUrl childUrl = KUrl(path);
587 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(KUrl(path));
588 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
589
590 m_columns[columnIndex]->setChildUrl(childUrl);
591 columnIndex++;
592
593 ColumnWidget* column = new ColumnWidget(viewport(), this, childUrl);
594 column->setVerticalScrollMode(ColumnWidget::ScrollPerPixel);
595 column->setHorizontalScrollMode(ColumnWidget::ScrollPerPixel);
596 column->setModel(model());
597 column->setRootIndex(proxyIndex);
598 column->setActive(false);
599
600 m_columns.append(column);
601
602 // Before invoking layoutColumns() the column must be set visible temporary.
603 // To prevent a flickering the initial geometry is set to a hidden position.
604 column->setGeometry(QRect(-1, -1, 1, 1));
605 column->show();
606 layoutColumns();
607 updateScrollBar();
608
609 // the layout is finished, now let the column be invisible until it
610 // gets a valid root index due to expandToActiveUrl()
611 column->hide();
612 }
613 }
614
615 // set the last column as active column without modifying the controller
616 // and hence the history
617 activeColumn()->setActive(false);
618 m_index = columnIndex;
619 activeColumn()->setActive(true);
620
621 expandToActiveUrl();
622 }
623
624 bool DolphinColumnView::isIndexHidden(const QModelIndex& index) const
625 {
626 Q_UNUSED(index);
627 return false;//activeColumn()->isIndexHidden(index);
628 }
629
630 QModelIndex DolphinColumnView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
631 {
632 // Parts of this code have been taken from QColumnView::moveCursor().
633 // Copyright (C) 1992-2007 Trolltech ASA.
634
635 Q_UNUSED(modifiers);
636 if (model() == 0) {
637 return QModelIndex();
638 }
639
640 const QModelIndex current = currentIndex();
641 if (isRightToLeft()) {
642 if (cursorAction == MoveLeft) {
643 cursorAction = MoveRight;
644 } else if (cursorAction == MoveRight) {
645 cursorAction = MoveLeft;
646 }
647 }
648
649 switch (cursorAction) {
650 case MoveLeft:
651 if (m_index > 0) {
652 setActiveColumnIndex(m_index - 1);
653 }
654 break;
655
656 case MoveRight:
657 if (m_index < m_columns.count() - 1) {
658 setActiveColumnIndex(m_index + 1);
659 }
660 break;
661
662 default:
663 break;
664 }
665
666 return QModelIndex();
667 }
668
669 void DolphinColumnView::setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags)
670 {
671 Q_UNUSED(rect);
672 Q_UNUSED(flags);
673 //activeColumn()->setSelection(rect, flags);
674 }
675
676 QRegion DolphinColumnView::visualRegionForSelection(const QItemSelection& selection) const
677 {
678 Q_UNUSED(selection);
679 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
680 }
681
682 int DolphinColumnView::horizontalOffset() const
683 {
684 return -m_contentX;
685 }
686
687 int DolphinColumnView::verticalOffset() const
688 {
689 return 0;
690 }
691
692 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
693 {
694 m_controller->triggerActivation();
695 QAbstractItemView::mousePressEvent(event);
696 }
697
698 void DolphinColumnView::resizeEvent(QResizeEvent* event)
699 {
700 QAbstractItemView::resizeEvent(event);
701 layoutColumns();
702 updateScrollBar();
703 }
704
705 void DolphinColumnView::zoomIn()
706 {
707 if (isZoomInPossible()) {
708 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
709 // TODO: get rid of K3Icon sizes
710 switch (settings->iconSize()) {
711 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
712 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
713 default: Q_ASSERT(false); break;
714 }
715 updateDecorationSize();
716 }
717 }
718
719 void DolphinColumnView::zoomOut()
720 {
721 if (isZoomOutPossible()) {
722 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
723 // TODO: get rid of K3Icon sizes
724 switch (settings->iconSize()) {
725 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
726 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
727 default: Q_ASSERT(false); break;
728 }
729 updateDecorationSize();
730 }
731 }
732
733 void DolphinColumnView::moveContentHorizontally(int x)
734 {
735 m_contentX = -x;
736 layoutColumns();
737 }
738
739 void DolphinColumnView::updateDecorationSize()
740 {
741 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
742 const int iconSize = settings->iconSize();
743
744 foreach (QObject* object, viewport()->children()) {
745 if (object->inherits("QListView")) {
746 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
747 widget->setDecorationSize(QSize(iconSize, iconSize));
748 }
749 }
750
751 m_controller->setZoomInPossible(isZoomInPossible());
752 m_controller->setZoomOutPossible(isZoomOutPossible());
753
754 doItemsLayout();
755 }
756
757 void DolphinColumnView::expandToActiveUrl()
758 {
759 const int lastIndex = m_columns.count() - 1;
760 Q_ASSERT(lastIndex >= 0);
761 const KUrl& activeUrl = m_columns[lastIndex]->url();
762 const KUrl rootUrl = m_dolphinModel->dirLister()->url();
763 const bool expand = rootUrl.isParentOf(activeUrl)
764 && !rootUrl.equals(activeUrl, KUrl::CompareWithoutTrailingSlash);
765 if (expand) {
766 m_dolphinModel->expandToUrl(activeUrl);
767 reloadColumns();
768 }
769 }
770
771 void DolphinColumnView::triggerReloadColumns(const QModelIndex& index)
772 {
773 Q_UNUSED(index);
774 // the reloading of the columns may not be done in the context of this slot
775 QMetaObject::invokeMethod(this, "reloadColumns", Qt::QueuedConnection);
776 }
777
778 void DolphinColumnView::reloadColumns()
779 {
780 const int end = m_columns.count() - 2; // next to last column
781 for (int i = 0; i <= end; ++i) {
782 ColumnWidget* nextColumn = m_columns[i + 1];
783
784 KDirLister* dirLister = m_dolphinModel->dirLister();
785 dirLister->updateDirectory(nextColumn->url());
786
787 const QModelIndex rootIndex = nextColumn->rootIndex();
788 if (rootIndex.isValid()) {
789 nextColumn->show();
790 } else {
791 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_columns[i]->childUrl());
792 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
793 if (proxyIndex.isValid()) {
794 nextColumn->setRootIndex(proxyIndex);
795 nextColumn->show();
796 if (nextColumn->isActive() && m_restoreActiveColumnFocus) {
797 nextColumn->setFocus();
798 m_restoreActiveColumnFocus = false;
799 }
800 }
801 }
802 }
803 assureVisibleActiveColumn();
804 }
805
806 bool DolphinColumnView::isZoomInPossible() const
807 {
808 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
809 return settings->iconSize() < K3Icon::SizeLarge;
810 }
811
812 bool DolphinColumnView::isZoomOutPossible() const
813 {
814 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
815 return settings->iconSize() > K3Icon::SizeSmall;
816 }
817
818 void DolphinColumnView::setActiveColumnIndex(int index)
819 {
820 if (m_index == index) {
821 return;
822 }
823
824 const bool hasActiveColumn = (m_index >= 0);
825 if (hasActiveColumn) {
826 m_columns[m_index]->setActive(false);
827 }
828
829 m_index = index;
830 m_columns[m_index]->setActive(true);
831
832 m_controller->setUrl(m_columns[m_index]->url());
833 }
834
835 void DolphinColumnView::layoutColumns()
836 {
837 int x = m_contentX;
838 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
839 const int columnWidth = settings->columnWidth();
840 foreach (ColumnWidget* column, m_columns) {
841 column->setGeometry(QRect(x, 0, columnWidth, viewport()->height()));
842 x += columnWidth;
843 }
844 }
845
846 void DolphinColumnView::updateScrollBar()
847 {
848 int contentWidth = 0;
849 foreach (ColumnWidget* column, m_columns) {
850 contentWidth += column->width();
851 }
852
853 horizontalScrollBar()->setPageStep(contentWidth);
854 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
855 }
856
857 void DolphinColumnView::assureVisibleActiveColumn()
858 {
859 const int viewportWidth = viewport()->width();
860 const int x = activeColumn()->x();
861 const int width = activeColumn()->width();
862 if (x + width > viewportWidth) {
863 int newContentX = m_contentX - x - width + viewportWidth;
864 if (newContentX > 0) {
865 newContentX = 0;
866 }
867 m_animation->setFrameRange(-m_contentX, -newContentX);
868 m_animation->start();
869 } else if (x < 0) {
870 const int newContentX = m_contentX - x;
871 m_animation->setFrameRange(-m_contentX, -newContentX);
872 m_animation->start();
873 }
874 }
875
876 void DolphinColumnView::requestActivation(ColumnWidget* column)
877 {
878 if (column->isActive()) {
879 assureVisibleActiveColumn();
880 } else {
881 int index = 0;
882 foreach (ColumnWidget* currColumn, m_columns) {
883 if (currColumn == column) {
884 setActiveColumnIndex(index);
885 assureVisibleActiveColumn();
886 return;
887 }
888 ++index;
889 }
890 }
891 }
892
893 #include "dolphincolumnview.moc"