profle: port event manipulation to QtQuick

UI change: the context menu is opened when left-clicking on
the event. This is probably more compatible with mobile UI.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-02-24 09:07:23 +01:00
parent f0fe52d42b
commit faf18ef36b
6 changed files with 147 additions and 124 deletions

View file

@ -526,6 +526,8 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, int animSpeed, boo
struct gasmix lastgasmix = d->get_gasmix_at_time(*currentdc, 1_sec);
for (auto [idx, event]: enumerated_range(currentdc->events)) {
if (event.hidden)
continue;
// if print mode is selected only draw headings, SP change, gas events or bookmark event
if (printMode) {
if (event.name.empty() ||
@ -663,3 +665,12 @@ std::vector<std::pair<QString, QPixmap>> ProfileScene::eventsAt(QPointF pos) con
}
return res;
}
DiveEventItem *ProfileScene::eventAtPosition(QPointF pos) const
{
for (const auto &item: eventItems) {
if (item->contains(item->mapFromScene(pos)))
return item.get();
}
return nullptr;
}

View file

@ -60,6 +60,7 @@ public:
double posAtDepth(double depth) const;
double yToScreen(double y) const; // For pictures: depth given in fration of displayed range.
std::vector<std::pair<QString, QPixmap>> eventsAt(QPointF pos) const;
DiveEventItem *eventAtPosition(QPointF pos) const; // null if no event icon at position.
const struct dive *d;
int dc;

View file

@ -2,6 +2,7 @@
#include "profileview.h"
#include "pictureitem.h"
#include "profilescene.h"
#include "diveeventitem.h"
#include "handleitem.h"
#include "ruleritem.h"
#include "tooltipitem.h"
@ -10,10 +11,13 @@
#include "core/divelog.h"
#include "commands/command.h"
#include "core/errorhelper.h"
#include "core/event.h"
#include "core/eventtype.h"
#include "core/imagedownloader.h"
#include "core/pref.h"
#include "core/qthelper.h" // for localFilePath()
#include "core/range.h"
#include "core/subsurface-string.h" // for empty_string()
#include "core/settings/qPrefDisplay.h"
#include "core/settings/qPrefPartialPressureGas.h"
#include "core/settings/qPrefTechnicalDetails.h"
@ -29,8 +33,9 @@
#ifndef SUBSURFACE_MOBILE
#include "core/device.h"
#include <QMenu>
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
#endif
static const QColor mouseFollowerColor = QColor(Qt::red).lighter();
@ -385,22 +390,38 @@ void ProfileView::wheelEvent(QWheelEvent *event)
struct MenuEntry {
QString text;
std::function<void()> action;
std::vector<MenuEntry> subitems;
MenuEntry(QString text, std::function<void()> action)
: text(text), action(std::move(action))
{
}
MenuEntry(QString text, std::vector<MenuEntry> subitems)
: text(text), subitems(std::move(subitems))
{
}
};
#ifndef SUBSURFACE_MOBILE
static void makeMenu(QMenu &m, const std::vector<MenuEntry> &entries)
{
for (const MenuEntry &e: entries) {
if (!e.subitems.empty())
makeMenu(*m.addMenu(e.text), e.subitems);
else if(e.action)
m.addAction(e.text, [f = e.action] { f(); }); // Dang. Qt doesn't support std::function!
// This is too many indirections for my taste. :(
}
}
#endif
static void execMenu(const std::vector<MenuEntry> &entries, QPoint pos)
{
if (entries.empty())
return;
#ifndef SUBSURFACE_MOBILE
QMenu m;
for (const MenuEntry &e: entries) {
// Dang. Qt doesn't support std::function! This is too many indirections for my taste. :(
m.addAction(e.text, [f = e.action] { f(); });
}
makeMenu(m, entries);
m.exec(pos);
#endif
}
@ -422,6 +443,72 @@ void ProfileView::renameCurrentDC()
#endif
}
// TODO: How should that work on mobile?
void ProfileView::editEventName(const struct event &event, int idx)
{
#ifndef SUBSURFACE_MOBILE
bool ok;
// TODO: center on window by passing a QWidget as first argument!
QString newName = QInputDialog::getText(nullptr, tr("Edit name of bookmark"),
tr("Custom name:"), QLineEdit::Normal,
event.name.c_str(), &ok);
if (ok && !newName.isEmpty()) {
if (newName.length() > 22) { //longer names will display as garbage.
QMessageBox lengthWarning;
lengthWarning.setText(tr("Name is too long!"));
lengthWarning.exec();
return;
}
Command::renameEvent(mutable_dive(), dc, idx, qPrintable(newName));
}
#endif
}
void ProfileView::hideEvent(DiveEventItem &item)
{
if(!d)
return;
struct divecomputer *currentdc = mutable_dive()->get_dc(dc);
int idx = item.idx;
if (!currentdc || idx < 0 || static_cast<size_t>(idx) >= currentdc->events.size())
return;
currentdc->events[idx].hidden = true;
replot();
}
void ProfileView::hideEventType(DiveEventItem &item)
{
if (!item.ev.name.empty()) {
hide_event_type(&item.ev);
replot();
}
}
// TODO: How should that work on mobile?
void ProfileView::removeEvent(DiveEventItem &item)
{
if(!d)
return;
#ifndef SUBSURFACE_MOBILE
const struct event &ev = item.ev;
if (QMessageBox::question(nullptr, TITLE_OR_TEXT(
tr("Remove the selected event?"),
tr("%1 @ %2:%3").arg(QString::fromStdString(ev.name)).arg(ev.time.seconds / 60).arg(ev.time.seconds % 60, 2, 10, QChar('0'))),
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok)
Command::removeEvent(mutable_dive(), dc, item.idx);
#endif
}
// Formats cylinder information for display.
// eg : "Cyl 1 (AL80 EAN32)"
static QString formatCylinderDescription(int i, const cylinder_t &cylinder)
{
QString label = gettextFromC::tr("Cyl") + QString(" %1").arg(i+1);
QString mix = get_gas_string(cylinder.gasmix);
label += QString(" (%2 %3)").arg(QString::fromStdString(cylinder.type.description), mix);
return label;
}
void ProfileView::mousePressEvent(QMouseEvent *event)
{
// Handle dragging of items
@ -451,8 +538,42 @@ void ProfileView::mousePressEvent(QMouseEvent *event)
}
if (currentdc->deviceid)
m.emplace_back(tr("Rename this dive computer"), [this] { renameCurrentDC(); });
execMenu(m, event->globalPos());
return;
return execMenu(m, event->globalPos());
}
DiveEventItem *item = profileScene->eventAtPosition(event->pos());
if (d && item) {
std::vector<MenuEntry> m;
const struct event &ev = item->ev;
if (ev.is_gaschange()) {
std::vector<MenuEntry> gas_menu;
for (auto [idx, cylinder]: enumerated_range(d->cylinders)) {
QString label = formatCylinderDescription(idx, cylinder);
gas_menu.emplace_back(label, [this, i = idx, time = ev.time.seconds] {
Command::addGasSwitch(mutable_dive(), dc, time, i);
});
}
m.emplace_back(tr("Edit gas change"), std::move(gas_menu));
}
m.emplace_back(tr("Remove event"), [this, item] {
removeEvent(*item);
});
m.emplace_back(tr("Hide event"), [this, item] {
hideEvent(*item);
});
if (!item->ev.name.empty()) {
m.emplace_back(tr("Hide events of type '%1'").arg(event_type_name(ev)), [this, item] {
hideEventType(*item);
});
}
if (ev.type == SAMPLE_EVENT_BOOKMARK) {
m.emplace_back(tr("Edit name"), [this, item] {
editEventName(item->ev, item->idx);
});
}
return execMenu(m, event->globalPos());
}
// Check if current picture is clicked

