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
17 // - Redo rendering code to *not* use Qt's transform functions, as they are tied
18 // to a left-handed system and we need a right-handed one. [DONE]
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 "drawdimensionaction.h"
39 #include "drawlineaction.h"
44 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
45 // The value in the settings file will override this.
46 useAntialiasing(true),
47 gridBackground(256, 256),
48 scale(1.0), offsetX(-10), offsetY(-10),
49 document(Vector(0, 0)),
50 // gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
51 gridSpacing(12.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
52 scrollDrag(false), addLineTool(false), addCircleTool(false),
53 addDimensionTool(false),
54 // selectionInProgress(false),
57 document.isTopLevelContainer = true;
58 setBackgroundRole(QPalette::Base);
59 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
61 // toolPalette = new ToolWindow();
63 // setCursor(cur[TOOLSelect]);
64 // setMouseTracking(true);
66 Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
68 document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
69 document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
70 document.Add(new Circle(Vector(100, 100), 36, &document));
71 document.Add(new Circle(Vector(50, 150), 49, &document));
72 document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
73 document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
75 Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), DTLinear, &document);
76 line->SetDimensionOnLine(dimension);
77 document.Add(dimension);
79 // Alternate way to do the above...
80 line->SetDimensionOnLine();
82 // connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
83 // SLOT(AddNewObjectToDocument(Object *)));
84 //This works, now how to scroll it???
85 // QPixmap pm(256, 256);
86 QPainter pmp(&gridBackground);
88 pmp.fillRect(0, 0, 256, 256, Qt::lightGray);
90 pmp.fillRect(0, 64, 64, 64, Qt::darkGray);
91 pmp.fillRect(0, 192, 64, 64, Qt::darkGray);
92 pmp.fillRect(64, 0, 64, 64, Qt::darkGray);
93 pmp.fillRect(64, 128, 64, 64, Qt::darkGray);
94 pmp.fillRect(128, 64, 64, 64, Qt::darkGray);
95 pmp.fillRect(128, 192, 64, 64, Qt::darkGray);
96 pmp.fillRect(192, 0, 64, 64, Qt::darkGray);
97 pmp.fillRect(192, 128, 64, 64, Qt::darkGray);
99 pmp.fillRect(0, 0, 256, 256, QColor(240, 240, 240));
100 pmp.setPen(QPen(QColor(210, 210, 255), 2.0, Qt::SolidLine));
101 for(int i=0; i<255; i+=12)
102 pmp.drawLine(i, 0, i, 255);
103 for(int i=0; i<255; i+=12)
104 pmp.drawLine(0, i, 255, i);
107 UpdateGridBackground();
111 void DrawingView::SetRotateToolActive(bool state/*= true*/)
118 void DrawingView::SetAddLineToolActive(bool state/*= true*/)
122 toolAction = new DrawLineAction();
123 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
124 SLOT(AddNewObjectToDocument(Object *)));
128 //printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction);
132 void DrawingView::SetAddCircleToolActive(bool state/*= true*/)
136 toolAction = new DrawCircleAction();
137 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
138 SLOT(AddNewObjectToDocument(Object *)));
145 void DrawingView::SetAddDimensionToolActive(bool state/*= true*/)
149 toolAction = new DrawDimensionAction();
150 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
151 SLOT(AddNewObjectToDocument(Object *)));
158 void DrawingView::UpdateGridBackground(void)
161 // Shift the background to match our scrolling...
162 QBrush newBrush = *backgroundBrush;
163 //QMatrix brushMatrix = backgroundBrush->matrix();
164 QTransform brushMatrix = backgroundBrush->transform();
165 brushMatrix.translate(Painter::origin.x, Painter::origin.y);
166 //brushMatrix.translate(15.0, 15.0);
167 //backgroundBrush->setMatrix(brushMatrix);
168 //backgroundBrush->setTransform(brushMatrix);
169 newBrush.setTransform(brushMatrix);
170 QPalette pal = palette();
171 //pal.setBrush(backgroundRole(), *backgroundBrush);
172 pal.setBrush(backgroundRole(), newBrush);
174 //Background painting does not honor the transformation matrix (either one)...
178 #define BG_BRUSH_SPAN 72
179 // Transform the origin to Qt coordinates
180 Vector pixmapOrigin = Painter::CartesianToQtCoords(Vector());
181 int x = (int)pixmapOrigin.x;
182 int y = (int)pixmapOrigin.y;
183 // Use mod arithmetic to grab the correct swatch of background
184 // Problem with mod 128: Negative numbers screw it up... [FIXED]
185 x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
186 y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
188 // Here we grab a section of the bigger pixmap, so that the background
189 // *looks* like it's scrolling...
190 QPixmap pm = gridBackground.copy(x, y, BG_BRUSH_SPAN, BG_BRUSH_SPAN);
191 QPalette pal = palette();
192 pal.setBrush(backgroundRole(), QBrush(pm));
193 setAutoFillBackground(true);
199 void DrawingView::AddNewObjectToDocument(Object * object)
203 object->Reparent(&document);
204 document.Add(object);
207 //printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object);
211 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
213 // This is undoing the transform, e.g. going from client coords to local coords.
214 // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
215 // conversion of the y-axis from increasing bottom to top.
216 return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
220 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
222 // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
223 // No voodoo here, it's just grouped wrong to see it. It should be:
224 // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
225 return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
229 void DrawingView::paintEvent(QPaintEvent * /*event*/)
231 QPainter qtPainter(this);
232 Painter painter(&qtPainter);
234 // qtPainter.setBackground(QBrush(Qt::DiagCrossPattern));
235 // qtPainter.setBackgroundMode(Qt::OpaqueMode);
238 qtPainter.setRenderHint(QPainter::Antialiasing);
240 Painter::screenSize = Vector(size().width(), size().height());
241 Object::SetViewportHeight(size().height());
243 // Draw coordinate axes
245 painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
246 painter.DrawLine(0, -16384, 0, 16384);
247 painter.DrawLine(-16384, 0, 16384, 0);
249 // Draw supplemental (tool related) points
250 // NOTE that this can be done as an action!
251 // In that case, we would need access to the document...
254 painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
255 painter.DrawLine(rx - 10, ry, rx + 10, ry);
256 painter.DrawLine(rx, ry - 10, rx, ry + 10);
259 // Maybe we can make the grid into a background brush instead, and let Qt deal
264 painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
266 //these two loops kill performance!
267 // Also, these overwrite our coordinate axes
268 for(double x=0; x<size().width(); x+=gridSpacing*10.0)
269 painter.drawLine((int)x, -16384, (int)x, 16384);
271 for(double y=0; y<size().height(); y+=gridSpacing*10.0)
272 painter.drawLine(-16384, (int)y, 16384, (int)y);
275 // painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
277 // for(double x=0; x<size().width(); x+=gridSpacing)
278 // for(double y=0; y<size().height(); y+=gridSpacing)
279 // painter.DrawPoint((int)x, (int)y);
281 // The top level document takes care of rendering for us...
282 document.Draw(&painter);
285 toolAction->Draw(&painter);
287 if (Object::selectionInProgress)
289 // painter.SetPen(QPen(Qt::green, 1.0, Qt::SolidLine));
290 painter.SetPen(QPen(QColor(255, 127, 0, 255)));
291 // painter.SetBrush(QBrush(Qt::NoBrush));
292 painter.SetBrush(QBrush(QColor(255, 127, 0, 100)));
293 painter.DrawRect(Object::selection);
298 void DrawingView::mousePressEvent(QMouseEvent * event)
300 if (event->button() == Qt::LeftButton)
302 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
303 collided = document.Collided(point);
306 update(); // Do an update if collided with at least *one* object in the document
309 toolAction->MouseDown(point);
311 // Didn't hit any object and not using a tool, so do a selection rectangle
312 if (!(collided || toolAction))
314 Object::selectionInProgress = true;
315 Object::selection.setTopLeft(QPointF(point.x, point.y));
316 Object::selection.setBottomRight(QPointF(point.x, point.y));
319 else if (event->button() == Qt::MiddleButton)
322 oldPoint = Vector(event->x(), event->y());
323 // Should also change the mouse pointer as well...
324 setCursor(Qt::SizeAllCursor);
329 void DrawingView::mouseMoveEvent(QMouseEvent * event)
331 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
332 Object::selection.setBottomRight(QPointF(point.x, point.y));
334 if (event->buttons() & Qt::MiddleButton)
336 point = Vector(event->x(), event->y());
337 // Since we're using Qt coords for scrolling, we have to adjust them here to
338 // conform to Cartesian coords, since the origin is using Cartesian. :-)
339 Vector delta(point, oldPoint);
340 delta /= Painter::zoom;
342 Painter::origin -= delta;
344 UpdateGridBackground();
350 // Grid processing...
352 // This looks strange, but it's really quite simple: We want a point that's
353 // more than half-way to the next grid point to snap there while conversely
354 // we want a point that's less than half-way to to the next grid point then
355 // snap to the one before it. So we add half of the grid spacing to the
356 // point, then divide by it so that we can remove the fractional part, then
357 // multiply it back to get back to the correct answer.
358 if (event->buttons() & Qt::LeftButton)
360 point += gridSpacing / 2.0; // *This* adds to Z!!!
361 point /= gridSpacing;
362 //200% is ok, gridSpacing = 6 in this case...
363 //won't run into problems until gridSpacing = 1.5 (zoom = 800%)
364 //run into problems with this approach: when zoom level is 200% this truncates to
365 //integers, which is *not* what's wanted here...
366 point.x = floor(point.x);//need to fix this for negative numbers...
367 point.y = floor(point.y);
368 point.z = 0; // Make *sure* Z doesn't go anywhere!!!
369 point *= gridSpacing;
372 //we should keep track of the last point here and only pass this down *if* the point
374 document.PointerMoved(point);
376 if (document.NeedsUpdate() || Object::selectionInProgress)
381 toolAction->MouseMoved(point);
387 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
389 if (event->button() == Qt::LeftButton)
391 document.PointerReleased();
393 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
394 //could set it up to use the document's update function (assumes that all object updates
395 //are being reported correctly:
396 // if (document.NeedsUpdate())
398 update(); // Do an update if collided with at least *one* object in the document
401 toolAction->MouseReleased();
403 if (Object::selectionInProgress)
405 // Select all the stuff inside of selection
406 Object::selectionInProgress = false;
409 else if (event->button() == Qt::MiddleButton)
412 setCursor(Qt::ArrowCursor);