summaryrefslogtreecommitdiffstats
path: root/Project/Workspace.cpp
blob: 69c11c3883daa7451d0b9b3b84fa7168b0af2ea3 (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#include "Workspace.h"

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

// Camera
Camera::Camera()
{
    m_translation = wxPoint2DDouble(0, 0);
    m_scale = 1.0;
}

Camera::~Camera() {}
wxPoint2DDouble Camera::ScreenToWorld(wxPoint2DDouble screenCoords) const
{
    return wxPoint2DDouble(screenCoords.m_x / m_scale - m_translation.m_x,
                           screenCoords.m_y / m_scale - m_translation.m_y);
}

void Camera::SetTranslation(wxPoint2DDouble screenPoint)
{
    m_translation = screenPoint / m_scale - m_translationStartPt;
}

void Camera::SetScale(wxPoint2DDouble screenPoint, double delta)
{
    m_translation -= screenPoint * (1.0 - m_scale) / m_scale;

    m_scale += delta;

    // Limits: 5% - 300%
    if(m_scale < m_zoomMin) m_scale = m_zoomMin;
    if(m_scale > m_zoomMax) m_scale = m_zoomMax;

    m_translation += screenPoint * (1.0 - m_scale) / m_scale;
}

wxPoint2DDouble Camera::GetMousePosition(bool worldCoords) const
{
    if(worldCoords) return ScreenToWorld(m_mousePosition);
    return m_mousePosition;
}

// Workspace
Workspace::Workspace() : WorkspaceBase(NULL) {}
Workspace::Workspace(wxWindow* parent, wxString name, wxStatusBar* statusBar) : WorkspaceBase(parent)
{
    m_name = name;
    m_statusBar = statusBar;
    m_glContext = new wxGLContext(m_glCanvas);
    m_camera = new Camera();
    m_selectionRect = wxRect2DDouble(0, 0, 0, 0);

    for(int i = 0; i < NUM_ELEMENTS; ++i) {
        m_elementNumber[i] = 1;
    }

    const int widths[4] = {-3, -1, 100, 100};
    m_statusBar->SetStatusWidths(4, widths);
}

Workspace::~Workspace()
{
    for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
        if(*it) delete *it;
    }
    m_elementList.clear();
    if(m_camera) delete m_camera;
}

void Workspace::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(m_glCanvas);
    m_glContext->SetCurrent(*m_glCanvas);
    SetViewport();

    // Set GLCanvas scale and translation.
    glScaled(m_camera->GetScale(), m_camera->GetScale(), 0.0);                          // Scale
    glTranslated(m_camera->GetTranslation().m_x, m_camera->GetTranslation().m_y, 0.0);  // Translation

    // Draw

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

    // Selection rectangle
    glLineWidth(1.0);
    glColor4d(0.0, 0.5, 1.0, 1.0);
    glBegin(GL_LINE_LOOP);
    glVertex2d(m_selectionRect.m_x, m_selectionRect.m_y);
    glVertex2d(m_selectionRect.m_x, m_selectionRect.m_y + m_selectionRect.m_height);
    glVertex2d(m_selectionRect.m_x + m_selectionRect.m_width, m_selectionRect.m_y + m_selectionRect.m_height);
    glVertex2d(m_selectionRect.m_x + m_selectionRect.m_width, m_selectionRect.m_y);
    glEnd();
    glColor4d(0.0, 0.5, 1.0, 0.3);
    glBegin(GL_QUADS);
    glVertex2d(m_selectionRect.m_x, m_selectionRect.m_y);
    glVertex2d(m_selectionRect.m_x, m_selectionRect.m_y + m_selectionRect.m_height);
    glVertex2d(m_selectionRect.m_x + m_selectionRect.m_width, m_selectionRect.m_y + m_selectionRect.m_height);
    glVertex2d(m_selectionRect.m_x + m_selectionRect.m_width, m_selectionRect.m_y);
    glEnd();

    glFlush();  // Sends all pending information directly to the GPU.
    m_glCanvas->SwapBuffers();
    event.Skip();
}

