/
graphpanelwidget.cpp
320 lines (224 loc) · 8.95 KB
/
graphpanelwidget.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
/*******************************************************************************
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 <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Graph panel widget
//==============================================================================
#include "coreguiutils.h"
#include "graphpanelwidget.h"
//==============================================================================
#include <QEvent>
#include <QHBoxLayout>
//==============================================================================
namespace OpenCOR {
namespace GraphPanelWidget {
//==============================================================================
GraphPanelWidgetProperties::GraphPanelWidgetProperties(const QColor &pBackgroundColor,
const QColor &pForegroundColor,
bool pLegend) :
mBackgroundColor(pBackgroundColor),
mForegroundColor(pForegroundColor),
mLegend(pLegend)
{
}
//==============================================================================
QColor GraphPanelWidgetProperties::backgroundColor() const
{
// Return our background colour
return mBackgroundColor;
}
//==============================================================================
QColor GraphPanelWidgetProperties::foregroundColor() const
{
// Return our foreground colour
return mForegroundColor;
}
//==============================================================================
bool GraphPanelWidgetProperties::legend() const
{
// Return our legend
return mLegend;
}
//==============================================================================
GraphPanelWidget::GraphPanelWidget(const GraphPanelWidgets &pNeighbors,
QAction *pSynchronizeXAxisAction,
QAction *pSynchronizeYAxisAction,
QWidget *pParent) :
Widget(pParent)
{
// Create and set our horizontal layout
auto layout = new QHBoxLayout(this);
layout->setContentsMargins({});
layout->setSpacing(0);
setLayout(layout);
// Create, customise and add an inactive marker to our layout
// Note: we want the width of our marker to be 3 pixels wide. However, on
// macOS, to actually get a 3-pixel-wide marker, we need to make it 4
// pixels wide...!?
enum {
#ifdef Q_OS_MAC
MarkerWidth = 4
#else
MarkerWidth = 3
#endif
};
mMarker = new QFrame(this);
mMarker->setFrameShape(QFrame::VLine);
mMarker->setLineWidth(MarkerWidth);
mMarker->setMinimumWidth(MarkerWidth);
setActive(false);
layout->addWidget(mMarker);
// Create and add a plot widget to our layout
GraphPanelPlotWidgets neighbors;
for (auto neighbor : pNeighbors) {
neighbors << neighbor->plot();
}
mPlot = new GraphPanelPlotWidget(neighbors, pSynchronizeXAxisAction,
pSynchronizeYAxisAction, this);
layout->addWidget(mPlot);
// Allow the graph panel to be of any vertical size
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
// Make sure that our marker's colour is properly intiialised
updateMarkerColor();
// Let our plot's neighbours know about our plot
for (auto neighbor : neighbors) {
neighbor->addNeighbor(mPlot);
}
}
//==============================================================================
GraphPanelWidget::GraphPanelWidget(const GraphPanelWidgets &pNeighbors,
QWidget *pParent) :
GraphPanelWidget(pNeighbors, nullptr, nullptr, pParent)
{
}
//==============================================================================
GraphPanelWidget::~GraphPanelWidget()
{
// Let our plot's neighbours know that our plot is not going to be their
// neighbour anymore
for (auto plot : mPlot->neighbors()) {
if (plot != mPlot) {
plot->removeNeighbor(mPlot);
}
}
}
//==============================================================================
void GraphPanelWidget::retranslateUi()
{
// Retranslate our plot
mPlot->retranslateUi();
}
//==============================================================================
void GraphPanelWidget::changeEvent(QEvent *pEvent)
{
// Default handling of the event
Core::Widget::changeEvent(pEvent);
// Check whether the palette or our enabled state has changed and if so then
// update the colour used to highlight the active graph panel
if ( (pEvent->type() == QEvent::PaletteChange)
|| (pEvent->type() == QEvent::EnabledChange)) {
updateMarkerColor();
}
}
//==============================================================================
void GraphPanelWidget::mousePressEvent(QMouseEvent *pEvent)
{
// Default handling of the event
QWidget::mousePressEvent(pEvent);
// Activate/inactivate the graph panel
// Note: we do it through setActive() because we want the graph panel to let
// people know that our active state has changed...
setActive(true);
}
//==============================================================================
GraphPanelPlotWidget * GraphPanelWidget::plot() const
{
// Return our plot
return mPlot;
}
//==============================================================================
GraphPanelPlotGraphs GraphPanelWidget::graphs() const
{
// Return all our plot's graphs
return mPlot->graphs();
}
//==============================================================================
void GraphPanelWidget::addGraph(GraphPanelPlotGraph *pGraph,
const GraphPanelPlotGraphProperties &pGraphProperties)
{
// Add the graph to our plot
if (mPlot->addGraph(pGraph)) {
emit graphAdded(this, pGraph, pGraphProperties);
}
}
//==============================================================================
void GraphPanelWidget::removeGraphs(const GraphPanelPlotGraphs &pGraphs)
{
// Remove the given graphs from our plot
GraphPanelPlotGraphs graphs;
for (auto graph : pGraphs) {
if (mPlot->removeGraph(graph)) {
graphs << graph;
}
}
emit graphsRemoved(this, graphs);
}
//==============================================================================
void GraphPanelWidget::removeAllGraphs()
{
// Remove all our graphs from our plot
removeGraphs(graphs());
}
//==============================================================================
bool GraphPanelWidget::isActive() const
{
// Return whether the graph panel is active
return mActive;
}
//==============================================================================
void GraphPanelWidget::updateMarkerColor()
{
// Update the marker's colour based on whether the graph panel is active
mMarker->setStyleSheet(QString("QFrame {"
" color: %1;"
"}").arg(mActive?
isEnabled()?
Core::highlightColor().name():
Core::highlightColor(QPalette::Disabled).name(QColor::HexArgb):
isEnabled()?
Core::windowColor().name():
Core::windowColor(QPalette::Disabled).name(QColor::HexArgb)));
}
//==============================================================================
void GraphPanelWidget::setActive(bool pActive, bool pForce)
{
if ((pActive == mActive) && !pForce) {
return;
}
// Set the graph panel's active state
mActive = pActive;
// Update the marker's colour
updateMarkerColor();
// Let people know if the graph panel has been activated or inactivated
if (pActive) {
emit activated(this);
} else {
emit inactivated(this);
}
}
//==============================================================================
} // namespace GraphPanelWidget
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================