2014-02-27 16:28:58 +00:00
|
|
|
#include "ruleritem.h"
|
2014-02-28 01:52:03 +00:00
|
|
|
#include "divetextitem.h"
|
|
|
|
|
2014-02-27 16:28:58 +00:00
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsScene>
|
2014-02-28 01:52:03 +00:00
|
|
|
#include <QGraphicsView>
|
2014-02-27 19:42:00 +00:00
|
|
|
#include <QDebug>
|
2014-02-27 16:28:58 +00:00
|
|
|
|
2014-02-27 17:59:41 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-02-27 16:28:58 +00:00
|
|
|
#include "profile.h"
|
|
|
|
#include "display.h"
|
|
|
|
|
2014-03-07 15:08:31 +00:00
|
|
|
RulerNodeItem2::RulerNodeItem2() : entry(NULL), ruler(NULL)
|
2014-02-27 16:28:58 +00:00
|
|
|
{
|
2014-03-07 15:08:31 +00:00
|
|
|
memset(&pInfo, 0, sizeof(pInfo));
|
2014-03-09 13:59:31 +00:00
|
|
|
setRect(-8, -8, 16, 16);
|
2014-02-27 16:28:58 +00:00
|
|
|
setBrush(QColor(0xff, 0, 0, 127));
|
2014-03-09 13:59:31 +00:00
|
|
|
setPen(QColor(Qt::red));
|
|
|
|
setFlag(ItemIsMovable);
|
2014-02-27 16:28:58 +00:00
|
|
|
setFlag(ItemSendsGeometryChanges);
|
|
|
|
setFlag(ItemIgnoresTransformations);
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:08:31 +00:00
|
|
|
void RulerNodeItem2::setPlotInfo(plot_info& info)
|
|
|
|
{
|
|
|
|
pInfo = info;
|
|
|
|
entry = pInfo.entry;
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:28:58 +00:00
|
|
|
void RulerNodeItem2::setRuler(RulerItem2 *r)
|
|
|
|
{
|
|
|
|
ruler = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RulerNodeItem2::recalculate()
|
|
|
|
{
|
2014-02-28 04:09:57 +00:00
|
|
|
struct plot_data *data = pInfo.entry + (pInfo.nr - 1);
|
2014-02-27 17:59:41 +00:00
|
|
|
uint16_t count = 0;
|
|
|
|
if (x() < 0) {
|
|
|
|
setPos(0, y());
|
|
|
|
} else if (x() > timeAxis->posAtValue(data->sec)) {
|
|
|
|
setPos(timeAxis->posAtValue(data->sec), y());
|
|
|
|
} else {
|
2014-02-27 19:28:19 +00:00
|
|
|
data = pInfo.entry;
|
2014-02-28 04:09:57 +00:00
|
|
|
count = 0;
|
2014-02-27 19:28:19 +00:00
|
|
|
while (timeAxis->posAtValue(data->sec) < x() && count < pInfo.nr) {
|
2014-02-28 04:09:57 +00:00
|
|
|
data = pInfo.entry + count;
|
2014-02-27 17:59:41 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
setPos(timeAxis->posAtValue(data->sec), depthAxis->posAtValue(data->depth));
|
2014-02-28 04:09:57 +00:00
|
|
|
entry = data;
|
2014-02-27 17:59:41 +00:00
|
|
|
}
|
2014-02-27 16:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant RulerNodeItem2::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == ItemPositionHasChanged) {
|
|
|
|
recalculate();
|
2014-03-09 13:59:31 +00:00
|
|
|
ruler->recalculate();
|
2014-02-27 16:28:58 +00:00
|
|
|
}
|
|
|
|
return QGraphicsEllipseItem::itemChange(change, value);
|
|
|
|
}
|
|
|
|
|
2014-03-10 11:43:43 +00:00
|
|
|
RulerItem2::RulerItem2() : source(new RulerNodeItem2()),
|
2014-03-07 15:08:31 +00:00
|
|
|
dest(new RulerNodeItem2()),
|
2014-03-10 11:43:43 +00:00
|
|
|
timeAxis(NULL),
|
|
|
|
depthAxis(NULL),
|
2014-03-11 22:09:53 +00:00
|
|
|
textItemBack(new QGraphicsRectItem(this)),
|
2014-02-28 01:52:03 +00:00
|
|
|
textItem(new QGraphicsSimpleTextItem(this))
|
2014-02-27 16:28:58 +00:00
|
|
|
{
|
2014-02-27 19:28:19 +00:00
|
|
|
memset(&pInfo, 0, sizeof(pInfo));
|
2014-02-27 17:59:41 +00:00
|
|
|
source->setRuler(this);
|
|
|
|
dest->setRuler(this);
|
2014-02-28 01:52:03 +00:00
|
|
|
textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
2014-03-11 22:09:53 +00:00
|
|
|
textItemBack->setBrush(QColor(0xff, 0xff, 0xff, 190));
|
|
|
|
textItemBack->setPen(QColor(Qt::white));
|
2014-02-27 16:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RulerItem2::recalculate()
|
|
|
|
{
|
|
|
|
char buffer[500];
|
|
|
|
QPointF tmp;
|
|
|
|
QFont font;
|
|
|
|
QFontMetrics fm(font);
|
|
|
|
|
2014-02-27 19:28:19 +00:00
|
|
|
if (timeAxis == NULL || depthAxis == NULL || pInfo.nr == 0)
|
2014-02-27 16:28:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
prepareGeometryChange();
|
|
|
|
startPoint = mapFromItem(source, 0, 0);
|
|
|
|
endPoint = mapFromItem(dest, 0, 0);
|
2014-02-28 01:52:03 +00:00
|
|
|
|
2014-02-27 16:28:58 +00:00
|
|
|
if (startPoint.x() > endPoint.x()) {
|
|
|
|
tmp = endPoint;
|
|
|
|
endPoint = startPoint;
|
|
|
|
startPoint = tmp;
|
|
|
|
}
|
|
|
|
QLineF line(startPoint, endPoint);
|
2014-03-09 13:59:31 +00:00
|
|
|
setLine(line);
|
2014-02-27 16:28:58 +00:00
|
|
|
compare_samples(source->entry, dest->entry, buffer, 500, 1);
|
|
|
|
text = QString(buffer);
|
|
|
|
|
2014-03-11 22:09:53 +00:00
|
|
|
// draw text
|
2014-02-28 01:52:03 +00:00
|
|
|
QGraphicsView *view = scene()->views().first();
|
|
|
|
QPoint begin = view->mapFromScene(mapToScene(startPoint));
|
|
|
|
textItem->setText(text);
|
2014-03-11 22:09:53 +00:00
|
|
|
qreal tgtX = startPoint.x();
|
|
|
|
const qreal diff = begin.x() + textItem->boundingRect().width();
|
|
|
|
// clamp so that the text doesn't go out of the screen to the right
|
|
|
|
if (diff > view->width()) {
|
|
|
|
begin.setX(begin.x() - (diff - view->width()));
|
|
|
|
tgtX = mapFromScene(view->mapToScene(begin)).x();
|
|
|
|
}
|
|
|
|
// always show the text bellow the lowest of the start and end points
|
|
|
|
qreal tgtY = (startPoint.y() >= endPoint.y()) ? startPoint.y() : endPoint.y();
|
|
|
|
textItem->setPos(tgtX - 1, tgtY + 4);
|
|
|
|
|
|
|
|
// setup the text background
|
|
|
|
textItemBack->setVisible(startPoint.x() != endPoint.x());
|
|
|
|
QPointF wh = mapFromScene(view->mapToScene(QPoint(textItem->boundingRect().width(),
|
|
|
|
textItem->boundingRect().height())));
|
|
|
|
textItemBack->setRect(tgtX - 2, tgtY + 3, wh.x() + 2, wh.y() + 3);
|
2014-02-27 16:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RulerNodeItem2 *RulerItem2::sourceNode() const
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
RulerNodeItem2 *RulerItem2::destNode() const
|
|
|
|
{
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2014-02-27 19:28:19 +00:00
|
|
|
void RulerItem2::setPlotInfo(plot_info info)
|
2014-02-27 17:59:41 +00:00
|
|
|
{
|
|
|
|
pInfo = info;
|
2014-03-07 15:08:31 +00:00
|
|
|
dest->setPlotInfo(info);
|
|
|
|
source->setPlotInfo(info);
|
2014-02-27 19:42:00 +00:00
|
|
|
dest->recalculate();
|
|
|
|
source->recalculate();
|
2014-03-07 15:08:31 +00:00
|
|
|
recalculate();
|
2014-02-27 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void RulerItem2::setAxis(DiveCartesianAxis *time, DiveCartesianAxis *depth)
|
2014-02-27 19:28:19 +00:00
|
|
|
{
|
|
|
|
timeAxis = time;
|
|
|
|
depthAxis = depth;
|
|
|
|
dest->depthAxis = depth;
|
|
|
|
dest->timeAxis = time;
|
|
|
|
source->depthAxis = depth;
|
|
|
|
source->timeAxis = time;
|
2014-02-27 17:59:41 +00:00
|
|
|
recalculate();
|
|
|
|
}
|