-
Notifications
You must be signed in to change notification settings - Fork 7
/
SC_undocommands.cpp
1828 lines (1639 loc) · 63.4 KB
/
SC_undocommands.cpp
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
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Authors: Alex Cope, Seb James **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "SC_undocommands.h"
#include "NL_projection_and_synapse.h"
#include "SC_network_layer_rootdata.h"
#include "NL_genericinput.h"
#include "CL_classes.h"
#include "NL_connection.h"
#include "mainwindow.h"
#include "SC_component_rootcomponentitem.h"
#include "SC_projectobject.h"
// ######## DELETE SELECTION #################
delSelection::delSelection(nl_rootdata * data, QVector <QSharedPointer<systemObject> > list, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->data = data;
this->setText("delete selection");
QVector < QSharedPointer<systemObject> > populations;
QVector < QSharedPointer<systemObject> > projections;
// spawn children
for (int i = 0; i < list.size(); ++i) {
// seperate out:
if (list[i]->type == populationObject) {
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (list[i]);
populations.push_back(list[i]);
new delPopulation(data, pop, this);
}
}
for (int i = 0; i < list.size(); ++i) {
if (list[i]->type == projectionObject) {
QSharedPointer <projection> proj = qSharedPointerDynamicCast <projection> (list[i]);
bool alreadyDeleted = false;
// check if any of the populations are the source or dest of this proj
// and it has therefore already been handled
for (int j = 0; j < populations.size(); ++j)
if (proj->source == populations[j] || proj->destination == populations[j])
alreadyDeleted = true;
if (!alreadyDeleted) {
new delProjection(data, proj, this);
projections.push_back(list[i]);
}
}
}
for (int i = 0; i < list.size(); ++i) {
if (list[i]->type == inputObject) {
QSharedPointer<genericInput> input = qSharedPointerDynamicCast<genericInput> (list[i]);
// check if the input has already been handled
bool alreadyDeleted = false;
for (int j = 0; j < populations.size(); ++j) {
if (input->source == populations[j] || input->destination == populations[j])
alreadyDeleted = true;
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (populations[j]);
for (int k = 0; k <pop->projections.size(); ++k) {
QSharedPointer <projection> proj = pop->projections[k];
for (int l = 0; l < proj->synapses.size(); ++l) {
if (input->srcCmpt == proj->synapses[l]->weightUpdateCmpt || input->dstCmpt == proj->synapses[l]->weightUpdateCmpt || \
input->srcCmpt == proj->synapses[l]->postSynapseCmpt || input->dstCmpt == proj->synapses[l]->postSynapseCmpt)
alreadyDeleted = true;
}
}
for (int k = 0; k <pop->reverseProjections.size(); ++k) {
QSharedPointer <projection> proj = pop->reverseProjections[k];
for (int l = 0; l < proj->synapses.size(); ++l) {
if (input->srcCmpt == proj->synapses[l]->weightUpdateCmpt || input->dstCmpt == proj->synapses[l]->weightUpdateCmpt || \
input->srcCmpt == proj->synapses[l]->postSynapseCmpt || input->dstCmpt == proj->synapses[l]->postSynapseCmpt)
alreadyDeleted = true;
}
}
}
if (!alreadyDeleted)
new delInput(data, input, this);
}
}
}
void delSelection::undo()
{
// do children by calling parent class function:
QUndoCommand::undo();
}
void delSelection::redo()
{
// do children by calling parent class function:
QUndoCommand::redo();
}
// ######## ADD POPULATION #################
addPopulationCmd::addPopulationCmd(nl_rootdata * data, QSharedPointer<population> pop, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->pop = pop;
this->data = data;
this->setText("add population");
selIndex = 1;
isDeleted = true;
}
void addPopulationCmd::undo()
{
pop->isDeleted = true;
isDeleted = true;
// remove from system:
for (int i = 0; i < data->populations.size(); ++i) {
if (this->pop == data->populations[i])
data->populations.erase(data->populations.begin()+i);
}
// might be selected:
for (int i = 0; i < data->selList.size(); ++i) {
if (this->pop == data->selList[i]) {
data->selList.erase(data->selList.begin()+i);
selIndex = i;
if (data->selList.size()==0) {
data->cursor.x = this->pop->currentLocation().x();
data->cursor.y = this->pop->currentLocation().y();
}
}
}
// do children by calling parent class function:
QUndoCommand::undo();
}
void addPopulationCmd::redo()
{
// add to system
data->populations.push_back(pop);
pop->isDeleted = false;
isDeleted = false;
if (selIndex != -1) {
data->selList.push_back(pop);
data->cursor.x = -100000;
data->cursor.y = -100000;
}
// do children by calling parent class function:
QUndoCommand::redo();
}
// ######## DELETE POPULATION #################
delPopulation::delPopulation(nl_rootdata * data, QSharedPointer<population> pop, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->pop = pop;
this->data = data;
this->setText("delete population " + this->pop->getName());
// spawn children
for (int i = 0; i < this->pop->neuronType->inputs.size(); ++i) {
new delInput(data, this->pop->neuronType->inputs[i], this);
}
for (int i = 0; i < this->pop->neuronType->outputs.size(); ++i) {
// ok, find out if the input for this output is deleted
bool destination_deleted = false;
for (int j = 0; j < data->selList.size(); ++j) {
if (this->pop->neuronType->outputs[i]->destination == data->selList[j])
destination_deleted = true;
if (data->selList[j]->type == populationObject) {
QSharedPointer <population> pop2 = qSharedPointerDynamicCast <population> (data->selList[j]);
for (int k = 0; k < pop2->projections.size(); ++k)
if (this->pop->neuronType->outputs[i]->destination == pop2->projections[k])
destination_deleted = true;
for (int k = 0; k < pop2->reverseProjections.size(); ++k)
if (this->pop->neuronType->outputs[i]->destination == pop2->reverseProjections[k])
destination_deleted = true;
}
}
if (!destination_deleted)
new delInput(data, this->pop->neuronType->outputs[i], this);
}
for (int i = 0; i < this->pop->projections.size(); ++i) {
new delProjection(data, this->pop->projections[i], this);
}
// only delete reverse projections if the source is not in the selList (otherwise we delete twice, which is a problem)
for (int i = 0; i < this->pop->reverseProjections.size(); ++i) {
bool source_deleted = false;
for (int j = 0; j < data->selList.size(); ++j) {
if (this->pop->reverseProjections[i]->source == data->selList[j])
source_deleted = true;
}
if (!source_deleted)
new delProjection(data, this->pop->reverseProjections[i], this);
}
// go into experiments and delete all referencing objects
for (int i = 0; i < this->data->experiments.size(); ++i) {
// for each experiment
experiment * currExpt = this->data->experiments[i];
// loop through inputs
for (int j = 0; j < currExpt->ins.size(); ++j) {
// if input references deleted pop the push a delete
exptInput * in = currExpt->ins[j];
if (in->target == this->pop->neuronType) {
new deleteInputUndo(data, currExpt, in, this);
}
}
// loop through outputs
for (int j = 0; j < currExpt->outs.size(); ++j) {
// if input references deleted pop the push a delete
exptOutput * out = currExpt->outs[j];
if (out->source == this->pop->neuronType) {
new deleteOutputUndo(data, currExpt, out, this);
}
}
// loop through chnaged properties
for (int j = 0; j < currExpt->changes.size(); ++j) {
// if input references deleted pop the push a delete
exptChangeProp * prop = currExpt->changes[j];
if (prop->component == this->pop->neuronType) {
new deleteChangePropUndo(data, currExpt, prop, this);
}
}
}
index = -1;
selIndex = -1;
isDeleted = true;
}
void delPopulation::undo()
{
pop->isDeleted = false;
// MUST HAVE A LOCAL COPY OR INCOMING UNDOS CAN CHANGE STATE BEFORE OUTGOING DESTRUCTOR CALLED
isDeleted = false;
if (index != -1) {
data->populations.insert(data->populations.begin()+index, pop);
}
if (selIndex != -1) {
data->selList.push_back(pop);
data->cursor.x = -100000;
data->cursor.y = -100000;
}
// do children by calling parent class function:
QUndoCommand::undo();
}
void delPopulation::redo()
{
// do children by calling parent class function:
QUndoCommand::redo();
// remove from system
for (int i = 0; i < data->populations.size(); ++i) {
if (pop == data->populations[i]) {
data->populations.erase(data->populations.begin()+i);
index = i;
}
}
// must be selected:
for (int i = 0; i < data->selList.size(); ++i) {
if (this->pop.data() == data->selList[i].data()) {
data->selList.erase(data->selList.begin()+i);
selIndex = i;
if (data->selList.size()==0) {
data->cursor.x = this->pop->currentLocation().x();
data->cursor.y = this->pop->currentLocation().y();
}
}
}
pop->isDeleted = true;
isDeleted = true;
}
// ######## MOVE POPULATION #################
movePopulation::movePopulation(nl_rootdata * data, QSharedPointer<population> pop, const QPointF& oldPos, const QPointF& newPos, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->pop = pop;
this->data = data;
this->setText("move population");
this->oldPos = oldPos;
this->newPos = newPos;
}
void movePopulation::undo()
{
pop->move (this->oldPos.x(), this->oldPos.y());
data->redrawViews();
// undo children by calling parent class function:
QUndoCommand::undo();
}
void movePopulation::redo()
{
// This zeroes the relative location, as newPos has been extracted
// from the population's targx, targy, which are absolute and
// not mouse positions.
pop->setLocationOffset(0, 0);
pop->move (this->newPos.x(), this->newPos.y());
data->redrawViews();
QUndoCommand::redo();
}
// ######## MOVE PROJECTION HANDLE #################
moveProjectionHandle::moveProjectionHandle(nl_rootdata * data, QSharedPointer<projection> proj, const QPointF& oldPos, const QPointF& newPos, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->proj = proj;
this->data = data;
this->setText("move handle");
this->oldPos = oldPos;
this->newPos = newPos;
}
void moveProjectionHandle::undo()
{
proj->moveSelectedControlPoint (this->oldPos.x(), this->oldPos.y());
data->redrawViews();
QUndoCommand::undo();
}
void moveProjectionHandle::redo()
{
proj->moveSelectedControlPoint (this->newPos.x(), this->newPos.y());
data->redrawViews();
QUndoCommand::redo();
}
// ######## ADD PROJECTION #################
addProjection::addProjection(nl_rootdata * data, QSharedPointer<projection> proj, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->proj = proj;
this->data = data;
this->setText("add projection " + this->proj->getName());
// spawn children
new addSynapse(data, this->proj, this);
selIndex = -1;
isDeleted = true;
}
void addProjection::undo()
{
proj->disconnect();
proj->isDeleted = true;
isDeleted = true;
// might be selected:
for (int i = 0; i < data->selList.size(); ++i) {
if (this->proj.data() == data->selList[i].data()) {
data->selList.erase(data->selList.begin()+i);
selIndex = i;
if (data->selList.size()==0) {
data->cursor.x = this->proj->currentLocation().x();
data->cursor.y = this->proj->currentLocation().y();
}
}
}
// do children by calling parent class function:
QUndoCommand::undo();
}
void addProjection::redo()
{
proj->connect(proj);
proj->isDeleted = false;
isDeleted = false;
if (selIndex != -1) {
data->selList.push_back(proj);
data->cursor.x = -100000;
data->cursor.y = -100000;
}
// do children by calling parent class function:
QUndoCommand::redo();
}
// ######## DELETE PROJECTION #################
delProjection::delProjection(nl_rootdata * data, QSharedPointer<projection> proj, QUndoCommand *parent) :
QUndoCommand(parent)
{
isChild = false;
if ((!(parent == 0)) && (parent->text() != "Delete selection")) isChild = true;
this->proj = proj;
this->data = data;
this->setText("delete projection " + this->proj->getName());
// spawn children
for (int i = 0; i < this->proj->synapses.size(); ++i) {
new delSynapse(data, this->proj, this->proj->synapses[i], this);
}
// go into experiments and delete all referencing objects
for (int i = 0; i < this->data->experiments.size(); ++i) {
// for each experiment
experiment * currExpt = this->data->experiments[i];
// loop through lesions
for (int j = 0; j < currExpt->lesions.size(); ++j) {
// if input references deleted pop the push a delete
exptLesion * lesion = currExpt->lesions[j];
if (lesion->proj == this->proj) {
new deleteLesionUndo(data, currExpt, lesion, this);
}
}
}
selIndex = -1;
isDeleted = true;
}
void delProjection::undo()
{
proj->connect(proj);
proj->isDeleted = false;
isDeleted = false;
if (selIndex != -1) {
data->selList.push_back(proj);
data->cursor.x = -100000;
data->cursor.y = -100000;
}
// do children by calling parent class function:
QUndoCommand::undo();
}
void delProjection::redo()
{
// do children by calling parent class function:
QUndoCommand::redo();
proj->disconnect();
proj->isDeleted = true;
isDeleted = true;
// might be selected:
for (int i = 0; i < data->selList.size(); ++i) {
if (this->proj == data->selList[i]) {
data->selList.erase(data->selList.begin()+i);
selIndex = i;
if (data->selList.size()==0) {
data->cursor.x = this->proj->currentLocation().x();
data->cursor.y = this->proj->currentLocation().y();
}
}
}
}
// ######## ADD SYNAPSE #################
addSynapse::addSynapse(nl_rootdata * data, QSharedPointer <projection> proj, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->syn.clear();
this->proj = proj;
this->data = data;
this->setText("add synapse to " + proj->getName());
this->syn = QSharedPointer<synapse>(new synapse(proj, data, true));
this->syn->connectionType->setSynapseIndex (proj->synapses.size());
this->syn->connectionType->setParent (proj); // proj as a QSharedPointer<systemObject>
proj->synapses.push_back (this->syn);
// spawn children for projInputs
new addInput(data, proj->source->neuronType, this->syn->weightUpdateCmpt, this);
new addInput(data, this->syn->weightUpdateCmpt, this->syn->postSynapseCmpt, this);
new addInput(data, this->syn->postSynapseCmpt, proj->destination->neuronType, this);
this->isDeleted = false;
}
void addSynapse::undo()
{
// delete Synapse
isDeleted = true;
syn->isDeleted = true;
QUndoCommand::undo();
data->reDrawAll();
}
void addSynapse::redo()
{
// create new Synapse on projection
isDeleted = false;
syn->isDeleted = false;
QUndoCommand::redo();
data->reDrawAll();
}
// ######## DELETE SYNAPSE #################
delSynapse::delSynapse(nl_rootdata * data, QSharedPointer <projection> proj, QSharedPointer <synapse> syn, QUndoCommand *parent) :
QUndoCommand(parent)
{
isChild = false;
if (!(parent == 0)) isChild = true;
this->syn = syn;
this->data = data;
this->proj = proj;
this->setText("delete synapse from " + this->proj->getName());
isUndone = false;
// spawn children
for (int i = 0; i < this->syn->postSynapseCmpt->inputs.size(); ++i) {
new delInput(data, this->syn->postSynapseCmpt->inputs[i], this);
}
for (int i = 0; i < this->syn->postSynapseCmpt->outputs.size(); ++i) {
// ok, find out if the input for this output is deleted
bool destination_deleted = false;
for (int j = 0; j < data->selList.size(); ++j) {
if (this->syn->postSynapseCmpt->outputs[i]->destination == data->selList[j])
destination_deleted = true;
if (data->selList[j]->type == populationObject) {
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (data->selList[j]);
for (int k = 0; k < pop->projections.size(); ++k)
if (this->syn->postSynapseCmpt->outputs[i]->destination == pop->projections[k])
destination_deleted = true;
for (int k = 0; k < pop->reverseProjections.size(); ++k)
if (this->syn->postSynapseCmpt->outputs[i]->destination == pop->reverseProjections[k])
destination_deleted = true;
}
}
if (!destination_deleted)
new delInput(data, this->syn->postSynapseCmpt->outputs[i], this);
}
for (int i = 0; i < this->syn->weightUpdateCmpt->inputs.size(); ++i) {
new delInput(data, this->syn->weightUpdateCmpt->inputs[i], this);
}
for (int i = 0; i < this->syn->weightUpdateCmpt->outputs.size(); ++i) {
// ok, find out if the input for this output is deleted
bool destination_deleted = false;
for (int j = 0; j < data->selList.size(); ++j) {
if (this->syn->weightUpdateCmpt->outputs[i]->destination == data->selList[j])
destination_deleted = true;
if (data->selList[j]->type == populationObject) {
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (data->selList[j]);
for (int k = 0; k < pop->projections.size(); ++k)
if (this->syn->weightUpdateCmpt->outputs[i]->destination == pop->projections[k])
destination_deleted = true;
for (int k = 0; k < pop->reverseProjections.size(); ++k)
if (this->syn->weightUpdateCmpt->outputs[i]->destination == pop->reverseProjections[k])
destination_deleted = true;
}
}
if (!destination_deleted) {
new delInput(data, this->syn->weightUpdateCmpt->outputs[i], this);
}
}
// go into experiments and delete all referencing objects
for (int i = 0; i < this->data->experiments.size(); ++i) {
// for each experiment
experiment * currExpt = this->data->experiments[i];
// loop through inputs
for (int j = 0; j < currExpt->ins.size(); ++j) {
// if input references deleted pop the push a delete
exptInput * in = currExpt->ins[j];
if (in->target == this->syn->weightUpdateCmpt || in->target == this->syn->postSynapseCmpt) {
new deleteInputUndo(data, currExpt, in, this);
}
}
// loop through outputs
for (int j = 0; j < currExpt->outs.size(); ++j) {
// if input references deleted pop the push a delete
exptOutput * out = currExpt->outs[j];
if (out->source == this->syn->weightUpdateCmpt || out->source == this->syn->postSynapseCmpt) {
new deleteOutputUndo(data, currExpt, out, this);
}
}
// loop through changed properties
for (int j = 0; j < currExpt->changes.size(); ++j) {
// if input references deleted pop the push a delete
exptChangeProp * prop = currExpt->changes[j];
if (prop->component == this->syn->weightUpdateCmpt || prop->component == this->syn->postSynapseCmpt) {
new deleteChangePropUndo(data, currExpt, prop, this);
}
}
}
projPos = -1;
}
void delSynapse::undo()
{
// add to on projection
if (projPos != -1)
proj->synapses.insert(proj->synapses.begin()+projPos, syn);
isUndone = true;
// do children by calling parent class function:
QUndoCommand::undo();
data->reDrawAll();
}
void delSynapse::redo()
{
// do children by calling parent class function:
QUndoCommand::redo();
// remove from projection
for (int i = 0; i < proj->synapses.size(); ++i) {
if (proj->synapses[i] == syn) {
proj->synapses.erase(proj->synapses.begin()+i);
projPos = i;
}
}
isUndone = false;
data->reDrawAll();
}
// ######## ADD GENERIC INPUT #################
addInput::addInput(nl_rootdata * d, QSharedPointer <ComponentInstance> source, QSharedPointer <ComponentInstance> dest, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->data = d;
this->src = source;
this->dst = dest;
this->setText("add Input from " + this->src->getXMLName() + " to " + this->dst->getXMLName());
this->input = QSharedPointer<genericInput> (new genericInput(this->src, this->dst, !(parent==0)));
this->input->conn->setParent (this->input);
this->input->connect (this->input);
this->input->disconnect();
}
void addInput::undo()
{
// delete input (must disconnect it first!)
this->input->disconnect();
this->isDeleted = true;
}
void addInput::redo()
{
// create new Synapse on projection
this->input->connect(input);
this->isDeleted = false;
}
// ######## DELETE GENERIC INPUT #################
delInput::delInput(nl_rootdata * d, QSharedPointer<genericInput> i, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->isChild = false;
if (!(parent == 0) && (parent->text() != "Delete selection")) {
this->isChild = true;
}
this->input = i;
this->data = d;
this->setText("delete Input from " + this->input->srcCmpt->getXMLName() + " to " + this->input->dstCmpt->getXMLName());
this->input->isDeleted = true;
this->isDeleted = true;
this->selIndex = -1;
// sanity
if (this->input->source == NULL || this->input->destination == NULL) {
qDebug() << "ERROR - input without source or destination set";
}
}
void delInput::undo()
{
// disconnect ties for input
input->connect(input);
input->isDeleted = false;
isDeleted = false;
if (selIndex != -1) {
data->selList.push_back(input);
data->cursor.x = -100000;
data->cursor.y = -100000;
}
}
void delInput::redo()
{
// reconnect ties for input
input->disconnect();
// might be selected:
for (int i = 0; i < data->selList.size(); ++i) {
if (this->input == data->selList[i]) {
data->selList.erase(data->selList.begin()+i);
selIndex = i;
if (data->selList.size()==0) {
data->cursor.x = this->input->currentLocation().x();
data->cursor.y = this->input->currentLocation().y();
}
}
}
input->isDeleted = true;
isDeleted = true;
}
// ######## CHANGE CONNECTION #################
changeConnection:: changeConnection(nl_rootdata * data, QSharedPointer<systemObject> pNewConn, int index, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->index = index;
this->newConn = pNewConn;
this->data = data;
this->setText("change connection type on " + this->newConn->getName());
// if we use an index only and the scripts change before an undo / redo we are in trouble, so if we have
// a script we must get the script name right now (NOTE: this could still be an issue if we rename scripts)
if (index >= Python) {
QSettings settings;
// get python scripts
settings.beginGroup("pythonscripts");
QStringList scripts = settings.childKeys();
// get the script associated with that index
int scriptIndex = index - (int) Python;
this->scriptName = scripts.at(scriptIndex);
index = none;
settings.endGroup();
settings.beginGroup ("connParams");
QStringList params = settings.childKeys();
if (!params.isEmpty()) {
foreach (QString param, params) {
// So write into a map in the updateConnection class.
this->mparams[param] = settings.value(param, "").toString();
}
}
settings.endGroup();
}
}
void changeConnection::undo()
{
if (newConn->type == inputObject) {
// Switching back from newConn to oldConn.
delete (qSharedPointerDynamicCast <genericInput> (newConn))->conn;
(qSharedPointerDynamicCast <genericInput> (newConn))->conn = oldConn;
}
if (newConn->type == synapseObject) {
delete (qSharedPointerDynamicCast <synapse> (newConn))->connectionType;
(qSharedPointerDynamicCast <synapse> (newConn))->connectionType = oldConn;
}
isUndone = true;
data->reDrawAll();
}
void changeConnection::redo()
{
// newConn is a QSharedPointer<systemObject>; it's the newConn
if (newConn->type == inputObject) {
QSharedPointer<genericInput> newConnIn = qSharedPointerDynamicCast <genericInput> (newConn);
// This ensures that oldConn points to the previous connection instance.
oldConn = newConnIn->conn; // connection* oldConn
// Now, for changed connections one creates a new connection
// object. This class was designed initially to handle just
// the case where the connection type was changed using the
// "Connectivity:" menu. However, there is an additional case
// where this code is used - where an explicit connection is
// swapped from a global delay to per-connection delays. In
// this case rather than creating a new csv_connection, we
// want to copy the existing csv_connection with all its
// features into oldConn.
switch(index) {
case AlltoAll:
newConnIn->conn = new alltoAll_connection;
newConnIn->conn->setSynapseIndex (oldConn->getSynapseIndex());
newConnIn->conn->setParent (oldConn->parent);
break;
case OnetoOne:
newConnIn->conn = new onetoOne_connection;
newConnIn->conn->setParent(newConnIn);
newConnIn->conn->setSynapseIndex (oldConn->getSynapseIndex());
newConnIn->conn->setParent (oldConn->parent);
break;
case FixedProb:
newConnIn->conn = new fixedProb_connection;
newConnIn->conn->setSynapseIndex (oldConn->getSynapseIndex());
newConnIn->conn->setParent (oldConn->parent);
break;
case CSV:
{
if (oldConn->type == CSV) {
// Then this is a csv_connection to csv_connection
// change, so transfer information from old to new.
csv_connection* oldcsv = static_cast<csv_connection*>(oldConn);
newConnIn->conn = oldcsv->newFromExisting(); // allocates csv_connection and copies most data.
csv_connection* newcsv = static_cast<csv_connection*>(newConnIn->conn);
// For some reason newFromExisting doesn't copy data values. dunno why.
newcsv->copyDataValues(oldcsv);
} else {
// Switching from another connection type (e.g. alltoall)
newConnIn->conn = new csv_connection;
}
newConnIn->conn->setSynapseIndex (oldConn->getSynapseIndex());
newConnIn->conn->setParent (oldConn->parent);
break;
}
case Python:
break;
case CSA:
break;
case none:
break;
}
// if we are using a script...
if (!scriptName.isEmpty()) {
QSettings settings;
// get python scripts
settings.beginGroup("pythonscripts");
QStringList scripts = settings.childKeys();
// get the script associated with that index
QString script = settings.value(scriptName, "").toString();
newConnIn->conn = new csv_connection;
newConnIn->conn->setSynapseIndex (oldConn->getSynapseIndex());
newConnIn->conn->setParent (oldConn->parent);
((csv_connection *)newConnIn->conn)->generator = new pythonscript_connection(qSharedPointerDynamicCast <population> (newConnIn->source), qSharedPointerDynamicCast <population> (newConnIn->destination), (csv_connection *) newConnIn->conn);
// setup the generator:
((pythonscript_connection *) ((csv_connection *) newConnIn->conn)->generator)->scriptText = script;
((pythonscript_connection *) ((csv_connection *) newConnIn->conn)->generator)->scriptName = scriptName;
((pythonscript_connection *) ((csv_connection *) newConnIn->conn)->generator)->configureFromScript(script);
settings.endGroup();
}
} else if (newConn->type == synapseObject) {
QSharedPointer<synapse> newConnSyn = qSharedPointerDynamicCast <synapse> (newConn);
oldConn = newConnSyn->connectionType;
switch(index) {
case AlltoAll:
newConnSyn->connectionType = new alltoAll_connection;
newConnSyn->connectionType->setSynapseIndex (oldConn->getSynapseIndex());
newConnSyn->connectionType->setParent (oldConn->parent);
break;
case OnetoOne:
newConnSyn->connectionType = new onetoOne_connection;
newConnSyn->connectionType->setParent(newConnSyn);
newConnSyn->connectionType->setSynapseIndex (oldConn->getSynapseIndex());
newConnSyn->connectionType->setParent (oldConn->parent);
break;
case FixedProb:
newConnSyn->connectionType = new fixedProb_connection;
newConnSyn->connectionType->setSynapseIndex (oldConn->getSynapseIndex());
newConnSyn->connectionType->setParent (oldConn->parent);
break;
case CSV:
if (oldConn->type == CSV) {
// Then this is a csv_connection to csv_connection
// change, so transfer information from old to new.
csv_connection* oldcsv = static_cast<csv_connection*>(oldConn);
newConnSyn->connectionType = oldcsv->newFromExisting(); // allocates csv_connection and copies most data.
csv_connection* newcsv = static_cast<csv_connection*>(newConnSyn->connectionType);
// For some reason newFromExisting doesn't copy data values. dunno why.
newcsv->copyDataValues(oldcsv);
} else {
// Switching from another connection type (e.g. alltoall)
newConnSyn->connectionType = new csv_connection;
}
newConnSyn->connectionType->setSynapseIndex (oldConn->getSynapseIndex());
newConnSyn->connectionType->setParent (oldConn->parent);
break;
case Python:
newConnSyn->connectionType = new csv_connection;
newConnSyn->connectionType->setParent (oldConn->parent);
((csv_connection *)newConnSyn->connectionType)->generator = new pythonscript_connection(qSharedPointerDynamicCast<population> (newConnSyn->proj->source), qSharedPointerDynamicCast<population> (newConnSyn->proj->destination), (csv_connection *)newConnSyn->connectionType);
break;
case CSA:
break;
case none:
break;
}
// if we are using a script...
if (!scriptName.isEmpty()) {
QSettings settings;
// get python scripts
settings.beginGroup("pythonscripts");
QStringList scripts = settings.childKeys();
// get the script associated with that index
QString script = settings.value(scriptName, "").toString();
newConnSyn->connectionType = new csv_connection;
newConnSyn->connectionType->setSynapseIndex (oldConn->getSynapseIndex());
newConnSyn->connectionType->setParent (oldConn->parent);
((csv_connection *)newConnSyn->connectionType)->generator = new pythonscript_connection(qSharedPointerDynamicCast<population> (newConnSyn->proj->source), qSharedPointerDynamicCast<population> (newConnSyn->proj->destination), (csv_connection *)newConnSyn->connectionType);
// setup the generator:
pythonscript_connection* pygen = (pythonscript_connection *) ((csv_connection *) newConnSyn->connectionType)->generator;
pygen->scriptText = script;
pygen->scriptName = scriptName;
// Need the version of configureFromScript which takes mparams as an arg here:
pygen->configureFromScript(script, this->mparams);
settings.endGroup(); // pythonscripts
}
} else {
DBG() << "Unexpected object pointed to by newConn.";
}
isUndone = false;
data->reDrawAll();
}
// ######## Update a CSV connections delay to be either global or per-connection #################
globalConnectionDelayChange:: globalConnectionDelayChange(nl_rootdata * d, QSharedPointer<systemObject> pExistingConn, bool gDelay, QUndoCommand *parent) :
QUndoCommand(parent)
{
this->connParent = pExistingConn;
this->data = d;
this->globalDelay = gDelay;
this->setText("change csv_connection global delay on " + this->connParent->getName());
}
void globalConnectionDelayChange::undo()
{
if (this->connParent->type == inputObject) {
// Switching back from connParent to oldConn.
delete (qSharedPointerDynamicCast <genericInput> (this->connParent))->conn;
(qSharedPointerDynamicCast <genericInput> (this->connParent))->conn = oldConn;
}
if (this->connParent->type == synapseObject) {
delete (qSharedPointerDynamicCast <synapse> (this->connParent))->connectionType;
(qSharedPointerDynamicCast <synapse> (this->connParent))->connectionType = oldConn;
}
this->isUndone = true;
this->data->reDrawAll();
}
void globalConnectionDelayChange::redo()
{
// connParent is a QSharedPointer<systemObject>; it's the connParent
if (this->connParent->type == inputObject) {
QSharedPointer<genericInput> connParentIn = qSharedPointerDynamicCast <genericInput> (this->connParent);
// This ensures that oldConn points to the previous connection instance.
this->oldConn = connParentIn->conn; // connection* oldConn
// Now, for changed connections one creates a new connection
// object. This class was designed initially to handle just
// the case where the connection type was changed using the
// "Connectivity:" menu. However, there is an additional case
// where this code is used - where an explicit connection is
// swapped from a global delay to per-connection delays. In
// this case rather than creating a new csv_connection, we
// want to copy the existing csv_connection with all its
// features into oldConn.
if (oldConn->type != CSV) {
DBG() << "Error, both connections expected to be csv_connections";
}
// This is a csv_connection to csv_connection
// change, so transfer information from old to new.
csv_connection* oldcsv = static_cast<csv_connection*>(this->oldConn);
connParentIn->conn = oldcsv->newFromExisting(); // allocates csv_connection and copies most data.
csv_connection* newcsv = static_cast<csv_connection*>(connParentIn->conn);
newcsv->copyDataValues(oldcsv); // This is not handled in newFromExisting.
// Now make the actual change to the new connection
if (this->globalDelay) {
newcsv->updateDataForNumCols(2);
} else {
newcsv->updateDataForNumCols(3);
}
connParentIn->conn->setSynapseIndex (this->oldConn->getSynapseIndex());
connParentIn->conn->setParent (this->oldConn->parent);
} else if (this->connParent->type == synapseObject) {
QSharedPointer<synapse> connParentSyn = qSharedPointerDynamicCast <synapse> (this->connParent);
this->oldConn = connParentSyn->connectionType;
// Then this is a csv_connection to csv_connection
// change, so transfer information from old to new.
csv_connection* oldcsv = static_cast<csv_connection*>(this->oldConn);
connParentSyn->connectionType = oldcsv->newFromExisting(); // allocates csv_connection and copies most data.
csv_connection* newcsv = static_cast<csv_connection*>(connParentSyn->connectionType);
newcsv->copyDataValues(oldcsv);
if (this->globalDelay) {
newcsv->updateDataForNumCols(2);
} else {
newcsv->updateDataForNumCols(3);
}
connParentSyn->connectionType->setSynapseIndex (this->oldConn->getSynapseIndex());
connParentSyn->connectionType->setParent (this->oldConn->parent);
} else {
DBG() << "Unexpected object pointed to by connParent.";
}