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