Power System Platform  2026w23a-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
39void Constant::DrawDC(GUIColour* guiColour, wxPoint2DDouble translation, double scale, wxGraphicsContext* gc) const
40{
41 if (m_selected) {
42 gc->SetPen(*wxTRANSPARENT_PEN);
43 gc->SetBrush(guiColour->selection);
44 double borderSize = (m_borderSize * 2.0 + 1.0) / scale;
45 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);
46 }
47 gc->SetPen(wxPen(guiColour->enabled, 1));
48 gc->SetBrush(guiColour->background);
49 //DrawRectangle(m_position, m_width, m_height);
50 gc->DrawRectangle(m_position.m_x - m_width / 2, m_position.m_y - m_height / 2, m_width, m_height);
51
52 // Plot number.
53 m_gcText->Draw(m_position + wxPoint2DDouble(-m_gcText->GetWidth() / 2, -m_gcText->GetHeight() / 2), gc, 0, guiColour->text);
54
55 gc->SetPen(*wxTRANSPARENT_PEN);
56 gc->SetBrush(guiColour->enabled);
57 DrawDCNodes(gc);
58}
59
60bool Constant::ShowForm(wxWindow* parent, Element* element, wxWindow* workspace)
61{
62 ConstantForm form(parent, this);
63 form.CenterOnParent();
64 if (form.ShowModal() == wxID_OK) {
65 return true;
66 }
67 return false;
68}
69
70void Constant::Rotate(bool clockwise)
71{
72 if (clockwise)
73 m_angle += 90.0;
74 else
75 m_angle -= 90.0;
76 if (m_angle >= 360.0)
77 m_angle = 0.0;
78 else if (m_angle < 0)
79 m_angle = 270.0;
80
81 UpdatePoints();
82
83 for (auto it = m_nodeList.begin(), itEnd = m_nodeList.end(); it != itEnd; ++it) {
84 Node* node = *it;
85 node->Rotate(clockwise);
86 }
87}
88
89void Constant::SetFont(wxFont& font)
90{
91 if (m_gcText) m_gcText->SetFont(font);
92 UpdateText();
93}
94
95void Constant::UpdatePoints()
96{
97 if (m_nodeList.size() != 0) {
98 if (m_angle == 0.0) {
99 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(-m_width / 2, 0));
100 }
101 else if (m_angle == 90.0) {
102 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, -m_height / 2));
103 }
104 else if (m_angle == 180.0) {
105 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(m_width / 2, 0));
106 }
107 else if (m_angle == 270.0) {
108 m_nodeList[0]->SetPosition(m_position + wxPoint2DDouble(0, m_height / 2));
109 }
110 }
111}
112
113void Constant::SetValue(double value)
114{
115 m_value = value;
116 wxString text = StringFromDouble(m_value);
117
118 if (m_gcText)
119 m_gcText->SetText(text);
120 else
121 m_gcText = new GCText(text);
122
123 m_width = m_gcText->GetWidth() + 6 + 2 * m_borderSize;
124 m_height = m_gcText->GetHeight() + 6 + 2 * m_borderSize;
125
126 UpdatePoints();
127}
128
130{
131 Constant* copy = new Constant(*this);
132 copy->m_gcText = m_gcText ? m_gcText->GetCopy() : nullptr;
133 return copy;
134}
135
137{
138 SetValue(m_value);
139 //if(!m_glText->IsTextureOK()) return false;
140 return true;
141}
142
143rapidxml::xml_node<>* Constant::SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode)
144{
145 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "Constant");
146 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
147
148 SaveCADProperties(doc, elementNode);
149 SaveControlNodes(doc, elementNode);
150
151 // Element properties
152 auto value = XMLParser::AppendNode(doc, elementNode, "Value");
153 XMLParser::SetNodeValue(doc, value, m_value);
154
155 return elementNode;
156}
157
158bool Constant::OpenElement(rapidxml::xml_node<>* elementNode)
159{
160 if (!OpenCADProperties(elementNode)) return false;
161 if (!OpenControlNodes(elementNode)) return false;
162
163 // Element properties
164 double value = XMLParser::GetNodeValueDouble(elementNode, "Value");
165
166 SetPosition(m_position);
167 SetValue(value);
168 return true;
169}
Form to edit the constant control data.
A control element that provides a constant value.
Definition Constant.h:37
virtual bool ShowForm(wxWindow *parent, Element *element, wxWindow *workspace=nullptr)
Show element data form.
Definition Constant.cpp:60
virtual bool UpdateText()
Update the OpenGL text in the element (if present).
Definition Constant.cpp:136
virtual void Rotate(bool clockwise=true)
Rotate the element.
Definition Constant.cpp:70
virtual void DrawDC(GUIColour *guiColour, wxPoint2DDouble translation, double scale, wxGraphicsContext *gc) const
Draw the element using GDI+.
Definition Constant.cpp:39
virtual Element * GetCopy()
Get a the element copy.
Definition Constant.cpp:129
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:114
void SetPosition(const wxPoint2DDouble position)
Set the element position and update the rectangle.
Definition Element.cpp:33
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.
Definition Element.cpp:466
Class to draw text on Graphics Context using wxWidgets.
Definition GCText.h:32
virtual GCText * GetCopy()
Get a deep text copy.
Definition GCText.cpp:99
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...