2014-02-05 16:25:28 +00:00
|
|
|
#include "divetooltipitem.h"
|
2014-02-05 16:53:57 +00:00
|
|
|
#include "divecartesianaxis.h"
|
2014-05-24 00:06:30 +00:00
|
|
|
#include "profilewidget2.h"
|
2014-01-19 00:21:13 +00:00
|
|
|
#include "dive.h"
|
2014-09-15 12:55:20 +00:00
|
|
|
#include "profile.h"
|
2014-01-19 00:21:13 +00:00
|
|
|
#include "membuffer.h"
|
2014-10-15 13:30:51 +00:00
|
|
|
#include "metrics.h"
|
2014-02-05 16:25:28 +00:00
|
|
|
#include <QPropertyAnimation>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QGraphicsView>
|
2015-01-16 18:17:26 +00:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2014-02-05 17:25:24 +00:00
|
|
|
#include <QDebug>
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
#define PORT_IN_PROGRESS 1
|
|
|
|
#ifdef PORT_IN_PROGRESS
|
|
|
|
#include "display.h"
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 15:02:18 +00:00
|
|
|
void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap& pixmap)
|
2014-02-05 16:25:28 +00:00
|
|
|
{
|
2014-10-15 13:30:52 +00:00
|
|
|
const IconMetrics& iconMetrics = defaultIconMetrics();
|
|
|
|
|
2015-01-14 19:11:41 +00:00
|
|
|
QGraphicsPixmapItem *iconItem = 0;
|
2014-10-15 13:30:47 +00:00
|
|
|
double yValue = title->boundingRect().height() + iconMetrics.spacing;
|
2014-05-22 18:40:22 +00:00
|
|
|
Q_FOREACH (ToolTip t, toolTips) {
|
2014-02-05 16:25:28 +00:00
|
|
|
yValue += t.second->boundingRect().height();
|
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
if (entryToolTip.second) {
|
|
|
|
yValue += entryToolTip.second->boundingRect().height();
|
|
|
|
}
|
|
|
|
iconItem = new QGraphicsPixmapItem(this);
|
2014-02-05 16:25:28 +00:00
|
|
|
if (!icon.isNull()) {
|
2015-01-14 19:11:41 +00:00
|
|
|
iconItem->setPixmap(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small));
|
|
|
|
} else if (!pixmap.isNull()) {
|
|
|
|
iconItem->setPixmap(pixmap);
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
iconItem->setPos(iconMetrics.spacing, yValue);
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
2014-10-15 13:30:52 +00:00
|
|
|
textItem->setPos(iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing, yValue);
|
2014-02-05 16:25:28 +00:00
|
|
|
textItem->setBrush(QBrush(Qt::white));
|
|
|
|
textItem->setFlag(ItemIgnoresTransformations);
|
|
|
|
toolTips.push_back(qMakePair(iconItem, textItem));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::clear()
|
|
|
|
{
|
2014-05-22 18:40:22 +00:00
|
|
|
Q_FOREACH (ToolTip t, toolTips) {
|
2014-02-05 16:25:28 +00:00
|
|
|
delete t.first;
|
|
|
|
delete t.second;
|
|
|
|
}
|
|
|
|
toolTips.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::setRect(const QRectF &r)
|
2014-02-05 16:25:28 +00:00
|
|
|
{
|
2015-01-16 18:01:54 +00:00
|
|
|
if( r == rect() ) {
|
2015-01-15 00:36:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-05 16:25:28 +00:00
|
|
|
|
2015-01-16 18:01:54 +00:00
|
|
|
QGraphicsRectItem::setRect(r);
|
2014-02-05 16:25:28 +00:00
|
|
|
updateTitlePosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::collapse()
|
|
|
|
{
|
2014-10-15 13:30:52 +00:00
|
|
|
int dim = defaultIconMetrics().sz_small;
|
|
|
|
|
2014-02-05 16:25:28 +00:00
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
|
|
|
|
animation->setDuration(100);
|
|
|
|
animation->setStartValue(nextRectangle);
|
2014-10-15 13:30:52 +00:00
|
|
|
animation->setEndValue(QRect(0, 0, dim, dim));
|
2014-02-05 16:25:28 +00:00
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
clear();
|
|
|
|
|
|
|
|
status = COLLAPSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::expand()
|
|
|
|
{
|
|
|
|
if (!title)
|
|
|
|
return;
|
|
|
|
|
2014-10-15 13:30:52 +00:00
|
|
|
const IconMetrics& iconMetrics = defaultIconMetrics();
|
|
|
|
|
2014-10-15 13:30:47 +00:00
|
|
|
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
|
2015-01-14 19:11:41 +00:00
|
|
|
Q_FOREACH (const ToolTip& t, toolTips) {
|
2014-02-05 16:25:28 +00:00
|
|
|
if (t.second->boundingRect().width() > width)
|
|
|
|
width = t.second->boundingRect().width();
|
|
|
|
height += t.second->boundingRect().height();
|
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
|
|
|
|
if (entryToolTip.first) {
|
|
|
|
if (entryToolTip.second->boundingRect().width() > width)
|
|
|
|
width = entryToolTip.second->boundingRect().width();
|
|
|
|
height += entryToolTip.second->boundingRect().height();
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:25:28 +00:00
|
|
|
/* Left padding, Icon Size, space, right padding */
|
2014-10-15 13:30:52 +00:00
|
|
|
width += iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing + iconMetrics.spacing;
|
2014-02-05 16:25:28 +00:00
|
|
|
|
2014-10-15 13:30:47 +00:00
|
|
|
if (width < title->boundingRect().width() + iconMetrics.spacing * 2)
|
|
|
|
width = title->boundingRect().width() + iconMetrics.spacing * 2;
|
2014-02-05 16:25:28 +00:00
|
|
|
|
2014-10-15 13:30:52 +00:00
|
|
|
if (height < iconMetrics.sz_small)
|
|
|
|
height = iconMetrics.sz_small;
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
nextRectangle.setWidth(width);
|
|
|
|
nextRectangle.setHeight(height);
|
|
|
|
|
2015-01-16 18:01:54 +00:00
|
|
|
if (nextRectangle != rect()) {
|
2015-01-15 00:30:14 +00:00
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this);
|
|
|
|
animation->setDuration(100);
|
2015-01-16 18:01:54 +00:00
|
|
|
animation->setStartValue(rect());
|
2015-01-15 00:30:14 +00:00
|
|
|
animation->setEndValue(nextRectangle);
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
}
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
status = EXPANDED;
|
|
|
|
}
|
|
|
|
|
2015-01-16 18:01:54 +00:00
|
|
|
ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent),
|
2014-02-09 18:04:59 +00:00
|
|
|
title(new QGraphicsSimpleTextItem(tr("Information"), this)),
|
|
|
|
status(COLLAPSED),
|
2014-02-20 00:06:16 +00:00
|
|
|
timeAxis(0),
|
|
|
|
lastTime(-1)
|
2014-02-05 16:25:28 +00:00
|
|
|
{
|
2014-02-09 18:04:59 +00:00
|
|
|
memset(&pInfo, 0, sizeof(pInfo));
|
2015-01-14 19:11:41 +00:00
|
|
|
entryToolTip.first = NULL;
|
|
|
|
entryToolTip.second = NULL;
|
2014-02-05 16:25:28 +00:00
|
|
|
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
|
2015-01-15 00:50:08 +00:00
|
|
|
|
|
|
|
QColor c = QColor(Qt::black);
|
|
|
|
c.setAlpha(155);
|
2015-01-16 18:06:14 +00:00
|
|
|
setBrush(c);
|
|
|
|
setPen(QPen(QBrush(Qt::transparent), 0));
|
2015-01-15 00:50:08 +00:00
|
|
|
|
2014-02-05 16:25:28 +00:00
|
|
|
setZValue(99);
|
2015-01-14 19:11:41 +00:00
|
|
|
|
|
|
|
addToolTip(QString(), QIcon(), QPixmap(16,60));
|
|
|
|
entryToolTip = toolTips.first();
|
|
|
|
toolTips.clear();
|
|
|
|
|
|
|
|
title->setFlag(ItemIgnoresTransformations);
|
|
|
|
title->setPen(QPen(Qt::white, 1));
|
|
|
|
title->setBrush(Qt::white);
|
2015-01-15 00:50:08 +00:00
|
|
|
|
|
|
|
setBrush(QBrush(Qt::white));
|
|
|
|
setPen(QPen(Qt::black, 0.5));
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipItem::~ToolTipItem()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::updateTitlePosition()
|
|
|
|
{
|
2014-10-15 13:30:52 +00:00
|
|
|
const IconMetrics& iconMetrics = defaultIconMetrics();
|
2015-01-16 18:01:54 +00:00
|
|
|
if (rect().width() < title->boundingRect().width() + iconMetrics.spacing * 4) {
|
|
|
|
QRectF newRect = rect();
|
2014-10-15 13:30:47 +00:00
|
|
|
newRect.setWidth(title->boundingRect().width() + iconMetrics.spacing * 4);
|
2014-10-15 13:30:52 +00:00
|
|
|
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.sz_small);
|
2014-02-05 16:25:28 +00:00
|
|
|
setRect(newRect);
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0);
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
bool ToolTipItem::isExpanded() const
|
|
|
|
{
|
2014-02-05 16:25:28 +00:00
|
|
|
return status == EXPANDED;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2014-02-05 16:25:28 +00:00
|
|
|
{
|
|
|
|
persistPos();
|
2015-01-16 18:01:54 +00:00
|
|
|
QGraphicsRectItem::mouseReleaseEvent(event);
|
2014-05-24 00:06:30 +00:00
|
|
|
Q_FOREACH (QGraphicsItem *item, oldSelection) {
|
|
|
|
item->setSelected(true);
|
|
|
|
}
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 18:17:26 +00:00
|
|
|
void ToolTipItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
painter->save();
|
|
|
|
painter->setClipRect(option->rect);
|
|
|
|
painter->setPen(pen());
|
|
|
|
painter->setBrush(brush());
|
|
|
|
painter->drawRoundedRect(rect(), 10, 10, Qt::AbsoluteSize);
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2014-02-05 16:25:28 +00:00
|
|
|
void ToolTipItem::persistPos()
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("ProfileMap");
|
2014-03-13 17:23:20 +00:00
|
|
|
s.setValue("tooltip_position", pos());
|
2014-02-05 16:25:28 +00:00
|
|
|
s.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::readPos()
|
|
|
|
{
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup("ProfileMap");
|
2014-03-13 17:23:20 +00:00
|
|
|
QPointF value = s.value("tooltip_position").toPoint();
|
2014-02-05 16:25:28 +00:00
|
|
|
if (!scene()->sceneRect().contains(value)) {
|
2014-02-28 04:09:57 +00:00
|
|
|
value = QPointF(0, 0);
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
setPos(value);
|
|
|
|
}
|
2014-02-05 16:53:57 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::setPlotInfo(const plot_info &plot)
|
2014-02-05 16:53:57 +00:00
|
|
|
{
|
|
|
|
pInfo = plot;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis)
|
2014-02-05 16:53:57 +00:00
|
|
|
{
|
|
|
|
timeAxis = axis;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::refresh(const QPointF &pos)
|
2014-02-05 16:53:57 +00:00
|
|
|
{
|
2014-09-19 06:48:19 +00:00
|
|
|
struct plot_data *entry;
|
2015-01-14 15:02:18 +00:00
|
|
|
static QPixmap tissues(16,60);
|
|
|
|
static QPainter painter(&tissues);
|
2015-01-15 00:18:20 +00:00
|
|
|
static struct membuffer mb = { 0 };
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
int time = timeAxis->valueAt(pos);
|
2014-02-20 00:06:16 +00:00
|
|
|
if (time == lastTime)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lastTime = time;
|
|
|
|
clear();
|
2014-01-19 00:21:13 +00:00
|
|
|
|
2015-01-15 00:18:20 +00:00
|
|
|
mb.len = 0;
|
2014-09-19 06:48:19 +00:00
|
|
|
entry = get_plot_details_new(&pInfo, time, &mb);
|
2014-09-20 16:41:36 +00:00
|
|
|
if (entry) {
|
2015-01-14 15:02:18 +00:00
|
|
|
tissues.fill();
|
|
|
|
painter.setPen(QColor(0, 0, 0, 0));
|
|
|
|
painter.setBrush(QColor(LIMENADE1));
|
|
|
|
painter.drawRect(0, 10 + (100 - AMB_PERCENTAGE) / 2, 16, AMB_PERCENTAGE / 2);
|
|
|
|
painter.setBrush(QColor(SPRINGWOOD1));
|
|
|
|
painter.drawRect(0, 10, 16, (100 - AMB_PERCENTAGE) / 2);
|
|
|
|
painter.setBrush(QColor(Qt::red));
|
|
|
|
painter.drawRect(0,0,16,10);
|
|
|
|
painter.setPen(QColor(0, 0, 0, 255));
|
|
|
|
painter.drawLine(0, 60 - entry->gfline / 2, 16, 60 - entry->gfline / 2);
|
|
|
|
painter.drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2,
|
2014-09-20 16:41:36 +00:00
|
|
|
16, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure /2);
|
2015-01-14 15:02:18 +00:00
|
|
|
painter.setPen(QColor(0, 0, 0, 127));
|
2015-01-14 19:11:41 +00:00
|
|
|
for (int i=0; i<16; i++) {
|
2015-01-14 15:02:18 +00:00
|
|
|
painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2);
|
2014-09-20 16:41:36 +00:00
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
entryToolTip.first->setPixmap(tissues);
|
|
|
|
entryToolTip.second->setText(QString::fromUtf8(mb.buffer, mb.len));
|
2014-09-19 06:48:19 +00:00
|
|
|
}
|
2014-02-05 16:53:57 +00:00
|
|
|
|
2015-01-14 14:52:23 +00:00
|
|
|
Q_FOREACH (QGraphicsItem *item, scene()->items(pos, Qt::IntersectsItemBoundingRect
|
2014-07-15 17:43:20 +00:00
|
|
|
,Qt::DescendingOrder, scene()->views().first()->transform())) {
|
2014-02-05 16:53:57 +00:00
|
|
|
if (!item->toolTip().isEmpty())
|
|
|
|
addToolTip(item->toolTip());
|
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
expand();
|
2014-02-05 16:53:57 +00:00
|
|
|
}
|
2014-05-24 00:06:30 +00:00
|
|
|
|
|
|
|
void ToolTipItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
oldSelection = scene()->selectedItems();
|
|
|
|
scene()->clearSelection();
|
|
|
|
QGraphicsItem::mousePressEvent(event);
|
|
|
|
}
|