void Workspace::SetViewport()
{
    glClearColor(1.0, 1.0, 1.0, 1.0);  // White background.
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_LINE_SMOOTH);

    double width = m_glCanvas->GetSize().x - 1;
    double height = m_glCanvas->GetSize().y - 1;

    // Viewport fit the screen.
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, height, 0.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Workspace::OnLeftClickDown(wxMouseEvent& event)
{
    bool foundElement = false;
    if(m_mode == MODE_INSERT || m_mode == MODE_DRAG_INSERT) {
        // Get the last element inserted on the list.
        Element* newElement = *(m_elementList.end() - 1);
        for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
            Element* element = *it;
            // Clicked in any element.
            if(element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
                // Click at a bus.
                if(typeid(*element) == typeid(Bus)) {
                    // Select the bus.
                    element->SetSelected();
                    foundElement = true;  // Element found.
                    // Add the new element's parent. If the element being inserted returns true, back to
                    // edit mode.
                    if(newElement->AddParent(element, m_camera->ScreenToWorld(event.GetPosition()))) {
                        ValidateElementsVoltages();
                        m_mode = MODE_EDIT;
                    }
                }
            }
        }
        // The line element can have an indefined number of points.
        if(!foundElement) {
            if(typeid(*newElement) == typeid(Line)) {
                newElement->AddPoint(m_camera->ScreenToWorld(event.GetPosition()));
            }
        }
        foundElement = true;
    } else {
        bool clickPickbox = false;
        for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
            Element* element = *it;
            element->ResetPickboxes();  // Reset pickbox state.

            // Set movement initial position (not necessarily will be moved).
            element->StartMove(m_camera->ScreenToWorld(event.GetPosition()));

            // Click in selected element node.
            if(element->NodeContains(m_camera->ScreenToWorld(event.GetPosition())) != 0 && element->IsSelected()) {
                m_mode = MODE_MOVE_NODE;
                foundElement = true;
            }

            // Click in an element.
            else if(element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
                if(!foundElement) {
                    // Select and show pickbox.
                    element->SetSelected();
                    element->ShowPickbox();
                    foundElement = true;
                }
                // If pickbox contains the click, move the pickbox
                if(element->PickboxContains(m_camera->ScreenToWorld(event.GetPosition()))) {
                    m_mode = MODE_MOVE_PICKBOX;
                    clickPickbox = true;
                }
                // If didn't found a pickbox, move the element
                if(!clickPickbox) {
                    m_mode = MODE_MOVE_ELEMENT;
                }
            }

            // Click in a switch.
            else if(element->SwitchesContains(m_camera->ScreenToWorld(event.GetPosition()))) {
                element->SetOnline(element->IsOnline() ? false : true);
            }
        }
    }

    if(!foundElement) {
        m_mode = MODE_SELECTION_RECT;
        m_startSelRect = m_camera->ScreenToWorld(event.GetPosition());
    }

    Redraw();
    UpdateStatusBar();
    event.Skip();
}

