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"
37 #include "drawlineaction.h"
42 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
43 // The value in the settings file will override this.
44 useAntialiasing(true),
45 scale(1.0), offsetX(-10), offsetY(-10),
46 document(Vector(0, 0)),
47 gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
48 scrollDrag(false), addLineTool(false), toolAction(NULL)
50 setBackgroundRole(QPalette::Base);
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
53 // toolPalette = new ToolWindow();
55 // setCursor(cur[TOOLSelect]);
56 // setMouseTracking(true);
58 Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
60 document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
61 document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
62 document.Add(new Circle(Vector(100, 100), 36, &document));
63 document.Add(new Circle(Vector(50, 150), 49, &document));
64 document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
65 document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
67 Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), &document);
68 line->SetDimensionOnLine(dimension);
69 document.Add(dimension);
71 // Alternate way to do the above...
72 line->SetDimensionOnLine();
74 // connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
75 // SLOT(AddNewObjectToDocument(Object *)));
78 void DrawingView::SetRotateToolActive(bool state/*= true*/)
84 void DrawingView::SetAddLineToolActive(bool state/*= true*/)
86 if (state && toolAction == NULL)
88 toolAction = new DrawLineAction();
89 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
90 SLOT(AddNewObjectToDocument(Object *)));
92 else if (!state && toolAction)
100 //printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction);
103 void DrawingView::AddNewObjectToDocument(Object * object)
107 object->Reparent(&document);
108 document.Add(object);
111 //printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object);
114 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
116 // This is undoing the transform, e.g. going from client coords to local coords.
117 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
118 // conversion of the y-axis from increasing bottom to top.
119 return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
122 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
124 // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
125 // No voodoo here, it's just grouped wrong to see it. It should be:
126 // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
127 return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
130 void DrawingView::paintEvent(QPaintEvent * /*event*/)
132 QPainter qtPainter(this);
133 Painter painter(&qtPainter);
136 qtPainter.setRenderHint(QPainter::Antialiasing);
138 Painter::screenSize = Vector(size().width(), size().height());
139 Object::SetViewportHeight(size().height());
141 // Draw coordinate axes
143 painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
144 painter.DrawLine(0, -16384, 0, 16384);
145 painter.DrawLine(-16384, 0, 16384, 0);
147 // Draw supplemental (tool related) points
151 painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
152 painter.DrawLine(rx - 10, ry, rx + 10, ry);
153 painter.DrawLine(rx, ry - 10, rx, ry + 10);
156 // Maybe we can make the grid into a background brush instead, and let Qt deal
161 painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
163 //these two loops kill performance!
164 // Also, these overwrite our coordinate axes
165 for(double x=0; x<size().width(); x+=gridSpacing*10.0)
166 painter.drawLine((int)x, -16384, (int)x, 16384);
168 for(double y=0; y<size().height(); y+=gridSpacing*10.0)
169 painter.drawLine(-16384, (int)y, 16384, (int)y);
172 painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
174 for(double x=0; x<size().width(); x+=gridSpacing)
175 for(double y=0; y<size().height(); y+=gridSpacing)
176 painter.DrawPoint((int)x, (int)y);
178 // The top level document takes care of rendering for us...
179 document.Draw(&painter);
182 toolAction->Draw(&painter);
185 void DrawingView::mousePressEvent(QMouseEvent * event)
187 if (event->button() == Qt::LeftButton)
189 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
190 collided = document.Collided(point);
193 update(); // Do an update if collided with at least *one* object in the document
196 toolAction->MouseDown(point);
198 else if (event->button() == Qt::MiddleButton)
201 oldPoint = Vector(event->x(), event->y());
202 // Should also change the mouse pointer as well...
203 setCursor(Qt::SizeAllCursor);
207 void DrawingView::mouseMoveEvent(QMouseEvent * event)
209 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
211 if (event->buttons() & Qt::MiddleButton)
213 point = Vector(event->x(), event->y());
214 // Since we're using Qt coords for scrolling, we have to adjust them here to
215 // conform to Cartesian coords, since the origin is using Cartesian. :-)
216 Vector delta(point, oldPoint);
217 delta /= Painter::zoom;
219 Painter::origin -= delta;
225 // Grid processing...
227 // This looks strange, but it's really quite simple: We want a point that's
228 // more than half-way to the next grid point to snap there while conversely
229 // we want a point that's less than half-way to to the next grid point then
230 // snap to the one before it. So we add half of the grid spacing to the
231 // point, then divide by it so that we can remove the fractional part, then
232 // multiply it back to get back to the correct answer.
233 if (event->buttons() & Qt::LeftButton)
235 point += gridSpacing / 2.0; // *This* adds to Z!!!
236 point /= gridSpacing;
237 point.x = floor(point.x);//need to fix this for negative numbers...
238 point.y = floor(point.y);
239 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
240 point *= gridSpacing;
243 //we should keep track of the last point here and only pass this down *if* the point
245 document.PointerMoved(point);
247 if (document.NeedsUpdate())
252 toolAction->MouseMoved(point);
257 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
259 if (event->button() == Qt::LeftButton)
261 document.PointerReleased();
263 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
264 //could set it up to use the document's update function (assumes that all object updates
265 //are being reported correctly:
266 // if (document.NeedsUpdate())
268 update(); // Do an update if collided with at least *one* object in the document
271 toolAction->MouseReleased();
273 else if (event->button() == Qt::MiddleButton)
276 setCursor(Qt::ArrowCursor);