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