profile: first rudimentary port of the ToolTipItem to qt-quick

Still behaves weirdly when panning the chart.

No support for moving the ToolTipItem.

Doesn't add information on bookmarks under the mouse cursor.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-06-16 18:24:22 +02:00
parent e3767976a3
commit 13c9218ecf
14 changed files with 260 additions and 48 deletions

View file

@ -128,7 +128,6 @@ SOURCES += subsurface-mobile-main.cpp \
core/subsurface-qt/divelistnotifier.cpp \ core/subsurface-qt/divelistnotifier.cpp \
backend-shared/exportfuncs.cpp \ backend-shared/exportfuncs.cpp \
backend-shared/plannershared.cpp \ backend-shared/plannershared.cpp \
backend-shared/roundrectitem.cpp \
qt-quick/chartitem.cpp \ qt-quick/chartitem.cpp \
qt-quick/chartview.cpp \ qt-quick/chartview.cpp \
stats/statsvariables.cpp \ stats/statsvariables.cpp \
@ -179,8 +178,8 @@ SOURCES += subsurface-mobile-main.cpp \
profile-widget/animationfunctions.cpp \ profile-widget/animationfunctions.cpp \
profile-widget/divepixmapcache.cpp \ profile-widget/divepixmapcache.cpp \
profile-widget/divepixmapitem.cpp \ profile-widget/divepixmapitem.cpp \
profile-widget/divetooltipitem.cpp \
profile-widget/tankitem.cpp \ profile-widget/tankitem.cpp \
profile-widget/tooltipitem.cpp \
profile-widget/divelineitem.cpp \ profile-widget/divelineitem.cpp \
profile-widget/diverectitem.cpp \ profile-widget/diverectitem.cpp \
profile-widget/divetextitem.cpp \ profile-widget/divetextitem.cpp \
@ -287,7 +286,6 @@ HEADERS += \
core/subsurface-qt/divelistnotifier.h \ core/subsurface-qt/divelistnotifier.h \
backend-shared/exportfuncs.h \ backend-shared/exportfuncs.h \
backend-shared/plannershared.h \ backend-shared/plannershared.h \
backend-shared/roundrectitem.h \
qt-quick/chartitem.h \ qt-quick/chartitem.h \
qt-quick/chartitemhelper.h \ qt-quick/chartitemhelper.h \
qt-quick/chartitem_ptr.h \ qt-quick/chartitem_ptr.h \
@ -340,8 +338,8 @@ HEADERS += \
profile-widget/diveprofileitem.h \ profile-widget/diveprofileitem.h \
profile-widget/profilescene.h \ profile-widget/profilescene.h \
profile-widget/diveeventitem.h \ profile-widget/diveeventitem.h \
profile-widget/divetooltipitem.h \
profile-widget/tankitem.h \ profile-widget/tankitem.h \
profile-widget/tooltipitem.h \
profile-widget/animationfunctions.h \ profile-widget/animationfunctions.h \
profile-widget/divecartesianaxis.h \ profile-widget/divecartesianaxis.h \
profile-widget/divelineitem.h \ profile-widget/divelineitem.h \

View file

@ -5,8 +5,6 @@ set(BACKEND_SRCS
exportfuncs.h exportfuncs.h
plannershared.cpp plannershared.cpp
plannershared.h plannershared.h
roundrectitem.cpp
roundrectitem.h
) )
add_library(subsurface_backend_shared STATIC ${BACKEND_SRCS}) add_library(subsurface_backend_shared STATIC ${BACKEND_SRCS})

View file

@ -1,22 +0,0 @@
#include "roundrectitem.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
RoundRectItem::RoundRectItem(double radius, QGraphicsItem *parent) : QGraphicsRectItem(parent),
radius(radius)
{
}
RoundRectItem::RoundRectItem(double radius) : RoundRectItem(radius, nullptr)
{
}
void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->save();
painter->setClipRect(option->rect);
painter->setPen(pen());
painter->setBrush(brush());
painter->drawRoundedRect(rect(), radius, radius, Qt::AbsoluteSize);
painter->restore();
}

