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