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