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