Procházet zdrojové kódy

DICOMTableView: Do not clear selection if user click outside of existing rows

Fixes #545

This is done by installing an event filter on the tableview viewport that
will block the mouse press and double click event if no row are pointed
by the cursor.

Reported-by: Sergey Aleshin <4memph@gmail.com>
Jean-Christophe Fillion-Robin před 10 roky
rodič
revize
de70697141

+ 24 - 0
Libs/DICOM/Widgets/ctkDICOMTableView.cpp

@@ -23,6 +23,7 @@
 #include "ui_ctkDICOMTableView.h"
 
 // Qt includes
+#include <QMouseEvent>
 #include <QSortFilterProxyModel>
 #include <QSqlQueryModel>
 
@@ -81,6 +82,7 @@ ctkDICOMTableViewPrivate::~ctkDICOMTableViewPrivate()
 {
 }
 
+//------------------------------------------------------------------------------
 void ctkDICOMTableViewPrivate::init()
 {
   Q_Q(ctkDICOMTableView);
@@ -93,6 +95,8 @@ void ctkDICOMTableViewPrivate::init()
     {
       this->setUpTableView();
     }
+
+  this->tblDicomDatabaseView->viewport()->installEventFilter(q);
 }
 
 //------------------------------------------------------------------------------
@@ -298,6 +302,26 @@ void ctkDICOMTableView::selectAll()
 }
 
 //------------------------------------------------------------------------------
+bool ctkDICOMTableView::eventFilter(QObject *obj, QEvent *event)
+{
+  Q_D(ctkDICOMTableView);
+  if (obj == d->tblDicomDatabaseView->viewport())
+    {
+      if (event->type() == QEvent::MouseButtonPress ||
+          event->type() == QEvent::MouseButtonDblClick)
+        {
+          QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
+          QPoint pos = mouseEvent->pos();
+          if (!d->tblDicomDatabaseView->indexAt(pos).isValid())
+            {
+              return true;
+            }
+        }
+    }
+  return QObject::eventFilter(obj, event);
+}
+
+//------------------------------------------------------------------------------
 void ctkDICOMTableView::setQuery(const QStringList &uids)
 {
   Q_D(ctkDICOMTableView);

+ 3 - 0
Libs/DICOM/Widgets/ctkDICOMTableView.h

@@ -150,6 +150,9 @@ protected Q_SLOTS:
 
   void selectAll();
 
+protected:
+  virtual bool eventFilter(QObject *obj, QEvent *event);
+
 Q_SIGNALS:
   /**
    * @brief Is emitted when the selection in the tableview has changed