forked from ahmedasad236/Logic_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationManager.cpp
More file actions
990 lines (932 loc) · 21.5 KB
/
ApplicationManager.cpp
File metadata and controls
990 lines (932 loc) · 21.5 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
#include "ApplicationManager.h"
#include "AllActions.h"
#include <stack>
ApplicationManager::ApplicationManager()
{
UI.addedGates = 0;
UI.firstGates = 0;
UI.addedTools = 0;
CompCount = 0;
delCount = 0;
actionCount = 0;
selectedCount = 0;
copiedCount = 0;
multiSelectVar = 0;
MultipleDelete = 0;
deletedConnectionCount = 0;
for (int i = 0; i < MaxCompCount; i++)
{
CompList[i] = NULL;
DeletedList[i] = NULL;
SelectedComponents[i] = NULL;
Copiedlist[i] = NULL;
}
for (int i = 0; i < 50; i++)
{
DeletedConnection[i][0] = NULL;
DeletedConnection[i][1] = NULL;
DeletedConnection[i][2] = NULL;
}
//Creates the Input / Output Objects & Initialize the GUI
OutputInterface = new Output();
InputInterface = OutputInterface->CreateInput();
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::AddComponent(Component* pComp)
{
if (pComp != NULL) {
CompList[CompCount] = pComp;
CompCount++;
}
Unselect();
}
////////////////////////////////////////////////////////////////////
ActionType ApplicationManager::GetUserAction()
{
//Call input to get what action is reuired from the user
return InputInterface->GetUserAction();
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::ExecuteAction(ActionType ActType)
{
Action* pAct = NULL;
switch (ActType)
{
case ADD_Buff:
AddToActionList(ADD_Buff);
pAct = new AddBUFFER(this);
break;
case ADD_INV:
AddToActionList(ADD_INV);
pAct = new AddNOTgate(this);
break;
case ADD_AND_GATE_2:
AddToActionList(ADD_AND_GATE_2);
pAct = new AddANDgate2(this);
break;
case ADD_OR_GATE_2:
AddToActionList(ADD_OR_GATE_2);
pAct = new AddORgate2(this);
break;
case ADD_NAND_GATE_2:
AddToActionList(ADD_NAND_GATE_2);
pAct = new AddNANDgate2(this);
break;
case ADD_NOR_GATE_2:
AddToActionList(ADD_NOR_GATE_2);
pAct = new AddNORgate2(this);
break;
case ADD_XNOR_GATE_2:
AddToActionList(ADD_XNOR_GATE_2);
pAct = new AddXNORgate2(this);
break;
case ADD_XOR_GATE_2:
AddToActionList(ADD_XOR_GATE_2);
pAct = new AddXORgate2(this);
break;
case ADD_AND_GATE_3:
AddToActionList(ADD_AND_GATE_3);
pAct = new AddANDgate3(this);
break;
case ADD_NOR_GATE_3:
AddToActionList(ADD_NOR_GATE_3);
pAct = new AddNORgate3(this);
break;
case ADD_XOR_GATE_3:
AddToActionList(ADD_XOR_GATE_3);
pAct = new AddXORgate3(this);
break;
case ADD_Switch:
AddToActionList(ADD_Switch);
pAct = new AddSWITCH(this);
break;
case ADD_LED:
AddToActionList(ADD_LED);
pAct = new AddLED(this);
break;
case ADD_CONNECTION:
AddToActionList(ADD_CONNECTION);
pAct = new AddConnection(this);
break;
case ADD_Label:
pAct = new Label(this);
break;
case EDIT_CONNECTION:
//AddToActionList(EDIT_CONNECTION);
pAct = new Edit(this);
break;
case ITM_SCROLL_1:
UI.firstGates = 0;
UI.addedGates = 1;
OutputInterface->CreateGatesToolBar(UI.firstGates);
break;
case ITM_SCROLL_2:
UI.firstGates = 1;
UI.addedGates = 1;
OutputInterface->CreateGatesToolBar(UI.firstGates);
break;
case UNDO:
pAct = new undo(this);
break;
case REDO:
pAct = new redo(this);
break;
case ADD_GATE:
pAct = new AddGate(this);
break;
case Create_TruthTable:
pAct = new TruthTable(this);
break;
case Change_Switch:
break;
case DEL:
AddToActionList(DEL);
lastDeletedCount = 0;
pAct = new Delete(this);
break;
case SELECT:
multiSelectVar = 0;
pAct = new Select(this);
break;
case VALIDATE:
pAct = new Validate(this);
break;
case COPY:
pAct = new Copy(this);
break;
case CUT:
pAct = new Cut(this);
break;
case SAVE:
pAct = new SaveFile(this);
break;
case LOAD:
pAct = new Load(this);
break;
case PASTE:
AddToActionList(PASTE);
pAct = new Paste(this);
break;
case EXIT:
pAct = new Exit(this);
break;
case SIMULATE:
pAct = new simulate(this);
break;
case MULTISELECT:
multiSelectVar = 1;
pAct = new Select(this);
break;
case DSN_MODE:
UI.AppMode = DESIGN;
UI.addedGates = 1;
UI.firstGates = 1;
OutputInterface->CreateDesignToolBar();
OutputInterface->CreateToolsToolBar();
OutputInterface->CreateGatesToolBar(UI.firstGates);
break;
case ADD_TOOLS:
UI.addedTools = 1;
OutputInterface->CreateToolsToolBar();
}
if (pAct != NULL)
{
pAct->Execute();
delete pAct;
pAct = NULL;
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::UpdateInterface()
{
for (int i = 0; i < CompCount; i++) {
if (UI.AppMode == DESIGN)
CompList[i]->Draw(OutputInterface, CompList[i]->isSelected);
else
{
if (dynamic_cast<SWITCH*> (CompList[i]) != NULL)
CompList[i]->Draw(OutputInterface, (bool)CompList[i]->GetOutPinStatus());
else if (dynamic_cast<led*>(CompList[i]) != NULL)
CompList[i]->Draw(OutputInterface, (bool)CompList[i]->GetInputPinStatus(0));
else if (dynamic_cast<Connection*>(CompList[i]) != NULL)
CompList[i]->Draw(OutputInterface, (bool)CompList[i]->GetOutPinStatus());
else
CompList[i]->Draw(OutputInterface, 0);
}
}
}
////////////////////////////////////////////////////////////////////
Input* ApplicationManager::GetInput()
{
return InputInterface;
}
ActionType ApplicationManager::getTheLastActionDone()
{
return ActionList.top();
}
ActionType ApplicationManager::getTheLastUndo()
{
return UndoList.top();
}
Component* ApplicationManager::getTheLastComponent()
{
return CompList[CompCount - 1];
}
void ApplicationManager::makeSelectedComponent(int x, int y)
{
/*if (!multiSelectVar)
{
selectedCount = 0;
for (int i = 0; i < CompCount; i++) CompList[i]->isSelected = 0;
}*/
for (int i = 0; i < CompCount; i++)
{
if (CompList[i]->ClickedHere(x, y))
{
if (!multiSelectVar)
setOtherSelectedToFalse(CompList[i]);
if (CompList[i]->isSelected == true && UI.AppMode == DESIGN)
{
CompList[i]->isSelected = false;
SelectedComponents[selectedCount--] = NULL;
return;
}
else
{
CompList[i]->isSelected = true;
SelectedComponents[selectedCount++] = CompList[i];
return;
}
}
}
}
void ApplicationManager::ExcuteSelectedSwitch()
{
Component* pSwitch = dynamic_cast<SWITCH*>(getSelectedComponent());
if (pSwitch != NULL)
{
pSwitch->Operate();
Action* pAct = new simulate(this);
pAct->Execute();
delete pAct;
}
}
////////////////////////////////////////////////////////////////////
Output* ApplicationManager::GetOutput()
{
return OutputInterface;
}
////////////////////////////////////////////////////////////////////
Component* ApplicationManager::getTheComponentClickedOn()
{
selectedCount = 0;
for (int i = 0; i < CompCount; i++)
{
CompList[i]->isSelected = 0;
}
Action* pAct;
InputInterface->GetUserAction();
pAct = new Select(this);
pAct->Execute();
UpdateInterface();
delete pAct;
for (int i = 0; i < CompCount; i++)
{
if (CompList[i]->isSelected)
return CompList[i];
}
return NULL;
}
void ApplicationManager::Undo(ActionType AType)
{
if (ActionList.size() >= 1)
{
Component* Comp = getTheLastComponent();
AddToUndoList(AType);
if (AType == ADD_CONNECTION)
{
if (dynamic_cast<Connection*>(Comp) != NULL)
NewConnection((Connection*)Comp);
}
DeleteCompWithConnections(Comp);
RemoveFromActionList();
}
}
void ApplicationManager::Redo(ActionType AType)
{
if (UndoList.size() >= 1)
{
Component* Comp = getTheLastDeletedComponent();
if (AType == ADD_CONNECTION)
{
Connection* Conn = (Connection*)DeletedConnection[deletedConnectionCount - 1][0];
Action* pAct = new AddConnection(this, DeletedConnection[deletedConnectionCount - 1][1], DeletedConnection[deletedConnectionCount - 1][2], Conn->getNumOfInputPin());
pAct->Execute();
int count = delCount;
delete pAct;
for (int i = 0; i < count; i++)
{
if (DeletedList[i] == Comp)
{
DeletedList[i] = DeletedList[delCount];
DeletedList[delCount] = NULL;
delCount--;
}
}
Component* temp = getTheLastDeletedComponent();
RedoConnection();
}
else
{
DeleteFromDeletedList(Comp);
}
if (dynamic_cast<Connection*>(Comp) == NULL)
DrawComponentLabel(Comp, Comp->getLabel());
else
DrawConnectionLabel(Comp, Comp->getLabel());
AddToActionList(AType);
RemoveFromUndoList();
}
}
void ApplicationManager::NewConnection(Connection* Conn)
{
DeletedConnection[deletedConnectionCount][0] = Conn;
for (int i = 0; i < CompCount; i++)
{
Gate* G = dynamic_cast<Gate*>(CompList[i]);
if (G != NULL && G->getOutputPin()->ConnectedTo((Connection*)DeletedConnection[deletedConnectionCount][0]))
{
DeletedConnection[deletedConnectionCount][1] = G;
break;
}
}
Connection* C = (Connection*)DeletedConnection[deletedConnectionCount][0];
DeletedConnection[deletedConnectionCount][2] = Conn->getDestPin()->getComponent();
deletedConnectionCount++;
}
void ApplicationManager::RedoConnection()
{
DeletedConnection[deletedConnectionCount - 1][0] = NULL;
DeletedConnection[deletedConnectionCount - 1][1] = NULL;
DeletedConnection[deletedConnectionCount - 1][2] = NULL;
deletedConnectionCount--;
}
void ApplicationManager::AddToCopiedList(Component* pComp)
{
Copiedlist[copiedCount++] = pComp;
}
void ApplicationManager::PasteFromCopiedList(GraphicsInfo g1)
{
if (Copiedlist[0] != NULL) {
if (!cut)
{
for (int i = 0; i < 1; i++)
{
makeNewComponent(Copiedlist[copiedCount - 1]);
}
}
else
{
makeNewComponent(Copiedlist[copiedCount - 1]);
AddToDeletedList(Copiedlist[copiedCount - 1]);
DeleteFromCompList(Copiedlist[copiedCount - 1]);
cut = false;
}
}
else {
OutputInterface->PrintMsg("Make Copy or Cut Actions first");
return;
}
}
void ApplicationManager::PasteCutComponent(GraphicsInfo G)
{
AddToDeletedList(Copiedlist[copiedCount - 1]);
DeleteFromCompList(Copiedlist[copiedCount - 1]);
Copiedlist[copiedCount - 1]->SetGraphicsInfo(G);
AddComponent(Copiedlist[copiedCount - 1]);
cut = false;
}
int ApplicationManager::getlastDeletedCount()
{
return lastDeletedCount;
}
void ApplicationManager::incrementLastDeletedCount()
{
lastDeletedCount++;
}
void ApplicationManager::setLastDeletedCount(int l)
{
lastDeletedCount = l;
}
Component* ApplicationManager::getSelectedComponent()
{
for (int i = 0; i < CompCount; i++)
if (CompList[i]->isSelected)
return CompList[i];
return nullptr;
}
Component* ApplicationManager::getTheComponentConnectedTo(Connection* c)
{
for (int i = 0; i < CompCount; i++) {
Gate* temp = dynamic_cast<Gate*>(CompList[i]);
if (temp != NULL && temp->ConnectedTo(c)) {
return CompList[i];
}
}
return NULL;
}
void ApplicationManager::AddToDeletedList(Component* pComp)
{
DeletedList[delCount++] = pComp;
}
void ApplicationManager::AddToActionList(ActionType ActType)
{
ActionList.push(ActType);
actionCount++;
}
void ApplicationManager::RemoveFromActionList()
{
ActionList.pop();
actionCount--;
}
int ApplicationManager::getActionListSize()
{
return ActionList.size();
}
void ApplicationManager::AddToUndoList(ActionType ActType)
{
UndoList.push(ActType);
}
void ApplicationManager::RemoveFromUndoList()
{
UndoList.pop();
}
int ApplicationManager::getUndoListSize()
{
return UndoList.size();
}
void ApplicationManager::DeleteFromCompList(Component* pComp)
{
for (int i = 0; i < CompCount; i++)
{
if (CompList[i] == pComp)
{
GraphicsInfo r_GfxInfo = CompList[i]->GetGraphicsInfo();
Connection* comp = dynamic_cast<Connection*> (CompList[i]);
DeleteComponentLabel(CompList[i]);
CompList[i]->isDeleted = 1;
CompList[i] = CompList[CompCount - 1];
CompList[CompCount - 1] = NULL;
CompCount--;
if (comp == NULL)
{
OutputInterface->DrawRectangle(r_GfxInfo);
}
else
{
OutputInterface->DrawConnection(r_GfxInfo, false, color(117, 117, 117));
Gate* temp = dynamic_cast<Gate*>(comp->getDestPin()->getComponent());
if (temp != NULL)
{
temp->freeInputPin(comp->getNumOfInputPin());
}
for (int j = 0; j < CompCount; j++)
{
Gate* temp2 = dynamic_cast<Gate*> (CompList[j]);
if (temp2 != NULL)
{
if (temp2->ConnectedTo(comp))
{
temp2->decrementFanOut();
}
}
}
}
return;
}
}
}
void ApplicationManager::DeleteFromDeletedList(Component* pComp)
{
int count = delCount;
for (int i = 0; i < count; i++)
{
if (DeletedList[i] == pComp)
{
AddComponent(DeletedList[i]);
DeletedList[i] = DeletedList[delCount - 1];
DeletedList[delCount - 1] = NULL;
delCount--;
}
}
}
void ApplicationManager::DeleteConnections(Component* Comp)
{
vector<Component*> deletedConnections;
for (int i = 0; i < CompCount; i++)
{
Connection* Conn = dynamic_cast<Connection*>(CompList[i]);
if (Conn != NULL)
{
if (Conn->getDestPin()->getComponent() == Comp)
{
deletedConnections.push_back(CompList[i]);
}
Gate* temp = dynamic_cast<Gate*>(Comp);
if (temp != NULL && temp->ConnectedTo(Conn))
{
deletedConnections.push_back(CompList[i]);
}
}
}
for (int i = 0 ; i < deletedConnections.size() ; i++)
{
AddToActionList(DEL);
DeleteCompWithConnections(deletedConnections[i]);
}
}
void ApplicationManager::DeleteCompWithConnections(Component* Comp)
{
lastDeletedCount = 0;
MultipleDelete = false;
if (Comp != NULL)
{
MultipleDelete = true;
this->DeleteConnections(Comp);
AddToDeletedList(Comp);
DeleteFromCompList(Comp);
lastDeletedCount++;
return;
}
int n = selectedCount;
if (n > 1)
MultipleDelete = true;
RemoveFromActionList();
for (int i = 0; i < n; i++)
{
AddToActionList(DEL);
this->DeleteConnections(SelectedComponents[i]);
AddToDeletedList(SelectedComponents[i]);
DeleteFromCompList(SelectedComponents[i]);
lastDeletedCount++;
}
}
void ApplicationManager::DeleteConnectionLabel(Component* Comp)
{
GraphicsInfo LabelGfx = Comp->GetLabelGraphics();
OutputInterface->DrawRectangle(LabelGfx);
}
void ApplicationManager::save(ofstream& f)
{
f << CompCount << endl;
for (int i = 0; i < CompCount; i++) {
if(dynamic_cast<Gate *>(CompList[i]) != NULL){
CompList[i]->save(f);
}
}
f << "CONNECTIONS\n";
for (int i = 0; i < CompCount; i++)
{
Connection* Conn = dynamic_cast<Connection*> (CompList[i]);
if (Conn != NULL)
{
Conn->save(f, this->getTheComponentConnectedTo(Conn));
}
}
}
Component* ApplicationManager::getTheLastDeletedComponent()
{
return DeletedList[delCount - 1];
}
void ApplicationManager::setOtherSelectedToFalse(Component* pComp)
{
selectedCount = 0;
for (int i = 0; i < CompCount; i++) {
if (CompList[i] != pComp)
CompList[i]->isSelected = false;
}
}
Component* ApplicationManager::getLastDeletedComponent()
{
return DeletedList[delCount - 1];
}
void ApplicationManager::setSelectedCount(int s)
{
selectedCount = s;
}
int ApplicationManager::getSelectedCount()
{
return selectedCount;
}
int ApplicationManager::getDeletedCount()
{
return delCount;
}
void ApplicationManager::Unselect()
{
selectedCount = 0;
for (int i = 0; i < CompCount; i++) {
CompList[i]->isSelected = false;
SelectedComponents[i] = NULL;
}
}
int ApplicationManager::setSwitches()
{
thisLevel.clear();
for (int i = 0; i < CompCount; i++)
{
Component* temp = dynamic_cast<SWITCH*> (CompList[i]);
if (temp != NULL)
{
thisLevel.push_back(temp);
}
}
return thisLevel.size();
}
void ApplicationManager::makeNewComponent(Component* C)
{
Action* pAct = NULL;
if (dynamic_cast<AND2*> (C) != NULL)
{
pAct = new AddANDgate2(this);
}
else if (dynamic_cast<buffer*> (C) != NULL)
{
pAct = new AddBUFFER(this);
}
else if (dynamic_cast<NOT*> (C) != NULL)
{
pAct = new AddNOTgate(this);
}
else if (dynamic_cast<OR2*> (C) != NULL)
{
pAct = new AddORgate2(this);
}
else if (dynamic_cast<NAND2*> (C) != NULL)
{
pAct = new AddNANDgate2(this);
}
else if (dynamic_cast<NOR2*> (C) != NULL)
{
pAct = new AddNORgate2(this);
}
else if (dynamic_cast<XNOR2*> (C) != NULL)
{
pAct = new AddXNORgate2(this);
}
else if (dynamic_cast<XOR2*> (C) != NULL)
{
pAct = new AddXORgate2(this);
}
else if (dynamic_cast<AND3*> (C) != NULL)
{
pAct = new AddANDgate3(this);
}
else if (dynamic_cast<NOR3*> (C) != NULL)
{
pAct = new AddNORgate3(this);
}
else if (dynamic_cast<XOR3*> (C) != NULL)
{
pAct = new AddXORgate3(this);
}
else if (dynamic_cast<SWITCH*> (C) != NULL)
{
pAct = new AddSWITCH(this);
}
else if (dynamic_cast<led*> (C) != NULL)
{
pAct = new AddLED(this);
}
if (pAct != NULL) {
pAct->Execute();
CompList[CompCount - 1]->setLabel(Copiedlist[copiedCount - 1]->getLabel());
DrawComponentLabel(CompList[CompCount - 1], Copiedlist[copiedCount - 1]->getLabel());
}
delete pAct;
}
void ApplicationManager::makeCopyOfSelectedComponents()
{
copiedCount = 0;
for (int i = 0; i < CompCount; i++)
{
if (CompList[i]->isSelected)
{
AddToCopiedList(CompList[i]);
}
}
}
void ApplicationManager::OrganizeComponents()
{
setSwitches();
bool Organized = 0;
while (!Organized)
{
Organized = organizeLevel();
}
}
string ApplicationManager::makeOutputForOneCase(string str)
{
setSwitches();
int n = thisLevel.size();
for (int i = 0; i < n; i++)
{
int temp = 0;
if (str[i] == '1') temp = 1;
if (thisLevel[i]->GetOutPinStatus() != temp)
{
thisLevel[i]->Operate();
}
printf("%d ", thisLevel[i]->GetOutPinStatus());
}
str += "|";
OrganizeComponents();
setLeds();
for (auto i : thisLevel)
{
if (i->GetInputPinStatus(0)) str += "1";
else str += "0";
printf("%d ", i->GetInputPinStatus(0));
}
printf("\n");
setSwitches();
return str;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::setLeds()
{
thisLevel.clear();
for (int i = 0; i < CompCount; i++)
{
Component* temp = dynamic_cast<led*> (CompList[i]);
if (temp != NULL)
{
thisLevel.push_back(temp);
}
}
}
// take setSwitches and createTruthTable from your code makeOutPutforOneCase
string ApplicationManager::CreateTruthTable(int switchNum , string str )
{
setSwitches();
if (switchNum == thisLevel.size())
{
string temp = makeOutputForOneCase(str);
return temp;
}
return CreateTruthTable(switchNum + 1, str + "0") + "\n" + CreateTruthTable(switchNum + 1, str + "1");
}
bool ApplicationManager::isValidCircuit()
{
bool isSwitch = 0, isLed = 0;
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<SWITCH*>(CompList[i]) != NULL) isSwitch = 1;
else if (dynamic_cast<led*>(CompList[i]) != NULL) isLed = 1;
Gate* temp = dynamic_cast<Gate*> (CompList[i]);
if (temp != NULL && !temp->ValidConnection())
return 0;
}
if(isSwitch && isLed)
return 1;
return 0;
}
bool ApplicationManager::organizeLevel()
{
for (int i = 0; i < CompCount; i++) CompList[i]->newLevel = 0;
for (auto i : thisLevel)
{
i->setAllNewLevels();
i->setAllStatus();
}
thisLevel.clear();
bool allLed = 1;
for (int i = 0; i < CompCount; i++)
{
if (CompList[i]->newLevel )
{
if (CompList[i]->ValidComponent())
{
CompList[i]->Operate();
thisLevel.push_back(CompList[i]);
}
if (dynamic_cast<led*>(CompList[i]) == NULL)
{
allLed = 0;
}
}
}
return allLed;
}
void ApplicationManager::DeleteComponentLabel(Component* Comp)
{
GraphicsInfo LabelGfx = Comp->GetLabelGraphics();
OutputInterface->DrawRectangle(LabelGfx);
}
void ApplicationManager::DrawComponentLabel(Component* Comp, string word)
{
GraphicsInfo LabelGfx;
Comp->setLabel(word);
int Len = UI.AND2_Width;
int Wdth = UI.AND2_Height;
float x, y;
float Cx = (Comp->GetGraphicsInfo().x1 + Comp->GetGraphicsInfo().x2) / 2.0;
float Cy = Comp->GetGraphicsInfo().y2;
if (word.size() <= 7) {
x = Cx - (Len / 2.0) + ((Len / 7.0) * (7 - word.size()) / 2.4);
y = (Cy - (1.36 * Wdth));
Gate* GateSwitch = dynamic_cast<Gate*>(Comp);
if (GateSwitch != NULL)
if (dynamic_cast<SWITCH*>(GateSwitch) != NULL)
y = (Cy - 1.3 * Wdth);
}
else
{
x = Cx - Len / 2.0 - (((Len / 7.0) * (word.size() - 7)) / 1.8);
y = (Cy - (1.36 * Wdth));
Gate* GateSwitch = dynamic_cast<Gate*>(Comp);
if (GateSwitch != NULL)
if (dynamic_cast<SWITCH*>(GateSwitch) != NULL)
y = (Cy - 1.3 * Wdth);
}
LabelGfx.x1 = x;
LabelGfx.x2 = x + (word.size() * (Len / 5.0));
LabelGfx.y1 = Comp->GetGraphicsInfo().y1;
LabelGfx.y2 = y;
Comp->setLabelGraphics(LabelGfx);
OutputInterface->printMsgOnDrawingArea(word, x, y);
Unselect();
}
void ApplicationManager::DrawConnectionLabel(Component* Comp, string word)
{
GraphicsInfo LabelGfx;
float Gx1 = Comp->GetGraphicsInfo().x1;
float Gx2 = Comp->GetGraphicsInfo().x2;
float Gy1 = Comp->GetGraphicsInfo().y1;
float Gy2 = Comp->GetGraphicsInfo().y2;
Comp->setLabel(word);
int Len = UI.AND2_Width;
int Wdth = UI.AND2_Height;
float x, y;
float Cx = (Gx1 + Gx2) / 2.0;
float Cy;
if (Gx1 < Gx2)
Cy = Gy1 < Gy2 ? Gy1 : Gy2;
else
Cy = Gy2;
if (word.size() <= 7) {
x = Cx - (Len / 2.0) + ((Len / 7.0) * (7 - word.size()) / 2.4);
if (Gx1 < Gx2)
y = Cy - 17;
else
y = Cy - 6;
}
else
{
x = Cx - Len / 2.0 - (((Len / 7) * (word.size() - 7)) / 1.8);
if (Gx1 < Gx2)
y = Cy - 18;
else
y = Cy - 5;
}
LabelGfx.x1 = x;
LabelGfx.x2 = x + (word.size() * (Len / 5.0));
LabelGfx.y1 = y;
LabelGfx.y2 = y + 20;
Comp->setLabelGraphics(LabelGfx);
OutputInterface->printMsgOnDrawingArea(word, x, y);
Unselect();
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::deleteAll()
{
OutputInterface->ClearDrawingArea();
for (int i = 0; i < CompCount; i++)
{
delete CompList[i];
CompList[i] = NULL;
}
CompCount = 0;
}
Component* ApplicationManager::getComponentByID(int ID)
{
for (int i = 0; i < CompCount; i++)
{
if (CompList[i]->GetID() == ID)
return CompList[i];
}
return NULL;
}
ApplicationManager::~ApplicationManager()
{
for (int i = 0; i < CompCount; i++)
{
delete CompList[i];
}
for (int i = 0; i < delCount; i++)
{
delete DeletedList[i];
}
delete OutputInterface;
delete InputInterface;
}