/
DrawViewCollection.cpp
200 lines (170 loc) · 6.83 KB
/
DrawViewCollection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2002 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
#endif
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include "DrawPage.h"
#include "DrawViewCollection.h"
using namespace TechDraw;
//===========================================================================
// DrawViewCollection
//===========================================================================
PROPERTY_SOURCE(TechDraw::DrawViewCollection, TechDraw::DrawView)
DrawViewCollection::DrawViewCollection()
{
nowUnsetting = false;
static const char *group = "Collection";
ADD_PROPERTY_TYPE(Views ,(0), group, App::Prop_None,"Collection Views");
Views.setScope(App::LinkScope::Global);
}
DrawViewCollection::~DrawViewCollection()
{
}
void DrawViewCollection::onChanged(const App::Property* prop)
{
TechDraw::DrawView::onChanged(prop);
}
short DrawViewCollection::mustExecute() const
{
if (Views.isTouched()) {
return 1;
} else {
return TechDraw::DrawView::mustExecute();
}
}
App::DocumentObjectExecReturn *DrawViewCollection::execute(void)
{
if (!keepUpdated()) {
return App::DocumentObject::StdReturn;
}
lockChildren();
return DrawView::execute();
}
int DrawViewCollection::addView(DrawView *view)
{
// Add the new view to the collection
std::vector<App::DocumentObject *> newViews(Views.getValues());
newViews.push_back(view);
Views.setValues(newViews);
return Views.getSize();
}
int DrawViewCollection::removeView(DrawView *view)
{
// Remove the view from the collection
const std::vector<App::DocumentObject*> currViews = Views.getValues();
std::vector<App::DocumentObject*> newViews;
std::vector<App::DocumentObject*>::const_iterator it = currViews.begin();
for (; it != currViews.end(); it++) {
std::string viewName = view->getNameInDocument();
if (viewName.compare((*it)->getNameInDocument()) != 0) {
newViews.push_back((*it));
}
}
Views.setValues(newViews);
return Views.getSize();
}
//make sure everything in View list represents a real DrawView docObj and occurs only once
void DrawViewCollection::rebuildViewList()
{
const std::vector<App::DocumentObject*> currViews = Views.getValues();
std::vector<App::DocumentObject*> newViews;
std::vector<App::DocumentObject*> children = getOutList();
for (std::vector<App::DocumentObject*>::iterator it = children.begin(); it != children.end(); ++it) {
if ((*it)->getTypeId().isDerivedFrom(DrawView::getClassTypeId())) {
bool found = false;
for (auto& v:currViews) {
if (v == (*it)) {
found = true;
break;
}
}
if (found) {
newViews.push_back((*it));
}
}
} // newViews contains only valid items, but may have duplicates
sort( newViews.begin(), newViews.end() );
newViews.erase( unique( newViews.begin(), newViews.end() ), newViews.end() );
Views.setValues(newViews);
}
int DrawViewCollection::countChildren()
{
//Count the children recursively if needed
int numChildren = 0;
const std::vector<App::DocumentObject *> &views = Views.getValues();
for(std::vector<App::DocumentObject *>::const_iterator it = views.begin(); it != views.end(); ++it) {
if((*it)->getTypeId().isDerivedFrom(TechDraw::DrawViewCollection::getClassTypeId())) {
TechDraw::DrawViewCollection *viewCollection = static_cast<TechDraw::DrawViewCollection *>(*it);
numChildren += viewCollection->countChildren() + 1;
} else {
numChildren += 1;
}
}
return numChildren;
}
void DrawViewCollection::onDocumentRestored()
{
DrawView::execute();
}
void DrawViewCollection::lockChildren(void)
{
for (auto& v:Views.getValues()) {
TechDraw::DrawView *view = dynamic_cast<TechDraw::DrawView *>(v);
if (!view) {
throw Base::ValueError("DrawViewCollection::lockChildren bad View\n");
}
view->handleXYLock();
}
}
void DrawViewCollection::unsetupObject()
{
nowUnsetting = true;
// Remove the collection's views from document
App::Document* doc = getDocument();
std::string docName = doc->getName();
const std::vector<App::DocumentObject*> currViews = Views.getValues();
std::vector<App::DocumentObject*> emptyViews;
std::vector<App::DocumentObject*>::const_iterator it = currViews.begin();
for (; it != currViews.end(); it++) {
std::string viewName = (*it)->getNameInDocument();
Base::Interpreter().runStringArg("App.getDocument(\"%s\").removeObject(\"%s\")",
docName.c_str(), viewName.c_str());
}
Views.setValues(emptyViews);
}
QRectF DrawViewCollection::getRect() const
{
QRectF result;
for (auto& v:Views.getValues()) {
TechDraw::DrawView *view = dynamic_cast<TechDraw::DrawView *>(v);
if (!view) {
throw Base::ValueError("DrawViewCollection::getRect bad View\n");
}
result = result.united(view->getRect().translated(view->X.getValue(),view->Y.getValue()));
}
return QRectF(0,0,getScale() * result.width(),getScale() * result.height());
}