void Workspace::OnLeftDoubleClick(wxMouseEvent& event)
{
    for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
        Element* element = *it;

        // Click in an element.
        if(element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
            bool elementIsBus = false;
            Bus oldBus;
            if(typeid(*element) == typeid(Bus)) {
                elementIsBus = true;
                oldBus = *(Bus*)element;
            }
            element->ShowForm(this, element);

            // If the edited element is a bus and was changed the nominal voltage, this voltage must be
            // propagated through the lines
            if(elementIsBus) {
                // The voltage was changed
                if(oldBus.GetEletricalData().nominalVoltage != ((Bus*)element)->GetEletricalData().nominalVoltage ||
                   oldBus.GetEletricalData().nominalVoltageUnit !=
                       ((Bus*)element)->GetEletricalData().nominalVoltageUnit) {
                    // Check if the lines has this bus as parent
                    for(auto it = m_elementList.begin(); it != m_elementList.end(); it++) {
                        Element* child = *it;

                        bool elementIsParent = false;
                        if(typeid(*child) == typeid(Line)) {
                            for(int i = 0; i < (int)child->GetParentList().size(); i++) {
                                Element* parent = child->GetParentList()[i];
                                if(parent == element) {
                                    // TODO: Ask the user if he wants to change all
                                    // voltages
                                    ValidateBusesVoltages(element);
                                    elementIsParent = true;
                                }
                            }
                        }
                        if(elementIsParent) break;
                    }
                }
                ValidateElementsVoltages();
            }
        }

        // Click in a switch.
        else if(element->SwitchesContains(m_camera->ScreenToWorld(event.GetPosition()))) {
            element->SetOnline(element->IsOnline() ? false : true);
        }
    }
}

void Workspace::OnRightClickDown(wxMouseEvent& event)
{
    bool redraw = false;
    if(m_mode == MODE_EDIT) {
        for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
            Element* element = *it;
            if(element->IsSelected()) {
                // Show context menu.
                if(element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
                    element->ShowPickbox(false);
                    wxMenu menu;
                    if(element->GetContextMenu(menu)) {
                        menu.SetClientData(element);
                        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Workspace::OnPopupClick), NULL,
                                     this);
                        PopupMenu(&menu);
                        redraw = true;
                    }
                    element->ResetPickboxes();

                    if(redraw) {
                        Redraw();
                        redraw = false;
                    }
                    // If the last element was removed using the menu, we must leave the "search for" to
                    // prevent error.
                    break;
                }
            }
        }
    }
}

void Workspace::OnLeftClickUp(wxMouseEvent& event)
{
    // This event (under certain conditions) deselects the elements and back to edit mode or select the elements using
    // the selection rectangle.
    bool foundPickbox = false;
    bool findNewParent = false;
    bool updateVoltages = false;
    auto itnp = m_elementList.begin();

    for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
        Element* element = *it;

        // The user was moving a pickbox.
        if(m_mode == MODE_MOVE_PICKBOX) {
            // Catch only the element that have the pickbox shown.
            if(element->IsPickboxShown()) {
                // If the element is a bus, check if a node is outside.
                if(typeid(*element) == typeid(Bus)) {
                    // Get all the bus children.
                    for(int i = 0; i < (int)m_elementList.size(); i++) {
                        Element* child = m_elementList[i];
                        for(int j = 0; j < (int)child->GetParentList().size(); j++) {
                            Element* parent = child->GetParentList()[j];
                            // The child have a parent that is the element.
                            if(parent == element) {
                                child->UpdateNodes();
                            }
                        }
                    }
                }
            }
        }

        if(m_mode == MODE_SELECTION_RECT) {
            if(element->Intersects(m_selectionRect)) {
                element->SetSelected();
            } else {
                element->SetSelected(false);
            }
        } else if(m_mode == MODE_MOVE_NODE) {
            if(element->IsSelected()) {
                for(int i = 0; i < (int)m_elementList.size(); i++) {
                    Element* parent = m_elementList[i];
                    if(typeid(*parent) == typeid(Bus)) {
                        if(element->SetNodeParent(parent)) {
                            findNewParent = true;
                            itnp = it;
                            element->ResetNodes();
                            break;
                        }
                    }
                }
                // element->ResetNodes();
            }
        } else {
            // Deselect
            if(!event.ControlDown()) {
                if(!element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
                    element->SetSelected(false);
                }
            }

            if(element->PickboxContains(m_camera->ScreenToWorld(event.GetPosition()))) {
                foundPickbox = true;
            } else {
                element->ShowPickbox(false);
                element->ResetPickboxes();
            }
        }
    }
    if(findNewParent) {
        std::rotate(itnp, itnp + 1, m_elementList.end());
        updateVoltages = true;
    }
    if(!foundPickbox) {
        SetCursor(wxCURSOR_ARROW);
    }

    if(m_mode != MODE_INSERT) {
        m_mode = MODE_EDIT;
    }

    if(updateVoltages) {
        ValidateElementsVoltages();
    }
    m_selectionRect = wxRect2DDouble(0, 0, 0, 0);
    Redraw();
    UpdateStatusBar();
}

