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 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"
37 #include "drawcircleaction.h"
38 #include "drawlineaction.h"
43 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
44 // The value in the settings file will override this.
45 useAntialiasing(true),
46 scale(1.0), offsetX(-10), offsetY(-10),
47 document(Vector(0, 0)),
48 gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
49 scrollDrag(false), addLineTool(false), toolAction(NULL)
51 setBackgroundRole(QPalette::Base);
52 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
54 // toolPalette = new ToolWindow();
56 // setCursor(cur[TOOLSelect]);
57 // setMouseTracking(true);
59 Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
61 document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
62 document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
63 document.Add(new Circle(Vector(100, 100), 36, &document));
64 document.Add(new Circle(Vector(50, 150), 49, &document));
65 document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
66 document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
68 Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), &document);
69 line->SetDimensionOnLine(dimension);
70 document.Add(dimension);
72 // Alternate way to do the above...
73 line->SetDimensionOnLine();
75 // connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
76 // SLOT(AddNewObjectToDocument(Object *)));
79 void DrawingView::SetRotateToolActive(bool state/*= true*/)
85 void DrawingView::SetAddLineToolActive(bool state/*= true*/)
87 if (state)// && toolAction == NULL)
92 addCircleTool = false;
93 toolAction = new DrawLineAction();
94 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
95 SLOT(AddNewObjectToDocument(Object *)));
97 else if (!state && addLineTool && toolAction)
105 //printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction);
108 void DrawingView::SetAddCircleToolActive(bool state/*= true*/)
110 if (state)// && toolAction == NULL)
116 toolAction = new DrawCircleAction();
117 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
118 SLOT(AddNewObjectToDocument(Object *)));
120 else if (!state && addCircleTool && toolAction)
126 addCircleTool = state;
128 //printf("DrawingView::SetAddCircleToolActive(). toolAction=%08X\n", toolAction);
131 void DrawingView::AddNewObjectToDocument(Object * object)
135 object->Reparent(&document);
136 document.Add(object);
139 //printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object);
142 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
144 // This is undoing the transform, e.g. going from client coords to local coords.
145 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
146 // conversion of the y-axis from increasing bottom to top.
147 return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
150 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
152 // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
153 // No voodoo here, it's just grouped wrong to see it. It should be:
154 // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
155 return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
158 void DrawingView::paintEvent(QPaintEvent * /*event*/)
160 QPainter qtPainter(this);
161 Painter painter(&qtPainter);
164 qtPainter.setRenderHint(QPainter::Antialiasing);
166 Painter::screenSize = Vector(size().width(), size().height());
167 Object::SetViewportHeight(size().height());
169 // Draw coordinate axes
171 painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
172 painter.DrawLine(0, -16384, 0, 16384);
173 painter.DrawLine(-16384, 0, 16384, 0);
175 // Draw supplemental (tool related) points
179 painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
180 painter.DrawLine(rx - 10, ry, rx + 10, ry);
181 painter.DrawLine(rx, ry - 10, rx, ry + 10);
184 // Maybe we can make the grid into a background brush instead, and let Qt deal
189 painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
191 //these two loops kill performance!
192 // Also, these overwrite our coordinate axes
193 for(double x=0; x<size().width(); x+=gridSpacing*10.0)
194 painter.drawLine((int)x, -16384, (int)x, 16384);
196 for(double y=0; y<size().height(); y+=gridSpacing*10.0)
197 painter.drawLine(-16384, (int)y, 16384, (int)y);
200 painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
202 for(double x=0; x<size().width(); x+=gridSpacing)
203 for(double y=0; y<size().height(); y+=gridSpacing)
204 painter.DrawPoint((int)x, (int)y);
206 // The top level document takes care of rendering for us...
207 document.Draw(&painter);
210 toolAction->Draw(&painter);
213 void DrawingView::mousePressEvent(QMouseEvent * event)
215 if (event->button() == Qt::LeftButton)
217 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
218 collided = document.Collided(point);
221 update(); // Do an update if collided with at least *one* object in the document
224 toolAction->MouseDown(point);
226 else if (event->button() == Qt::MiddleButton)
229 oldPoint = Vector(event->x(), event->y());
230 // Should also change the mouse pointer as well...
231 setCursor(Qt::SizeAllCursor);
235 void DrawingView::mouseMoveEvent(QMouseEvent * event)
237 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
239 if (event->buttons() & Qt::MiddleButton)
241 point = Vector(event->x(), event->y());
242 // Since we're using Qt coords for scrolling, we have to adjust them here to
243 // conform to Cartesian coords, since the origin is using Cartesian. :-)
244 Vector delta(point, oldPoint);
245 delta /= Painter::zoom;
247 Painter::origin -= delta;
253 // Grid processing...
255 // This looks strange, but it's really quite simple: We want a point that's
256 // more than half-way to the next grid point to snap there while conversely
257 // we want a point that's less than half-way to to the next grid point then
258 // snap to the one before it. So we add half of the grid spacing to the
259 // point, then divide by it so that we can remove the fractional part, then
260 // multiply it back to get back to the correct answer.
261 if (event->buttons() & Qt::LeftButton)
263 point += gridSpacing / 2.0; // *This* adds to Z!!!
264 point /= gridSpacing;
265 point.x = floor(point.x);//need to fix this for negative numbers...
266 point.y = floor(point.y);
267 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
268 point *= gridSpacing;
271 //we should keep track of the last point here and only pass this down *if* the point
273 document.PointerMoved(point);
275 if (document.NeedsUpdate())
280 toolAction->MouseMoved(point);
285 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
287 if (event->button() == Qt::LeftButton)
289 document.PointerReleased();
291 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
292 //could set it up to use the document's update function (assumes that all object updates
293 //are being reported correctly:
294 // if (document.NeedsUpdate())
296 update(); // Do an update if collided with at least *one* object in the document
299 toolAction->MouseReleased();
301 else if (event->button() == Qt::MiddleButton)
304 setCursor(Qt::ArrowCursor);