/
graphpanelswidget.cpp
421 lines (298 loc) · 14.4 KB
/
graphpanelswidget.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Graph panels widget
//==============================================================================
#include "coreguiutils.h"
#include "graphpanelswidget.h"
#include "i18ninterface.h"
//==============================================================================
#include <QAction>
#include <QSettings>
//==============================================================================
#include "sedmlapibegin.h"
#include "sedml/SedDocument.h"
#include "sedmlapiend.h"
//==============================================================================
namespace OpenCOR {
namespace GraphPanelWidget {
//==============================================================================
GraphPanelsWidget::GraphPanelsWidget(QWidget *pParent) :
Core::SplitterWidget(pParent),
mGraphPanels(GraphPanelWidgets()),
mActiveGraphPanel(0),
mInternalSizes(QIntList()),
mUseInternalSizes(true)
{
// Set our orientation
setOrientation(Qt::Vertical);
// Create our actions
mSynchronizeXAxisAction = Core::newAction(true, this);
mSynchronizeYAxisAction = Core::newAction(true, this);
connect(mSynchronizeXAxisAction, SIGNAL(triggered(bool)),
this, SLOT(synchronizeXAxis()));
connect(mSynchronizeYAxisAction, SIGNAL(triggered(bool)),
this, SLOT(synchronizeYAxis()));
// Check whether one of our splitters has move and, therefore, whether our
// sizes have changed
connect(this, SIGNAL(splitterMoved(int, int)),
this, SLOT(stopUsingInternalSizes()));
}
//==============================================================================
void GraphPanelsWidget::retranslateUi()
{
// Retranslate our actions
I18nInterface::retranslateAction(mSynchronizeXAxisAction, tr("Synchonise X Axis"),
tr("Synchronise the X axis of all graph panels"));
I18nInterface::retranslateAction(mSynchronizeYAxisAction, tr("Synchonise Y Axis"),
tr("Synchronise the Y axis of all graph panels"));
// Retranslate all our graph panels
foreach (GraphPanelWidget *graphPanel, mGraphPanels)
graphPanel->retranslateUi();
}
//==============================================================================
void GraphPanelsWidget::initialize()
{
// Create a default graph panel, if none exists
if (mGraphPanels.isEmpty())
addGraphPanel();
}
//==============================================================================
GraphPanelWidgets GraphPanelsWidget::graphPanels() const
{
// Return our graph panels
return mGraphPanels;
}
//==============================================================================
GraphPanelWidget * GraphPanelsWidget::activeGraphPanel() const
{
// Return our active graph panel
return mActiveGraphPanel;
}
//==============================================================================
GraphPanelWidget * GraphPanelsWidget::addGraphPanel(const bool &pActive)
{
// Create a new graph panel, add it to ourselves and keep track of it
GraphPanelWidget *res = new GraphPanelWidget(mGraphPanels,
mSynchronizeXAxisAction,
mSynchronizeYAxisAction,
this);
mGraphPanels << res;
// Resize the graph panels, if needed, making sure that their size is what
// it should be
// Note: we use our internal sizes for as long as possible (i.e. as long as
// none of our splitters has moved; although, we can reuse our
// internal sizes if we have only one graph panel left). Indeed, if we
// were to open a CellML file and create two graph panels, then their
// height would be based on actual heights since everything would be
// visible. On the other hand, if we were to open a SED-ML file with
// three graph panels and that we were to rely on their actual height
// to resize them, then their new height would be based on 'hidden'
// heights since nothing would be visible. Now, the problem is that
// those 'hidden' heights tend to be much smaller than actual heights.
// The end result is that the two sets of graph panels may end up
// being quite different, something that we can prevent by using our
// internal heights...
if (mUseInternalSizes) {
mInternalSizes << 1;
setSizes(mInternalSizes);
} else {
QIntList oldSizes = sizes();
QIntList newSizes = QIntList();
int oldTotalHeight = height()-(mGraphPanels.count()-2)*handleWidth();
int newTotalHeight = oldTotalHeight-handleWidth();
double scalingFactor = double(mGraphPanels.count()-1)/mGraphPanels.count()*newTotalHeight/oldTotalHeight;
for (int i = 0, iMax = oldSizes.count()-1; i < iMax; ++i)
newSizes << round(scalingFactor*oldSizes[i]);
setSizes(newSizes << newTotalHeight-std::accumulate(newSizes.begin(), newSizes.end(), 0));
}
// Keep track of whenever a graph panel gets activated
connect(res, SIGNAL(activated(OpenCOR::GraphPanelWidget::GraphPanelWidget *)),
this, SIGNAL(graphPanelActivated(OpenCOR::GraphPanelWidget::GraphPanelWidget *)));
connect(res, SIGNAL(activated(OpenCOR::GraphPanelWidget::GraphPanelWidget *)),
this, SLOT(updateGraphPanels(OpenCOR::GraphPanelWidget::GraphPanelWidget *)));
// Keep track of the addition and removal of a graph
connect(res, SIGNAL(graphAdded(OpenCOR::GraphPanelWidget::GraphPanelWidget *, OpenCOR::GraphPanelWidget::GraphPanelPlotGraph *, const OpenCOR::GraphPanelWidget::GraphPanelPlotGraphProperties &)),
this, SIGNAL(graphAdded(OpenCOR::GraphPanelWidget::GraphPanelWidget *, OpenCOR::GraphPanelWidget::GraphPanelPlotGraph *, const OpenCOR::GraphPanelWidget::GraphPanelPlotGraphProperties &)));
connect(res, SIGNAL(graphsRemoved(OpenCOR::GraphPanelWidget::GraphPanelWidget *, const OpenCOR::GraphPanelWidget::GraphPanelPlotGraphs &)),
this, SIGNAL(graphsRemoved(OpenCOR::GraphPanelWidget::GraphPanelWidget *, const OpenCOR::GraphPanelWidget::GraphPanelPlotGraphs &)));
// In/activate the graph panel
res->setActive(pActive);
// Let people know that we have added a graph panel
emit graphPanelAdded(res, pActive);
// Synchronise the axes of our graph panels, if needed, and ensure that they
// are all aligned with one another by forcing the setting of the axes of
// our active graph panel
// Note: at startup, activeGraphPanelPlot is (obviously) null, hence we use
// our newly created graph panel's plot instead...
GraphPanelPlotWidget *activeGraphPanelPlot = mActiveGraphPanel?mActiveGraphPanel->plot():0;
if (!activeGraphPanelPlot)
activeGraphPanelPlot = mActiveGraphPanel->plot();
activeGraphPanelPlot->setAxes(activeGraphPanelPlot->minX(),
activeGraphPanelPlot->maxX(),
activeGraphPanelPlot->minY(),
activeGraphPanelPlot->maxY(),
true, true, true, true, true);
// Return our newly created graph panel
return res;
}
//==============================================================================
bool GraphPanelsWidget::removeGraphPanel(GraphPanelWidget *pGraphPanel)
{
if (!pGraphPanel)
return false;
// Retrieve the index of the given graph panel
int index = mGraphPanels.indexOf(pGraphPanel);
// Let people know that we have removed it (or, rather, about to remove it)
// Note: we let people know before we actually delete the graph panel,
// because some people interested in that signal might have used the
// pointer to keep track of some information, as is done in
// SimulationExperimentViewInformationGraphPanelAndGraphsWidget for
// example...
emit graphPanelRemoved(pGraphPanel);
// Update some trackers
// Note: mActiveGraphPanel will automatically get updated when another graph
// panel gets selected...
mGraphPanels.removeOne(pGraphPanel);
if (mUseInternalSizes)
mInternalSizes.removeLast();
if (mGraphPanels.count() == 1)
mUseInternalSizes = true;
// Now, we can delete our graph panel
delete pGraphPanel;
// Activate the next graph panel or the last one available, if any
if (index < mGraphPanels.count()) {
// There is a next graph panel, so activate it
mGraphPanels[index]->setActive(true);
} else {
// We were dealing with the last graph panel, so just activate the new
// last graph panel
mGraphPanels[mGraphPanels.count()-1]->setActive(true);
}
// Ask our active graph panel's plot to align itself against its neighbours,
// if any
if (!mGraphPanels.isEmpty())
mActiveGraphPanel->plot()->forceAlignWithNeighbors();
return true;
}
//==============================================================================
bool GraphPanelsWidget::removeCurrentGraphPanel()
{
// Remove the current graph panel
if (mGraphPanels.count() == 1) {
// There is only one graph panel, so add one and then remove our 'first'
// graph panel
addGraphPanel();
return removeGraphPanel(mGraphPanels.first());
} else {
return removeGraphPanel(mActiveGraphPanel);
}
}
//==============================================================================
void GraphPanelsWidget::removeAllGraphPanels()
{
// Remove all the graph panels, after having created an 'empty' one (since
// we want at least one graph panel at any given point in time)
addGraphPanel();
while (mGraphPanels.count() > 1)
removeGraphPanel(mGraphPanels.first());
}
//==============================================================================
void GraphPanelsWidget::setActiveGraphPanel(GraphPanelWidget *pGraphPanel)
{
// Make sure that we own the given graph panel
if (!mGraphPanels.contains(pGraphPanel))
return;
// Make the given graph panel the active one
pGraphPanel->setActive(true, true);
}
//==============================================================================
QIntList GraphPanelsWidget::sizes() const
{
// Return either our internal or default sizes, depending on our current
// state
return mUseInternalSizes?mInternalSizes:QSplitter::sizes();
}
//==============================================================================
void GraphPanelsWidget::setSizes(const QIntList &pSizes)
{
// Make sure that we have the correct number of sizes
if (pSizes.count() != count())
return;
// Set our sizes as internal and/or default sizes, depending on whether the
// given sizes consist of ones
foreach (const int &size, pSizes) {
if (size != 1) {
mUseInternalSizes = false;
break;
}
}
if (mUseInternalSizes)
mInternalSizes = pSizes;
QSplitter::setSizes(pSizes);
}
//==============================================================================
void GraphPanelsWidget::stopUsingInternalSizes()
{
// Stop using our internal sizes
mUseInternalSizes = false;
}
//==============================================================================
void GraphPanelsWidget::updateGraphPanels(OpenCOR::GraphPanelWidget::GraphPanelWidget *pGraphPanel)
{
// Keep track of the newly activated graph panel
mActiveGraphPanel = pGraphPanel;
// Inactivate all the other graph panels
foreach (GraphPanelWidget *graphPanel, mGraphPanels) {
if (graphPanel != pGraphPanel) {
// We are not dealing with the graph panel that just got activated,
// so inactivate it
graphPanel->setActive(false);
}
}
}
//==============================================================================
void GraphPanelsWidget::synchronizeXAxis()
{
// Synchronise the X axis of our graph panels, if needed, by forcing the
// setting of the axes of our active graph panel
if (mSynchronizeXAxisAction->isChecked()) {
GraphPanelPlotWidget *activeGraphPanelPlot = mActiveGraphPanel->plot();
activeGraphPanelPlot->setAxes(activeGraphPanelPlot->minX(),
activeGraphPanelPlot->maxX(),
activeGraphPanelPlot->minY(),
activeGraphPanelPlot->maxY(),
true, true, true, true, false);
}
}
//==============================================================================
void GraphPanelsWidget::synchronizeYAxis()
{
// Synchronise the Y axis of our graph panels, if needed, by forcing the
// setting of the axes of our active graph panel
if (mSynchronizeYAxisAction->isChecked()) {
GraphPanelPlotWidget *activeGraphPanelPlot = mActiveGraphPanel->plot();
activeGraphPanelPlot->setAxes(activeGraphPanelPlot->minX(),
activeGraphPanelPlot->maxX(),
activeGraphPanelPlot->minY(),
activeGraphPanelPlot->maxY(),
true, true, true, false, true);
}
}
//==============================================================================
} // namespace GraphPanelWidget
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================