void Workspace::OnMouseMotion(wxMouseEvent& event)
{
    bool redraw = false;
    switch(m_mode) {
        case MODE_INSERT: {
            Element* newElement = *(m_elementList.end() - 1);  // Get the last element in the list.
            newElement->SetPosition(m_camera->ScreenToWorld(event.GetPosition()));
            redraw = true;
        } break;

        case MODE_DRAG:
        case MODE_DRAG_INSERT: {
            m_camera->SetTranslation(event.GetPosition());
            redraw = true;
        } break;

        case MODE_EDIT: {
            bool foundPickbox = false;
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* element = *it;
                if(element->IsSelected()) {
                    // Show element pickbox (when it has) if the mouse is over the selected object.
                    if(element->Contains(m_camera->ScreenToWorld(event.GetPosition()))) {
                        element->ShowPickbox();
                        redraw = true;

                        // If the mouse is over a pickbox set correct mouse cursor.
                        if(element->PickboxContains(m_camera->ScreenToWorld(event.GetPosition()))) {
                            foundPickbox = true;
                            SetCursor(element->GetBestPickboxCursor());
                        } else if(!foundPickbox) {
                            SetCursor(wxCURSOR_ARROW);
                            element->ResetPickboxes();
                        }
                    } else if(!foundPickbox) {
                        if(element->IsPickboxShown()) redraw = true;

                        element->ShowPickbox(false);
                        element->ResetPickboxes();
                        SetCursor(wxCURSOR_ARROW);
                    }
                }
            }
        } break;

        case MODE_MOVE_NODE: {
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* element = *it;
                if(element->IsSelected()) {
                    element->MoveNode(NULL, m_camera->ScreenToWorld(event.GetPosition()));
                    redraw = true;
                }
            }
        } break;

        case MODE_MOVE_PICKBOX: {
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* element = *it;
                if(element->IsSelected()) {
                    element->MovePickbox(m_camera->ScreenToWorld(event.GetPosition()));
                    redraw = true;
                }
            }
        } break;

        case MODE_MOVE_ELEMENT: {
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* element = *it;
                // Parent's element moving...
                for(int i = 0; i < (int)element->GetParentList().size(); i++) {
                    Element* parent = element->GetParentList()[i];
                    if(parent) {
                        if(parent->IsSelected()) {
                            element->MoveNode(parent, m_camera->ScreenToWorld(event.GetPosition()));
                        }
                    }
                }
                if(element->IsSelected()) {
                    element->Move(m_camera->ScreenToWorld(event.GetPosition()));
                    redraw = true;
                }
            }
        } break;

        case MODE_SELECTION_RECT: {
            wxPoint2DDouble currentPos = m_camera->ScreenToWorld(event.GetPosition());
            double x, y, w, h;
            if(currentPos.m_x < m_startSelRect.m_x) {
                x = currentPos.m_x;
                w = m_startSelRect.m_x - currentPos.m_x;
            } else {
                x = m_startSelRect.m_x;
                w = currentPos.m_x - m_startSelRect.m_x;
            }
            if(currentPos.m_y < m_startSelRect.m_y) {
                y = currentPos.m_y;
                h = m_startSelRect.m_y - currentPos.m_y;
            } else {
                y = m_startSelRect.m_y;
                h = currentPos.m_y - m_startSelRect.m_y;
            }

            m_selectionRect = wxRect2DDouble(x, y, w, h);
            redraw = true;
        } break;
    }

    if(redraw) Redraw();
    m_camera->UpdateMousePosition(event.GetPosition());
    UpdateStatusBar();
    event.Skip();
}

