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