-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSC_network_layer_rootdata.cpp
2400 lines (2046 loc) · 89.8 KB
/
SC_network_layer_rootdata.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_network_layer_rootdata.h"
#include "SC_connectionlistdialog.h"
#include "EL_experiment.h"
#include "SC_undocommands.h"
#include "SC_export_network_image.h"
#include "mainwindow.h"
#include "SC_projectobject.h"
#include "SC_systemmodel.h"
#include "SC_component_rootcomponentitem.h"
/*
Alex Cope 2012
The rootData class stores the 9ML description of the system -
what populations there are, what projections, and the parameters of all these.
It is a central target for all changes to the system, and any changes made will
propagate through here.
*/
#ifdef SHOW_MOUSE_DEBUGGING
# define DBGMOUSE() qDebug() << __FUNCTION__ << ": "
#endif
nl_rootdata::nl_rootdata(QObject *parent) :
QObject(parent)
{
QSettings settings;
//QString localName = settings.value("versioning/localName", QHostInfo::localHostName()).toString();
//version.setVersion(0,0,1,localName);
this->cursor.x = 0;
this->cursor.y = 0;
this->largestIndex = 0;
this->catalogUnsorted.push_back(QSharedPointer<Component>(new Component()));
this->catalogUnsorted[0]->name = "none";
this->catalogUnsorted[0]->type = "moo";
this->catalogNrn.push_back(QSharedPointer<Component>(new Component()));
this->catalogNrn[0]->name = "none";
this->catalogNrn[0]->type = "neuron_body";
this->catalogWU.push_back(QSharedPointer<Component>(new Component()));
this->catalogWU[0]->name = "none";
this->catalogWU[0]->type = "weight_update";
this->catalogPS.push_back(QSharedPointer<Component>(new Component()));
this->catalogPS[0]->name = "none";
this->catalogPS[0]->type = "postsynapse";
this->catalogLayout.push_back(QSharedPointer<NineMLLayout>(new NineMLLayout()));
this->catalogLayout[0]->name = "none";
this->selectionMoved = false;
this->selChange = false;
if (!popImage.load( ":/icons/objects/icons/nrn.png" )) std::cerr << "warn" << endl;
// update version and name
setCaption("");
clipboardCData.clear();
projectActions = NULL;
this->experimentActions = NULL;
catalogConn.push_back("1");
catalogConn.push_back("2");
}
void nl_rootdata::redrawViews()
{
// redraw the network
reDrawAll();
redrawGLview();
// redraw the component file list
main->viewCL.fileList->disconnect();
main->addComponentsToFileList();
connect(main->viewCL.fileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), main, SLOT(fileListItemChanged(QListWidgetItem*,QListWidgetItem*)));
// clear edited component
if (main->viewCL.root != NULL) {
delete main->viewCL.root;
main->viewCL.root = NULL;
}
// redraw experiments
main->viewELhandler->redraw();
if ( main->viewVZ.OpenGLWidget != NULL) {
// redraw viz
main->viewVZ.OpenGLWidget->refreshAll();
// clear away old stuff
main->viewVZ.currObject = (QSharedPointer<systemObject>)0;
main->viewVZhandler->clearAll();
main->viewVZ.OpenGLWidget->clear();
// configure TreeView
if (!(main->viewVZ.sysModel == NULL)) {
delete main->viewVZ.sysModel;
}
main->viewVZ.sysModel = new systemmodel(this);
main->viewVZ.treeView->setModel(main->viewVZ.sysModel);
// connect for function
connect(main->viewVZ.treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), main->viewVZhandler, SLOT(selectionChanged(QItemSelection,QItemSelection)));
connect(main->viewVZ.treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), main->viewVZ.OpenGLWidget, SLOT(selectionChanged(QItemSelection,QItemSelection)));
connect(main->viewVZ.sysModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), main->viewVZ.OpenGLWidget, SLOT(sysSelectionChanged(QModelIndex,QModelIndex)));
main->viewVZhandler->redrawHeaders();
main->viewVZ.OpenGLWidget->sysSelectionChanged(QModelIndex(), QModelIndex());
}
}
void nl_rootdata::selectProject(QAction * action)
{
// deselect the current project
this->currProject->deselect_project(this);
// select the new project
this->projects[action->property("number").toInt()]->select_project(this);
this->main->setExperimentMenu();
this->redrawViews();
this->main->updateTitle();
// Update the viewGV for the new project/experiment, initialising
// if necessary with MainWindow::viewGVreshow:
this->main->viewGVreshow();
}
void nl_rootdata::selectExperiment(QAction* action)
{
DBG() << "nl_rootdata::selectExperiment called.";
// Update the selected experiment in data.
this->main->selectExperiment (action->property("number").toInt());
this->main->viewELhandler->redraw();
this->main->viewGVreshow();
}
bool nl_rootdata::doesExperimentExist (experiment* e)
{
// Search QVector < experiment *> experiments;
QVector<experiment*>::const_iterator ex = this->experiments.constBegin();
while (ex != this->experiments.constEnd()) {
if ((*ex) == e) {
//DBG() << e << " exists in nl_rootdata::experiments.";
return true;
}
++ex;
}
// Search each QVector < projectObject * > projects;
QVector<projectObject*>::const_iterator po = this->projects.constBegin();
while (po != this->projects.constEnd()) {
if ((*po)->doesExperimentExist (e) == true) {
//DBG() << e << " exists in nl_rootdata::projects.";
return true;
}
++po;
}
return false;
}
void nl_rootdata::replaceComponent(QSharedPointer<Component> oldComp, QSharedPointer<Component> newComp)
{
for (int p = 0; p < this->populations.size(); ++p) {
QSharedPointer <population> pop = this->populations[p];
// replace references
if (pop->neuronType->component == oldComp) {
// has the type changed?
if (newComp->type != "neuron_body") {
pop->neuronType->migrateComponent(catalogNrn[0]);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(pop->neuronType);
}
} else {
pop->neuronType->migrateComponent(newComp);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(pop->neuronType);
}
}
}
for (int pr = 0; pr < pop->projections.size(); ++pr) {
QSharedPointer <projection> proj = pop->projections[pr];
for (int sy = 0; sy < proj->synapses.size(); ++sy) {
QSharedPointer <synapse> syn = proj->synapses[sy];
// replace references
if (syn->weightUpdateCmpt->component == oldComp) {
// has the type changed?
if (newComp->type != "weight_update") {
syn->weightUpdateCmpt->migrateComponent(catalogWU[0]);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(syn->weightUpdateCmpt);
}
} else {
syn->weightUpdateCmpt->migrateComponent(newComp);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(syn->weightUpdateCmpt);
}
}
}
// replace references
if (syn->postSynapseCmpt->component == oldComp) {
// has the type changed?
if (newComp->type != "postsynapse") {
syn->postSynapseCmpt->migrateComponent(catalogPS[0]);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(syn->postSynapseCmpt);
}
} else {
syn->postSynapseCmpt->migrateComponent(newComp);
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->updateChanges(syn->postSynapseCmpt);
}
}
}
}
}
// also fix experiments with bad pointers to PORTS and PARS & COMPONENTS
for (int i = 0; i < experiments.size(); ++i) {
experiment * currExpt = experiments[i];
currExpt->purgeBadPointer(oldComp, newComp);
}
}
// clear undo
this->currProject->undoStack->clear();
}
// centralised function for finding if a component is in the model
bool nl_rootdata::isComponentInUse(QSharedPointer<Component> oldComp)
{
for (int p = 0; p < populations.size(); ++p) {
QSharedPointer <population> pop = populations[p];
// replace references
if (pop->neuronType->component == oldComp) {
return true;
}
for (int pr = 0; pr < pop->projections.size(); ++pr) {
QSharedPointer <projection> proj = pop->projections[pr];
for (int sy = 0; sy < proj->synapses.size(); ++sy) {
QSharedPointer <synapse> syn = proj->synapses[sy];
// replace references
if (syn->weightUpdateCmpt->component == oldComp) {
return true;
}
// replace references
if (syn->postSynapseCmpt->component == oldComp) {
return true;
}
}
}
// also fix experiments with bad pointers:
}
// component not found
return false;
}
// centralised function for finding if a component is in the model
bool nl_rootdata::removeComponent(QSharedPointer<Component> oldComp)
{
if (isComponentInUse(oldComp)) {
return false;
}
if (oldComp == NULL) {
return true;
}
QVector<QSharedPointer<Component> >* curr_lib = (QVector<QSharedPointer<Component> >*)0;
if (oldComp->type == "neuron_body") {
curr_lib = &this->catalogNrn;
}
if (oldComp->type == "weight_update") {
curr_lib = &this->catalogWU;
}
if (oldComp->type == "postsynapse") {
curr_lib = &this->catalogPS;
}
for (int i = 0; i < curr_lib->size(); ++i) {
if ((*curr_lib)[i] == oldComp) {
curr_lib->erase(curr_lib->begin()+i);
oldComp.clear();
return true;
}
}
// component not found
return false;
}
void nl_rootdata::updateStatusBar(QString in_string, int time)
{
emit statusBarUpdate(in_string, time);
}
void nl_rootdata::reDrawPanel()
{
emit updatePanel(this);
}
void nl_rootdata::callRedrawGLview()
{
emit redrawGLview();
}
void nl_rootdata::saveImage(QString fileName)
{
//if (!fileName.isEmpty()) {
saveNetworkImageDialog svImDiag(this, fileName);
svImDiag.exec();
//}
}
void nl_rootdata::reDrawAll()
{
// update panel - we don't always want to do this as it loses focus from widgets
emit updatePanel(this);
}
void nl_rootdata::reDrawAll(QPainter *painter, float GLscale, float viewX, float viewY, int width, int height, drawStyle style)
{
if (style == standardDrawStyle) {
for (int i = 0; i < selList.size(); ++i) {
if (selList[i]->type == populationObject) {
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (selList[i]);
float left = ((pop->getLeft()+viewX)*GLscale+float(width))/2;
float right = ((pop->getRight()+viewX)*GLscale+float(width))/2;
float top = ((-pop->getTop()+viewY)*GLscale+float(height))/2;
float bottom = ((-pop->getBottom()+viewY)*GLscale+float(height))/2;
if (pop->isSpikeSource) {
// Shadow the spike source.
for (int i = 5; i > 1; --i) {
QPen pen(QColor(0,0,0,50/i));
pen.setWidthF(float(i*2));
painter->setPen(pen);
painter->drawEllipse(QPointF((right+left)/2.0, (top+bottom)/2.0),0.5*GLscale/2.0,0.5*GLscale/2.0);
}
} else {
// This draws a shadow around the rectangle by
// drawing concentric rectangles of slightly
// differing size and alpha.
for (int i = 5; i > 1; --i) {
QPen pen(QColor(0,0,0,50/i));
pen.setWidthF(float(i*2));
painter->setPen(pen);
QRectF rectangle(left, top, right-left, bottom-top);
painter->drawRoundedRect(rectangle,0.05*GLscale,0.05*GLscale);
}
}
}
}
}
// populations
for (int i = 0; i < this->populations.size(); ++i) {
this->populations[i]->draw(painter, GLscale, viewX, viewY, width, height, this->popImage, style);
}
for (int i = 0; i < this->populations.size(); ++i) {
this->populations[i]->drawSynapses(painter, GLscale, viewX, viewY, width, height, style);
}
for (int i = 0; i < this->populations.size(); ++i) {
QPen pen(QColor(100,0,0,100));
QSettings settings;
float dpi = settings.value("dpi", "1").toFloat();
pen.setWidthF(float(1)/dpi);
painter->setPen(pen);
this->populations[i]->drawInputs(painter, GLscale, viewX, viewY, width, height, style);
}
// selected object
if (style == standardDrawStyle) {
for (int i = 0; i < selList.size(); ++i) {
if (selList[i]->type == projectionObject) {
QSharedPointer <projection> col = qSharedPointerDynamicCast <projection> (selList[i]);
for (int i = 5; i > 1; --i) {
QPen pen(QColor(0,0,0,30/i));
pen.setWidthF(float(i*2));
painter->setPen(pen);
col->draw(painter, GLscale, viewX, viewY, width, height, this->popImage, standardDrawStyle);
// only draw handles if we aren't using multiple selection
if (selList.size() == 1) {
col->drawHandles(painter, GLscale, viewX, viewY, width, height);
}
}
}
if (selList[i]->type == inputObject) {
QSharedPointer<genericInput> input = qSharedPointerDynamicCast<genericInput> (selList[i]);
for (int i = 5; i > 1; --i) {
QPen shadowPen = painter->pen();
shadowPen.setColor(QColor(0,0,0,30/i));
shadowPen.setWidthF(float(i*2));
painter->setPen(shadowPen);
// Calling this results in the shadowing being
// drawn in the colour of the generic input, NOT
// in the colour set in shadowPen, above, so I've
// commented out for now. I can't work out where
// the additional re-draws are occuring, although
// I HAVE noticed that the system is doing many
// redraws on selection of a generic input (and
// ifdeffing this code out doesn't prevent all
// those re-draws)
#if 0
input->draw(painter, GLscale, viewX, viewY, width, height, this->popImage, standardDrawStyle);
#endif
// only draw handles if we aren't using multiple selection
if (selList.size() == 1) {
input->drawHandles(painter, GLscale, viewX, viewY, width, height);
}
}
}
}
}
painter->setPen(QCOL_BLACK);
float x = ((this->cursor.x+viewX)*GLscale+float(width))/2;
float y = ((-this->cursor.y+viewY)*GLscale+float(height))/2;
QPointF point(x,y);
painter->drawEllipse(point,10,10);
painter->drawLine(QLineF(x, y-14.0f, x, y+14.0f));
painter->drawLine(QLineF(x-14.0f, y, x+14.0f, y));
// update positions
for (int i = 0; i < this->populations.size(); ++i) {
this->populations[i]->animate(this->populations[i]);
}
// draw dragselect if present
if (fabs(this->dragSelection.width()) > 0.001) {
// convert to screen co-ords
QRectF dragSelectionScreen;
// top left conversion
QPointF point = dragSelection.topLeft();
point.setX(((point.x()+viewX)*GLscale+width)/2);
point.setY(((-point.y()+viewY)*GLscale+height)/2);
dragSelectionScreen.setTopLeft(point);
// bottom right conversion
point = dragSelection.bottomRight();
point.setX(((point.x()+viewX)*GLscale+width)/2);
point.setY(((-point.y()+viewY)*GLscale+height)/2);
dragSelectionScreen.setBottomRight(point);
// make sure the rect is not inverted (i.e. non-negative with & height) so draws correctly
dragSelectionScreen = dragSelectionScreen.normalized();
// set draw style
painter->save();
painter->setPen(QColor(0,255,0,200));
painter->setBrush(QColor(0,255,0,50));
// draw
painter->drawRect(dragSelectionScreen);
painter->restore();
}
}
void destroyDom(QDomNode &node)
{
QDomNodeList childList = node.childNodes();
QDomNode child;
for (int i = 0; i < childList.count(); ++i) {
child = childList.item(i);
destroyDom(child);
child.clear();
}
}
void nl_rootdata::onRightMouseDown(float xGL, float yGL, float GLscale)
{
qDebug() << "onRightMouseDown";
// insert new point into projection
if (selList.size() == 1) {
if (this->selList[0]->type == projectionObject || this->selList[0]->type == inputObject) {
// try to delete control point, if that fails then add one!
QSharedPointer <projection> proj = qSharedPointerDynamicCast <projection> (selList[0]);
if (!proj->deleteControlPoint(xGL, yGL, GLscale)) {
proj->insertControlPoint(xGL, yGL, GLscale);
}
}
}
// log the co-ordinates for drag select
this->dragListStart = QPointF(xGL, yGL);
}
void nl_rootdata::dragSelect(float xGL, float yGL)
{
bool addSelection = (QApplication::keyboardModifiers() & Qt::ShiftModifier);
// setup the QRect for the selection
this->dragSelection = QRectF(this->dragListStart, QPointF(xGL, yGL));
QVector <QSharedPointer<systemObject> > oldSel = selList;
// clear slection list if shift not held
if (!addSelection) {
selList.clear();
}
// add selected objects to list
for (int i = 0; i < this->populations.size(); ++i) {
QSharedPointer <population> pop = populations[i];
if (dragSelection.contains(pop->x, pop->y)) {
// if not already selected
bool alreadySelected = false;
for (int s = 0; s < this->selList.size(); ++s) {
if (selList[s] == pop) {
alreadySelected = true;
}
}
if (!alreadySelected) {
selList.push_back(pop);
}
}
for (int j = 0; j < pop->neuronType->inputs.size(); ++j) {
QSharedPointer<genericInput> in = pop->neuronType->inputs[j];
if (in->curves.size() > 0) {
if (dragSelection.contains(in->start) && dragSelection.contains(in->curves.back().end)) {
// if not already selected
bool alreadySelected = false;
for (int s = 0; s < this->selList.size(); ++s) {
if (selList[s] == in) {
alreadySelected = true;
}
}
if (!alreadySelected) {
selList.push_back(in);
}
}
}
}
for (int j = 0; j < pop->projections.size(); ++j) {
QSharedPointer <projection> proj = pop->projections[j];
if (proj->curves.size() > 0) {
if (dragSelection.contains(proj->start) && dragSelection.contains(proj->curves.back().end)) {
// if not already selected
bool alreadySelected = false;
for (int s = 0; s < this->selList.size(); ++s) {
if (selList[s] == proj) {
alreadySelected = true;
}
}
if (!alreadySelected) {
selList.push_back(proj);
}
}
}
for (int pt = 0; pt < proj->synapses.size(); ++pt) {
QSharedPointer <synapse> projT = proj->synapses[pt];
// select generic inputs for weightupdate
for (int c = 0; c < projT->weightUpdateCmpt->inputs.size(); ++c) {
QSharedPointer<genericInput> in = projT->weightUpdateCmpt->inputs[c];
if (in->curves.size() > 0) {
if (dragSelection.contains(in->start) && dragSelection.contains(in->curves.back().end)) {
// if not already selected
bool alreadySelected = false;
for (int s = 0; s < this->selList.size(); ++s) {
if (selList[s] == in) {
alreadySelected = true;
}
}
if (!alreadySelected) {
selList.push_back(in);
}
}
}
}
// select generic inputs for postsynapse
for (int c = 0; c < projT->postSynapseCmpt->inputs.size(); ++c) {
QSharedPointer<genericInput> in = projT->postSynapseCmpt->inputs[c];
if (in->curves.size() > 0) {
if (dragSelection.contains(in->start) && dragSelection.contains(in->curves.back().end)) {
// if not already selected
bool alreadySelected = false;
for (int s = 0; s < this->selList.size(); ++s) {
if (selList[s] == in) {
alreadySelected = true;
}
}
if (!alreadySelected) {
selList.push_back(in);
}
}
}
}
}
}
}
// check if the selection has changed to prevent unecessary redrawing
bool selectionChanged = false;
if (oldSel.size() == selList.size()) {
for (int i = 0; i < oldSel.size(); ++i) {
if (oldSel[i] != selList[i]) {
selectionChanged = true;
}
}
} else {
selectionChanged = true;
}
if (selectionChanged) {
reDrawAll();
}
}
void nl_rootdata::endDragSelection()
{
this->dragSelection = QRect(-1,-1,0,0);
}
#ifdef NEED_MOUSE_UP_LOGIC
void rootData::onLeftMouseUp (float xGL, float yGL, float GLscale)
{
// All selection logic is in onLeftMouseDown(), so currently, this
// method is a no-op.
}
#endif
void nl_rootdata::itemWasMoved()
{
if (!this->selList.empty()) {
// We have a pointer(s) to the moved item(s). Check types to
// see what to do with it/them. If ANY object in selList is a
// population, then call populationMoved.
QVector <QSharedPointer<population> > pops = this->currSelPopulations();
if (!pops.empty()) {
this->populationMoved (pops);
} // else do nothing
if (this->selList.size() == 1
&& (this->selList[0]->type == projectionObject
|| this->selList[0]->type == inputObject)) {
// The selected thing was moved.
this->projectionHandleMoved();
}
}
}
void nl_rootdata::projectionHandleMoved()
{
// Already checked before call, but lets be safe
if (this->selList.size() == 1
&& (this->selList[0]->type == projectionObject
|| this->selList[0]->type == inputObject)) {
// New position of the handle
QSharedPointer<projection> projptr = qSharedPointerDynamicCast<projection>(this->selList[0]);
QPointF newPos = projptr->selectedControlPointLocation();
this->currProject->undoStack->push(new moveProjectionHandle(this, projptr, lastLeftMouseDownPos, newPos));
}
}
void nl_rootdata::populationMoved(const QVector <QSharedPointer<population> >& pops)
{
if (pops.empty()) {
return;
}
// We don't need to record that the population moved here - the
// mouseMove events already handle that (allowing the population
// widget to be re-drawn as it's moved. However, what we DO need
// to do is to record that the population moved in the undo stack.
QVector <QSharedPointer<population> >::const_iterator popsi = pops.begin();
// A parent undocommand which will group together potentially multiple undos.
QUndoCommand* parentCmd = new QUndoCommand();
while (popsi != pops.end()) {
// New position of the population, which it already has a record of.
QPointF newPos((*popsi)->targx, (*popsi)->targy);
// The last position of the population is the
// lastSelectionPosition (which is the *mouse* position) offset by
// the current offset in the population (as the population moves,
// the mouse remains in the same location on the object).
QPointF lastPopulationPosition = lastLeftMouseDownPos + (*popsi)->getLocationOffset();
new movePopulation(this, (*popsi), lastPopulationPosition, newPos, parentCmd);
++popsi;
}
if (pops.size() > 1) {
parentCmd->setText ("move populations");
} else {
parentCmd->setText ("move population");
}
this->currProject->undoStack->push(parentCmd);
}
void nl_rootdata::onNewSelections (float xGL, float yGL)
{
//DBGMOUSE() << " emitting updatePanel";
emit updatePanel(this);
for (int i = 0; i < this->selList.size(); ++i) {
// register locations relative to cursor:
//DBGMOUSE() << "Setting a location offset...";
this->selList[i]->setLocationOffsetRelTo(xGL, yGL);
}
}
bool nl_rootdata::selListContains (const QVector <QSharedPointer<systemObject> >& objectList)
{
QVector <QSharedPointer<systemObject> >::const_iterator i = objectList.begin();
while (i != objectList.end()) {
if (std::find (this->selList.begin(), this->selList.end(), *i) != this->selList.end()) {
return true;
}
++i;
}
return false;
}
void nl_rootdata::deleteFromSelList (const QVector <QSharedPointer<systemObject> >& objectList)
{
QVector <QSharedPointer<systemObject> >::const_iterator i = objectList.begin();
while (i != objectList.end()) {
QVector <QSharedPointer<systemObject> >::iterator j = this->selList.begin();
while (j != this->selList.end()) {
if (*i == *j) { // That is, selList contains a member of objectList
j = this->selList.erase (j);
} else {
++j;
}
}
++i;
}
}
// When the "left" mouse goes down, select what's underneath, if anything.
void nl_rootdata::onLeftMouseDown(float xGL, float yGL, float GLscale, bool shiftDown)
{
//DBGMOUSE() << " called, shift is " << (shiftDown ? "Down" : "Up");
// Record the position of the selection.
this->lastLeftMouseDownPos.setX(xGL);
this->lastLeftMouseDownPos.setY(yGL);
// Before finding selections, see if user has an already selected
// projection/input and if so, if user has clicked on a handle.
if (!this->selList.empty() && this->selList.size() == 1) {
if (this->selList[0]->type == projectionObject || this->selList[0]->type == inputObject) {
QSharedPointer <projection> proj = qSharedPointerDynamicCast <projection> (selList[0]);
if ( proj->selectControlPoint(xGL, yGL, GLscale) ) {
return;
} // else no handle on the inputObject/projectionObject was selected
} // else the previously selected object was not an input or projection
}
// A list of things which have been selected with this left mouse
// down click. Will be added to this->selList after the logic in
// this method.
QVector <QSharedPointer<systemObject> > newlySelectedList;
this->findSelection (xGL, yGL, GLscale, newlySelectedList);
// Possibilities:
// 1. Nothing previously selected, user clicked on new object, shift OR no shift -> new selection
// 3. Nothing previously selected, user unselected, shift OR no shift -> do nothing
//
// 4. Something previously selected, user clicked on new object, no shift -> switch selection
// 5. Something previously selected, user clicked on new object, WITH shift -> add to selection
// 6. Something previously selected, user unselected, WITH shift -> do nothing
// 7. Something previously selected, user unselected, no shift -> clear selection
//
// 8. Something(s) were previously selected, user clicked on one of them -> User is moving selection.
if (this->selList.empty()) { // Nothing previously selected.
//DBGMOUSE() << "Nothing prev. selected...";
if (newlySelectedList.empty()) {
// Nothing selected now, do nothing but show the cursor.
DBG() << "Show cursor at position " << xGL << "," << yGL;
cursor.x = xGL;
cursor.y = yGL;
emit updatePanel(this);
} else {
// Have new selection, selList is empty, swap the contents
// of newlySelectedList into selList.
//DBGMOUSE() << "New selection, swap new selection into selList.";
this->selList.swap (newlySelectedList);
this->onNewSelections(xGL, yGL);
}
} else { // We have a previous selection.
//DBGMOUSE() << "We have a previous selection...";
if (newlySelectedList.empty()) {
// Nothing selected now.
//DBGMOUSE() << "Nothing is newly selected...";
if (shiftDown) {
// User still has shift down; do nothing. Show cursor?
//DBGMOUSE() << "User has shift down. Show cursor.";
cursor.x = xGL;
cursor.y = yGL;
// If we selected nothing, then just this:
emit updatePanel(this);
} else {
// Clear selection and show cursor:
//DBGMOUSE() << "User doesn't have shift down, so clear selection and show cursor.";
this->cursor.x = xGL;
this->cursor.y = yGL;
this->selList.clear();
this->onNewSelections(xGL, yGL);
}
} else {
// Have new selection
//DBGMOUSE() << "We have a new selection...";
if (shiftDown) {
// User still has shift down; append, leaving cursor unchanged
if (!this->selListContains (newlySelectedList)) {
//DBGMOUSE() << "User has shift down, (some of) newlySelected is not in selList, so append newlySelected onto selList";
//this->selList.insert (this->selList.end(), newlySelectedList.begin(), newlySelectedList.end());
// since we have moved to QVectors this should have the same effect as the above - Alex 17 July 2014
this->selList += newlySelectedList;
} else {
// user has shift down,. but newlySelected is already in selList, so in this case REMOVE it!
//DBGMOUSE() << "user has shift down clicking on existing object, so delete";
this->deleteFromSelList (newlySelectedList);
}
} else {
// Shift not down, user wishes to switch selection OR move several selected items
//DBGMOUSE() << "Shift is not down, so user wishes to switch selection or move selected items. Swap newly selected into selList";
if (this->selListContains (newlySelectedList)) {
//DBGMOUSE() << "selList contains newly selected; user wishes to MOVE selected items.";
// Nothing further to do here?
} else {
// Swap selection, leave cursor unchanged.
//DBGMOUSE() << "newly selected not in selList. user wishes to switch selection. Swap newly selected into selList";
this->selList.swap (newlySelectedList);
}
#if 0
// Or maybe:?
for (int i = 0; i < selList.size(); ++i) {
// register locations relative to cursor:
this->selList[i]->setLocationOffsetRelTo(xGL, yGL);
}
this->selectionMoved = false;
#endif
}
// As we selected something do this:
this->onNewSelections(xGL, yGL);
}
}
}
void nl_rootdata::findSelection (float xGL, float yGL, float GLscale, QVector <QSharedPointer<systemObject> >& newlySelectedList)
{
// Now look at populations and their inputs.
for (int i = 0; i < this->populations.size(); ++i) {
// check population inputs:
for (int j = 0; j < this->populations[i]->neuronType->inputs.size(); ++j) {
// find if an edge of the input is hit
if (this->populations[i]->neuronType->inputs[j]->is_clicked(xGL, yGL, GLscale)) {
// add the input to the selection list
newlySelectedList.push_back(this->populations[i]->neuronType->inputs[j]);
// selection complete, move on
return;
}
} // for loop checking population inputs
// check projections
for (int j = 0; j < this->populations[i]->projections.size(); ++j) {
// find if an edge of the projection is hit
if (this->populations[i]->projections[j]->is_clicked(xGL, yGL, GLscale)) {
// add to selection list
newlySelectedList.push_back(this->populations[i]->projections[j]);
// selection complete, move on
return;
}
// check proj inputs:
for (int k = 0; k < this->populations[i]->projections[j]->synapses.size(); ++k) {
QSharedPointer <synapse> col = this->populations[i]->projections[j]->synapses[k];
for (int l = 0; l < col->weightUpdateCmpt->inputs.size(); ++l) {
// find if an edge of the input is hit
if (col->weightUpdateCmpt->inputs[l]->is_clicked(xGL, yGL, GLscale)) {
// add to selection list
newlySelectedList.push_back(col->weightUpdateCmpt->inputs[l]);
// selection complete, move on
return;
}
}
for (int l = 0; l < col->postSynapseCmpt->inputs.size(); ++l) {
// find if an edge of the input is hit
if (col->postSynapseCmpt->inputs[l]->is_clicked(xGL, yGL, GLscale)) {
// add to selection list
newlySelectedList.push_back(col->postSynapseCmpt->inputs[l]);
// selection complete, move on
return;
}
}
} // for loop checking projection inputs.
} // for-loop over projections
// select if under the cursor - no two objects should overlap!
if (this->populations[i]->is_clicked(xGL, yGL, GLscale)) {
// add to selection list
newlySelectedList.push_back(this->populations[i]);
// selection complete, move on
return;
}
} // for-loop over populations
}
QColor nl_rootdata::getColor(QColor initCol)
{
// launch the QT colorpicker, and get a colour - if cancel is
// pressed then the output fails .isValid(), and we return the
// input instead
QColor out = QColorDialog::getColor(initCol);
if (out.isValid()) {
return out;
} else {
return initCol;
}
}
void nl_rootdata::deleteCurrentSelection()
{
if (selList.size() > 1) {
this->currProject->undoStack->push(new delSelection(this, selList));
} else if (selList.size() == 1) {
// separate out:
if (selList[0]->type == populationObject) {
QSharedPointer <population> pop = qSharedPointerDynamicCast <population> (selList[0]);
this->currProject->undoStack->push(new delPopulation(this,pop));
}