]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincolumnview.cpp
don't use the term "Bookmarks" anymore, use "Places" instead (includes devices etc.)
[dolphin.git] / src / dolphincolumnview.cpp
index 81337cb20c296bbecfba385e6b045aef1f09bc49..e4669442ccc231519181581a4d8d0bba1c8a0f3a 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at)                  *
+ *   Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at>                  *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
 
 #include <kcolorutils.h>
 #include <kcolorscheme.h>
-#include <kdirmodel.h>
 #include <kdirlister.h>
-#include <kfileitem.h>
+#include <kdirmodel.h>
 
 #include <QAbstractProxyModel>
+#include <QApplication>
 #include <QPoint>
 
+/*
+ * General implementation notes
+ * ----------------------------
+ *
+ * In Qt4.3 the QColumnView widget has a default behavior regarding the
+ * active column and the selection handling, which leads to some usability
+ * problems within Dolphin:
+ *
+ * - No matter which mouse button has been clicked: If the mouse is above
+ *   a folder, the folder content will be loaded in the next column. The problem
+ *   is that this column also will marked as 'active column' within QColumnView,
+ *   hence it is not possible to select more than one folder within a column.
+ *
+ * - The currently opened folder is not always marked in the left column when
+ *   doing drag & drop and selections inside other columns.
+ *
+ * - The currently active column is visually not recognizable.
+ *
+ * - It is not possible for derived classes to remove inactive columns.
+ *
+ * DolphinView tries to bypass those points, but this required some workarounds:
+ *
+ * - QColumnView internally maps the selection model from the ColumnView to the
+ *   active column. As the active column from the Dolphin perspective is different
+ *   as the active column from QColumnView, the selection model is adjusted on
+ *   each interaction by the methods QColumnWidget::obtainSelectionModel(),
+ *   QColumnWidget::releaseSelectionModel() and QColumnView::requestSelectionModel().
+ *   QColumnView offers no hook to adjust this behavior, so those methods have to
+ *   be invoked throughout the code...
+ *
+ * - Some copy/paste code from QColumnView is part of DolphinColumnView::createColumn(), but Qt 4.4
+ *   will offer a solution for this.
+ *
+ * - The mousePressEvent() has been customized to prevent that folders are loaded on each
+ *   mouse click.
+ *
+ * We'll try to give some input for Trolltech if the Dolphin solution is stable enough, so hopefully
+ * some workarounds can be removed when switching to Qt 4.4 or later.
+ */
+
 /**
  * Represents one column inside the DolphinColumnView and has been
  * extended to respect view options and hovering information.
@@ -54,9 +94,22 @@ public:
      * drawn in a lighter color. All operations are applied to this column.
      */
     void setActive(bool active);
+    inline bool isActive() const;
 
     inline const KUrl& url() const;
 
+    /**
+     * Obtains the selection model from the column view. This assures that
+     * selections of the column view will always applied to the active column.
+     */
+    void obtainSelectionModel();
+
+    /**
+     * Releases the selection model from the column view and replaces it by
+     * a custom selection model.
+     */
+    void releaseSelectionModel();
+
 protected:
     virtual QStyleOptionViewItem viewOptions() const;
     virtual void dragEnterEvent(QDragEnterEvent* event);
@@ -64,9 +117,14 @@ protected:
     virtual void dragMoveEvent(QDragMoveEvent* event);
     virtual void dropEvent(QDropEvent* event);
     virtual void mousePressEvent(QMouseEvent* event);
+    virtual void mouseMoveEvent(QMouseEvent* event);
+    virtual void mouseReleaseEvent(QMouseEvent* event);
     virtual void paintEvent(QPaintEvent* event);
     virtual void contextMenuEvent(QContextMenuEvent* event);
 
+protected slots:
+    virtual void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
+
 private:
     /** Used by ColumnWidget::setActive(). */
     void activate();
@@ -76,8 +134,10 @@ private:
 
 private:
     bool m_active;
