forked from NatLabRockies/SAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariablegrid.cpp
More file actions
1326 lines (1156 loc) · 36.4 KB
/
variablegrid.cpp
File metadata and controls
1326 lines (1156 loc) · 36.4 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
/*
BSD 3-Clause License
Copyright (c) Alliance for Energy Innovation, LLC. See also https://github.com/NREL/SAM/blob/develop/LICENSE
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <algorithm>
#include <set>
#include <wx/sizer.h>
#include <wx/bitmap.h>
#include <wx/msgdlg.h>
#include <wx/tokenzr.h>
#include <wx/clipbrd.h>
#include <wx/filename.h>
#include <wx/busyinfo.h>
#include <wex/metro.h>
#include "variablegrid.h"
#include "widgets.h"
#include "inputpage.h"
#include "object.h"
#include "main.h"
#define COMPARE_SHOW_ALL 0
#define COMPARE_SHOW_DIFFERENT 1
#define COMPARE_SHOW_SAME 2
//VariableGridData::VariableGridData(ProjectFile* pf, Case* c, VarTable* vt) : m_pf(pf), m_vt(vt)
VariableGridData::VariableGridData(ProjectFile* pf, Case* c, std::vector<VarTable*> *vts) : m_pf(pf), m_vts(vts)
{
m_attr_for_calculated = new wxGridCellAttr;
m_attr_for_calculated->SetBackgroundColour(UIColorCalculatedBack);
m_attr_for_calculated->SetTextColour(UIColorCalculatedFore);
m_sorted = false;
if (c)
m_cases.push_back(c);
else
m_cases = m_pf->GetCases();
Init();
}
VariableGridData::~VariableGridData()
{
m_attr_for_calculated->DecRef();
}
void VariableGridData::Init()
{
m_col_hdrs.Clear();
m_var_names.Clear();
m_var_labels.Clear();
// m_vsVariableData.clear();
m_var_info_lookup_vec.clear();
m_var_table_vec.clear();
VarInfoLookup vi_not_calculated;
VarTable vt_not_calculated;
size_t i, n;
if (m_cases.size() > 0)
{
m_col_hdrs.push_back("Variable");
m_col_hdrs.push_back("Label");
if (m_cases.size() == 1)
{
m_col_hdrs.push_back(m_pf->GetCaseName(m_cases[0]));
// if (m_vt)
// m_var_table_vec.push_back(m_vt); // for parametric simulation
// else
// m_var_table_vec.push_back(&m_cases[0]->Values(0));// TODO: hybrids
ConfigInfo* ci = m_cases[0]->GetConfiguration();
if (!ci) return; // throw error?
n = ci->Technology.size();
if (m_vts) {
m_var_table_vec.push_back(*m_vts); // for parametric simulation
}
else {
std::vector<VarTable*> pvt;
for (i = 0; i < n; i++)
pvt.push_back(&m_cases[0]->Values(i));
m_var_table_vec.push_back(pvt);
}
std::vector<VarInfoLookup*> pvil;
for (i = 0; i < n; i++)
pvil.push_back(&m_cases[0]->Variables(i));
m_var_info_lookup_vec.push_back(pvil);
}
else
{
for (std::vector<Case*>::iterator it = m_cases.begin(); it != m_cases.end(); ++it)
{
m_col_hdrs.push_back(m_pf->GetCaseName(*it));
ConfigInfo* ci = (*it)->GetConfiguration();
if (!ci) return; // throw error?
n = ci->Technology.size();
std::vector<VarTable*> pvt;
for (i = 0; i < n; i++)
pvt.push_back(&(*it)->Values(i));
m_var_table_vec.push_back(pvt);
std::vector<VarInfoLookup*> pvil;
for (i = 0; i < n; i++)
pvil.push_back(&(*it)->Variables(i));
m_var_info_lookup_vec.push_back(pvil);
// m_var_table_vec.push_back(&(*it)->Values(0));// TODO: hybrids
// m_var_info_lookup_vec.push_back(&(*it)->Variables(0));// TODO: hybrids
}
}
m_cols = m_col_hdrs.Count();
std::set<wxString> var_names; // set - unique names
// variable names
// skip calculated
// iterate over cases
for (size_t iCase = 0; iCase < m_cases.size(); iCase++) {
ConfigInfo* ci = m_cases[iCase]->GetConfiguration();
// loop through all vartables inside a case (non-hybrid has one vartable, hybrids have one vartable for each generator, fuel cell and battery, and remainder of system)
for (size_t iVarTable = 0; iVarTable < m_var_info_lookup_vec[iCase].size(); iVarTable++) {
// iterate over case vartables
wxString prepend = ci->Technology[iVarTable].Lower() + ":";
wxArrayString as = m_var_info_lookup_vec[iCase][iVarTable]->ListAll();
for (size_t i = 0; i < as.Count(); i++) {
// if ((!((*it)->Lookup(as[i])->Flags & VF_CALCULATED)) &&
// (!((*it)->Lookup(as[i])->Flags & VF_INDICATOR)))
if (!(m_var_info_lookup_vec[iCase][iVarTable]->Lookup(as[i])->Flags & VF_INDICATOR)) {
// check to see if name in set
if (ci->Technology.size() == 1) {// non-hybrid
var_names.insert(as[i]);
}
else { // hybrid
// if dependent variable then skip
bool dependentVariable = false;
for (auto& hv : m_cases[iCase]->GetConfiguration()->HybridVariables) {
if (hv.DependentVariableVarTable == iVarTable && hv.DependentVariableName == as[i])
dependentVariable = true;
}
// do not prepend "Hybrid" vartable
if (iVarTable == ci->Technology.size() - 1) prepend = "";
if (!dependentVariable)
var_names.insert(prepend + as[i]); // e.g. "pvwatts_tilt" for hybrid - currently will not compare to "tilt" in non-hybrid configurations
}
}
}
}
}
/* all
for (std::vector<VarTable*>::iterator it = m_var_table_vec.begin(); it != m_var_table_vec.end(); ++it)
{
wxArrayString as = (*it)->ListAll();
for (size_t i = 0; i < as.Count(); i++)
var_names.insert(as[i]);
}
*/
int row_change = (int)var_names.size() - m_rows;
if (m_rows > 0) {
if (row_change > 0)
AppendRows(row_change);
else if (row_change < 0)
DeleteRows(0,-row_change);
}
m_rows = var_names.size();
for (int row = 0; row < m_rows; row++)
m_sorted_index.Add(row);
// variable labels after determining which variables to display in var_names
for (std::set<wxString>::iterator idx = var_names.begin(); idx != var_names.end(); ++idx) {
wxString str_label = " ";
bool found = false;
for (size_t iCase = 0; iCase < m_cases.size() && !found; iCase++) {
ConfigInfo* ci = m_cases[iCase]->GetConfiguration();
Case* c = m_cases[iCase];
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, *idx, &var_name, &ndx_hybrid)) {
if (m_var_info_lookup_vec[iCase][ndx_hybrid]->Lookup(var_name)) {
found = true;
wxString prepend = "";
// prepend to Label to hybrid subsystems and not general financial parameters
if (ci->Technology.size() > 1 && ndx_hybrid < ci->Technology.size() - 1) prepend = ci->Technology[ndx_hybrid] + " ";
str_label = m_var_info_lookup_vec[iCase][ndx_hybrid]->Label(var_name);
if (str_label.length() > 0) str_label = prepend + str_label;
}
}
}
m_var_labels.push_back(str_label);
m_var_names.push_back(*idx);
}
}
}
int VariableGridData::GetNumberRows()
{
return m_rows;
}
int VariableGridData::GetNumberCols()
{
return m_cols;
}
bool VariableGridData::UpdateVarNameNdxHybrid(Case *c, const wxString& input_name, wxString* var_name, size_t* ndx_hybrid)
{
if (!c) return false;
*ndx_hybrid = c->GetConfiguration()->Technology.size() - 1; // size always >=1
*var_name = input_name;
// decode if necessary for hybrids varname for unsorted index
// if (c->GetConfiguration()->Technology.size() > 1) {
// split hybrid name and match with Technology name or use "Hybrid" for remainder
wxArrayString as = wxSplit(input_name, ':');
if (as.size() > 1) {
for (size_t j = 0; j < c->GetConfiguration()->Technology.size(); j++) {
if (c->GetConfiguration()->Technology[j].Lower() == as[0]) {
// if (c->GetConfiguration()->Simulations[j].Lower() == as[0]) {
*ndx_hybrid = j;
*var_name = input_name.Right(input_name.length() - (as[0].length() + 1));
}
}
}
// }
return true;
}
bool VariableGridData::IsEmptyCell(int row, int col)
{
if (!GetView()->GetParent()->IsShown()) return true;
if (col == 0) // variable name
return (m_var_names[row].IsEmpty());
else if (col == 1) // variable label
return (m_var_labels[row].IsEmpty());
else // get var table and value
{
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (!UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid))
return false;
else {
if (m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name))
{
if ((col - 2) >= (int)m_var_table_vec.size())
return true;
else
return (m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name)->AsString() == wxEmptyString);
}
else
return true;
}
}
}
wxString VariableGridData::GetColLabelValue(int col)
{
if (col < (int)m_col_hdrs.size())
return m_col_hdrs[col];
else
return wxEmptyString;
}
wxString VariableGridData::GetVarName(int row, int col)
{
wxString ret_val = wxEmptyString;
if ((col > 0) && (col < (int)m_var_names.Count()) && (row > -1) && (row < m_rows))
{
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
ret_val = m_var_names[lookup_row];
}
return ret_val;
}
Case* VariableGridData::GetCase(int row, int col)
{
Case* c = NULL;
int lookup_col = col - 2; // skip variable and label columns
if (lookup_col > -1 && lookup_col < (int)m_cases.size())
c = m_cases[lookup_col];
return c;
}
VarInfo* VariableGridData::GetVarInfo(int row, int col)
{
VarInfo* vi = NULL;
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if ((col > 1) && ((col - 2) < (int)m_var_info_lookup_vec.size())) {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid))
vi = m_var_info_lookup_vec[col - 2][ndx_hybrid]->Lookup(var_name);
}
return vi;
}
void VariableGridData::SetVarInfo(int row, int col, VarInfo *vi)
{
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if ((col > 1) && ((col - 2) < (int)m_var_info_lookup_vec.size())) {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (VarInfo* var_info = m_var_info_lookup_vec[col - 2][ndx_hybrid]->Lookup(var_name))
var_info = vi;
}
}
}
VarValue* VariableGridData::GetVarValue(int row, int col)
{
VarValue* vv = NULL;
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if ((col > 1) && ((col - 2) < (int)m_var_table_vec.size())) {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
vv = m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name);
}
}
return vv;
}
void VariableGridData::SetVarValue(int row, int col, VarValue *vv)
{
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if ((col > 1) && ((col - 2) < (int)m_var_table_vec.size())) {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (VarValue* var_value = m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name))
var_value = vv;
}
}
}
/*
bool VariableGridData::CanGetValueAs(int row, int col, const wxString &typeName)
{
if (GetTypeName(row, col) == wxGRID_VALUE_CHOICE)
return (typeName == wxGRID_VALUE_NUMBER);
}
bool VariableGridData::CanSetValueAs(int row, int col, const wxString &typeName)
{
if (GetTypeName(row, col) == wxGRID_VALUE_CHOICE)
return (typeName == wxGRID_VALUE_NUMBER);
}
*/
wxString VariableGridData::GetChoice(int row, int col)
{
wxString ret_str = wxEmptyString;
if ((col>-1) && (col < m_cols))
{
if (VarInfo *vi = GetVarInfo(row, col))
{
wxArrayString as = vi->IndexLabels;
int ndx = -1;
double val;
if (GetValue(row, col).ToDouble(&val)) ndx = int(val);
if ((as.Count() > 0) && (ndx >= 0) && (ndx < (int)as.Count()))
ret_str = as[ndx];
}
}
return ret_str;
}
wxString VariableGridData::GetChoices(int row, int col)
{
wxString ret_str = wxEmptyString;
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if ( col >= 2 ) // get var table and value
{
if ((col - 2) < (int)m_var_info_lookup_vec.size()) {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (m_var_info_lookup_vec[col - 2][ndx_hybrid]->Lookup(var_name)) {
wxArrayString as = m_var_info_lookup_vec[col - 2][ndx_hybrid]->Lookup(var_name)->IndexLabels;
if (as.Count() > 0) {
for (int i = 0; i < (int)as.Count() - 1; i++)
ret_str += as[i] + ",";
ret_str += as[as.Count() - 1];
}
}
}
}
}
return ret_str;
}
wxString VariableGridData::GetValue(int row, int col)
{
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
if (col == 0) // variable name
return m_var_names[lookup_row];
else if (col == 1) // variable label
return m_var_labels[lookup_row];
else { // get var table and value
if ((col - 2) >= (int)m_var_table_vec.size())
return wxEmptyString;
else {
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name))
return m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name)->AsString();
else
return wxEmptyString;
}
}
}
}
void VariableGridData::SetValue(int row, int col, const wxString& value)
{
if (col == 0)
return; // no updating of variable name allowed here!
else if (col == 1)
return; // no updating of variable label allowed here!
else if ((col > 1) && (col < (int)(m_cases.size() + 2))) {
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name)) {
VarValue* vv = m_var_table_vec[col - 2][ndx_hybrid]->Get(var_name);
if (vv) {
VarValue::Parse(vv->Type(), value, *vv);
// updates ui from grid
m_cases[col - 2]->VariableChanged(var_name, ndx_hybrid);
}
}
}
}
}
void VariableGridData::Sort(int col, bool ascending)
{
// get all values for column
m_sorted = false;
wxArrayString col_values;
for (int row = 0; row < m_rows; row++)
col_values.Add(GetValue(row, col));
wxArrayString sorted_col_value(col_values);
// sort
sorted_col_value.Sort(!ascending);
// update index to get value
// TODO - handle same values
for (int row = 0; row < m_rows; row++)
m_sorted_index[row] = -1;
for (int row = 0; row < m_rows; row++)
{
int ndx = col_values.Index(sorted_col_value[row]);
if (m_sorted_index.Index(ndx) != wxNOT_FOUND)
col_values[ndx] = "|||||||||"; // hopefully not in original list
ndx = col_values.Index(sorted_col_value[row]);
m_sorted_index[row] = ndx;
}
m_sorted = true;
}
wxGridCellAttr *VariableGridData::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind)
{
wxGridCellAttr *attr = NULL;
if (GetAttrProvider())
{
attr = GetAttrProvider()->GetAttr(row, col, kind);
if (GetTypeName(row, col) == "GridCellCalculated")
{
if (!attr)
{
attr = m_attr_for_calculated;
attr->IncRef();
}
else if (!attr->HasBackgroundColour())
{
wxGridCellAttr *attrNew = attr->Clone();
attr->DecRef();
attr = attrNew;
attr->SetTextColour(UIColorCalculatedFore);
attr->SetBackgroundColour(UIColorCalculatedBack);
}
}
}
return attr;
}
wxString VariableGridData::GetTypeName(int row, int col)
{
if ((col > -1) && (col < 2))
return wxGRID_VALUE_STRING;
else if (col < m_cols) {
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
Case* c = GetCase(row, col);
wxString var_name;
size_t ndx_hybrid;
if (lookup_row < m_var_names.size() && UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (VarInfo* var_info = m_var_info_lookup_vec[col - 2][ndx_hybrid]->Lookup(var_name))
{ // TODO - better control list maintenance here and in UIEditorPanel
wxString type = var_info->UIObject;
bool calculated = (var_info->Flags & VF_CALCULATED) > 0;
if (calculated)
return "GridCellCalculated";
else if (type == "Numeric")
return wxGRID_VALUE_STRING;
else if (type == "Choice")
return "GridCellChoice";
else if (type == "ListBox")
return "GridCellVarValue";
else if (type == "RadioChoice")
return "GridCellVarValue";
else if (type == "TextEntry")
return wxGRID_VALUE_STRING;
else if (type == "MultilineText")
return wxGRID_VALUE_STRING;
else if (type == "Slider")
return "GridCellVarValue";
else if (type == "CheckBox")
return "GridCellCheckBox";
else if (type == "SchedNumeric")
return "GridCellVarValue";
else if (type == "TOUSchedule")
return "GridCellVarValue";
else if (type == "PTLayout")
return "GridCellVarValue";
else if (type == "MaterialProperties")
return "GridCellVarValue";
else if (type == "TroughLoop")
return "GridCellVarValue";
else if (type == "MonthlyFactor")
return "GridCellVarValue";
else if (type == "SearchListBox")
return "GridCellVarValue";
else if (type == "DataArray")
return "GridCellVarValue";
else if (type == "DataLifetimeArray")
return "GridCellVarValue";
else if (type == "StringArray")
return "GridCellVarValue";
else if (type == "DataMatrix")
return "GridCellVarValue";
else if (type == "DataLifetimeMatrix")
return "GridCellVarValue";
else if (type == "ShadingFactors")
return "GridCellVarValue";
else if (type == "ValueMatrix")
return "GridCellVarValue";
else if (type == "MonthByHourFactors")
return "GridCellVarValue";
else if (type == "Library")
return "GridCellVarValue";
else if (type == "LossAdjustment")
return "GridCellVarValue";
else if (type == "DiurnalPeriod")
return "GridCellVarValue";
else if (var_info->UIObject == VUIOBJ_NONE)
return wxGRID_VALUE_STRING;
else
return wxGRID_VALUE_STRING;
}
else
return wxGRID_VALUE_STRING;
}
else
return wxGRID_VALUE_STRING;
}
else
return wxGRID_VALUE_STRING;
}
bool VariableGridData::ShowRow(int row, int comparison_type, bool show_calculated)
{
bool show = true;
if (m_cases.size() > 1) // comparison
{
switch (comparison_type)
{
case COMPARE_SHOW_DIFFERENT:
case COMPARE_SHOW_SAME:
{ // note that the comparison starts with first case and then check other cases if variable exists - ORDER DEPENDENT
int lookup_row = row;
if (m_sorted) lookup_row = m_sorted_index[row];
Case* c = m_cases[0];
wxString var_name;
size_t ndx_hybrid;
if (m_var_names.size() > 0) {
if (UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if (m_var_table_vec[0][ndx_hybrid]->Get(var_name)) {
VarValue* vv = m_var_table_vec[0][ndx_hybrid]->Get(var_name);
bool row_varvalues_same = true;
for (int col = 1; col < (int)m_cases.size(); col++) {
// decode and key off of m_var_names[lookup_row]
wxString col_var_name;
size_t col_ndx_hybrid;
if (UpdateVarNameNdxHybrid(m_cases[col], m_var_names[lookup_row], &col_var_name, &col_ndx_hybrid)) {
if (m_var_table_vec[col][col_ndx_hybrid]->Get(col_var_name)) {
VarValue* vv_new = m_var_table_vec[col][col_ndx_hybrid]->Get(col_var_name);
if (vv->Type() == vv_new->Type())
row_varvalues_same = (row_varvalues_same && (vv->ValueEqual(*vv_new)));
else {
show = false; // different variable type in row - should not happen
continue;
}
}
else {
show = false; // no variable value for (row, col)
continue;
}
}
else {
show = false; // no m_var_names[lookup_row] value for (row, col)
continue;
}
}
if (comparison_type == COMPARE_SHOW_DIFFERENT)
show = (show && !row_varvalues_same);
else
show = (show && row_varvalues_same);
}
else
show = false; // no variable in first case
}
else
show = false; // no variable value for (row, col=0)
}
else
show = false; // no var name for (row, col=0)
}
break;
case COMPARE_SHOW_ALL:
default:
break;
}
}
// check for calculated inputs
if (show) {
bool calculated = false;
int lookup_row = row;
if ((row < (int)m_sorted_index.Count()) && (m_sorted)) lookup_row = m_sorted_index[row];
// calculated = calculated in any case!
for (size_t iCase = 0; iCase < m_cases.size(); iCase++) {
Case* c = m_cases[iCase];
wxString var_name;
size_t ndx_hybrid;
if (lookup_row < m_var_names.size() && UpdateVarNameNdxHybrid(c, m_var_names[lookup_row], &var_name, &ndx_hybrid)) {
if ((lookup_row < (int)m_var_names.Count()) && (m_var_info_lookup_vec[iCase][ndx_hybrid]->Lookup(var_name))) {
VarInfo* vi = m_var_info_lookup_vec[iCase][ndx_hybrid]->Lookup(var_name);
if (vi)
calculated = calculated || (vi->Flags & VF_CALCULATED) > 0;
}
}
}
if (calculated) show = show && show_calculated;
}
return show;
}
bool VariableGridData::DeleteCase(Case *c)
{
std::vector<Case*>::iterator it = std::find(m_cases.begin(), m_cases.end(), c);
if (it != m_cases.end())
{
m_cases.erase(it);
Init();
DeleteCols();
return true;
}
return false;
}
bool VariableGridData::DeleteCols(size_t pos, size_t numCols)
{
if (GetView())
{
wxGridTableMessage msg(this,
wxGRIDTABLE_NOTIFY_COLS_DELETED,
pos,
numCols);
GetView()->ProcessTableMessage(msg);
}
return true;
}
bool VariableGridData::DeleteRows(size_t pos, size_t numRows)
{
if (GetView())
{
wxGridTableMessage msg(this,
wxGRIDTABLE_NOTIFY_ROWS_DELETED,
pos,
numRows);
GetView()->ProcessTableMessage(msg);
}
return true;
}
bool VariableGridData::AddCase(Case *c)
{
std::vector<Case*>::iterator it = std::find(m_cases.begin(), m_cases.end(), c);
if (it == m_cases.end())
{
m_cases.push_back(c);
if (c->GetConfiguration()) {
Init();
AppendCols();
return true;
}
}
return false;
}
bool VariableGridData::AppendCols(size_t numCols)
{
if (GetView())
{
wxGridTableMessage msg(this,
wxGRIDTABLE_NOTIFY_COLS_APPENDED,
numCols);
GetView()->ProcessTableMessage(msg);
}
return true;
}
bool VariableGridData::AppendRows(size_t numRows)
{
if (GetView())
{
wxGridTableMessage msg(this,
wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
numRows);
GetView()->ProcessTableMessage(msg);
}
return true;
}
bool VariableGridData::RenameCase(const wxString &old_name, const wxString &new_name)
{
int ndx = m_col_hdrs.Index(old_name);
if (ndx == wxNOT_FOUND)
return false;
else
{
SetColLabelValue(ndx, new_name);
return true;
}
}
void VariableGridData::SetColLabelValue(int col, const wxString &label)
{
if ((col > 0) && (col < (int)m_col_hdrs.Count()))
{
m_col_hdrs[col] = label;
}
}
////////////////////////////////////////////////////////////////////////////////////////
BEGIN_EVENT_TABLE(VariableGrid, wxExtGridCtrl)
EVT_GRID_CELL_LEFT_CLICK(VariableGrid::OnLeftClick)
END_EVENT_TABLE()
VariableGrid::VariableGrid(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long , const wxString &) : wxExtGridCtrl(parent, id, pos, size)
{
}
VariableGrid::~VariableGrid()
{
}
void VariableGrid::OnLeftClick(wxGridEvent &evt)
{
SetGridCursor(evt.GetRow(), evt.GetCol());
evt.Skip();
}
////////////////////////////////////////////////////////////////////////////////////////
enum {
__idFirst = wxID_HIGHEST + 992,
ID_SHOW_DIFFERENT, ID_SHOW_SAME, ID_SHOW_ALL, ID_EXP_CLIPBOARD, ID_EXP_CSV, ID_EXP_EXCEL, ID_EXP_BTN, ID_VIEW_BTN, ID_FILTER, ID_SHOW_CALCULATED
};
BEGIN_EVENT_TABLE(VariableGridFrame, wxFrame)
EVT_MENU(ID_SHOW_DIFFERENT, VariableGridFrame::OnCommand)
EVT_MENU(ID_SHOW_SAME, VariableGridFrame::OnCommand)
EVT_MENU(ID_SHOW_ALL, VariableGridFrame::OnCommand)
EVT_MENU(ID_SHOW_CALCULATED, VariableGridFrame::OnCommand)
EVT_MENU(ID_EXP_CLIPBOARD, VariableGridFrame::OnCommand)
EVT_MENU(ID_EXP_CSV, VariableGridFrame::OnCommand)
EVT_MENU(ID_EXP_EXCEL, VariableGridFrame::OnCommand)
EVT_BUTTON(ID_EXP_BTN, VariableGridFrame::OnCommand)
EVT_BUTTON(ID_VIEW_BTN, VariableGridFrame::OnCommand)
EVT_BUTTON(wxID_HELP, VariableGridFrame::OnCommand)
EVT_TEXT( ID_FILTER, VariableGridFrame::OnCommand)
EVT_GRID_COL_SORT(VariableGridFrame::OnGridColSort)
// EVT_SHOW(VariableGridFrame::OnShow)
END_EVENT_TABLE()
VariableGridFrame::VariableGridFrame(wxWindow* parent, ProjectFile* pf, Case* c, std::vector<VarTable*>* vts, wxString frame_title)
: wxFrame(parent, wxID_ANY, "Variable Grid", wxDefaultPosition, wxScaleSize(800, 700)), m_pf(pf)
// VariableGridFrame::VariableGridFrame(wxWindow* parent, ProjectFile* pf, Case* c, VarTable* vt, wxString frame_title)
// : wxFrame(parent, wxID_ANY, "Variable Grid", wxDefaultPosition, wxScaleSize(800, 700)), m_pf(pf)
{
m_show_calculated = false;
SetBackgroundColour( wxMetroTheme::Colour( wxMT_FOREGROUND ) );
if (!m_pf) return;
// if (!vt) m_pf->AddListener(this); // no listeners when using parametric var tables
if (!vts) m_pf->AddListener(this); // no listeners when using parametric var tables
if (c)
{
m_cases.push_back(c);
m_input_list = true; // input list - do not allow for cases to be added
}
else
{
m_cases = m_pf->GetCases();
m_input_list = false;
}
if (m_cases.size() > 0)
{
// if (!vt)
if (!vts)
{
for (size_t i = 0; i < m_cases.size(); i++)
m_cases[i]->AddListener(this);
}
wxString title=frame_title;
if (title.IsEmpty())
{
if (m_cases.size() == 1)
title = "Inputs Browser"; //Current Case Values: " + m_pf->GetCaseName(m_cases[0]);
else
title = "Inputs Browser"; //Case comparison";
}
SetTitle(title);
// m_griddata = new VariableGridData(m_pf, c, vt);
m_griddata = new VariableGridData(m_pf, c, vts);
m_grid = new VariableGrid(this, wxID_ANY);
m_grid->RegisterDataType("GridCellCheckBox", new GridCellCheckBoxRenderer, new GridCellCheckBoxEditor);
m_grid->RegisterDataType("GridCellChoice", new GridCellChoiceRenderer, new GridCellChoiceEditor);
m_grid->RegisterDataType("GridCellVarValue", new GridCellVarValueRenderer, new GridCellVarValueEditor);
m_grid->RegisterDataType("GridCellCalculated", new GridCellCalculatedRenderer, new GridCellCalculatedEditor);
m_grid->HideRowLabels();
m_grid->SetTable(m_griddata, true, wxGrid::wxGridSelectRows);
// wxBoxSizer *comparetools = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *tools = new wxBoxSizer(wxHORIZONTAL);
m_btn_export = new wxMetroButton(this, ID_EXP_BTN, "Export", wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxMB_DOWNARROW);
tools->Add(m_btn_export, 0, wxALL | wxEXPAND, 0);
m_btn_view = new wxMetroButton(this, ID_VIEW_BTN, "View", wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxMB_DOWNARROW);
tools->Add( m_btn_view, 0, wxALL|wxEXPAND, 0 );
tools->AddSpacer(5);
m_filter = new wxTextCtrl( this, ID_FILTER );
wxStaticText *lblfilter = new wxStaticText( this, wxID_ANY, "Search:" );
lblfilter->SetForegroundColour( *wxWHITE );
lblfilter->SetFont( wxMetroTheme::Font( wxMT_NORMAL ) );
tools->Add( lblfilter, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3 );
tools->Add( m_filter, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3 );
tools->AddStretchSpacer();
tools->Add(new wxMetroButton(this, wxID_HELP, "Help"), 0, wxALL | wxEXPAND, 0);
m_sizer = new wxBoxSizer(wxVERTICAL);
m_sizer->Add(tools, 0, wxALL | wxEXPAND, 0);
m_sizer->Add(m_grid, 1, wxALL | wxEXPAND, 0);
SetSizer(m_sizer);
SizeColumns();
m_compare_show_type = m_cases.size() > 1 ? COMPARE_SHOW_DIFFERENT : COMPARE_SHOW_ALL;
UpdateGrid();
}
#ifdef __WXMSW__
SetIcon(wxICON(appicon));
#endif
CenterOnParent();
Show();
}
void VariableGridFrame::OnShow(wxShowEvent& )
{
m_griddata->Sort(0, !(m_grid->IsSortingBy(0) &&
m_grid->IsSortOrderAscending()));
UpdateGrid();
}
VariableGridFrame::~VariableGridFrame()
{
/*Strange issue with VS2013 in release mode only*/
/*The following code caused AV when exiting with input list open*/
/*No issue running in debug mode or outside of VS2013*/
std::vector<Case*> pfcases;
if ( m_pf != 0 )
{
pfcases = m_pf->GetCases();