Diffstat (limited to 'core/apps/calibrate/calibrate.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | core/apps/calibrate/calibrate.cpp | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/core/apps/calibrate/calibrate.cpp b/core/apps/calibrate/calibrate.cpp index 54fa8ec..1f4fb1b 100644 --- a/core/apps/calibrate/calibrate.cpp +++ b/core/apps/calibrate/calibrate.cpp | |||
@@ -88,31 +88,63 @@ void Calibrate::reset() | |||
88 | penPos = QPoint(); | 88 | penPos = QPoint(); |
89 | location = QWSPointerCalibrationData::TopLeft; | 89 | location = QWSPointerCalibrationData::TopLeft; |
90 | crossPos = fromDevice( cd.screenPoints[ location ] ); | 90 | crossPos = fromDevice( cd.screenPoints[ location ] ); |
91 | } | 91 | } |
92 | 92 | ||
93 | QPoint Calibrate::fromDevice( const QPoint &p ) | 93 | QPoint Calibrate::fromDevice( const QPoint &p ) |
94 | { | 94 | { |
95 | return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) ); | 95 | return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) ); |
96 | } | 96 | } |
97 | 97 | ||
98 | bool Calibrate::sanityCheck() | 98 | bool Calibrate::sanityCheck() |
99 | { | 99 | { |
100 | QRect r ( cd.devPoints[ QWSPointerCalibrationData::TopLeft ], cd.devPoints[ QWSPointerCalibrationData::BottomRight ] ); | 100 | QPoint tl = cd.devPoints[QWSPointerCalibrationData::TopLeft]; |
101 | r = r. normalize ( ); // This should also handle rotated TS controllers | 101 | QPoint tr = cd.devPoints[QWSPointerCalibrationData::TopRight]; |
102 | 102 | QPoint bl = cd.devPoints[QWSPointerCalibrationData::BottomLeft]; | |
103 | cd. devPoints [QWSPointerCalibrationData::TopRight] = r. topRight ( ); | 103 | QPoint br = cd.devPoints[QWSPointerCalibrationData::BottomRight]; |
104 | cd. devPoints [QWSPointerCalibrationData::BottomLeft] = r. bottomLeft ( ); | 104 | |
105 | cd. devPoints [QWSPointerCalibrationData::Center] = r. center ( ); | 105 | // not needed anywhere .. just calculate it, so it's there |
106 | 106 | cd. devPoints [QWSPointerCalibrationData::Center] = QRect ( tl, br ). normalize ( ). center ( ); | |
107 | |||
108 | int dlx = QABS( bl. x ( ) - tl. x ( )); | ||
109 | int dly = QABS( bl. y ( ) - tl. y ( )); | ||
110 | int drx = QABS( br. x ( ) - tr. x ( )); | ||
111 | int dry = QABS( br. y ( ) - tr. y ( )); | ||
112 | int dtx = QABS( tr. x ( ) - tl. x ( )); | ||
113 | int dty = QABS( tr. y ( ) - tl. y ( )); | ||
114 | int dbx = QABS( br. x ( ) - bl. x ( )); | ||
115 | int dby = QABS( br. y ( ) - bl. y ( )); | ||
116 | |||
117 | int dl = (int) ::sqrt (( dlx * dlx ) + ( dly * dly )); // calculate vector lengths for all sides | ||
118 | int dr = (int) ::sqrt (( drx * drx ) + ( dry * dry )); | ||
119 | int dt = (int) ::sqrt (( dtx * dtx ) + ( dty * dty )); | ||
120 | int db = (int) ::sqrt (( dbx * dbx ) + ( dby * dby )); | ||
121 | |||
122 | // Calculate leeway for x/y (we do not care if diff1/diff2 is for x or y here !) | ||
123 | int diff1 = QABS( dl - dr ); | ||
124 | int avg1 = ( dl + dr ) / 2; | ||
125 | int diff2 = QABS( dt - db ); | ||
126 | int avg2 = ( dt + db ) / 2; | ||
127 | |||
128 | // Calculate leeway for "real" vector length against "manhattan" vector length | ||
129 | // This is a check, if the rect is rotated (other then 0/90/180/270) | ||
130 | // It needs to be performed only for the triange (bl, tl, tr) | ||
131 | int diff3 = QABS(( dlx + dly + dtx + dty ) - ( dl + dt )); | ||
132 | int avg3 = (( dlx + dly + dtx + dty ) + ( dl + dt )) / 2; | ||
133 | |||
134 | if (( diff1 > ( avg1 / 20 )) || // 5% leeway | ||
135 | ( diff2 > ( avg2 / 20 )) || | ||
136 | ( diff3 > ( avg3 / 20 ))) | ||
137 | return false; | ||
138 | else | ||
107 | return true; | 139 | return true; |
108 | } | 140 | } |
109 | 141 | ||
110 | void Calibrate::moveCrosshair( QPoint pt ) | 142 | void Calibrate::moveCrosshair( QPoint pt ) |
111 | { | 143 | { |
112 | showCross = FALSE; | 144 | showCross = FALSE; |
113 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); | 145 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
114 | showCross = TRUE; | 146 | showCross = TRUE; |
115 | crossPos = pt; | 147 | crossPos = pt; |
116 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); | 148 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
117 | } | 149 | } |
118 | 150 | ||
@@ -157,26 +189,26 @@ void Calibrate::mousePressEvent( QMouseEvent *e ) | |||
157 | penPos = QPoint( ( penPos.x() + devPos.x() ) / 2, | 189 | penPos = QPoint( ( penPos.x() + devPos.x() ) / 2, |
158 | ( penPos.y() + devPos.y() ) / 2 ); | 190 | ( penPos.y() + devPos.y() ) / 2 ); |
159 | } | 191 | } |
160 | 192 | ||
161 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) | 193 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) |
162 | { | 194 | { |
163 | if ( timer->isActive() ) | 195 | if ( timer->isActive() ) |
164 | return ; | 196 | return ; |
165 | 197 | ||
166 | bool doMove = TRUE; | 198 | bool doMove = TRUE; |
167 | 199 | ||
168 | cd.devPoints[ location ] = penPos; | 200 | cd.devPoints[ location ] = penPos; |
169 | if ( location == QWSPointerCalibrationData::TopLeft ) { | 201 | if ( location < QWSPointerCalibrationData::TopRight ) { |
170 | location = QWSPointerCalibrationData::BottomRight; | 202 | location = (QWSPointerCalibrationData::Location) ( int( location ) + 1 ); |
171 | } | 203 | } |
172 | else { | 204 | else { |
173 | if ( sanityCheck() ) { | 205 | if ( sanityCheck() ) { |
174 | reset(); | 206 | reset(); |
175 | goodcd = cd; | 207 | goodcd = cd; |
176 | hide(); | 208 | hide(); |
177 | emit accept(); | 209 | emit accept(); |
178 | doMove = FALSE; | 210 | doMove = FALSE; |
179 | } | 211 | } |
180 | else { | 212 | else { |
181 | location = QWSPointerCalibrationData::TopLeft; | 213 | location = QWSPointerCalibrationData::TopLeft; |
182 | } | 214 | } |