View file

@ -1,16 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef ROUNDRECTITEM_H
#define ROUNDRECTITEM_H
#include <QGraphicsRectItem>
class RoundRectItem : public QGraphicsRectItem {
public:
RoundRectItem(double radius, QGraphicsItem *parent);
RoundRectItem(double radius);
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
double radius;
};
#endif

View file

@ -20,14 +20,15 @@ set(SUBSURFACE_PROFILE_LIB_SRCS
diverectitem.h diverectitem.h
divetextitem.cpp divetextitem.cpp
divetextitem.h divetextitem.h
divetooltipitem.cpp
divetooltipitem.h
profilescene.cpp profilescene.cpp
profilescene.h profilescene.h
profiletranslations.h
profileview.cpp profileview.cpp
profileview.h profileview.h
tankitem.cpp tankitem.cpp
tankitem.h tankitem.h
tooltipitem.h
tooltipitem.cpp
) )
if (SUBSURFACE_TARGET_EXECUTABLE MATCHES "MobileExecutable") if (SUBSURFACE_TARGET_EXECUTABLE MATCHES "MobileExecutable")
set(SUBSURFACE_PROFILE_LIB_SRCS set(SUBSURFACE_PROFILE_LIB_SRCS

View file

@ -162,6 +162,11 @@ ProfileScene::~ProfileScene()
{ {
} }
const plot_info &ProfileScene::getPlotInfo() const
{
return plotInfo;
}
void ProfileScene::clear() void ProfileScene::clear()
{ {
for (AbstractProfilePolygonItem *item: profileItems) for (AbstractProfilePolygonItem *item: profileItems)
@ -613,3 +618,8 @@ double ProfileScene::calcZoomPosition(double zoom, double originalPos, double de
double newPos = newRelStart / factor; double newPos = newRelStart / factor;
return std::clamp(newPos, 0.0, 1.0); return std::clamp(newPos, 0.0, 1.0);
} }
int ProfileScene::timeAt(QPointF pos) const
{
return lrint(timeAxis->valueAt(pos));
}

View file

@ -48,6 +48,8 @@ public:
const struct dive *d, int dc, const struct dive *d, int dc,
DivePlannerPointsModel *plannerModel = nullptr, bool inPlanner = false); DivePlannerPointsModel *plannerModel = nullptr, bool inPlanner = false);
double calcZoomPosition(double zoom, double originalPos, double delta); double calcZoomPosition(double zoom, double originalPos, double delta);
const plot_info &getPlotInfo() const;
int timeAt(QPointF pos) const;
const struct dive *d; const struct dive *d;
int dc; int dc;

View file

@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-2.0
// Dummy object for profile-module related translations
#ifndef PROFILE_TRANSLATIONS_H
#define PROFILE_TRANSLATIONS_H
#include <QObject>
class ProfileTranslations : public QObject
{
Q_OBJECT
};
#endif

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "profileview.h" #include "profileview.h"
#include "profilescene.h" #include "profilescene.h"
#include "tooltipitem.h"
#include "zvalues.h" #include "zvalues.h"
#include "core/dive.h" #include "core/dive.h"
#include "core/divelog.h" #include "core/divelog.h"
@ -46,6 +47,7 @@ public:
ProfileView::ProfileView(QQuickItem *parent) : ChartView(parent, ProfileZValue::Count), ProfileView::ProfileView(QQuickItem *parent) : ChartView(parent, ProfileZValue::Count),
d(nullptr), d(nullptr),
dc(0), dc(0),
dpr(1.0),
zoomLevel(1.00), zoomLevel(1.00),
zoomedPosition(0.0), zoomedPosition(0.0),
panning(false), panning(false),
@ -84,6 +86,7 @@ ProfileView::ProfileView(QQuickItem *parent) : ChartView(parent, ProfileZValue::
connect(pp_gas, &qPrefPartialPressureGas::po2Changed, this, &ProfileView::replot); connect(pp_gas, &qPrefPartialPressureGas::po2Changed, this, &ProfileView::replot);
setAcceptTouchEvents(true); setAcceptTouchEvents(true);
setAcceptHoverEvents(true);
} }
ProfileView::ProfileView() : ProfileView(nullptr) ProfileView::ProfileView() : ProfileView(nullptr)
@ -119,6 +122,7 @@ void ProfileView::clear()
profileScene->clear(); profileScene->clear();
//handles.clear(); //handles.clear();
//gases.clear(); //gases.clear();
tooltip.reset();
empty = true; empty = true;
d = nullptr; d = nullptr;
dc = 0; dc = 0;
@ -135,7 +139,7 @@ void ProfileView::plotDive(const struct dive *dIn, int dcIn, int flags)
// We can't create the scene in the constructor, because we can't get the DPR property there. Oh joy! // We can't create the scene in the constructor, because we can't get the DPR property there. Oh joy!
if (!profileScene) { if (!profileScene) {
double dpr = std::clamp(property("dpr").toReal(), 0.5, 100.0); dpr = std::clamp(property("dpr").toReal(), 0.5, 100.0);
profileScene = std::make_unique<ProfileScene>(dpr, false, false); profileScene = std::make_unique<ProfileScene>(dpr, false, false);
} }
// If there was no previously displayed dive, turn off animations // If there was no previously displayed dive, turn off animations
@ -166,7 +170,6 @@ void ProfileView::plotDive(const struct dive *dIn, int dcIn, int flags)
profileItem->draw(size(), background, *profileScene); profileItem->draw(size(), background, *profileScene);
//rulerItem->setVisible(prefs.rulergraph && currentState != PLAN && currentState != EDIT); //rulerItem->setVisible(prefs.rulergraph && currentState != PLAN && currentState != EDIT);
//toolTipItem->setPlotInfo(profileScene->plotInfo);
//rulerItem->setPlotInfo(d, profileScene->plotInfo); //rulerItem->setPlotInfo(d, profileScene->plotInfo);
//if ((currentState == EDIT || currentState == PLAN) && plannerModel) { //if ((currentState == EDIT || currentState == PLAN) && plannerModel) {
@ -194,6 +197,15 @@ void ProfileView::plotDive(const struct dive *dIn, int dcIn, int flags)
report_error("%s", qPrintable(tr("Show NDL / TTS was disabled because of excessive processing time"))); report_error("%s", qPrintable(tr("Show NDL / TTS was disabled because of excessive processing time")));
} }
if (!tooltip)
tooltip = createChartItem<ToolTipItem>(dpr);
if (prefs.infobox) {
tooltip->setVisible(true);
tooltip->update(d, dpr, 0, profileScene->getPlotInfo(), flags & RenderFlags::PlanMode);
} else {
tooltip->setVisible(false);
}
// Reset animation. // Reset animation.
if (animSpeed <= 0) if (animSpeed <= 0)
animation.reset(); animation.reset();
@ -347,3 +359,23 @@ void ProfileView::pan(double x, double y)
if (oldPos != zoomedPosition) if (oldPos != zoomedPosition)
plotDive(d, dc, RenderFlags::Instant | RenderFlags::DontRecalculatePlotInfo); // TODO: animations don't work when scrolling plotDive(d, dc, RenderFlags::Instant | RenderFlags::DontRecalculatePlotInfo); // TODO: animations don't work when scrolling
} }
void ProfileView::hoverEnterEvent(QHoverEvent *)
{
}
void ProfileView::hoverMoveEvent(QHoverEvent *event)
{
if (!profileScene)
return;
QPointF pos = event->pos();
int time = profileScene->timeAt(pos);
bool requires_update = false;
if (tooltip) {
tooltip->update(d, dpr, time, profileScene->getPlotInfo(), false); // TODO: plan mode
requires_update = true;
}
if (requires_update)
update();
}