View file

@ -9,6 +9,7 @@
class ChartGraphicsSceneItem;
class ChartLineItem;
class ChartRectItem;
class DiveEventItem;
class DivePlannerPointsModel;
class HandleItem;
class PictureItem;
@ -136,6 +137,12 @@ private:
// DC related
void renameCurrentDC();
// Event related
void editEventName(const struct event &event, int idx);
void hideEvent(DiveEventItem &item);
void hideEventType(DiveEventItem &item);
void removeEvent(DiveEventItem &item);
// The list of pictures in this plot. The pictures are sorted by offset in seconds.
// For the same offset, sort by filename.
// Pictures that are outside of the dive time are not shown.

View file

@ -371,7 +371,6 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
// Add or edit Gas Change
if (d && item && item->ev.is_gaschange()) {
addGasChangeMenu(m, tr("Edit gas change"), *d, dc, item->ev.time.seconds);
} else if (d && d->cylinders.size() > 1) {
// if we have more than one gas, offer to switch to another one
const struct divecomputer *currentdc = d->get_dc(dc);
@ -397,51 +396,6 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
changeMode->addAction(gettextFromC::tr(divemode_text_ui[PSCR]),
[this, seconds](){ addDivemodeSwitch(seconds, PSCR); });
if (DiveEventItem *item = dynamic_cast<DiveEventItem *>(sceneItem)) {
m.addAction(tr("Remove event"), [this,item] { removeEvent(item); });
m.addAction(tr("Hide event"), [this, item] { hideEvent(item); });
m.addAction(tr("Hide events of type '%1'").arg(event_type_name(item->ev)),
[this, item] { hideEventType(item); });
if (item->ev.type == SAMPLE_EVENT_BOOKMARK)
m.addAction(tr("Edit name"), [this, item] { editName(item); });
#if 0 // TODO::: FINISH OR DISABLE
QPointF scenePos = mapToScene(event->pos());
int idx = getEntryFromPos(scenePos);
// this shows how to figure out if we should ask the user if they want adjust interpolated pressures
// at either side of a gas change
if (item->ev->type == SAMPLE_EVENT_GASCHANGE || item->ev->type == SAMPLE_EVENT_GASCHANGE2) {
int gasChangeIdx = idx;
while (gasChangeIdx > 0) {
--gasChangeIdx;
if (plotInfo.entry[gasChangeIdx].sec <= item->ev->time.seconds)
break;
}
const struct plot_data &gasChangeEntry = plotInfo.entry[newGasIdx];
// now gasChangeEntry points at the gas change, that entry has the final pressure of
// the old tank, the next entry has the starting pressure of the next tank
if (gasChangeIdx < plotInfo.nr - 1) {
int newGasIdx = gasChangeIdx + 1;
const struct plot_data &newGasEntry = plotInfo.entry[newGasIdx];
if (get_plot_sensor_pressure(&plotInfo, gasChangeIdx) == 0 || d->get_cylinder(gasChangeEntry->sensor[0])->sample_start.mbar == 0) {
// if we have no sensorpressure or if we have no pressure from samples we can assume that
// we only have interpolated pressure (the pressure in the entry may be stored in the sensor
// pressure field if this is the first or last entry for this tank... see details in gaspressures.c
pressure_t pressure;
pressure.mbar = get_plot_interpolated_pressure(&plotInfo, gasChangeIdx) ? : get_plot_sensor_pressure(&plotInfo, gasChangeIdx);
QAction *adjustOldPressure = m.addAction(tr("Adjust pressure of cyl. %1 (currently interpolated as %2)")
.arg(gasChangeEntry->sensor[0] + 1).arg(get_pressure_string(pressure)));
}
if (get_plot_sensor_pressure(&plotInfo, newGasIdx) == 0 || d->get_cylinder(newGasEntry->sensor[0])->sample_start.mbar == 0) {
// we only have interpolated press -- see commend above
pressure_t pressure;
pressure.mbar = get_plot_interpolated_pressure(&plotInfo, newGasIdx) ? : get_plot_sensor_pressure(&plotInfo, newGasIdx);
QAction *adjustOldPressure = m.addAction(tr("Adjust pressure of cyl. %1 (currently interpolated as %2)")
.arg(newGasEntry->sensor[0] + 1).arg(get_pressure_string(pressure)));
}
}
}
#endif
}
if (any_event_types_hidden()) {
QMenu *m2 = m.addMenu(tr("Unhide event type"));
for (int i: hidden_event_types()) {
@ -459,27 +413,6 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
m.exec(event->globalPos());
}
void ProfileWidget2::hideEvent(DiveEventItem *item)
{
if (!d)
return;
struct divecomputer *currentdc = mutable_dive()->get_dc(dc);
int idx = item->idx;
if (!currentdc || idx < 0 || static_cast<size_t>(idx) >= currentdc->events.size())
return;
currentdc->events[idx].hidden = true;
item->hide();
}
void ProfileWidget2::hideEventType(DiveEventItem *item)
{
if (!item->ev.name.empty()) {
hide_event_type(&item->ev);
replot();
}
}
void ProfileWidget2::unhideEvents()
{
if (!d)
@ -500,16 +433,6 @@ void ProfileWidget2::unhideEventTypes()
replot();
}
void ProfileWidget2::removeEvent(DiveEventItem *item)
{
const struct event &ev = item->ev;
if (QMessageBox::question(this, TITLE_OR_TEXT(
tr("Remove the selected event?"),
tr("%1 @ %2:%3").arg(QString::fromStdString(ev.name)).arg(ev.time.seconds / 60).arg(ev.time.seconds % 60, 2, 10, QChar('0'))),
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok)
Command::removeEvent(mutable_dive(), dc, item->idx);
}
void ProfileWidget2::addBookmark(int seconds)
{
if (d)
@ -537,44 +460,6 @@ void ProfileWidget2::splitDive(int seconds)
Command::splitDives(mutable_dive(), duration_t{ .seconds = seconds });
}
void ProfileWidget2::addGasSwitch(int tank, int seconds)
{
if (!d || tank < 0 || static_cast<size_t>(tank) >= d->cylinders.size())
return;
Command::addGasSwitch(mutable_dive(), dc, seconds, tank);
}
void ProfileWidget2::changeGas(int index, int newCylinderId)
{
if ((currentState == PLAN || currentState == EDIT) && plannerModel) {
QModelIndex modelIndex = plannerModel->index(index, DivePlannerPointsModel::GAS);
plannerModel->gasChange(modelIndex.sibling(modelIndex.row() + 1, modelIndex.column()), newCylinderId);
if (currentState == EDIT)
emit stopEdited();
}
}
void ProfileWidget2::editName(DiveEventItem *item)
{
if (!d)
return;
bool ok;
QString newName = QInputDialog::getText(this, tr("Edit name of bookmark"),
tr("Custom name:"), QLineEdit::Normal,
item->ev.name.c_str(), &ok);
if (ok && !newName.isEmpty()) {
if (newName.length() > 22) { //longer names will display as garbage.
QMessageBox lengthWarning;
lengthWarning.setText(tr("Name is too long!"));
lengthWarning.exec();
return;
}
Command::renameEvent(mutable_dive(), dc, item->idx, newName.toStdString());
}
}
void ProfileWidget2::connectPlannerModel()
{
connect(plannerModel, &DivePlannerPointsModel::dataChanged, this, &ProfileWidget2::replot);

View file

@ -109,8 +109,6 @@ private:
void splitDive(int seconds);
void addSetpointChange(int seconds);
void removeEvent(DiveEventItem *item);
void hideEvent(DiveEventItem *item);
void hideEventType(DiveEventItem *item);
void editName(DiveEventItem *item);
void unhideEvents();
void unhideEventTypes();