summaryrefslogtreecommitdiffstats
path: root/Project/WorkspaceDC.cpp
blob: b517d2d09ff2caa7926a8a830eb26b31be44d596 (plain)
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
#include "WorkspaceDC.h"

#include "Camera.h"
#include "Element.h"
#include "Workspace.h"
//#include "Bus.h"
#include "Capacitor.h"
#include "ElementDataObject.h"
#include "HarmCurrent.h"
#include "IndMotor.h"
#include "Inductor.h"
#include "Line.h"
#include "Load.h"
#include "SyncGenerator.h"
#include "SyncMotor.h"
#include "Transformer.h"

#include "Text.h"

#include "Electromechanical.h"
#include "Fault.h"
#include "PowerFlow.h"
#include "PowerQuality.h"

#include "ChartView.h"
#include "ElementPlotData.h"

#include "PropertiesData.h"

#include "FrequencyResponseForm.h"

WorkspaceDC::WorkspaceDC()
{
}

WorkspaceDC::WorkspaceDC(wxWindow* parent, wxString name, wxStatusBar* statusBar) : Workspace(parent, name, statusBar, nullptr)
{
	// Disconnect events from GLCanvas
	m_glCanvas->Disconnect(wxEVT_PAINT, wxPaintEventHandler(Workspace::OnPaint), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(Workspace::OnLeftClickDown), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Workspace::OnKeyDown), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_MOTION, wxMouseEventHandler(Workspace::OnMouseMotion), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(Workspace::OnMiddleDown), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_MIDDLE_UP, wxMouseEventHandler(Workspace::OnMiddleUp), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(Workspace::OnLeftClickUp), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(Workspace::OnScroll), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(Workspace::OnRightClickDown), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(Workspace::OnLeftDoubleClick), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_IDLE, wxIdleEventHandler(Workspace::OnIdle), nullptr, this);
	m_glCanvas->Disconnect(wxEVT_MIDDLE_DCLICK, wxMouseEventHandler(Workspace::OnMiddleDoubleClick), nullptr, this);

	// Reconnect events to this
	this->Connect(wxEVT_PAINT, wxPaintEventHandler(WorkspaceDC::OnPaint), nullptr, this); // Connect to overloaded method
	this->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(Workspace::OnLeftClickDown), nullptr, this);
	this->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Workspace::OnKeyDown), nullptr, this);
	this->Connect(wxEVT_MOTION, wxMouseEventHandler(Workspace::OnMouseMotion), nullptr, this);
	this->Connect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(Workspace::OnMiddleDown), nullptr, this);
	this->Connect(wxEVT_MIDDLE_UP, wxMouseEventHandler(Workspace::OnMiddleUp), nullptr, this);
	this->Connect(wxEVT_LEFT_UP, wxMouseEventHandler(Workspace::OnLeftClickUp), nullptr, this);
	this->Connect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(Workspace::OnScroll), nullptr, this);
	this->Connect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(Workspace::OnRightClickDown), nullptr, this);
	this->Connect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(Workspace::OnLeftDoubleClick), nullptr, this);
	this->Connect(wxEVT_IDLE, wxIdleEventHandler(WorkspaceDC::OnIdle), nullptr, this);  // Connect to overloaded method
	this->Connect(wxEVT_MIDDLE_DCLICK, wxMouseEventHandler(Workspace::OnMiddleDoubleClick), nullptr, this);

	this->GetSizer()->Remove(this->GetSizer()->GetChildren()[0]->GetId()); // remove m_glCanvas object from sizer

	delete m_glCanvas; // Delete GLcanvas to allow drawing with wxDC
	m_glCanvas = nullptr;
    m_glContext = nullptr;
	SetBackgroundColour(wxColour(255, 255, 255));
	SetBackgroundStyle(wxBG_STYLE_PAINT); // To allow wxBufferedPaintDC works properly.
	Redraw();
}

WorkspaceDC::~WorkspaceDC()
{
	// Disconnect events
	this->Disconnect(wxEVT_PAINT, wxPaintEventHandler(WorkspaceDC::OnPaint), nullptr, this);
	this->Disconnect(wxEVT_LEFT_DOWN, wxMouseEventHandler(Workspace::OnLeftClickDown), nullptr, this);
	this->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Workspace::OnKeyDown), nullptr, this);
	this->Disconnect(wxEVT_MOTION, wxMouseEventHandler(Workspace::OnMouseMotion), nullptr, this);
	this->Disconnect(wxEVT_MIDDLE_DOWN, wxMouseEventHandler(Workspace::OnMiddleDown), nullptr, this);
	this->Disconnect(wxEVT_MIDDLE_UP, wxMouseEventHandler(Workspace::OnMiddleUp), nullptr, this);
	this->Disconnect(wxEVT_LEFT_UP, wxMouseEventHandler(Workspace::OnLeftClickUp), nullptr, this);
	this->Disconnect(wxEVT_MOUSEWHEEL, wxMouseEventHandler(Workspace::OnScroll), nullptr, this);
	this->Disconnect(wxEVT_RIGHT_DOWN, wxMouseEventHandler(Workspace::OnRightClickDown), nullptr, this);
	this->Disconnect(wxEVT_LEFT_DCLICK, wxMouseEventHandler(Workspace::OnLeftDoubleClick), nullptr, this);
	this->Disconnect(wxEVT_IDLE, wxIdleEventHandler(WorkspaceDC::OnIdle), nullptr, this);
	this->Disconnect(wxEVT_MIDDLE_DCLICK, wxMouseEventHandler(Workspace::OnMiddleDoubleClick), nullptr, this);

	// Recreate the GLCanvas. This is necessary to prevent WorkspaceBase destructor access nullpr.
	// Also avoid the code editing of WorkspaceBase, since is automatically generated by wxCrafter.
	// TODO(?): Find a better way to solve this problem.
	m_glCanvas = new wxGLCanvas(this);
}

void WorkspaceDC::OnPaint(wxPaintEvent& event)
{
	wxBufferedPaintDC dc(this);
	dc.Clear();
	wxGraphicsContext* gc = wxGraphicsContext::Create(dc);

	
	// Draw
	if (gc) {

		gc->Scale(m_camera->GetScale(), m_camera->GetScale());
		gc->Translate(m_camera->GetTranslation().m_x, m_camera->GetTranslation().m_y);

		// Elements
		for (auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
			Element* element = *it;
			element->DrawDC(m_camera->GetTranslation(), m_camera->GetScale(), gc);
		}

		// Texts
		for (auto it = m_textList.begin(); it != m_textList.end(); ++it) {
			Text* text = *it;
			text->DrawDC(m_camera->GetTranslation(), m_camera->GetScale(), gc);
		}

		// Selection rectangle
		gc->SetPen(wxPen(wxColour(0, 125, 255, 255)));
		gc->SetBrush(wxBrush(wxColour(0, 125, 255, 125)));
		gc->DrawRectangle(m_selectionRect.m_x, m_selectionRect.m_y, m_selectionRect.m_width, m_selectionRect.m_height);

		delete gc;
	}
	event.Skip();
}