2021-08-30 20:59:45 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.
|
2015-07-17 16:09:42 +00:00
|
|
|
#include "qmlprofile.h"
|
2021-08-04 05:27:41 +00:00
|
|
|
#include "profilescene.h"
|
2018-06-08 10:56:23 +00:00
|
|
|
#include "mobile-widgets/qmlmanager.h"
|
2024-06-07 08:25:09 +00:00
|
|
|
#include "core/divelist.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "core/errorhelper.h"
|
2022-08-30 16:13:13 +00:00
|
|
|
#include "core/subsurface-float.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/metrics.h"
|
2022-04-04 16:57:28 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2015-07-25 16:10:51 +00:00
|
|
|
#include <QTransform>
|
2016-03-09 03:33:19 +00:00
|
|
|
#include <QScreen>
|
2020-02-17 18:45:28 +00:00
|
|
|
#include <QElapsedTimer>
|
2015-07-17 16:09:42 +00:00
|
|
|
|
|
|
|
QMLProfile::QMLProfile(QQuickItem *parent) :
|
2015-12-29 17:50:47 +00:00
|
|
|
QQuickPaintedItem(parent),
|
2022-09-17 15:19:11 +00:00
|
|
|
m_diveId(0),
|
|
|
|
m_dc(0),
|
2016-01-28 15:02:20 +00:00
|
|
|
m_devicePixelRatio(1.0),
|
2018-05-25 21:30:10 +00:00
|
|
|
m_margin(0),
|
2020-02-17 18:46:31 +00:00
|
|
|
m_xOffset(0.0),
|
2021-05-31 19:44:37 +00:00
|
|
|
m_yOffset(0.0)
|
2015-07-17 16:09:42 +00:00
|
|
|
{
|
2021-05-31 19:44:37 +00:00
|
|
|
createProfileView();
|
2015-12-29 17:51:34 +00:00
|
|
|
setAntialiasing(true);
|
2020-02-17 18:46:31 +00:00
|
|
|
setFlags(QQuickItem::ItemClipsChildrenToShape | QQuickItem::ItemHasContents );
|
2016-03-09 03:33:19 +00:00
|
|
|
connect(QMLManager::instance(), &QMLManager::sendScreenChanged, this, &QMLProfile::screenChanged);
|
2020-03-24 22:02:17 +00:00
|
|
|
connect(this, &QMLProfile::scaleChanged, this, &QMLProfile::triggerUpdate);
|
2020-12-21 12:42:04 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &QMLProfile::divesChanged);
|
2016-03-09 03:33:19 +00:00
|
|
|
setDevicePixelRatio(QMLManager::instance()->lastDevicePixelRatio());
|
2015-11-12 00:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:27:41 +00:00
|
|
|
QMLProfile::~QMLProfile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-31 19:44:37 +00:00
|
|
|
void QMLProfile::createProfileView()
|
|
|
|
{
|
2022-03-13 00:35:56 +00:00
|
|
|
m_profileWidget.reset(new ProfileScene(m_devicePixelRatio * 0.8, false, false));
|
2021-05-31 19:44:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 22:02:17 +00:00
|
|
|
// we need this so we can connect update() to the scaleChanged() signal - which the connect above cannot do
|
|
|
|
// directly as it chokes on the default parameter for update().
|
|
|
|
// If the scale changes we may need to change our offsets to ensure that we still only show a subset of
|
|
|
|
// the profile and not empty space around it, which the paint() method below will take care of, which will
|
|
|
|
// eventually get called after we call update()
|
|
|
|
void QMLProfile::triggerUpdate()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:09:42 +00:00
|
|
|
void QMLProfile::paint(QPainter *painter)
|
|
|
|
{
|
2020-02-17 18:45:28 +00:00
|
|
|
QElapsedTimer timer;
|
|
|
|
if (verbose)
|
|
|
|
timer.start();
|
|
|
|
|
2016-01-18 20:32:47 +00:00
|
|
|
// let's look at the intended size of the content and scale our scene accordingly
|
2022-02-27 21:09:35 +00:00
|
|
|
// for some odd reason the painter transformation is set up to scale by the dpr - which results
|
|
|
|
// in applying that dpr scaling twice. So we hard-code it here to be the identity matrix
|
2016-03-09 03:38:03 +00:00
|
|
|
QRect painterRect = painter->viewport();
|
2022-02-27 21:09:35 +00:00
|
|
|
painter->resetTransform();
|
2021-08-30 20:59:45 +00:00
|
|
|
if (m_diveId < 0)
|
|
|
|
return;
|
2024-06-07 08:25:09 +00:00
|
|
|
struct dive *d = divelog.dives.get_by_uniq_id(m_diveId);
|
2021-08-30 20:59:45 +00:00
|
|
|
if (!d)
|
|
|
|
return;
|
2022-09-17 15:19:11 +00:00
|
|
|
m_profileWidget->draw(painter, painterRect, d, m_dc, nullptr, false);
|
2015-07-17 16:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 17:50:47 +00:00
|
|
|
void QMLProfile::setMargin(int margin)
|
|
|
|
{
|
|
|
|
m_margin = margin;
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:52:41 +00:00
|
|
|
int QMLProfile::diveId() const
|
2015-07-17 16:09:42 +00:00
|
|
|
{
|
|
|
|
return m_diveId;
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:42:56 +00:00
|
|
|
void QMLProfile::setDiveId(int diveId)
|
|
|
|
{
|
|
|
|
m_diveId = diveId;
|
2022-09-17 15:19:11 +00:00
|
|
|
emit numDCChanged();
|
2020-02-17 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 02:05:06 +00:00
|
|
|
qreal QMLProfile::devicePixelRatio() const
|
|
|
|
{
|
|
|
|
return m_devicePixelRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMLProfile::setDevicePixelRatio(qreal dpr)
|
|
|
|
{
|
|
|
|
if (dpr != m_devicePixelRatio) {
|
|
|
|
m_devicePixelRatio = dpr;
|
2021-05-31 19:57:39 +00:00
|
|
|
// Recreate the view to redraw the text items with the new scale.
|
|
|
|
createProfileView();
|
2016-01-13 02:05:06 +00:00
|
|
|
emit devicePixelRatioChanged();
|
|
|
|
}
|
2015-07-17 16:09:42 +00:00
|
|
|
}
|
2016-03-09 03:33:19 +00:00
|
|
|
|
2020-02-17 18:46:31 +00:00
|
|
|
// don't update the profile here, have the user update x and y and then manually trigger an update
|
|
|
|
void QMLProfile::setXOffset(qreal value)
|
|
|
|
{
|
2022-08-30 15:55:43 +00:00
|
|
|
if (nearly_equal(value, m_xOffset))
|
2020-02-17 18:46:31 +00:00
|
|
|
return;
|
|
|
|
m_xOffset = value;
|
|
|
|
emit xOffsetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't update the profile here, have the user update x and y and then manually trigger an update
|
|
|
|
void QMLProfile::setYOffset(qreal value)
|
|
|
|
{
|
2022-08-30 15:55:43 +00:00
|
|
|
if (nearly_equal(value, m_yOffset))
|
2020-02-17 18:46:31 +00:00
|
|
|
return;
|
|
|
|
m_yOffset = value;
|
|
|
|
emit yOffsetChanged();
|
|
|
|
}
|
|
|
|
|
2016-03-09 03:33:19 +00:00
|
|
|
void QMLProfile::screenChanged(QScreen *screen)
|
|
|
|
{
|
|
|
|
setDevicePixelRatio(screen->devicePixelRatio());
|
|
|
|
}
|
2020-12-21 12:42:04 +00:00
|
|
|
|
|
|
|
void QMLProfile::divesChanged(const QVector<dive *> &dives, DiveField)
|
|
|
|
{
|
|
|
|
for (struct dive *d: dives) {
|
|
|
|
if (d->id == m_diveId) {
|
2024-03-25 20:17:29 +00:00
|
|
|
report_info("dive #%d changed, trigger profile update", d->number);
|
2020-12-21 12:42:04 +00:00
|
|
|
triggerUpdate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 15:19:11 +00:00
|
|
|
|
|
|
|
void QMLProfile::nextDC()
|
|
|
|
{
|
|
|
|
rotateDC(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMLProfile::prevDC()
|
|
|
|
{
|
|
|
|
rotateDC(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QMLProfile::rotateDC(int dir)
|
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
struct dive *d = divelog.dives.get_by_uniq_id(m_diveId);
|
2022-09-17 15:19:11 +00:00
|
|
|
if (!d)
|
|
|
|
return;
|
2024-06-30 15:16:14 +00:00
|
|
|
int numDC = d->number_of_computers();
|
2022-09-17 15:19:11 +00:00
|
|
|
if (numDC == 1)
|
|
|
|
return;
|
|
|
|
m_dc = (m_dc + dir) % numDC;
|
|
|
|
if (m_dc < 0)
|
|
|
|
m_dc += numDC;
|
|
|
|
triggerUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QMLProfile::numDC() const
|
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
struct dive *d = divelog.dives.get_by_uniq_id(m_diveId);
|
2024-06-30 15:16:14 +00:00
|
|
|
return d ? d->number_of_computers() : 0;
|
2022-09-17 15:19:11 +00:00
|
|
|
}
|