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
12 // JLH 09/29/2011 Added middle mouse button panning
20 // - Redo rendering code to *not* use Qt's transform functions, as they are tied
21 // to a left-handed system and we need a right-handed one.
24 // Uncomment this for debugging...
26 //#define DEBUGFOO // Various tool debugging...
27 //#define DEBUGTP // Toolpalette debugging...
29 #include "drawingview.h"
32 #include "mathconstants.h"
36 #include "dimension.h"
41 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
42 // The value in the settings file will override this.
43 useAntialiasing(true),
44 scale(1.0), offsetX(-10), offsetY(-10),
45 document(Vector(0, 0)),
46 gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
49 setBackgroundRole(QPalette::Base);
50 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
52 // toolPalette = new ToolWindow();
54 // setCursor(cur[TOOLSelect]);
55 // setMouseTracking(true);
57 Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
59 document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
60 document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
61 document.Add(new Circle(Vector(100, 100), 36, &document));
62 document.Add(new Circle(Vector(50, 150), 49, &document));
63 document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
64 document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
66 Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), &document);
67 line->SetDimensionOnLine(dimension);
68 document.Add(dimension);
70 // Alternate way to do the above...
71 line->SetDimensionOnLine();
75 void DrawingView::SetRotateToolActive(bool state/*= true*/)
81 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
83 // This is undoing the transform, e.g. going from client coords to local coords.
84 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
85 // conversion of the y-axis from increasing bottom to top.
86 return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
89 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
91 // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
92 // No voodoo here, it's just grouped wrong to see it. It should be:
93 // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
94 return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
97 void DrawingView::paintEvent(QPaintEvent * /*event*/)
99 QPainter qtPainter(this);
100 Painter painter(&qtPainter);
103 qtPainter.setRenderHint(QPainter::Antialiasing);
105 Painter::screenSize = Vector(size().width(), size().height());
106 Object::SetViewportHeight(size().height());
108 // Draw coordinate axes
110 painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
111 painter.DrawLine(0, -16384, 0, 16384);
112 painter.DrawLine(-16384, 0, 16384, 0);
114 // Draw supplemental (tool related) points
118 painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
119 painter.DrawLine(rx - 10, ry, rx + 10, ry);
120 painter.DrawLine(rx, ry - 10, rx, ry + 10);
123 // Maybe we can make the grid into a background brush instead, and let Qt deal
128 painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
130 //these two loops kill performance!
131 // Also, these overwrite our coordinate axes
132 for(double x=0; x<size().width(); x+=gridSpacing*10.0)
133 painter.drawLine((int)x, -16384, (int)x, 16384);
135 for(double y=0; y<size().height(); y+=gridSpacing*10.0)
136 painter.drawLine(-16384, (int)y, 16384, (int)y);
139 painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
141 for(double x=0; x<size().width(); x+=gridSpacing)
142 for(double y=0; y<size().height(); y+=gridSpacing)
143 painter.DrawPoint((int)x, (int)y);
145 // The top level document takes care of rendering for us...
146 document.Draw(&painter);
149 void DrawingView::mousePressEvent(QMouseEvent * event)
151 if (event->button() == Qt::LeftButton)
153 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
154 collided = document.Collided(point);
157 update(); // Do an update if collided with at least *one* object in the document
159 else if (event->button() == Qt::MiddleButton)
162 oldPoint = Vector(event->x(), event->y());
163 // Should also change the mouse pointer as well...
164 setCursor(Qt::SizeAllCursor);
168 void DrawingView::mouseMoveEvent(QMouseEvent * event)
170 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
172 if (event->buttons() & Qt::MiddleButton)
174 point = Vector(event->x(), event->y());
175 // Since we're using Qt coords for scrolling, we have to adjust them here to
176 // conform to Cartesian coords, since the origin is using them. :-)
177 Vector delta(point, oldPoint);
178 delta /= Painter::zoom;
180 Painter::origin -= delta;
186 // Grid processing...
188 // This looks strange, but it's really quite simple: We want a point that's
189 // more than half-way to the next grid point to snap there while conversely
190 // we want a point that's less than half-way to to the next grid point then
191 // snap to the one before it. So we add half of the grid spacing to the
192 // point, then divide by it so that we can remove the fractional part, then
193 // multiply it back to get back to the correct answer.
194 if (event->buttons() & Qt::LeftButton)
196 point += gridSpacing / 2.0; // *This* adds to Z!!!
197 point /= gridSpacing;
198 point.x = floor(point.x);//need to fix this for negative numbers...
199 point.y = floor(point.y);
200 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
201 point *= gridSpacing;
204 //we should keep track of the last point here and only pass this down *if* the point
206 document.PointerMoved(point);
208 if (document.NeedsUpdate())
212 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
214 if (event->button() == Qt::LeftButton)
216 document.PointerReleased();
218 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
219 //could set it up to use the document's update function (assumes that all object updates
220 //are being reported correctly:
221 // if (document.NeedsUpdate())
223 update(); // Do an update if collided with at least *one* object in the document
225 else if (event->button() == Qt::MiddleButton)
228 setCursor(Qt::ArrowCursor);