-    KUrl m_url;
+    bool m_swallowMouseMoveEvents;
     DolphinColumnView* m_view;
+    KUrl m_url;
+    KUrl m_childUrl; // URL of the next column that is shown
     QStyleOptionViewItem m_viewOptions;
 
     bool m_dragging;   // TODO: remove this property when the issue #160611 is solved in Qt 4.4
@@ -89,15 +149,13 @@ ColumnWidget::ColumnWidget(QWidget* parent,
                            const KUrl& url) :
     QListView(parent),
     m_active(true),
-    m_url(url),
+    m_swallowMouseMoveEvents(false),
     m_view(columnView),
+    m_url(url),
+    m_childUrl(),
     m_dragging(false),
     m_dropRect()
 {
-    setAcceptDrops(true);
-    setDragDropMode(QAbstractItemView::DragDrop);
-    setDropIndicatorShown(false);
-
     setMouseTracking(true);
     viewport()->setAttribute(Qt::WA_Hover);
 
@@ -130,6 +188,12 @@ void ColumnWidget::setDecorationSize(const QSize& size)
 
 void ColumnWidget::setActive(bool active)
 {
+    if (active) {
+        obtainSelectionModel();
+    } else {
+        releaseSelectionModel();
+    }
+
     if (m_active == active) {
         return;
     }
@@ -143,11 +207,33 @@ void ColumnWidget::setActive(bool active)
     }
 }
 
+inline bool ColumnWidget::isActive() const
+{
+    return m_active;
+}
+
 const KUrl& ColumnWidget::url() const
 {
     return m_url;
 }
 
