diff options
author | Tomaz Canabrava <tcanabrava@kde.org> | 2014-01-14 16:20:15 -0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-01-16 10:12:29 +0700 |
commit | 49f4052c67d31d4870a86f53a52381a4bbf609fd (patch) | |
tree | 53ef8e50600ae29eb3564dc89e77a97a0a9199a2 /qt-ui | |
parent | ca07f45561aecf788dfbbbda9e36135f6fae69d2 (diff) | |
download | subsurface-49f4052c67d31d4870a86f53a52381a4bbf609fd.tar.gz |
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 <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r-- | qt-ui/profile/divecartesianaxis.cpp | 160 | ||||
-rw-r--r-- | qt-ui/profile/divecartesianaxis.h | 46 |
2 files changed, 206 insertions, 0 deletions
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 <QPen> +#include <QGraphicsScene> +#include <QDebug> +#include <QPropertyAnimation> +#include <QGraphicsView> +#include <QStyleOption> + +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); +} diff --git a/qt-ui/profile/divecartesianaxis.h b/qt-ui/profile/divecartesianaxis.h new file mode 100644 index 000000000..1c8170ef4 --- /dev/null +++ b/qt-ui/profile/divecartesianaxis.h @@ -0,0 +1,46 @@ +#ifndef DIVECARTESIANAXIS_H +#define DIVECARTESIANAXIS_H + +#include <QObject> +#include <QGraphicsLineItem> +class DiveTextItem; +class DiveLineItem; + +class DiveCartesianAxis : public QObject, public QGraphicsLineItem{ + Q_OBJECT + Q_PROPERTY(QLineF line WRITE setLine READ line) + Q_PROPERTY(QPointF pos WRITE setPos READ pos) + Q_PROPERTY(qreal x WRITE setX READ x) + Q_PROPERTY(qreal y WRITE setY READ y) +public: + DiveCartesianAxis(); + virtual ~DiveCartesianAxis(); + void setMinimum(double minimum); + void setMaximum(double maximum); + void setTickInterval(double interval); + void setOrientation(Qt::Orientation orientation); + void setTickSize(qreal size); + void updateTicks(); + double minimum() const; + double maximum() const; + qreal valueAt(const QPointF& p); + qreal percentAt(const QPointF& p); + qreal posAtValue(qreal value); + void setColor(const QColor& color); + void setTextColor(const QColor& color); + int unitSystem; + +protected: + virtual QString textForValue(double value); + + Qt::Orientation orientation; + QList<DiveLineItem*> ticks; + QList<DiveTextItem*> labels; + double min; + double max; + double interval; + double tickSize; + QColor textColor; +}; + +#endif
\ No newline at end of file |