-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathWorkflowController.cpp
589 lines (459 loc) · 20 KB
/
WorkflowController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) 2020-2020, 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 "WorkflowController.hpp"
#include "WorkflowView.hpp"
#include "MeasureDragData.hpp"
#include "MeasureManager.hpp"
#include "EditController.hpp"
#include "BaseApp.hpp"
#include "../openstudio_lib/OSAppBase.hpp"
#include "../openstudio_lib/OSDocument.hpp"
#include "LocalLibraryController.hpp"
#include "WorkflowTools.hpp"
#include "../model_editor/OSProgressBar.hpp"
#include "../model_editor/Utilities.hpp"
#include <openstudio/energyplus/ForwardTranslator.hpp>
#include <openstudio/utilities/core/Assert.hpp>
#include <openstudio/utilities/core/Compare.hpp>
#include <openstudio/utilities/core/Containers.hpp>
#include <openstudio/utilities/core/RubyException.hpp>
#include <openstudio/utilities/core/PathHelpers.hpp>
#include <openstudio/utilities/bcl/BCLMeasure.hpp>
#include <openstudio/utilities/filetypes/WorkflowStep_Impl.hpp>
#include <QByteArray>
#include <QDialog>
#include <QDropEvent>
#include <QFileDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QMimeData>
#include <QPushButton>
#include <QRadioButton>
namespace openstudio {
namespace measuretab {
WorkflowController::WorkflowController(BaseApp* t_baseApp) : OSListController() {
QSharedPointer<WorkflowSectionItem> workflowSectionItem;
workflowSectionItem = QSharedPointer<WorkflowSectionItem>(new WorkflowSectionItem(MeasureType::ModelMeasure, "OpenStudio Measures", t_baseApp));
addItem(workflowSectionItem);
workflowSectionItem =
QSharedPointer<WorkflowSectionItem>(new WorkflowSectionItem(MeasureType::EnergyPlusMeasure, "EnergyPlus Measures", t_baseApp));
addItem(workflowSectionItem);
workflowSectionItem = QSharedPointer<WorkflowSectionItem>(new WorkflowSectionItem(MeasureType::ReportingMeasure, "Reporting Measures", t_baseApp));
addItem(workflowSectionItem);
}
void WorkflowController::addItem(QSharedPointer<OSListItem> item) {
if (QSharedPointer<WorkflowSectionItem> workflowSectionItem = item.dynamicCast<WorkflowSectionItem>()) {
m_workflowSectionItems.push_back(workflowSectionItem);
workflowSectionItem->setController(this);
workflowSectionItem->workflowStepController()->setSelectionController(selectionController());
}
}
QSharedPointer<OSListItem> WorkflowController::itemAt(int i) {
if (i >= 0 && i < count()) {
return m_workflowSectionItems[i];
}
return QSharedPointer<OSListItem>();
}
int WorkflowController::count() {
return m_workflowSectionItems.size();
}
WorkflowSectionItem::WorkflowSectionItem(const MeasureType& measureType, const QString& label, BaseApp* t_baseApp)
: OSListItem(),
m_label(label),
m_measureType(measureType),
m_workflowStepController(QSharedPointer<MeasureStepController>::create(measureType, t_baseApp)) {}
QString WorkflowSectionItem::label() const {
return m_label;
}
QSharedPointer<WorkflowStepController> WorkflowSectionItem::workflowStepController() const {
return m_workflowStepController;
}
WorkflowSectionItemDelegate::WorkflowSectionItemDelegate() {}
QWidget* WorkflowSectionItemDelegate::view(QSharedPointer<OSListItem> dataSource) {
if (QSharedPointer<WorkflowSectionItem> workflowSectionItem = dataSource.objectCast<WorkflowSectionItem>()) {
QSharedPointer<WorkflowStepController> workflowStepController = workflowSectionItem->workflowStepController();
if (QSharedPointer<MeasureStepController> measureStepController = qSharedPointerCast<MeasureStepController>(workflowStepController)) {
QSharedPointer<MeasureStepItemDelegate> measureStepItemDelegate = QSharedPointer<MeasureStepItemDelegate>(new MeasureStepItemDelegate());
MeasureType measureType = measureStepController->measureType();
QString acceptedMimeType = MeasureDragData::mimeType(measureType);
auto workflowSectionView = new WorkflowSectionView(measureType);
workflowSectionView->content->newMeasureDropZone->setAcceptedMimeType(acceptedMimeType);
connect(workflowSectionView->content->newMeasureDropZone, &NewMeasureDropZone::dataDropped, measureStepController.data(),
&MeasureStepController::addItemForDroppedMeasure);
workflowSectionView->content->workflowStepsView->setListController(measureStepController);
workflowSectionView->content->workflowStepsView->setDelegate(measureStepItemDelegate);
workflowSectionView->header->label->setText(workflowSectionItem->label());
return workflowSectionView;
}
}
return new QWidget();
}
WorkflowStepController::WorkflowStepController(openstudio::BaseApp* t_app) : OSListController() {}
MeasureStepController::MeasureStepController(MeasureType measureType, openstudio::BaseApp* t_app)
: WorkflowStepController(t_app), m_measureType(measureType), m_app(t_app) {}
MeasureType MeasureStepController::measureType() const {
return m_measureType;
}
std::vector<MeasureStep> MeasureStepController::measureSteps() const {
WorkflowJSON workflowJSON = m_app->currentModel()->workflowJSON();
std::vector<MeasureStep> result = workflowJSON.getMeasureSteps(m_measureType);
return result;
}
QSharedPointer<OSListItem> MeasureStepController::itemAt(int i) {
std::vector<MeasureStep> steps = this->measureSteps();
if (i >= 0 && i < (int)steps.size()) {
MeasureStep step = steps[i];
QSharedPointer<MeasureStepItem> item = QSharedPointer<MeasureStepItem>(new MeasureStepItem(m_measureType, step, m_app));
item->setController(this);
return item;
}
return QSharedPointer<MeasureStepItem>();
}
int MeasureStepController::count() {
return measureSteps().size();
}
void MeasureStepController::removeItemForStep(MeasureStep step) {
std::vector<MeasureStep> oldMeasureSteps = measureSteps();
bool didRemove = false;
std::vector<MeasureStep> newMeasureSteps;
newMeasureSteps.reserve(oldMeasureSteps.size());
for (const auto& oldMeasureStep : oldMeasureSteps) {
if (oldMeasureStep == step) {
didRemove = true;
} else {
newMeasureSteps.push_back(oldMeasureStep);
}
}
if (didRemove) {
// DLM: TODO actually remove the directory here, make sure it not being used by any other steps first
// maybe want a purge unused measures in WorkflowJSON?
// set the new steps
m_app->currentModel()->workflowJSON().setMeasureSteps(m_measureType, newMeasureSteps);
emit modelReset();
}
}
void MeasureStepController::addItemForDroppedMeasure(QDropEvent* event) {
// accept the event to make the icon refresh
event->accept();
const QMimeData* mimeData = event->mimeData();
QByteArray byteArray;
byteArray = mimeData->data(MeasureDragData::mimeType(m_measureType));
MeasureDragData measureDragData(byteArray);
UUID id = measureDragData.id();
std::shared_ptr<OSDocument> document = nullptr;
if (dynamic_cast<OSAppBase*>(m_app)) {
document = dynamic_cast<OSAppBase*>(m_app)->currentDocument();
document->disable();
}
boost::optional<BCLMeasure> projectMeasure;
try {
// Get the measure, will throw if error occurs
projectMeasure = m_app->measureManager().insertReplaceMeasure(id);
} catch (const std::exception& e) {
QString errorMessage("Failed to add measure: \n\n");
errorMessage += QString::fromStdString(e.what());
QMessageBox::information(m_app->mainWidget(), QString("Failed to add measure"), errorMessage);
if (document) {
document->enable();
}
return;
}
OS_ASSERT(projectMeasure);
if (projectMeasure->measureType() != m_measureType) {
QString errorMessage("Failed to add measure at this workflow location.");
QMessageBox::information(m_app->mainWidget(), QString("Failed to add measure"), errorMessage);
if (document) {
document->enable();
}
return;
}
// Since we set the measure_paths, we only neeed to reference the name of the directory (=last level directory name)
// eg: /path/to/measure_folder => measure_folder
MeasureStep measureStep(toString(getLastLevelDirectoryName(projectMeasure->directory())));
try {
std::vector<measure::OSArgument> arguments = m_app->measureManager().getArguments(*projectMeasure);
} catch (const RubyException& e) {
QString errorMessage("Failed to compute arguments for measure: \n\n");
errorMessage += QString::fromStdString(e.what());
QMessageBox::information(m_app->mainWidget(), QString("Failed to add measure"), errorMessage);
if (document) {
document->enable();
}
return;
}
// the new measure
std::string name = m_app->measureManager().suggestMeasureName(*projectMeasure);
// DLM: moved to WorkflowStepResult
//measureStep.setMeasureId(projectMeasure->uid());
//measureStep.setVersionId(projectMeasure->versionId());
//std::vector<std::string> tags = projectMeasure->tags();
//if (!tags.empty()){
// measureStep.setTaxonomy(tags[0]);
//}
measureStep.setName(name);
//measureStep.setDisplayName(name); // DLM: TODO
measureStep.setDescription(projectMeasure->description());
measureStep.setModelerDescription(projectMeasure->modelerDescription());
WorkflowJSON workflowJSON = m_app->currentModel()->workflowJSON();
std::vector<MeasureStep> measureSteps = workflowJSON.getMeasureSteps(m_measureType);
measureSteps.push_back(measureStep);
bool test = workflowJSON.setMeasureSteps(m_measureType, measureSteps);
OS_ASSERT(test);
//workflowJSON.save();
if (document) {
document->enable();
}
emit modelReset();
}
void MeasureStepController::moveUp(MeasureStep step) {
std::vector<MeasureStep> oldMeasureSteps = measureSteps();
bool found = false;
size_t i;
size_t n = oldMeasureSteps.size();
for (i = 0; i < n; ++i) {
if (oldMeasureSteps[i] == step) {
found = true;
break;
}
}
if (found && i > 0) {
// swap i with i-1
MeasureStep temp = oldMeasureSteps[i - 1];
oldMeasureSteps[i - 1] = oldMeasureSteps[i];
oldMeasureSteps[i] = temp;
m_app->currentModel()->workflowJSON().setMeasureSteps(m_measureType, oldMeasureSteps);
emit itemChanged(i - 1);
emit itemChanged(i);
}
}
void MeasureStepController::moveDown(MeasureStep step) {
std::vector<MeasureStep> oldMeasureSteps = measureSteps();
bool found = false;
size_t i;
size_t n = oldMeasureSteps.size();
for (i = 0; i < n; ++i) {
if (oldMeasureSteps[i] == step) {
found = true;
break;
}
}
if (found && i < (n - 1)) {
// swap i with i+1
MeasureStep temp = oldMeasureSteps[i + 1];
oldMeasureSteps[i + 1] = oldMeasureSteps[i];
oldMeasureSteps[i] = temp;
m_app->currentModel()->workflowJSON().setMeasureSteps(m_measureType, oldMeasureSteps);
emit itemChanged(i);
emit itemChanged(i + 1);
}
}
MeasureStepItem::MeasureStepItem(MeasureType measureType, MeasureStep step, openstudio::BaseApp* t_app)
: OSListItem(),
m_measureType(measureType),
m_step(step),
m_app(t_app)
{}
QString MeasureStepItem::name() const {
QString result;
if (boost::optional<std::string> name = m_step.name()) {
return result = QString::fromStdString(*name);
}
return result;
}
//QString MeasureStepItem::displayName() const
//{
// // DLM: TODO, add display name
// QString result;
// if (boost::optional<std::string> name = m_step.name()){
// return result = QString::fromStdString(*name);
// }
// return result;
//}
MeasureType MeasureStepItem::measureType() const {
OptionalBCLMeasure bclMeasure = this->bclMeasure();
OS_ASSERT(bclMeasure);
return bclMeasure->measureType();
}
MeasureStep MeasureStepItem::measureStep() const {
return m_step;
}
QString MeasureStepItem::description() const {
QString result;
if (boost::optional<std::string> description = m_step.description()) {
return result = QString::fromStdString(*description);
}
return result;
}
QString MeasureStepItem::modelerDescription() const {
QString result;
if (boost::optional<std::string> modelerDescription = m_step.modelerDescription()) {
return result = QString::fromStdString(*modelerDescription);
}
return result;
}
QString MeasureStepItem::scriptFileName() const {
QString result;
OptionalBCLMeasure bclMeasure = this->bclMeasure();
if (bclMeasure) {
boost::optional<path> primaryRubyScriptPath = bclMeasure->primaryRubyScriptPath();
if (primaryRubyScriptPath) {
result = toQString(*primaryRubyScriptPath);
}
}
return result;
}
OptionalBCLMeasure MeasureStepItem::bclMeasure() const {
return m_app->currentModel()->workflowJSON().getBCLMeasure(m_step);
}
std::vector<measure::OSArgument> MeasureStepItem::arguments() const {
std::vector<measure::OSArgument> result;
// get arguments from the BCL Measure (computed using the current model)
OptionalBCLMeasure bclMeasure = this->bclMeasure();
if (bclMeasure) {
result = m_app->measureManager().getArguments(*bclMeasure);
}
// fill in with any arguments in this WorkflowJSON
for (auto& argument : result) {
boost::optional<Variant> variant = m_step.getArgument(argument.name());
if (variant) {
VariantType variantType = variant->variantType();
if (variantType == VariantType::Boolean) {
argument.setValue(variant->valueAsBoolean());
} else if (variantType == VariantType::Integer) {
argument.setValue(variant->valueAsInteger());
} else if (variantType == VariantType::Double) {
argument.setValue(variant->valueAsDouble());
} else if (variantType == VariantType::String) {
argument.setValue(variant->valueAsString());
}
}
}
return result;
}
bool MeasureStepItem::hasIncompleteArguments() const {
return (incompleteArguments().size() > 0);
}
std::vector<measure::OSArgument> MeasureStepItem::incompleteArguments() const {
std::vector<measure::OSArgument> result;
// find any required arguments without a value
for (const auto& argument : arguments()) {
if (argument.required() && !argument.hasDefaultValue()) {
boost::optional<Variant> variant = m_step.getArgument(argument.name());
if (!variant) {
result.push_back(argument);
}
}
}
return result;
}
void MeasureStepItem::remove() {
// if this step is being edited, clear the edit controller
MeasureStepItem* measureStepItem = m_app->editController()->measureStepItem();
if (measureStepItem) {
if (measureStepItem->measureStep() == m_step) {
m_app->editController()->reset();
}
}
qobject_cast<MeasureStepController*>(controller())->removeItemForStep(m_step);
}
void MeasureStepItem::moveUp() {
qobject_cast<MeasureStepController*>(controller())->moveUp(m_step);
}
void MeasureStepItem::moveDown() {
qobject_cast<MeasureStepController*>(controller())->moveDown(m_step);
}
void MeasureStepItem::setName(const QString& name) {
m_step.setName(name.toStdString());
emit nameChanged(name);
}
//void MeasureStepItem::setDisplayName(const QString & displayName)
//{
// m_step.setName(displayName.toStdString());
//
// emit displayNameChanged(displayName);
//}
void MeasureStepItem::setDescription(const QString& description) {
m_step.setDescription(description.toStdString());
}
void MeasureStepItem::setArgument(const measure::OSArgument& argument) {
if (argument.hasValue()) {
if (argument.type() == measure::OSArgumentType::Boolean) {
m_step.setArgument(argument.name(), argument.valueAsBool());
} else if (argument.type() == measure::OSArgumentType::Double) {
m_step.setArgument(argument.name(), argument.valueAsDouble());
} else if (argument.type() == measure::OSArgumentType::Quantity) {
m_step.setArgument(argument.name(), argument.valueAsDouble());
} else if (argument.type() == measure::OSArgumentType::Integer) {
m_step.setArgument(argument.name(), argument.valueAsInteger());
} else {
m_step.setArgument(argument.name(), argument.valueAsString());
}
} else {
m_step.removeArgument(argument.name());
}
emit argumentsChanged(hasIncompleteArguments());
}
void MeasureStepItem::setSelected(bool isSelected) {
OSListItem::setSelected(isSelected);
// Ugly hack to prevent the controller from doing this for tab 2.
// Please somebody fix, perhaps be using a new signal from OSItemSelectionController.
if (!controller()->selectionController()->allowMultipleSelections()) {
if (isSelected) {
m_app->chooseHorizontalEditTab();
m_app->editController()->setMeasureStepItem(this, m_app);
} else {
m_app->editController()->reset();
}
}
}
MeasureStepItemDelegate::MeasureStepItemDelegate() {}
QWidget* MeasureStepItemDelegate::view(QSharedPointer<OSListItem> dataSource) {
if (QSharedPointer<MeasureStepItem> measureStepItem = dataSource.objectCast<MeasureStepItem>()) {
auto workflowStepView = new WorkflowStepView();
workflowStepView->workflowStepButton->nameLabel->setText(measureStepItem->name());
connect(measureStepItem.data(), &MeasureStepItem::nameChanged, workflowStepView->workflowStepButton->nameLabel, &QLabel::setText);
// Remove
connect(workflowStepView->removeButton, &QPushButton::clicked, measureStepItem.data(), &MeasureStepItem::remove);
// Selection
workflowStepView->workflowStepButton->setHasEmphasis(measureStepItem->isSelected());
connect(workflowStepView->workflowStepButton, &WorkflowStepButton::clicked, measureStepItem.data(), &MeasureStepItem::toggleSelected);
connect(measureStepItem.data(), &MeasureStepItem::selectedChanged, workflowStepView->workflowStepButton, &WorkflowStepButton::setHasEmphasis);
// Warning Icon
workflowStepView->workflowStepButton->cautionLabel->setVisible(measureStepItem->hasIncompleteArguments());
connect(measureStepItem.data(), &MeasureStepItem::argumentsChanged, workflowStepView->workflowStepButton->cautionLabel, &QLabel::setVisible);
// Up and down buttons
connect(workflowStepView->upButton, &QPushButton::clicked, measureStepItem.data(), &MeasureStepItem::moveUp);
connect(workflowStepView->downButton, &QPushButton::clicked, measureStepItem.data(), &MeasureStepItem::moveDown);
return workflowStepView;
}
return new QWidget();
}
} // namespace measuretab
} // namespace openstudio