+void ColumnWidget::obtainSelectionModel()
+{
+    if (selectionModel() != m_view->selectionModel()) {
+        selectionModel()->deleteLater();
+        setSelectionModel(m_view->selectionModel());
+        clearSelection();
+    }
+}
+
+void ColumnWidget::releaseSelectionModel()
+{
+    if (selectionModel() == m_view->selectionModel()) {
+        QItemSelectionModel* replacementModel = new QItemSelectionModel(model());
+        setSelectionModel(replacementModel);
+    }
+}
+
 QStyleOptionViewItem ColumnWidget::viewOptions() const
 {
     return m_viewOptions;
@@ -175,7 +261,7 @@ void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
 {
     QListView::dragMoveEvent(event);
 
-   // TODO: remove this code when the issue #160611 is solved in Qt 4.4
+    // TODO: remove this code when the issue #160611 is solved in Qt 4.4
     const QModelIndex index = indexAt(event->pos());
     setDirtyRegion(m_dropRect);
     m_dropRect = visualRect(index);
@@ -188,8 +274,8 @@ void ColumnWidget::dropEvent(QDropEvent* event)
     if (!urls.isEmpty()) {
         event->acceptProposedAction();
         m_view->m_controller->indicateDroppedUrls(urls,
-                                                        indexAt(event->pos()),
-                                                        event->source());
+                                                  indexAt(event->pos()),
+                                                  event->source());
     }
     QListView::dropEvent(event);
     m_dragging = false;
@@ -197,17 +283,119 @@ void ColumnWidget::dropEvent(QDropEvent* event)
 
 void ColumnWidget::mousePressEvent(QMouseEvent* event)
 {
-    if (m_active || indexAt(event->pos()).isValid()) {
-        // Only accept the mouse press event in inactive views,
-        // if a click is done on an item. This assures that
-        // the current selection, which usually shows the
-        // the directory for next column, won't get deleted.
+    // On each mouse press event QColumnView triggers the loading of the
+    // current folder in the next column. This is not wanted for Dolphin when
+    // opening a context menu or when the CTRL modifier is pressed. Beside usability
+    // aspects the loading of the folder also implies losing the current selection,
+    // which makes it impossible to select folders from the current column. To bypass
+    // this behavior QListView::mousePressEvent() is not invoked in those cases, which
+    // is not a nice solution. Maybe another solution can be found in future versions
+    // of QColumnView.
+
+    m_view->requestSelectionModel(this);
+
+    bool swallowMousePressEvent = false;
+    const QModelIndex index = indexAt(event->pos());
+    if (index.isValid()) {
+        // a click on an item has been done
+        const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
+        const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+        const QModelIndex dirIndex = proxyModel->mapToSource(index);
+        KFileItem item = dirModel->itemForIndex(dirIndex);
+        if (!item.isNull()) {
+            QItemSelectionModel* selModel = selectionModel();
+
+            bool activate = true;
+            const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
+            if (modifier & Qt::ControlModifier) {
+                m_view->requestActivation(this);
+                if (!selModel->hasSelection()) {
+                    // Assure to set the current index, so that a selection by the SHIFT key
+                    // will work. TODO: If the index specifies a folder, the loading of the folder will
+                    // be triggered by QColumnView although this is not wanted by Dolphin.
+                    selModel->setCurrentIndex(index, QItemSelectionModel::Select);
+                }
+                selModel->select(index, QItemSelectionModel::Toggle);
+                swallowMousePressEvent = true;
+            } else if (item.isDir()) {
+                m_childUrl = item.url();
+                viewport()->update();
+
+                // Only request the activation if not the left button is pressed.
+                // The left button on a directory opens a new column, hence requesting
+                // an activation is useless as the new column will request the activation
+                // afterwards.
+                if (event->button() == Qt::LeftButton) {
+                    activate = false;
+                }
+            }
+
+            if (activate) {
+                m_view->requestActivation(this);
+            }
+
+            // TODO: is the assumption OK that Qt::RightButton always represents the context menu button?
+            if (event->button() == Qt::RightButton) {
+                swallowMousePressEvent = true;
+                if (!selModel->isSelected(index)) {
+                    clearSelection();
+                }
+                selModel->select(index, QItemSelectionModel::Select);
+            }
+        }
+    } else {
+        // a click on the viewport has been done
+        m_view->requestActivation(this);
+
+        // Swallow mouse move events if a click is done on the viewport. Otherwise the QColumnView
+        // triggers an unwanted loading of directories on hovering folder items.
+        m_swallowMouseMoveEvents = true;
+        clearSelection();
+    }
+
+    if (!swallowMousePressEvent) {
         QListView::mousePressEvent(event);
     }
 }
 
+void ColumnWidget::mouseMoveEvent(QMouseEvent* event)
+{
+    // see description in ColumnView::mousePressEvent()
+    if (!m_swallowMouseMoveEvents) {
+        QListView::mouseMoveEvent(event);
+    }
+}
+
+void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
+{
+    QListView::mouseReleaseEvent(event);
+    m_swallowMouseMoveEvents = false;
+}
+
+
 void ColumnWidget::paintEvent(QPaintEvent* event)
 {
+    if (!m_childUrl.isEmpty()) {
+        // indicate the shown URL of the next column by highlighting the shown folder item
+        const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
+        const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+        const QModelIndex dirIndex = dirModel->indexForUrl(m_childUrl);
+        const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex);
+        if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
+            const QRect itemRect = visualRect(proxyIndex);
+            QPainter painter(viewport());
+            painter.save();
+
+            QColor color = KColorScheme(KColorScheme::View).foreground();
+            color.setAlpha(32);
+            painter.setPen(Qt::NoPen);
+            painter.setBrush(color);
+            painter.drawRect(itemRect);
+
+            painter.restore();
+        }
+    }
+
     QListView::paintEvent(event);
 
     // TODO: remove this code when the issue #160611 is solved in Qt 4.4
@@ -219,22 +407,14 @@ void ColumnWidget::paintEvent(QPaintEvent* event)
 
 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
 {
-    if (m_view->viewport()->children().first() == this) {
-        // This column widget represents the root column. DolphinColumnView::createColumn()
-        // cannot retrieve the correct URL at this stage, as the directory lister will be
-        // started after the model has been assigned. This will be fixed here, where it is
-        // assured that the directory lister has been started already.
-        const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
-        const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
-        const KDirLister* dirLister = dirModel->dirLister();
-        m_url = dirLister->url();
+    if (!m_active) {
+        m_view->requestActivation(this);
     }
 
     QListView::contextMenuEvent(event);
 
     const QModelIndex index = indexAt(event->pos());
-    const KUrl& navigatorUrl = m_view->m_controller->url();
-    if (index.isValid() || (m_url == navigatorUrl)) {
+    if (index.isValid() || m_active) {
         // Only open a context menu above an item or if the mouse is above
         // the active column.
         const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
@@ -242,6 +422,15 @@ void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
     }
 }
 
+void ColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
+{
+    // inactive views should not have any selection
+    if (!m_active) {
+        clearSelection();
+    }
+    QListView::selectionChanged(selected, deselected);
+}
+
 void ColumnWidget::activate()
 {
     const QColor bgColor = KColorScheme(KColorScheme::View).background();
@@ -249,7 +438,7 @@ void ColumnWidget::activate()
     palette.setColor(viewport()->backgroundRole(), bgColor);
     viewport()->setPalette(palette);
 
-    setSelectionMode(MultiSelection);
+    update();
 }
 
 void ColumnWidget::deactivate()
@@ -262,7 +451,7 @@ void ColumnWidget::deactivate()
     palette.setColor(viewport()->backgroundRole(), bgColor);
     viewport()->setPalette(palette);
 
-    setSelectionMode(SingleSelection);
+    update();
 }
 
 // ---
@@ -274,9 +463,9 @@ DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* control
     Q_ASSERT(controller != 0);
 
     setAcceptDrops(true);
-    setSelectionBehavior(SelectItems);
     setDragDropMode(QAbstractItemView::DragDrop);
     setDropIndicatorShown(false);
+    setSelectionMode(ExtendedSelection);
 
     if (KGlobalSettings::singleClick()) {
         connect(this, SIGNAL(clicked(const QModelIndex&)),
@@ -285,8 +474,6 @@ DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* control
         connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
                 this, SLOT(triggerItem(const QModelIndex&)));
     }
-    connect(this, SIGNAL(activated(const QModelIndex&)),
-            this, SLOT(triggerItem(const QModelIndex&)));
     connect(this, SIGNAL(entered(const QModelIndex&)),
             controller, SLOT(emitItemEntered(const QModelIndex&)));
     connect(this, SIGNAL(viewportEntered()),
@@ -295,6 +482,8 @@ DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* control
             this, SLOT(zoomIn()));
     connect(controller, SIGNAL(zoomOut()),
             this, SLOT(zoomOut()));
+    connect(controller, SIGNAL(urlChanged(const KUrl&)),
+            this, SLOT(updateColumnsState(const KUrl&)));
 
     updateDecorationSize();
 }
@@ -303,25 +492,36 @@ DolphinColumnView::~DolphinColumnView()
 {
 }
 
-QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
+void DolphinColumnView::invertSelection()
 {
-    // To be able to visually indicate whether a column is active (which means
-    // that it represents the content of the URL navigator), the column
-    // must remember its URL.
-    const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
-    const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+    selectActiveColumn(QItemSelectionModel::Toggle);
+}
 
-    const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
-    KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
+void DolphinColumnView::selectAll()
+{
+    selectActiveColumn(QItemSelectionModel::Select);
+}
 
+QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
+{
+    // let the column widget be aware about its URL...
     KUrl columnUrl;
-    if (fileItem != 0) {
-        columnUrl = fileItem->url();
+    if (viewport()->children().count() == 0) {
+        // For the first column widget the directory lister has not been started
+        // yet, hence use the URL from the controller instead.
+        columnUrl = m_controller->url();
+    } else {
+        const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
+        const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+
+        const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
+        KFileItem fileItem = dirModel->itemForIndex(dirModelIndex);
+        if (!fileItem.isNull()) {
+            columnUrl = fileItem.url();
+        }
     }
 
-    ColumnWidget* view = new ColumnWidget(viewport(),
-                                          this,
-                                          columnUrl);
+    ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
 
     // The following code has been copied 1:1 from QColumnView::createColumn().
     // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
@@ -420,16 +620,36 @@ void DolphinColumnView::zoomOut()
 void DolphinColumnView::triggerItem(const QModelIndex& index)
 {
     m_controller->triggerItem(index);
+    updateColumnsState(m_controller->url());
+}
+
+void DolphinColumnView::updateColumnsState(const KUrl& url)
+{
+    foreach (QObject* object, viewport()->children()) {
+        if (object->inherits("QListView")) {
+            ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+            widget->setActive(widget->url() == url);
+        }
+    }
+}
+
+
+void DolphinColumnView::updateDecorationSize()
+{
+    ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
+    const int iconSize = settings->iconSize();
 
-    // Update the activation state of all columns. Only the column
-    // which represents the URL of the URL navigator is marked as active.
-    const KUrl& navigatorUrl = m_controller->url();
     foreach (QObject* object, viewport()->children()) {
         if (object->inherits("QListView")) {
             ColumnWidget* widget = static_cast<ColumnWidget*>(object);
-            widget->setActive(navigatorUrl == widget->url());
+            widget->setDecorationSize(QSize(iconSize, iconSize));
         }
     }
+
+    m_controller->setZoomInPossible(isZoomInPossible());
+    m_controller->setZoomOutPossible(isZoomOutPossible());
+
+    doItemsLayout();
 }
 
 bool DolphinColumnView::isZoomInPossible() const
@@ -444,22 +664,65 @@ bool DolphinColumnView::isZoomOutPossible() const
     return settings->iconSize() > K3Icon::SizeSmall;
 }
 
-void DolphinColumnView::updateDecorationSize()
+void DolphinColumnView::requestActivation(QWidget* column)
 {
-    ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
-    const int iconSize = settings->iconSize();
+    foreach (QObject* object, viewport()->children()) {
+        if (object->inherits("QListView")) {
+            ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+            const bool isActive = (widget == column);
+            widget->setActive(isActive);
+            if (isActive) {
+               m_controller->setUrl(widget->url());
+            }
+        }
+    }
+}
 
+void DolphinColumnView::requestSelectionModel(QAbstractItemView* view)
+{
     foreach (QObject* object, viewport()->children()) {
         if (object->inherits("QListView")) {
             ColumnWidget* widget = static_cast<ColumnWidget*>(object);
-            widget->setDecorationSize(QSize(iconSize, iconSize));
+            if (widget == view) {
+                widget->obtainSelectionModel();
+            } else {
+                widget->releaseSelectionModel();
+            }
+        }
+    }
+}
+
+void DolphinColumnView::selectActiveColumn(QItemSelectionModel::SelectionFlags flags)
+{
+    // TODO: this approach of selecting the active column is very slow. It should be
+    // possible to speedup the implementation by using QItemSelection, but all adempts
+    // have failed yet...
+
+    // assure that the selection model of the active column is set properly, otherwise
+    // no visual update of the selections is done
+    const KUrl& activeUrl = m_controller->url();
+    foreach (QObject* object, viewport()->children()) {
+        if (object->inherits("QListView")) {
+            ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+            if (widget->url() == activeUrl) {
+                widget->obtainSelectionModel();
+            } else {
+                widget->releaseSelectionModel();
+            }
         }
     }
 
-    m_controller->setZoomInPossible(isZoomInPossible());
-    m_controller->setZoomOutPossible(isZoomOutPossible());
+    QItemSelectionModel* selModel = selectionModel();
 
-    doItemsLayout();
+    const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
+    const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+    KDirLister* dirLister = dirModel->dirLister();
+
+    const KFileItemList list = dirLister->itemsForDir(activeUrl);
+    foreach (KFileItem* item, list) {
+        const QModelIndex index = dirModel->indexForUrl(item->url());
+        selModel->select(proxyModel->mapFromSource(index), flags);
+    }
 }
 
 #include "dolphincolumnview.moc"