changeset 22726:dd2f547c21a0

QtHandles: firie listbox callback on click of selected item (Bug #44748) * libgui/graphics/ListBoxControl.h, libgui/graphics/ListBoxControl.cc (class ListBoxControl) new variable m_selectionChanged (ListBoxControl::itemActivated): new function (ListBoxControl::itemPressed): new function (ListBoxControl::ListBoxControl): connect slots itemActivated, itemPressed (ListBoxControl::itemSelectionChanged) set selectionChanged flag, move old content to sendSelectionChange. (ListBoxControl::sendSelectionChange) new function. (ListBoxControl::eventFilter) new function. * libgui/graphics/PopupMenuControl.cc (PopupMenuControl::PopupMenuControl): use activated instead of currentIndexChanged signal
author John Donoghue
date Fri, 22 May 2015 13:04:32 -0400
parents 03cebe1fb0e3
children ed99654ec281
files libgui/graphics/ListBoxControl.cc libgui/graphics/ListBoxControl.h libgui/graphics/PopupMenuControl.cc
diffstat 3 files changed, 104 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/graphics/ListBoxControl.cc	Thu Nov 03 13:24:21 2016 +0100
+++ b/libgui/graphics/ListBoxControl.cc	Fri May 22 13:04:32 2015 -0400
@@ -25,6 +25,8 @@
 #endif
 
 #include <QListWidget>
+#include <QEvent>
+#include <QMouseEvent>
 
 #include "Container.h"
 #include "ListBoxControl.h"
@@ -79,7 +81,7 @@
   }
 
   ListBoxControl::ListBoxControl (const graphics_object& go, QListWidget* list)
-    : BaseControl (go, list), m_blockCallback (false)
+    : BaseControl (go, list), m_blockCallback (false), m_selectionChanged (false)
   {
     uicontrol::properties& up = properties<uicontrol> ();
 
@@ -109,11 +111,14 @@
           }
       }
 
-    list->removeEventFilter (this);
     list->viewport ()->installEventFilter (this);
 
     connect (list, SIGNAL (itemSelectionChanged (void)),
              SLOT (itemSelectionChanged (void)));
+    connect (list, SIGNAL (activated (const QModelIndex &)),
+             SLOT (itemActivated (const QModelIndex &)));
+    connect (list, SIGNAL (itemPressed (QListWidgetItem*)),
+           SLOT (itemPressed (QListWidgetItem*)));
   }
 
   ListBoxControl::~ListBoxControl (void)
@@ -157,7 +162,7 @@
   }
 
   void
-  ListBoxControl::itemSelectionChanged (void)
+  ListBoxControl::sendSelectionChange()
   {
     if (! m_blockCallback)
       {
@@ -173,7 +178,95 @@
         gh_manager::post_set (m_handle, "value", octave_value (value), false);
         gh_manager::post_callback (m_handle, "callback");
       }
+
+      m_selectionChanged = false;
+  }
+
+  void
+  ListBoxControl::itemSelectionChanged (void)
+  {
+    if (! m_blockCallback)
+      m_selectionChanged = true;
+  }
+
+  void 
+  ListBoxControl::itemActivated (const QModelIndex &)
+  {
+    m_selectionChanged = true;
+  }
+  void 
+  ListBoxControl::itemPressed (QListWidgetItem*)
+  {
+    m_selectionChanged = true;
   }
 
+  bool 
+  ListBoxControl::eventFilter (QObject* watched, QEvent* e)
+  {
+    // listbox change
+    if (watched == m_qobject)
+      {
+        switch (e->type ())
+          {
+            case QEvent::KeyRelease:
+              if (m_selectionChanged)
+                sendSelectionChange ();
+              m_selectionChanged = false;
+              break;
+ 
+            default:
+              break;
+          } 
+          
+        return Object::eventFilter (watched, e);
+      }
+    // listbox viewport
+    else
+      {
+        bool override_return = false;
+        QListWidget* list = qWidget<QListWidget> ();
+
+        switch (e->type ())
+          {
+            case QEvent::MouseButtonPress: 
+              {
+                QMouseEvent* m = dynamic_cast<QMouseEvent*> (e);
+
+                if (m->button () & Qt::RightButton)
+                  override_return = true;
+                else
+                  {
+                    if(!list->indexAt(m->pos()).isValid()) override_return = true;
+                    m_selectionChanged = true;
+                  }
+                break;
+              }
+            case QEvent::MouseButtonRelease:
+              {
+                QMouseEvent* m = dynamic_cast<QMouseEvent*> (e);
+  
+                if (m->button () & Qt::RightButton)
+                  override_return = true;
+
+                else if(!list->indexAt(m->pos()).isValid()) 
+                  {
+                    list->setCurrentRow(list->count()-1); 
+                    override_return = true;
+                  }
+
+                if (m_selectionChanged)
+                  sendSelectionChange ();
+                m_selectionChanged = false;
+              
+                break;
+              }
+            default:
+              break;
+ 
+          }
+        return BaseControl::eventFilter (watched, e) || override_return;
+      }
+  }
 }
 
+
--- a/libgui/graphics/ListBoxControl.h	Thu Nov 03 13:24:21 2016 +0100
+++ b/libgui/graphics/ListBoxControl.h	Fri May 22 13:04:32 2015 -0400
@@ -26,6 +26,8 @@
 #include "BaseControl.h"
 
 class QListWidget;
+class QListWidgetItem;
+class QModelIndex;
 
 namespace QtHandles
 {
@@ -42,12 +44,17 @@
 
   protected:
     void update (int pId);
+    bool eventFilter (QObject* watched, QEvent* e);
+    void sendSelectionChange();
 
   private slots:
     void itemSelectionChanged (void);
+    void itemActivated (const QModelIndex &);
+    void itemPressed (QListWidgetItem*);
 
   private:
     bool m_blockCallback;
+    bool m_selectionChanged;
   };
 
 }
--- a/libgui/graphics/PopupMenuControl.cc	Thu Nov 03 13:24:21 2016 +0100
+++ b/libgui/graphics/PopupMenuControl.cc	Fri May 22 13:04:32 2015 -0400
@@ -58,7 +58,7 @@
 
     update (uicontrol::properties::ID_VALUE);
 
-    connect (box, SIGNAL (currentIndexChanged (int)),
+    connect (box, SIGNAL (activated (int)),
              SLOT (currentIndexChanged (int)));
   }