Changeset View
Changeset View
Standalone View
Standalone View
tools/polygonal/kpToolPolygonalBase.cpp
Show First 20 Lines • Show All 135 Lines • ▼ Show 20 Line(s) | 129 | { | |||
---|---|---|---|---|---|
136 | 136 | | |||
137 | viewManager ()->unsetCursor (); | 137 | viewManager ()->unsetCursor (); | ||
138 | } | 138 | } | ||
139 | 139 | | |||
140 | 140 | | |||
141 | void kpToolPolygonalBase::beginDraw () | 141 | void kpToolPolygonalBase::beginDraw () | ||
142 | { | 142 | { | ||
143 | qCDebug(kpLogTools) << "kpToolPolygonalBase::beginDraw() d->points=" << d->points.toList () | 143 | qCDebug(kpLogTools) << "kpToolPolygonalBase::beginDraw() d->points=" << d->points.toList () | ||
144 | << ", startPoint=" << startPoint () << endl; | 144 | << ", startPoint=" << startPoint (); | ||
145 | 145 | | |||
146 | bool endedShape = false; | 146 | bool endedShape = false; | ||
147 | 147 | | |||
148 | // We now need to start with dragging out the initial line? | 148 | // We now need to start with dragging out the initial line? | ||
149 | if (d->points.count () == 0) | 149 | if (d->points.count () == 0) | ||
150 | { | 150 | { | ||
151 | d->originatingMouseButton = mouseButton (); | 151 | d->originatingMouseButton = mouseButton (); | ||
152 | 152 | | |||
Show All 39 Lines | 189 | { | |||
192 | QPoint &lineStartPoint = d->points [count - 2]; | 192 | QPoint &lineStartPoint = d->points [count - 2]; | ||
193 | QPoint &lineEndPoint = d->points [count - 1]; | 193 | QPoint &lineEndPoint = d->points [count - 1]; | ||
194 | 194 | | |||
195 | qCDebug(kpLogTools) << "kpToolPolygonalBase::applyModifiers() #pts=" << count | 195 | qCDebug(kpLogTools) << "kpToolPolygonalBase::applyModifiers() #pts=" << count | ||
196 | << " line: startPt=" << lineStartPoint | 196 | << " line: startPt=" << lineStartPoint | ||
197 | << " endPt=" << lineEndPoint | 197 | << " endPt=" << lineEndPoint | ||
198 | << " modifiers: shift=" << shiftPressed () | 198 | << " modifiers: shift=" << shiftPressed () | ||
199 | << " alt=" << altPressed () | 199 | << " alt=" << altPressed () | ||
200 | << " ctrl=" << controlPressed () | 200 | << " ctrl=" << controlPressed (); | ||
201 | << endl; | | |||
202 | 201 | | |||
203 | // angles | 202 | // angles | ||
204 | if (shiftPressed () || controlPressed ()) | 203 | if (shiftPressed () || controlPressed ()) | ||
205 | { | 204 | { | ||
206 | int diffx = lineEndPoint.x () - lineStartPoint.x (); | 205 | int diffx = lineEndPoint.x () - lineStartPoint.x (); | ||
207 | int diffy = lineEndPoint.y () - lineStartPoint.y (); | 206 | int diffy = lineEndPoint.y () - lineStartPoint.y (); | ||
208 | 207 | | |||
209 | double ratio; | 208 | double ratio; | ||
210 | if (diffx == 0) | 209 | if (diffx == 0) { | ||
211 | ratio = DBL_MAX; | 210 | ratio = DBL_MAX; | ||
212 | else | 211 | } | ||
212 | else { | ||||
213 | ratio = fabs (double (diffy) / double (diffx)); | 213 | ratio = fabs (double (diffy) / double (diffx)); | ||
214 | qCDebug(kpLogTools) << "\tdiffx=" << diffx << " diffy=" << diffy | 214 | } | ||
215 | << " ratio=" << ratio | 215 | qCDebug(kpLogTools) << "\tdiffx=" << diffx << " diffy=" << diffy << " ratio=" << ratio; | ||
216 | << endl; | | |||
217 | 216 | | |||
218 | // Shift = 0, 45, 90 | 217 | // Shift = 0, 45, 90 | ||
219 | // Ctrl = 0, 30, 60, 90 | 218 | // Ctrl = 0, 30, 60, 90 | ||
220 | // Shift + Ctrl = 0, 30, 45, 60, 90 | 219 | // Shift + Ctrl = 0, 30, 45, 60, 90 | ||
221 | double angles [10]; // "ought to be enough for anybody" | 220 | double angles [10]; // "ought to be enough for anybody" | ||
222 | int numAngles = 0; | 221 | int numAngles = 0; | ||
223 | angles [numAngles++] = 0; | 222 | angles [numAngles++] = 0; | ||
224 | if (controlPressed ()) | 223 | if (controlPressed ()) { | ||
225 | angles [numAngles++] = M_PI / 6; | 224 | angles [numAngles++] = M_PI / 6; | ||
226 | if (shiftPressed ()) | 225 | } | ||
226 | if (shiftPressed ()) { | ||||
227 | angles [numAngles++] = M_PI / 4; | 227 | angles [numAngles++] = M_PI / 4; | ||
228 | if (controlPressed ()) | 228 | } | ||
229 | if (controlPressed ()) { | ||||
229 | angles [numAngles++] = M_PI / 3; | 230 | angles [numAngles++] = M_PI / 3; | ||
231 | } | ||||
230 | angles [numAngles++] = M_PI / 2; | 232 | angles [numAngles++] = M_PI / 2; | ||
231 | Q_ASSERT (numAngles <= int (sizeof (angles) / sizeof (angles [0]))); | 233 | Q_ASSERT (numAngles <= int (sizeof (angles) / sizeof (angles [0]))); | ||
232 | 234 | | |||
233 | double angle = angles [numAngles - 1]; | 235 | double angle = angles [numAngles - 1]; | ||
234 | for (int i = 0; i < numAngles - 1; i++) | 236 | for (int i = 0; i < numAngles - 1; i++) | ||
235 | { | 237 | { | ||
236 | double acceptingRatio = std::tan ((angles [i] + angles [i + 1]) / 2.0); | 238 | double acceptingRatio = std::tan ((angles [i] + angles [i + 1]) / 2.0); | ||
237 | if (ratio < acceptingRatio) | 239 | if (ratio < acceptingRatio) | ||
Show All 29 Lines | 262 | { | |||
267 | const int newdy = qRound (dist * sin (angle) * sgn (diffy)); | 269 | const int newdy = qRound (dist * sin (angle) * sgn (diffy)); | ||
268 | #undef sgn | 270 | #undef sgn | ||
269 | 271 | | |||
270 | lineEndPoint = QPoint (lineStartPoint.x () + newdx, | 272 | lineEndPoint = QPoint (lineStartPoint.x () + newdx, | ||
271 | lineStartPoint.y () + newdy); | 273 | lineStartPoint.y () + newdy); | ||
272 | 274 | | |||
273 | qCDebug(kpLogTools) << "\t\tdiagonal line: dist=" << dist | 275 | qCDebug(kpLogTools) << "\t\tdiagonal line: dist=" << dist | ||
274 | << " angle=" << (angle * 180 / M_PI) | 276 | << " angle=" << (angle * 180 / M_PI) | ||
275 | << " endPoint=" << lineEndPoint | 277 | << " endPoint=" << lineEndPoint; | ||
276 | << endl; | | |||
277 | } | 278 | } | ||
278 | } // if (shiftPressed () || controlPressed ()) { | 279 | } // if (shiftPressed () || controlPressed ()) { | ||
279 | 280 | | |||
280 | // centring | 281 | // centring | ||
281 | if (altPressed () && 0/*ALT is unreliable*/) | 282 | if (altPressed () && 0/*ALT is unreliable*/) | ||
282 | { | 283 | { | ||
283 | // start = start - diff | 284 | // start = start - diff | ||
284 | // = start - (end - start) | 285 | // = start - (end - start) | ||
285 | // = start - end + start | 286 | // = start - end + start | ||
286 | // = 2 * start - end | 287 | // = 2 * start - end | ||
287 | if (count == 2) | 288 | if (count == 2) { | ||
288 | lineStartPoint += (lineStartPoint - lineEndPoint); | 289 | lineStartPoint += (lineStartPoint - lineEndPoint); | ||
289 | else | 290 | } | ||
291 | else { | ||||
290 | lineEndPoint += (lineEndPoint - lineStartPoint); | 292 | lineEndPoint += (lineEndPoint - lineStartPoint); | ||
293 | } | ||||
291 | } // if (altPressed ()) { | 294 | } // if (altPressed ()) { | ||
292 | } | 295 | } | ||
293 | 296 | | |||
294 | 297 | | |||
295 | // protected | 298 | // protected | ||
296 | QPolygon *kpToolPolygonalBase::points () const | 299 | QPolygon *kpToolPolygonalBase::points () const | ||
297 | { | 300 | { | ||
298 | return &d->points; | 301 | return &d->points; | ||
Show All 10 Lines | |||||
309 | 312 | | |||
310 | // virtual | 313 | // virtual | ||
311 | void kpToolPolygonalBase::draw (const QPoint &, const QPoint &, const QRect &) | 314 | void kpToolPolygonalBase::draw (const QPoint &, const QPoint &, const QRect &) | ||
312 | { | 315 | { | ||
313 | // A click of the other mouse button (to finish shape, instead of adding | 316 | // A click of the other mouse button (to finish shape, instead of adding | ||
314 | // another control point) would have caused endShape() to have been | 317 | // another control point) would have caused endShape() to have been | ||
315 | // called in kpToolPolygonalBase::beginDraw(). The points list would now | 318 | // called in kpToolPolygonalBase::beginDraw(). The points list would now | ||
316 | // be empty. We are being called by kpTool::mouseReleaseEvent(). | 319 | // be empty. We are being called by kpTool::mouseReleaseEvent(). | ||
317 | if (d->points.count () == 0) | 320 | if (d->points.count () == 0) { | ||
318 | return; | 321 | return; | ||
322 | } | ||||
319 | 323 | | |||
320 | qCDebug(kpLogTools) << "kpToolPolygonalBase::draw() d->points=" << d->points.toList () | 324 | qCDebug(kpLogTools) << "kpToolPolygonalBase::draw() d->points=" << d->points.toList () | ||
321 | << ", endPoint=" << currentPoint () << endl; | 325 | << ", endPoint=" << currentPoint (); | ||
322 | 326 | | |||
323 | // Update points() so that last point reflects current mouse position. | 327 | // Update points() so that last point reflects current mouse position. | ||
324 | const int count = d->points.count (); | 328 | const int count = d->points.count (); | ||
325 | d->points [count - 1] = currentPoint (); | 329 | d->points [count - 1] = currentPoint (); | ||
326 | 330 | | |||
327 | qCDebug(kpLogTools) << "\tafterwards, d->points=" << d->points.toList (); | 331 | qCDebug(kpLogTools) << "\tafterwards, d->points=" << d->points.toList (); | ||
328 | 332 | | |||
329 | // Are we drawing a line? | 333 | // Are we drawing a line? | ||
Show All 34 Lines | |||||
364 | { | 368 | { | ||
365 | return kpColor::Invalid; | 369 | return kpColor::Invalid; | ||
366 | } | 370 | } | ||
367 | 371 | | |||
368 | // TODO: code dup with kpToolRectangle | 372 | // TODO: code dup with kpToolRectangle | ||
369 | // protected slot | 373 | // protected slot | ||
370 | void kpToolPolygonalBase::updateShape () | 374 | void kpToolPolygonalBase::updateShape () | ||
371 | { | 375 | { | ||
372 | if (d->points.count () == 0) | 376 | if (d->points.count () == 0) { | ||
373 | return; | 377 | return; | ||
378 | } | ||||
374 | 379 | | |||
375 | const QRect boundingRect = kpTool::neededRect ( | 380 | const QRect boundingRect = kpTool::neededRect ( | ||
376 | d->points.boundingRect (), | 381 | d->points.boundingRect (), | ||
377 | d->toolWidgetLineWidth->lineWidth ()); | 382 | d->toolWidgetLineWidth->lineWidth ()); | ||
378 | 383 | | |||
379 | qCDebug(kpLogTools) << "kpToolPolygonalBase::updateShape() boundingRect=" | 384 | qCDebug(kpLogTools) << "kpToolPolygonalBase::updateShape() boundingRect=" | ||
380 | << boundingRect | 385 | << boundingRect | ||
381 | << " lineWidth=" | 386 | << " lineWidth=" | ||
Show All 28 Lines | 414 | { | |||
410 | viewManager ()->invalidateTempImage (); | 415 | viewManager ()->invalidateTempImage (); | ||
411 | d->points.resize (0); | 416 | d->points.resize (0); | ||
412 | 417 | | |||
413 | setUserMessage (i18n ("Let go of all the mouse buttons.")); | 418 | setUserMessage (i18n ("Let go of all the mouse buttons.")); | ||
414 | } | 419 | } | ||
415 | 420 | | |||
416 | void kpToolPolygonalBase::releasedAllButtons () | 421 | void kpToolPolygonalBase::releasedAllButtons () | ||
417 | { | 422 | { | ||
418 | if (!hasBegunShape ()) | 423 | if (!hasBegunShape ()) { | ||
419 | setUserMessage (/*virtual*/haventBegunShapeUserMessage ()); | 424 | setUserMessage (/*virtual*/haventBegunShapeUserMessage ()); | ||
425 | } | ||||
420 | 426 | | |||
421 | // --- else case already handled by endDraw() --- | 427 | // --- else case already handled by endDraw() --- | ||
422 | } | 428 | } | ||
423 | 429 | | |||
424 | // public virtual [base kpTool] | 430 | // public virtual [base kpTool] | ||
425 | void kpToolPolygonalBase::endShape (const QPoint &, const QRect &) | 431 | void kpToolPolygonalBase::endShape (const QPoint &, const QRect &) | ||
426 | { | 432 | { | ||
427 | qCDebug(kpLogTools) << "kpToolPolygonalBase::endShape() d->points=" | 433 | qCDebug(kpLogTools) << "kpToolPolygonalBase::endShape() d->points=" | ||
428 | << d->points.toList (); | 434 | << d->points.toList (); | ||
429 | 435 | | |||
430 | if (!hasBegunShape ()) | 436 | if (!hasBegunShape ()) { | ||
431 | return; | 437 | return; | ||
438 | } | ||||
432 | 439 | | |||
433 | viewManager ()->invalidateTempImage (); | 440 | viewManager ()->invalidateTempImage (); | ||
434 | 441 | | |||
435 | QRect boundingRect = kpTool::neededRect ( | 442 | QRect boundingRect = kpTool::neededRect ( | ||
436 | d->points.boundingRect (), | 443 | d->points.boundingRect (), | ||
437 | d->toolWidgetLineWidth->lineWidth ()); | 444 | d->toolWidgetLineWidth->lineWidth ()); | ||
438 | 445 | | |||
439 | commandHistory ()->addCommand ( | 446 | commandHistory ()->addCommand ( | ||
Show All 33 Lines |