Skip to content

Commit 9970cde

Browse files
committed
Add inserted state to elements and filter on save
Introduced an 'inserted' state to elements, with getter and setter methods in Element.h. Updated element creation and loading logic to set this state appropriately. Modified FileHanding::SaveProject to only save elements that are properly inserted, preventing incomplete or placeholder elements from being serialized. Removed redundant m_inserted members from derived classes and updated project toolset version.
1 parent ec6227f commit 9970cde

File tree

10 files changed

+104
-64
lines changed

10 files changed

+104
-64
lines changed

Project/PSP-UFU.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2929
<ConfigurationType>Application</ConfigurationType>
3030
<UseDebugLibraries>true</UseDebugLibraries>
31-
<PlatformToolset>v143</PlatformToolset>
31+
<PlatformToolset>v145</PlatformToolset>
3232
<CharacterSet>Unicode</CharacterSet>
3333
</PropertyGroup>
3434
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
@@ -41,13 +41,13 @@
4141
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4242
<ConfigurationType>Application</ConfigurationType>
4343
<UseDebugLibraries>true</UseDebugLibraries>
44-
<PlatformToolset>v143</PlatformToolset>
44+
<PlatformToolset>v145</PlatformToolset>
4545
<CharacterSet>Unicode</CharacterSet>
4646
</PropertyGroup>
4747
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4848
<ConfigurationType>Application</ConfigurationType>
4949
<UseDebugLibraries>false</UseDebugLibraries>
50-
<PlatformToolset>v143</PlatformToolset>
50+
<PlatformToolset>v145</PlatformToolset>
5151
<WholeProgramOptimization>true</WholeProgramOptimization>
5252
<CharacterSet>Unicode</CharacterSet>
5353
</PropertyGroup>

Project/editors/Workspace.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void Workspace::OnLeftClickDown(wxMouseEvent& event)
205205
m_timer->Stop();
206206
showNewElementForm = true;
207207
m_mode = WorkspaceMode::MODE_EDIT;
208+
element->SetInserted();
208209
}
209210
}
210211
}

Project/elements/Element.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,18 @@ class Element
592592
*/
593593
virtual double PointToLineDistance(wxPoint2DDouble point, int* segmentNumber = nullptr) const;
594594

595+
/**
596+
* @brief Check if the element is properly inserted in the workspace.
597+
* @return true if is inserted properly, false otherwise
598+
*/
599+
bool IsInserted() const { return m_inserted; }
600+
601+
/**
602+
* @brief Set if the element is properly inserted in the workspace.
603+
* @param inserted true if is inserted properly, false otherwise
604+
*/
605+
void SetInserted(bool inserted = true) { m_inserted = inserted; }
606+
595607
virtual rapidxml::xml_node<>* SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode) { return nullptr; }
596608
virtual bool OpenElement(rapidxml::xml_node<>* elementNode) { return true; }
597609
void SaveCADProperties(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementNode);
@@ -617,6 +629,7 @@ class Element
617629
bool m_selected = false;
618630
bool m_dragging = false;
619631
bool m_showPickbox = false;
632+
bool m_inserted = false;
620633

621634
int m_activePickboxID = ID_PB_NONE;
622635
int m_activeNodeID = 0;

Project/elements/Text.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,5 +1050,6 @@ bool Text::OpenElement(rapidxml::xml_node<>* elementNode)
10501050
SetDirection(XMLParser::GetNodeValueDouble(textProperties, "Direction"));
10511051
SetDecimalPlaces(XMLParser::GetNodeValueDouble(textProperties, "DecimalPlaces"));
10521052
SetElementNumber(XMLParser::GetNodeValueInt(textProperties, "ElementNumber"));
1053+
SetInserted();
10531054
return true;
10541055
}

Project/elements/powerElement/Branch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class Branch : public PowerElement
5353
virtual void UpdateSwitchesPosition();
5454
virtual void UpdateSwitches();
5555

