ginebra2/ActionButton.cpp
changeset 6 1c3b8676e58c
parent 5 0f2326c2a325
--- a/ginebra2/ActionButton.cpp	Wed Jun 23 17:59:43 2010 +0300
+++ b/ginebra2/ActionButton.cpp	Tue Jul 06 14:03:49 2010 +0300
@@ -27,8 +27,10 @@
   ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent)
     : NativeChromeItem(snippet, parent),
       m_internalAction(NULL),
-      m_triggerOn(QEvent::GraphicsSceneMouseRelease),
-      m_active(false)
+      m_triggerOnUp(true),
+      m_triggerOnDown(false),
+      m_active(false),
+      m_activeOnPress(true)
   {
 
 
@@ -44,15 +46,12 @@
     QIcon::Mode mode = QIcon::Normal;
 
     if (action) {
-        if (m_active || action->isChecked() ) {
-            mode = QIcon::Selected;
+        if (m_active) {
+            mode = QIcon::Active;
         }
         else if (!action->isEnabled()) {
             mode = QIcon::Disabled;
         }
-        else {
-            mode = QIcon::Active;
-        }
     }
     m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On);
     painter->restore();
@@ -61,14 +60,18 @@
 
   void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev )
   {
+
     QAction * action = defaultAction();
-    if (action ) {
-      if (m_triggerOn == ev->type()){
+    if (action && (action->isEnabled()) ) {
+      //qDebug() << "mousePressEvent" << m_triggerOnDown <<  ev->type();
+      // If m_activeOnPress is true, set active flag to set icon state to Selected
+      if (m_activeOnPress )
+        setActive(true);
+      if (m_triggerOnDown == true) {
         if (ev->button() == Qt::LeftButton) {
-            if (action->isEnabled()){
+
               action->trigger();
               emit activated();
-            }
         }
       }
 
@@ -78,30 +81,32 @@
 
   void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev )
   {
+
+    bool trigger = sceneBoundingRect().contains(ev->scenePos());
+
     QAction * action = defaultAction();
     //qDebug() << "ActionButton::mouseReleaseEvent " << m_snippet->elementId();
-    if (m_triggerOn == ev->type()){
+
+    if (   trigger && m_triggerOnUp == true) {
       if (ev->button() == Qt::LeftButton) {
         if (action && action->isEnabled()){
           action->trigger();
           emit activated();
+
         }
       }
     }
+    // If m_activeOnPress is true, reset active flag to set icon state to Normal
+    if (m_activeOnPress || !trigger)
+      setActive(false);
     emit mouseEvent(ev->type() );
   }
 
-  void ActionButton::contextMenuEvent( QGraphicsSceneContextMenuEvent * ev )
-  {
-    Q_UNUSED(ev)
-    emit contextMenuEvent();
-  }
-
   //Action buttons only have one action at a time, so whenever we add an action, we remove any previously set action
   //NB: The action is typically one of the available actions on a view (via ControllableView.getContext()).
   //ActionButtonSnippet provides the scriptable method connectAction() to create native connections to view actions
 
-  void ActionButton::setAction ( QAction * action, QEvent::Type triggerOn )
+  void ActionButton::setAction ( QAction * action, bool triggerOnDown,  bool triggerOnUp )
   {
     QAction * currentAction = defaultAction();
     if (currentAction == action)
@@ -112,39 +117,33 @@
     }
     addAction(action);
     connect(action, SIGNAL(changed()),this, SLOT(onActionChanged()));
-    m_triggerOn = triggerOn;
+    m_triggerOnUp = triggerOnUp;
+    m_triggerOnDown = triggerOnDown;
 
 
     // Save the action as the internal action and set its properties
     m_internalAction = action;
-    m_internalAction->setCheckable(true);
+    m_internalAction->setCheckable(false);
 
     update();
   }
 
-  void ActionButton::disconnectAction () {
-    setAction(NULL);
-  }
-
   void ActionButton::setEnabled(bool enabled)
   {
     m_internalAction->setEnabled(enabled);
   }
 
-  void ActionButton::setChecked(bool checked)
+  void ActionButton::setActiveOnPress(bool active)
   {
-    m_internalAction->setChecked(checked);
+    m_activeOnPress = active;
   }
   
   void ActionButton::setActive(bool active)
   {
-    m_active = active;
-    update();
-  }
-
-  void ActionButton::setInputEvent(QEvent::Type event)
-  {
-    m_triggerOn = event;
+    if (m_active != active ) {
+        m_active = active;
+        update();
+    }
   }
 
   //NB: handle icon on/off states too?
@@ -181,7 +180,7 @@
     QAction * action = defaultAction();
     if (action && action->isEnabled() && !action->isChecked()){
       action->setChecked(true);
-      update();
+      setActive(true);
     }
   }