# HG changeset patch
# User John Donoghue
# Date 1432314272 14400
#      Fri May 22 13:04:32 2015 -0400
# Branch stable
# Node ID c471edc51e04f99b8480bf4f6d2cc0d6bd5ec1be
# Parent  7adc5bbd840a7d880fda2412171782dc994494eb
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

diff -r 7adc5bbd840a -r c471edc51e04 libgui/graphics/ListBoxControl.cc
--- a/libgui/graphics/ListBoxControl.cc	Thu Oct 13 08:04:41 2016 -0400
+++ 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,65 @@
         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
+      {
+        switch (e->type ())
+          {
+            case QEvent::MouseButtonRelease:
+              if (m_selectionChanged)
+                sendSelectionChange ();
+              m_selectionChanged = false;
+              break;
+            default:
+              break;
+ 
+          }
+        return BaseControl::eventFilter (watched, e);
+      }
+  }
 }
 
+
diff -r 7adc5bbd840a -r c471edc51e04 libgui/graphics/ListBoxControl.h
--- a/libgui/graphics/ListBoxControl.h	Thu Oct 13 08:04:41 2016 -0400
+++ 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;
   };
 
 }
diff -r 7adc5bbd840a -r c471edc51e04 libgui/graphics/PopupMenuControl.cc
--- a/libgui/graphics/PopupMenuControl.cc	Thu Oct 13 08:04:41 2016 -0400
+++ 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)));
   }
 
