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
|
#include "plotbase.h"
PlotBase::PlotBase() {
_horizontalScale.SignalLinkedRedraw().connect(_signalLinkedRedraw);
}
void PlotBase::ZoomFit() {
_xZoomStart = 0.0;
_xZoomEnd = 1.0;
_yZoomStart = 0.0;
_yZoomEnd = 1.0;
_onZoomChanged.emit();
}
void PlotBase::ZoomIn() {
const double distX = (_xZoomEnd - _xZoomStart) * 0.25;
_xZoomStart += distX;
_xZoomEnd -= distX;
const double distY = (_yZoomEnd - _yZoomStart) * 0.25;
_yZoomStart += distY;
_yZoomEnd -= distY;
_onZoomChanged.emit();
}
void PlotBase::ZoomInOn(double x, double y) {
const double zoomWidth = _xZoomEnd - _xZoomStart;
const double zoomHeight = _yZoomEnd - _yZoomStart;
const double xr = _horizontalScale.UnitToAxis(x) * zoomWidth + _xZoomStart;
const double yr = _verticalScale.UnitToAxis(y) * zoomHeight + _yZoomStart;
const double distX = zoomWidth * 0.25;
_xZoomStart = xr - distX;
_xZoomEnd = xr + distX;
if (_xZoomStart < 0.0) {
_xZoomEnd -= _xZoomStart;
_xZoomStart = 0.0;
}
if (_xZoomEnd > 1.0) {
_xZoomStart -= _xZoomEnd - 1.0;
_xZoomEnd = 1.0;
}
const double distY = zoomHeight * 0.25;
_yZoomStart = yr - distY;
_yZoomEnd = yr + distY;
if (_yZoomStart < 0.0) {
_yZoomEnd -= _yZoomStart;
_yZoomStart = 0.0;
}
if (_yZoomEnd > 1.0) {
_yZoomStart -= _yZoomEnd - 1.0;
_yZoomEnd = 1.0;
}
_onZoomChanged.emit();
}
void PlotBase::ZoomOut() {
if (!IsZoomedOut()) {
const double distX = std::max(0.01, (_xZoomEnd - _xZoomStart) * 0.5);
_xZoomStart -= distX;
_xZoomEnd += distX;
if (_xZoomStart < 0.0) {
_xZoomEnd -= _xZoomStart;
_xZoomStart = 0.0;
}
if (_xZoomEnd > 1.0) {
_xZoomStart -= _xZoomEnd - 1.0;
_xZoomEnd = 1.0;
}
if (_xZoomStart < 0.0) _xZoomStart = 0.0;
const double distY = std::max(0.01, (_yZoomEnd - _yZoomStart) * 0.5);
_yZoomStart -= distY;
_yZoomEnd += distY;
if (_yZoomStart < 0.0) {
_yZoomEnd -= _yZoomStart;
_yZoomStart = 0.0;
}
if (_yZoomEnd > 1.0) {
_yZoomStart -= _yZoomEnd - 1.0;
_yZoomEnd = 1.0;
}
if (_yZoomStart < 0.0) _yZoomStart = 0.0;
_onZoomChanged.emit();
}
}
void PlotBase::ZoomTo(double x1, double y1, double x2, double y2) {
if (x1 > x2) std::swap(x1, x2);
if (y1 > y2) std::swap(y1, y2);
const double width = _xZoomEnd - _xZoomStart;
const double height = _yZoomEnd - _yZoomStart;
const double newX1 = width * _horizontalScale.UnitToAxis(x1) + _xZoomStart;
const double newX2 = width * _horizontalScale.UnitToAxis(x2) + _xZoomStart;
const double newY1 = height * _verticalScale.UnitToAxis(y1) + _yZoomStart;
const double newY2 = height * _verticalScale.UnitToAxis(y2) + _yZoomStart;
_xZoomStart = std::max(0.0, std::min(1.0, newX1));
_xZoomEnd = std::max(0.0, std::min(1.0, newX2));
_yZoomStart = std::max(0.0, std::min(1.0, newY1));
_yZoomEnd = std::max(0.0, std::min(1.0, newY2));
_onZoomChanged.emit();
}
void PlotBase::Pan(double xDisplacement, double yDisplacement) {
const Rectangle plotArea = getPlotArea(_width, _height);
const double xFraction = xDisplacement / plotArea.width;
const double yFraction = yDisplacement / plotArea.height;
double dx = -xFraction * (_xZoomEnd - _xZoomStart);
double dy = yFraction * (_yZoomEnd - _yZoomStart);
if (_xZoomStart + dx < 0.0) dx = -_xZoomStart;
if (_yZoomStart + dy < 0.0) dy = -_yZoomStart;
if (_xZoomEnd + dx > 1.0) dx = 1.0 - _xZoomEnd;
if (_yZoomEnd + dy > 1.0) dy = 1.0 - _yZoomEnd;
_xZoomStart += dx;
_xZoomEnd += dx;
_yZoomStart += dy;
_yZoomEnd += dy;
_onZoomChanged.emit();
}
|