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