Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
Constant.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "Constant.h"
19#include "../../forms/ConstantForm.h"
20
21Constant::Constant(int id) : ControlElement(id)
22{
23 SetValue(m_value);
24 m_angle = 180.0;
25 Node* nodeOut = new Node(m_position + wxPoint2DDouble(m_width / 2, 0), Node::NodeType::NODE_OUT, m_borderSize);
26 nodeOut->SetAngle(180.0);
27 nodeOut->StartMove(m_position);
28 m_nodeList.push_back(nodeOut);
29}
30
31Constant::~Constant()
32{
33 //if(m_glText) delete m_glText;
34 if(m_gcText) delete m_gcText;
35 for (auto& node : m_nodeList) if (node) delete node;
36 m_nodeList.clear();
37}
38
39//void Constant::Draw(wxPoint2DDouble translation, double scale) const
40//{
41// glLineWidth(1.0);
42// if(m_selected) {
43// glColor4dv(m_selectionColour.GetRGBA());
44// double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
45// DrawRectangle(m_position, m_width + borderSize, m_height + borderSize);
46// }
47// glColor4d(1.0, 1.0, 1.0, 1.0);
48// DrawRectangle(m_position, m_width, m_height);
49// glColor4d(0.0, 0.0, 0.0, 1.0);
50// DrawRectangle(m_position, m_width, m_height, GL_LINE_LOOP);
51//
52// // Plot number.
53// m_glText->Draw(m_position);
54//
55// glColor4d(0.0, 0.0, 0.0, 1.0);
56// DrawNodes();
57//}
58
59void Constant::DrawDC(wxPoint2DDouble translation, double scale, wxGraphicsContext* gc) const
60{
61 if (m_selected) {
62 //gc->SetPen(wxPen(wxColour(m_selectionColour)));
63 //gc->SetBrush(*wxTRANSPARENT_BRUSH);
64 gc->SetPen(*wxTRANSPARENT_PEN);
65 gc->SetBrush(wxBrush(wxColour(m_selectionColour)));
66 double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
67 gc->DrawRectangle(m_position.m_x - m_width / 2 - borderSize / 2, m_position.m_y - m_height / 2 - borderSize / 2, m_width + borderSize, m_height + borderSize);
68 }
69 gc->SetPen(wxPen(wxColour(0, 0, 0, 255), 1));
70 gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
71 //DrawRectangle(m_position, m_width, m_height);
72 gc->DrawRectangle(m_position.m_x - m_width / 2, m_position.m_y - m_height / 2, m_width, m_height);
73
74 // Plot number.
75 m_gcText->Draw(m_position + wxPoint2DDouble(-m_gcText->GetWidth() / 2, -m_gcText->GetHeight() / 2), gc);
76
77 gc->SetPen(*wxTRANSPARENT_PEN);
78 gc->SetBrush(wxBrush(wxColour(0, 0, 0, 255)));
79 DrawDCNodes(gc);
80}
81
82bool Constant::ShowForm(wxWindow* parent, Element* element)
83{
84 ConstantForm form(parent, this);
85 form.CenterOnParent();
86 if(form.ShowModal() == wxID_OK) {
87 return true;
88 }
89 return false;
90}
91
92void Constant::Rotate(bool clockwise)
93{
94 if(clockwise)
95 m_angle += 90.0;
96 else
97 m_angle -= 90.0;
98 if(m_angle >= 360.0)
99 m_angle = 0.0;
100 else if(m_angle < 0)
101 m_angle = 270.0;
102
103 UpdatePoints();
104
105 for(auto it = m_nodeList.begin(), itEnd = m_nodeList.end(); it != itEnd; ++it) {
106 Node* node = *it;
107 node->Rotate(clockwise);
108 }
109}
110
111void Constant::UpdatePoints()
112{
113 if(m_nodeList.size() != 0) {
114 if(m_angle == 0.0) {
115 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
116 } else if(m_angle == 90.0) {
117 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
118 } else if(m_angle == 180.0) {
119 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
120 } else if(m_angle == 270.0) {
121 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
122 }
123 }
124}
125
126void Constant::SetValue(double value)
127{
128 m_value = value;
129 wxString text = StringFromDouble(m_value);
130
131 if(m_gcText)
132 m_gcText->SetText(text);
133 else
134 m_gcText = new GCText(text);
135
136 m_width = m_gcText->GetWidth() + 6 + 2 * m_borderSize;
137 m_height = m_gcText->GetHeight() + 6 + 2 * m_borderSize;
138
139 UpdatePoints();
140}
141
143{
144 Constant* copy = new Constant(m_elementID);
145 *copy = *this;
146 copy->m_gcText = m_gcText->GetCopy();
147 return copy;
148}
149
151{
152 SetValue(m_value);
153 //if(!m_glText->IsTextureOK()) return false;
154 return true;
155}
156
157rapidxml::xml_node<>* Constant::SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode)
158{
159 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "Constant");
160 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
161
162 SaveCADProperties(doc, elementNode);
163 SaveControlNodes(doc, elementNode);
164
165 // Element properties
166 auto value = XMLParser::AppendNode(doc, elementNode, "Value");
167 XMLParser::SetNodeValue(doc, value, m_value);
168
169 return elementNode;
170}
171
172bool Constant::OpenElement(rapidxml::xml_node<>* elementNode)
173{
174 if(!OpenCADProperties(elementNode)) return false;
175 if(!OpenControlNodes(elementNode)) return false;
176
177 // Element properties
178 double value = XMLParser::GetNodeValueDouble(elementNode, "Value");
179
180 SetPosition(m_position);
181 SetValue(value);
182 return true;
183}
Form to edit the constant control data.
A control element that provides a constant value.
Definition Constant.h:37
virtual bool UpdateText()
Update the OpenGL text in the element (if present).
Definition Constant.cpp:150
virtual void Rotate(bool clockwise=true)
Rotate the element.
Definition Constant.cpp:92
virtual void DrawDC(wxPoint2DDouble translation, double scale, wxGraphicsContext *gc) const
Draw the element using GDI+.
Definition Constant.cpp:59
virtual Element * GetCopy()
Get a the element copy.
Definition Constant.cpp:142
virtual bool ShowForm(wxWindow *parent, Element *element)
Show element data form.
Definition Constant.cpp:82
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:112
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:27
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.
Definition Element.cpp:533
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual GCText * GetCopy()
Get a deep text copy.
Definition GCText.cpp:161
virtual void Draw(wxPoint2DDouble position, wxGraphicsContext *gc, double angle=0.0, wxColour colour= *wxBLACK) const
Draw the text in wxGraphicsContext.
Definition GCText.cpp:35
virtual void SetText(wxString text)
Set correctly a new text string.
Definition GCText.cpp:68
Node of a control element. This class manages the user interaction with the connection and control el...