summaryrefslogtreecommitdiffstats
path: root/profile-widget
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2021-01-24 09:13:56 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2021-04-02 13:53:23 -0700
commit396758d4896197be9dd9f6d71e094068203e52a6 (patch)
tree5f87c0f5dd2c7362be06d1626ffdfe8601a02481 /profile-widget
parent1b14a211f0719aa3a46bf0584d5652bc7c7a4b6f (diff)
downloadsubsurface-396758d4896197be9dd9f6d71e094068203e52a6.tar.gz
profile use unique_ptr to manage dive handler objects
Instead of manually deleting them (and the gases). Currently there is only one point where these are deleted, but if we implement proper Qt model/view semantics, this makes things less headachy. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'profile-widget')
-rw-r--r--profile-widget/divehandler.cpp2
-rw-r--r--profile-widget/profilewidget2.cpp50
-rw-r--r--profile-widget/profilewidget2.h5
3 files changed, 31 insertions, 26 deletions
diff --git a/profile-widget/divehandler.cpp b/profile-widget/divehandler.cpp
index 4a5088964..2f06ea661 100644
--- a/profile-widget/divehandler.cpp
+++ b/profile-widget/divehandler.cpp
@@ -22,7 +22,7 @@ DiveHandler::DiveHandler(const struct dive *d) : dive(d)
int DiveHandler::parentIndex()
{
ProfileWidget2 *view = qobject_cast<ProfileWidget2 *>(scene()->views().first());
- return view->handles.indexOf(this);
+ return view->handleIndex(this);
}
void DiveHandler::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp
index 0808e2932..5a32c30d8 100644
--- a/profile-widget/profilewidget2.cpp
+++ b/profile-widget/profilewidget2.cpp
@@ -1062,7 +1062,7 @@ bool ProfileWidget2::eventFilter(QObject *object, QEvent *event)
template <typename T>
static void hideAll(const T &container)
{
- for (auto *item: container)
+ for (auto &item: container)
item->setVisible(false);
}
@@ -1693,12 +1693,20 @@ void ProfileWidget2::disconnectTemporaryConnections()
}
}
+int ProfileWidget2::handleIndex(const DiveHandler *h) const
+{
+ auto it = std::find_if(handles.begin(), handles.end(),
+ [h] (const std::unique_ptr<DiveHandler> &h2)
+ { return h == h2.get(); });
+ return it != handles.end() ? it - handles.begin() : -1;
+}
+
#ifndef SUBSURFACE_MOBILE
void ProfileWidget2::pointInserted(const QModelIndex&, int, int)
{
DiveHandler *item = new DiveHandler(&displayed_dive);
scene()->addItem(item);
- handles << item;
+ handles.emplace_back(item);
connect(item, &DiveHandler::moved, this, &ProfileWidget2::recreatePlannedDive);
connect(item, &DiveHandler::clicked, this, &ProfileWidget2::divePlannerHandlerClicked);
@@ -1707,21 +1715,17 @@ void ProfileWidget2::pointInserted(const QModelIndex&, int, int)
scene()->addItem(gasChooseBtn);
gasChooseBtn->setZValue(10);
gasChooseBtn->setFlag(QGraphicsItem::ItemIgnoresTransformations);
- gases << gasChooseBtn;
+ gases.emplace_back(gasChooseBtn);
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
if (plannerModel->recalcQ())
replot();
}
void ProfileWidget2::pointsRemoved(const QModelIndex &, int start, int end)
-{ // start and end are inclusive.
- int num = (end - start) + 1;
- for (int i = num; i != 0; i--) {
- delete handles.back();
- handles.pop_back();
- delete gases.back();
- gases.pop_back();
- }
+{
+ // Qt's model/view API is mad. The end-point is inclusive, which means that the empty range is [0,-1]!
+ handles.erase(handles.begin() + start, handles.begin() + end + 1);
+ gases.erase(gases.begin() + start, gases.begin() + end + 1);
scene()->clearSelection();
replot();
}
@@ -1735,7 +1739,7 @@ void ProfileWidget2::repositionDiveHandlers()
struct divedatapoint datapoint = plannerModel->at(i);
if (datapoint.time == 0) // those are the magic entries for tanks
continue;
- DiveHandler *h = handles.at(i);
+ DiveHandler *h = handles[i].get();
h->setVisible(datapoint.entered);
h->setPos(timeAxis->posAtValue(datapoint.time), profileYAxis->posAtValue(datapoint.depth.mm));
QPointF p1;
@@ -1763,14 +1767,14 @@ void ProfileWidget2::repositionDiveHandlers()
int ProfileWidget2::fixHandlerIndex(DiveHandler *activeHandler)
{
- int index = handles.indexOf(activeHandler);
- if (index > 0 && index < handles.count() - 1) {
- DiveHandler *before = handles[index - 1];
+ int index = handleIndex(activeHandler);
+ if (index > 0 && index < (int)handles.size() - 1) {
+ DiveHandler *before = handles[index - 1].get();
if (before->pos().x() > activeHandler->pos().x()) {
std::swap(handles[index], handles[index - 1]);
return index - 1;
}
- DiveHandler *after = handles[index + 1];
+ DiveHandler *after = handles[index + 1].get();
if (after->pos().x() < activeHandler->pos().x()) {
std::swap(handles[index], handles[index + 1]);
return index + 1;
@@ -1817,7 +1821,7 @@ void ProfileWidget2::keyDownAction()
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
- int row = handles.indexOf(handler);
+ int row = handleIndex(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.depth.mm >= profileYAxis->maximum())
continue;
@@ -1839,7 +1843,7 @@ void ProfileWidget2::keyUpAction()
bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
- int row = handles.indexOf(handler);
+ int row = handleIndex(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.depth.mm <= 0)
@@ -1862,7 +1866,7 @@ void ProfileWidget2::keyLeftAction()
bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
- int row = handles.indexOf(handler);
+ int row = handleIndex(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.time / 60 <= 0)
@@ -1872,7 +1876,7 @@ void ProfileWidget2::keyLeftAction()
// maybe this is a good place for a 'goto'?
double xpos = timeAxis->posAtValue((dp.time - 60) / 60);
bool nextStep = false;
- Q_FOREACH (DiveHandler *h, handles) {
+ for (const auto &h: handles) {
if (IS_FP_SAME(h->pos().x(), xpos)) {
nextStep = true;
break;
@@ -1898,7 +1902,7 @@ void ProfileWidget2::keyRightAction()
bool oldRecalc = plannerModel->setRecalc(false);
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
- int row = handles.indexOf(handler);
+ int row = handleIndex(handler);
divedatapoint dp = plannerModel->at(row);
if (dp.time / 60.0 >= timeAxis->maximum())
continue;
@@ -1907,7 +1911,7 @@ void ProfileWidget2::keyRightAction()
// maybe this is a good place for a 'goto'?
double xpos = timeAxis->posAtValue((dp.time + 60) / 60);
bool nextStep = false;
- Q_FOREACH (DiveHandler *h, handles) {
+ for (const auto &h: handles) {
if (IS_FP_SAME(h->pos().x(), xpos)) {
nextStep = true;
break;
@@ -1935,7 +1939,7 @@ void ProfileWidget2::keyDeleteAction()
QVector<int> selectedIndices;
Q_FOREACH (QGraphicsItem *i, scene()->selectedItems()) {
if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler *>(i)) {
- selectedIndices.push_back(handles.indexOf(handler));
+ selectedIndices.push_back(handleIndex(handler));
handler->hide();
}
}
diff --git a/profile-widget/profilewidget2.h b/profile-widget/profilewidget2.h
index b2632d511..395832d40 100644
--- a/profile-widget/profilewidget2.h
+++ b/profile-widget/profilewidget2.h
@@ -227,7 +227,7 @@ private:
#endif
TankItem *tankItem;
- QList<QGraphicsSimpleTextItem *> gases;
+ std::vector<std::unique_ptr<QGraphicsSimpleTextItem>> gases;
//specifics for ADD and PLAN
#ifndef SUBSURFACE_MOBILE
@@ -251,7 +251,8 @@ private:
void updateThumbnailPaintOrder();
#endif
- QList<DiveHandler *> handles;
+ std::vector<std::unique_ptr<DiveHandler>> handles;
+ int handleIndex(const DiveHandler *h) const;
#ifndef SUBSURFACE_MOBILE
void repositionDiveHandlers();
int fixHandlerIndex(DiveHandler *activeHandler);