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