]> Shamusworld >> Repos - architektonas/blob - src/base/rs_preview.cpp
Fixed preview rendering for ActionDrawLine...
[architektonas] / src / base / rs_preview.cpp
1 // rs_preview.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/21/2010  Added this text. :-)
13 //
14
15 #include "rs_preview.h"
16
17 #include "rs_entitycontainer.h"
18 #include "graphicview.h"
19 #include "rs_information.h"
20 #include "paintintf.h"
21 #include "settings.h"
22
23 /**
24  * Constructor.
25  */
26 RS_Preview::RS_Preview(RS_EntityContainer * parent): RS_EntityContainer(parent),
27         visible(false)
28 {
29         settings.beginGroup("Appearance");
30         maxEntities = settings.value("MaxPreview", 100).toInt();
31         settings.endGroup();
32 }
33
34 /**
35  * Destructor.
36  */
37 RS_Preview::~RS_Preview()
38 {
39 }
40
41 /**
42  * Adds an entity to this preview and removes any attributes / layer
43  * connectsions before that.
44  */
45 void RS_Preview::addEntity(RS_Entity * entity)
46 {
47         if (entity == NULL || entity->isUndone())
48                 return;
49
50         // only border preview for complex entities:
51         //if ((entity->count() > maxEntities-count()) &&
52
53         bool addBorder = false;
54
55         if (entity->rtti() == RS2::EntityImage || entity->rtti() == RS2::EntityHatch
56                 || entity->rtti() == RS2::EntityInsert)
57         {
58                 addBorder = true;
59         }
60         else
61         {
62                 if (entity->isContainer() && entity->rtti() != RS2::EntitySpline)
63                 {
64                         if (entity->countDeep() > maxEntities-countDeep())
65                                 addBorder = true;
66                 }
67         }
68
69         if (addBorder)
70         {
71                 Vector min = entity->getMin();
72                 Vector max = entity->getMax();
73
74                 RS_Line * l1 = new RS_Line(this, RS_LineData(Vector(min.x, min.y), Vector(max.x, min.y)));
75                 RS_Line * l2 = new RS_Line(this, RS_LineData(Vector(max.x, min.y), Vector(max.x, max.y)));
76                 RS_Line * l3 = new RS_Line(this, RS_LineData(Vector(max.x, max.y), Vector(min.x, max.y)));
77                 RS_Line * l4 = new RS_Line(this, RS_LineData(Vector(min.x, max.y), Vector(min.x, min.y)));
78
79                 RS_EntityContainer::addEntity(l1);
80                 RS_EntityContainer::addEntity(l2);
81                 RS_EntityContainer::addEntity(l3);
82                 RS_EntityContainer::addEntity(l4);
83
84                 delete entity;
85                 entity = NULL;
86         }
87         else
88         {
89                 entity->setLayer(NULL);
90                 entity->setSelected(false);
91                 entity->reparent(this);
92                 entity->setPen(RS_Pen(RS_Color(255, 255, 255), RS2::Width00, RS2::SolidLine));
93                 RS_EntityContainer::addEntity(entity);
94         }
95 }
96
97 /**
98  * Clones the given entity and adds the clone to the preview.
99  */
100 void RS_Preview::addCloneOf(RS_Entity * entity)
101 {
102         if (!entity)
103                 return;
104
105         RS_Entity * clone = entity->clone();
106         addEntity(clone);
107 }
108
109 /**
110  * Adds all entities from 'container' to the preview (unselected).
111  */
112 void RS_Preview::addAllFrom(RS_EntityContainer & container)
113 {
114         int c = 0;
115
116         for(RS_Entity * e=container.firstEntity(); e!=NULL; e=container.nextEntity())
117         {
118                 if (c < maxEntities)
119                 {
120                         RS_Entity * clone = e->clone();
121                         clone->setSelected(false);
122                         clone->reparent(this);
123
124                         c += clone->countDeep();
125                         addEntity(clone);
126                         // clone might be NULL after this point
127                 }
128         }
129 }
130
131 /**
132  * Adds all selected entities from 'container' to the preview (unselected).
133  */
134 void RS_Preview::addSelectionFrom(RS_EntityContainer & container)
135 {
136         int c = 0;
137
138         for(RS_Entity * e=container.firstEntity(); e!=NULL; e=container.nextEntity())
139         {
140                 if (e->isSelected() && c < maxEntities)
141                 {
142                         RS_Entity * clone = e->clone();
143                         clone->setSelected(false);
144                         clone->reparent(this);
145
146                         c += clone->countDeep();
147                         addEntity(clone);
148                         // clone might be NULL after this point
149                 }
150         }
151 }
152
153 /**
154  * Adds all entities in the given range and those which have endpoints in the
155  * given range to the preview.
156  */
157 void RS_Preview::addStretchablesFrom(RS_EntityContainer & container, const Vector & v1,
158         const Vector & v2)
159 {
160         int c = 0;
161
162         for(RS_Entity * e=container.firstEntity(); e!=NULL; e=container.nextEntity())
163         {
164                 if (e->isVisible() && e->rtti() != RS2::EntityHatch && (e->isInWindow(v1, v2)
165                         || e->hasEndpointsWithinWindow(v1, v2)) && c < maxEntities)
166                 {
167                         RS_Entity * clone = e->clone();
168                         //clone->setSelected(false);
169                         clone->reparent(this);
170
171                         c += clone->countDeep();
172                         addEntity(clone);
173                         // clone might be NULL after this point
174                 }
175         }
176 }
177
178 void RS_Preview::SetOffset(Vector v)
179 {
180         offset = v;
181 }
182
183 Vector RS_Preview::Offset(void)
184 {
185         return offset;
186 }
187
188 void RS_Preview::SetVisible(bool visibility/*= true*/)
189 {
190         visible = visibility;
191 }
192
193 bool RS_Preview::Visible(void)
194 {
195         return visible;
196 }
197
198 void RS_Preview::Draw(GraphicView * view, PaintInterface * painter)
199 {
200         if (isEmpty())
201                 return;
202
203 //      painter->setPreviewMode();
204         painter->setOffset(offset);
205         view->drawEntity(this, false);
206         painter->setOffset(Vector(0, 0));
207 }