forked from SpineML/SpineCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SC_projectobject.cpp
1825 lines (1523 loc) · 60.4 KB
/
SC_projectobject.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
#include "SC_projectobject.h"
#include "CL_classes.h"
#include "CL_layout_classes.h"
#include "SC_network_layer_rootdata.h"
#include "mainwindow.h"
#include "SC_versioncontrol.h"
#include "EL_experiment.h"
#include "SC_systemmodel.h"
projectObject::projectObject(QObject *parent) :
QObject(parent)
{
this->name = "New Project";
this->menuAction = new QAction(this);
this->undoStack = new QUndoStack(this);
// Screen cursor pos initialised in the nl_rootdata object to 0,0 also.
//this->currentCursorPos.x = 0.0;
//this->currentCursorPos.y = 0.0;
// default fileNames
this->networkFile = "model.xml";
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
// On loading an old-style project, this->metaFile is set up from the project XML file.
this->metaFile = "";
#endif
// create the catalog blank entries:
this->catalogGC.push_back(QSharedPointer<Component> (new Component()));
this->catalogGC[0]->name = "none";
this->catalogGC[0]->type = "moo";
this->catalogNB.push_back(QSharedPointer<Component> (new Component()));
this->catalogNB[0]->name = "none";
this->catalogNB[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->catalogLAY.push_back(QSharedPointer<NineMLLayout> (new NineMLLayout()));
this->catalogLAY[0]->name = "none";
#ifdef CLEANUP_AT_PROJECT_OPEN
// NO, not here, do this in mainwindow.cpp when the PROGRAM
// starts, not when a project is opened. If it's cleaned up here,
// then a second opened project will remove the files needed by
// the first opened project.
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QDir lib_dir = QDir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
#else
QDir lib_dir = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
#endif
lib_dir.setFilter(QDir::Files);
foreach(QString dirFile, lib_dir.entryList()) {
lib_dir.remove(dirFile);
}
#endif
}
projectObject::~projectObject()
{
// clean up these
delete this->undoStack;
delete this->menuAction;
// destroy experiments
for (int i = 0; i < this->experimentList.size(); ++i) {
delete this->experimentList[i];
}
for (int i = 0; i < network.size(); ++i) {
for (int j = 0; j < network[i]->projections.size(); ++j) {
for (int k = 0; k < network[i]->projections[j]->synapses.size(); ++k) {
network[i]->projections[j]->synapses[k].clear();
}
network[i]->projections[j].clear();
}
network[i].clear();
}
// delete catalog components
for (int i = 0; i < this->catalogLAY.size(); ++i) {
this->catalogLAY[i].clear();
}
for (int i = 0; i < this->catalogNB.size(); ++i) {
this->catalogNB[i].clear();
}
for (int i = 0; i < this->catalogPS.size(); ++i) {
this->catalogPS[i].clear();
}
for (int i = 0; i < this->catalogWU.size(); ++i) {
this->catalogWU[i].clear();
}
for (int i = 0; i < this->catalogGC.size(); ++i) {
this->catalogGC[i].clear();
}
// clear catalog vectors
this->catalogLAY.clear();
this->catalogNB.clear();
this->catalogPS.clear();
this->catalogWU.clear();
this->catalogGC.clear();
}
QString projectObject::getFilenameFriendlyName (void)
{
// Make nameFname directory-friendly, replace spaces with '_'
QString nameFname = this->name;
nameFname.replace(' ', '_');
return nameFname;
}
bool projectObject::open_project(QString fileName)
{
QDir project_dir(fileName);
// remove filename
project_dir.cdUp();
QSettings settings;
settings.setValue("files/currentFileName", project_dir.absolutePath());
// Set currentCursorPos to 0 before opening a project to ensure we
// don't translate anything in position.
this->currentCursorPos.x = 0;
this->currentCursorPos.y = 0;
// first try and open the project file
if (!this->load_project_file(fileName)) {
printErrors("Errors found loading the project file:");
return false;
}
// then load in all the components listed in the project file
for (int i = 0; i < this->components.size(); ++i) {
this->loadComponent(this->components[i], project_dir);
}
printErrors("Errors found loading project Components:");
// then load in all the layouts listed in the project file
for (int i = 0; i < this->layouts.size(); ++i) {
this->loadLayout(this->layouts[i], project_dir);
}
printErrors("Errors found loading project Layouts:");
// now the network
this->loadNetwork(this->networkFile, project_dir);
if (printErrors("Errors prevented loading the Project:")) {
return false;
}
// finally the experiments (this->experiments populated in this->load_project_file)
for (int i = 0; i < this->experiments.size(); ++i) {
loadExperiment(this->experiments[i], project_dir);
}
printErrors("Errors found loading project Experiments:");
// check for errors
printWarnings("Issues were found while loading the project:");
// store the new file name
this->filePath = fileName;
return true;
}
bool projectObject::save_project(QString fileName, nl_rootdata * data)
{
if (!fileName.contains(".")) {
QMessageBox msgBox;
msgBox.setText("Project file needs .proj suffix.");
msgBox.exec();
return false;
}
DBG() << "save_project ('" << fileName << "', rootData*)";
QDir project_dir(fileName);
// remove filename
project_dir.cdUp();
// check for version control
this->version.setupVersion();
// No longer remove explicitDataBinaryFiles on save - we'll
// overwrite those files which need overwriting, and we'll use the
// files present in the directory to help choose new names for new
// property/explicitDataBinaryFiles.
//
// However, we DO remove old connection binary files (but not
// explicitData binary files).
project_dir.setNameFilters(QStringList() << "conn*.bin");
QStringList files = project_dir.entryList(QDir::Files);
for (int i = 0; i < files.size(); ++i) {
// delete
project_dir.remove(files[i]);
// and remove from version control
if (this->version.isModelUnderVersion()) {
this->version.removeFromVersion(files[i]);
}
}
// sync project
copy_back_data(data);
// write project file
if (!save_project_file(fileName)) {
return false;
}
// write components
for (int i = 1; i < this->catalogNB.size(); ++i) {
saveComponent(this->catalogNB[i]->getXMLName(), project_dir, this->catalogNB[i]);
}
for (int i = 1; i < this->catalogWU.size(); ++i) {
saveComponent(this->catalogWU[i]->getXMLName(), project_dir, this->catalogWU[i]);
}
for (int i = 1; i < this->catalogPS.size(); ++i) {
saveComponent(this->catalogPS[i]->getXMLName(), project_dir, this->catalogPS[i]);
}
for (int i = 1; i < this->catalogGC.size(); ++i) {
saveComponent(this->catalogGC[i]->getXMLName(), project_dir, this->catalogGC[i]);
}
// write layouts
for (int i = 1; i < this->catalogLAY.size(); ++i) {
saveLayout(this->catalogLAY[i]->getXMLName(), project_dir, this->catalogLAY[i]);
}
// write network
saveNetwork(this->networkFile, project_dir);
// write experiments
for (int i = 0; i < this->experimentList.size(); ++i) {
saveExperiment("experiment" + QString::number(i) + ".xml", project_dir, this->experimentList[i]);
}
// copy additional files
for (int i = 0; i < this->additionalFiles.size(); ++i) {
// copy additionalFiles[i] to project_dir / additionalFiles[i].fileName()
QFileInfo fileInfo(this->additionalFiles[i]);
QFile::copy(additionalFiles[i], project_dir.absolutePath() + QDir::separator() + fileInfo.fileName());
}
// store the new file name
this->filePath = fileName;
if (printErrors("Errors found")) {
return false;
}
if (printWarnings("Warnings found")) {
return false;
}
this->undoStack->setClean();
return true;
}
bool projectObject::import_network(QString fileName, cursorType cursorPos)
{
DBG() << "projectObject::import_network(" << fileName << ")";
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
// In case there is a metaFile set, make a copy of it.
QString metaFileCopy(this->metaFile);
#endif
// Compute the extent of the existing network:
std::pair<QPointF, QPointF> ext = this->getNetworkExtent (this->network);
DBG() << "Network extent: TL:" << ext.first << " BR: " << ext.second;
QDir project_dir(fileName);
// Update current cursor position, used to offset the imported
// network (so it won't land on top of the existing network).
this->currentCursorPos = cursorPos;
// remove filename
project_dir.cdUp();
// Set currentFileName
QSettings settings;
settings.setValue("files/currentFileName", project_dir.absolutePath());
// get a list of all the files in the directory containing fileName
QStringList files = project_dir.entryList();
// load all the component files
for (int i = 0; i < files.size(); ++i) {
if (isComponent(project_dir.absoluteFilePath(files[i]))) {
this->loadComponent(files[i], project_dir);
}
}
// load all the layout files
for (int i = 0; i < files.size(); ++i) {
if (isLayout(project_dir.absoluteFilePath(files[i]))) {
this->loadLayout(files[i], project_dir);
}
}
int firstNewPop = this->network.size();
// load the network file itself
this->loadNetwork(fileName, project_dir, false);
if (printErrors("Errors prevented importing the Network:")) {
return false;
}
// set up metaData if not loaded. That's _this_ metaFile. We need
// to load the network making use of the metadata that comes
// alongside (old format) or inside (new format) the model.xml
// file. loadNetwork() can set this->metaFile to "not found".
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
if (this->metaFile == "not found") {
this->metaFile = metaFileCopy;
#endif
// place the new populations in a diagonal line:
for (int i = firstNewPop; i < this->network.size(); ++i) {
QSharedPointer <population> p = this->network[i];
// Adds a bit to x and y positions, leaving the existing
// network populations unchanged and applying the cursor
// position offset - that is, the diagonal line of new
// populations starts at the cursor and is directed up and
// right.
p->x = (i-firstNewPop)*2.0f + this->currentCursorPos.x;
p->targx = p->x;
p->y = (i-firstNewPop)*2.0f + this->currentCursorPos.y;
p->targy = p->y;
p->size = 1.0f;
p->aspect_ratio = 5.0f/3.0f;
p->setupBounds();
}
// make projection and generic input curves to link up the
// newly placed populations
for (int i = firstNewPop; i < this->network.size(); ++i) {
DBG() << "Placing " << this->network[i]->projections.size() << " projections for population in network["<<i<<"]";
for (int j = 0; j < this->network[i]->projections.size(); ++j) {
this->network[i]->projections[j]->add_curves();
}
DBG() << "Placing " << this->network[i]->neuronType->inputs.size() << " inputs for network["<<i<<"]";
for (int j = 0; j < this->network[i]->neuronType->inputs.count(); ++j) {
this->network[i]->neuronType->inputs[j]->add_curves();
}
}
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
}
#endif
// finally load the experiments. This will load ALL experiment
// files in the directory, which may include some stale ones,
// which will cause errors.
for (int i = 0; i < files.size(); ++i) {
this->loadExperiment(files[i], project_dir, true);
}
printWarnings("Issues found importing the Network:");
printErrors("Errors found importing the Network:");
return true;
}
void projectObject::import_component(QString fileName)
{
QDir project_dir(fileName);
project_dir.cdUp(); // removes filename
loadComponent(fileName, project_dir);
}
void projectObject::import_layout(QString fileName)
{
QDir project_dir(fileName);
project_dir.cdUp();
loadLayout(fileName, project_dir);
}
bool projectObject::load_project_file(QString fileName)
{
// open the file
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox;
msgBox.setText("Could not open the project file");
msgBox.exec();
return false;
}
// Extract project_dir from project filename
QDir project_dir(fileName);
project_dir.cdUp();
// get a streamreader
QXmlStreamReader * reader = new QXmlStreamReader;
reader->setDevice(&file);
// read elements
while (reader->readNextStartElement()) {
if (reader->name() == "SpineCreatorProject") {
while (reader->readNextStartElement()) {
if (reader->name() == "Network") {
while (reader->readNextStartElement()) {
if (reader->name() == "File") {
if (reader->attributes().hasAttribute("name")) {
this->networkFile = reader->attributes().value("name").toString();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - missing attribute 'name'");
settings.endArray();
}
#ifdef KEEP_OLD_STYLE_METADATA_XML_FILE_LOADING_FOR_COMPATIBILITY
if (reader->attributes().hasAttribute("metaFile")) {
this->metaFile = reader->attributes().value("metaFile").toString();
} // else expect to be using new in-model LL:Annotation format
#endif
reader->skipCurrentElement();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else if (reader->name() == "Components") {
while (reader->readNextStartElement()) {
if (reader->name() == "File") {
if (reader->attributes().hasAttribute("name")) {
this->components.push_back(reader->attributes().value("name").toString());
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - missing attribute 'name'");
settings.endArray();
}
reader->skipCurrentElement();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else if (reader->name() == "Layouts") {
while (reader->readNextStartElement()) {
if (reader->name() == "File") {
if (reader->attributes().hasAttribute("name")) {
this->layouts.push_back(reader->attributes().value("name").toString());
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - missing attribute 'name'");
settings.endArray();
}
reader->skipCurrentElement();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else if (reader->name() == "Experiments") {
while (reader->readNextStartElement()) {
if (reader->name() == "File") {
if (reader->attributes().hasAttribute("name")) {
this->experiments.push_back(reader->attributes().value("name").toString());
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - missing attribute 'name'");
settings.endArray();
}
reader->skipCurrentElement();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else if (reader->name() == "AdditionalFiles") {
while (reader->readNextStartElement()) {
if (reader->name() == "File") {
if (reader->attributes().hasAttribute("name")) {
// Note that we store the additional file as a full path.
this->additionalFiles.push_back(project_dir.absolutePath() + QDir::separator() + reader->attributes().value("name").toString());
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - missing attribute 'name'");
settings.endArray();
}
reader->skipCurrentElement();
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - unknown tag '" + reader->name().toString() + "'");
settings.endArray();
}
}
} else {
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
settings.beginWriteArray("errors");
settings.setArrayIndex(num_errs + 1);
settings.setValue("errorText", "XML Error in Project File - incorrect start tag");
settings.endArray();
}
}
return true;
}
bool projectObject::save_project_file(QString fileName)
{
// complain if there's no extension (client code should correctly set fileName)
if (!fileName.contains(".")) {
QMessageBox msgBox;
msgBox.setText("Project file needs .proj suffix.");
msgBox.exec();
return false;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox msgBox;
msgBox.setText("Could not create the project file '" + fileName + "'");
msgBox.exec();
return false;
}
// get a streamwriter
QXmlStreamWriter * writer = new QXmlStreamWriter;
writer->setDevice(&file);
// write elements
writer->writeStartDocument();
writer->writeStartElement("SpineCreatorProject");
writer->writeStartElement("Network");
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->networkFile);
#if 0 // In new format, metadata is stored in model.xml (and component.xml files too)
writer->writeAttribute("metaFile", this->metaFile);
#endif
writer->writeEndElement(); // Network
writer->writeStartElement("Components");
for (int i = 1; i < this->catalogNB.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->catalogNB[i]->getXMLName());
}
for (int i = 1; i < this->catalogWU.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->catalogWU[i]->getXMLName());
}
for (int i = 1; i < this->catalogPS.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->catalogPS[i]->getXMLName());
}
for (int i = 1; i < this->catalogGC.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->catalogGC[i]->getXMLName());
}
writer->writeEndElement(); // Components
writer->writeStartElement("Layouts");
for (int i = 0; i < this->catalogLAY.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", this->catalogLAY[i]->getXMLName());
}
writer->writeEndElement(); // Layouts
writer->writeStartElement("Experiments");
for (int i = 0; i < this->experimentList.size(); ++i) {
writer->writeEmptyElement("File");
writer->writeAttribute("name", "experiment" + QString::number(i) + ".xml");
}
writer->writeEndElement(); // Experiments
if (!this->additionalFiles.isEmpty()) {
writer->writeStartElement("AdditionalFiles");
for (int i = 0; i < this->additionalFiles.size(); ++i) {
QFileInfo fileInfo(this->additionalFiles[i]);
writer->writeEmptyElement("File");
writer->writeAttribute("name", fileInfo.fileName());
}
writer->writeEndElement(); // AdditionalFiles
}
writer->writeEndElement(); // SpineCreatorProject
// add to version control
if (this->version.isModelUnderVersion()) {
this->version.addToVersion(file.fileName());
}
return true;
}
bool projectObject::isComponent(QString fileName)
{
// try opening the file and loading the XML
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
if (!this->doc.setContent(&file)) {
return false;
}
// we have loaded the XML - discard the file handle
file.close();
// confirm root tag is correct
QDomElement root = this->doc.documentElement();
if (root.tagName() != "SpineML" ) {
this->doc.clear();
return false;
}
// if a componentclass
QDomElement classType = root.firstChildElement();
if (classType.tagName() != "ComponentClass") {
this->doc.clear();
return false;
}
// clean up
this->doc.clear();
return true;
}
bool projectObject::isLayout(QString fileName)
{
// try opening the file and loading the XML
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) {
return false;
}
if(!this->doc.setContent(&file)) {
return false;
}
// we have loaded the XML - discard the file handle
file.close();
// confirm root tag is correct
QDomElement root = this->doc.documentElement();
if (root.tagName() != "SpineML") {
this->doc.clear();
return false;
}
// if a componentclass
QDomElement classType = root.firstChildElement();
if (classType.tagName() != "LayoutClass") {
this->doc.clear();
return false;
}
// clean up
this->doc.clear();
return true;
}
void projectObject::loadComponent(QString fileName, QDir project_dir)
{
if (fileName == "none.xml") {
return;
}
// try opening the file and loading the XML
QFile file(project_dir.absoluteFilePath(fileName));
if (!file.open(QIODevice::ReadOnly)) {
addError("Cannot open required file '" + fileName + "'");
return;
}
if (!this->doc.setContent(&file)) {
addError("Cannot read required file '" + fileName + "'");
return;
}
// confirm root tag is correct
QDomElement root = this->doc.documentElement();
if (root.tagName() != "SpineML" ) {
addError("Missing or incorrect root tag in required file '" + fileName + "'");
return;
}
// if a componentclass
QDomElement classType = root.firstChildElement();
if (classType.tagName() == "ComponentClass") {
// HANDLE SPINEML COMPONENTS //
// create a new AL class instance and populate it from the data
QSharedPointer<Component>tempALobject = QSharedPointer<Component> (new Component());
tempALobject->load(&this->doc);
// check for errors:
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
// if there are errors then clean up and leave
if (num_errs != 0) {
tempALobject.clear();
// write tail for errors:
addError("<b>IN COMPONENT FILE '" + fileName + "'</b>");
return;
}
// get lib to add component to
QVector < QSharedPointer<Component> > * curr_lib;
if (tempALobject->type == "neuron_body") {
curr_lib = &this->catalogNB;
} else if (tempALobject->type == "weight_update") {
curr_lib = &this->catalogWU;
} else if (tempALobject->type == "postsynapse") {
curr_lib = &this->catalogPS;
} else {
curr_lib = &this->catalogGC;
}
// check the name doesn't already exist in the library
for (int i = 0; i < curr_lib->size(); ++i) {
if ((*curr_lib)[i]->name == tempALobject->name
&& (*curr_lib)[i]->path == tempALobject->path
&& tempALobject->name != "none") {
// same name
QString ees = "Two required files have the same Component Name (" + (tempALobject->path) + "/" + (tempALobject->name) + "). This project may be corrupted";
addWarning(ees);
tempALobject.clear();
return;
}
}
// add to the correct catalog
curr_lib->push_back(tempALobject);
} else {
addError("Unknown XML tag found in required file '" + fileName + "'");
}
}
void projectObject::saveComponent(QString fileName, QDir project_dir, QSharedPointer<Component> component)
{
// if no extension then append a .xml
if (!fileName.contains(".")) {
fileName.append(".xml");
}
QString fname = project_dir.absoluteFilePath(fileName);
QFile file(fname);
if (!file.open(QIODevice::WriteOnly)) {
addError("saveComponent: Error creating file for '" + fname + "' - is there sufficient disk space?");
return;
}
this->doc.setContent(QString(""));
// get the 9ML description
component->write(&this->doc);
// write out to file
QTextStream tsFromFile(&file );
tsFromFile << this->doc.toString();
tsFromFile.flush();
// add to version control
if (this->version.isModelUnderVersion()) {
this->version.addToVersion(file.fileName());
}
file.close();
// store path for easy access
component->filePath = project_dir.absoluteFilePath(fileName);
// kill off the DOM document
this->doc.clear();
}
void projectObject::loadLayout(QString fileName, QDir project_dir)
{
if (fileName == "none.xml") {
return;
}
// try opening the file and loading the XML
QFile file(project_dir.absoluteFilePath(fileName));
if (!file.open(QIODevice::ReadOnly)) {
addError("Cannot open required file '" + fileName + "'");
return;
}
if (!this->doc.setContent(&file)) {
addError("Cannot read required file '" + fileName + "'");
return;
}
// confirm root tag is correct
QDomElement root = this->doc.documentElement();
if (root.tagName() != "SpineML" ) {
addError("Missing or incorrect root tag in required file '" + fileName + "'");
return;
}
// if a componentclass
QDomElement classType = root.firstChildElement();
if (classType.tagName() == "LayoutClass") {
// HANDLE LAYOUTS
// create a new AL class instance and populate it from the data
QSharedPointer<NineMLLayout>tempALobject = QSharedPointer<NineMLLayout> (new NineMLLayout());
tempALobject->load(&this->doc);
// check for errors:
QSettings settings;
int num_errs = settings.beginReadArray("errors");
settings.endArray();
if (num_errs != 0) {
tempALobject.clear();
// tail for errors:
addError("<b>IN LAYOUT FILE '" + fileName + "'</b>");
return;
}
for (int i = 0; i < this->catalogLAY.size(); ++i) {
if (this->catalogLAY[i]->name.compare(tempALobject->name) == 0 && tempALobject->name != "none") {
// same name
addWarning("Two required files have the same Layout Name - this project may be corrupted");
tempALobject.clear();
return;
}
}
// all good - add layout to catalog
this->catalogLAY.push_back(tempALobject);
} else {
addError("Unknown XML tag found in required file '" + fileName + "'");
}
}
void projectObject::saveLayout(QString fileName, QDir project_dir, QSharedPointer<NineMLLayout> layout)
{
// if no extension then append a .xml
if (!fileName.contains(".")) {
fileName.append(".xml");
}
QFile file(project_dir.absoluteFilePath(fileName));
if (!file.open(QIODevice::WriteOnly)) {
addError("Error creating file for '" + fileName + "' - is there sufficient disk space?");
return;
}
this->doc.setContent(QString(""));
// get the 9ML description
layout->write(&this->doc);
// write out to file
QTextStream tsFromFile(&file);
tsFromFile << this->doc.toString();
tsFromFile.flush();
// add to version control
if (this->version.isModelUnderVersion()) {
this->version.addToVersion(file.fileName());
}
file.close();
// store path for easy access
layout->filePath = project_dir.absoluteFilePath(fileName);
// kill off the DOM document
this->doc.clear();
}
cursorType
projectObject::getCursorPos (void)
{
return this->currentCursorPos;
}
void projectObject::loadNetwork(QString fileName, QDir project_dir, bool isProject)
{
// load up the file and check it is valid XML
QFile file(project_dir.absoluteFilePath(fileName));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
addError("Could not open the Network file for reading");
return;
}
if (!this->doc.setContent(&file)) {
addError("Could not parse the Network file XML - is the selected file correctly formed XML?");
return;
}
// we have loaded the XML file - discard the file handle
file.close();
// confirm root tag is correct
QDomElement root = this->doc.documentElement();
if (root.tagName() != "LL:SpineML") {
addError("Network file is not valid SpineML Low Level Network Layer description");