56-
protected:
57-
bool m_inserted = false;
56+
//protected:
57+
//bool m_inserted = false;
5858
};
5959

6060
#endif // BRANCH_H

Project/elements/powerElement/Bus.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,5 +449,6 @@ bool Bus::OpenElement(rapidxml::xml_node<>* elementNode)
449449
if (powerQuality) m_electricalData.plotPQData = XMLParser::GetNodeValueInt(powerQuality, "Plot");
450450

451451
if (m_electricalData.stabHasFault) SetDynamicEvent(true);
452+
SetInserted();
452453
return true;
453454
}

Project/elements/powerElement/Line.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ bool Line::AddParent(Element* parent, wxPoint2DDouble position)
257257
m_switchRect.push_back(genRect);
258258
UpdateSwitches();
259259

260-
m_inserted = true;
260+
SetInserted();
261261
UpdatePowerFlowArrowsPosition();
262262
return true;
263263
}

Project/elements/powerElement/Machines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Machines : public PowerElement
5959
protected:
6060
void UpdateSwitchesPosition();
6161
void UpdatePowerFlowArrowsPosition();
62-
bool m_inserted = false;
62+
//bool m_inserted = false;
6363
};
6464

6565
#endif // MACHINES_H

Project/elements/powerElement/Shunt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Shunt : public PowerElement
5050
void UpdatePowerFlowArrowsPosition();
5151
//void DrawGround(wxPoint2DDouble position) const;
5252
void DrawDCGround(wxPoint2DDouble position, wxGraphicsContext* gc) const;
53-
bool m_inserted = false;
53+
//bool m_inserted = false;
5454
};
5555

5656
#endif // SHUNT_H