View file

@ -8,6 +8,7 @@
class ChartGraphicsSceneItem; class ChartGraphicsSceneItem;
class ProfileAnimation; class ProfileAnimation;
class ProfileScene; class ProfileScene;
class ToolTipItem;
class ProfileView : public ChartView { class ProfileView : public ChartView {
Q_OBJECT Q_OBJECT
@ -47,6 +48,7 @@ signals:
private: private:
const struct dive *d; const struct dive *d;
int dc; int dc;
double dpr;
double zoomLevel, zoomLevelPinchStart; double zoomLevel, zoomLevelPinchStart;
double zoomedPosition; // Position when zoomed: 0.0 = beginning, 1.0 = end. double zoomedPosition; // Position when zoomed: 0.0 = beginning, 1.0 = end.
bool panning; // Currently panning. bool panning; // Currently panning.
@ -64,11 +66,15 @@ private:
void replot(); void replot();
void setZoom(double level); void setZoom(double level);
void hoverEnterEvent(QHoverEvent *event) override;
void hoverMoveEvent(QHoverEvent *event) override;
void wheelEvent(QWheelEvent *event) override; void wheelEvent(QWheelEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
ChartItemPtr<ToolTipItem> tooltip;
// For mobile // For mobile
int getDiveId() const; int getDiveId() const;
void setDiveId(int id); void setDiveId(int id);

View file

@ -0,0 +1,159 @@
// SPDX-License-Identifier: GPL-2.0
#include "tooltipitem.h"
#include "profiletranslations.h"
#include "zvalues.h"
#include "core/color.h"
#include "core/membuffer.h"
#include "core/profile.h"
#include "core/qthelper.h" // for decoMode
#include <cmath>
#include <QApplication>
#include <QFontMetrics>
static const int tooltipBorder = 2;
static const double tooltipBorderRadius = 4.0; // Radius of rounded corners
static QColor tooltipBorderColor(Qt::white);
static QColor tooltipColor(0, 0, 0, 155);
static QColor tooltipFontColor(Qt::white);
static QFont makeFont(double dpr)
{
QFont font(qApp->font());
if (dpr != 1.0) {
int pixelSize = font.pixelSize();
if (pixelSize > 0) {
pixelSize = lrint(static_cast<double>(pixelSize) * dpr);
font.setPixelSize(pixelSize);
} else {
font.setPointSizeF(font.pointSizeF() * dpr);
}
}
return font;
}
ToolTipItem::ToolTipItem(ChartView &view, double dpr) :
ChartRectItem(view, ProfileZValue::ToolTipItem,
QPen(tooltipBorderColor, tooltipBorder),
QBrush(tooltipColor), tooltipBorderRadius),
font(makeFont(dpr)),
fm(font),
fontHeight(fm.height())
{
title = stringToPixmap(ProfileTranslations::tr("Information"));
}
QPixmap ToolTipItem::stringToPixmap(const QString &str) const
{
QSize s = fm.size(Qt::TextSingleLine, str);
if (s.width() <= 0 || s.height() <= 0)
return QPixmap(1,1);
QPixmap res(s);
res.fill(Qt::transparent);
QPainter painter(&res);
painter.setFont(font);
painter.setPen(tooltipFontColor);
painter.drawText(QRect(QPoint(), s), str);
return res;
}
// Split a membuffer into strings, skip empty lines.
// Return string / width (in pixels) pairs.
static std::vector<std::pair<QString, int>> split_mb_into_strings(const membuffer &mb, const QFontMetrics &fm)
{
std::vector<std::pair<QString, int>> res;
for (size_t i = 0; i < mb.len; ++i) {
size_t j;
for (j = i; j < mb.len && mb.buffer[j] != '\n'; ++j)
;
if (j > i) {
QString s = QString::fromUtf8(mb.buffer + i, j - i);
int width = fm.size(Qt::TextSingleLine, s).width();
res.emplace_back(s, width);
}
i = j; // Note: loop iteration will skip over '\n'
}
return res;
}
static QPixmap drawTissues(const plot_info &pInfo, double dpr, int idx, bool inPlanner)
{
QPixmap tissues(16,60);
QPainter painter(&tissues);
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);
if (!idx)
return tissues;
const struct plot_data *entry = &pInfo.entry[idx];
painter.setPen(QColor(0, 0, 0, 255));
if (decoMode(inPlanner) == BUEHLMANN)
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));
painter.setPen(QColor(0, 0, 0, 127));
for (int i = 0; i < 16; i++)
painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2);
if (dpr == 1.0)
return tissues;
// Scale according to DPR
int new_width = lrint(tissues.width() * dpr);
int new_height = lrint(tissues.width() * dpr);
return tissues.scaled(new_width, new_height);
}
void ToolTipItem::update(const dive *d, double dpr, int time, const plot_info &pInfo, bool inPlanner)
{
struct membufferpp mb;
int idx = get_plot_details_new(d, &pInfo, time, &mb);
QPixmap tissues = drawTissues(pInfo, dpr, idx, inPlanner);
std::vector<std::pair<QString, int>> strings = split_mb_into_strings(mb, fm);
//TODO: add event tool tips!
//const auto l = scene()->items(pos, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder,
//scene()->views().first()->transform());
//for (QGraphicsItem *item: l) {
//if (!item->toolTip().isEmpty())
//addToolTip(item->toolTip(), QPixmap());
//}
width = title.size().width();
for (auto &[s,w]: strings)
width = std::max(width, static_cast<double>(w));
width += tissues.width();
width += 6.0 * tooltipBorder;
height = 4.0 * tooltipBorder + title.height() +
std::max((static_cast<double>(strings.size()) + 1.0) * fontHeight,
static_cast<double>(tissues.height()));
ChartRectItem::resize(QSizeF(width, height));
painter->setFont(font);
painter->setPen(QPen(tooltipFontColor)); // QPainter uses QPen to set text color!
double x = 4.0 * tooltipBorder + tissues.width();
double y = 2.0 * tooltipBorder;
double titleOffset = (width - title.width()) / 2.0;
painter->drawPixmap(lrint(titleOffset), lrint(y), title, 0, 0, title.width(), title.height());
y += round(fontHeight);
painter->drawPixmap(lrint(2.0 * tooltipBorder), lrint(y), tissues, 0, 0, tissues.width(), tissues.height());
y += round(fontHeight);
for (auto &[s,w]: strings) {
QRectF rect(x, y, w, fontHeight);
painter->drawText(rect, s);
y += fontHeight;
}
setTextureDirty();
}

View file

@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef PROFILE_TOOLTIPITEM_H
#define PROFILE_TOOLTIPITEM_H
#include "qt-quick/chartitem.h"
#include <QFont>
#include <QPixmap>
struct dive;
struct plot_info;
class ToolTipItem : public ChartRectItem {
public:
ToolTipItem(ChartView &view, double dpr);
void update(const dive *d, double dpr, int time, const plot_info &pInfo, bool inPlanner);
private:
QFont font;
QFontMetrics fm;
double fontHeight;
QPixmap title;
double width, height;
QPixmap stringToPixmap(const QString &s) const;
};
#endif // PROFILE_TOOLTIPITEM

View file

@ -12,6 +12,7 @@
struct ProfileZValue { struct ProfileZValue {
enum ZValues { enum ZValues {
Profile = 0, Profile = 0,
ToolTipItem,
Count Count
}; };
}; };

View file

@ -94,6 +94,7 @@ void ChartPixmapItem::resize(QSizeF size)
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
} }
rect.setSize(size); rect.setSize(size);
setPositionDirty(); // position includes the size.
setTextureDirty(); setTextureDirty();
} }