-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationManager.cpp
More file actions
750 lines (657 loc) · 16.9 KB
/
Copy pathApplicationManager.cpp
File metadata and controls
750 lines (657 loc) · 16.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
#include "ApplicationManager.h"
#include "Utility\RouteFollower.h"
#include "Actions\AllActions.h"
#include "Components\AllComponents.h"
#include "Utility\RTTI.h"
//Although these are implicity included by AllComponents but I prefer to keep them
#include "Components\InputPin.h"
#include "Components\OutputPin.h"
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <exception>
#include <cstring>
using namespace std;
ApplicationManager::ApplicationManager()
{
ActionLevel = 0;
CompCount = 0;
ConnCount = 0;
memset(CompList,NULL,sizeof(CompList));
//Creates the Input / Output Objects & Initialize the GUI
OutputInterface = new Output();
InputInterface = OutputInterface->CreateInput();
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::AddComponent(Component* pComp)
{
CompList[CompCount++] = pComp;
if(is_a(pComp,Connection))
ConnCount++;
}
////////////////////////////////////////////////////////////////////
ActionType ApplicationManager::GetUserAction()
{
//Call input to get what action is reuired from the user
ActionType t;
Component* clickedcomp=NULL;
int x,y;
t=InputInterface->GetUserAction();
if(t==SELECT)
{
InputInterface->GetLastPointClicked(x,y);
clickedcomp=GetComponentAt(x,y);
if(ReturnOutputPinAT(x,y))
return ADD_CONNECTION;
if(!clickedcomp)
return SELECT;
else if(clickedcomp->is_selected() && is_not_a(clickedcomp,Connection)) //User clicked on one of the already selected component(s) -> user wants to move them
return MOVE;
return SELECT;
}
return t;
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::ExecuteAction(ActionType ActType)
{
Action* pAct = NULL;
UpdateToolBar=false;
switch (ActType)
{
case ADD_Buff:
pAct = new AddBuff(this);
break;
case ADD_INV:
pAct = new AddNOT(this);
break;
case ADD_AND_GATE_2:
pAct= new AddANDgate2(this);
break;
case ADD_OR_GATE_2:
pAct= new AddORgate2(this);
break;
case ADD_NAND_GATE_2:
pAct= new AddNANDgate2(this);
break;
case ADD_NOR_GATE_2:
pAct= new AddNORgate2(this);
break;
case ADD_XOR_GATE_2:
pAct= new AddXORgate2(this);
break;
case ADD_XNOR_GATE_2:
pAct= new AddXNORgate2(this);
break;
case ADD_AND_GATE_3:
pAct= new AddANDgate3(this);
break;
case ADD_OR_GATE_3:
pAct= new AddORgate3(this);
break;
case ADD_NOR_GATE_3:
pAct= new AddNORgate3(this);
break;
case ADD_XOR_GATE_3:
pAct= new AddXORgate3(this);
break;
case ADD_XNOR_GATE_3:
pAct= new AddXNORgate3(this);
break;
case ADD_CONNECTION:
pAct= new AddConnection(this);
break;
case ADD_Switch:
pAct= new AddSwitch(this);
break;
case ADD_LED:
pAct= new AddLed(this);
break;
case SIM_MODE:
pAct= new SimModeAction(this);
break;
case TOOLBAR_SCROLL_NEXT:
UpdateToolBar=true;
UI.ScrollCount=SCROLL(int(UI.ScrollCount)+1);
break;
case TOOLBAR_SCROLL_BACK:
UpdateToolBar=true;
UI.ScrollCount=SCROLL(int(UI.ScrollCount)-1);
break;
case TAB_SELECT_API:
UpdateToolBar=true;
UI.SelectedTab=API_TAB;
break;
case TAB_SELECT_OPTIONS:
UpdateToolBar=true;
UI.SelectedTab=OPTIONS_TAB;
break;
case Change_Mode:
UpdateToolBar=true;
if(UI.AppMode==DESIGN)
pAct= new ChangeToSimulation(this);
else{
pAct= new ChangeToDesign(this);
if(!UndoActions.empty()) UI.enable_undo=true;
if(!RedoActions.empty()) UI.enable_redo=true;
if(!ClipBoard.empty()) UI.enable_paste=true;
}
UI.SelectedTab=API_TAB;
break;
case STATUS_BAR:
OutputInterface->PrintMsg("Ready");
break;
case Create_TruthTable:
pAct= new CreateTruthTable(this);
break;
case SAVE:
UpdateToolBar=true;
pAct= new SaveFileAction(this);
break;
case LOAD:
UpdateToolBar=true;
pAct= new OpenFileAction(this);
break;
case SELECT:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Select(this);
break;
case MOVE:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Move(this);
break;
case COPY:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Copy(this);
break;
case CUT:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Cut(this);
break;
case PASTE:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Paste(this);
break;
case DEL:
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
pAct= new Delete(this);
break;
case EDIT_Label:
UpdateToolBar=true;
pAct= new Edit(this);
break;
case RECONNECT:
UpdateToolBar=true;
pAct= new EditConnection(this);
break;
case DSN_TOOL:
OutputInterface->PrintMsg("Ready");
break;
case UNDO:
UpdateToolBar=true;
this->undo();
break;
case REDO:
UpdateToolBar=true;
this->redo();
break;
case EXIT:
pAct= new Exit(this);
break;
}
if(pAct)
{
pAct->Execute();
if(pAct->CanBe_Undone())
{
if(UI.SelectedTab==OPTIONS_TAB) UpdateToolBar=true;
UndoActions.push(pAct);
flush_redo();
UI.enable_redo=false;
UI.enable_save=true;
UI.enable_undo=true;
ActionLevel++;
}
else
delete pAct;
pAct = NULL;
}
}
////////////////////////////////////////////////////////////////////
vector <Component*> ApplicationManager::GetSelectedComponentList()
{
return SelectionList;
}
void ApplicationManager::RemoveByID(int CompID)
{
for(int i=0;i<CompCount;i++)
{
if(CompList[i]->GetID()==CompID)
{
//No I am not deleting to let UNDO be able to bring it back again
//Also note that ApplicationManager is not the creater of it, thus not of
//it's responsibility to delete it
if(is_a(CompList[i],Connection))
ConnCount--;
CompList[i]=NULL;
swap(CompList[i],CompList[--CompCount]);
return;
}
}
}
void ApplicationManager::RemoveByPTR(Component* Comp)
{
for(int i=0;i<CompCount;i++)
{
if(CompList[i]==Comp)
{
//No I am not deleting to let UNDO be able to bring it back again
//Also note that ApplicationManager is not the creater of it, thus not of
//it's responsibility to delete it
if(is_a(CompList[i],Connection))
ConnCount--;
CompList[i]=NULL;
swap(CompList[i],CompList[--CompCount]);
return;
}
}
}
void ApplicationManager::SetSelectedComponentList(const vector <Component*>& list)
{
SelectionList=list;
}
void ApplicationManager::ResetSelectedComponentList()
{
for(auto& Comp:SelectionList)
Comp->un_select();
SelectionList.clear();
UI.enable_edit=UI.enable_editconn=UI.enable_copy_cut_delete=false;
}
vector <Component*> ApplicationManager::GetComponentsInsideAndSelect(const GraphicsInfo& Rectangle)
{
vector <Component*> newly_selected;
for(int i=0;i<CompCount;i++)
if(CompList[i]->set_selected(Rectangle))
newly_selected.push_back(CompList[i]);
return newly_selected;
}
void ApplicationManager::SelectComponentsInside(const GraphicsInfo& Rectangle)
{
for(int i=0;i<CompCount;i++)
CompList[i]->set_selected(Rectangle);
}
void ApplicationManager::UpdateInterface()
{
string title;
title = (UI.enable_save ? "*" : "");
title += (UI.filename=="" ? "Untitled" : UI.filename);
title += " - Logic Simulator";
OutputInterface->ChangeTitle(title);
if(UpdateToolBar)
OutputInterface->CreateTabToolbar();
FastUpdateInterface();
OutputInterface->UpdateBuffer();
}
void ApplicationManager::FastUpdateInterface()
{
OutputInterface->ClearDrawingArea();
for(int i=0; i<CompCount; i++)
CompList[i]->Draw(OutputInterface);
}
void ApplicationManager::DrawNonSelectedItems()
{
for(int i=0;i<CompCount;i++)
if(!CompList[i]->is_selected())
CompList[i]->Draw(OutputInterface);
}
////////////////////////////////////////////////////////////////////
Input* ApplicationManager::GetInput()
{
return InputInterface;
}
////////////////////////////////////////////////////////////////////
Output* ApplicationManager::GetOutput()
{
return OutputInterface;
}
////////////////////////////////////////////////////////////////////
//For UNDO/REDO
void ApplicationManager::undo()
{
Action *pAct;
pAct=UndoActions.top();
UndoActions.pop();
pAct->Undo();
RedoActions.push(pAct);
UI.enable_redo=true;
if(UndoActions.empty())
UI.enable_undo=false;
ActionLevel--;
UI.enable_save=(ActionLevel!=0);
}
void ApplicationManager::redo()
{
Action *pAct;
pAct=RedoActions.top();
RedoActions.pop();
pAct->Redo();
UndoActions.push(pAct);
UI.enable_undo=true;
UI.enable_save=true;
if(RedoActions.empty())
UI.enable_redo=false;
ActionLevel++;
UI.enable_save=(ActionLevel!=0);
}
void ApplicationManager::flush_redo()
{
Action *pAct;
while(!RedoActions.empty())
{
pAct=RedoActions.top();
delete pAct;
RedoActions.pop();
}
UI.enable_redo=false;
}
void ApplicationManager::flush_undo()
{
Action *pAct;
while(!UndoActions.empty())
{
pAct=UndoActions.top();
delete pAct;
UndoActions.pop();
}
UI.enable_undo=false;
}
void ApplicationManager::ResetActionLevel()
{
ActionLevel=0;
}
void ApplicationManager::HighlightPinsAT(int x,int y)
{
for(int i=0;i<CompCount;i++)
{
CompList[i]->HighlightPinsAT(x,y);
}
}
bool ApplicationManager::CheckConflict_GATE(const GraphicsInfo& gfx)const
{
for(int i=0;i<CompCount;i++)
{
if(!CompList[i]->CheckSpaceVacancy_GATE(gfx))
return true;
}
return false;
}
bool ApplicationManager::CheckConflict_MOVE(const GraphicsInfo& gfx)const
{
for(int i=0;i<CompCount;i++)
{
if(!CompList[i]->CheckSpaceVacancy_GATE(gfx) && !CompList[i]->is_selected() && is_not_a(CompList[i],Connection))
return true;
}
return false;
}
void ApplicationManager::SaveFile(ofstream& out)
{
out << CompCount-ConnCount << endl;
for(int i=0;i<CompCount;i++)
if(is_not_a(CompList[i],Connection))
CompList[i]->Save(out);
out << endl << "Connections" << endl;
for(int i=0;i<CompCount;i++)
if(is_a(CompList[i],Connection))
CompList[i]->Save(out);
out << endl << -1;
}
void ApplicationManager::LoadFile(ifstream& in)
{
unordered_map<int,Component*> map;
unordered_map<int,Component*>::iterator Iterator1,Iterator2;
OutputPin* src;
InputPin* des;
int n;
string Current;
Component* c;
in>>n;
for(int i=0;i<n;i++)
{
c=NULL;
in >> Current;
if(Current==AND2Type)
c=new AND2(AND2_FANOUT);
else if(Current==AND3Type)
c=new AND3(AND3_FANOUT);
else if(Current==LEDType)
c=new Led;
else if(Current==NAND2Type)
c=new NAND2(NAND2_FANOUT);
else if(Current==NOR2Type)
c=new NOR2(NOR2_FANOUT);
else if(Current==NOR3Type)
c=new NOR3(NOR3_FANOUT);
else if(Current==NOTType)
c=new NOT(NOT_FANOUT);
else if(Current==OR2Type)
c=new OR2(OR2_FANOUT);
else if(Current==OR3Type)
c=new OR3(OR3_FANOUT);
else if(Current==SWITCHType)
c=new Switch;
else if(Current==XNOR2Type)
c=new XNOR2(XNOR2_FANOUT);
else if(Current==XNOR3Type)
c=new XNOR3(XNOR3_FANOUT);
else if(Current==XOR2Type)
c=new XOR2(XOR2_FANOUT);
else if(Current==XOR3Type)
c=new XOR3(XOR3_FANOUT);
else if(Current==BUFFERType)
c=new Buffer(Buffer_FANOUT);
else
throw exception(string("Error 1: Invalid Component type: "+Current).c_str());
c->Load(in);
AddComponent(c);
map.insert(make_pair(c->GetID(),c));
}
in >> Current;
int SrcID, DesID, PinNo;
in >> SrcID;
while(SrcID!=-1)
{
des=NULL;
src=NULL;
in >> DesID >> PinNo;
Iterator1=map.find(SrcID);
Iterator2=map.find(DesID);
if(Iterator1!=map.end())
src=Iterator1->second->GetOutputPin();
else
throw exception(string("Error 2: Invalid SrcPin Component ID: "+to_string(SrcID)).c_str());
if(!src->Connectable())
throw exception(string("Error 4: SrcPin for Component ID: "+to_string(SrcID) + " maximum fanout reached").c_str());
if(Iterator2!=map.end())
des=Iterator2->second->GetInputPin(PinNo);
else
throw exception(string("Error 3: Invalid DesPin Component ID: "+to_string(DesID)).c_str());
if(des->is_connected())
throw exception(string("Error 5: Duplicate connection for DesPin for Component ID: "+to_string(SrcID) + " PinNo: "+to_string(PinNo)).c_str());
c=new Connection(src,des);
c->Load(in);
in >> SrcID;
AddComponent(c);
}
}
OutputPin* ApplicationManager::ReturnOutputPinAT(int& x,int& y)
{
OutputPin* pin=NULL;
for(int i=0;i<CompCount;i++)
{
pin=CompList[i]->ReturnOutputPinAT(x,y);
if(pin)
return pin;
}
return NULL;
}
vector <Component*> ApplicationManager::GetClipBoard()
{
return ClipBoard;
}
void ApplicationManager::SetClipBoard(const vector <Component*>& list)
{
ClipBoard=list;
}
InputPin* ApplicationManager::ReturnInputPinAT(int& x,int& y)
{
InputPin* pin=NULL;
for(int i=0;i<CompCount;i++)
{
pin=CompList[i]->ReturnInputPinAT(x,y);
if(pin)
return pin;
}
return NULL;
}
Component* ApplicationManager::GetComponentAt(int x,int y)
{
for(int i=0;i<CompCount;i++)
if(CompList[i]->isAt(x,y))
return CompList[i];
return NULL;
}
int ApplicationManager::ValidateCircuit()
{
int circuit_errors=NO_ERRORS;
int count=0;
Traverser t;
//0 Check: Check if there is a component
if(CompCount==0)
return NOTHING_TO_SIMULATE;
//1st pass Check: First Check if there is no pins are floating
//and Check for first level & last level errors
for(int i=0;i<CompCount;i++)
{
circuit_errors|=CompList[i]->Validate();
}
//Second Check for feedback error and meanwhile add levels to each gate also. i.e follow the route starting form the switches
for(int i=0;i<CompCount;i++)
{
if(is_a(CompList[i],Switch))
{
t.set_starting_point(CompList[i]);
circuit_errors|=t.Loop_detect();
OutputInterface->PrintMsg("Validating circuit... " + to_string(count*100/CompCount) + "%");
count++;
if(circuit_errors&FEEDBACK)
break;
}
}
count=t.Get_NumberOf_Visited_Components();
//No Switches
if(count==0)
circuit_errors|=FIRST_LEVEL_ERROR;
//All Components are visited which means all components are reachable from the switches
else if(count==CompCount)
return circuit_errors;
//If not, it means that some are not reachable form the switches (FIRST_LEVEL_ERROR)
circuit_errors|=FIRST_LEVEL_ERROR;
for(int i=0;i<CompCount;i++)
{
//Optionally, we may just break here but we are interseted to detect Feedbacks in them
if(CompList[i]->getLevel()==-1)
{
t.set_starting_point(CompList[i]);
circuit_errors|=t.Loop_detect();
count++;
if(circuit_errors&FEEDBACK)
break;
}
OutputInterface->PrintMsg("Validating circuit... " + to_string(count*100/CompCount) + "%");
}
return circuit_errors;
}
void ApplicationManager::OperateCicuit()
{
for(int i=0;i<CompCount;i++)
CompList[i]->Operate();
}
void ApplicationManager::CreateSimulationParameters()
{
for(int i=0;i<CompCount;i++)
{
if(is_a(CompList[i],Switch))
sim_switchs.push_back(dynamic_cast<Switch*>(CompList[i]));
else if(is_a(CompList[i],Led))
sim_leds.push_back(dynamic_cast<Led*>(CompList[i]));
}
sort(sim_switchs.begin(),sim_switchs.end(),[](Switch* s1,Switch* s2)->bool{
return s1->GetLabel() < s2->GetLabel();
});
sort(sim_leds.begin(),sim_leds.end(),[](Led* l1,Led* l2)->bool{
return l1->GetLabel() < l2->GetLabel();
});
sort(CompList,CompList+CompCount,[](Component* c1,Component* c2)->bool{
return c1->getLevel() < c2->getLevel();
});
}
vector <Switch*> ApplicationManager::get_all_switch()
{
return sim_switchs;
}
vector <Led*> ApplicationManager::get_all_leds()
{
return sim_leds;
}
void ApplicationManager::DestroySimulationParameters()
{
sim_switchs.clear();
sim_leds.clear();
for(int i=0;i<CompCount;i++)
CompList[i]->resetLevel();
}
void ApplicationManager::ResetCircuit()
{
for(int i=0;i<CompCount;i++)
CompList[i]->Reset();
}
void ApplicationManager::DestroySelectionList()
{
ResetSelectedComponentList();
}
void ApplicationManager::ResetApp()
{
flush_undo();
flush_redo();
DestroySelectionList();
DestroySimulationParameters();
for(auto& Comp:CompList)
delete Comp;
memset(CompList,NULL,sizeof(CompList));
ActionLevel=0;
CompCount=0;
ConnCount=0;
UI.enable_copy_cut_delete=false;
UI.enable_undo=false;
UI.enable_redo=false;
UI.enable_save=false;
UI.enable_edit=false;
UI.enable_editconn=false;
UI.AppMode=DESIGN;
UI.filename.clear();
Component::ResetCount();
Switch::resetSwitchCount();
Led::resetLedCount();
}
ApplicationManager::~ApplicationManager()
{
for(auto& Comp:ClipBoard)
delete Comp;
ClipBoard.clear();
flush_undo();
flush_redo();
for(auto& Comp:CompList)
delete Comp;
delete OutputInterface;
delete InputInterface;
}