void Workspace::OnMiddleDown(wxMouseEvent& event)
{
    // Set to drag mode.
    if(m_mode != MODE_INSERT && m_mode != MODE_DRAG_INSERT) {
        m_mode = MODE_DRAG;
    } else {
        m_mode = MODE_DRAG_INSERT;
    }
    m_camera->StartTranslation(m_camera->ScreenToWorld(event.GetPosition()));
    UpdateStatusBar();
}
void Workspace::OnMiddleUp(wxMouseEvent& event)
{
    if(m_mode != MODE_INSERT && m_mode != MODE_DRAG_INSERT) {
        // Set to edit mode back.
        m_mode = MODE_EDIT;
    } else if(m_mode == MODE_DRAG_INSERT) {
        m_mode = MODE_INSERT;
    }
    UpdateStatusBar();
}
void Workspace::OnScroll(wxMouseEvent& event)
{
    if(event.GetWheelRotation() > 0)
        m_camera->SetScale(event.GetPosition(), +0.05);
    else
        m_camera->SetScale(event.GetPosition(), -0.05);

    UpdateStatusBar();
    Redraw();
}

void Workspace::OnKeyDown(wxKeyEvent& event)
{
    char key = event.GetUnicodeKey();
    if(key != WXK_NONE) {
        switch(key) {
            case WXK_ESCAPE:  // Cancel operations.
            {
                if(m_mode == MODE_INSERT) {
                    m_elementList.pop_back();  // Removes the last element being inserted.
                    m_mode = MODE_EDIT;
                    Redraw();
                }
            } break;
            case WXK_DELETE:  // Delete selected elements
            {
                DeleteSelectedElements();
            } break;
            case 'F': {
                if(event.GetModifiers() == wxMOD_SHIFT) {
                    Fit();
                }
            } break;
            case 'R':  // Rotate the selected elements.
            {
                RotateSelectedElements(event.GetModifiers() != wxMOD_SHIFT);
            } break;
            case 'B':  // Insert a bus.
            {
                if(m_mode != MODE_INSERT) {
                    Bus* newBus = new Bus(m_camera->ScreenToWorld(event.GetPosition()),
                                          wxString::Format(_("Bus %d"), GetElementNumber(ID_BUS)));
                    IncrementElementNumber(ID_BUS);
                    m_elementList.push_back(newBus);
                    m_mode = MODE_INSERT;
                    m_statusBar->SetStatusText(_("Insert Bus: Click to insert, ESC to cancel."));
                    Redraw();
                }
            } break;
            case 'L': {
                if(m_mode != MODE_INSERT) {
                    if(event.GetModifiers() == wxMOD_SHIFT) {  // Insert a load.
                        Load* newLoad = new Load(wxString::Format(_("Load %d"), GetElementNumber(ID_LOAD)));
                        IncrementElementNumber(ID_LOAD);
                        m_elementList.push_back(newLoad);
                        m_mode = MODE_INSERT;
                        m_statusBar->SetStatusText(_("Insert Load: Click on a buses, ESC to cancel."));
                    } else {  // Insert a power line.
                        Line* newLine = new Line(wxString::Format(_("Line %d"), GetElementNumber(ID_LINE)));
                        IncrementElementNumber(ID_LINE);
                        m_elementList.push_back(newLine);
                        m_mode = MODE_INSERT;
                        m_statusBar->SetStatusText(_("Insert Line: Click on two buses, ESC to cancel."));
                    }
                    Redraw();
                }
            } break;
            case 'T':  // Insert a transformer.
            {
                if(m_mode != MODE_INSERT) {
                    Transformer* newTransformer =
                        new Transformer(wxString::Format(_("Transformer %d"), GetElementNumber(ID_TRANSFORMER)));
                    IncrementElementNumber(ID_TRANSFORMER);
                    m_elementList.push_back(newTransformer);
                    m_mode = MODE_INSERT;
                    m_statusBar->SetStatusText(_("Insert Transformer: Click on two buses, ESC to cancel."));
                    Redraw();
                }
            } break;
            case 'G':  // Insert a generator.
            {
                if(m_mode != MODE_INSERT) {
                    SyncGenerator* newGenerator =
                        new SyncGenerator(wxString::Format(_("Generator %d"), GetElementNumber(ID_SYNCGENERATOR)));
                    IncrementElementNumber(ID_SYNCGENERATOR);
                    m_elementList.push_back(newGenerator);
                    m_mode = MODE_INSERT;
                    m_statusBar->SetStatusText(_("Insert Generator: Click on a buses, ESC to cancel."));
                    Redraw();
                }
            } break;
            case 'I': {
                if(m_mode != MODE_INSERT) {
                    if(event.GetModifiers() == wxMOD_SHIFT) {  // Insert an inductor.
                        Inductor* newInductor = new Inductor(wxString::Format(_("Inductor %d"), GetElementNumber(ID_INDUCTOR)));
                        IncrementElementNumber(ID_INDUCTOR);
                        m_elementList.push_back(newInductor);
                        m_mode = MODE_INSERT;
                        m_statusBar->SetStatusText(_("Insert Inductor: Click on a buses, ESC to cancel."));
                    } else  // Insert an induction motor.
                    {
                        IndMotor* newIndMotor = new IndMotor();
                        m_elementList.push_back(newIndMotor);
                        m_mode = MODE_INSERT;
                        m_statusBar->SetStatusText(_("Insert Induction Motor: Click on a buses, ESC to cancel."));
                    }
                    Redraw();
                }
            } break;
            case 'K':  // Insert a synchronous condenser.
            {
                if(m_mode != MODE_INSERT) {
                    SyncMotor* newSyncCondenser = new SyncMotor();
                    m_elementList.push_back(newSyncCondenser);
                    m_mode = MODE_INSERT;
                    m_statusBar->SetStatusText(_("Insert Synchronous Condenser: Click on a buses, ESC to cancel."));
                    Redraw();
                }
            } break;
            case 'C': {
                if(m_mode != MODE_INSERT) {
                    if(event.GetModifiers() == wxMOD_SHIFT) {  // Insert a capacitor.
                        Capacitor* newCapacitor = new Capacitor(wxString::Format(_("Capacitor %d"), GetElementNumber(ID_CAPACITOR)));
                        IncrementElementNumber(ID_CAPACITOR);
                        m_elementList.push_back(newCapacitor);
                        m_mode = MODE_INSERT;
                        m_statusBar->SetStatusText(_("Insert Capacitor: Click on a buses, ESC to cancel."));
                        Redraw();
                    }
                }
            } break;
            default:
                break;
        }
    }

    UpdateStatusBar();
    event.Skip();
}

