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