Project/utils/FileHanding.cpp

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -128,143 +128,167 @@ void FileHanding::SaveProject(wxFileName path)
128128
//{ Buses
129129
auto busesNode = XMLParser::AppendNode(doc, elementsNode, "BusList");
130130
auto busList = allElements.GetBusList();
131-
for(auto it = busList.begin(), itEnd = busList.end(); it != itEnd; ++it) {
132-
(*it)->SetID(elementID);
133-
(*it)->SaveElement(doc, busesNode);
134-
elementID++;
131+
for(auto* bus : busList) {
132+
if (bus->IsInserted()) {
133+
bus->SetID(elementID);
134+
bus->SaveElement(doc, busesNode);
135+
elementID++;
136+
}
135137
}
136138
//}
137139

138140
//{ Capacitor
139141
auto capacitorsNode = XMLParser::AppendNode(doc, elementsNode, "CapacitorList");
140142
auto capacitorList = allElements.GetCapacitorList();
141143
elementID = 0;
142-
for(auto it = capacitorList.begin(), itEnd = capacitorList.end(); it != itEnd; ++it) {
143-
(*it)->SetID(elementID);
144-
(*it)->SaveElement(doc, capacitorsNode);
145-
elementID++;
144+
for(auto* capacitor : capacitorList) {
145+
if (capacitor->IsInserted()) {
146+
capacitor->SetID(elementID);
147+
capacitor->SaveElement(doc, capacitorsNode);
148+
elementID++;
149+
}
146150
}
147151
//}
148152

149153
//{ IndMotor
150154
auto indMotorsNode = XMLParser::AppendNode(doc, elementsNode, "IndMotorList");
151155
auto indMotorList = allElements.GetIndMotorList();
152156
elementID = 0;
153-
for(auto it = indMotorList.begin(), itEnd = indMotorList.end(); it != itEnd; ++it) {
154-
(*it)->SetID(elementID);
155-
(*it)->SaveElement(doc, indMotorsNode);
156-
elementID++;
157+
for(auto* indMotor : indMotorList) {
158+
if (indMotor->IsInserted()) {
159+
indMotor->SetID(elementID);
160+
indMotor->SaveElement(doc, indMotorsNode);
161+
elementID++;
162+
}
157163
}
158164
//}
159165

160166
//{ Inductor
161167
auto inductorsNode = XMLParser::AppendNode(doc, elementsNode, "InductorList");
162168
auto inductorList = allElements.GetInductorList();
163169
elementID = 0;
164-
for(auto it = inductorList.begin(), itEnd = inductorList.end(); it != itEnd; ++it) {
165-
(*it)->SetID(elementID);
166-
(*it)->SaveElement(doc, inductorsNode);
167-
elementID++;
170+
for(auto* inductor : inductorList) {
171+
if (inductor->IsInserted()) {
172+
inductor->SetID(elementID);
173+
inductor->SaveElement(doc, inductorsNode);
174+
elementID++;
175+
}
168176
}
169177
//}
170178

171179
//{ Line
172180
auto linesNode = XMLParser::AppendNode(doc, elementsNode, "LineList");
173181
auto lineList = allElements.GetLineList();
174182
elementID = 0;
175-
for(auto it = lineList.begin(), itEnd = lineList.end(); it != itEnd; ++it) {
176-
(*it)->SetID(elementID);
177-
(*it)->SaveElement(doc, linesNode);
178-
elementID++;
183+
for(auto* line : lineList) {
184+
if (line->IsInserted()) {
185+
line->SetID(elementID);
186+
line->SaveElement(doc, linesNode);
187+
elementID++;
188+
}
179189
}
180190
//}
181191

182192
//{ Load
183193
auto loadsNode = XMLParser::AppendNode(doc, elementsNode, "LoadList");
184194
auto loadList = allElements.GetLoadList();
185195
elementID = 0;
186-
for(auto it = loadList.begin(), itEnd = loadList.end(); it != itEnd; ++it) {
187-
(*it)->SetID(elementID);
188-
(*it)->SaveElement(doc, loadsNode);
189-
elementID++;
196+
for(auto* load : loadList) {
197+
if(load->IsInserted()) {
198+
load->SetID(elementID);
199+
load->SaveElement(doc, loadsNode);
200+
elementID++;
201+
}
190202
}
191203
//}
192204

193205
//{ SyncGenerator
194206
auto syncGeneratorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncGeneratorList");
195207
auto syncGeneratorList = allElements.GetSyncGeneratorList();
196208
elementID = 0;
197-
for(auto it = syncGeneratorList.begin(), itEnd = syncGeneratorList.end(); it != itEnd; ++it) {
198-
(*it)->SetID(elementID);
199-
auto elementNode = (*it)->SaveElement(doc, syncGeneratorsNode);
209+
for(auto syncGen : syncGeneratorList) {
210+
if (syncGen->IsInserted()) {
211+
syncGen->SetID(elementID);
212+
auto elementNode = syncGen->SaveElement(doc, syncGeneratorsNode);
200213

201-
// Save controls
202-
auto data = (*it)->GetElectricalData();
203-
auto electricalProp = elementNode->first_node("ElectricalProperties");
204-
auto stability = electricalProp->first_node("Stability");
214+
// Save controls
215+
auto data = syncGen->GetElectricalData();
216+
auto electricalProp = elementNode->first_node("ElectricalProperties");
217+
auto stability = electricalProp->first_node("Stability");
205218

206-
auto avr = XMLParser::AppendNode(doc, stability, "AVR");
207-
if(data.avr) SaveControlElements(doc, avr, data.avr);
219+
auto avr = XMLParser::AppendNode(doc, stability, "AVR");
220+
if (data.avr) SaveControlElements(doc, avr, data.avr);
208221

209-
auto speedGov = XMLParser::AppendNode(doc, stability, "SpeedGovernor");
210-
if(data.speedGov) SaveControlElements(doc, speedGov, data.speedGov);
222+
auto speedGov = XMLParser::AppendNode(doc, stability, "SpeedGovernor");
223+
if (data.speedGov) SaveControlElements(doc, speedGov, data.speedGov);
211224

212-
elementID++;
225+
elementID++;
226+
}
213227
}
214228
//}
215229

216230
//{ SyncMotor
217231
auto syncMotorsNode = XMLParser::AppendNode(doc, elementsNode, "SyncMotorList");
218232
auto syncMotorList = allElements.GetSyncMotorList();
219233
elementID = 0;
220-
for(auto it = syncMotorList.begin(), itEnd = syncMotorList.end(); it != itEnd; ++it) {
221-
(*it)->SetID(elementID);
222-
(*it)->SaveElement(doc, syncMotorsNode);
223-
elementID++;
234+
for(auto* syncMotor : syncMotorList) {
235+
if(syncMotor->IsInserted()) {
236+
syncMotor->SetID(elementID);
237+
syncMotor->SaveElement(doc, syncMotorsNode);
238+
elementID++;
239+
}
224240
}
225241
//}
226242

227243
//{ Transfomer
228244
auto transformersNode = XMLParser::AppendNode(doc, elementsNode, "TransformerList");
229245
auto transformerList = allElements.GetTransformerList();
230246
elementID = 0;
231-
for(auto it = transformerList.begin(), itEnd = transformerList.end(); it != itEnd; ++it) {
232-
(*it)->SetID(elementID);
233-
(*it)->SaveElement(doc, transformersNode);
234-
elementID++;
247+
for(auto* transformer : transformerList) {
248+
if (transformer->IsInserted()) {
249+
transformer->SetID(elementID);
250+
transformer->SaveElement(doc, transformersNode);
251+
elementID++;
252+
}
235253
}
236254
//}
237255

238256
//{ HarmCurrent
239257
auto harmCurrentNode = XMLParser::AppendNode(doc, elementsNode, "HarmCurrentList");
240258
auto harmCurrentList = allElements.GetHarmCurrentList();
241259
elementID = 0;
242-
for(auto it = harmCurrentList.begin(), itEnd = harmCurrentList.end(); it != itEnd; ++it) {
243-
(*it)->SetID(elementID);
244-
(*it)->SaveElement(doc, harmCurrentNode);
245-
elementID++;
260+
for(auto* harmCurrent : harmCurrentList) {
261+
if (harmCurrent->IsInserted()) {
262+
harmCurrent->SetID(elementID);
263+
harmCurrent->SaveElement(doc, harmCurrentNode);
264+
elementID++;
265+
}
246266
}
247267
//}
248268

249269
//{ EMTElement
250270
auto emtElementNode = XMLParser::AppendNode(doc, elementsNode, "EMTElementList");
251271
auto emtElementList = allElements.GetEMTElementList();
252272
elementID = 0;
253-
for (auto it = emtElementList.begin(), itEnd = emtElementList.end(); it != itEnd; ++it) {
254-
(*it)->SetID(elementID);
255-
(*it)->SaveElement(doc, emtElementNode);
256-
elementID++;
273+
for (auto* emtElement : emtElementList) {
274+
if(emtElement->IsInserted()) {
275+
emtElement->SetID(elementID);
276+
emtElement->SaveElement(doc, emtElementNode);
277+
elementID++;
278+
}
257279
}
258280
//}
259281

260282
//{ Text
261283
auto textsNode = XMLParser::AppendNode(doc, elementsNode, "TextList");
262284
auto textList = m_workspace->GetTextList();
263285
elementID = 0;
264-
for(auto it = textList.begin(), itEnd = textList.end(); it != itEnd; ++it) {
265-
(*it)->SetID(elementID);
266-
(*it)->SaveElement(doc, textsNode);
267-
elementID++;
286+
for(auto* text : textList) {
287+
if(text->IsInserted()) {
288+
text->SetID(elementID);
289+
text->SaveElement(doc, textsNode);
290+
elementID++;
291+
}
268292
}
269293
//}
270294

0 commit comments

Comments
 (0)