Make part of the context menu work (gas change event)

The code of the context menu and the gas change event callback is mostly
the same as the old profile, with minimum modifications, as this changes
the order of the code on the callback to make it a bit saner (declare
variables first, call code later).

This also fixes a bug on the model that was not cleaning itself in the
correct way after a call to clear.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-02-17 19:15:40 -03:00 committed by Dirk Hohndel
parent d81223b3f4
commit 31ee4dac65
4 changed files with 83 additions and 2 deletions

View file

@ -100,6 +100,8 @@ void DivePlotDataModel::clear()
if (rowCount() != 0) {
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
pInfo.nr = 0;
diveId = -1;
dcNr = -1;
endRemoveRows();
}
}

View file

@ -142,7 +142,6 @@ private:
int meanDepth;
DiveTextItem *leftText;
DiveTextItem *rightText;
DiveCartesianAxis *axis;
};
class PartialPressureGasItem : public AbstractProfilePolygonItem{

View file

@ -10,6 +10,7 @@
#include "divetextitem.h"
#include "divetooltipitem.h"
#include "animationfunctions.h"
#include "planner.h"
#include <QSignalTransition>
#include <QPropertyAnimation>
#include <QMenu>
@ -543,3 +544,80 @@ void ProfileWidget2::setProfileState()
event->setVisible(true);
}
}
extern struct ev_select *ev_namelist;
extern int evn_allocated;
extern int evn_used;
void ProfileWidget2::contextMenuEvent(QContextMenuEvent* event)
{
if (selected_dive == -1)
return;
QMenu m;
QMenu *gasChange = m.addMenu(tr("Add Gas Change"));
GasSelectionModel *model = GasSelectionModel::instance();
model->repopulate();
int rowCount = model->rowCount();
for (int i = 0; i < rowCount; i++) {
QAction *action = new QAction(&m);
action->setText( model->data(model->index(i, 0),Qt::DisplayRole).toString());
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeGas()));
action->setData(event->globalPos());
gasChange->addAction(action);
}
QAction *action = m.addAction(tr("Add Bookmark"), this, SLOT(addBookmark()));
action->setData(event->globalPos());
QList<QGraphicsItem*> itemsAtPos = scene()->items(mapToScene(mapFromGlobal(event->globalPos())));
Q_FOREACH(QGraphicsItem *i, itemsAtPos) {
EventItem *item = dynamic_cast<EventItem*>(i);
if (!item)
continue;
action = new QAction(&m);
action->setText(tr("Remove Event"));
action->setData(QVariant::fromValue<void*>(item)); // so we know what to remove.
connect(action, SIGNAL(triggered(bool)), this, SLOT(removeEvent()));
m.addAction(action);
action = new QAction(&m);
action->setText(tr("Hide similar events"));
action->setData(QVariant::fromValue<void*>(item));
connect(action, SIGNAL(triggered(bool)), this, SLOT(hideEvents()));
m.addAction(action);
break;
}
bool some_hidden = false;
for (int i = 0; i < evn_used; i++) {
if (ev_namelist[i].plot_ev == false) {
some_hidden = true;
break;
}
}
if (some_hidden) {
action = m.addAction(tr("Unhide all events"), this, SLOT(unhideEvents()));
action->setData(event->globalPos());
}
m.exec(event->globalPos());
}
void ProfileWidget2::changeGas()
{
QAction *action = qobject_cast<QAction*>(sender());
QPointF scenePos = mapToScene(mapFromGlobal(action->data().toPoint()));
QString gas = action->text();
// backup the things on the dataModel, since we will clear that out.
int diveComputer = dataModel->dcShown();
int diveId = dataModel->id();
int o2, he;
int seconds = timeAxis->valueAt(scenePos);
struct dive *d = getDiveById(diveId);
validate_gas(gas.toUtf8().constData(), &o2, &he);
add_gas_switch_event(d, get_dive_dc(d, diveComputer), seconds, get_gasidx(d, o2, he));
// this means we potentially have a new tank that is being used and needs to be shown
fixup_dive(d);
MainWindow::instance()->information()->updateDiveInfo(selected_dive);
mark_divelist_changed(true);
// force the redraw of the dive.
//TODO: find a way to make this do not need a full redraw
dataModel->clear();
plotDives(QList<dive*>() << getDiveById(diveId));
}

View file

@ -54,11 +54,13 @@ public slots: // Necessary to call from QAction's signals.
void settingsChanged();
void setEmptyState();
void setProfileState();
void changeGas();
protected:
virtual void resizeEvent(QResizeEvent* event);
virtual void wheelEvent(QWheelEvent* event);
virtual void mouseMoveEvent(QMouseEvent* event);
virtual void contextMenuEvent(QContextMenuEvent* event);
private: /*methods*/
void fixBackgroundPos();
void scrollViewTo(const QPoint& pos);