From 9999691af2c69e2aee7068d2cac50391139f415b Mon Sep 17 00:00:00 2001
From: BenjaminLong <benjamin.long@kitware.com>
Date: Tue, 22 Nov 2011 09:12:02 -0500
Subject: [PATCH] Fix all the QSpinBox issues - Close #88

---
 pqSpinBoxEventTranslator.cxx |   71 +++++++++++++++++++++--------------------
 pqSpinBoxEventTranslator.h   |    6 +++
 2 files changed, 42 insertions(+), 35 deletions(-)

diff --git a/pqSpinBoxEventTranslator.cxx b/pqSpinBoxEventTranslator.cxx
index 9529304..ffee69c 100644
--- a/pqSpinBoxEventTranslator.cxx
+++ b/pqSpinBoxEventTranslator.cxx
@@ -41,6 +41,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 pqSpinBoxEventTranslator::pqSpinBoxEventTranslator(QObject* p)
   : pqWidgetEventTranslator(p)
 {
+  this->CurrentObject = 0;
 }
 
 bool pqSpinBoxEventTranslator::translateEvent(QObject* Object, QEvent* Event, bool& /*Error*/)
@@ -54,49 +55,49 @@ bool pqSpinBoxEventTranslator::translateEvent(QObject* Object, QEvent* Event, bo
     }
 
   if(!object)
-    return false;
-    
-  switch(Event->type())
+      return false;
+
+  if(Event->type() == QEvent::Enter && Object==object)
     {
-    case QEvent::MouseButtonPress:
+    if(this->CurrentObject != Object)
       {
-      QMouseEvent* me = static_cast<QMouseEvent*>(Event);
-      if(me->button() == Qt::LeftButton)
+      if(this->CurrentObject)
         {
-        QStyle* style = object->style();
-        QStyleOptionSpinBox option;
-        option.initFrom(object);
-        option.subControls = QStyle::SC_All;
-        QStyle::SubControl sub = style->hitTestComplexControl(
-          QStyle::CC_SpinBox, &option, me->pos(), object);
-        if(sub == QStyle::SC_SpinBoxUp)
-          {
-          emit recordEvent(object, "spin", "up");
-          }
-        else if(sub == QStyle::SC_SpinBoxDown)
-          {
-          emit recordEvent(object, "spin", "down");
-          }
+        disconnect(this->CurrentObject, 0, this, 0);
         }
+      this->CurrentObject = Object;
+      this->Value = object->value();
+      connect(object, SIGNAL(valueChanged(int)),this, SLOT(onValueChanged(int)));
+      connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
       }
-      break;
-    case QEvent::KeyRelease:
+    }
+
+  if(Event->type() == QEvent::KeyRelease && Object == object)
+    {
+    QKeyEvent* ke = static_cast<QKeyEvent*>(Event);
+    QString keyText = ke->text();
+    this->Value = object->value();
+    if(keyText.length() && keyText.at(0).isLetterOrNumber())
       {
-      QKeyEvent* ke = static_cast<QKeyEvent*>(Event);
-      QString keyText = ke->text();
-      if(keyText.length() && keyText.at(0).isLetterOrNumber())
-        {
-        emit recordEvent(object, "set_int", QString("%1").arg(object->value()));
-        }
-      else
-        {
-        emit recordEvent(object, "key", QString("%1").arg(ke->key()));
-        }
+      emit recordEvent(object, "set_int", QString("%1").arg(object->value()));
+      }
+    else
+      {
+      emit recordEvent(object, "key", QString("%1").arg(ke->key()));
       }
-    default:
-      break;
     }
-      
   return true;
 }
 
+void pqSpinBoxEventTranslator::onDestroyed(QObject* /*Object*/)
+{
+  this->CurrentObject = 0;
+}
+
+void pqSpinBoxEventTranslator::onValueChanged(int number)
+{
+  QString sens = (number - this->Value > 0) ? "up" : "down";
+  this->Value = number;
+  emit recordEvent(this->CurrentObject, "spin", sens);
+}
+
diff --git a/pqSpinBoxEventTranslator.h b/pqSpinBoxEventTranslator.h
index 65d8b00..b284e8e 100644
--- a/pqSpinBoxEventTranslator.h
+++ b/pqSpinBoxEventTranslator.h
@@ -55,6 +55,12 @@ private:
   pqSpinBoxEventTranslator(const pqSpinBoxEventTranslator&);
   pqSpinBoxEventTranslator& operator=(const pqSpinBoxEventTranslator&);
 
+  int      Value;
+  QObject* CurrentObject;
+
+private slots:
+  void onDestroyed(QObject*);
+  void onValueChanged(int number);
 };
 
 #endif // !_pqSpinBoxEventTranslator_h
-- 
1.7.4.1

