1 | /** |
---|
2 | * @file mythstroke.h |
---|
3 | * @author Micah F. Galizia <mfgalizi@csd.uwo.ca> |
---|
4 | * @brief A C++ ripoff of the stroke library for MythTV. |
---|
5 | * |
---|
6 | * Copyright (C) 2005 Micah Galizia |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License as |
---|
10 | * published by the Free Software Foundation; either version 2, or (at |
---|
11 | * your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, but |
---|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | * 02111-1307, USA |
---|
22 | */ |
---|
23 | #ifndef MYTHSTROKE_H |
---|
24 | #define MYTHSTROKE_H |
---|
25 | |
---|
26 | #include <sys/types.h> |
---|
27 | |
---|
28 | #include <qpoint.h> |
---|
29 | #include <qvaluelist.h> |
---|
30 | #include <qevent.h> |
---|
31 | |
---|
32 | const int MythGestureEventType = 24427; |
---|
33 | |
---|
34 | class MythGestureEvent : public QCustomEvent |
---|
35 | { |
---|
36 | |
---|
37 | public: |
---|
38 | |
---|
39 | /** |
---|
40 | * @brief The types of gestures supported by myth |
---|
41 | */ |
---|
42 | enum Gesture { |
---|
43 | |
---|
44 | /* Couldn't figure out that one */ |
---|
45 | Unknown, |
---|
46 | |
---|
47 | /* A click */ |
---|
48 | Click, |
---|
49 | |
---|
50 | /* Horizontal and vertical lines */ |
---|
51 | Up, |
---|
52 | Down, |
---|
53 | Left, |
---|
54 | Right, |
---|
55 | |
---|
56 | /* Diagonal lines */ |
---|
57 | UpLeft, |
---|
58 | UpRight, |
---|
59 | DownLeft, |
---|
60 | DownRight, |
---|
61 | |
---|
62 | /* Two Lines */ |
---|
63 | UpThenLeft, |
---|
64 | UpThenRight, |
---|
65 | DownThenLeft, |
---|
66 | DownThenRight, |
---|
67 | LeftThenUp, |
---|
68 | LeftThenDown, |
---|
69 | RightThenUp, |
---|
70 | RightThenDown, |
---|
71 | |
---|
72 | /* This isn't real */ |
---|
73 | MaxGesture |
---|
74 | }; |
---|
75 | |
---|
76 | /** |
---|
77 | * @brief Create a myth gesture. |
---|
78 | * @param type The gesture type, as per the Type enumeration. |
---|
79 | * @sa Type |
---|
80 | */ |
---|
81 | inline MythGestureEvent(size_t gesture):QCustomEvent(MythGestureEventType) |
---|
82 | { |
---|
83 | (gesture >= MaxGesture) ? _gesture = Unknown : _gesture = gesture; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief Get the gesture type. |
---|
88 | * @return The gesture value corresponding to the Gesture |
---|
89 | * enumeration. |
---|
90 | */ |
---|
91 | inline int gesture(void) const { return this->_gesture; } |
---|
92 | |
---|
93 | /** |
---|
94 | * @brief Get the symbolic name of the gesture. |
---|
95 | * @return A string containing the symbolic name of the gesture. |
---|
96 | */ |
---|
97 | operator QString() const; |
---|
98 | |
---|
99 | private: |
---|
100 | size_t _gesture; |
---|
101 | }; |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * @class MythStroke |
---|
107 | * @brief Contains the points in a stroke, and translates them into |
---|
108 | * gestures. |
---|
109 | */ |
---|
110 | class MythStroke |
---|
111 | { |
---|
112 | public: |
---|
113 | |
---|
114 | /** |
---|
115 | * @brief Create a new stroke, specifying tuning values |
---|
116 | * |
---|
117 | * @param max_points The maximum number of points to record. |
---|
118 | * @param min_points The minimum number of points to record. |
---|
119 | * @param max_sequence The maximum producible sequence size. |
---|
120 | * @param scale_ratio The stroke scale ratio |
---|
121 | * @param bin_percent The bin count percentage required |
---|
122 | * to add to the sequence. |
---|
123 | */ |
---|
124 | MythStroke(size_t max_points = 10000, size_t min_points = 50, |
---|
125 | size_t max_sequence = 20, size_t scale_ratio = 4, |
---|
126 | float bin_percent = 0.07); |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief Start recording. |
---|
130 | */ |
---|
131 | inline void start(void) { this->recording = true; } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief Complete the stroke. |
---|
136 | * @return A new gesture event, or NULL on error. |
---|
137 | * |
---|
138 | * After a stroke is completed, it will no longer record. |
---|
139 | */ |
---|
140 | MythGestureEvent *complete(); |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Record a point. |
---|
144 | * @param p The point to record. |
---|
145 | * @return True if the point was recorded, otherwise, false. |
---|
146 | */ |
---|
147 | bool record(const QPoint &p); |
---|
148 | |
---|
149 | /** |
---|
150 | * @brief Determine if the stroke has the minimum required points. |
---|
151 | * @return true if the gesture can be translated, otherwise, false. |
---|
152 | */ |
---|
153 | bool hasMinimumPoints(void) const { return points.size() >= min_points; } |
---|
154 | |
---|
155 | protected: |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief Translate the stroke into a sequence. |
---|
159 | * @return The sequence string made by the mouse. |
---|
160 | * |
---|
161 | * @note The points will be removed during this method. |
---|
162 | */ |
---|
163 | QString translate(void); |
---|
164 | |
---|
165 | /** |
---|
166 | * @brief Adjust horizontal and vertical extremes. |
---|
167 | * @param x The new horizontal extreme. |
---|
168 | * @param y The new vertical extreme |
---|
169 | */ |
---|
170 | void adjustExtremes(int x, int y); |
---|
171 | |
---|
172 | private: |
---|
173 | |
---|
174 | bool recording; |
---|
175 | int min_x; |
---|
176 | int max_x; |
---|
177 | int min_y; |
---|
178 | int max_y; |
---|
179 | size_t max_points; |
---|
180 | size_t min_points; |
---|
181 | size_t max_sequence; |
---|
182 | int scale_ratio; |
---|
183 | float bin_percent; |
---|
184 | QValueList <QPoint> points; |
---|
185 | QMap <QString, MythGestureEvent::Gesture> sequences; |
---|
186 | }; |
---|
187 | |
---|
188 | #endif /* MYTHSTROKE_H */ |
---|