-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLocationTabView.cpp
More file actions
836 lines (653 loc) · 31.1 KB
/
Copy pathLocationTabView.cpp
File metadata and controls
836 lines (653 loc) · 31.1 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
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) 2020-2021, OpenStudio Coalition and other contributors. 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 any contributors may be used to endorse or promote products
* derived from this software without specific prior written permission from the respective party.
*
* (4) Other than as required in clauses (1) and (2), distributions in any form of modifications or other derivative works
* may not use the "OpenStudio" trademark, "OS", "os", or any other confusingly similar designation without specific prior
* written permission from Alliance for Sustainable Energy, LLC.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND ANY 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(S), ANY CONTRIBUTORS, THE UNITED STATES GOVERNMENT, OR THE UNITED
* STATES DEPARTMENT OF ENERGY, NOR ANY OF THEIR EMPLOYEES, 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 "LocationTabView.hpp"
#include "DesignDayGridView.hpp"
#include "ModelObjectListView.hpp"
#include "OSAppBase.hpp"
#include "OSDocument.hpp"
#include "OSDropZone.hpp"
#include "OSItemSelectorButtons.hpp"
#include "SchedulesTabController.hpp"
#include "../shared_gui_components/OSGridView.hpp"
#include "../openstudio_app/OpenStudioApp.hpp"
#include <openstudio/model/ClimateZones.hpp>
#include <openstudio/model/ClimateZones_Impl.hpp>
#include <openstudio/model/DesignDay.hpp>
#include <openstudio/model/DesignDay_Impl.hpp>
#include <openstudio/model/Model_Impl.hpp>
#include <openstudio/model/RunPeriod.hpp>
#include <openstudio/model/RunPeriodControlDaylightSavingTime.hpp>
#include <openstudio/model/RunPeriodControlDaylightSavingTime_Impl.hpp>
#include <openstudio/model/RunPeriod_Impl.hpp>
#include <openstudio/model/Site_Impl.hpp>
#include <openstudio/model/SizingPeriod.hpp>
#include <openstudio/model/SizingPeriod_Impl.hpp>
#include <openstudio/model/WeatherFile.hpp>
#include <openstudio/model/WeatherFileConditionType.hpp>
#include <openstudio/model/WeatherFileDays.hpp>
#include <openstudio/model/WeatherFile_Impl.hpp>
#include <openstudio/model/YearDescription_Impl.hpp>
#include "../model_editor/Utilities.hpp"
#include <openstudio/energyplus/ReverseTranslator.hpp>
//#include "../runmanager/lib/ConfigOptions.hpp"
#include <openstudio/utilities/core/Assert.hpp>
#include <openstudio/utilities/filetypes/EpwFile.hpp>
#include <openstudio/utilities/idf/IdfFile.hpp>
#include <openstudio/utilities/idd/IddEnums.hxx>
#include <boost/smart_ptr.hpp>
#include <QBoxLayout>
#include <QComboBox>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QScrollArea>
#include <QSettings>
#include <QSizePolicy>
static constexpr auto NAME("Name: ");
static constexpr auto LATITUDE("Latitude: ");
static constexpr auto LONGITUDE("Longitude: ");
static constexpr auto ELEVATION("Elevation: ");
static constexpr auto TIME_ZONE("Time Zone: ");
static constexpr auto SETWEATHERFILE("Set Weather File");
static constexpr auto CHANGEWEATHERFILE("Change Weather File");
namespace openstudio {
LocationTabView::LocationTabView(const model::Model& model, const QString& modelTempDir, QWidget* parent)
: MainTabView(tr("Site"), MainTabView::SUB_TAB, parent) {}
LocationView::LocationView(bool isIP, const model::Model& model, const QString& modelTempDir)
: m_model(model),
m_site(m_model.getUniqueModelObject<model::Site>()),
m_yearDescription(m_model.getUniqueModelObject<model::YearDescription>()),
m_modelTempDir(modelTempDir),
m_isIP(isIP) {
OS_ASSERT(m_site);
loadQSettings();
auto climateZones = m_model.getUniqueModelObject<model::ClimateZones>();
// ***** Main Layout *****
auto* mainLayout = new QVBoxLayout();
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
setLayout(mainLayout);
// ***** Scroll *****
auto* scrollLayout = new QVBoxLayout();
scrollLayout->setContentsMargins(0, 0, 0, 0);
scrollLayout->setSpacing(0);
auto* scrollWidget = new QWidget();
scrollWidget->setObjectName("ScrollWidget");
scrollWidget->setStyleSheet("QWidget#ScrollWidget { background: transparent; }");
scrollWidget->setLayout(scrollLayout);
auto* scrollArea = new QScrollArea();
scrollArea->setContentsMargins(0, 0, 0, 0);
scrollArea->setFrameStyle(QFrame::NoFrame);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setWidget(scrollWidget);
scrollArea->setWidgetResizable(true);
scrollArea->setBackgroundRole(QPalette::NoRole);
mainLayout->addWidget(scrollArea);
// ***** Upper Horizontal Layout *****
auto* upperHorizontalLayout = new QHBoxLayout();
upperHorizontalLayout->setContentsMargins(0, 0, 0, 0);
upperHorizontalLayout->setSpacing(0);
// ***** Left Vertical Layout *****
auto* leftVLayout = new QVBoxLayout();
leftVLayout->setContentsMargins(7, 2, 7, 7);
leftVLayout->setSpacing(0);
// ***** Weather File GridLayout *****
auto* weatherFileGridLayout = new QGridLayout();
weatherFileGridLayout->setContentsMargins(7, 3, 7, 7);
weatherFileGridLayout->setSpacing(7);
// ***** Measure Tags GridLayout *****
auto* measureTagsGridLayout = new QGridLayout();
measureTagsGridLayout->setContentsMargins(7, 7, 7, 7);
measureTagsGridLayout->setSpacing(7);
// ***** Schedules Layout *****
auto* schedulesLayout = new QVBoxLayout();
schedulesLayout->setContentsMargins(0, 0, 0, 0);
schedulesLayout->setSpacing(0);
// ***** Vertical Line *****
auto* vLine = new QFrame();
vLine->setFrameShape(QFrame::VLine);
vLine->setFrameShadow(QFrame::Sunken);
// ***** Small Horizontal Line *****
auto* smallHLine = new QFrame();
smallHLine->setFrameShape(QFrame::HLine);
smallHLine->setFrameShadow(QFrame::Sunken);
// ***** Main Horizontal Line *****
auto* mainHLine = new QFrame();
mainHLine->setFrameShape(QFrame::HLine);
mainHLine->setFrameShadow(QFrame::Sunken);
// ***** Weather File *****
auto* label = new QLabel(tr("Weather File"));
label->setObjectName("H2");
m_weatherFileBtn = new QPushButton(this);
m_weatherFileBtn->setFlat(true);
m_weatherFileBtn->setObjectName("StandardGrayButton");
connect(m_weatherFileBtn, &QPushButton::clicked, this, &LocationView::onWeatherFileBtnClicked);
auto* hLayout = new QHBoxLayout();
hLayout->setContentsMargins(0, 0, 0, 0);
hLayout->setSpacing(7);
hLayout->addWidget(label, 0, Qt::AlignLeft);
hLayout->addWidget(m_weatherFileBtn, 0, Qt::AlignLeft);
hLayout->addStretch();
leftVLayout->addLayout(hLayout);
// ***** Site Info *****
int i = 0;
label = new QLabel(tr("Name: "));
m_siteName = new QLineEdit();
connect(m_siteName, &QLineEdit::textEdited, this, &LocationView::onSiteNameChanged);
hLayout = new QHBoxLayout();
hLayout->setContentsMargins(0, 0, 0, 0);
hLayout->setSpacing(7);
hLayout->addWidget(label, 0, Qt::AlignLeft);
hLayout->addWidget(m_siteName, 0, Qt::AlignLeft);
hLayout->addStretch();
weatherFileGridLayout->addLayout(hLayout, i++, 0);
m_latitudeLbl = new QLabel(tr("Latitude: "));
weatherFileGridLayout->addWidget(m_latitudeLbl, i++, 0);
m_longitudeLbl = new QLabel(tr("Longitude: "));
weatherFileGridLayout->addWidget(m_longitudeLbl, i++, 0);
m_elevationLbl = new QLabel(tr("Elevation: "));
weatherFileGridLayout->addWidget(m_elevationLbl, i++, 0);
m_timeZoneLbl = new QLabel(tr("Time Zone: "));
weatherFileGridLayout->addWidget(m_timeZoneLbl, i++, 0);
// ***** Weather File Download Location *****
label = new QLabel(tr("Download weather files at <a href=\"http://www.energyplus.net/weather\">www.energyplus.net/weather</a>"));
label->setOpenExternalLinks(true);
weatherFileGridLayout->addWidget(label, i++, 0);
// ***** Add Weather File GridLayout *****
weatherFileGridLayout->setColumnStretch(i, 10);
leftVLayout->addLayout(weatherFileGridLayout);
// ***** Climate Zones *****
label = new QLabel(tr("Measure Tags (Optional):"));
label->setObjectName("H2");
leftVLayout->addWidget(label);
label = new QLabel(tr("ASHRAE Climate Zone"));
label->setObjectName("StandardsInfo");
m_ashraeClimateZone = new QComboBox();
m_ashraeClimateZone->setFixedWidth(200);
i = 0;
measureTagsGridLayout->addWidget(label, i, 0);
measureTagsGridLayout->addWidget(m_ashraeClimateZone, i++, 1);
m_ashraeClimateZone->addItem("");
std::vector<std::string> ashraeClimateZoneValues =
model::ClimateZones::validClimateZoneValues(model::ClimateZones::ashraeInstitutionName(), model::ClimateZones::ashraeDefaultYear());
for (const std::string& climateZone : ashraeClimateZoneValues) {
m_ashraeClimateZone->addItem(toQString(climateZone));
}
model::ClimateZone ashraeClimateZone =
climateZones.getClimateZone(model::ClimateZones::ashraeInstitutionName(), model::ClimateZones::ashraeDefaultYear());
if (ashraeClimateZone.empty()) {
ashraeClimateZone = climateZones.appendClimateZone(model::ClimateZones::ashraeInstitutionName(), model::ClimateZones::ashraeDefaultYear(), "");
}
//ashraeClimateZone.setType(model::ClimateZones::ashraeInstitutionName(), model::ClimateZones::ashraeDocumentName(), model::ClimateZones::ashraeDefaultYear());
std::string ashraeClimateZoneValue = ashraeClimateZone.value();
auto idx = m_ashraeClimateZone->findText(toQString(ashraeClimateZoneValue));
OS_ASSERT(idx != -1);
m_ashraeClimateZone->setCurrentIndex(idx);
connect(m_ashraeClimateZone, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentTextChanged), this,
&LocationView::onASHRAEClimateZoneChanged);
label = new QLabel(tr("CEC Climate Zone"));
label->setObjectName("StandardsInfo");
m_cecClimateZone = new QComboBox();
m_cecClimateZone->setFixedWidth(200);
measureTagsGridLayout->addWidget(label, i, 0);
measureTagsGridLayout->addWidget(m_cecClimateZone, i++, 1);
m_cecClimateZone->addItem("");
std::vector<std::string> cecClimateZoneValues =
model::ClimateZones::validClimateZoneValues(model::ClimateZones::cecInstitutionName(), model::ClimateZones::cecDefaultYear());
for (const std::string& climateZone : cecClimateZoneValues) {
m_cecClimateZone->addItem(toQString(climateZone));
}
model::ClimateZone cecClimateZone = climateZones.getClimateZone(model::ClimateZones::cecInstitutionName(), model::ClimateZones::cecDefaultYear());
if (cecClimateZone.empty()) {
cecClimateZone = climateZones.appendClimateZone(model::ClimateZones::cecInstitutionName(), model::ClimateZones::cecDefaultYear(), "");
}
//cecClimateZone.setType(model::ClimateZones::cecInstitutionName(), model::ClimateZones::cecDocumentName(), model::ClimateZones::cecDefaultYear());
std::string cecClimateZoneValue = cecClimateZone.value();
idx = m_cecClimateZone->findText(toQString(cecClimateZoneValue));
OS_ASSERT(idx != -1);
m_cecClimateZone->setCurrentIndex(idx);
connect(m_cecClimateZone, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentTextChanged), this,
&LocationView::onCECClimateZoneChanged);
// ***** Measure Tags GridLayout *****
measureTagsGridLayout->setColumnStretch(i, 10);
leftVLayout->addLayout(measureTagsGridLayout);
// ***** Schedules *****
m_yearSettingsWidget = new YearSettingsWidget(m_model);
schedulesLayout->addWidget(m_yearSettingsWidget);
connect(m_yearSettingsWidget, &YearSettingsWidget::calendarYearSelected, this, &LocationView::setCalendarYear);
connect(m_yearSettingsWidget, &YearSettingsWidget::firstDayofYearSelected, this, &LocationView::setFirstDayofYear);
connect(m_yearSettingsWidget, &YearSettingsWidget::daylightSavingTimeClicked, this, &LocationView::setDaylightSavingsTime);
connect(m_yearSettingsWidget, &YearSettingsWidget::dstStartDayOfWeekAndMonthChanged, this, &LocationView::setDstStartDayOfWeekAndMonth);
connect(m_yearSettingsWidget, &YearSettingsWidget::dstStartDateChanged, this, &LocationView::setDstStartDate);
connect(m_yearSettingsWidget, &YearSettingsWidget::dstEndDayOfWeekAndMonthChanged, this, &LocationView::setDstEndDayOfWeekAndMonth);
connect(m_yearSettingsWidget, &YearSettingsWidget::dstEndDateChanged, this, &LocationView::setDstEndDate);
// ***** Add Vertical Layout *****
upperHorizontalLayout->addLayout(leftVLayout);
// ***** Add Vertical Line *****
upperHorizontalLayout->addWidget(vLine);
// ***** Add Schedules Layout *****
upperHorizontalLayout->addLayout(schedulesLayout);
// ***** Add Upper Horizontal Layout *****
scrollLayout->addLayout(upperHorizontalLayout);
// ***** Add Main Horizontal Line *****
scrollLayout->addWidget(mainHLine);
// ***** Design Days *****
label = new QLabel(tr("Design Days"));
label->setObjectName("H2");
auto* btn = new QPushButton(tr("Import From DDY"), this);
btn->setFlat(true);
btn->setObjectName("StandardGrayButton");
connect(btn, &QPushButton::clicked, this, &LocationView::onDesignDayBtnClicked);
hLayout = new QHBoxLayout();
hLayout->setContentsMargins(7, 7, 0, 7);
hLayout->setSpacing(7);
hLayout->addWidget(label, 0, Qt::AlignLeft);
hLayout->addWidget(btn, 0, Qt::AlignLeft);
hLayout->addStretch();
scrollLayout->addLayout(hLayout);
m_designDaysGridView = new DesignDayGridView(m_isIP, model, this->parentWidget());
connect(this, &LocationView::toggleUnitsClicked, this, &LocationView::toggleUnits);
connect(this, &LocationView::toggleUnitsClicked, m_designDaysGridView, &DesignDayGridView::toggleUnitsClicked);
scrollLayout->addWidget(m_designDaysGridView, 1, Qt::AlignTop);
// ***** Item Selector Buttons *****
m_itemSelectorButtons = new OSItemSelectorButtons();
m_itemSelectorButtons->hideDropZone();
mainLayout->addWidget(m_itemSelectorButtons, 0, Qt::AlignBottom);
connect(m_itemSelectorButtons, &OSItemSelectorButtons::addClicked, m_designDaysGridView, &DesignDayGridView::onAddClicked);
connect(m_itemSelectorButtons, &OSItemSelectorButtons::copyClicked, m_designDaysGridView, &DesignDayGridView::onCopyClicked);
connect(m_itemSelectorButtons, &OSItemSelectorButtons::removeClicked, m_designDaysGridView, &DesignDayGridView::onRemoveClicked);
connect(m_itemSelectorButtons, &OSItemSelectorButtons::purgeClicked, m_designDaysGridView, &DesignDayGridView::onPurgeClicked);
update();
onSelectItem();
}
LocationView::~LocationView() {
saveQSettings();
}
bool LocationView::calendarYearChecked() {
if (m_yearSettingsWidget != nullptr) {
return m_yearSettingsWidget->calendarYearChecked();
}
return false;
}
std::set<model::ModelObject> LocationView::selectedObjects() const {
return m_designDaysGridView->selectedObjects();
}
void LocationView::onSelectModelObject(const openstudio::model::ModelObject& modelObject) {}
void LocationView::onUpdate() {}
void LocationView::refresh() {}
void LocationView::toggleUnits(bool isIP) {
m_isIP = isIP;
}
void LocationView::onSiteNameChanged(const QString& text) {
auto temp = m_site->setName(text.toStdString());
if (!temp) {
m_siteName->setText("");
} else if (QString(temp.get().c_str()) != text) {
m_siteName->setText(temp.get().c_str());
}
}
void LocationView::loadQSettings() {
QString organizationName = QCoreApplication::organizationName();
QString applicationName = QCoreApplication::applicationName();
QSettings settings(organizationName, applicationName);
m_lastEpwPathOpened = settings.value("m_lastEpwPathOpened").toString();
m_lastDdyPathOpened = settings.value("m_lastDdyPathOpened").toString();
}
void LocationView::saveQSettings() const {
QString organizationName = QCoreApplication::organizationName();
QString applicationName = QCoreApplication::applicationName();
QSettings settings(organizationName, applicationName);
settings.setValue("m_lastEpwPathOpened", m_lastEpwPathOpened);
settings.setValue("m_lastDdyPathOpened", m_lastDdyPathOpened);
}
void LocationView::update() {
boost::optional<model::WeatherFile> weatherFile = m_model.getOptionalUniqueModelObject<model::WeatherFile>();
if (weatherFile) {
auto fileExists = QFile(m_lastEpwPathOpened).exists();
if (!fileExists) {
boost::optional<openstudio::path> epwPath = weatherFile->path();
if (epwPath) {
// If there is a path, and a file at that path, our job is done
fileExists = QFile(epwPath->string().c_str()).exists();
if (!fileExists) {
// Construct the absolute path as dictated by the osm location, and check for the file
openstudio::OSAppBase* app = OSAppBase::instance();
if ((app != nullptr) && app->currentDocument()) {
QString savePath = app->currentDocument()->savePath();
QString filePath;
if (savePath.contains(".osm")) {
savePath.chop(4);
filePath = savePath + "/files/";
filePath += (epwPath.get().filename()).string().c_str();
}
fileExists = QFile(filePath).exists();
}
if (!fileExists) {
// check the temp dir, this is required when launching the os app with a filename since app->currentDocument() is not set when constructing this tab
QString filePath = m_modelTempDir + "/resources/files/";
filePath += (epwPath.get().filename()).string().c_str();
fileExists = QFile(filePath).exists();
}
}
}
}
if (fileExists) {
m_weatherFileBtn->setText(tr("Change Weather File"));
setSiteInfo();
} else {
m_weatherFileBtn->setText(tr("Set Weather File"));
clearSiteInfo();
}
} else {
m_weatherFileBtn->setText(tr("Set Weather File"));
clearSiteInfo();
}
}
void LocationView::setSiteInfo() {
QString info = tr(LATITUDE);
QString temp;
if (m_site->name() && !m_site->name()->empty()) {
m_siteName->setText(m_site->name().get().c_str());
} else {
m_siteName->setText("");
}
temp.setNum(m_site->latitude());
info += temp;
m_latitudeLbl->setText(info);
info = tr(LONGITUDE);
temp.setNum(m_site->longitude());
info += temp;
m_longitudeLbl->setText(info);
info = tr(ELEVATION);
temp.setNum(m_site->elevation());
info += temp;
m_elevationLbl->setText(info);
info = tr(TIME_ZONE);
temp.setNum(m_site->timeZone());
info += temp;
m_timeZoneLbl->setText(info);
}
void LocationView::clearSiteInfo() {
m_siteName->setText("");
m_latitudeLbl->setText(tr(LATITUDE));
m_longitudeLbl->setText(tr(LONGITUDE));
m_elevationLbl->setText(tr(ELEVATION));
m_timeZoneLbl->setText(tr(TIME_ZONE));
}
// ***** SLOTS *****
void LocationView::onWeatherFileBtnClicked() {
QString fileTypes(tr("EPW Files (*.epw);; All Files (*.*)"));
QString lastPath = m_lastEpwPathOpened;
if (lastPath.isEmpty() && m_lastDdyPathOpened.isEmpty()) {
//openstudio::runmanager::ConfigOptions co(true);
//lastPath = toQString(co.getDefaultEPWLocation().native());
} else if (lastPath.isEmpty()) {
QString path = m_lastDdyPathOpened;
lastPath = path.replace(".ddy", ".epw");
}
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Weather File"), lastPath, fileTypes);
if (!fileName.isEmpty()) {
openstudio::path epwPath = toPath(fileName);
openstudio::path newPath = toPath(m_modelTempDir) / toPath("resources/files") / epwPath.filename();
openstudio::path previousEPWPath;
StringStreamLogSink ss;
ss.setChannelRegex(boost::regex(".*EpwFile.*"));
ss.setLogLevel(Error);
try {
boost::optional<openstudio::model::WeatherFile> weatherFile = m_model.getOptionalUniqueModelObject<model::WeatherFile>();
if (weatherFile) {
boost::optional<openstudio::path> temp = weatherFile->path();
if (temp) {
openstudio::path previousEPWName = temp->filename();
//std::string ex = toString(previousEPWName.extension());
if (!previousEPWName.empty() && previousEPWName.extension() == toPath(".epw")) {
previousEPWPath = toPath(m_modelTempDir) / toPath("resources/files") / previousEPWName;
}
}
}
// duplicate code in OSDocument::fixWeatherFilePath
openstudio::filesystem::copy_file(epwPath, newPath, openstudio::filesystem::copy_option::overwrite_if_exists);
// this can throw
EpwFile epwFile(newPath);
double totalDays = (epwFile.endDate() - epwFile.startDate()).totalDays() + 1;
if (totalDays > 366) {
LOG_FREE(Error, "openstudio.EpwFile", "Cannot accept weather file with more than 366 days of data");
throw openstudio::Exception("Cannot accept weather file with more than 366 days of data");
}
weatherFile = openstudio::model::WeatherFile::setWeatherFile(m_model, epwFile);
OS_ASSERT(weatherFile);
weatherFile->makeUrlRelative(toPath(m_modelTempDir) / toPath("resources/files"));
m_model.workflowJSON().setWeatherFile(newPath.filename());
if (!previousEPWPath.empty()) {
if (previousEPWPath.filename() != newPath.filename()) {
if (openstudio::filesystem::exists(previousEPWPath)) {
openstudio::filesystem::remove_all(previousEPWPath);
}
}
}
// set run period based on weather file
openstudio::model::RunPeriod runPeriod = m_model.getUniqueModelObject<openstudio::model::RunPeriod>();
runPeriod.setBeginMonth(epwFile.startDate().monthOfYear().value());
runPeriod.setBeginDayOfMonth(epwFile.startDate().dayOfMonth());
runPeriod.setEndMonth(epwFile.endDate().monthOfYear().value());
runPeriod.setEndDayOfMonth(epwFile.endDate().dayOfMonth());
// set the calendar year or start day of week
openstudio::model::YearDescription yearDescription = m_model.getUniqueModelObject<openstudio::model::YearDescription>();
boost::optional<int> startDateActualYear = epwFile.startDateActualYear();
if (startDateActualYear) {
yearDescription.resetDayofWeekforStartDay();
yearDescription.setCalendarYear(*startDateActualYear);
} else {
yearDescription.resetCalendarYear();
yearDescription.setDayofWeekforStartDay(epwFile.startDayOfWeek().valueName());
}
// update site info
m_site->setName(weatherFile->city());
m_site->setLatitude(weatherFile->latitude());
m_site->setLongitude(weatherFile->longitude());
m_site->setElevation(weatherFile->elevation());
m_site->setTimeZone(weatherFile->timeZone());
m_lastEpwPathOpened = QFileInfo(fileName).absoluteFilePath();
update();
} catch (...) {
openstudio::filesystem::remove_all(newPath);
QMessageBox box(QMessageBox::Warning, tr("Failed To Set Weather File"), tr("Failed To Set Weather File To ") + fileName, QMessageBox::Ok);
box.setDetailedText(toQString(ss.string()));
box.exec();
boost::optional<model::WeatherFile> weatherFile = m_model.weatherFile();
if (weatherFile) {
boost::optional<openstudio::path> weatherFilePath = weatherFile->path();
if (weatherFilePath) {
if (!previousEPWPath.empty()) {
if ((previousEPWPath.filename() != weatherFilePath->filename()) || (!openstudio::filesystem::exists(previousEPWPath))) {
weatherFile->remove();
}
}
}
}
update();
}
}
}
void LocationView::onDesignDayBtnClicked() {
QString fileTypes("Files (*.ddy)");
QString lastPath = m_lastDdyPathOpened;
if (lastPath.isEmpty() && m_lastEpwPathOpened.isEmpty()) {
//openstudio::runmanager::ConfigOptions co(true);
//lastPath = toQString(co.getDefaultEPWLocation().native());
} else if (lastPath.isEmpty()) {
QString path = m_lastEpwPathOpened;
lastPath = path.replace(".epw", ".ddy");
}
QString fileName = QFileDialog::getOpenFileName(this, tr("Open DDY File"), lastPath, fileTypes);
if (!fileName.isEmpty()) {
boost::optional<IdfFile> ddyIdfFile = openstudio::IdfFile::load(toPath(fileName));
if (ddyIdfFile) {
openstudio::Workspace ddyWorkspace(StrictnessLevel::None, IddFileType::EnergyPlus);
for (const IdfObject& idfObject : ddyIdfFile->objects()) {
IddObjectType iddObjectType = idfObject.iddObject().type();
if ((iddObjectType == IddObjectType::SizingPeriod_DesignDay) || (iddObjectType == IddObjectType::SizingPeriod_WeatherFileDays)
|| (iddObjectType == IddObjectType::SizingPeriod_WeatherFileConditionType)) {
ddyWorkspace.addObject(idfObject);
}
}
energyplus::ReverseTranslator reverseTranslator;
model::Model ddyModel = reverseTranslator.translateWorkspace(ddyWorkspace);
// Use a heuristic based on the ddy files provided by EnergyPlus
// Filter out the days that are not helpful.
if (!ddyModel.objects().empty()) {
// Containers to hold 99%, 99.6%, 2%, 1%, and 0.4% design points
std::vector<model::DesignDay> days99;
std::vector<model::DesignDay> days99_6;
std::vector<model::DesignDay> days2;
std::vector<model::DesignDay> days1;
std::vector<model::DesignDay> days0_4;
bool unknownDay = false;
for (const model::DesignDay& designDay : ddyModel.getConcreteModelObjects<model::DesignDay>()) {
boost::optional<std::string> name;
name = designDay.name();
if (name) {
QString qname = QString::fromStdString(name.get());
if (qname.contains("99%")) {
days99.push_back(designDay);
} else if (qname.contains("99.6%")) {
days99_6.push_back(designDay);
} else if (qname.contains("2%")) {
days2.push_back(designDay);
} else if (qname.contains("1%")) {
days1.push_back(designDay);
} else if (qname.contains(".4%")) {
days0_4.push_back(designDay);
} else {
unknownDay = true;
}
}
}
// Pick only the most stringent design points
if (!unknownDay) {
if (!days99_6.empty()) {
for (model::DesignDay designDay : days99) {
designDay.remove();
}
}
if (!days0_4.empty()) {
for (model::DesignDay designDay : days1) {
designDay.remove();
}
for (model::DesignDay designDay : days2) {
designDay.remove();
}
} else if (!days1.empty()) {
for (model::DesignDay designDay : days2) {
designDay.remove();
}
}
}
// Evan note: do not remove existing design days
//for (model::SizingPeriod sizingPeriod : m_model.getModelObjects<model::SizingPeriod>()){
// sizingPeriod.remove();
//}
m_model.insertObjects(ddyModel.objects());
m_lastDdyPathOpened = QFileInfo(fileName).absoluteFilePath();
}
}
QTimer::singleShot(0, this, &LocationView::checkNumDesignDays);
}
}
void LocationView::checkNumDesignDays() {
if (m_model.getModelObjects<model::SizingPeriod>().empty()) {
QMessageBox box(QMessageBox::Warning, tr("No Design Days in DDY File"),
tr("This DDY file does not contain any valid design days. Check the DDY file itself for errors or omissions."), QMessageBox::Ok);
box.exec();
}
}
void LocationView::onASHRAEClimateZoneChanged(const QString& climateZone) {
auto climateZones = m_model.getUniqueModelObject<model::ClimateZones>();
model::ClimateZone ashraeClimateZone =
climateZones.getClimateZone(model::ClimateZones::ashraeInstitutionName(), model::ClimateZones::ashraeDefaultYear());
ashraeClimateZone.setValue(toString(climateZone));
}
void LocationView::onCECClimateZoneChanged(const QString& climateZone) {
auto climateZones = m_model.getUniqueModelObject<model::ClimateZones>();
model::ClimateZone cecClimateZone = climateZones.getClimateZone(model::ClimateZones::cecInstitutionName(), model::ClimateZones::cecDefaultYear());
cecClimateZone.setValue(toString(climateZone));
}
void LocationView::setCalendarYear(int year) {
m_yearDescription->setCalendarYear(year);
emit calendarYearSelectionChanged();
}
void LocationView::setFirstDayofYear(const QString& firstDayofYear) {
m_yearDescription->resetCalendarYear();
auto idx = YearSettingsWidget::validDayofWeekforStartDay().indexOf(firstDayofYear);
m_yearDescription->setDayofWeekforStartDay(model::YearDescription::validDayofWeekforStartDayValues()[idx]);
emit calendarYearSelectionChanged();
}
void LocationView::setDaylightSavingsTime(bool enabled) {
if (enabled) {
m_model.getUniqueModelObject<model::RunPeriodControlDaylightSavingTime>();
} else {
if (boost::optional<model::RunPeriodControlDaylightSavingTime> dst =
m_model.getOptionalUniqueModelObject<model::RunPeriodControlDaylightSavingTime>()) {
dst->remove();
}
}
}
void LocationView::setDstStartDayOfWeekAndMonth(int newWeek, int newDay, int newMonth) {
auto dst = m_model.getUniqueModelObject<model::RunPeriodControlDaylightSavingTime>();
dst.setStartDate(NthDayOfWeekInMonth(newWeek), DayOfWeek(newDay), MonthOfYear(newMonth));
}
void LocationView::setDstStartDate(const QDate& newdate) {
auto dst = m_model.getUniqueModelObject<model::RunPeriodControlDaylightSavingTime>();
dst.setStartDate(monthOfYear(newdate.month()), newdate.day());
}
void LocationView::setDstEndDayOfWeekAndMonth(int newWeek, int newDay, int newMonth) {
auto dst = m_model.getUniqueModelObject<model::RunPeriodControlDaylightSavingTime>();
dst.setEndDate(NthDayOfWeekInMonth(newWeek), DayOfWeek(newDay), MonthOfYear(newMonth));
}
void LocationView::setDstEndDate(const QDate& newdate) {
auto dst = m_model.getUniqueModelObject<model::RunPeriodControlDaylightSavingTime>();
dst.setEndDate(monthOfYear(newdate.month()), newdate.day());
}
void LocationView::onSelectItem() {
m_itemSelectorButtons->enableCopyButton();
m_itemSelectorButtons->enableRemoveButton();
m_itemSelectorButtons->enablePurgeButton();
}
void LocationView::onClearSelection() {
m_itemSelectorButtons->disableCopyButton();
m_itemSelectorButtons->disableRemoveButton();
m_itemSelectorButtons->disablePurgeButton();
}
} // namespace openstudio