Debian Codegraph @

avogadro/qtgui/chartwidget.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "chartwidget.h"
7
8 #include <jkqtplotter/jkqtplotter.h>
9 #include <jkqtplotter/graphs/jkqtplines.h>
10
11 #include <QDebug>
12 #include <QEvent>
13 #include <QHBoxLayout>
14 #include <QPalette>
15
16 namespace Avogadro::QtGui {
17
18 class ChartWidget::ChartWidgetImpl
19 {
20 public:
21 ChartWidgetImpl()
22 {
23 plot = new JKQTPlotter;
24 updateAxisColorsForTheme();
25 }
26 ~ChartWidgetImpl() { delete plot; }
27
28 ChartWidgetImpl(const ChartWidgetImpl&) = delete;
29 ChartWidgetImpl& operator=(const ChartWidgetImpl&) = delete;
30 ChartWidgetImpl(ChartWidgetImpl&&) = delete;
31 ChartWidgetImpl& operator=(ChartWidgetImpl&&) = delete;
32
33 void updateAxisColorsForTheme()
34 {
35 const QPalette defaultPalette;
36 // is the text lighter than the window color?
37 bool darkMode = (defaultPalette.color(QPalette::WindowText).lightness() >
38 defaultPalette.color(QPalette::Window).lightness());
39 QColor textColor = darkMode ? Qt::white : Qt::black;
40 plot->getXAxis()->setTickLabelColor(textColor);
41 plot->getYAxis()->setTickLabelColor(textColor);
42 plot->getXAxis()->setLabelColor(textColor);
43 plot->getYAxis()->setLabelColor(textColor);
44 }
45
46 JKQTPlotter* plot; // widget
47 };
48
49 ChartWidget::ChartWidget(QWidget* p) : QWidget(p), m_impl(new ChartWidgetImpl)
50 {
51 auto hLayout = new QHBoxLayout(this);
52 auto* plot = m_impl->plot;
53
54 // connect the single-click and double-click signals
55 connect(plot, &JKQTPlotter::plotMouseClicked, this,
56 &ChartWidget::plotClicked);
57 connect(plot, &JKQTPlotter::plotMouseDoubleClicked, this,
58 &ChartWidget::resetZoom);
59
60 hLayout->setContentsMargins(0, 0, 0, 0);
61 hLayout->addWidget(plot);
62 setLayout(hLayout);
63 setMinimumWidth(100);
64 setMinimumHeight(100);
65 }
66
67 ChartWidget::~ChartWidget() = default;
68
69 bool ChartWidget::addPlot(const std::vector<float>& x,
70 const std::vector<float>& y,
71 const std::array<unsigned char, 4>& color,
72 const QString& xName, const QString& yName)
73 {
74 // The x and y arrays must be of the same length, otherwise it is not x, y...
75 if (x.size() != y.size())
76 return false;
77
78 auto* plot = m_impl->plot;
79 if (plot == nullptr)
80 return false;
81
82 auto* ds = plot->getDatastore();
83 size_t columnX = ds->addCopiedColumn(x, xName);
84 size_t columnY = ds->addCopiedColumn(y, yName);
85
86 JKQTPXYLineGraph* graph = new JKQTPXYLineGraph(plot);
87 graph->setXColumn(columnX);
88 graph->setYColumn(columnY);
89 if (!m_showPoints)
90 graph->setSymbolType(JKQTPNoSymbol);
91 else
92 graph->setSymbolType(JKQTPFilledCircle);
93
94 graph->setLineWidth(m_lineWidth);
95
96 QColor c(color[0], color[1], color[2], color[3]);
97 graph->setLineColor(c, color[3] / 255.0);
98 graph->setTitle(yName);
99
100 plot->addGraph(graph);
101 return true;
102 }
103
104 bool ChartWidget::addSeries(const std::vector<float>& newSeries,
105 const std::array<unsigned char, 4>& color,
106 const QString& name)
107 {
108 if (newSeries.empty())
109 return false;
110
111 auto* plot = m_impl->plot;
112 if (plot == nullptr)
113 return false;
114
115 auto* ds = plot->getDatastore();
116 // get the x column
117 size_t maxRows = ds->getMaxRows();
118 if (newSeries.size() != maxRows)
119 return false;
120
121 size_t columnY = ds->addCopiedColumn(newSeries, name);
122
123 JKQTPXYLineGraph* graph = new JKQTPXYLineGraph(plot);
124 graph->setXColumn(0);
125 graph->setYColumn(columnY);
126 if (!m_showPoints)
127 graph->setSymbolType(JKQTPNoSymbol);
128 else
129 graph->setSymbolType(JKQTPFilledCircle);
130
131 graph->setLineWidth(m_lineWidth);
132
133 QColor c(color[0], color[1], color[2], color[3]);
134 graph->setLineColor(c, color[3] / 255.0);
135 graph->setTitle(name);
136
137 plot->addGraph(graph);
138 return true;
139 }
140
141 bool ChartWidget::addPlots(const std::vector<std::vector<float>>& plotData,
142 const std::array<unsigned char, 4>& color,
143 const QStringList& names)
144 {
145 // Need at least an x and a y.
146 if (plotData.size() < 2)
147 return false;
148
149 // All arrays must be the same size to go in the same table.
150 auto xSize = plotData[0].size();
151 for (const auto& d : plotData)
152 if (xSize != d.size())
153 return false;
154
155 // check to make sure names is the same size as plotData
156 if (names.size() != plotData.size())
157 return false;
158
159 auto* plot = m_impl->plot;
160 if (plot == nullptr)
161 return false;
162
163 auto* ds = plot->getDatastore();
164 const QString xName = names[0];
165 size_t columnX = ds->addCopiedColumn(plotData[0], xName);
166 QColor c(color[0], color[1], color[2], color[3]);
167
168 // loop through the columns
169 for (size_t i = 1; i < plotData.size(); i++) {
170 const QString yName = names[i];
171 size_t columnY = ds->addCopiedColumn(plotData[i], yName);
172
173 JKQTPXYLineGraph* graph = new JKQTPXYLineGraph(plot);
174 graph->setXColumn(columnX);
175 graph->setYColumn(columnY);
176 if (!m_showPoints)
177 graph->setSymbolType(JKQTPNoSymbol);
178 else
179 graph->setSymbolType(JKQTPFilledCircle);
180
181 graph->setLineWidth(m_lineWidth);
182 graph->setLineColor(c, color[3] / 255.0);
183 graph->setTitle(yName);
184
185 plot->addGraph(graph);
186 }
187
188 return true;
189 }
190
191 void ChartWidget::changeEvent(QEvent* event)
192 {
193 if (event->type() == QEvent::PaletteChange) {
194 m_impl->updateAxisColorsForTheme();
195 m_impl->plot->update();
196 }
197 QWidget::changeEvent(event);
198 }
199
200 void ChartWidget::resetZoom()
201 {
202 m_impl->plot->zoomToFit();
203 }
204
205 void ChartWidget::plotClicked(double x, double y,
206 Qt::KeyboardModifiers modifiers,
207 Qt::MouseButton button)
208 {
209 emit clicked(x, y, modifiers);
210 }
211
212 void ChartWidget::clearPlots()
213 {
214 m_impl->plot->clearGraphs();
215 m_impl->plot->getDatastore()->clear();
216 }
217
218 void ChartWidget::setXAxisTitle(const QString& title)
219 {
220 QString label = QString("{\\bf %1 }").arg(title);
221 m_impl->plot->getXAxis()->setAxisLabel(label);
222 }
223
224 void ChartWidget::setYAxisTitle(const QString& title)
225 {
226 QString label = QString("{\\bf %1 }").arg(title);
227 m_impl->plot->getYAxis()->setAxisLabel(label);
228 }
229
230 void ChartWidget::setFontSize(int size)
231 {
232 auto* plot = m_impl->plot;
233 plot->getXAxis()->setTickLabelFontSize(size);
234 plot->getYAxis()->setTickLabelFontSize(size);
235
236 int titleSize = round(size * 1.25);
237 plot->getXAxis()->setLabelFontSize(titleSize);
238 plot->getYAxis()->setLabelFontSize(titleSize);
239 }
240
241 void ChartWidget::setLineWidth(float width)
242 {
243 m_lineWidth = width;
244 for (auto* g : m_impl->plot->getGraphs()) {
245 auto* lineGraph = dynamic_cast<JKQTPXYLineGraph*>(g);
246 if (lineGraph != nullptr)
247 lineGraph->setLineWidth(m_lineWidth);
248 }
249 // redraw
250 m_impl->plot->update();
251 }
252
253 void ChartWidget::setAxisColor(Axis a, const color4ub& color)
254 {
255 QColor c(color[0], color[1], color[2], color[3]);
256 if (a == Axis::x) {
257 m_impl->plot->getXAxis()->setAxisColor(c);
258 } else {
259 m_impl->plot->getYAxis()->setAxisColor(c);
260 }
261 }
262
263 void ChartWidget::setBackgroundColor(const color4ub& color)
264 {
265 QColor c(color[0], color[1], color[2], color[3]);
266 m_impl->plot->getPlotter()->setPlotBackgroundColor(c);
267 }
268
269 void ChartWidget::setLegendLocation(LegendLocation location)
270 {
271
272 m_impl->plot->getPlotter()->setShowKey(true);
273 switch (location) {
274 case LegendLocation::TopLeft:
275 m_impl->plot->getPlotter()->setKeyPosition(JKQTPKeyInsideTopLeft);
276 break;
277 case LegendLocation::TopRight:
278 m_impl->plot->getPlotter()->setKeyPosition(JKQTPKeyInsideTopRight);
279 break;
280 case LegendLocation::BottomLeft:
281 m_impl->plot->getPlotter()->setKeyPosition(JKQTPKeyInsideBottomLeft);
282 break;
283 case LegendLocation::BottomRight:
284 m_impl->plot->getPlotter()->setKeyPosition(JKQTPKeyInsideBottomRight);
285 break;
286 default:
287 m_impl->plot->getPlotter()->setShowKey(false);
288 break;
289 }
290 }
291
292 void ChartWidget::setTickLabels(Axis a, const std::vector<float>& tickPositions,
293 const QStringList& tickLabels)
294 {
295 // check to see if they're the same size
296 if (tickPositions.size() != tickLabels.size())
297 return;
298
299 if (tickPositions.size() == 0)
300 return;
301
302 JKQTPCoordinateAxis* axis;
303 if (a == Axis::x) {
304 axis = m_impl->plot->getXAxis();
305 } else {
306 axis = m_impl->plot->getYAxis();
307 }
308
309 for (int i = 0; i < tickPositions.size(); i++) {
310 axis->addAxisTickLabel(tickPositions[i], tickLabels[i]);
311 }
312 }
313
314 void ChartWidget::setAxisLimits(Axis a, float min, float max)
315 {
316 bool inverted = (min > max);
317
318 if (inverted)
319 std::swap(min, max);
320
321 if (a == Axis::x) {
322 m_impl->plot->getXAxis()->setRange(min, max);
323 m_impl->plot->getXAxis()->setInverted(inverted);
324 } else {
325 m_impl->plot->getYAxis()->setRange(min, max);
326 m_impl->plot->getYAxis()->setInverted(inverted);
327 }
328 }
329
330 void ChartWidget::setXAxisLimits(float min, float max)
331 {
332 setAxisLimits(Axis::x, min, max);
333 }
334
335 void ChartWidget::setYAxisLimits(float min, float max)
336 {
337 setAxisLimits(Axis::y, min, max);
338 }
339
340 void ChartWidget::setAxisLogScale(Axis a, bool logScale)
341 {
342 if (a == Axis::x)
343 m_impl->plot->getXAxis()->setLogAxis(logScale);
344 else
345 m_impl->plot->getYAxis()->setLogAxis(logScale);
346 }
347
348 void ChartWidget::setAxisDigits(Axis a, int digits)
349 {
350 if (a == Axis::x)
351 m_impl->plot->getXAxis()->setLabelDigits(digits);
352 else
353 m_impl->plot->getYAxis()->setLabelDigits(digits);
354 }
355
356 void ChartWidget::labelPeaks(int yColumn, float threshold, int window)
357 {
358 std::vector<std::pair<float, float>> peaks;
359
360 /* auto& x = m_impl->plot->getDatastore()->getColumn(0);
361 auto& y = m_impl->plot->getDatastore()->getColumn(yColumn);
362
363 const int n = static_cast<int>(y.size());
364 if (n == 0)
365 return peaks;
366
367 int i = 0;
368 while (i < n) {
369 if (y[i] <= threshold) {
370 ++i;
371 continue;
372 }
373
374 // --- We’re inside a peak region ---
375 int regionStart = std::max(0, i - window);
376 int regionEnd = std::min(n - 1, i + window);
377
378 // Expand region forward while we're above threshold
379 while (regionEnd + 1 < n && y[regionEnd + 1] > threshold)
380 ++regionEnd;
381
382 // Find max within this region
383 int maxIdx = regionStart;
384 for (int j = regionStart + 1; j <= regionEnd; ++j) {
385 if (y[j] > y[maxIdx])
386 maxIdx = j;
387 }
388
389 peaks.emplace_back(x[maxIdx], y[maxIdx]);
390
391 // Skip past this region to avoid duplicates
392 i = regionEnd + 1;
393 }
394 */
395
396 // add them to the plot
397 }
398
399 } // namespace Avogadro::QtGui