|
|
|
|
@ -14,11 +14,17 @@
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <QFontMetricsF>
|
|
|
|
|
#include <QLocale>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QPainterPath>
|
|
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
ZX_DEFINE_DYNAMIC(mPlotView, nmPlotView)
|
|
|
|
|
|
|
|
|
|
nmPlotView::nmPlotView()
|
|
|
|
|
: m_mapHistoryLocked(false)
|
|
|
|
|
: m_scaleBarVisible(true), m_mapHistoryLocked(false)
|
|
|
|
|
{
|
|
|
|
|
// 未按下鼠标时也接收视口移动事件,实时更新模型位置。
|
|
|
|
|
viewport()->setMouseTracking(true);
|
|
|
|
|
@ -26,6 +32,88 @@ nmPlotView::nmPlotView()
|
|
|
|
|
connect(&m_mapHistories, SIGNAL(canRedoChanged(bool)), this, SLOT(updateMapHistoryAvailability()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmPlotView::setScaleBarVisible(bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (m_scaleBarVisible == visible) return;
|
|
|
|
|
m_scaleBarVisible = visible;
|
|
|
|
|
updateViewport();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmPlotView::paintOverlay(QPainter* painter)
|
|
|
|
|
{
|
|
|
|
|
tCurvePlotView::paintOverlay(painter);
|
|
|
|
|
if (!m_scaleBarVisible) return;
|
|
|
|
|
|
|
|
|
|
const nmPlotScene* plotScene = dynamic_cast<const nmPlotScene*>(getScene());
|
|
|
|
|
if (plotScene == nullptr || plotScene->m_pPlot == nullptr) return;
|
|
|
|
|
ZxPlot* plot = plotScene->m_pPlot;
|
|
|
|
|
const ZxSubAxisX* axis = plot->getMainAxisX();
|
|
|
|
|
if (axis == nullptr) return;
|
|
|
|
|
|
|
|
|
|
const QRectF area = mapFromScene(plot->getInnerRectF())
|
|
|
|
|
.intersected(QRectF(viewport()->rect()));
|
|
|
|
|
const qreal margin = 10.0;
|
|
|
|
|
const qreal tickHeight = 5.0;
|
|
|
|
|
const qreal labelGap = 2.0;
|
|
|
|
|
const QFont labelFont = font();
|
|
|
|
|
const QFontMetricsF metrics(labelFont, painter->device());
|
|
|
|
|
const qreal maxWidth = qMin(qreal(120.0), area.width() - 4.0 * margin);
|
|
|
|
|
if (maxWidth < 24.0 || area.height() < 2.0 * margin + tickHeight + labelGap + metrics.height()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按视口横向距离反算地图坐标,独立响应水平和竖直缩放。
|
|
|
|
|
const QPointF start = mapToScene(QPointF(area.left(), area.center().y()));
|
|
|
|
|
const QPointF end = mapToScene(QPointF(area.left() + maxWidth, area.center().y()));
|
|
|
|
|
const double maxDistance = qAbs(axis->getValueForPos(end.x()) - axis->getValueForPos(start.x()));
|
|
|
|
|
if (!qIsFinite(maxDistance) || maxDistance <= 0.0) return;
|
|
|
|
|
|
|
|
|
|
// 在可用宽度内取整齐的 1、2、5 倍十进制刻度。
|
|
|
|
|
const double decade = std::pow(10.0, std::floor(std::log10(maxDistance)));
|
|
|
|
|
if (!qIsFinite(decade) || decade <= 0.0) return;
|
|
|
|
|
const double leading = maxDistance / decade;
|
|
|
|
|
const double distance = decade * (leading >= 5.0 ? 5.0 : (leading >= 2.0 ? 2.0 : 1.0));
|
|
|
|
|
const qreal width = maxWidth * (distance / maxDistance);
|
|
|
|
|
const bool useKilometres = distance >= 1000.0;
|
|
|
|
|
const QString zero = QLatin1String("0");
|
|
|
|
|
const QString label = QLocale().toString(useKilometres ? distance / 1000.0 : distance, 'g', 6)
|
|
|
|
|
+ (useKilometres ? QLatin1String(" km") : QLatin1String(" m"));
|
|
|
|
|
const qreal startLabelWidth = metrics.width(zero);
|
|
|
|
|
const qreal endLabelWidth = metrics.width(label);
|
|
|
|
|
if (width < (startLabelWidth + endLabelWidth) / 2.0 + labelGap ||
|
|
|
|
|
width + (startLabelWidth + endLabelWidth) / 2.0 + 2.0 * margin > area.width()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const qreal right = area.right() - margin - endLabelWidth / 2.0;
|
|
|
|
|
const qreal left = right - width;
|
|
|
|
|
const qreal bottom = area.bottom() - margin;
|
|
|
|
|
const qreal top = bottom - tickHeight;
|
|
|
|
|
QPainterPath bar;
|
|
|
|
|
bar.moveTo(left, top);
|
|
|
|
|
bar.lineTo(left, bottom);
|
|
|
|
|
bar.lineTo(right, bottom);
|
|
|
|
|
bar.lineTo(right, top);
|
|
|
|
|
|
|
|
|
|
const qreal baseline = top - labelGap - metrics.descent();
|
|
|
|
|
QPainterPath labels;
|
|
|
|
|
labels.addText(QPointF(left - startLabelWidth / 2.0, baseline), labelFont, zero);
|
|
|
|
|
labels.addText(QPointF(right - endLabelWidth / 2.0, baseline), labelFont, label);
|
|
|
|
|
|
|
|
|
|
// 用视口像素绘制,白色描边使刻度在底图和填充区上都能读清。
|
|
|
|
|
const QColor ink(32, 32, 32);
|
|
|
|
|
const QPen halo(QBrush(Qt::white), 3.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
|
|
|
|
|
painter->save();
|
|
|
|
|
painter->resetTransform();
|
|
|
|
|
painter->setClipRect(area, Qt::IntersectClip);
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
painter->strokePath(bar, halo);
|
|
|
|
|
painter->strokePath(bar, QPen(ink));
|
|
|
|
|
painter->strokePath(labels, halo);
|
|
|
|
|
painter->fillPath(labels, ink);
|
|
|
|
|
painter->restore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void nmPlotView::setMapHistoryOwner(QObject* analysis)
|
|
|
|
|
{
|
|
|
|
|
QUndoStack* stack = m_analysisHistories.value(analysis, nullptr);
|
|
|
|
|
|