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