ginebra2/ActionButton.cpp
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
child 5 0f2326c2a325
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ActionButton.h"
       
    20 #include <QDebug>
       
    21 
       
    22 namespace GVA {
       
    23 
       
    24   ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent)
       
    25     : NativeChromeItem(snippet, parent),
       
    26       m_internalAction(new QAction(this)),
       
    27       m_triggerOn(QEvent::GraphicsSceneMousePress),
       
    28       m_active(false)
       
    29   {
       
    30     m_internalAction->setCheckable(true);
       
    31     m_internalAction->setEnabled(true);
       
    32     setAction(m_internalAction);
       
    33 
       
    34     //Just testing . . .
       
    35     //addIcon(":/chrome/demochrome/HandButton.png");
       
    36     //addIcon(":/chrome/demochrome/Stop.png", QIcon::Disabled);
       
    37     connect(m_internalAction, SIGNAL(triggered(bool)), this, SLOT(onTriggered(bool)));
       
    38   }
       
    39 
       
    40   void ActionButton::paint( QPainter * painter, const QStyleOptionGraphicsItem * opt, QWidget * widget )
       
    41   {
       
    42     Q_UNUSED(opt)
       
    43     Q_UNUSED(widget)
       
    44 
       
    45     painter->save();
       
    46     QAction * action = defaultAction();
       
    47     QIcon::Mode mode = QIcon::Normal;
       
    48     if(m_active)
       
    49       mode = QIcon::Active;
       
    50     else if(action){
       
    51       if(action->isChecked())
       
    52 	mode = QIcon::Selected;
       
    53       else if(!action->isEnabled())
       
    54 	mode = QIcon::Disabled;
       
    55     }
       
    56     m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On);
       
    57     painter->restore();
       
    58   }
       
    59 
       
    60   void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev )
       
    61   {
       
    62     if(m_triggerOn == ev->type()){
       
    63       if (ev->button() == Qt::LeftButton) {
       
    64 	QAction * action = defaultAction();
       
    65 	if(action && action->isEnabled()){
       
    66 	  action->trigger();
       
    67 	  emit activated();
       
    68 	}
       
    69       }
       
    70       m_active = true;
       
    71     }
       
    72   }
       
    73 
       
    74   void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev )
       
    75   {
       
    76     if(m_triggerOn == ev->type()){
       
    77       if (ev->button() == Qt::LeftButton) {
       
    78 	QAction * action = defaultAction();
       
    79 	if(action && action->isEnabled()){
       
    80 	  action->trigger();
       
    81 	  emit activated();
       
    82 	}
       
    83       }
       
    84       m_active = false;
       
    85     }
       
    86   }
       
    87 
       
    88   void ActionButton::contextMenuEvent( QGraphicsSceneContextMenuEvent * ev )
       
    89   {
       
    90     Q_UNUSED(ev)
       
    91     emit contextMenuEvent();
       
    92   }
       
    93 
       
    94   //Action buttons only have one action at a time, so whenever we add an action, we remove any previously set action
       
    95   //NB: The action is typically one of the available actions on a view (via ControllableView.getContext()).
       
    96   //ActionButtonSnippet provides the scriptable method connectAction() to create native connections to view actions
       
    97  
       
    98   void ActionButton::setAction ( QAction * action, QEvent::Type triggerOn )
       
    99   {
       
   100     QAction * currentAction = defaultAction();
       
   101     if(currentAction == action)
       
   102       return;
       
   103     if(currentAction){
       
   104       disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged()));
       
   105       removeAction(currentAction);
       
   106     }
       
   107     addAction(action);
       
   108     connect(action, SIGNAL(changed()),this, SLOT(onActionChanged()));
       
   109     m_triggerOn = triggerOn;
       
   110     update();
       
   111   }
       
   112 
       
   113   void ActionButton::disconnectAction () {
       
   114     setAction(m_internalAction);
       
   115   }
       
   116 
       
   117   void ActionButton::setEnabled(bool enabled)
       
   118   {
       
   119     m_internalAction->setEnabled(enabled);
       
   120   }
       
   121 
       
   122   void ActionButton::setChecked(bool checked)
       
   123   {
       
   124     m_internalAction->setChecked(checked);
       
   125   }
       
   126 
       
   127   void ActionButton::setInputEvent(QEvent::Type event)
       
   128   {
       
   129     m_triggerOn = event;
       
   130   }
       
   131 
       
   132   //NB: handle icon on/off states too?
       
   133 
       
   134   void ActionButton::addIcon( const QString & resource, QIcon::Mode mode )
       
   135   {
       
   136     m_icon.addPixmap( QPixmap(resource), mode, QIcon::On );
       
   137   }        
       
   138 
       
   139   QAction * ActionButton::defaultAction()
       
   140   {
       
   141     if (actions().isEmpty())
       
   142       return 0;
       
   143     return actions()[0];
       
   144   }
       
   145 
       
   146   void ActionButton::onActionChanged(){
       
   147     //Repaint when the action changes state
       
   148     update();
       
   149   }
       
   150  
       
   151   // For testing only . . .
       
   152   void ActionButton::onTriggered(bool checked){
       
   153     Q_UNUSED(checked)
       
   154     qDebug() << "ActionButton::triggered";
       
   155   }
       
   156 
       
   157 }//end of name space