]> Shamusworld >> Repos - architektonas/blob - src/drawingview.cpp
Added Architektonas drawing file loading/saving infrastructure.
[architektonas] / src / drawingview.cpp
1 // drawingview.cpp
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  03/22/2011  Created this file
12 // JLH  09/29/2011  Added middle mouse button panning
13 //
14
15 // FIXED:
16 //
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]
19 //
20 // STILL TO BE DONE:
21 //
22 //
23
24 // Uncomment this for debugging...
25 //#define DEBUG
26 //#define DEBUGFOO                              // Various tool debugging...
27 //#define DEBUGTP                               // Toolpalette debugging...
28
29 #include "drawingview.h"
30
31 #include <stdint.h>
32 #include "mathconstants.h"
33
34 #include "arc.h"
35 #include "circle.h"
36 #include "dimension.h"
37 #include "drawcircleaction.h"
38 #include "drawlineaction.h"
39 #include "line.h"
40 #include "painter.h"
41
42
43 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
44         // The value in the settings file will override this.
45         useAntialiasing(true),
46         gridBackground(256, 256),
47         scale(1.0), offsetX(-10), offsetY(-10),
48         document(Vector(0, 0)),
49 //      gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
50         gridSpacing(12.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
51         scrollDrag(false), addLineTool(false), toolAction(NULL)
52 {
53         setBackgroundRole(QPalette::Base);
54         setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
55
56 //      toolPalette = new ToolWindow();
57 //      CreateCursors();
58 //      setCursor(cur[TOOLSelect]);
59 //      setMouseTracking(true);
60
61         Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
62         document.Add(line);
63         document.Add(new Line(Vector(50, 40), Vector(10, 83), &document));
64         document.Add(new Line(Vector(10, 83), Vector(17, 2), &document));
65         document.Add(new Circle(Vector(100, 100), 36, &document));
66         document.Add(new Circle(Vector(50, 150), 49, &document));
67         document.Add(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3, &document)),
68         document.Add(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5, &document));
69 #if 1
70         Dimension * dimension = new Dimension(Vector(0, 0), Vector(0, 0), DTLinear, &document);
71         line->SetDimensionOnLine(dimension);
72         document.Add(dimension);
73 #else
74         // Alternate way to do the above...
75         line->SetDimensionOnLine();
76 #endif
77 //      connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
78 //              SLOT(AddNewObjectToDocument(Object *)));
79 //This works, now how to scroll it???
80 //      QPixmap pm(256, 256);
81         QPainter pmp(&gridBackground);
82 #if 0
83         pmp.fillRect(0, 0, 256, 256, Qt::lightGray);
84
85         pmp.fillRect(0,   64,  64, 64, Qt::darkGray);
86         pmp.fillRect(0,   192, 64, 64, Qt::darkGray);
87         pmp.fillRect(64,  0,   64, 64, Qt::darkGray);
88         pmp.fillRect(64,  128, 64, 64, Qt::darkGray);
89         pmp.fillRect(128, 64,  64, 64, Qt::darkGray);
90         pmp.fillRect(128, 192, 64, 64, Qt::darkGray);
91         pmp.fillRect(192, 0,   64, 64, Qt::darkGray);
92         pmp.fillRect(192, 128, 64, 64, Qt::darkGray);
93 #else
94         pmp.fillRect(0, 0, 256, 256, QColor(240, 240, 240));
95         pmp.setPen(QPen(QColor(210, 210, 255), 2.0, Qt::SolidLine));
96         for(int i=0; i<255; i+=12)
97                 pmp.drawLine(i, 0, i, 255);
98         for(int i=0; i<255; i+=12)
99                 pmp.drawLine(0, i, 255, i);
100 #endif
101         pmp.end();
102         UpdateGridBackground();
103 }
104
105 void DrawingView::SetRotateToolActive(bool state/*= true*/)
106 {
107         rotateTool = state;
108         update();
109 }
110
111 void DrawingView::SetAddLineToolActive(bool state/*= true*/)
112 {
113         if (state)// && toolAction == NULL)
114         {
115                 if (toolAction)
116                         delete toolAction;
117
118                 addCircleTool = false;
119                 toolAction = new DrawLineAction();
120                 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
121                         SLOT(AddNewObjectToDocument(Object *)));
122         }
123         else if (!state && addLineTool && toolAction)
124         {
125                 delete toolAction;
126                 toolAction = NULL;
127         }
128
129         addLineTool = state;
130         update();
131 //printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction);
132 }
133
134 void DrawingView::SetAddCircleToolActive(bool state/*= true*/)
135 {
136         if (state)// && toolAction == NULL)
137         {
138                 if (toolAction)
139                         delete toolAction;
140
141                 addLineTool = false;
142                 toolAction = new DrawCircleAction();
143                 connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
144                         SLOT(AddNewObjectToDocument(Object *)));
145         }
146         else if (!state && addCircleTool && toolAction)
147         {
148                 delete toolAction;
149                 toolAction = NULL;
150         }
151
152         addCircleTool = state;
153         update();
154 //printf("DrawingView::SetAddCircleToolActive(). toolAction=%08X\n", toolAction);
155 }
156
157 void DrawingView::UpdateGridBackground(void)
158 {
159 #if 0
160 // Shift the background to match our scrolling...
161 QBrush newBrush = *backgroundBrush;
162 //QMatrix brushMatrix = backgroundBrush->matrix();
163 QTransform brushMatrix = backgroundBrush->transform();
164 brushMatrix.translate(Painter::origin.x, Painter::origin.y);
165 //brushMatrix.translate(15.0, 15.0);
166 //backgroundBrush->setMatrix(brushMatrix);
167 //backgroundBrush->setTransform(brushMatrix);
168 newBrush.setTransform(brushMatrix);
169 QPalette pal = palette();
170 //pal.setBrush(backgroundRole(), *backgroundBrush);
171 pal.setBrush(backgroundRole(), newBrush);
172 setPalette(pal);
173 //Background painting does not honor the transformation matrix (either one)...
174 // So...
175 #else
176 //was: 128
177 #define BG_BRUSH_SPAN 72
178         // Transform the origin to Qt coordinates
179         Vector pixmapOrigin = Painter::CartesianToQtCoords(Vector());
180         int x = (int)pixmapOrigin.x;
181         int y = (int)pixmapOrigin.y;
182         // Use mod arithmetic to grab the correct swatch of background
183         // Problem with mod 128: Negative numbers screw it up... [FIXED]
184         x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
185         y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
186
187         // Here we grab a section of the bigger pixmap, so that the background
188         // *looks* like it's scrolling...
189         QPixmap pm = gridBackground.copy(x, y, BG_BRUSH_SPAN, BG_BRUSH_SPAN);
190         QPalette pal = palette();
191         pal.setBrush(backgroundRole(), QBrush(pm));
192         setAutoFillBackground(true);
193         setPalette(pal);
194 #endif
195 }
196
197 void DrawingView::AddNewObjectToDocument(Object * object)
198 {
199         if (object)
200         {
201                 object->Reparent(&document);
202                 document.Add(object);
203                 update();
204         }
205 //printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object);
206 }
207
208 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
209 {
210         // This is undoing the transform, e.g. going from client coords to local coords.
211         // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
212         // conversion of the y-axis from increasing bottom to top.
213         return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
214 }
215
216 QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
217 {
218         // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
219         // No voodoo here, it's just grouped wrong to see it. It should be:
220         // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
221         return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
222 }
223
224 void DrawingView::paintEvent(QPaintEvent * /*event*/)
225 {
226         QPainter qtPainter(this);
227         Painter painter(&qtPainter);
228
229 //      qtPainter.setBackground(QBrush(Qt::DiagCrossPattern));
230 //      qtPainter.setBackgroundMode(Qt::OpaqueMode);
231
232         if (useAntialiasing)
233                 qtPainter.setRenderHint(QPainter::Antialiasing);
234
235         Painter::screenSize = Vector(size().width(), size().height());
236         Object::SetViewportHeight(size().height());
237
238         // Draw coordinate axes
239
240         painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
241         painter.DrawLine(0, -16384, 0, 16384);
242         painter.DrawLine(-16384, 0, 16384, 0);
243
244         // Draw supplemental (tool related) points
245 // NOTE that this can be done as an action!
246 // In that case, we would need access to the document...
247         if (rotateTool)
248         {
249                 painter.SetPen(QPen(QColor(0, 200, 0), 2.0, Qt::SolidLine));
250                 painter.DrawLine(rx - 10, ry, rx + 10, ry);
251                 painter.DrawLine(rx, ry - 10, rx, ry + 10);
252         }
253
254 // Maybe we can make the grid into a background brush instead, and let Qt deal
255 // with it???
256         // Draw grid
257
258 #if 0
259         painter.setPen(QPen(QColor(90, 90, 90), 1.0, Qt::DotLine));
260
261         //these two loops kill performance!
262         // Also, these overwrite our coordinate axes
263         for(double x=0; x<size().width(); x+=gridSpacing*10.0)
264                 painter.drawLine((int)x, -16384, (int)x, 16384);
265
266         for(double y=0; y<size().height(); y+=gridSpacing*10.0)
267                 painter.drawLine(-16384, (int)y, 16384, (int)y);
268 #endif
269
270 //      painter.SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
271 //
272 //      for(double x=0; x<size().width(); x+=gridSpacing)
273 //              for(double y=0; y<size().height(); y+=gridSpacing)
274 //                      painter.DrawPoint((int)x, (int)y);
275
276         // The top level document takes care of rendering for us...
277         document.Draw(&painter);
278
279         if (toolAction)
280                 toolAction->Draw(&painter);
281 }
282
283 void DrawingView::mousePressEvent(QMouseEvent * event)
284 {
285         if (event->button() == Qt::LeftButton)
286         {
287                 Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
288                 collided = document.Collided(point);
289
290                 if (collided)
291                         update();       // Do an update if collided with at least *one* object in the document
292
293                 if (toolAction)
294                         toolAction->MouseDown(point);
295         }
296         else if (event->button() == Qt::MiddleButton)
297         {
298                 scrollDrag = true;
299                 oldPoint = Vector(event->x(), event->y());
300                 // Should also change the mouse pointer as well...
301                 setCursor(Qt::SizeAllCursor);
302         }
303 }
304
305 void DrawingView::mouseMoveEvent(QMouseEvent * event)
306 {
307         Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
308
309         if (event->buttons() & Qt::MiddleButton)
310         {
311                 point = Vector(event->x(), event->y());
312                 // Since we're using Qt coords for scrolling, we have to adjust them here to
313                 // conform to Cartesian coords, since the origin is using Cartesian. :-)
314                 Vector delta(point, oldPoint);
315                 delta /= Painter::zoom;
316                 delta.y = -delta.y;
317                 Painter::origin -= delta;
318
319                 UpdateGridBackground();
320                 update();
321                 oldPoint = point;
322                 return;
323         }
324
325         // Grid processing...
326 #if 1
327         // This looks strange, but it's really quite simple: We want a point that's
328         // more than half-way to the next grid point to snap there while conversely
329         // we want a point that's less than half-way to to the next grid point then
330         // snap to the one before it. So we add half of the grid spacing to the
331         // point, then divide by it so that we can remove the fractional part, then
332         // multiply it back to get back to the correct answer.
333         if (event->buttons() & Qt::LeftButton)
334         {
335                 point += gridSpacing / 2.0;                                     // *This* adds to Z!!!
336                 point /= gridSpacing;
337 //200% is ok, gridSpacing = 6 in this case...
338 //won't run into problems until gridSpacing = 1.5 (zoom = 800%)
339 //run into problems with this approach: when zoom level is 200% this truncates to
340 //integers, which is *not* what's wanted here...
341                 point.x = floor(point.x);//need to fix this for negative numbers...
342                 point.y = floor(point.y);
343                 point.z = 0;                                                            // Make *sure* Z doesn't go anywhere!!!
344                 point *= gridSpacing;
345         }
346 #endif
347 //we should keep track of the last point here and only pass this down *if* the point
348 //changed...
349         document.PointerMoved(point);
350
351         if (document.NeedsUpdate())
352                 update();
353
354         if (toolAction)
355         {
356                 toolAction->MouseMoved(point);
357                 update();
358         }
359 }
360
361 void DrawingView::mouseReleaseEvent(QMouseEvent * event)
362 {
363         if (event->button() == Qt::LeftButton)
364         {
365                 document.PointerReleased();
366
367 //We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
368 //could set it up to use the document's update function (assumes that all object updates
369 //are being reported correctly:
370 //              if (document.NeedsUpdate())
371 //              if (collided)
372                         update();       // Do an update if collided with at least *one* object in the document
373
374                 if (toolAction)
375                         toolAction->MouseReleased();
376         }
377         else if (event->button() == Qt::MiddleButton)
378         {
379                 scrollDrag = false;
380                 setCursor(Qt::ArrowCursor);
381         }
382 }