]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincolumnview.cpp
use "Sort by" instead of "Sort By"
[dolphin.git] / src / dolphincolumnview.cpp
index f4a5ef19a9cb4f7e78449d7bf391f43d304c03bf..e4669442ccc231519181581a4d8d0bba1c8a0f3a 100644 (file)
 #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.
@@ -58,7 +98,16 @@ public:
 
     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:
@@ -173,6 +222,7 @@ void ColumnWidget::obtainSelectionModel()
     if (selectionModel() != m_view->selectionModel()) {
         selectionModel()->deleteLater();
         setSelectionModel(m_view->selectionModel());
+        clearSelection();
     }
 }
 
@@ -233,42 +283,56 @@ void ColumnWidget::dropEvent(QDropEvent* event)
 
 void ColumnWidget::mousePressEvent(QMouseEvent* event)
 {
+    // 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. Only request an activation
-        // if the item is not a directory.
+        // 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 != 0) {
+        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);
-                selModel->select(index, QItemSelectionModel::Select);
+                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();
+            } 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) {
-                    m_view->requestActivation(this);
+                if (event->button() == Qt::LeftButton) {
+                    activate = false;
                 }
-            } else {
-                m_view->requestActivation(this);
             }
 
-            // TODO: check behavior with ShiftModifier
-            //if (modifier & Qt::ShiftModifier)
+            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) {
@@ -428,6 +492,16 @@ DolphinColumnView::~DolphinColumnView()
 {
 }
 
+void DolphinColumnView::invertSelection()
+{
+    selectActiveColumn(QItemSelectionModel::Toggle);
+}
+
+void DolphinColumnView::selectAll()
+{
+    selectActiveColumn(QItemSelectionModel::Select);
+}
+
 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
 {
     // let the column widget be aware about its URL...
@@ -441,9 +515,9 @@ QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
         const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
 
         const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
-        KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
-        if (fileItem != 0) {
-            columnUrl = fileItem->url();
+        KFileItem fileItem = dirModel->itemForIndex(dirModelIndex);
+        if (!fileItem.isNull()) {
+            columnUrl = fileItem.url();
         }
     }
 
@@ -598,7 +672,7 @@ void DolphinColumnView::requestActivation(QWidget* column)
             const bool isActive = (widget == column);
             widget->setActive(isActive);
             if (isActive) {
-                m_controller->setUrl(widget->url());
+               m_controller->setUrl(widget->url());
             }
         }
     }
@@ -618,4 +692,37 @@ void DolphinColumnView::requestSelectionModel(QAbstractItemView* view)
     }
 }
 
+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();
+            }
+        }
+    }
+
+    QItemSelectionModel* selModel = selectionModel();
+
+    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"