2017-04-27 18:26:36 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "profile-widget/divetooltipitem.h"
|
|
|
|
#include "profile-widget/divecartesianaxis.h"
|
|
|
|
#include "core/profile.h"
|
|
|
|
#include "core/membuffer.h"
|
|
|
|
#include "core/metrics.h"
|
2018-08-20 09:51:48 +00:00
|
|
|
#include "core/settings/qPrefDisplay.h"
|
2014-02-05 16:25:28 +00:00
|
|
|
#include <QPropertyAnimation>
|
|
|
|
#include <QGraphicsView>
|
2017-01-07 02:01:14 +00:00
|
|
|
#include "core/qthelper.h"
|
2014-02-05 16:25:28 +00:00
|
|
|
|
2021-02-12 17:32:40 +00:00
|
|
|
void ToolTipItem::addToolTip(const QString &toolTip, const QPixmap &pixmap)
|
2014-02-05 16:25:28 +00:00
|
|
|
{
|
2021-02-12 17:22:56 +00:00
|
|
|
const IconMetrics &iconMetrics = defaultIconMetrics();
|
2014-10-15 13:30:52 +00:00
|
|
|
|
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);
|
2021-02-12 17:32:40 +00:00
|
|
|
if (!pixmap.isNull())
|
2015-01-14 19:11:41 +00:00
|
|
|
iconItem->setPixmap(pixmap);
|
2015-11-08 13:41:42 +00:00
|
|
|
const int sp2 = iconMetrics.spacing * 2;
|
|
|
|
iconItem->setPos(sp2, yValue);
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
2015-11-08 13:41:42 +00:00
|
|
|
textItem->setPos(sp2 + iconMetrics.sz_small + sp2, 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
|
|
|
{
|
2021-02-12 17:22:56 +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;
|
|
|
|
|
2018-08-20 09:51:48 +00:00
|
|
|
if (qPrefDisplay::animation_speed()) {
|
2015-01-16 18:39:14 +00:00
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
|
|
|
|
animation->setDuration(100);
|
|
|
|
animation->setStartValue(nextRectangle);
|
|
|
|
animation->setEndValue(QRect(0, 0, dim, dim));
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
} else {
|
|
|
|
setRect(nextRectangle);
|
|
|
|
}
|
2014-02-05 16:25:28 +00:00
|
|
|
clear();
|
|
|
|
|
|
|
|
status = COLLAPSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::expand()
|
|
|
|
{
|
|
|
|
if (!title)
|
|
|
|
return;
|
|
|
|
|
2021-02-12 17:22:56 +00:00
|
|
|
const IconMetrics &iconMetrics = defaultIconMetrics();
|
2014-10-15 13:30:52 +00:00
|
|
|
|
2014-10-15 13:30:47 +00:00
|
|
|
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
|
2021-02-12 17:22:56 +00:00
|
|
|
Q_FOREACH (const ToolTip &t, toolTips) {
|
2015-01-16 18:29:07 +00:00
|
|
|
QRectF sRect = t.second->boundingRect();
|
|
|
|
if (sRect.width() > width)
|
|
|
|
width = sRect.width();
|
|
|
|
height += sRect.height();
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
2015-01-14 19:11:41 +00:00
|
|
|
|
|
|
|
if (entryToolTip.first) {
|
2015-01-16 18:29:07 +00:00
|
|
|
QRectF sRect = entryToolTip.second->boundingRect();
|
|
|
|
if (sRect.width() > width)
|
|
|
|
width = sRect.width();
|
|
|
|
height += sRect.height();
|
2015-01-14 19:11:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 13:41:42 +00:00
|
|
|
const int sp2 = iconMetrics.spacing * 2;
|
2021-02-12 17:22:56 +00:00
|
|
|
// pixmap left padding, icon, pixmap right padding, right padding
|
2015-11-08 13:41:42 +00:00
|
|
|
width += sp2 + iconMetrics.sz_small + sp2 + sp2 * 2;
|
|
|
|
// bottom padding
|
|
|
|
height += sp2;
|
2014-02-05 16:25:28 +00:00
|
|
|
|
2015-11-08 13:41:42 +00:00
|
|
|
// clip the tooltip width
|
|
|
|
if (width < title->boundingRect().width() + sp2)
|
|
|
|
width = title->boundingRect().width() + sp2;
|
|
|
|
// clip the height
|
|
|
|
if (entryToolTip.first) {
|
2017-03-23 01:13:49 +00:00
|
|
|
const int minH = lrint(entryToolTip.first->y() + entryToolTip.first->pixmap().height() + sp2);
|
2015-11-08 13:41:42 +00:00
|
|
|
if (height < minH)
|
|
|
|
height = minH;
|
|
|
|
} else if (height < iconMetrics.sz_small) {
|
2014-10-15 13:30:52 +00:00
|
|
|
height = iconMetrics.sz_small;
|
2015-11-08 13:41:42 +00:00
|
|
|
}
|
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()) {
|
2018-08-20 09:51:48 +00:00
|
|
|
if (qPrefDisplay::animation_speed()) {
|
2015-01-16 18:39:14 +00:00
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this);
|
|
|
|
animation->setDuration(prefs.animation_speed);
|
|
|
|
animation->setStartValue(rect());
|
|
|
|
animation->setEndValue(nextRectangle);
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
} else {
|
|
|
|
setRect(nextRectangle);
|
|
|
|
}
|
2015-01-15 00:30:14 +00:00
|
|
|
}
|
2014-02-05 16:25:28 +00:00
|
|
|
|
|
|
|
status = EXPANDED;
|
|
|
|
}
|
|
|
|
|
2021-01-03 11:49:26 +00:00
|
|
|
ToolTipItem::ToolTipItem(QGraphicsItem *parent) : RoundRectItem(8.0, 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
|
|
|
{
|
2019-11-15 19:50:44 +00:00
|
|
|
clearPlotInfo();
|
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);
|
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
|
|
|
|
2021-02-12 17:32:40 +00:00
|
|
|
addToolTip(QString(), QPixmap(16,60));
|
2015-01-14 19:11:41 +00:00
|
|
|
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
|
|
|
|
2015-01-16 18:19:28 +00:00
|
|
|
setPen(QPen(Qt::white, 2));
|
2015-01-16 18:45:13 +00:00
|
|
|
refreshTime.start();
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipItem::~ToolTipItem()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::updateTitlePosition()
|
|
|
|
{
|
2021-02-12 17:22:56 +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);
|
|
|
|
}
|
|
|
|
|
2015-01-16 18:29:07 +00:00
|
|
|
title->setPos(rect().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
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::persistPos()
|
|
|
|
{
|
2018-08-15 15:04:51 +00:00
|
|
|
qPrefDisplay::set_tooltip_position(pos());
|
2014-02-05 16:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipItem::readPos()
|
|
|
|
{
|
2018-08-15 15:04:51 +00:00
|
|
|
QPointF value = qPrefDisplay::tooltip_position();
|
2021-02-12 17:22:56 +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;
|
|
|
|
}
|
|
|
|
|
2019-11-15 19:50:44 +00:00
|
|
|
void ToolTipItem::clearPlotInfo()
|
|
|
|
{
|
|
|
|
memset(&pInfo, 0, sizeof(pInfo));
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis)
|
2014-02-05 16:53:57 +00:00
|
|
|
{
|
|
|
|
timeAxis = axis;
|
|
|
|
}
|
|
|
|
|
2021-02-12 17:19:24 +00:00
|
|
|
void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner)
|
2014-02-05 16:53:57 +00:00
|
|
|
{
|
2015-01-14 15:02:18 +00:00
|
|
|
static QPixmap tissues(16,60);
|
|
|
|
static QPainter painter(&tissues);
|
2016-03-09 18:15:53 +00:00
|
|
|
static struct membuffer mb = {};
|
2015-01-15 00:18:20 +00:00
|
|
|
|
2015-01-16 18:45:13 +00:00
|
|
|
if(refreshTime.elapsed() < 40)
|
|
|
|
return;
|
|
|
|
refreshTime.start();
|
|
|
|
|
2017-03-23 01:13:49 +00:00
|
|
|
int time = lrint(timeAxis->valueAt(pos));
|
2014-02-20 00:06:16 +00:00
|
|
|
|
|
|
|
lastTime = time;
|
|
|
|
clear();
|
2014-01-19 00:21:13 +00:00
|
|
|
|
2015-01-15 00:18:20 +00:00
|
|
|
mb.len = 0;
|
2021-01-09 17:11:56 +00:00
|
|
|
int idx = get_plot_details_new(d, &pInfo, time, &mb);
|
2016-10-01 15:38:12 +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);
|
2019-07-06 09:58:27 +00:00
|
|
|
if (idx) {
|
2016-11-01 16:00:06 +00:00
|
|
|
ProfileWidget2 *view = qobject_cast<ProfileWidget2*>(scene()->views().first());
|
|
|
|
Q_ASSERT(view);
|
|
|
|
|
2019-07-06 09:58:27 +00:00
|
|
|
const struct plot_data *entry = &pInfo.entry[idx];
|
2015-01-14 15:02:18 +00:00
|
|
|
painter.setPen(QColor(0, 0, 0, 255));
|
2021-02-12 17:19:24 +00:00
|
|
|
if (decoMode(inPlanner) == BUEHLMANN)
|
2017-03-23 01:13:49 +00:00
|
|
|
painter.drawLine(0, lrint(60 - entry->gfline / 2), 16, lrint(60 - entry->gfline / 2));
|
|
|
|
painter.drawLine(0, lrint(60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2),
|
|
|
|
16, lrint(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));
|
2021-02-12 17:22:56 +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);
|
2015-01-14 19:11:41 +00:00
|
|
|
entryToolTip.second->setText(QString::fromUtf8(mb.buffer, mb.len));
|
2014-09-19 06:48:19 +00:00
|
|
|
}
|
2016-10-01 15:38:12 +00:00
|
|
|
entryToolTip.first->setPixmap(tissues);
|
2014-02-05 16:53:57 +00:00
|
|
|
|
2019-04-01 20:15:19 +00:00
|
|
|
const auto l = scene()->items(pos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder,
|
|
|
|
scene()->views().first()->transform());
|
|
|
|
for (QGraphicsItem *item: l) {
|
2014-02-05 16:53:57 +00:00
|
|
|
if (!item->toolTip().isEmpty())
|
2021-02-12 17:32:40 +00:00
|
|
|
addToolTip(item->toolTip(), QPixmap());
|
2014-02-05 16:53:57 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|