-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget.cpp
More file actions
1984 lines (1744 loc) · 65.3 KB
/
widget.cpp
File metadata and controls
1984 lines (1744 loc) · 65.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
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
/*
* Copyright (C) 2011 Weill Medical College of Cornell University
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <QCheckBox>
#include <QComboBox>
#include <QFile>
#include <QFileDialog>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QInputDialog>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QMessageBox>
#include <QPushButton>
#include <QScrollBar>
#include <QSignalMapper>
#include <QSpacerItem>
#include <QSpinBox>
#include <QTableWidget>
#include <QTimer>
#include <QVBoxLayout>
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include "widget.hpp"
#include <qtablewidget.h>
#include <qwt_legend.h>
#include <rtxi/debug.hpp>
#include <rtxi/event.hpp>
#include <rtxi/fifo.hpp>
#include <rtxi/rt.hpp>
#include <rtxi/rtos.hpp>
void clamp_protocol::Protocol::addStep(size_t seg_id)
{
if (seg_id >= segments.size()) { // If segment doesn't exist or not at end
return;
}
clamp_protocol::ProtocolSegment& segment = getSegment(seg_id);
segment.steps.push_back({});
}
void clamp_protocol::Protocol::insertStep(size_t seg_id, size_t step_id)
{
if (seg_id > segments.size() || step_id > segments.at(seg_id).steps.size()) {
return;
}
auto iter = segments.at(seg_id).steps.begin() + static_cast<int>(step_id);
segments.at(seg_id).steps.insert(iter, {});
}
void clamp_protocol::Protocol::deleteStep(size_t seg_id, size_t step_id)
{
if (seg_id > segments.size() || step_id > segments.at(seg_id).steps.size()) {
return;
}
clamp_protocol::ProtocolSegment segment = getSegment(seg_id);
auto it = segment.steps.begin() + static_cast<int>(step_id);
segment.steps.erase(it);
}
void clamp_protocol::Protocol::modifyStep(
size_t seg_id, size_t step_id, const clamp_protocol::ProtocolStep& step)
{
segments.at(seg_id).steps.at(step_id) = step;
}
std::array<std::vector<double>, 2> clamp_protocol::Protocol::dryrun(
double period)
{
std::array<std::vector<double>, 2> result;
int segmentIdx = 0;
int sweepsIdx = 0;
int stepIdx = 0;
double time_elapsed_ms = 0.0;
double current_time_ms = 0.0;
double voltage_mv = 0.0;
while (segmentIdx < segments.size()) {
while (sweepsIdx < segments.at(segmentIdx).numSweeps) {
while (stepIdx < segments.at(segmentIdx).steps.size()) {
ProtocolStep& step = getStep(segmentIdx, stepIdx);
switch (step.stepType) {
case clamp_protocol::STEP: {
voltage_mv = step.parameters[clamp_protocol::HOLDING_LEVEL_1]
+ step.parameters[clamp_protocol::DELTA_HOLDING_LEVEL_1]
* sweepsIdx;
break;
}
case clamp_protocol::RAMP: {
const double y2 = step.parameters[clamp_protocol::HOLDING_LEVEL_2]
+ (step.parameters[clamp_protocol::DELTA_HOLDING_LEVEL_2]
* sweepsIdx);
const double y1 = step.parameters[clamp_protocol::HOLDING_LEVEL_1]
+ (step.parameters[clamp_protocol::DELTA_HOLDING_LEVEL_1]
* sweepsIdx);
const double max_time =
step.parameters[clamp_protocol::STEP_DURATION]
+ (step.parameters[clamp_protocol::DELTA_STEP_DURATION]
* sweepsIdx);
const double slope = (y2 - y1) / max_time;
const double time_ms = std::min(max_time, time_elapsed_ms);
voltage_mv = slope * time_ms;
break;
}
default:
ERROR_MSG(
"ERROR - In function Protocol::dryrun() switch( stepType ) "
"default case called");
return {};
}
// update parameters
result[0].push_back(current_time_ms);
result[1].push_back(voltage_mv);
current_time_ms += period;
time_elapsed_ms += period;
stepIdx += static_cast<int>(
time_elapsed_ms
> (step.parameters[clamp_protocol::STEP_DURATION]
+ step.parameters[clamp_protocol::DELTA_STEP_DURATION]
* segmentIdx));
} // step loop
time_elapsed_ms = 0.0;
++sweepsIdx;
} // sweep loop
++segmentIdx;
} // segment loop
return result;
}
void clamp_protocol::Protocol::addSegment()
{
segments.emplace_back();
}
void clamp_protocol::Protocol::deleteSegment(size_t seg_id)
{
if (seg_id >= segments.size()) {
return;
}
auto it = segments.begin();
segments.erase(it + static_cast<int>(seg_id));
}
void clamp_protocol::Protocol::modifySegment(
size_t seg_id, const clamp_protocol::ProtocolSegment& segment)
{
segments.at(seg_id) = segment;
}
size_t clamp_protocol::Protocol::numSweeps(size_t seg_id)
{
return segments.at(seg_id).numSweeps;
}
void clamp_protocol::Protocol::setSweeps(size_t seg_id, uint32_t sweeps)
{
segments.at(seg_id).numSweeps = sweeps;
}
clamp_protocol::ProtocolSegment& clamp_protocol::Protocol::getSegment(
size_t seg_id)
{
return segments.at(seg_id);
}
clamp_protocol::ProtocolStep& clamp_protocol::Protocol::getStep(size_t segment,
size_t step)
{
return segments.at(segment).steps.at(step);
}
size_t clamp_protocol::Protocol::numSegments()
{
return segments.size();
}
size_t clamp_protocol::Protocol::segmentSize(size_t seg_id)
{
return segments.at(seg_id).steps.size();
}
QDomElement clamp_protocol::Protocol::stepToNode(QDomDocument& doc,
size_t seg_id,
size_t stepNum)
{
// Converts protocol step to XML node
QDomElement stepElement = doc.createElement("step"); // Step element
clamp_protocol::ProtocolStep step = segments.at(seg_id).steps.at(stepNum);
// Set attributes of step to element
stepElement.setAttribute("stepNumber", QString::number(stepNum));
stepElement.setAttribute("ampMode", QString::number(step.ampMode));
stepElement.setAttribute("stepType", QString::number(step.stepType));
stepElement.setAttribute(
"stepDuration",
QString::number(step.parameters.at(clamp_protocol::STEP_DURATION)));
stepElement.setAttribute(
"deltaStepDuration",
QString::number(step.parameters.at(clamp_protocol::DELTA_STEP_DURATION)));
stepElement.setAttribute(
"holdingLevel1",
QString::number(step.parameters.at(clamp_protocol::HOLDING_LEVEL_1)));
stepElement.setAttribute("deltaHoldingLevel1",
QString::number(step.parameters.at(
clamp_protocol::DELTA_HOLDING_LEVEL_1)));
stepElement.setAttribute(
"holdingLevel2",
QString::number(step.parameters.at(clamp_protocol::HOLDING_LEVEL_2)));
stepElement.setAttribute("deltaHoldingLevel2",
QString::number(step.parameters.at(
clamp_protocol::DELTA_HOLDING_LEVEL_2)));
return stepElement;
}
// Converts protocol segment to XML node
QDomElement clamp_protocol::Protocol::segmentToNode(QDomDocument& doc,
size_t seg_id)
{
QDomElement segmentElement = doc.createElement("segment"); // Segment element
const clamp_protocol::ProtocolSegment& segment = segments.at(seg_id);
segmentElement.setAttribute("numSweeps", QString::number(segment.numSweeps));
// Add each step as a child to segment element
for (size_t i = 0; i < segment.steps.size(); ++i) {
segmentElement.appendChild(stepToNode(doc, seg_id, i));
}
return segmentElement;
}
void clamp_protocol::Protocol::clear()
{
segments.clear();
}
// Convert protocol to QDomDocument
void clamp_protocol::Protocol::toDoc()
{
QDomDocument doc("ClampProtocolML");
QDomElement root = doc.createElement("Clamp-Suite-Protocol-v2.0");
doc.appendChild(root);
// Add segment elements to protocolDoc
for (size_t i = 0; i < segments.size(); ++i) {
root.appendChild(segmentToNode(doc, i));
}
protocolDoc = doc; // Shallow copy
}
// Load protocol from QDomDocument
void clamp_protocol::Protocol::fromDoc(const QDomDocument& doc)
{
QDomElement root = doc.documentElement(); // Get root element from document
// Retrieve information from document and set to protocolContainer
QDomNode segmentNode = root.firstChild(); // Retrieve first segment
clear(); // Clear vector containing protocol
size_t segmentCount = 0;
while (!segmentNode.isNull()) { // Segment iteration
QDomElement segmentElement = segmentNode.toElement();
size_t stepCount = 0;
segments.emplace_back(); // Add segment to protocol container
segments.at(segmentCount).numSweeps =
segmentElement.attribute("numSweeps").toInt();
QDomNode stepNode = segmentNode.firstChild();
while (!stepNode.isNull()) { // Step iteration
segments.at(segmentCount).steps.emplace_back();
clamp_protocol::ProtocolStep& step =
getStep(segmentCount, stepCount); // Retrieve step pointer
QDomElement stepElement = stepNode.toElement();
// Retrieve attributes
step.ampMode = static_cast<clamp_protocol::ampMode_t>(
stepElement.attribute("ampMode").toInt());
step.stepType = static_cast<clamp_protocol::stepType_t>(
stepElement.attribute("stepType").toInt());
step.parameters.at(clamp_protocol::STEP_DURATION) =
stepElement.attribute("stepDuration").toDouble();
step.parameters.at(clamp_protocol::DELTA_STEP_DURATION) =
stepElement.attribute("deltaStepDuration").toDouble();
step.parameters.at(clamp_protocol::HOLDING_LEVEL_1) =
stepElement.attribute("holdingLevel1").toDouble();
step.parameters.at(clamp_protocol::DELTA_HOLDING_LEVEL_1) =
stepElement.attribute("deltaHoldingLevel1").toDouble();
step.parameters.at(clamp_protocol::HOLDING_LEVEL_2) =
stepElement.attribute("holdingLevel2").toDouble();
step.parameters.at(clamp_protocol::DELTA_HOLDING_LEVEL_2) =
stepElement.attribute("deltaHoldingLevel2").toDouble();
stepNode = stepNode.nextSibling(); // Move to next step
stepCount++;
} // End step iteration
segmentNode = segmentNode.nextSibling(); // Move to next segment
segmentCount++;
} // End segment iteration
}
clamp_protocol::ClampProtocolEditor::ClampProtocolEditor(QWidget* parent)
: QWidget(parent)
{
createGUI();
setAttribute(Qt::WA_DeleteOnClose);
// QStringList for amp mode and step type combo boxes;
ampModeList.append("Voltage");
ampModeList.append("Current");
stepTypeList.append("Step");
stepTypeList.append("Ramp");
resize(minimumSize()); // Set window size to minimum
}
// Adds another segment to protocol: listview, protocol container, and calls
// summary update
void clamp_protocol::ClampProtocolEditor::addSegment()
{
protocol.addSegment();
QString segmentName = "Segment ";
// To help with sorting, a zero prefix is used for single digits
if (protocol.numSegments() < 10) {
segmentName += "0";
}
// Make QString of 'Segment ' + number of
// segments in container
segmentName.append(QString("%1").arg(protocol.numSegments()));
// Add segment reference to listView
auto* element = new QListWidgetItem(segmentName);
segmentListWidget->addItem(element);
segmentListWidget->setCurrentItem(element); // Focus on newly created segment
updateSegment(element);
}
void clamp_protocol::ClampProtocolEditor::deleteSegment()
{ // Deletes segment selected in listview: listview, protocol container, and
// calls summary update
int currentSegmentNumber = segmentListWidget->currentRow();
if (currentSegmentNumber < 0)
{ // If no segment exists, return and output error box
QMessageBox::warning(
this, "Error", "No segment has been created or selected.");
return;
}
// Message box asking for confirmation whether step should be deleted
QString segmentString;
segmentString.setNum(currentSegmentNumber);
QString text = "Do you wish to delete Segment " + segmentString
+ "?"; // Text pointing out specific segment and step
if (QMessageBox::question(
this, "Delete Segment Confirmation", text, "Yes", "No")
!= 0)
{
return; // Answer is no
}
if (protocol.numSegments() == 1)
{ // If only 1 segment exists, clear protocol
protocol.clear();
} else {
protocol.deleteSegment(currentSegmentNumber
- 1); // - 1 since parameter is an index
}
segmentListWidget->clear(); // Clear list view
// Rebuild list view
QListWidgetItem* element = nullptr;
for (int i = 0; i < protocol.numSegments(); i++) {
segmentString = "Segment ";
if (i < 10) { // Prefix with zero if a single digit
segmentString += "0";
}
segmentString += QString::number(i); // Add number to segment string
element = new QListWidgetItem(
segmentString, segmentListWidget); // Add segment to list view
segmentListWidget->addItem(element);
}
// Set to last segment and update table
if (protocol.numSegments() > 0) {
segmentListWidget->setCurrentItem(segmentListWidget->item(
segmentListWidget->count()
- 1)); // Apparently, qlistwidgets index by 0, which I know now no
// thanks to Qt's documentation.
updateSegment(segmentListWidget->item(segmentListWidget->count() - 1));
updateTable();
} else { // No segments are left
currentSegmentNumber = 0;
protocolTable->setColumnCount(0); // Clear table
// Prevent resetting of spinbox from triggering slot function by
// disconnecting
QObject::disconnect(segmentSweepSpinBox,
SIGNAL(valueChanged(int)),
this,
SLOT(updateSegmentSweeps(int)));
segmentSweepSpinBox->setValue(0); // Set sweep number spin box to zero
QObject::connect(segmentSweepSpinBox,
SIGNAL(valueChanged(int)),
this,
SLOT(updateSegmentSweeps(int)));
}
}
void clamp_protocol::ClampProtocolEditor::addStep()
{ // Adds step to a protocol segment: updates protocol container
if (segmentListWidget->count() == 0)
{ // If no segment exists, return and output error box
QMessageBox::warning(
this, "Error", "No segment has been created or selected.");
return;
}
protocol.addStep(segmentListWidget->currentRow()); // Add step to segment
updateTable(); // Rebuild table
// Set scroll bar all the way to the right when step is added
QScrollBar* hbar = protocolTable->horizontalScrollBar();
hbar->setValue(hbar->maximum());
}
void clamp_protocol::ClampProtocolEditor::insertStep()
{ // Insert step to a protocol segment: updates protocol container
if (segmentListWidget->currentRow() < 0) { // If no segment exists
QMessageBox::warning(this, "Error", "No segment has been created or selected.");
return;
}
if (protocolTable->currentColumn() >= 0) { // If other steps exist
protocol.insertStep(segmentListWidget->currentRow(),
protocolTable->currentColumn()); // Add step to segment
} else { // currentColumn() returns -1 if no columns exist
protocol.addStep(segmentListWidget->currentRow()); // Add step to segment
}
updateTable(); // Rebuild table
}
void clamp_protocol::ClampProtocolEditor::deleteStep()
{ // Delete step from a protocol segment: updates table, listview, and protocol
// container
if (segmentListWidget->currentRow() < 0) { // If no segment exists
QMessageBox::warning(this, "Error", "No segment has been created or selected.");
return;
}
int stepNum = protocolTable->currentColumn();
if (stepNum == -1) { // If no step exists, return and output error box
QMessageBox::warning(
this, "Error", "No step has been created or selected.");
return;
}
// Message box asking for confirmation whether step should be deleted
QString stepString;
stepString.setNum(stepNum);
QString segmentString;
segmentString.setNum(segmentListWidget->currentRow());
QString text = "Do you wish to delete Step " + stepString + " of Segment "
+ segmentString + "?"; // Text pointing out specific segment and step
bool answer =
QMessageBox::question(this, "Delete Step Confirmation", text, "Yes", "No")
!= 0;
if (answer) {
return;
}
protocol.deleteStep(segmentListWidget->currentRow(), stepNum);
updateTable();
}
void clamp_protocol::ClampProtocolEditor::updateColumn(
const clamp_protocol::ProtocolSegment& segment, int stepNum)
{
if (segment.steps.size() < stepNum) {
ERROR_MSG("Number of steps exceeded while buiding table. Aborting!");
return;
}
// protocolTable->insertColumn(stepNum); // Insert new column
QString headerLabel =
"Step " + QString("%1").arg(stepNum); // Make header label
auto* horizontalHeader = new QTableWidgetItem;
horizontalHeader->setText(headerLabel);
protocolTable->setHorizontalHeaderItem(stepNum, horizontalHeader);
auto* comboItem = new QComboBox(protocolTable);
clamp_protocol::ProtocolStep& step =
protocol.getStep(segmentListWidget->currentRow(), stepNum);
comboItem->addItems(ampModeList);
comboItem->setCurrentIndex(step.ampMode);
protocolTable->setCellWidget(0, stepNum, comboItem);
QObject::connect(comboItem,
SIGNAL(currentIndexChanged(int)),
this,
SLOT(comboBoxChanged()));
comboItem = new QComboBox(protocolTable);
comboItem->addItems(stepTypeList);
comboItem->setCurrentIndex(step.stepType); // Set box to retrieved attribute
protocolTable->setCellWidget(
1, stepNum, comboItem); // Add step type combo box
QObject::connect(comboItem,
SIGNAL(currentIndexChanged(int)),
this,
SLOT(comboBoxChanged()));
QTableWidgetItem* item = nullptr;
QString text;
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::STEP_DURATION)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(2, stepNum, item);
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::DELTA_STEP_DURATION)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(3, stepNum, item);
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::HOLDING_LEVEL_1)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(4, stepNum, item);
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::DELTA_HOLDING_LEVEL_1)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(5, stepNum, item);
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::HOLDING_LEVEL_2)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(6, stepNum, item);
item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
text.setNum(step.parameters.at(
clamp_protocol::DELTA_HOLDING_LEVEL_2)); // Retrieve attribute value
item->setText(text);
item->setFlags(item->flags() | Qt::ItemIsEditable);
protocolTable->setItem(7, stepNum, item);
}
void clamp_protocol::ClampProtocolEditor::comboBoxChanged()
{
auto* box = dynamic_cast<QComboBox*>(QObject::sender());
const int max_row = protocolTable->rowCount();
const int max_col = protocolTable->columnCount();
// Because Qt does not provide us with a function to find a qwidgetitem from
// the containing widget, we must do it ourselves.
for (int col = 0; col < max_col; ++col) {
for (int row = 0; row < max_row; ++row) {
if (protocolTable->cellWidget(row, col) == box) {
updateStepAttribute(row, col);
return;
}
}
}
}
void clamp_protocol::ClampProtocolEditor::updateSegment(
QListWidgetItem* segment)
{
// Updates protocol description table when segment is clicked in listview
// Update currentSegment to indicate which segment is selected
int currentSegmentNumber = segmentListWidget->row(segment);
if (currentSegmentNumber < 0) {
ERROR_MSG(
"clamp_protocol::ClampProtocolEditor : Segment somehow doesn't exist!");
return;
}
segmentSweepSpinBox->setValue(
static_cast<int>(protocol.numSweeps(static_cast<size_t>(
currentSegmentNumber)))); // Set sweep number spin box to value
// stored for particular segment
updateTableLabel(); // Update label of protocol table
}
void clamp_protocol::ClampProtocolEditor::updateSegmentSweeps(int sweepNum)
{ // Update container that holds number of segment sweeps when spinbox value is
// changed
protocol.setSweeps(segmentListWidget->currentRow(),
sweepNum); // Set segment sweep value to spin box value
}
void clamp_protocol::ClampProtocolEditor::updateTableLabel()
{ // Updates the label above protocol table to show current selected segment
// and step
QString text = "Segment ";
text.append(QString::number(segmentListWidget->currentRow()));
int col = protocolTable->currentColumn();
if (col != 0) {
text.append(": Step ");
text.append(QString::number(col));
}
segmentStepLabel->setText(text);
}
void clamp_protocol::ClampProtocolEditor::updateTable()
{
ProtocolSegment& segment =
protocol.getSegment(segmentListWidget->currentRow());
protocolTable->setColumnCount(segment.steps.size());
// Load steps from current clicked segment into protocol
for (int i = 0; i < segment.steps.size(); i++) {
updateColumn(segment, i); // Update step in protocol table
}
}
void clamp_protocol::ClampProtocolEditor::updateStepAttribute(int row, int col)
{ // Updates protocol container when a table cell is changed
clamp_protocol::ProtocolStep& step =
protocol.getStep(segmentListWidget->currentRow(), col);
QComboBox* comboItem = nullptr;
QString text;
QVariant val;
// Check which row and update corresponding attribute in step container
switch (row) {
case 0:
// Retrieve current item of combo box and set ampMode
comboItem = qobject_cast<QComboBox*>(protocolTable->cellWidget(row, col));
comboItem->blockSignals(true);
comboItem->setCurrentIndex(step.ampMode);
comboItem->blockSignals(true);
break;
case 1:
// Retrieve current item of combo box and set stepType
comboItem = qobject_cast<QComboBox*>(protocolTable->cellWidget(row, col));
updateStepType(col, step.stepType);
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
break;
default:
ERROR_MSG("Error - ProtocolEditor::updateStepAttribute() - default case");
break;
}
}
void clamp_protocol::ClampProtocolEditor::updateStepType(
int stepNum, clamp_protocol::stepType_t stepType)
{
// Disable unneeded attributes depending on step type
// Enable needed attributes and set text to stored value
clamp_protocol::ProtocolStep step =
protocol.getStep(segmentListWidget->currentRow(), stepNum);
QTableWidgetItem* item = nullptr;
const QString nullentry = "---";
switch (stepType) {
case clamp_protocol::STEP:
for (int i = clamp_protocol::HOLDING_LEVEL_2;
i < clamp_protocol::PROTOCOL_PARAMETERS_SIZE;
i++)
{
item = protocolTable->item(i + clamp_protocol::param_2_row_offset,
stepNum);
item->setText(nullentry);
item->setData(Qt::UserRole, 0.0);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
updateStepAttribute(i + clamp_protocol::param_2_row_offset, stepNum);
}
for (int i = clamp_protocol::STEP_DURATION;
i <= clamp_protocol::DELTA_HOLDING_LEVEL_1;
i++)
{
item = protocolTable->item(i + clamp_protocol::param_2_row_offset,
stepNum);
item->setText(QString::number(
step.parameters.at(i))); // Retrieve attribute and set text
item->setData(Qt::UserRole, step.parameters.at(i));
item->setFlags(item->flags() | Qt::ItemIsEditable);
updateStepAttribute(i + clamp_protocol::param_2_row_offset, stepNum);
}
break;
case clamp_protocol::RAMP:
for (int i = clamp_protocol::STEP_DURATION;
i <= clamp_protocol::DELTA_HOLDING_LEVEL_2;
i++)
{
item = protocolTable->item(i + clamp_protocol::param_2_row_offset,
stepNum);
item->setText(QString::number(
step.parameters.at(i))); // Retrieve attribute and set text
item->setData(Qt::UserRole, step.parameters.at(i));
item->setFlags(item->flags() | Qt::ItemIsEditable);
updateStepAttribute(i + clamp_protocol::param_2_row_offset, stepNum);
}
break;
default:
break;
}
}
int clamp_protocol::ClampProtocolEditor::loadFileToProtocol(
const QString& fileName)
{ // Loads XML file of protocol data: updates table, listview, and protocol
// container
// If protocol is present, warn user that protocol will be lost upon loading
if (protocol.numSegments() != 0
&& QMessageBox::warning(this,
"Load Protocol",
"All unsaved changes to current protocol will be "
"lost.\nDo you wish to continue?",
QMessageBox::Yes | QMessageBox::No)
!= QMessageBox::Yes)
{
return 0; // Return if answer is no
}
QDomDocument doc("protocol");
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{ // Make sure file can be opened, if not, warn user
QMessageBox::warning(this, "Error", "Unable to open protocol file");
return 0;
}
if (!doc.setContent(&file))
{ // Make sure file contents are loaded into document
QMessageBox::warning(
this, "Error", "Unable to set file contents to document");
file.close();
return 0;
}
file.close();
protocol.fromDoc(doc); // Translate document into protocol
if (protocol.numSegments() == 0) {
QMessageBox::warning(
this, "Error", "Protocol did not contain any segments");
return 0;
}
// Build segment listview
for (int i = 0; i < protocol.numSegments(); i++) {
QString segmentName = "Segment ";
if (protocol.numSegments() < 10)
{ // To help with sorting, a zero prefix is used for single digits
segmentName += "0";
}
segmentName += QString::number(i);
auto* element = new QListWidgetItem(
segmentName, segmentListWidget); // Add segment reference to listView
segmentListWidget->addItem(element);
}
segmentListWidget->setCurrentItem(segmentListWidget->item(0));
updateSegment(segmentListWidget->item(0));
updateTable();
return 1;
}
QString clamp_protocol::ClampProtocolEditor::loadProtocol()
{
// Save dialog to retrieve desired filename and location
QString fileName = QFileDialog::getOpenFileName(
this,
"Open a protocol",
"~/",
"Clamp Protocol Files (*.csp);;All Files(*.*)");
if (fileName.isNull() || fileName.isEmpty()) {
return ""; // User cancelled dialog
}
clearProtocol();
int retval = loadFileToProtocol(fileName);
if (retval == 0) {
return ""; // If error occurs
}
return fileName;
}
void clamp_protocol::ClampProtocolEditor::loadProtocol(const QString& fileName)
{
loadFileToProtocol(fileName);
}
void clamp_protocol::ClampProtocolEditor::saveProtocol()
{ // Takes data within protocol container and converts to XML and saves to file
if (protocolEmpty()) { // Exit if protocol is empty
return;
}
protocol.toDoc(); // Update protocol QDomDocument
// Save dialog to retrieve desired filename and location
QString fileName = QFileDialog::getSaveFileName(
this,
"Save the protocol",
"~/",
"Clamp Protocol Files (*.csp);;All Files (*.*)");
// If filename does not include .csp extension, add extension
if (!(fileName.endsWith(".csp"))) {
fileName.append(".csp");
}
// If filename exists, warn user
if (QFileInfo(fileName).exists()
&& QMessageBox::warning(this,
"File Exists",
"Do you wish to overwrite " + fileName + "?",
QMessageBox::Yes | QMessageBox::No)
!= QMessageBox::Yes)
{
return; // Return if answer is no
}
// Save protocol to file
QFile file(fileName); // Open file
if (!file.open(QIODevice::WriteOnly))
{ // Open file, return error if unable to do so
QMessageBox::warning(
this, "Error", "Unable to save file: Please check folder permissions.");
return;
}
QTextStream ts(&file); // Open text stream
ts << protocol.getProtocolDoc().toString(); // Write to file
file.close(); // Close file
}
void clamp_protocol::ClampProtocolEditor::syncTableState(QTableWidgetItem* item)
{
const int row = item->row();
const int col = item->column();
if (row < 0 || col < 0) {
ERROR_MSG(
"clamp_protocol::ClampProtocolEditor::syncTableState : invalid row or "
"column values!");
return;
}
ProtocolSegment& segment =
protocol.getSegment(segmentListWidget->currentRow());
segment.steps.at(col).parameters.at(row - param_2_row_offset) =
item->text().toInt();
}
void clamp_protocol::ClampProtocolEditor::clearProtocol()
{ // Clear protocol
protocol.clear();
protocolTable->setColumnCount(0); // Clear table
segmentListWidget->clear();
// Prevent resetting of spinbox from triggering slot function by disconnecting
QObject::disconnect(segmentSweepSpinBox,
SIGNAL(valueChanged(int)),
this,
SLOT(updateSegmentSweeps(int)));
segmentSweepSpinBox->setValue(1); // Set sweep number spin box to zero
QObject::connect(segmentSweepSpinBox,
SIGNAL(valueChanged(int)),
this,
SLOT(updateSegmentSweeps(int)));
}
void clamp_protocol::ClampProtocolEditor::exportProtocol()
{ // Export protocol to a text file format ( time : output )
if (protocolEmpty()) { // Exit if protocol is empty
return;
}
// Grab period
bool ok = false;
double period = QInputDialog::getDouble(this,
"Export Clamp Protocol",
"Enter the period (ms): ",
0.010,
0,
1000,
3,
&ok);
if (!ok) {
return; // User cancels
}
// Save dialog to retrieve desired filename and location
QString fileName =
QFileDialog::getSaveFileName(this,
"Export Clamp Protocol",
"~/",
"Text files (*.txt);;All Files (*.*)");
// If filename does not include .txt extension, add extension
if (!(fileName.endsWith(".txt"))) {
fileName.append(".txt");
}
// If filename exists, warn user
if (QFileInfo(fileName).exists()
&& QMessageBox::warning(this,
"File Exists",
"Do you wish to overwrite " + fileName + "?",
QMessageBox::Yes | QMessageBox::No)
!= QMessageBox::Yes)
{
return; // Return if answer is no
}
// Save protocol to file
QFile file(fileName); // Open file
if (!file.open(QIODevice::WriteOnly))
{ // Open file, return error if unable to do so
QMessageBox::warning(
this, "Error", "Unable to save file: Please check folder permissions.");
return;
}
if (fileName.isNull() || fileName.isEmpty()) {
return; // User cancelled dialog
}
// Run protocol with user specified period
std::array<std::vector<double>, 2> run;
run = protocol.dryrun(period);
std::vector<double> time = run.at(0);
std::vector<double> output = run.at(1);
QTextStream ts(&file);
for (auto itx = time.begin(), ity = output.begin(); itx < time.end();
itx++, ity++)
{ // Iterate through vectors and output to file
ts << *itx << " " << *ity << "\n";
}
file.close(); // Close file
}
void clamp_protocol::ClampProtocolEditor::previewProtocol()
{ // Graph protocol output in a simple plot window
if (protocolEmpty()) {
return; // Exit if protocol is empty
}
// Create a dialog with a BasicPlot
auto* dlg = new QDialog(this, Qt::Dialog);