3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 03/22/2011 Created this file
21 // Uncomment this for debugging...
23 //#define DEBUGFOO // Various tool debugging...
24 //#define DEBUGTP // Toolpalette debugging...
26 #include "drawingview.h"
29 #include "mathconstants.h"
33 #include "dimension.h"
37 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
38 // scale(1.0), offsetX(-10), offsetY(-10), tool(TOOLSelect),
39 // ptHighlight(-1), oldPtHighlight(-1), ptNextHighlight(-1), oldPtNextHighlight(-1),
40 // polyFirstPoint(true)
41 scale(1.0), offsetX(-10), offsetY(-10),
42 document(Vector(0, 0)),
43 gridSpacing(32.0), collided(false)
45 setBackgroundRole(QPalette::Base);
46 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
48 // toolPalette = new ToolWindow();
50 // setCursor(cur[TOOLSelect]);
51 // setMouseTracking(true);
53 Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
55 document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
56 document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
57 document.Add(new Circle(Vector(100, 100), 36, &document));
58 document.Add(new Circle(Vector(50, 150), 49, &document));
59 document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
60 document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
61 Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), &document);
62 line->SetDimensionOnPoint1(dimension);
63 line->SetDimensionOnPoint2(dimension);
64 document.Add(dimension);
67 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
69 // This is undoing the transform, e.g. going from client coords to local coords.
70 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
71 // conversion of the y-axis from increasing bottom to top.
72 return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
75 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
77 // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
78 // No voodoo here, it's just grouped wrong to see it. It should be:
79 // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
80 return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
83 void DrawingView::paintEvent(QPaintEvent * /*event*/)
85 QPainter painter(this);
86 painter.setRenderHint(QPainter::Antialiasing);
89 painter.translate(QPoint(-offsetX, size.height() - (-offsetY)));
90 painter.scale(1.0, -1.0);
93 //order of operations is important! N.B.: Can't use scaling other than 1.0, it
94 //causes lines to look strange (i.e., it scales the pen strokes too)
95 // transform.translate(-offsetX, size().height() - (-offsetY));
96 transform.scale(1.0, -1.0);
97 transform.translate(-offsetX, -size().height() - offsetY);
98 // transform.scale(0.25, 0.25);
99 painter.setTransform(transform);
101 Object::SetViewportHeight(size().height());
103 // Draw coordinate axes
105 painter.setPen(QPen(Qt::blue, 1.0, Qt::DotLine));
106 painter.drawLine(0, -16384, 0, 16384);
107 painter.drawLine(-16384, 0, 16384, 0);
109 // Maybe we can make the grid into a background brush instead, and let Qt deal
114 painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
116 //these two loops kill performance!
117 for(double x=0; x<size().width(); x+=gridSpacing*10.0)
118 p.drawLine((int)x, -16384, (int)x, 16384);
120 for(double y=0; y<size().height(); y+=gridSpacing*10.0)
121 p.drawLine(-16384, (int)y, 16384, (int)y);
124 painter.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
126 for(double x=0; x<size().width(); x+=gridSpacing)
127 for(double y=0; y<size().height(); y+=gridSpacing)
128 painter.drawPoint((int)x, (int)y);
130 // The top level document takes care of rendering for us...
131 document.Draw(&painter);
134 void DrawingView::mousePressEvent(QMouseEvent * event)
136 if (event->button() == Qt::LeftButton)
138 QPoint pt = GetAdjustedMousePosition(event);
139 Vector point(pt.x(), pt.y());
141 collided = document.Collided(point);
144 update(); // Do an update if collided with at least *one* object in the document
148 void DrawingView::mouseMoveEvent(QMouseEvent * event)
150 QPoint pt = GetAdjustedMousePosition(event);
151 Vector point(pt.x(), pt.y());
153 // Grid processing...
155 // This looks strange, but it's really quite simple: We want a point that's
156 // more than half-way to the next grid point to snap there while conversely
157 // we want a point that's less than half-way to to the next grid point then
158 // snap to the one before it. So we add half of the grid spacing to the
159 // point, then divide by it so that we can remove the fractional part, then
160 // multiply it back to get back to the correct answer.
161 point += gridSpacing / 2.0; // *This* adds to Z!!!
162 point /= gridSpacing;
163 point.x = floor(point.x);//need to fix this for negative numbers...
164 point.y = floor(point.y);
165 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
166 point *= gridSpacing;
168 //we should keep track of the last point here and only pass this down *if* the point
170 document.PointerMoved(point);
172 if (document.NeedsUpdate())
176 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
178 if (event->button() == Qt::LeftButton)
180 document.PointerReleased();
182 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
183 //could set it up to use the document's update function (assumes that all object updates
184 //are being reported correctly:
185 // if (document.NeedsUpdate())
187 update(); // Do an update if collided with at least *one* object in the document
193 QSize DrawingView::minimumSizeHint() const
195 return QSize(50, 50);
198 QSize DrawingView::sizeHint() const
200 return QSize(400, 400);
203 void DrawingView::CreateCursors(void)
205 int hotx[8] = { 1, 1, 11, 15, 1, 1, 1, 1 };
206 int hoty[8] = { 1, 1, 11, 13, 1, 1, 1, 1 };
208 for(int i=0; i<8; i++)
211 s.sprintf(":/res/cursor%u.png", i+1);
213 cur[i] = QCursor(pmTmp, hotx[i], hoty[i]);
219 o Different colors for polys on selected points
220 o Different colors for handles on non-selected polys
221 o Line of sight (dashed, dotted) for off-curve points
222 o Repaints for press/release of CTRL/SHIFT during point creation
224 void DrawingView::paintEvent(QPaintEvent * /*event*/)
228 // p.setRenderHint(QPainter::Antialiasing);
230 //dc.SetBackground(*wxWHITE_BRUSH);
232 // Due to the screwiness of wxWidgets coord system, the origin is ALWAYS
233 // the upper left corner--regardless of axis orientation, etc...
234 // int width, height;
235 // dc.GetSize(&width, &height);
236 QSize winSize = size();
238 // dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
239 // dc.SetAxisOrientation(true, true);
240 p.translate(QPoint(-offsetX, winSize.height() - (-offsetY)));
243 // Scrolling can be done by using OffsetViewportOrgEx
244 // Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
245 // you'd use: % = ViewportExt / WindowExt
246 // But it makes the window look like crap: fuggetuboutit.
247 // Instead, we have to scale EVERYTHING by hand. Crap!
248 // It's not *that* bad, but not as convenient either...
250 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0xFF), 1, wxDOT)));
251 //// dc.DrawLine(0, 0, 10, 10);
252 p.setPen(QPen(Qt::blue, 1.0, Qt::DotLine));
254 // Draw coordinate axes
256 // dc.CrossHair(0, 0);
257 p.drawLine(0, -16384, 0, 16384);
258 p.drawLine(-16384, 0, 16384, 0);
262 for(int i=0; i<pts.GetNumPoints(); i++)
264 if (i == ptHighlight)
266 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
267 //// SelectObject(hdc, hRedPen1);
268 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
270 if (pts.GetOnCurve(i))
272 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
273 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
277 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
278 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
281 else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)
283 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0xAF, 0x00), 1, wxSOLID)));
284 //// SelectObject(hdc, hGreenPen1);
285 p.setPen(QPen(Qt::green, 1.0, Qt::SolidLine));
287 if (pts.GetOnCurve(i))
289 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 7);
290 DrawSquareDotN(p, pts.GetX(i), pts.GetY(i), 9);
294 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 7);
295 DrawRoundDotN(p, pts.GetX(i), pts.GetY(i), 9);
300 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
301 //// SelectObject(hdc, hBlackPen1);
302 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
304 if (pts.GetOnCurve(i))
305 DrawSquareDot(p, pts.GetX(i), pts.GetY(i));
307 DrawRoundDot(p, pts.GetX(i), pts.GetY(i));
310 if (tool == TOOLDelPt && i == ptHighlight)
313 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
314 // SelectObject(hdc, hRedPen1);
315 // MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5, NULL);
316 // LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) + 5);
317 // LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5);//Lameness!
318 // MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5, NULL);
319 // LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) - 5);
320 // LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5);//More lameness!!
322 p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
323 p.drawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5);
324 p.drawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5);
328 //// SelectObject(hdc, hBlackPen1);
329 // dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
330 p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
332 // Draw curve formed by points
334 for(int poly=0; poly<pts.GetNumPolys(); poly++)
336 if (pts.GetNumPoints(poly) > 2)
339 // If it's not on curve, then move to it, otherwise move to last point...
343 if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
344 x = (int)pts.GetX(poly, pts.GetNumPoints(poly) - 1), y = (int)pts.GetY(poly, pts.GetNumPoints(poly) - 1);
346 x = (int)pts.GetX(poly, 0), y = (int)pts.GetY(poly, 0);
348 for(int i=0; i<pts.GetNumPoints(poly); i++)
350 if (pts.GetOnCurve(poly, i))
351 // LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
353 p.drawLine(x, y, pts.GetX(poly, i), pts.GetY(poly, i));
354 x = (int)pts.GetX(poly, i), y = (int)pts.GetY(poly, i);
358 uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
359 float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
360 nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
362 if (!pts.GetOnCurve(poly, prev))
363 px = (px + pts.GetX(poly, i)) / 2.0f,
364 py = (py + pts.GetY(poly, i)) / 2.0f;
366 if (!pts.GetOnCurve(poly, next))
367 nx = (nx + pts.GetX(poly, i)) / 2.0f,
368 ny = (ny + pts.GetY(poly, i)) / 2.0f;
370 Bezier(p, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
371 x = (int)nx, y = (int)ny;
373 if (pts.GetOnCurve(poly, next))
374 i++; // Following point is on curve, so move past it
381 void DrawingView::mousePressEvent(QMouseEvent * event)
383 if (event->button() == Qt::RightButton)
385 toolPalette->move(event->globalPos());
386 toolPalette->setVisible(true);
387 setCursor(cur[TOOLSelect]);
388 toolPalette->prevTool = TOOLSelect;
390 else if (event->button() == Qt::MidButton)
392 setCursor(cur[2]); // Scrolling cursor
394 else if (event->button() == Qt::LeftButton)
396 if (tool == TOOLScroll || tool == TOOLZoom)
397 ;//meh CaptureMouse(); // Make sure we capture the mouse when in scroll/zoom mode
398 else if (tool == TOOLAddPt) // "Add Point" tool
400 if (pts.GetNumPoints() > 0)
402 QPoint pt = GetAdjustedMousePosition(event);
403 pts.InsertPoint(pts.GetNext(ptHighlight), pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
404 ptHighlight = ptNextHighlight;
408 else if (tool == TOOLAddPoly) // "Add Poly" tool
411 WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
415 polyFirstPoint = false;
416 pts.AddNewPolyAtEnd();
419 QPoint pt = GetAdjustedMousePosition(event);
420 //printf("GetAdjustedMousePosition = %i, %i\n", pt.x(), pt.y());
421 // Append a point to the end of the structure
422 pts += IPoint(pt.x(), pt.y(), ((event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier) ? false : true));
423 ptHighlight = pts.GetNumPoints() - 1;
426 WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());
429 else if (tool == TOOLSelect || tool == TOOLPolySelect)
431 if (pts.GetNumPoints() > 0)
433 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
434 //printf("GetAdjustedClientPosition = %i, %i\n", pt.x(), pt.y());
435 // WarpPointer(pt.x, pt.y);
436 QCursor::setPos(mapToGlobal(pt));
438 if (event->modifiers() == Qt::ShiftModifier || event->modifiers() == Qt::ControlModifier)
440 pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
445 else if (tool == TOOLDelPt)
447 if (pts.GetNumPoints() > 0)
449 // if (ptHighlight != -1)
451 //This assumes that WM_MOUSEMOVE happens before this!
452 //The above commented out line should take care of this contingency... !!! FIX !!!
453 pts.DeletePoint(ptHighlight);
462 void DrawingView::mouseMoveEvent(QMouseEvent * event)
464 if (event->buttons() == Qt::RightButton)
466 ToolType newTool = toolPalette->FindSelectedTool();
468 if (newTool != toolPalette->prevTool)
470 toolPalette->prevTool = newTool;
471 toolPalette->repaint();
474 else if (event->buttons() == Qt::MidButton)
476 // Calc offset from previous point
478 ptOffset = QPoint(pt.x() - ptPrevious.x(), pt.y() - ptPrevious.y());
480 // Then multiply it by the scaling factor. Whee!
481 // This looks wacky because we're using screen coords for the offset...
482 // Otherwise, we would subtract both offsets!
483 offsetX -= ptOffset.x(), offsetY += ptOffset.y();
487 else if (event->buttons() == Qt::LeftButton)
490 if (tool == TOOLScroll)
492 // Extract current point from lParam/calc offset from previous point
494 pt = e.GetPosition();
495 ptOffset.x = pt.x - ptPrevious.x,
496 ptOffset.y = pt.y - ptPrevious.y;
498 // NOTE: OffsetViewportOrg operates in DEVICE UNITS...
500 //Seems there's no equivalent for this in wxWidgets...!
502 // hdc = GetDC(hWnd);
503 // OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);
504 // ReleaseDC(hWnd, hdc);
506 // this shows that it works, so the logic above must be faulty...
507 // And it is. It should convert the coords first, then do the subtraction to figure the offset...
509 // Then multiply it by the scaling factor. Whee!
510 // This looks wacky because we're using screen coords for the offset...
511 // Otherwise, we would subtract both offsets!
512 offsetX -= ptOffset.x, offsetY += ptOffset.y;
517 if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)
519 if (tool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
521 //temporary, for testing. BTW, Select drag bug is here...!
523 QPoint pt2 = GetAdjustedMousePosition(event);
524 pts.SetXY(ptHighlight, pt2.x(), pt2.y());
529 else if (tool == TOOLPolySelect)
531 if (pts.GetNumPoints() > 0)
533 QPoint pt2 = GetAdjustedMousePosition(event);
534 // Should also set onCurve here as well, depending on keystate
536 pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x() - pts.GetX(ptHighlight), pt2.y() - pts.GetY(ptHighlight));
541 else if (event->buttons() == Qt::NoButton)
543 // Moving, not dragging...
544 if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
545 || tool == TOOLPolySelect)// || tool == TOOLAddPoly)
547 QPoint pt2 = GetAdjustedMousePosition(event);
548 double closest = 1.0e+99;
550 for(int i=0; i<pts.GetNumPoints(); i++)
552 double dist = ((pt2.x() - pts.GetX(i)) * (pt2.x() - pts.GetX(i)))
553 + ((pt2.y() - pts.GetY(i)) * (pt2.y() - pts.GetY(i)));
556 closest = dist, ptHighlight = i;
559 if (ptHighlight != oldPtHighlight)
561 oldPtHighlight = ptHighlight;
565 // What follows here looks like voodoo, but is really simple. What we do is
566 // check to see if the mouse point has a perpendicular intersection with any of
567 // the line segments. If it does, calculate the length of the perpendicular
568 // and choose the smallest length. If there is no perpendicular, then choose the
569 // length of line connecting the closer of either the first endpoint or the
570 // second and choose the smallest of those.
572 // There is one bit of math that looks like voodoo to me ATM--will explain once
573 // I understand it better (the calculation of the length of the perpendicular).
575 if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)
577 double smallest = 1.0e+99;
579 for(int i=0; i<pts.GetNumPoints(); i++)
581 int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
582 p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
584 vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x(), pt2.y(), 0, p1x, p1y, 0),
585 v2(pt2.x(), pt2.y(), 0, p2x, p2y, 0);
586 double pp = ls.dot(v1) / ls.length(), dist;
587 // Geometric interpretation:
588 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
589 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
590 // then the perpendicular lies beyond the 2nd endpoint.
594 else if (pp > ls.length())
596 else // distance = ?Det?(ls, v1) / |ls|
597 dist = fabs((ls.x * v1.y - v1.x * ls.y) / ls.length());
599 //The answer to the above looks like it might be found here:
601 //If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
602 //intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
603 //{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
604 //computed by first computing the area of the triangle the three points form, then dividing by the
605 //length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
606 //triangle formed by three points is the determinant of the following matrix:
612 //By translating the start point to the origin, this can be rewritten as:
613 //By subtracting row 1 from all rows, you get the following:
614 //[because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
615 // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
618 //(ex - sx) (ey - sy) 0
619 //(px - sx) (py - sy) 0
621 //which greatly simplifies the calculation of the determinant.
624 smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
627 if (ptNextHighlight != oldPtNextHighlight)
629 oldPtNextHighlight = ptNextHighlight;
635 ptPrevious = event->pos();
641 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
643 if (event->button() == Qt::RightButton)
645 ToolType newTool = toolPalette->FindSelectedTool();
647 // We only change the tool if a new one was actually selected. Otherwise, we do nothing.
648 if (newTool != TOOLNone)
652 if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly
653 || tool == TOOLDelPoly)
656 if (tool == TOOLAddPoly)
657 polyFirstPoint = true;
660 toolPalette->setVisible(false);
661 setCursor(cur[tool]);
662 // Just in case we changed highlighting style with the new tool...
665 else if (event->button() == Qt::MidButton)
667 setCursor(cur[tool]); // Restore previous cursor
669 else if (event->button() == Qt::LeftButton)
671 // if (tool == TOOLScroll || tool == TOOLZoom)
673 //this is prolly too much
674 ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&pts);
675 ((TTEdit *)qApp)->charWnd->update();