|
| 1 | +/*************************************************************************** |
| 2 | +** ** |
| 3 | +** QCustomPlot, an easy to use, modern plotting widget for Qt ** |
| 4 | +** Copyright (C) 2011-2018 Emanuel Eichhammer ** |
| 5 | +** ** |
| 6 | +** This program is free software: you can redistribute it and/or modify ** |
| 7 | +** it under the terms of the GNU General Public License as published by ** |
| 8 | +** the Free Software Foundation, either version 3 of the License, or ** |
| 9 | +** (at your option) any later version. ** |
| 10 | +** ** |
| 11 | +** This program is distributed in the hope that it will be useful, ** |
| 12 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of ** |
| 13 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** |
| 14 | +** GNU General Public License for more details. ** |
| 15 | +** ** |
| 16 | +** You should have received a copy of the GNU General Public License ** |
| 17 | +** along with this program. If not, see http://www.gnu.org/licenses/. ** |
| 18 | +** ** |
| 19 | +**************************************************************************** |
| 20 | +** Author: Emanuel Eichhammer ** |
| 21 | +** Website/Contact: http://www.qcustomplot.com/ ** |
| 22 | +** Date: 25.06.18 ** |
| 23 | +** Version: 2.0.1 ** |
| 24 | +****************************************************************************/ |
| 25 | + |
| 26 | +#include "axistag.h" |
| 27 | + |
| 28 | +AxisTag::AxisTag(QCPAxis *parentAxis) : |
| 29 | + QObject(parentAxis), |
| 30 | + mAxis(parentAxis) |
| 31 | +{ |
| 32 | + // The dummy tracer serves here as an invisible anchor which always sticks to the right side of |
| 33 | + // the axis rect |
| 34 | + mDummyTracer = new QCPItemTracer(mAxis->parentPlot()); |
| 35 | + mDummyTracer->setVisible(false); |
| 36 | + mDummyTracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio); |
| 37 | + mDummyTracer->position->setTypeY(QCPItemPosition::ptPlotCoords); |
| 38 | + mDummyTracer->position->setAxisRect(mAxis->axisRect()); |
| 39 | + mDummyTracer->position->setAxes(0, mAxis); |
| 40 | + mDummyTracer->position->setCoords(1, 0); |
| 41 | + |
| 42 | + // the arrow end (head) is set to move along with the dummy tracer by setting it as its parent |
| 43 | + // anchor. Its coordinate system (setCoords) is thus pixels, and this is how the needed horizontal |
| 44 | + // offset for the tag of the second y axis is achieved. This horizontal offset gets dynamically |
| 45 | + // updated in AxisTag::updatePosition. the arrow "start" is simply set to have the "end" as parent |
| 46 | + // anchor. It is given a horizontal offset to the right, which results in a 15 pixel long arrow. |
| 47 | + mArrow = new QCPItemLine(mAxis->parentPlot()); |
| 48 | + mArrow->setLayer("overlay"); |
| 49 | + mArrow->setClipToAxisRect(false); |
| 50 | + mArrow->setHead(QCPLineEnding::esSpikeArrow); |
| 51 | + mArrow->end->setParentAnchor(mDummyTracer->position); |
| 52 | + mArrow->start->setParentAnchor(mArrow->end); |
| 53 | + mArrow->start->setCoords(15, 0); |
| 54 | + |
| 55 | + // The text label is anchored at the arrow start (tail) and has its "position" aligned at the |
| 56 | + // left, and vertically centered to the text label box. |
| 57 | + mLabel = new QCPItemText(mAxis->parentPlot()); |
| 58 | + mLabel->setLayer("overlay"); |
| 59 | + mLabel->setClipToAxisRect(false); |
| 60 | + mLabel->setPadding(QMargins(3, 0, 3, 0)); |
| 61 | + mLabel->setBrush(QBrush(Qt::white)); |
| 62 | + mLabel->setPen(QPen(Qt::blue)); |
| 63 | + mLabel->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter); |
| 64 | + mLabel->position->setParentAnchor(mArrow->start); |
| 65 | +} |
| 66 | + |
| 67 | +AxisTag::~AxisTag() |
| 68 | +{ |
| 69 | + if (mDummyTracer) |
| 70 | + mDummyTracer->parentPlot()->removeItem(mDummyTracer); |
| 71 | + if (mArrow) |
| 72 | + mArrow->parentPlot()->removeItem(mArrow); |
| 73 | + if (mLabel) |
| 74 | + mLabel->parentPlot()->removeItem(mLabel); |
| 75 | +} |
| 76 | + |
| 77 | +void AxisTag::setPen(const QPen &pen) |
| 78 | +{ |
| 79 | + mArrow->setPen(pen); |
| 80 | + mLabel->setPen(pen); |
| 81 | +} |
| 82 | + |
| 83 | +void AxisTag::setBrush(const QBrush &brush) |
| 84 | +{ |
| 85 | + mLabel->setBrush(brush); |
| 86 | +} |
| 87 | + |
| 88 | +void AxisTag::setText(const QString &text) |
| 89 | +{ |
| 90 | + mLabel->setText(text); |
| 91 | +} |
| 92 | + |
| 93 | +void AxisTag::updatePosition(double value) |
| 94 | +{ |
| 95 | + // since both the arrow and the text label are chained to the dummy tracer (via anchor |
| 96 | + // parent-child relationships) it is sufficient to update the dummy tracer coordinates. The |
| 97 | + // Horizontal coordinate type was set to ptAxisRectRatio so to keep it aligned at the right side |
| 98 | + // of the axis rect, it is always kept at 1. The vertical coordinate type was set to |
| 99 | + // ptPlotCoordinates of the passed parent axis, so the vertical coordinate is set to the new |
| 100 | + // value. |
| 101 | + mDummyTracer->position->setCoords(1, value); |
| 102 | + |
| 103 | + // We want the arrow head to be at the same horizontal position as the axis backbone, even if |
| 104 | + // the axis has a certain offset from the axis rect border (like the added second y axis). Thus we |
| 105 | + // set the horizontal pixel position of the arrow end (head) to the axis offset (the pixel |
| 106 | + // distance to the axis rect border). This works because the parent anchor of the arrow end is |
| 107 | + // the dummy tracer, which, as described earlier, is tied to the right axis rect border. |
| 108 | + mArrow->end->setCoords(mAxis->offset(), 0); |
| 109 | +} |
0 commit comments