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