ginebra2/ActionButton.cpp
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
child 6 1c3b8676e58c
--- a/ginebra2/ActionButton.cpp	Fri May 14 15:40:36 2010 +0300
+++ b/ginebra2/ActionButton.cpp	Wed Jun 23 17:59:43 2010 +0300
@@ -1,21 +1,24 @@
 /*
 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
-* This component and the accompanying materials are made available
-* under the terms of "Eclipse Public License v1.0"
-* which accompanies this distribution, and is available
-* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Lesser General Public License as published by
+* the Free Software Foundation, version 2.1 of the License.
 *
-* Initial Contributors:
-* Nokia Corporation - initial contribution.
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU Lesser General Public License for more details.
 *
-* Contributors:
+* You should have received a copy of the GNU Lesser General Public License
+* along with this program.  If not,
+* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
 *
-* Description: 
+* Description:
 *
 */
 
-
 #include "ActionButton.h"
 #include <QDebug>
 
@@ -23,18 +26,12 @@
 
   ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent)
     : NativeChromeItem(snippet, parent),
-      m_internalAction(new QAction(this)),
-      m_triggerOn(QEvent::GraphicsSceneMousePress),
+      m_internalAction(NULL),
+      m_triggerOn(QEvent::GraphicsSceneMouseRelease),
       m_active(false)
   {
-    m_internalAction->setCheckable(true);
-    m_internalAction->setEnabled(true);
-    setAction(m_internalAction);
 
-    //Just testing . . .
-    //addIcon(":/chrome/demochrome/HandButton.png");
-    //addIcon(":/chrome/demochrome/Stop.png", QIcon::Disabled);
-    connect(m_internalAction, SIGNAL(triggered(bool)), this, SLOT(onTriggered(bool)));
+
   }
 
   void ActionButton::paint( QPainter * painter, const QStyleOptionGraphicsItem * opt, QWidget * widget )
@@ -45,44 +42,53 @@
     painter->save();
     QAction * action = defaultAction();
     QIcon::Mode mode = QIcon::Normal;
-    if(m_active)
-      mode = QIcon::Active;
-    else if(action){
-      if(action->isChecked())
-	mode = QIcon::Selected;
-      else if(!action->isEnabled())
-	mode = QIcon::Disabled;
+
+    if (action) {
+        if (m_active || action->isChecked() ) {
+            mode = QIcon::Selected;
+        }
+        else if (!action->isEnabled()) {
+            mode = QIcon::Disabled;
+        }
+        else {
+            mode = QIcon::Active;
+        }
     }
     m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On);
     painter->restore();
+    NativeChromeItem::paint(painter, opt, widget);
   }
 
   void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev )
   {
-    if(m_triggerOn == ev->type()){
-      if (ev->button() == Qt::LeftButton) {
-	QAction * action = defaultAction();
-	if(action && action->isEnabled()){
-	  action->trigger();
-	  emit activated();
-	}
+    QAction * action = defaultAction();
+    if (action ) {
+      if (m_triggerOn == ev->type()){
+        if (ev->button() == Qt::LeftButton) {
+            if (action->isEnabled()){
+              action->trigger();
+              emit activated();
+            }
+        }
       }
-      m_active = true;
+
     }
+    emit mouseEvent(ev->type() );
   }
 
   void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev )
   {
-    if(m_triggerOn == ev->type()){
+    QAction * action = defaultAction();
+    //qDebug() << "ActionButton::mouseReleaseEvent " << m_snippet->elementId();
+    if (m_triggerOn == ev->type()){
       if (ev->button() == Qt::LeftButton) {
-	QAction * action = defaultAction();
-	if(action && action->isEnabled()){
-	  action->trigger();
-	  emit activated();
-	}
+        if (action && action->isEnabled()){
+          action->trigger();
+          emit activated();
+        }
       }
-      m_active = false;
     }
+    emit mouseEvent(ev->type() );
   }
 
   void ActionButton::contextMenuEvent( QGraphicsSceneContextMenuEvent * ev )
@@ -94,24 +100,30 @@
   //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 )
   {
     QAction * currentAction = defaultAction();
-    if(currentAction == action)
+    if (currentAction == action)
       return;
-    if(currentAction){
+    if (currentAction){
       disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged()));
       removeAction(currentAction);
     }
     addAction(action);
     connect(action, SIGNAL(changed()),this, SLOT(onActionChanged()));
     m_triggerOn = triggerOn;
+
+
+    // Save the action as the internal action and set its properties
+    m_internalAction = action;
+    m_internalAction->setCheckable(true);
+
     update();
   }
 
   void ActionButton::disconnectAction () {
-    setAction(m_internalAction);
+    setAction(NULL);
   }
 
   void ActionButton::setEnabled(bool enabled)
@@ -123,6 +135,12 @@
   {
     m_internalAction->setChecked(checked);
   }
+  
+  void ActionButton::setActive(bool active)
+  {
+    m_active = active;
+    update();
+  }
 
   void ActionButton::setInputEvent(QEvent::Type event)
   {
@@ -134,7 +152,7 @@
   void ActionButton::addIcon( const QString & resource, QIcon::Mode mode )
   {
     m_icon.addPixmap( QPixmap(resource), mode, QIcon::On );
-  }        
+  }
 
   QAction * ActionButton::defaultAction()
   {
@@ -147,11 +165,25 @@
     //Repaint when the action changes state
     update();
   }
- 
-  // For testing only . . .
-  void ActionButton::onTriggered(bool checked){
-    Q_UNUSED(checked)
-    qDebug() << "ActionButton::triggered";
+
+  void ActionButton::onHidden(){
+
+    QAction * action = defaultAction();
+    if (action && action->isChecked() ){
+      action->setChecked(false);
+    }
+    setActive(false);
+
   }
 
+  void ActionButton::onShown(){
+
+    QAction * action = defaultAction();
+    if (action && action->isEnabled() && !action->isChecked()){
+      action->setChecked(true);
+      update();
+    }
+  }
+
+
 }//end of name space