-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationManager.cpp
More file actions
1209 lines (1003 loc) · 27.9 KB
/
ApplicationManager.cpp
File metadata and controls
1209 lines (1003 loc) · 27.9 KB
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ApplicationManager.h"
#include "Actions\Actionheaders.h"
#include <fstream>
ApplicationManager::ApplicationManager()
{
CompCount = 0;
nGates = 0;
for(int i=0; i<MaxCompCount; i++)
CompList[i] = NULL;
//Creates the Input / Output Objects & Initialize the GUI
OutputInterface = new Output();
InputInterface = OutputInterface->CreateInput();
CutItem = NULL;
CutElement = { NUMP , -1 };
CopyType = NUMP;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::AddComponent(Component* pComp)
{
if (dynamic_cast<Gate*>(pComp)) { nGates++; };
CompList[CompCount++] = pComp;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::InitializeGateList() {
int k = 0;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Gate*>(CompList[i])) {
GateList[k] = dynamic_cast<Gate*>(CompList[i]);
k++;
}
}
}
///////////////////////////create TT area/////////////////////////////////////////
void ApplicationManager::CreateTruth(ofstream& tt)
{
int r = 0;
GetNofSwitches();
GetNofLeds();
IntializeSwitchList();
IntializeLedList();
int c = nS;
int** comb = new int* [pow(2, nS)]; //no. of rows
for (int i = 0; i < pow(2, nS); i++) {
comb[i] = new int[nS + nL]; //no. of columns
}
while (c > 0 && r < pow(2, nS)) { //Generating Input combinations
for (int i = 0; i < pow(2, nS - c); i++) {
comb[r][c - 1] = 0;
r++;
}
for (int j = 0; j < pow(2, nS - c); j++) {
comb[r][c - 1] = 1;
r++;
}
if (r == pow(2, nS)) { c--; r = 0; }
}
//Generating Output
for (int i = 0; i < pow(2, nS); i++) { //jumps from row to another
for (int j = 0; j < nS; j++) {
STATUS s=LOW;
if (comb[i][j] == 0)
SwitchList[j]->setInputPinStatus(1, s);
else {
s = HIGH;
SwitchList[j]->setInputPinStatus(1, s);
}
}
Simulate(this).Execute();
for (int k = 0; k < nL; k++) {
comb[i][k + nS] = (LedList[k]->GetInputPinStatus(1) == HIGH) ? 1 : 0;
}
}
for (int a = 0; a < pow(2, nS); a++) {
tt << endl;
for (int b = 0; b < nS + nL; b++) {
tt << comb[a][b] << " ";
}
}
for (int i = 0; i < pow(2, nS); i++) {
delete [] comb[i];
}
delete[]comb;
}
void ApplicationManager::SortSwitchesVertically()
{
IntializeSwitchList();
int y1, y2;
Switch* S1;
Switch* S2;
for (int i = 0; i < nS; i++) {
for (int j = 0; j < nS - 1; j++) {
S1 = SwitchList[j];
y1 = S1->GetGraphicsInfo().y1;
S2 = SwitchList[j + 1];
y2 = S2->GetGraphicsInfo().y1;
if (y2 > y1) {
SwitchList[j] = S2;
SwitchList[j + 1] = S1;
}
}
}
}
void ApplicationManager::SortLedsVertically()
{
IntializeLedList();
int y1, y2;
Switch* L1;
Switch* L2;
for (int i = 0; i < nS; i++) {
for (int j = 0; j < nS - 1; j++) {
L1 = SwitchList[j];
y1 = L1->GetGraphicsInfo().y1;
L2 = SwitchList[j + 1];
y2 = L2->GetGraphicsInfo().y1;
if (y2 > y1) {
SwitchList[j] = L2;
SwitchList[j + 1] = L1;
}
}
}
}
int ApplicationManager::GetNofSwitches()
{
nS = 0;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Switch*>(CompList[i])) {
nS++;
}
}
return nS;
}
int ApplicationManager::GetNofLeds()
{
nL = 0;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Led*>(CompList[i])) {
nL++;
}
}
return nL;
}
void ApplicationManager::IntializeSwitchList()
{
int k = 0;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Switch*>(CompList[i])) {
SwitchList[k] = dynamic_cast<Switch*>(CompList[i]);
k++;
}
}
}
void ApplicationManager::IntializeLedList()
{
int k = 0;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Led*>(CompList[i])) {
LedList[k] = dynamic_cast<Led*>(CompList[i]);
k++;
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::DeleteEverthing() {
GraphicsInfo G;
int i = 0;
while (CompCount>0) {
if (CompList[i] == NULL) { i = 0; }
G=CompList[i]->GetGraphicsInfo();
DeleteComponent(G.x1 + 2, G.y1 +3);
i++;
if (i == CompCount) { i = 0; }
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::Sortelementsgraphically() {
InitializeGateList();
int x1, x2;
Gate* G1;
Gate* G2;
for (int i = 0; i < nGates; i++) {
for (int j = 0; j < nGates-1; j++) {
G1 = GateList[j];
x1 = G1->GetGraphicsInfo().x1;
G2 = GateList[j+1];
x2 = G2->GetGraphicsInfo().x1;
if (x1 > x2) {
GateList[j] = G2;
GateList[j+1] = G1;
}
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::OperateAll() {
for (int i = 0; i < nGates; i++) {
GateList[i]->Operate();
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::DeleteGate(Component* comp)
{
if (!comp) return;
Gate* g = (Gate*)(comp);
g->DeleteGate();
nGates -= 1;
FindAndReplace(g);
// Update Interface
OutputInterface->ClearDrawingArea();
UpdateInterface();
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::DeleteComponent(int Cx, int Cy)
{
int i = FindComp(Cx, Cy);
Component* comp = CompList[i];
if (dynamic_cast<Gate*> (comp)) // if it is a gate
{
DeleteGate(comp);
OutputInterface->PrintMsg("Gate is Deleted Successfully!");
}
else if (dynamic_cast<Connection*>(comp))
{
Connection* c = (Connection*)(comp);
DeleteConnection(c);
OutputInterface->PrintMsg("Connection is Deleted Successfully! ");
}
}
////////////////////////////////////////////////////////////////////
int ApplicationManager::FindComp(Component* c)
{
if (!c)
return -1;
for (int i = 0; i < CompCount; i++)
{
if (c == CompList[i])
{
return i;
}
}
}
////////////////////////////////////////////////////////////////////
int ApplicationManager::FindComp(int Cx, int Cy)
{
for (int i = 0; i < CompCount; i++)
{
Component* comp = CompList[i];
if (comp->Checkinside(Cx, Cy))
{
return i;
}
}
return -1;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::FindAndReplace(Component* c)
{
int index = FindComp(c);
if (!c || index == -1) return;
CompCount -= 1;
/*delete CompList[index];*/
CompList[index] = CompList[CompCount];
CompList[CompCount] = NULL;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::DeleteConnection(Connection* c)
{
if (!c) return;
c->DeleteConnection();
FindAndReplace(c);
// Update the interface
OutputInterface->ClearDrawingArea();
UpdateInterface();
}
////////////////////////////////////////////////////////////////////
ActionType ApplicationManager::GetUserAction()
{
//Call input to get what action is reuired from the user
return InputInterface->GetUserAction(a,b);
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::ExecuteAction(ActionType ActType)
{
Action* pAct = NULL;
switch (ActType)
{
case EDIT_Label:
if (CompCount > 0)
Edit(this).Execute();
else { OutputInterface->PrintMsg("There isn't enough gates"); }
break;
case ADD_Label:
if (CompCount > 0)
Label(this).Execute();
else { OutputInterface->PrintMsg("There isn't enough gates"); }
break;
case ADD_GATE:
OutputInterface->PrintMsg("Action: Add a gate , Click anywhere");
OutputInterface->CreateGATESToolBar();
break;
case ADD_Buff:
AddBUFFgate(this).Execute();
OutputInterface->PrintMsg("Action: Clicked Buffer Gate , Click anywhere");
break;
case ADD_INV:
AddINVgate(this).Execute();
OutputInterface->PrintMsg("Action: CLicked Inverter , Click anywhere");
break;
case ADD_AND_GATE_2:
AddANDgate2 (this).Execute();
OutputInterface->PrintMsg("Action: Clicked AND GATE with 2 inputs , Click anywhere");
break;
case ADD_OR_GATE_2:
AddORgate2(this).Execute();
OutputInterface->PrintMsg("Action: Clicked OR GATE with 2 inputs , Click anywhere");
break;
case ADD_NAND_GATE_2:
AddNANDgate2(this).Execute();
OutputInterface->PrintMsg("Action: Clicked NAND gate with 2 inputs , Click anywhere");
break;
case ADD_NOR_GATE_2:
AddNORgate2(this).Execute();
OutputInterface->PrintMsg("Action: CLicked NOR GATE with 2 inputs, Click anywhere");
break;
case ADD_XOR_GATE_2:
AddXORgate2(this).Execute();
OutputInterface->PrintMsg("Action: Clicked XOR GATE with 2 inputs , Click anywhere");
break;
case ADD_XNOR_GATE_2:
AddXNORgate2(this).Execute();
OutputInterface->PrintMsg("Action: Clicked XNOR GATE with 2 inputs , Click anywhere");
break;
case ADD_AND_GATE_3:
AddANDgate3(this).Execute();
OutputInterface->PrintMsg("Action: CLicked AND GATE with 3 inputs , Click anywhere");
break;
case ADD_XOR_GATE_3:
AddXORgate3(this).Execute();
OutputInterface->PrintMsg("Action: Clicked XOR GATE with 3 inputs, Click anywhere");
break;
case ADD_NOR_GATE_3:
AddNORgate3(this).Execute();
OutputInterface->PrintMsg("Action: Clicked NOR GATE with 3 inputs, Click anywhere");
break;
case ADD_TOOL:
OutputInterface->PrintMsg("Action: Add a tool , Click anywhere");
OutputInterface->CreateTOOLStoolbar();
break;
case ADD_CONNECTION:
if (CompCount >= 2)
CONNECT(this).Execute();
else {
OutputInterface->PrintMsg("There isn't Enough Gates");
}
break;
case ADD_Switch:
AddSwitch(this).Execute();
OutputInterface->PrintMsg("Action: Add a switch , Click anywhere");
break;
case ADD_LED:
AddLED(this).Execute();
OutputInterface->PrintMsg("Action : Add an LED , Click anywhere");
break;
case DEL:
if (!CompCount)
{
OutputInterface->PrintMsg("Not enough elments to delete. ");
}
else
{
OutputInterface->PrintMsg("Choose an element to delete .");
Delete(this).Execute();
}
break;
case MOVE:
OutputInterface->PrintMsg("Action: clicked MOVE , Click anywhere");
break;
case COPY:
if (!nGates)
{
OutputInterface->PrintMsg("There is no any gate to copy. ");
}
else
{
OutputInterface->PrintMsg("Choose an Element to Copy");
Copy(this).Execute();
OutputInterface->PrintMsg("Elmenet is added to the Clipboard! ");
}
//OutputInterface->PrintMsg("Action: clicked Copy , choose a gate to copy it ");
break;
case CUT:
if (!nGates)
{
OutputInterface->PrintMsg("Choose an Element to Cut");
OutputInterface->PrintMsg("There is no any gate to Cut. ");
}
else
{
CutA(this).Execute();
OutputInterface->PrintMsg("Elmenet is added to the Clipboard! ");
}
//OutputInterface->PrintMsg("Action: clicked Copy , choose a gate to copy it ");
break;
case PASTE:
if (CopyType == ITM_NGATES && CutElement.CutType == ITM_NGATES)
{
OutputInterface->PrintMsg("No elments in the Clipboard. ");
}
else
{
OutputInterface->PrintMsg("Choose somewhere to paste");
Paste(this).Execute();
}
break;
case LOAD:
if (CompCount == 0)
Load(this).Execute();
else
{
int d = stoi(InputInterface->GetSrting(OutputInterface, "Do you want to delete everything? 0 for no and 1 for yes."));
if (d == 1) {
DeleteEverthing();
Load(this).Execute();
}else {OutputInterface->PrintMsg("Action cancelled.");}
}
break;
case SAVE:
if (!CompCount) {
OutputInterface->PrintMsg("No Items to Save ");
}
else {
Save(this).Execute();
}
break;
case SELECT:
if (Checkinside(a, b)) {
SelectComponent(a, b);
}
else
OutputInterface->PrintMsg("A click on the design Area, Click on any Comp to highlight it.");
break;
case STATUS_BAR:
OutputInterface->PrintMsg("Action: a click on the Status Bar, Click anywhere");
break;
case DSN_TOOL:
OutputInterface->PrintMsg("Action: a click on empty area in the Tool Bar, Click anywhere");
break;
case SIM_MODE:
//SIMULATE HERE
OutputInterface->CreateSimulationToolBar();
if(ValidateCircuit())
Simulate(this).Execute();
break;
case Create_TruthTable:
OutputInterface->PrintMsg("Action : clicked Create Truth Table , Click anywhere");
CreateTT(this).Execute();
break;
case VALIDATE:
OutputInterface->PrintMsg("Validating..");
if (ValidateCircuit())
OutputInterface->PrintMsg("Circuit is Valid!");
else
OutputInterface->PrintMsg("Circuit is Invalid!");
break;
case PROBE:
OutputInterface->PrintMsg("Pls, choose a pin or a connection. ");
Probe(this).Execute();
break;
case DSN_MODE:
OutputInterface->PrintMsg("Action: Return to Design Mode, creating Design tool bar");
OutputInterface->CreateDesignToolBar();
break;
case EXIT:
break;
}
if(pAct)
{
pAct->Execute();
delete pAct;
pAct = NULL;
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::UpdateInterface()
{
OutputInterface->ClearDrawingArea();
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<Connection*>(CompList[i]))
{
Connection* c = (Connection*)(CompList[i]);
if (!c->getDestPin() || !c->getSourcePin())
{
DeleteConnection(c);
continue;
}
}
CompList[i]->Draw(OutputInterface);
}
}
////////////////////////////////////////////////////////////////////
Input* ApplicationManager::GetInput()
{
return InputInterface;
}
////////////////////////////////////////////////////////////////////
Output* ApplicationManager::GetOutput()
{
return OutputInterface;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::SelectComponent(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) {
CompList[i]->Select();
}
if (UI.AppMode == SIMULATION) {
Switch* S=dynamic_cast<Switch*>(CompList[i]);
if (CompList[i]->Checkinside(Cx, Cy) && dynamic_cast<Switch*>(CompList[i])) {
S->SwitchOn();
if(ValidateCircuit())
Simulate(this).Execute();
}
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::LabelComponent(string x, int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) {
if (CompList[i]->SetLabel(x)) {
OutputInterface->PrintMsg("Component Labeled");
}
else { OutputInterface->PrintMsg("Component Already Named"); };
break;
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::EditComponent(string x, int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) {
CompList[i]->EditLabel(x); break;
}
}
}
////////////////////////////////////////////////////////////////////
bool ApplicationManager::ChecksameComponent(int Cx1, int Cy1, int Cx2, int Cy2)
{
if (CompCount == 0) { return false; }
int i;
for (i = 0; i < CompCount; i++) {
//if It's a gate:
if (dynamic_cast<Gate*>(CompList[i])) {
if (CompList[i]->Checkinside(Cx1, Cy1)) { break; }
}
}int k;
for (k = 0; k < CompCount; k++) {
//if It's a gate:
if (dynamic_cast<Gate*>(CompList[k])) {
if (CompList[k]->Checkinside(Cx2, Cy2)) { break; }
}
}
return i == k;
}
////////////////////////////////////////////////////////////////////
InputPin* ApplicationManager::GetInputPin(int Cx, int Cy, int n) {
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) { return CompList[i]->GetInputPin(n); }
}
}
////////////////////////////////////////////////////////////////////
bool ApplicationManager::Checkinside(int Cx, int Cy) {
if (CompCount == 0) { return false; }
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) { return true; }
}
return false;
}
////////////////////////////////////////////////////////////////////
bool ApplicationManager::Checkaround(int Cx, int Cy) {
if (CompCount == 0) { return false; }
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkaround(Cx, Cy)) { return true; }
}
return false;
}
////////////////////////////////////////////////////////////////////
//1 for gate, 0 for connection
bool ApplicationManager::ConnorGate(int Cx, int Cy) {
if (CompCount == 0) { return false; }
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy)) {
if (dynamic_cast<Gate*>(CompList[i])) {
return true; //1 for gate
}
else { return false; }
}
}
}
////////////////////////////////////////////////////////////////////
OutputPin* ApplicationManager::GetOutputPin(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Gate*>(CompList[i]) && CompList[i]->Checkinside(Cx, Cy))
{
return CompList[i]->GetOutputPin();
}
}
}
////////////////////////////////////////////////////////////////////
int ApplicationManager::GetNofGate(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Gate*>(CompList[i]) && CompList[i]->Checkinside(Cx, Cy))
{
return dynamic_cast<Gate*>(CompList[i])->GetNinputs();
}
}
}
////////////////////////////////////////////////////////////////////
Connection* ApplicationManager::GetConnection(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (CompList[i]->Checkinside(Cx, Cy))
{
return dynamic_cast<Connection*>(CompList[i]);
}
}
}
////////////////////////////////////////////////////////////////////
GraphicsInfo ApplicationManager::getGraphicsInfo(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
//if It's a gate:
if (dynamic_cast<Gate*>(CompList[i])) {
if (CompList[i]->Checkinside(Cx, Cy)) { return CompList[i]->GetGraphicsInfo(); }
}
}
}
////////////////////////////////////////////////////////////////////
bool ApplicationManager::isCompfull(int Cx, int Cy) {
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Gate*>(CompList[i]) && CompList[i]->Checkinside(Cx, Cy))
{
return CompList[i]->CheckinputPins();
}
}
//if it's not a gate assume it's true
return true;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::Copy2(int Cx, int Cy)
{
int i = FindComp(Cx, Cy);
if (dynamic_cast<Gate*>(CompList[i]))
{
Gate* item = (Gate*)(CompList[i]);
CopyType = item->getType();
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::Cut2(int Cx, int Cy)
{
int i = FindComp(Cx, Cy);
if (dynamic_cast<Gate*>(CompList[i]))
{
Gate* item = (Gate*)(CompList[i]);
CutElement.CutType = item->getType();
CutElement.ind = i;
CutItem = CompList[i];
// Remove the copied element from the clipboard
CopyType = NUMP;
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::Paste2(int Cx, int Cy)
{
// Check for CopyType
if (CopyType != NUMP)
{
switch (CopyType)
{
case I_AND2:
AddANDgate2(this).Execute();
OutputInterface->PrintMsg("2-AND Gate is Pasted!");
break;
case I_BUFF:
AddBUFFgate(this).Execute();
OutputInterface->PrintMsg("Buff Gate is Pasted!");
break;
case I_INV:
AddINVgate(this).Execute();
OutputInterface->PrintMsg("INVERTER Gate is Pasted!");
break;
case I_OR2:
AddORgate2(this).Execute();
OutputInterface->PrintMsg("2-OR Gate is Pasted!");
break;
case I_NAND2:
AddNANDgate2(this).Execute();
OutputInterface->PrintMsg("2-NAND Gate is Pasted!");
break;
case I_NOR2:
AddNORgate2(this).Execute();
OutputInterface->PrintMsg("2-AND Gate is Pasted!");
break;
case I_XOR2:
AddXORgate2(this).Execute();
OutputInterface->PrintMsg("2-XOR Gate is Pasted!");
break;
case I_XNOR2:
AddXNORgate2(this).Execute();
OutputInterface->PrintMsg("2-XNOR Gate is Pasted!");
break;
case I_AND3:
AddANDgate3(this).Execute();
OutputInterface->PrintMsg("3-AND Gate is Pasted!");
break;
case I_XOR3:
AddXORgate3(this).Execute();
OutputInterface->PrintMsg("3-XOR Gate is Pasted!");
break;
case I_NOR3:
AddNORgate3(this).Execute();
OutputInterface->PrintMsg("3-NOR Gate is Pasted!");
break;
case I_SWITCH:
AddSwitch(this).Execute();
OutputInterface->PrintMsg("SWITCH Gate is Pasted!");
break;
case I_LED:
AddLED(this).Execute();
OutputInterface->PrintMsg("LED Gate is Pasted!");
break;
default:
CopyType = NUMP;
}
}
// Check for CutType
if (CutElement.CutType != NUMP && CompList[CutElement.ind] == CutItem)
{
switch (CutElement.CutType)
{
case I_AND2:
AddANDgate2(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("2-AND Gate is Pasted!");
break;
case I_BUFF:
AddBUFFgate(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("Buff Gate is Pasted!");
break;
case I_INV:
AddINVgate(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("INVERTER Gate is Pasted!");
break;
case I_OR2:
AddORgate2(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("2-OR Gate is Pasted!");
break;
case I_NAND2:
AddNANDgate2(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("2-NAND Gate is Pasted!");
break;
case I_NOR2:
AddNORgate2(this).Execute();
OutputInterface->PrintMsg("2-AND Gate is Pasted!");
break;
case I_XOR2:
AddXORgate2(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("2-XOR Gate is Pasted!");
break;
case I_XNOR2:
AddXNORgate2(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("2-XNOR Gate is Pasted!");
break;
case I_AND3:
AddANDgate3(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("3-AND Gate is Pasted!");
break;
case I_XOR3:
AddXORgate3(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("3-XOR Gate is Pasted!");
break;
case I_NOR3:
AddNORgate3(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("3-NOR Gate is Pasted!");
break;
case I_SWITCH:
AddSwitch(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("SWITCH Gate is Pasted!");
break;
case I_LED:
AddLED(this).Execute();
DeleteGate(CutItem);
OutputInterface->PrintMsg("LED Gate is Pasted!");
break;
default:
CutElement.CutType = NUMP;
CutItem = NULL;
CutElement.ind = -1;
break;
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::SaveCircuit(ofstream& outfile)
{
outfile << nGates << endl;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Gate*>(CompList[i])) {
CompList[i]->SaveComponent(outfile);
}
}
outfile << "Connections" << endl;
for (int i = 0; i < CompCount; i++) {
if (dynamic_cast<Connection*>(CompList[i])) {
CompList[i]->SaveComponent(outfile);
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::LoadCircuit(ifstream& infile)
{
bool flag=true; //used to start the for loop 1 time only, then it's set to false at the end.
string ctype, clabel;
int cID, cID2, noOfgates, noOfpin;
GraphicsInfo gfxload;
infile >> noOfgates;
while (!infile.eof()) {
if(flag)
for (int i = 0; i < noOfgates; i++) {
infile>>ctype >> cID >> clabel >> gfxload.x1 >> gfxload.y1;