source: eviacam/trunk/fuentes/src/hotkeymanager.cpp @ 567

Last change on this file since 567 was 567, checked in by mabarracus, 4 years ago

updated sources to version 2.0.3

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        hotkeymanager.cpp
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     30/12/2010
7// Copyright:   (C) 2010-11 Cesar Mauri Loba - CREA Software Systems
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 3 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21/////////////////////////////////////////////////////////////////////////////
22#include "hotkeymanager.h"
23#include "eviacamdefs.h"
24#include "eviacamapp.h"
25#include "viacamcontroller.h"
26#include "pointeraction.h"
27
28class CKeyCommandEnable : public CKeyCommand {
29public:
30        CKeyCommandEnable()
31        {
32                SetName(_T("hotKeyEnable"));
33                SetDescription(_("Enable eViacam"));
34                SetKey(CKeyboardCode::FromWXKeyCode(WXK_SCROLL));
35                SetEnabled(true);
36        }
37
38        void Command()
39        {
40                wxGetApp().GetController().SetEnabled(!wxGetApp().GetController().GetEnabled(), true);
41        }
42};
43
44class CKeyCommandWorkspace : public CKeyCommand {
45public:
46        CKeyCommandWorkspace()
47        {
48                SetName(_T("hotKeyWorkspace"));
49                SetDescription(_("Enable workspace limit"));
50                SetKey(CKeyboardCode::FromWXKeyCode(WXK_F12));
51                SetEnabled(false);
52        }
53
54        void Command()
55        {
56                wxGetApp().GetController().GetPointerAction().SetRestrictedWorkingArea(!wxGetApp().GetController().GetPointerAction().GetRestrictedWorkingArea());
57        }
58};
59
60class CKeyCommandCenterPointer : public CKeyCommand {
61public:
62        CKeyCommandCenterPointer()
63        {
64                SetName(_T("hotKeyCenterPointer"));
65                SetDescription(_("Center the pointer"));
66                SetKey(CKeyboardCode::FromWXKeyCode(WXK_HOME));
67                SetEnabled(true);
68        }
69
70        void Command()
71        {
72                wxGetApp().GetController().GetPointerAction().CenterPointer();
73        }
74};
75
76class CKeyCommandIncreaseX : public CKeyCommand {
77public:
78        CKeyCommandIncreaseX()
79        {
80                SetName(_T("hotKeyIncreaseX"));
81                SetDescription(_("Increase the X axis speed"));
82                SetKey(CKeyboardCode::FromWXKeyCode (WXK_RIGHT));
83                SetEnabled(false);
84        }
85
86        void Command()
87        {
88                wxGetApp().GetController().GetPointerAction().SetXSpeed(wxGetApp().GetController().GetPointerAction().GetXSpeed()+1);
89        }
90};
91
92class CKeyCommandIncreaseY : public CKeyCommand {
93public:
94        CKeyCommandIncreaseY()
95        {
96                SetName(_T("hotKeyIncreaseY"));
97                SetDescription(_("Increase the Y axis speed"));
98                SetKey(CKeyboardCode::FromWXKeyCode (WXK_UP));
99                SetEnabled(false);
100        }
101
102        void Command()
103        {
104                wxGetApp().GetController().GetPointerAction().SetYSpeed(wxGetApp().GetController().GetPointerAction().GetYSpeed()+1);
105        }
106};
107
108class CKeyCommandDecreaseX : public CKeyCommand {
109public:
110        CKeyCommandDecreaseX()
111        {
112                SetName(_T("hotKeyDecreaseX"));
113                SetDescription(_("Decrease the X axis speed"));
114                SetKey(CKeyboardCode::FromWXKeyCode (WXK_LEFT));
115                SetEnabled(false);     
116        }
117
118        void Command()
119        {
120                wxGetApp().GetController().GetPointerAction().SetXSpeed(wxGetApp().GetController().GetPointerAction().GetXSpeed()-1);
121        }
122};
123
124class CKeyCommandDecreaseY : public CKeyCommand {
125public:
126        CKeyCommandDecreaseY()
127        {
128                SetName(_T("hotKeyDecreaseY"));
129                SetDescription(_("Decrease the Y axis speed"));
130                SetKey(CKeyboardCode::FromWXKeyCode (WXK_DOWN));
131                SetEnabled(false);
132        }
133
134        void Command()
135        {
136                wxGetApp().GetController().GetPointerAction().SetYSpeed(wxGetApp().GetController().GetPointerAction().GetYSpeed()-1);
137        }
138};
139
140
141// TODO: to avoid problems with synchronization, implement this by deriving
142// from wxEvtHandler and sending hot-key messages using wxPostEvent. This
143// way we make sure that all hot-key driven actions are always executed from
144// the main thread. This approach will also make easier the transition to
145// an implementation based on keyboard hooks
146
147CHotkeyManager::CHotkeyManager() : m_lastKeyCode()
148{
149        InitDefaults();
150}
151
152CHotkeyManager::~CHotkeyManager()
153{
154        while (!m_keyCommands.empty()) m_keyCommands.pop_back();
155}
156
157void CHotkeyManager::InitDefaults()
158{
159#if defined(__WXGTK__)
160        m_keyCommands.push_back(new CKeyCommandEnable());
161        m_keyCommands.push_back(new CKeyCommandWorkspace());
162        m_keyCommands.push_back(new CKeyCommandCenterPointer());
163        m_keyCommands.push_back(new CKeyCommandIncreaseX());
164        m_keyCommands.push_back(new CKeyCommandIncreaseY());
165        m_keyCommands.push_back(new CKeyCommandDecreaseX());
166        m_keyCommands.push_back(new CKeyCommandDecreaseY());
167#endif // __WXGTK___
168}
169
170void CHotkeyManager::CheckKeyboardStatus()
171{
172#if defined(__WXGTK__)
173        BEGIN_GUI_CALL_MUTEX()
174        CKeyboardCode kc = CKeyboardCode::ReadKeyCode();
175        if (kc!= m_lastKeyCode) {
176                m_lastKeyCode= kc;
177                int index= IsKeyUsed(kc);
178                if (index != -1 and m_keyCommands[index]->IsEnabled())
179                        m_keyCommands[index]->Command();
180        }
181        END_GUI_CALL_MUTEX()
182#endif // __WXGTK___
183}
184
185
186void CHotkeyManager::WriteProfileData(wxConfigBase* pConfObj)
187{
188        pConfObj->SetPath (_T("hotKeyManager"));
189#if defined(__WXGTK__)
190        for (unsigned int i=0; i<m_keyCommands.size(); i++) {
191                pConfObj->Write(m_keyCommands[i]->GetName(), m_keyCommands[i]->IsEnabled());
192                pConfObj->Write(m_keyCommands[i]->GetName() + _T("Key"), static_cast<long>(m_keyCommands[i]->GetKey().GetRawValue()));
193        }
194#endif // __WXGTK___
195        pConfObj->SetPath (_T(".."));
196} 
197
198void CHotkeyManager::ReadProfileData(wxConfigBase* pConfObj)
199{
200        pConfObj->SetPath (_T("hotKeyManager"));
201#if defined(__WXGTK__)
202        bool isEnabled;
203        long rawKeyCode;
204       
205        for (unsigned int i=0; i<m_keyCommands.size(); i++) {
206                if(pConfObj->Read(m_keyCommands[i]->GetName(), &isEnabled))
207                        m_keyCommands[i]->SetEnabled(isEnabled);
208               
209                if (pConfObj->Read(m_keyCommands[i]->GetName() + _T("Key"), &rawKeyCode))
210                        m_keyCommands[i]->SetKey(CKeyboardCode::FromRawValue(static_cast<unsigned long>(rawKeyCode)));
211        }
212#endif // __WXGTK___
213        pConfObj->SetPath (_T(".."));
214}
215
216int CHotkeyManager::IsKeyUsed (CKeyboardCode kc)
217{
218        for (unsigned int i=0; i<m_keyCommands.size(); i++)
219        {
220                if (m_keyCommands[i]->GetKey() == kc)
221                        return i;
222        }
223        return -1;
224}
225
226bool CHotkeyManager::SetKeyCommand (unsigned int index, CKeyboardCode kc)
227{
228        assert (index < m_keyCommands.size());
229        if (IsKeyUsed(kc) == -1) {
230                m_keyCommands[index]->SetKey(kc);
231                return true;
232        } else {
233                return false;
234        }
235}
Note: See TracBrowser for help on using the repository browser.