-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractiveqwtplot.cpp
More file actions
97 lines (78 loc) · 3.58 KB
/
Copy pathinteractiveqwtplot.cpp
File metadata and controls
97 lines (78 loc) · 3.58 KB
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
// Copyright (c) 2025 Jesiel Emerim Schardosim
// SPDX-License-Identifier: Apache-2.0
//
// This file is part of the PolyApprox project.
// See the LICENSE and NOTICE files in the project root for details.
#include "interactiveqwtplot.h"
InteractiveQwtPlot::InteractiveQwtPlot(QWidget *parent) : QwtPlot(parent) {}
void InteractiveQwtPlot::setRatioWidth(double rw) { ratio_width = rw; }
void InteractiveQwtPlot::setRatioHeight(double rh) { ratio_height = rh; }
double InteractiveQwtPlot::getRatioWidth() { return ratio_width; }
double InteractiveQwtPlot::getRatioHeight() { return ratio_height; }
void InteractiveQwtPlot::adjustAxesToMaintainRatio() {
QSize current_size = size();
adjustAxesToMaintainRatio(current_size, current_size);
}
void InteractiveQwtPlot::adjustAxesToMaintainRatio(const QSize &old_size,
const QSize &size) {
const QwtScaleDiv &x_axis_scale_div = axisScaleDiv(QwtPlot::xBottom);
const QwtScaleDiv &y_axis_scale_div = axisScaleDiv(QwtPlot::yLeft);
if (x_axis_scale_div.isEmpty() ||
(!qIsFinite(x_axis_scale_div.lowerBound()) ||
!qIsFinite(x_axis_scale_div.upperBound())) ||
y_axis_scale_div.isEmpty() ||
(!qIsFinite(y_axis_scale_div.lowerBound()) ||
!qIsFinite(y_axis_scale_div.upperBound()))) {
qWarning() << "Chart has empty or invalid axis scale division. Ratio cannot be maintained.";
return;
}
qreal x_range = x_axis_scale_div.upperBound() - x_axis_scale_div.lowerBound();
qreal y_range = y_axis_scale_div.upperBound() - y_axis_scale_div.lowerBound();
if (x_range <= 0 || y_range <= 0) {
qWarning() << "Chart has axis with zero or negative range. Ratio cannot be maintained.";
return;
}
QRectF canvas_area_px = canvas()->rect();
if (canvas_area_px.isEmpty()) {
qWarning() << "Canvas has empty area. Ratio cannot be maintained.";
return;
}
qreal canvas_width_px = canvas_area_px.width();
qreal canvas_height_px = canvas_area_px.height();
if (canvas_width_px <= 0 || canvas_height_px <= 0) {
qWarning() << "Canvas has dimension with zero or negative value. Ratio cannot be maintained.";
return;
}
qreal x_px_per_unit = canvas_width_px / x_range;
qreal y_px_per_unit = canvas_height_px / y_range;
if (qFuzzyCompare(x_px_per_unit / ratio_height,
y_px_per_unit / ratio_width)) {
return;
}
if (old_size.height() != size.height()) {
qreal target_y_range =
(canvas_height_px / x_px_per_unit) * (ratio_height / ratio_width);
qreal current_y_center =
(y_axis_scale_div.lowerBound() + y_axis_scale_div.upperBound()) / 2.0;
QwtScaleDiv y_axis_new(current_y_center - target_y_range / 2.0,
current_y_center + target_y_range / 2.0);
setAxisScale(QwtPlot::yLeft, current_y_center - target_y_range / 2.0,
current_y_center + target_y_range / 2.0,
1.0 * ratio_height);
} else {
qreal target_x_range =
(canvas_width_px / y_px_per_unit) * (ratio_width / ratio_height);
qreal current_x_center =
(x_axis_scale_div.lowerBound() + x_axis_scale_div.upperBound()) / 2.0;
QwtScaleDiv x_axis_new(current_x_center - target_x_range / 2.0,
current_x_center + target_x_range / 2.0);
setAxisScale(
QwtPlot::xBottom, current_x_center - target_x_range / 2.0,
current_x_center + target_x_range / 2.0, 1.0 * ratio_width);
}
replot();
}
void InteractiveQwtPlot::resizeEvent(QResizeEvent *event) {
QwtPlot::resizeEvent(event);
adjustAxesToMaintainRatio(event->oldSize(), event->size());
}