void Workspace::UpdateStatusBar()
{
    switch(m_mode) {
        case MODE_DRAG: {
            m_statusBar->SetStatusText(_("MODE: DRAG"), 1);
        } break;

        case MODE_INSERT:
        case MODE_DRAG_INSERT: {
            m_statusBar->SetStatusText(_("MODE: INSERT"), 1);
        } break;

        case MODE_MOVE_ELEMENT:
        case MODE_MOVE_PICKBOX:
        case MODE_MOVE_NODE:
        case MODE_SELECTION_RECT:
        case MODE_EDIT: {
            m_statusBar->SetStatusText(wxT(""));
            m_statusBar->SetStatusText(_("MODE: EDIT"), 1);
        } break;
    }

    m_statusBar->SetStatusText(wxString::Format(_("ZOOM: %d%%"), (int)(m_camera->GetScale() * 100.0)), 2);
    m_statusBar->SetStatusText(
        wxString::Format(wxT("X: %.1f  Y: %.1f"), m_camera->GetMousePosition().m_x, m_camera->GetMousePosition().m_y),
        3);
}

void Workspace::OnPopupClick(wxCommandEvent& event)
{
    wxMenu* menu = (wxMenu*)event.GetEventObject();
    Element* element = (Element*)menu->GetClientData();
    switch(event.GetId()) {
        case ID_EDIT_BUS: {
            wxMessageBox("Edit bus!");
        } break;
        case ID_EDIT_LINE: {
            wxMessageBox("Edit line!");
        } break;
        case ID_EDIT_TRANSFORMER: {
            wxMessageBox("Edit transformer!");
        } break;
        case ID_LINE_ADD_NODE: {
            Line* line = (Line*)element;
            line->AddNode(m_camera->GetMousePosition());
            Redraw();
        } break;
        case ID_LINE_REMOVE_NODE: {
            Line* line = (Line*)element;
            line->RemoveNode(m_camera->GetMousePosition());
            Redraw();
        } break;
        case ID_ROTATE_CLOCK: {
            element->Rotate();
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* iElement = *it;
                // Parent's element rotating...
                for(int i = 0; i < (int)iElement->GetParentList().size(); i++) {
                    Element* parent = iElement->GetParentList()[i];
                    if(parent == element) {
                        iElement->RotateNode(parent);
                    }
                }
            }
            Redraw();
        } break;
        case ID_ROTATE_COUNTERCLOCK: {
            element->Rotate(false);
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* iElement = *it;
                // Parent's element rotating...
                for(int i = 0; i < (int)iElement->GetParentList().size(); i++) {
                    Element* parent = iElement->GetParentList()[i];
                    if(parent == element) {
                        iElement->RotateNode(parent, false);
                    }
                }
            }
            Redraw();
        } break;
        case ID_DELETE: {
            for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
                Element* iElement = *it;

                if(element == iElement) {
                    for(auto itp = m_elementList.begin(); itp != m_elementList.end(); ++itp) {
                        Element* child = *itp;
                        // Parent's element being deleted...
                        for(int i = 0; i < (int)child->GetParentList().size(); i++) {
                            Element* parent = child->GetParentList()[i];
                            if(parent == element) {
                                child->RemoveParent(parent);
                            }
                        }
                    }
                    m_elementList.erase(it--);
                }
            }
        } break;
    }
}

