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