/
PlotWindowContainer.cpp
630 lines (602 loc) · 24.9 KB
/
PlotWindowContainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "PlotWindowContainer.h"
#include "MainWindow.h"
#include "Options/OptionsDialog.h"
#include "Modeling/ModelWidgetContainer.h"
#include "Modeling/MessagesWidget.h"
#include "Plotting/VariablesWidget.h"
#include "Plotting/DiagramWindow.h"
using namespace OMPlot;
/*!
* \class PlotWindowContainer
* \brief A MDI area for plot windows.
*/
/*!
* \brief PlotWindowContainer::PlotWindowContainer
* \param pParent
*/
PlotWindowContainer::PlotWindowContainer(QWidget *pParent)
: QMdiArea(pParent), mpDiagramWindow(0)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setActivationOrder(QMdiArea::ActivationHistoryOrder);
setDocumentMode(true);
#if QT_VERSION >= 0x040800
setTabsClosable(true);
setTabsMovable(true);
#endif
if (OptionsDialog::instance()->getPlottingPage()->getPlottingViewMode().compare(Helper::subWindow) == 0) {
setViewMode(QMdiArea::SubWindowView);
} else {
setViewMode(QMdiArea::TabbedView);
}
}
/*!
* \brief PlotWindowContainer::getUniqueName
* Returns a unique name for new plot window.
* \param name
* \param number
* \return
*/
QString PlotWindowContainer::getUniqueName(QString name, int number)
{
QString newName;
newName = name + QString::number(number);
foreach (QMdiSubWindow *pWindow, subWindowList()) {
if (pWindow->widget()->windowTitle().compare(newName) == 0) {
newName = getUniqueName(name, ++number);
break;
}
}
return newName;
}
/*!
* \brief PlotWindowContainer::getCurrentWindow
* Returns the current plot window, if the last window is animation, return null
* \return
*/
PlotWindow* PlotWindowContainer::getCurrentWindow()
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
} else {
if (isPlotWindow(subWindowList(QMdiArea::ActivationHistoryOrder).last()->widget())) {
return qobject_cast<PlotWindow*>(subWindowList(QMdiArea::ActivationHistoryOrder).last()->widget());
} else {
return 0;
}
}
}
PlotWindow* PlotWindowContainer::getInteractiveWindow(QString targetWindow)
{
if (subWindowList().size() == 0) {
return 0;
} else {
foreach (QMdiSubWindow *pSubWindow, subWindowList()) {
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
if (pPlotWindow) {
if (pPlotWindow->getInteractiveOwner() == targetWindow) {
return pPlotWindow;
}
}
}
return 0;
}
}
/*!
* \brief PlotWindowContainer::getTopPlotWindow
* Finds the top PlotWindow and returns it. If there is no PlotWindow then return 0.
* \return
*/
PlotWindow* PlotWindowContainer::getTopPlotWindow()
{
QList<QMdiSubWindow*> subWindowsList = subWindowList(QMdiArea::ActivationHistoryOrder);
for (int i = subWindowsList.size() - 1 ; i >= 0 ; i--) {
if (isPlotWindow(subWindowsList.at(i)->widget())) {
return qobject_cast<PlotWindow*>(subWindowsList.at(i)->widget());
}
}
return 0;
}
/*!
* \brief PlotWindowContainer::setTopPlotWindowActive
* Finds the top PlotWindow and sets it as active subwindow.
*/
void PlotWindowContainer::setTopPlotWindowActive()
{
QList<QMdiSubWindow*> subWindowsList = subWindowList(QMdiArea::ActivationHistoryOrder);
for (int i = subWindowsList.size() - 1 ; i >= 0 ; i--) {
if (isPlotWindow(subWindowsList.at(i)->widget())) {
setActiveSubWindow(subWindowsList.at(i));
return;
}
}
}
#if !defined(WITHOUT_OSG)
/*!
* \brief PlotWindowContainer::getCurrentAnimationWindow
* Returns the current animation window, if the last window is plot, return null
* \return
*/
AnimationWindow* PlotWindowContainer::getCurrentAnimationWindow()
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
} else {
if (isAnimationWindow(subWindowList(QMdiArea::ActivationHistoryOrder).last()->widget())) {
return qobject_cast<AnimationWindow*>(subWindowList(QMdiArea::ActivationHistoryOrder).last()->widget());
} else {
return 0;
}
}
}
#endif
/*!
* \brief PlotWindowContainer::getDiagramSubWindowFromMdi
* Returns the diagram sub window, if there is any in the PlotWindowContainer
* \return
*/
QMdiSubWindow* PlotWindowContainer::getDiagramSubWindowFromMdi()
{
if (subWindowList(QMdiArea::ActivationHistoryOrder).size() == 0) {
return 0;
} else {
QList<QMdiSubWindow*> subWindowsList = subWindowList(QMdiArea::ActivationHistoryOrder);
for (int i = subWindowsList.size() - 1 ; i >= 0 ; i--) {
if (isDiagramWindow(subWindowsList.at(i)->widget())) {
return subWindowsList.at(i);
}
}
return 0;
}
}
/*!
* \brief PlotWindowContainer::isPlotWindow
* Returns true if pObject is a PlotWindow.
* \param pObject
* \return
*/
bool PlotWindowContainer::isPlotWindow(QObject *pObject)
{
if (pObject && 0 != pObject->objectName().compare("animationWindow")
&& 0 != pObject->objectName().compare("diagramWindow")) {
return true;
}
return false;
}
/*!
* \brief PlotWindowContainer::isAnimationWindow
* Returns true if pObject is a AnimationWindow.
* \param pObject
* \return
*/
bool PlotWindowContainer::isAnimationWindow(QObject *pObject)
{
if (pObject && 0 == pObject->objectName().compare("animationWindow")) {
return true;
}
return false;
}
/*!
* \brief PlotWindowContainer::isDiagramWindow
* Returns true if pObject is a DiagramWindow.
* \param pObject
* \return
*/
bool PlotWindowContainer::isDiagramWindow(QObject *pObject)
{
if (pObject && 0 == pObject->objectName().compare("diagramWindow")) {
return true;
}
return false;
}
/*!
* \brief PlotWindowContainer::eventFilter
* \param pObject
* \param pEvent
* \return
*/
bool PlotWindowContainer::eventFilter(QObject *pObject, QEvent *pEvent)
{
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pObject);
if (pPlotWindow && isPlotWindow(pObject) && pEvent->type() == QEvent::Paint) {
QPainter painter (pPlotWindow);
painter.setPen(Qt::gray);
QRect rectangle = pPlotWindow->rect();
rectangle.setWidth(pPlotWindow->rect().width() - 1);
rectangle.setHeight(pPlotWindow->rect().height() - 1);
painter.drawRect(rectangle);
return true;
}
return QMdiArea::eventFilter(pObject, pEvent);
}
/*!
* \brief PlotWindowContainer::addPlotWindow
* Adds a new Plot Window.
* \param maximized - sets the window state maximized
*/
void PlotWindowContainer::addPlotWindow(bool maximized)
{
try {
PlotWindow *pPlotWindow = new PlotWindow(QStringList(), this);
pPlotWindow->setPlotType(PlotWindow::PLOT);
pPlotWindow->setWindowTitle(getUniqueName("Plot : "));
pPlotWindow->setTitle("");
pPlotWindow->setLegendPosition("top");
pPlotWindow->setAutoScale(OptionsDialog::instance()->getPlottingPage()->getAutoScaleCheckBox()->isChecked());
pPlotWindow->setPrefixUnits(OptionsDialog::instance()->getPlottingPage()->getPrefixUnitsCheckbox()->isChecked());
pPlotWindow->setTimeUnit(MainWindow::instance()->getVariablesWidget()->getSimulationTimeComboBox()->currentText());
pPlotWindow->setXLabel(QString("time"));
pPlotWindow->installEventFilter(this);
QMdiSubWindow *pSubWindow = addSubWindow(pPlotWindow);
PlottingPage *pPlottingPage = OptionsDialog::instance()->getPlottingPage();
pPlotWindow->getPlot()->setFontSizes(pPlottingPage->getTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisNumbersFontSizeSpinBox()->value(),
pPlottingPage->getHorizontalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getHorizontalAxisNumbersFontSizeSpinBox()->value(), pPlottingPage->getFooterFontSizeSpinBox()->value(),
pPlottingPage->getLegendFontSizeSpinBox()->value());
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/plot-window.svg"));
pPlotWindow->show();
if (maximized) {
pPlotWindow->setWindowState(Qt::WindowMaximized);
}
}
catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
}
}
/*!
* \brief PlotWindowContainer::addParametricPlotWindow
* Adds a new Plot Parametric Window.
*/
void PlotWindowContainer::addParametricPlotWindow()
{
try {
PlotWindow *pPlotWindow = new PlotWindow(QStringList(), this);
pPlotWindow->setPlotType(PlotWindow::PLOTPARAMETRIC);
pPlotWindow->setWindowTitle(getUniqueName("Parametric Plot : "));
pPlotWindow->setTitle("");
pPlotWindow->setLegendPosition("top");
pPlotWindow->setAutoScale(OptionsDialog::instance()->getPlottingPage()->getAutoScaleCheckBox()->isChecked());
pPlotWindow->setPrefixUnits(OptionsDialog::instance()->getPlottingPage()->getPrefixUnitsCheckbox()->isChecked());
pPlotWindow->setTimeUnit(MainWindow::instance()->getVariablesWidget()->getSimulationTimeComboBox()->currentText());
pPlotWindow->installEventFilter(this);
QMdiSubWindow *pSubWindow = addSubWindow(pPlotWindow);
PlottingPage *pPlottingPage = OptionsDialog::instance()->getPlottingPage();
pPlotWindow->getPlot()->setFontSizes(pPlottingPage->getTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisNumbersFontSizeSpinBox()->value(),
pPlottingPage->getHorizontalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getHorizontalAxisNumbersFontSizeSpinBox()->value(), pPlottingPage->getFooterFontSizeSpinBox()->value(),
pPlottingPage->getLegendFontSizeSpinBox()->value());
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/parametric-plot-window.svg"));
pPlotWindow->show();
}
catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
}
}
/*!
* \brief PlotWindowContainer::addArrayPlotWindow
* Adds a new ArrayPlot Window.
* \param maximized - sets the window state maximized
*/
void PlotWindowContainer::addArrayPlotWindow(bool maximized)
{
try {
PlotWindow *pPlotWindow = new PlotWindow(QStringList(), this);
pPlotWindow->setPlotType(PlotWindow::PLOTARRAY);
pPlotWindow->setWindowTitle(getUniqueName("Array Plot : "));
pPlotWindow->setTitle("");
pPlotWindow->setLegendPosition("top");
pPlotWindow->setAutoScale(OptionsDialog::instance()->getPlottingPage()->getAutoScaleCheckBox()->isChecked());
pPlotWindow->setPrefixUnits(OptionsDialog::instance()->getPlottingPage()->getPrefixUnitsCheckbox()->isChecked());
QComboBox* unitComboBox = MainWindow::instance()->getVariablesWidget()->getSimulationTimeComboBox();
if (unitComboBox->currentText() == ""){
int currentIndex = unitComboBox->findText("s", Qt::MatchExactly);
if (currentIndex > -1) {
unitComboBox->setCurrentIndex(currentIndex);
}
}
pPlotWindow->setTimeUnit(unitComboBox->currentText());
pPlotWindow->setXLabel(QString("array element index"));
pPlotWindow->installEventFilter(this);
QMdiSubWindow *pSubWindow = addSubWindow(pPlotWindow);
PlottingPage *pPlottingPage = OptionsDialog::instance()->getPlottingPage();
pPlotWindow->getPlot()->setFontSizes(pPlottingPage->getTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisNumbersFontSizeSpinBox()->value(),
pPlottingPage->getHorizontalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getHorizontalAxisNumbersFontSizeSpinBox()->value(), pPlottingPage->getFooterFontSizeSpinBox()->value(),
pPlottingPage->getLegendFontSizeSpinBox()->value());
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/array-plot-window.svg"));
pPlotWindow->show();
if (maximized) {
pPlotWindow->setWindowState(Qt::WindowMaximized);
}
}
catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
}
}
/*!
* \brief PlotWindowContainer::addInteractivePlotWindow
* Adds a new Interactive Plot Window
*/
PlotWindow* PlotWindowContainer::addInteractivePlotWindow(bool maximized, QString owner, int port)
{
try {
PlotWindow *pPlotWindow = new PlotWindow(QStringList(), this, true);
pPlotWindow->setPlotType(PlotWindow::PLOTINTERACTIVE);
pPlotWindow->setInteractiveOwner(owner);
pPlotWindow->setInteractivePort(port);
connect(pPlotWindow, SIGNAL(closingDown()), SLOT(removeInteractivePlotWindow()));
pPlotWindow->setWindowTitle(tr("Interactive Plot : %1").arg(owner));
pPlotWindow->setTitle("");
pPlotWindow->setLegendPosition("top");
pPlotWindow->setAutoScale(OptionsDialog::instance()->getPlottingPage()->getAutoScaleCheckBox()->isChecked());
pPlotWindow->setPrefixUnits(OptionsDialog::instance()->getPlottingPage()->getPrefixUnitsCheckbox()->isChecked());
pPlotWindow->setTimeUnit(MainWindow::instance()->getVariablesWidget()->getSimulationTimeComboBox()->currentText());
pPlotWindow->setXLabel(QString("time"));
pPlotWindow->installEventFilter(this);
QMdiSubWindow *pSubWindow = addSubWindow(pPlotWindow);
PlottingPage *pPlottingPage = OptionsDialog::instance()->getPlottingPage();
pPlotWindow->getPlot()->setFontSizes(pPlottingPage->getTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisNumbersFontSizeSpinBox()->value(),
pPlottingPage->getHorizontalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getHorizontalAxisNumbersFontSizeSpinBox()->value(), pPlottingPage->getFooterFontSizeSpinBox()->value(),
pPlottingPage->getLegendFontSizeSpinBox()->value());
pPlotWindow->setSubWindow(pSubWindow);
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/interaction.svg"));
pPlotWindow->show();
if (maximized) {
pPlotWindow->setWindowState(Qt::WindowMaximized);
}
return pPlotWindow;
}
catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
return 0;
}
}
/*!
* \brief PlotWindowContainer::addParametricArrayPlotWindow
* Adds a new Array Parametric Plot Window.
*/
void PlotWindowContainer::addArrayParametricPlotWindow()
{
try {
PlotWindow *pPlotWindow = new PlotWindow(QStringList(), this);
pPlotWindow->setPlotType(PlotWindow::PLOTARRAYPARAMETRIC);
pPlotWindow->setWindowTitle(getUniqueName("Array Parametric Plot : "));
pPlotWindow->setTitle("");
pPlotWindow->setLegendPosition("top");
pPlotWindow->setAutoScale(OptionsDialog::instance()->getPlottingPage()->getAutoScaleCheckBox()->isChecked());
pPlotWindow->setPrefixUnits(OptionsDialog::instance()->getPlottingPage()->getPrefixUnitsCheckbox()->isChecked());
QComboBox* unitComboBox = MainWindow::instance()->getVariablesWidget()->getSimulationTimeComboBox();
if (unitComboBox->currentText() == ""){
int currentIndex = unitComboBox->findText("s", Qt::MatchExactly);
if (currentIndex > -1) {
unitComboBox->setCurrentIndex(currentIndex);
}
}
pPlotWindow->setTimeUnit(unitComboBox->currentText());
pPlotWindow->installEventFilter(this);
QMdiSubWindow *pSubWindow = addSubWindow(pPlotWindow);
PlottingPage *pPlottingPage = OptionsDialog::instance()->getPlottingPage();
pPlotWindow->getPlot()->setFontSizes(pPlottingPage->getTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getVerticalAxisNumbersFontSizeSpinBox()->value(),
pPlottingPage->getHorizontalAxisTitleFontSizeSpinBox()->value(), pPlottingPage->getHorizontalAxisNumbersFontSizeSpinBox()->value(), pPlottingPage->getFooterFontSizeSpinBox()->value(),
pPlottingPage->getLegendFontSizeSpinBox()->value());
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/array-parametric-plot-window.svg"));
pPlotWindow->show();
}
catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
}
}
/*!
* \brief PlotWindowContainer::addAnimationWindow
* Adds an animation widget as subwindow
* \param maximized - sets the window state maximized
*/
void PlotWindowContainer::addAnimationWindow(bool maximized)
{
#if !defined(WITHOUT_OSG)
AnimationWindow *pAnimationWindow = new AnimationWindow(this);
pAnimationWindow->setWindowTitle(getUniqueName("Animation : "));
QMdiSubWindow *pSubWindow = addSubWindow(pAnimationWindow);
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/animation.svg"));
pAnimationWindow->show();
if (maximized) {
pAnimationWindow->setWindowState(Qt::WindowMaximized);
}
#else
Q_UNUSED(maximized);
assert(0);
#endif
}
/*!
* \brief PlotWindowContainer::addDiagramWindow
* Adds a diagram window as subwindow
* \param pModelWidget
* \param maximized - sets the window state maximized
*/
void PlotWindowContainer::addDiagramWindow(ModelWidget *pModelWidget, bool maximized)
{
if (!mpDiagramWindow) {
mpDiagramWindow = new DiagramWindow(this);
}
mpDiagramWindow->showVisualizationDiagram(pModelWidget ? pModelWidget : MainWindow::instance()->getModelWidgetContainer()->getCurrentModelWidget());
QMdiSubWindow *pSubWindow = getDiagramSubWindowFromMdi();
if (!pSubWindow) {
pSubWindow = addSubWindow(mpDiagramWindow);
addCloseActionsToSubWindowSystemMenu(pSubWindow);
pSubWindow->setWindowIcon(QIcon(":/Resources/icons/modeling.png"));
}
mpDiagramWindow->show();
if (maximized) {
mpDiagramWindow->setWindowState(Qt::WindowMaximized);
}
setActiveSubWindow(pSubWindow);
}
/*!
* \brief PlotWindowContainer::removeInteractivePlotWindow
* If an interactive plot window is closed, also remove the tree item
*/
void PlotWindowContainer::removeInteractivePlotWindow()
{
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(sender());
QString owner = pPlotWindow->getInteractiveOwner();
MainWindow::instance()->getVariablesWidget()->getVariablesTreeModel()->removeVariableTreeItem(owner);
}
/*!
* \brief PlotWindowContainer::clearPlotWindow
* Clears the plot window
*/
void PlotWindowContainer::clearPlotWindow()
{
PlotWindow *pPlotWindow = getCurrentWindow();
if (!pPlotWindow) {
QMessageBox::information(this, QString(Helper::applicationName).append(" - ").append(Helper::information),
tr("No plot window is active for clearing curves."), Helper::ok);
return;
}
int i = 0;
while(i != pPlotWindow->getPlot()->getPlotCurvesList().size()) {
PlotCurve *pPlotCurve = pPlotWindow->getPlot()->getPlotCurvesList()[i];
pPlotWindow->getPlot()->removeCurve(pPlotCurve);
pPlotCurve->detach();
i = 0; //Restart iteration
}
pPlotWindow->fitInView();
MainWindow::instance()->getVariablesWidget()->updateVariablesTreeHelper(subWindowList(QMdiArea::ActivationHistoryOrder).last());
}
/*!
* \brief PlotWindowContainer::exportVariables
* Exports the selected variables to CSV file.
*/
void PlotWindowContainer::exportVariables()
{
PlotWindow *pPlotWindow = getCurrentWindow();
if (!pPlotWindow) {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("No plot window is active for exporting variables."), Helper::ok);
return;
}
if (pPlotWindow->getPlotType() == PlotWindow::PLOTPARAMETRIC || pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("Cannot export parametric plot."), Helper::ok);
return;
}
if (pPlotWindow->getPlot()->getPlotCurvesList().isEmpty()) {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("No variables are selected for exporting."), Helper::ok);
return;
}
QString filePath = "";
QwtArray<double> timeVector;
QStringList headers;
headers << "\"time\"";
int i = 0;
foreach (PlotCurve *pPlotCurve, pPlotWindow->getPlot()->getPlotCurvesList()) {
if (pPlotCurve) {
if (i == 0) { // first iteration
filePath = pPlotCurve->getAbsoluteFilePath();
}
if (timeVector.size() < pPlotCurve->mXAxisVector.size()) {
timeVector = pPlotCurve->mXAxisVector;
}
if (filePath.compare(pPlotCurve->getAbsoluteFilePath()) != 0) {
QMessageBox::information(this, QString("%1 - %2").arg(Helper::applicationName, Helper::information), tr("Not possible to export variables from different result files."), Helper::ok);
return;
}
headers << QString("\"%1\"").arg(pPlotCurve->getYVariable());
}
++i;
}
QString name = QStringLiteral("exportedVariables");
QString fileName = StringHandler::getSaveFileName(this, QString("%1 - %2").arg(Helper::applicationName, Helper::exportVariables), NULL, "CSV Files (*.csv)", NULL, "csv", &name);
if (fileName.isEmpty()) { // if user press ESC
return;
}
// write the csv header
QString contents;
contents.append(headers.join(",")).append("\n");
// write csv data
for (int i = 0 ; i < timeVector.size() ; ++i) {
QStringList data;
// write time data
data << StringHandler::number(timeVector.at(i));
foreach (PlotCurve *pPlotCurve, pPlotWindow->getPlot()->getPlotCurvesList()) {
double value;
if (pPlotCurve && pPlotCurve->mYAxisVector.size() > i) { // parameters have just start and stop points in the dataset
value = pPlotCurve->mYAxisVector.at(i);
} else if (pPlotCurve && pPlotCurve->mYAxisVector.size() > 0) { // Set last value to have constant values for parameters
value = pPlotCurve->mYAxisVector.last();
} else { // otherwise set value to 0.0 but perhaps we should never reach there.
value = 0.0;
}
OMCInterface::convertUnits_res convertUnit = MainWindow::instance()->getOMCProxy()->convertUnits(pPlotCurve->getYDisplayUnit(), pPlotCurve->getYUnit());
if (convertUnit.unitsCompatible) {
data << StringHandler::number(Utilities::convertUnit(value, convertUnit.offset, convertUnit.scaleFactor));
} else {
data << StringHandler::number(value);
}
}
contents.append(data.join(",")).append("\n");
}
// create a file
if (MainWindow::instance()->getLibraryWidget()->saveFile(fileName, contents)) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, tr("Exported variables in %1").arg(fileName), Helper::scriptingKind, Helper::notificationLevel));
}
}
/*!
* \brief PlotWindowContainer::updatePlotWindows
* Updates the plot windows when the result file is updated.
* \param variable
*/
void PlotWindowContainer::updatePlotWindows(QString variable)
{
foreach (QMdiSubWindow *pSubWindow, subWindowList()) {
if (isPlotWindow(pSubWindow->widget())) {
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
foreach (PlotCurve *pPlotCurve, pPlotWindow->getPlot()->getPlotCurvesList()) {
if (variable.compare(pPlotCurve->getFileName()) == 0) {
pPlotWindow->getPlot()->removeCurve(pPlotCurve);
pPlotCurve->detach();
if (pPlotWindow->getAutoScaleButton()->isChecked()) {
pPlotWindow->fitInView();
} else {
pPlotWindow->updatePlot();
}
}
}
} // is plotWidget
}
}