void Workspace::RotateSelectedElements(bool clockwise)
{
    for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
        Element* element = *it;
        // Parent's element rotating...
        for(int i = 0; i < (int)element->GetParentList().size(); i++) {
            Element* parent = element->GetParentList()[i];
            if(parent) {  // Check if parent is not null
                if(parent->IsSelected()) {
                    element->RotateNode(parent, clockwise);
                    // Update the positions used on motion action, the element will not be necessarily
                    // moved.
                    element->StartMove(m_camera->GetMousePosition());
                }
            }
        }
        if(element->IsSelected()) {
            element->Rotate(clockwise);
            element->StartMove(m_camera->GetMousePosition());
        }
    }
    Redraw();
}

void Workspace::DeleteSelectedElements()
{
    for(auto it = m_elementList.begin(); it != m_elementList.end(); ++it) {
        Element* element = *it;

        if(element->IsSelected()) {
            for(auto itp = m_elementList.begin(); itp != m_elementList.end(); ++itp) {
                Element* child = *itp;
                // Parent's element being deleted...
                for(int i = 0; i < (int)child->GetParentList().size(); i++) {
                    Element* parent = child->GetParentList()[i];
                    if(parent == element) {
                        child->RemoveParent(parent);
                    }
                }
            }
            m_elementList.erase(it--);
        }
    }
    Redraw();
}

