1 /* |
1 /* |
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
3 * All rights reserved. |
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 * |
4 * |
9 * Initial Contributors: |
5 * This program is free software: you can redistribute it and/or modify |
10 * Nokia Corporation - initial contribution. |
6 * it under the terms of the GNU Lesser General Public License as published by |
|
7 * the Free Software Foundation, version 2.1 of the License. |
11 * |
8 * |
12 * Contributors: |
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU Lesser General Public License for more details. |
13 * |
13 * |
14 * Description: |
14 * You should have received a copy of the GNU Lesser General Public License |
|
15 * along with this program. If not, |
|
16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
17 * |
|
18 * Description: |
15 * |
19 * |
16 */ |
20 */ |
17 |
|
18 |
21 |
19 #include "ActionButton.h" |
22 #include "ActionButton.h" |
20 #include <QDebug> |
23 #include <QDebug> |
21 |
24 |
22 namespace GVA { |
25 namespace GVA { |
23 |
26 |
24 ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent) |
27 ActionButton::ActionButton(ChromeSnippet * snippet, QGraphicsItem* parent) |
25 : NativeChromeItem(snippet, parent), |
28 : NativeChromeItem(snippet, parent), |
26 m_internalAction(new QAction(this)), |
29 m_internalAction(NULL), |
27 m_triggerOn(QEvent::GraphicsSceneMousePress), |
30 m_triggerOnUp(true), |
28 m_active(false) |
31 m_triggerOnDown(false), |
|
32 m_active(false), |
|
33 m_activeOnPress(true) |
29 { |
34 { |
30 m_internalAction->setCheckable(true); |
|
31 m_internalAction->setEnabled(true); |
|
32 setAction(m_internalAction); |
|
33 |
35 |
34 //Just testing . . . |
36 |
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 } |
37 } |
39 |
38 |
40 void ActionButton::paint( QPainter * painter, const QStyleOptionGraphicsItem * opt, QWidget * widget ) |
39 void ActionButton::paint( QPainter * painter, const QStyleOptionGraphicsItem * opt, QWidget * widget ) |
41 { |
40 { |
42 Q_UNUSED(opt) |
41 Q_UNUSED(opt) |
43 Q_UNUSED(widget) |
42 Q_UNUSED(widget) |
44 |
43 |
45 painter->save(); |
44 painter->save(); |
46 QAction * action = defaultAction(); |
45 QAction * action = defaultAction(); |
47 QIcon::Mode mode = QIcon::Normal; |
46 QIcon::Mode mode = QIcon::Normal; |
48 if(m_active) |
47 |
49 mode = QIcon::Active; |
48 if (action) { |
50 else if(action){ |
49 if (m_active) { |
51 if(action->isChecked()) |
50 mode = QIcon::Active; |
52 mode = QIcon::Selected; |
51 } |
53 else if(!action->isEnabled()) |
52 else if (!action->isEnabled()) { |
54 mode = QIcon::Disabled; |
53 mode = QIcon::Disabled; |
|
54 } |
55 } |
55 } |
56 m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On); |
56 m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, mode, QIcon::On); |
57 painter->restore(); |
57 painter->restore(); |
|
58 NativeChromeItem::paint(painter, opt, widget); |
58 } |
59 } |
59 |
60 |
60 void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev ) |
61 void ActionButton::mousePressEvent( QGraphicsSceneMouseEvent * ev ) |
61 { |
62 { |
62 if(m_triggerOn == ev->type()){ |
63 |
63 if (ev->button() == Qt::LeftButton) { |
64 QAction * action = defaultAction(); |
64 QAction * action = defaultAction(); |
65 if (action && (action->isEnabled()) ) { |
65 if(action && action->isEnabled()){ |
66 //qDebug() << "mousePressEvent" << m_triggerOnDown << ev->type(); |
66 action->trigger(); |
67 // If m_activeOnPress is true, set active flag to set icon state to Selected |
67 emit activated(); |
68 if (m_activeOnPress ) |
68 } |
69 setActive(true); |
|
70 if (m_triggerOnDown == true) { |
|
71 if (ev->button() == Qt::LeftButton) { |
|
72 |
|
73 action->trigger(); |
|
74 emit activated(); |
|
75 } |
69 } |
76 } |
70 m_active = true; |
77 |
71 } |
78 } |
|
79 emit mouseEvent(ev->type() ); |
72 } |
80 } |
73 |
81 |
74 void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev ) |
82 void ActionButton::mouseReleaseEvent( QGraphicsSceneMouseEvent * ev ) |
75 { |
83 { |
76 if(m_triggerOn == ev->type()){ |
84 |
|
85 bool trigger = sceneBoundingRect().contains(ev->scenePos()); |
|
86 |
|
87 QAction * action = defaultAction(); |
|
88 //qDebug() << "ActionButton::mouseReleaseEvent " << m_snippet->elementId(); |
|
89 |
|
90 if ( trigger && m_triggerOnUp == true) { |
77 if (ev->button() == Qt::LeftButton) { |
91 if (ev->button() == Qt::LeftButton) { |
78 QAction * action = defaultAction(); |
92 if (action && action->isEnabled()){ |
79 if(action && action->isEnabled()){ |
93 action->trigger(); |
80 action->trigger(); |
94 emit activated(); |
81 emit activated(); |
95 |
82 } |
96 } |
83 } |
97 } |
84 m_active = false; |
|
85 } |
98 } |
86 } |
99 // If m_activeOnPress is true, reset active flag to set icon state to Normal |
87 |
100 if (m_activeOnPress || !trigger) |
88 void ActionButton::contextMenuEvent( QGraphicsSceneContextMenuEvent * ev ) |
101 setActive(false); |
89 { |
102 emit mouseEvent(ev->type() ); |
90 Q_UNUSED(ev) |
|
91 emit contextMenuEvent(); |
|
92 } |
103 } |
93 |
104 |
94 //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 |
95 //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()). |
96 //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 |
97 |
108 |
98 void ActionButton::setAction ( QAction * action, QEvent::Type triggerOn ) |
109 void ActionButton::setAction ( QAction * action, bool triggerOnDown, bool triggerOnUp ) |
99 { |
110 { |
100 QAction * currentAction = defaultAction(); |
111 QAction * currentAction = defaultAction(); |
101 if(currentAction == action) |
112 if (currentAction == action) |
102 return; |
113 return; |
103 if(currentAction){ |
114 if (currentAction){ |
104 disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged())); |
115 disconnect(currentAction, SIGNAL(changed()), this, SLOT(onActionChanged())); |
105 removeAction(currentAction); |
116 removeAction(currentAction); |
106 } |
117 } |
107 addAction(action); |
118 addAction(action); |
108 connect(action, SIGNAL(changed()),this, SLOT(onActionChanged())); |
119 connect(action, SIGNAL(changed()),this, SLOT(onActionChanged())); |
109 m_triggerOn = triggerOn; |
120 m_triggerOnUp = triggerOnUp; |
|
121 m_triggerOnDown = triggerOnDown; |
|
122 |
|
123 |
|
124 // Save the action as the internal action and set its properties |
|
125 m_internalAction = action; |
|
126 m_internalAction->setCheckable(false); |
|
127 |
110 update(); |
128 update(); |
111 } |
|
112 |
|
113 void ActionButton::disconnectAction () { |
|
114 setAction(m_internalAction); |
|
115 } |
129 } |
116 |
130 |
117 void ActionButton::setEnabled(bool enabled) |
131 void ActionButton::setEnabled(bool enabled) |
118 { |
132 { |
119 m_internalAction->setEnabled(enabled); |
133 m_internalAction->setEnabled(enabled); |
120 } |
134 } |
121 |
135 |
122 void ActionButton::setChecked(bool checked) |
136 void ActionButton::setActiveOnPress(bool active) |
123 { |
137 { |
124 m_internalAction->setChecked(checked); |
138 m_activeOnPress = active; |
125 } |
139 } |
126 |
140 |
127 void ActionButton::setInputEvent(QEvent::Type event) |
141 void ActionButton::setActive(bool active) |
128 { |
142 { |
129 m_triggerOn = event; |
143 if (m_active != active ) { |
|
144 m_active = active; |
|
145 update(); |
|
146 } |
130 } |
147 } |
131 |
148 |
132 //NB: handle icon on/off states too? |
149 //NB: handle icon on/off states too? |
133 |
150 |
134 void ActionButton::addIcon( const QString & resource, QIcon::Mode mode ) |
151 void ActionButton::addIcon( const QString & resource, QIcon::Mode mode ) |
135 { |
152 { |
136 m_icon.addPixmap( QPixmap(resource), mode, QIcon::On ); |
153 m_icon.addPixmap( QPixmap(resource), mode, QIcon::On ); |
137 } |
154 } |
138 |
155 |
139 QAction * ActionButton::defaultAction() |
156 QAction * ActionButton::defaultAction() |
140 { |
157 { |
141 if (actions().isEmpty()) |
158 if (actions().isEmpty()) |
142 return 0; |
159 return 0; |