From 49f4052c67d31d4870a86f53a52381a4bbf609fd Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Tue, 14 Jan 2014 16:20:15 -0200 Subject: Cartesian Axis, based on the Ruler class on the Dive Planner. This is the same class as the Ruler, but uses the DiveLineItem and DiveTextItem classes created to make it animateable. The next few commits will work on that part. The Ruler was a very bad name for a class that's actually an Axis, that's why I depreceated the later. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- qt-ui/profile/divecartesianaxis.cpp | 160 ++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 qt-ui/profile/divecartesianaxis.cpp (limited to 'qt-ui/profile/divecartesianaxis.cpp') diff --git a/qt-ui/profile/divecartesianaxis.cpp b/qt-ui/profile/divecartesianaxis.cpp new file mode 100644 index 000000000..78ef38e68 --- /dev/null +++ b/qt-ui/profile/divecartesianaxis.cpp @@ -0,0 +1,160 @@ +#include "divecartesianaxis.h" +#include "divelineitem.h" +#include "divetextitem.h" +#include "helpers.h" + +#include +#include +#include +#include +#include +#include + +void DiveCartesianAxis::setMaximum(double maximum) +{ + max = maximum; +} + +void DiveCartesianAxis::setMinimum(double minimum) +{ + min = minimum; +} + +void DiveCartesianAxis::setTextColor(const QColor& color) +{ + textColor = color; +} + +DiveCartesianAxis::DiveCartesianAxis() : orientation(Qt::Horizontal) +{ +} + +DiveCartesianAxis::~DiveCartesianAxis() +{ + +} + +void DiveCartesianAxis::setOrientation(Qt::Orientation o) +{ + orientation = o; + // position the elements on the screen. + setMinimum(minimum()); + setMaximum(maximum()); +} + +void DiveCartesianAxis::updateTicks() +{ + qDeleteAll(ticks); + ticks.clear(); + qDeleteAll(labels); + labels.clear(); + + QLineF m = line(); + DiveLineItem *item = NULL; + DiveTextItem *label = NULL; + + double steps = (max - min) / interval; + qreal pos; + double currValue = min; + + if (orientation == Qt::Horizontal) { + double stepSize = (m.x2() - m.x1()) / steps; + for (pos = m.x1(); pos <= m.x2(); pos += stepSize, currValue += interval) { + item = new DiveLineItem(this); + item->setLine(pos, m.y1(), pos, m.y1() + tickSize); + item->setPen(pen()); + ticks.push_back(item); + + label = new DiveTextItem(this); + label->setText(QString::number(currValue)); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5); + labels.push_back(label); + } + } else { + double stepSize = (m.y2() - m.y1()) / steps; + for (pos = m.y1(); pos <= m.y2(); pos += stepSize, currValue += interval) { + item = new DiveLineItem(this); + item->setLine(m.x1(), pos, m.x1() - tickSize, pos); + item->setPen(pen()); + ticks.push_back(item); + + label = new DiveTextItem(this); + label->setText(get_depth_string(currValue, false, false)); + label->setBrush(QBrush(textColor)); + label->setFlag(ItemIgnoresTransformations); + label->setPos(m.x2() - 80, pos); + labels.push_back(label); + } + } +} + +QString DiveCartesianAxis::textForValue(double value) +{ + return QString::number(value); +} + +void DiveCartesianAxis::setTickSize(qreal size) +{ + tickSize = size; +} + +void DiveCartesianAxis::setTickInterval(double i) +{ + interval = i; +} + +qreal DiveCartesianAxis::valueAt(const QPointF& p) +{ + QLineF m = line(); + double retValue = orientation == Qt::Horizontal ? + max * (p.x() - m.x1()) / (m.x2() - m.x1()) : + max * (p.y() - m.y1()) / (m.y2() - m.y1()); + return retValue; +} + +qreal DiveCartesianAxis::posAtValue(qreal value) +{ + QLineF m = line(); + QPointF p = pos(); + + double size = max - min; + double percent = value / size; + double realSize = orientation == Qt::Horizontal ? + m.x2() - m.x1() : + m.y2() - m.y1(); + double retValue = realSize * percent; + retValue = (orientation == Qt::Horizontal) ? + retValue + m.x1() + p.x(): + retValue + m.y1() + p.y(); + return retValue; +} + +qreal DiveCartesianAxis::percentAt(const QPointF& p) +{ + qreal value = valueAt(p); + double size = max - min; + double percent = value / size; + return percent; +} + +double DiveCartesianAxis::maximum() const +{ + return max; +} + +double DiveCartesianAxis::minimum() const +{ + return min; +} + +void DiveCartesianAxis::setColor(const QColor& color) +{ + QPen defaultPen(color); + defaultPen.setJoinStyle(Qt::RoundJoin); + defaultPen.setCapStyle(Qt::RoundCap); + defaultPen.setWidth(2); + defaultPen.setCosmetic(true); + setPen(defaultPen); +} -- cgit v1.2.3-70-g09d2