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