ginebra2/ActionButton.cpp
changeset 6 1c3b8676e58c
parent 5 0f2326c2a325
equal deleted inserted replaced
5:0f2326c2a325 6:1c3b8676e58c
    25 namespace GVA {
    25 namespace GVA {
    26 
    26 
    27   ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent)
    27   ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent)
    28     : NativeChromeItem(snippet, parent),
    28     : NativeChromeItem(snippet, parent),
    29       m_internalAction(NULL),
    29       m_internalAction(NULL),
    30       m_triggerOn(QEvent::GraphicsSceneMouseRelease),
    30       m_triggerOnUp(true),
    31       m_active(false)
    31       m_triggerOnDown(false),
       
    32       m_active(false),
       
    33       m_activeOnPress(true)
    32   {
    34   {
    33 
    35 
    34 
    36 
    35   }
    37   }
    36 
    38 
    42     painter->save();
    44     painter->save();
    43     QAction * action = defaultAction();
    45     QAction * action = defaultAction();
    44     QIcon::Mode mode = QIcon::Normal;
    46     QIcon::Mode mode = QIcon::Normal;
    45 
    47 
    46     if (action) {
    48     if (action) {
    47         if (m_active || action->isChecked() ) {
    49         if (m_active) {
    48             mode = QIcon::Selected;
    50             mode = QIcon::Active;
    49         }
    51         }
    50         else if (!action->isEnabled()) {
    52         else if (!action->isEnabled()) {
    51             mode = QIcon::Disabled;
    53             mode = QIcon::Disabled;
    52         }
       
    53         else {
       
    54             mode = QIcon::Active;
       
    55         }
    54         }
    56     }
    55     }
    57     m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On);
    56     m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On);
    58     painter->restore();
    57     painter->restore();
    59     NativeChromeItem::paint(painter, opt, widget);
    58     NativeChromeItem::paint(painter, opt, widget);
    60   }
    59   }
    61 
    60 
    62   void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev )
    61   void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev )
    63   {
    62   {
       
    63 
    64     QAction * action = defaultAction();
    64     QAction * action = defaultAction();
    65     if (action ) {
    65     if (action && (action->isEnabled()) ) {
    66       if (m_triggerOn == ev->type()){
    66       //qDebug() << "mousePressEvent" << m_triggerOnDown <<  ev->type();
       
    67       // If m_activeOnPress is true, set active flag to set icon state to Selected
       
    68       if (m_activeOnPress )
       
    69         setActive(true);
       
    70       if (m_triggerOnDown == true) {
    67         if (ev->button() == Qt::LeftButton) {
    71         if (ev->button() == Qt::LeftButton) {
    68             if (action->isEnabled()){
    72 
    69               action->trigger();
    73               action->trigger();
    70               emit activated();
    74               emit activated();
    71             }
       
    72         }
    75         }
    73       }
    76       }
    74 
    77 
    75     }
    78     }
    76     emit mouseEvent(ev->type() );
    79     emit mouseEvent(ev->type() );
    77   }
    80   }
    78 
    81 
    79   void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev )
    82   void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev )
    80   {
    83   {
       
    84 
       
    85     bool trigger = sceneBoundingRect().contains(ev->scenePos());
       
    86 
    81     QAction * action = defaultAction();
    87     QAction * action = defaultAction();
    82     //qDebug() << "ActionButton::mouseReleaseEvent " << m_snippet->elementId();
    88     //qDebug() << "ActionButton::mouseReleaseEvent " << m_snippet->elementId();
    83     if (m_triggerOn == ev->type()){
    89 
       
    90     if (   trigger && m_triggerOnUp == true) {
    84       if (ev->button() == Qt::LeftButton) {
    91       if (ev->button() == Qt::LeftButton) {
    85         if (action && action->isEnabled()){
    92         if (action && action->isEnabled()){
    86           action->trigger();
    93           action->trigger();
    87           emit activated();
    94           emit activated();
       
    95 
    88         }
    96         }
    89       }
    97       }
    90     }
    98     }
       
    99     // If m_activeOnPress is true, reset active flag to set icon state to Normal
       
   100     if (m_activeOnPress || !trigger)
       
   101       setActive(false);
    91     emit mouseEvent(ev->type() );
   102     emit mouseEvent(ev->type() );
    92   }
       
    93 
       
    94   void ActionButton::contextMenuEvent( QGraphicsSceneContextMenuEvent * ev )
       
    95   {
       
    96     Q_UNUSED(ev)
       
    97     emit contextMenuEvent();
       
    98   }
   103   }
    99 
   104 
   100   //Action buttons only have one action at a time, so whenever we add an action, we remove any previously set action
   105   //Action buttons only have one action at a time, so whenever we add an action, we remove any previously set action
   101   //NB: The action is typically one of the available actions on a view (via ControllableView.getContext()).
   106   //NB: The action is typically one of the available actions on a view (via ControllableView.getContext()).
   102   //ActionButtonSnippet provides the scriptable method connectAction() to create native connections to view actions
   107   //ActionButtonSnippet provides the scriptable method connectAction() to create native connections to view actions
   103 
   108 
   104   void ActionButton::setAction ( QAction * action, QEvent::Type triggerOn )
   109   void ActionButton::setAction ( QAction * action, bool triggerOnDown,  bool triggerOnUp )
   105   {
   110   {
   106     QAction * currentAction = defaultAction();
   111     QAction * currentAction = defaultAction();
   107     if (currentAction == action)
   112     if (currentAction == action)
   108       return;
   113       return;
   109     if (currentAction){
   114     if (currentAction){
   110       disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged()));
   115       disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged()));
   111       removeAction(currentAction);
   116       removeAction(currentAction);
   112     }
   117     }
   113     addAction(action);
   118     addAction(action);
   114     connect(action, SIGNAL(changed()),this, SLOT(onActionChanged()));
   119     connect(action, SIGNAL(changed()),this, SLOT(onActionChanged()));
   115     m_triggerOn = triggerOn;
   120     m_triggerOnUp = triggerOnUp;
       
   121     m_triggerOnDown = triggerOnDown;
   116 
   122 
   117 
   123 
   118     // Save the action as the internal action and set its properties
   124     // Save the action as the internal action and set its properties
   119     m_internalAction = action;
   125     m_internalAction = action;
   120     m_internalAction->setCheckable(true);
   126     m_internalAction->setCheckable(false);
   121 
   127 
   122     update();
   128     update();
   123   }
       
   124 
       
   125   void ActionButton::disconnectAction () {
       
   126     setAction(NULL);
       
   127   }
   129   }
   128 
   130 
   129   void ActionButton::setEnabled(bool enabled)
   131   void ActionButton::setEnabled(bool enabled)
   130   {
   132   {
   131     m_internalAction->setEnabled(enabled);
   133     m_internalAction->setEnabled(enabled);
   132   }
   134   }
   133 
   135 
   134   void ActionButton::setChecked(bool checked)
   136   void ActionButton::setActiveOnPress(bool active)
   135   {
   137   {
   136     m_internalAction->setChecked(checked);
   138     m_activeOnPress = active;
   137   }
   139   }
   138   
   140   
   139   void ActionButton::setActive(bool active)
   141   void ActionButton::setActive(bool active)
   140   {
   142   {
   141     m_active = active;
   143     if (m_active != active ) {
   142     update();
   144         m_active = active;
   143   }
   145         update();
   144 
   146     }
   145   void ActionButton::setInputEvent(QEvent::Type event)
       
   146   {
       
   147     m_triggerOn = event;
       
   148   }
   147   }
   149 
   148 
   150   //NB: handle icon on/off states too?
   149   //NB: handle icon on/off states too?
   151 
   150 
   152   void ActionButton::addIcon( const QString & resource, QIcon::Mode mode )
   151   void ActionButton::addIcon( const QString & resource, QIcon::Mode mode )
   179   void ActionButton::onShown(){
   178   void ActionButton::onShown(){
   180 
   179 
   181     QAction * action = defaultAction();
   180     QAction * action = defaultAction();
   182     if (action && action->isEnabled() && !action->isChecked()){
   181     if (action && action->isEnabled() && !action->isChecked()){
   183       action->setChecked(true);
   182       action->setChecked(true);
   184       update();
   183       setActive(true);
   185     }
   184     }
   186   }
   185   }
   187 
   186 
   188 
   187 
   189 }//end of name space
   188 }//end of name space