profile: simplify checking for DiveTextItem

When creating the context menu on the profile, the code has
to check whether the context menu is activated on the
dive computer name to show a special menu (delete / split
dive computer).

This was done by setting a special property on the item
and then checking for that property on the item that
the menu is invoked on or its parents.

The reason the code didn't simply check the pointer was
probably that DiveTextItem uses multiple inheritance:
It derives from QObject and QGraphicsItem. It has to derive
from QObject first, because (the ridiculously broken) MOC
needs it that way. The object added to the scene is a
QGraphicsItem. Thus, we get a pointer _into_ the DiveTextItem
object.

However, that's all completely unnecessary. We can simply
compare the pointers, as the compiler will understand that
QGraphicsItem is only the second base class of DiveTextItem.
Magic!

Let's remove the cruft and simply compare the pointers.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-04-25 22:06:14 +02:00 committed by Dirk Hohndel
parent e9dd1b150f
commit 78f4d7b2b9

View file

@ -217,9 +217,6 @@ void ProfileWidget2::addActionShortcut(const Qt::Key shortcut, void (ProfileWidg
}
#endif // SUBSURFACE_MOBILE
#define SUBSURFACE_OBJ_DATA 1
#define SUBSURFACE_OBJ_DC_TEXT 0x42
void ProfileWidget2::addItemsToScene()
{
scene()->addItem(background);
@ -228,11 +225,6 @@ void ProfileWidget2::addItemsToScene()
scene()->addItem(temperatureAxis);
scene()->addItem(timeAxis);
scene()->addItem(cylinderPressureAxis);
// I cannot seem to figure out if an object that I find with itemAt() on the scene
// is the object I am looking for - my guess is there's a simple way in Qt to do that
// but nothing I tried worked.
// so instead this adds a special magic key/value pair to the object to mark it
diveComputerText->setData(SUBSURFACE_OBJ_DATA, SUBSURFACE_OBJ_DC_TEXT);
scene()->addItem(diveComputerText);
scene()->addItem(tankItem);
scene()->addItem(decoModelParameters);
@ -1365,7 +1357,7 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
if (sceneItem) {
QGraphicsItem *parentItem = sceneItem;
while (parentItem) {
if (parentItem->data(SUBSURFACE_OBJ_DATA) == SUBSURFACE_OBJ_DC_TEXT) {
if (parentItem == diveComputerText) {
isDCName = true;
break;
}