click to inactive file list won't change selection
[case] / src / filelist.cpp
index 9b52332..a4927b8 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <QProcess>
 #include <QUrl>
+#include <QEvent>
 
 #include <hildon-mime.h>
 #include <dbus/dbus.h>
@@ -28,7 +29,8 @@
 
 FileList::FileList(QWidget *parent) :
     QListView(parent),
-    fileSystemModel(new QFileSystemModel)
+    fileSystemModel(new QFileSystemModel),
+    dontSelect(0)
 {
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
@@ -85,14 +87,40 @@ void FileList::toggleShowHiddenFiles() {
 }
 
 
+void FileList::preventNextSelection() {
+    dontSelect = 2;
+}
+
+
+void FileList::mousePressEvent(QMouseEvent *event) {
+    emit mousePressed();
+    QListView::mousePressEvent(event);
+}
+
+
+QItemSelectionModel::SelectionFlags FileList::selectionCommand(const QModelIndex &index, const QEvent *event) const {
+    if (dontSelect && event && event->type() == QEvent::MouseButtonPress) {
+        --dontSelect;
+    }
+
+    if (dontSelect && event &&
+        (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove))
+    {
+        return QItemSelectionModel::NoUpdate;
+    }
+
+    return QListView::selectionCommand(index, event);
+}
+
+
 void FileList::activateItem(QModelIndex index) {
     const QFileInfo &file = fileSystemModel->fileInfo(index);
 
     if(file.isDir()) {
         changePath(file.absoluteFilePath());
-        // hack: we reset it to MultiSelection again in the mousePressEvent
-        // without this, the item under the cursor gets selected right after changing the directory
-        setSelectionMode(QAbstractItemView::NoSelection);
+        // hack: this will prevent selecting the item under the cursor on the second click
+        // of the doubleclick that changed the directory
+        dontSelect = 1;
     } else if(file.isExecutable()) {
         QProcess::startDetached(file.absoluteFilePath());
     } else {
@@ -106,11 +134,3 @@ void FileList::activateItem(QModelIndex index) {
         //QDesktopServices::openUrl(QUrl::fromLocalFile(file.absoluteFilePath()));
     }
 }
-
-
-void FileList::mousePressEvent(QMouseEvent *event) {
-    emit mousePressed();
-    QListView::mousePressEvent(event);
-    // need to reset the selection mode in case it was set to NoSelection in activateItem(...)
-    setSelectionMode(QAbstractItemView::MultiSelection);
-}