void Workspace::Fit()
{
    if(m_elementList.size() > 0) {
        wxPoint2DDouble leftUpCorner(0, 0);
        wxPoint2DDouble rightDownCorner(0, 0);
        m_elementList[0]->CalculateBoundaries(leftUpCorner, rightDownCorner);

        for(auto it = m_elementList.begin() + 1; it != m_elementList.end(); it++) {
            Element* element = *it;
            wxPoint2DDouble leftUp;
            wxPoint2DDouble rightDown;
            element->CalculateBoundaries(leftUp, rightDown);
            if(leftUp.m_x < leftUpCorner.m_x) leftUpCorner.m_x = leftUp.m_x;
            if(leftUp.m_y < leftUpCorner.m_y) leftUpCorner.m_y = leftUp.m_y;
            if(rightDown.m_x > rightDownCorner.m_x) rightDownCorner.m_x = rightDown.m_x;
            if(rightDown.m_y > rightDownCorner.m_y) rightDownCorner.m_y = rightDown.m_y;
        }

        int width = 0.0;
        int height = 0.0;
        GetSize(&width, &height);

        double scaleX = double(width) / (rightDownCorner.m_x - leftUpCorner.m_x);
        double scaleY = double(height) / (rightDownCorner.m_y - leftUpCorner.m_y);

        double scale = scaleX < scaleY ? scaleX : scaleY;
        if(scale > m_camera->GetZoomMax()) scale = m_camera->GetZoomMax();
        if(scale < m_camera->GetZoomMin()) scale = m_camera->GetZoomMin();

        m_camera->SetScale(scale);

        m_camera->StartTranslation(leftUpCorner);
        m_camera->SetTranslation(wxPoint2DDouble(0, 0));
        Redraw();
    }
}

void Workspace::ValidateBusesVoltages(Element* initialBus)
{
    double nominalVoltage = ((Bus*)initialBus)->GetEletricalData().nominalVoltage;
    ElectricalUnit nominalVoltageUnit = ((Bus*)initialBus)->GetEletricalData().nominalVoltageUnit;

    for(auto it = m_elementList.begin(); it != m_elementList.end(); it++) {
        Element* child = *it;

        if(typeid(*child) == typeid(Line)) {
            if(child->GetParentList()[0] && child->GetParentList()[1]) {
                BusElectricalData data1 = ((Bus*)child->GetParentList()[0])->GetEletricalData();
                BusElectricalData data2 = ((Bus*)child->GetParentList()[1])->GetEletricalData();

                if(data1.nominalVoltage != data2.nominalVoltage ||
                   data1.nominalVoltageUnit != data2.nominalVoltageUnit) {
                    data1.nominalVoltage = nominalVoltage;
                    data2.nominalVoltage = nominalVoltage;
                    data1.nominalVoltageUnit = nominalVoltageUnit;
                    data2.nominalVoltageUnit = nominalVoltageUnit;

                    ((Bus*)child->GetParentList()[0])->SetElectricalData(data1);
                    ((Bus*)child->GetParentList()[1])->SetElectricalData(data2);

                    it = m_elementList.begin();  // Restart search.
                }
            }
        }
    }

    // ValidateElementsVoltages();
}

void Workspace::ValidateElementsVoltages()
{
    for(auto it = m_elementList.begin(); it != m_elementList.end(); it++) {
        Element* child = *it;

        std::vector<double> nominalVoltage;
        std::vector<ElectricalUnit> nominalVoltageUnit;
        for(int i = 0; i < (int)child->GetParentList().size(); i++) {
            Bus* parent = (Bus*)child->GetParentList()[i];
            if(parent) {
                nominalVoltage.push_back(parent->GetEletricalData().nominalVoltage);
                nominalVoltageUnit.push_back(parent->GetEletricalData().nominalVoltageUnit);
            }
        }
        child->SetNominalVoltage(nominalVoltage, nominalVoltageUnit);
    }
}