|
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 #include "qstmstateengine.h" |
|
19 #include "qstmuievent.h" |
|
20 |
|
21 #include "qstmfilelogger.h" |
|
22 |
|
23 using namespace qstmUiEventEngine; |
|
24 |
|
25 |
|
26 QStm_UiEventSender::QStm_UiEventSender() |
|
27 { |
|
28 m_loggingenabled = false ; |
|
29 for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) |
|
30 { |
|
31 m_events[i] = NULL ; |
|
32 } |
|
33 } |
|
34 |
|
35 QStm_UiEventSender::~QStm_UiEventSender() |
|
36 { |
|
37 // remove the possible events from the buffers if release was missed |
|
38 for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) |
|
39 { |
|
40 if (m_events[i] != NULL) delete m_events[i] ; |
|
41 } |
|
42 m_observers.clear(); |
|
43 } |
|
44 |
|
45 /*! |
|
46 * Add new UI event to the list or send it directly to the observers |
|
47 * depending on the m_directsending flag. |
|
48 * \param aUiEvent the new UI event to be sent to the observers. |
|
49 */ |
|
50 int QStm_UiEventSender::addEvent(QStm_UiEvent* uiEvent) |
|
51 { |
|
52 int pointerIndex = uiEvent->index() ; |
|
53 // Store the new UI event. Check what kind of event it is and compress the set of events |
|
54 // stored so far if possible |
|
55 compressStack(uiEvent) ; |
|
56 uiEvent->setPrevious(m_events[pointerIndex]) ; |
|
57 m_events[pointerIndex] = uiEvent ; // Store the new event |
|
58 // TESTING: logging the speed calculations |
|
59 if (m_loggingenabled) |
|
60 { |
|
61 uiEvent->logSpeed() ; |
|
62 } |
|
63 emitEvent(*uiEvent) ; |
|
64 |
|
65 if (m_loggingenabled) |
|
66 { |
|
67 LOGARG("Sent event: %s: (ptr %d) (%d,%d)", qstmUiEventEngine::event_name(uiEvent->code()), pointerIndex, |
|
68 uiEvent->currentXY().x(), uiEvent->currentXY().y()); |
|
69 } |
|
70 // If this was release event, then the chain can be removed |
|
71 if (uiEvent->code() == qstmUiEventEngine::ERelease) |
|
72 { |
|
73 delete uiEvent; // This will delete the whole chain |
|
74 m_events[pointerIndex] = NULL ; |
|
75 } |
|
76 return 0; |
|
77 } |
|
78 /*! |
|
79 * Call each observer with the event |
|
80 */ |
|
81 void QStm_UiEventSender::emitEvent(const QStm_UiEvent& event) |
|
82 { |
|
83 int count = m_observers.count(); |
|
84 for (int i = 0; i < count; i++) |
|
85 { |
|
86 m_observers[i]->handleUiEvent(event); |
|
87 } |
|
88 } |
|
89 |
|
90 /*! |
|
91 * Add a new observer. Note that current implementation is very rude: |
|
92 * max 5 observers in a simple array. |
|
93 */ |
|
94 bool QStm_UiEventSender::addObserver(QStm_UiEventObserverIf* observer ) |
|
95 { |
|
96 m_observers.append(observer) ; |
|
97 return true ; |
|
98 } |
|
99 |
|
100 // Check if the parameter refers to one of our observers |
|
101 bool QStm_UiEventSender::isObserver(void* aObserver ) |
|
102 { |
|
103 QObject* pccToCheck = (QObject*) aObserver ; |
|
104 int count = m_observers.count(); |
|
105 for (int i = 0; i < count; i++) |
|
106 { |
|
107 try |
|
108 { |
|
109 QObject* pcc = dynamic_cast<QObject*>(m_observers[i]) ; |
|
110 if (pcc == pccToCheck) |
|
111 return true ; |
|
112 } |
|
113 catch(...) |
|
114 { |
|
115 |
|
116 } |
|
117 } |
|
118 return false ; // Could not find observer |
|
119 } |
|
120 |
|
121 /* |
|
122 * remove observer from list |
|
123 */ |
|
124 bool QStm_UiEventSender::removeObserver(QStm_UiEventObserverIf* observer ) |
|
125 { |
|
126 int i = m_observers.indexOf(observer) ; |
|
127 if (i != -1) |
|
128 { |
|
129 m_observers.removeAt(i) ; |
|
130 return true ; |
|
131 |
|
132 } |
|
133 return false ; // Could not find observer |
|
134 } |
|
135 |
|
136 |
|
137 void QStm_UiEventSender::compressStack(QStm_UiEvent* uiEvent) |
|
138 { |
|
139 int pointerIndex = uiEvent->index() ; |
|
140 QStm_UiEvent*& top = m_events[pointerIndex] ; |
|
141 if(!top) |
|
142 { |
|
143 return; |
|
144 } |
|
145 if (uiEvent->code() == qstmUiEventEngine::EHold) |
|
146 { |
|
147 // assumption: in case of hold, we can discard all previous messages |
|
148 delete top ; |
|
149 top = NULL ; |
|
150 } |
|
151 else |
|
152 { |
|
153 // Check if there would too many moves |
|
154 QStm_UiEvent* next = dynamic_cast<QStm_UiEvent*>(top->previousEvent()) ; |
|
155 if (next != 0 && next->code() == qstmUiEventEngine::EMove) |
|
156 { |
|
157 // leave only the topmost to the stack |
|
158 top->setPrevious(0) ; |
|
159 delete next ; |
|
160 } |
|
161 } |
|
162 } |
|
163 |