]> Shamusworld >> Repos - architektonas/blob - src/drawdimensionaction.cpp
Added angle snap to whole degrees, ability to manipulate Dimensions.
[architektonas] / src / drawdimensionaction.cpp
1 // drawdimensionaction.cpp: Action class for drawing dimensions
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/15/2013  Created this file
12 //
13
14 #include "drawdimensionaction.h"
15 #include "applicationwindow.h"
16 #include "dimension.h"
17 #include "drawingview.h"
18 #include "line.h"
19 #include "painter.h"
20
21
22 enum { FIRST_POINT, NEXT_POINT, NO_POINT };
23
24
25 DrawDimensionAction::DrawDimensionAction(): state(0), dimension(NULL)
26 {
27 }
28
29
30 DrawDimensionAction::~DrawDimensionAction()
31 {
32 }
33
34
35 /*virtual*/ void DrawDimensionAction::Draw(Painter * painter)
36 {
37         painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
38
39         if (state == FIRST_POINT)
40         {
41                 painter->DrawHandle(p1);
42         }
43         else if (state == NEXT_POINT)
44         {
45                 painter->DrawLine(p1, p2);
46                 painter->DrawHandle(p2);
47         }
48 }
49
50
51 /*virtual*/ void DrawDimensionAction::MouseDown(Vector point)
52 {
53         obj = ApplicationWindow::drawing->document.lastObjectClicked;
54
55         if (obj)
56         {
57                 if (obj->type == OTLine)
58                 {
59                         // Make sure we didn't hit an endpoint...
60                         if (!(((Line *) obj)->hitPoint1 || ((Line *)obj)->hitPoint2))
61                         {
62                                 state = NO_POINT;
63                                 return;
64                         }
65                 }
66         }
67
68         if (state == FIRST_POINT)
69                 p1 = point;
70         else
71                 p2 = point;
72 }
73
74
75 /*virtual*/ void DrawDimensionAction::MouseMoved(Vector point)
76 {
77         if (state == FIRST_POINT)
78                 p1 = point;
79         else
80                 p2 = point;
81 }
82
83
84 /*virtual*/ void DrawDimensionAction::MouseReleased(void)
85 {
86         if (state == NO_POINT)
87         {
88                 HandleAddDimensionToObject();//ApplicationWindow::drawing->document.lastObjectClicked);
89                 state = FIRST_POINT;
90         }
91         else if (state == FIRST_POINT)
92         {
93                 p2 = p1;
94                 state = NEXT_POINT;
95         }
96         else if (state == NEXT_POINT)
97         {
98                 // We create the new object here, and then pass it off to the
99                 // DrawingView which stuffs it into the document.
100                 dimension = new Dimension(p1, p2);
101                 // We don't need no stinkin' sentinels, when we have signals & slots!
102                 emit ObjectReady(dimension);
103
104                 state = FIRST_POINT;
105         }
106 }
107
108
109 /*virtual*/ void DrawDimensionAction::KeyDown(int /*key*/)
110 {
111 }
112
113
114 /*virtual*/ void DrawDimensionAction::KeyReleased(int /*key*/)
115 {
116 }
117
118
119 void DrawDimensionAction::HandleAddDimensionToObject(void)//Object * obj)
120 {
121 //printf("Adding dimension to object...\n");
122         if (obj->type == OTLine)
123         {
124                 Object * existing = ((Line *)obj)->FindAttachedDimension();
125
126                 if (existing)
127                 {
128                         ((Dimension *)existing)->FlipSides();
129                 }
130 //printf("--> type == LINE\n");
131                 // Should also check here to see if it hit the line proper or just hit
132                 // an endpoint...
133                 else
134                 {
135                         dimension = new Dimension(p1, p2);
136                         ((Line *)obj)->SetDimensionOnLine(dimension);
137                         emit ObjectReady(dimension);
138                 }
139         }
140 }
141