-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApplicationManager.cpp
More file actions
680 lines (600 loc) · 15.5 KB
/
ApplicationManager.cpp
File metadata and controls
680 lines (600 loc) · 15.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
#include "ApplicationManager.h"
#include "Actions\AddANDgate2.h"
#include "Actions\AddINVgate.h"
#include "Actions\AddNORgate2.h"
#include "Actions\AddXORgate3.h"
#include "Actions\AddConnection.h"
#include "Actions\AddBUFFgate.h"
#include "Actions\AddNANDgate2.h"
#include "Actions\AddANDgate3.h"
#include "Actions\AddLED.h"
#include "Actions\AddSwitch.h"
#include "Actions\AddORgate2.h"
#include "Actions\AddXORgate2.h"
#include "Actions\AddXNORgate2.h"
#include "Actions\AddNORgate3.h"
#include "Actions\AddLabel.h"
#include "Actions\SwitchMode.h"
#include "Select.h"
#include "Actions/EditLabel.h"
#include "Actions/Copy.h"
#include "Actions/Paste.h"
#include "Actions/Cut.h"
#include "Actions/Delete.h"
#include "Actions/Load.h"
#include "Actions/Save.h"
#include <fstream>
using namespace std;
#include "Actions/Undo.h"
#include "Actions/Redo.h"
#include "Actions/EditConnection.h"
#include "Actions/Exit.h"
ApplicationManager::ApplicationManager()
{
CompCount = 0;
RecycleBinIndex = 0;
mode = DESIGN;
for (int i = 0; i < MaxCompCount; i++)
{
CompList[i] = NULL;
RecycleBin[i] = NULL;
}
//Creates the Input / Output Objects & Initialize the GUI
OutputInterface = new Output();
InputInterface = OutputInterface->CreateInput();
//initializing ComponentIsSelected to NULL
ComponentIsSelected = NULL;
//initializing ComponenetIsCopied to NULL
ComponenetIsCopied_Cut = NULL;
CopyOrcut = 0;// 0 is the default value of this integer(no component is copied or cut),
//1 indicates a component is copied,
//2 indicates that a component is cut
CopyOrCut_ptr = &CopyOrcut;//Pointer to CopyOrcut
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::AddComponent(Component* pComp)
{
if(!dynamic_cast<Connection*>(pComp))
pComp->Set_Comp_ID(CompCount);
CompList[CompCount++] = pComp;
}
////////////////////////////////////////////////////////////////////
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:
pAct = new AddBUFFgate(this);
EmptyRecycleBin();
break;
case ADD_INV:
pAct = new AddINVgate(this);
EmptyRecycleBin();
break;
case ADD_AND_GATE_2:
pAct= new AddANDgate2(this);
EmptyRecycleBin();
break;
case ADD_NAND_GATE_2:
pAct = new AddNANDgate2(this);
EmptyRecycleBin();
break;
case ADD_OR_GATE_2:
pAct = new AddORgate2(this);
EmptyRecycleBin();
break;
case ADD_NOR_GATE_2:
pAct = new AddNORgate2(this);
EmptyRecycleBin();
break;
case ADD_XOR_GATE_2:
pAct = new AddXORgate2(this);
EmptyRecycleBin();
break;
case ADD_XNOR_GATE_2:
pAct = new AddXNORgate2(this);
EmptyRecycleBin();
break;
case ADD_AND_GATE_3:
pAct = new AddANDgate3(this);
EmptyRecycleBin();
break;
case ADD_NOR_GATE_3:
pAct = new AddNORgate3(this);
EmptyRecycleBin();
break;
case ADD_XOR_GATE_3:
pAct = new AddXORgate3(this);
EmptyRecycleBin();
break;
case ADD_Switch:
pAct = new AddSwitch(this);
EmptyRecycleBin();
break;
case ADD_LED:
pAct = new AddLED(this);
EmptyRecycleBin();
break;
case ADD_CONNECTION:
pAct = new AddConnection(this);
EmptyRecycleBin();
break;
//############################################################
case SELECT:
pAct = new Select(this, ComponentIsSelected, r_GfxInfoUsed, mode);
break;
case SIM_MODE:
mode = SIMULATION;
pAct = new SwitchMode(this, SIMULATION);
break;
case DSN_MODE:
mode = DESIGN;
pAct = new SwitchMode(this, DESIGN);
break;
case ADD_Label:
pAct = new AddLabel(this, ComponentIsSelected);
break;
case EDIT_Label:
pAct = new EditLabel(this, ComponentIsSelected);
break;
case EDIT_Connection:
pAct = new EditConnection(this, ComponentIsSelected);
break;
case COPY:
ComponenetIsCopied_Cut = NULL;
pAct = new Copy(this, ComponentIsSelected, ComponenetIsCopied_Cut, CopyOrCut_ptr);
break;
case PASTE:
pAct = new Paste(this, ComponentIsSelected, ComponenetIsCopied_Cut, CopyOrCut_ptr);
break;
case CUT:
ComponenetIsCopied_Cut = NULL;
pAct = new Cut(this, ComponentIsSelected, ComponenetIsCopied_Cut, CopyOrCut_ptr);
break;
case DEL:
pAct = new Delete(this, ComponentIsSelected);
break;
case UNDO:
pAct = new undo(this);
break;
case REDO:
pAct = new redo(this);
break;
case SAVE:
pAct = new Save(this);
break;
case LOAD:
pAct = new Load(this);
break;
case EXIT:
pAct = new Exit(this);
break;
}
if(pAct)
{
pAct->Execute();
delete pAct;
pAct = NULL;
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::UpdateInterface()
{
OutputInterface->ClearDrawingArea();
for (int i = 0; i < CompCount; i++)
{
if (mode == DESIGN)
{
CompList[i]->Draw(OutputInterface);
}
else
{
CompList[i]->Operate();
CompList[i]->Draw(OutputInterface);
//to update the drawing area after calling the operate function of each gate
//because some of the components inside the compList won't catch
//the change in Pins status instantaneously
Refresh();
}
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::Refresh()
{
for (int i = 0; i < CompCount; i++)
{
CompList[i]->Operate();
CompList[i]->Draw(OutputInterface);
}
}
////////////////////////////////////////////////////////////////////
void ApplicationManager::TurnOffComponents()
{
//this is used when switching back to designing mode
//to turn off the switches and the LEDs
for (int i = 0; i < CompCount; i++)
{
if (CompList[i] != NULL)
{
if (dynamic_cast<Switch*>(CompList[i]))
{
((Switch*)CompList[i])->Setswitch(LOW);
}
else if (dynamic_cast<LED*>(CompList[i]))
{
((LED*)CompList[i])->setInputPinStatus(1, LOW);
}
}
}
}
////////////////////////////////////////////////////////////////////
Input* ApplicationManager::GetInput()
{
return InputInterface;
}
////////////////////////////////////////////////////////////////////
Output* ApplicationManager::GetOutput()
{
return OutputInterface;
}
////////////////////////////////////////////////////////////////////
ApplicationManager::~ApplicationManager()
{
//Delete Connections first
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<Connection*>(CompList[i]))
{
delete CompList[i];
CompList[i] = NULL;
}
}
//Delete the Rest of the Components
for (int i = 0; i < CompCount; i++)
{
if(CompList[i])
delete CompList[i];
}
delete OutputInterface;
delete InputInterface;
}
Component* ApplicationManager::getComponent(int x, int y, GraphicsInfo& r_GfxInfo)
{
Component* component = NULL;
for (int i = 0; i < CompCount; i++)
{
if (CompList[i] != NULL)
{
if (CompList[i]->InArea(x, y))
{
component = CompList[i];
r_GfxInfo = CompList[i]->getLocation();
break;
}
}
}
return component;
}
Component* ApplicationManager:: getComponentBy_ID(int id)
{
//used in the loading process
for (int i = 0; i < CompCount; i++)
{
if(CompList[i]->Get_Comp_Id()==id)
return CompList[i];
}
}
void ApplicationManager::UnselectOtherComponents(Component* newSelectedComp)
{
// this functions unselects previously selected components
// it is used when selecting a new component to unselect the previous one
for (int i = 0; i < CompCount; i++)
{
if (CompList[i] != NULL)
{
if (newSelectedComp != CompList[i] && CompList[i]->getIsSelected())
{
CompList[i]->setIsSelected(false);
break;
}
}
}
}
void ApplicationManager::UnselectComponent()
{
// this functions unselects the selected component, if existed
if(ComponentIsSelected)
ComponentIsSelected->setIsSelected(false);
ComponentIsSelected = NULL;
}
bool ApplicationManager::IsCompListEmpty()
{
//check if the component list is empty or not
//it returns true if it's empty
//this function is used in Exit class
if (CompCount == 0)
return true;
else
return false;
}
void ApplicationManager::Remove(Component*& comp)
{
if (comp)
{
int i;
for (i = 0; i < CompCount; i++)
{
if (CompList[i] == comp)
break;
}
if (!dynamic_cast<Connection*>(CompList[i]))
{
//if the component is a gate or switch or LED
//instead of deleting we put it in a RecycleBin so the user can undo
AddToRecycleBin(comp);
}
else
{
delete CompList[i];
}
comp = NULL;
// if the component is not the last one in the array
// then shift every element to left
// else just delete it and reduce CompCount by 1
// CompCount is sent by reference so we can modify its value
// inside the application manager
if (i < CompCount)
{
for (int j = i; j < CompCount; j++)
{
// shift every element to the left
CompList[j] = CompList[j + 1];
}
CompCount--;
}
}
}
void ApplicationManager::Remove_Connections(OutputPin* SrcPin, InputPin* DesPin)
{
for (int i = 0; i < CompCount; i++)
{
if (CompList[i] && dynamic_cast<Connection*>(CompList[i]))
{
if (SrcPin == ((Connection*)CompList[i])->getSourcePin() || DesPin == ((Connection*)CompList[i])->getDestPin())
{
delete CompList[i];
CompList[i] = NULL;
for (int j = i; j < CompCount; j++)
{
// shift every element to the left
CompList[j] = CompList[j + 1];
}
//Reducing i-- because I want to check also the shifted components
//if I didn't reduce i-- that could cause that a connection
//not getting deleted, because in the shifting process
//we shift every element to the left, and the shifted the first element
//could be a connection, and when the loop continues it will skip
//the first shifted element in the array
i--;
CompCount--;
}
}
}
}
void ApplicationManager::AddToRecycleBin(Component* ComponentRemoved)
{
if (ComponentRemoved)
{
if (ComponentRemoved->getIsSelected())
{
ComponentRemoved->setIsSelected(false);
}
RecycleBin[RecycleBinIndex++] = ComponentRemoved;
}
}
void ApplicationManager::undofn()
{
if (CompCount > 0 && !dynamic_cast<Connection*>(CompList[CompCount - 1]) && CompList[CompCount - 1])
{
Remove(CompList[CompCount - 1]);
OutputInterface->PrintMsg("");
}
}
void ApplicationManager::redofn()
{
if (RecycleBinIndex > 0 && RecycleBin[RecycleBinIndex - 1])
{
AddComponent(RecycleBin[RecycleBinIndex - 1]);
RecycleBin[RecycleBinIndex - 1] = NULL;
RecycleBinIndex--;
}
else if (RecycleBinIndex == 0 && RecycleBin[RecycleBinIndex - 1])
{
AddComponent(RecycleBin[RecycleBinIndex - 1]);
RecycleBin[RecycleBinIndex - 1] = NULL;
}
else
OutputInterface->PrintMsg("Redo action cannot be done, Undo first to be able to Redo");
}
void ApplicationManager::EmptyRecycleBin()
{
for (int i = 0; i < RecycleBinIndex; i++)
{
if (RecycleBin[i])
{
delete RecycleBin[i];
RecycleBin[i] = NULL;
}
}
}
//-------------------------------Save and Load functions-------------------------------
void ApplicationManager::save(ofstream&outputFile)
{
int CountComp_no_Connections=0;
//count number of Components that are not Connections
for (int i = 0; i < CompCount; i++)
{
if (!dynamic_cast<Connection*>(CompList[i]))
CountComp_no_Connections++;
}
outputFile << CountComp_no_Connections <<endl;
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<Switch*>(CompList[i]))
{
CompList[i]->SaveComponent(outputFile);
}
}
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<Gate*>(CompList[i]))
{
CompList[i]->SaveComponent(outputFile);
}
}
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<LED*>(CompList[i]))
{
CompList[i]->SaveComponent(outputFile);
}
}
outputFile << "Connections" << endl;
for (int i = 0; i < CompCount; i++)
{
if (dynamic_cast<Connection*>(CompList[i]))
{
CompList[i]->SaveComponent(outputFile);
}
}
outputFile << "-1" << endl;
}
void ApplicationManager::load(ifstream& inputFiles)
{
string Component_type;
int count_Comp_inFile;
inputFiles >> count_Comp_inFile;
int Comp_type;
GraphicsInfo GInfo_SaveOrLoad_Dummy;
GInfo_SaveOrLoad_Dummy.x1 = 0;
GInfo_SaveOrLoad_Dummy.x2 = 0;
GInfo_SaveOrLoad_Dummy.y1 = 0;
GInfo_SaveOrLoad_Dummy.y2 = 0;
//load Each component (Gates, Led and Switches) by calling its Load function
for (int i = 0;i < count_Comp_inFile;i++)
{
inputFiles >> Comp_type;//take the Component Type Number
if (Comp_type == _BUFF)//identifying the Component Type using the Type Number(from file) with the enum ComponentType
{
BUFF* pCompLoad = new BUFF(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _INV)
{
INV* pCompLoad = new INV(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _AND2)
{
AND2* pCompLoad = new AND2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _OR2)
{
OR2* pCompLoad = new OR2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _NAND2)
{
NAND2* pCompLoad = new NAND2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _NOR2)
{
NOR2* pCompLoad = new NOR2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _XOR2)
{
XOR2* pCompLoad = new XOR2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _XNOR2)
{
XNOR2* pCompLoad = new XNOR2(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _AND3)
{
AND3* pCompLoad = new AND3(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _NOR3)
{
NOR3* pCompLoad = new NOR3(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _XOR3)
{
XOR3* pCompLoad = new XOR3(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _Switch)
{
Switch* pCompLoad = new Switch(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
else if (Comp_type == _LED)
{
LED* pCompLoad = new LED(GInfo_SaveOrLoad_Dummy, AND2_FANOUT);
AddComponent(pCompLoad);
pCompLoad->LoadComponent(inputFiles);
}
}
//load Connections
string DUMMMY;//just for the word "Connections" in the file
inputFiles >> DUMMMY;
//load each Connection till the file reach -1
while (!inputFiles.eof())
{
Connection* pCompLoad = new Connection(GInfo_SaveOrLoad_Dummy, NULL, NULL);
int* Source_Dest_ids_Pin = pCompLoad->LoadComponent(inputFiles);
if (Source_Dest_ids_Pin == NULL)
{
break;
}
else
{
int Source_Comp_=Source_Dest_ids_Pin[0];
int Dest_Comp_ = Source_Dest_ids_Pin[1];
int Pin_Num = Source_Dest_ids_Pin[2];
Component* Comp_Source;
Component* Comp_Destination;
Comp_Source = getComponentBy_ID(Source_Comp_);
Comp_Destination = getComponentBy_ID(Dest_Comp_);
delete pCompLoad;
pCompLoad = NULL;
Action* p_action = new AddConnection(this, Comp_Source, Comp_Destination, Pin_Num);
p_action->Execute();
}
}
}