With the Gas Change Event working, the rest's not yet. :)
From 77de8cfef44038a122bd1bf72abefd2f8b6940df Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tomaz.canabr...@intel.com>
Date: Mon, 17 Feb 2014 19:09:06 -0300
Subject: [PATCH] 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,
I changed a bit the order of the code on the callback to make
it a bit saner ( declare variables first, call code later )

and fixed 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.canabr...@intel.com>
---
 qt-ui/profile/diveplotdatamodel.cpp |  2 +
 qt-ui/profile/diveprofileitem.h     |  1 -
 qt-ui/profile/profilewidget2.cpp    | 78 +++++++++++++++++++++++++++++++++++++
 qt-ui/profile/profilewidget2.h      |  4 +-
 4 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/qt-ui/profile/diveplotdatamodel.cpp b/qt-ui/profile/diveplotdatamodel.cpp
index 700ca65..1111965 100644
--- a/qt-ui/profile/diveplotdatamodel.cpp
+++ b/qt-ui/profile/diveplotdatamodel.cpp
@@ -100,6 +100,8 @@ void DivePlotDataModel::clear()
 	if (rowCount() != 0) {
 		beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
 		pInfo.nr = 0;
+		diveId = -1;
+		dcNr = -1;
 		endRemoveRows();
 	}
 }
diff --git a/qt-ui/profile/diveprofileitem.h b/qt-ui/profile/diveprofileitem.h
index b519581..e8bac04 100644
--- a/qt-ui/profile/diveprofileitem.h
+++ b/qt-ui/profile/diveprofileitem.h
@@ -142,7 +142,6 @@ private:
 	int meanDepth;
 	DiveTextItem *leftText;
 	DiveTextItem *rightText;
-	DiveCartesianAxis *axis;
 };
 
 class PartialPressureGasItem : public AbstractProfilePolygonItem{
diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp
index 659ac1c..bba1a96 100644
--- a/qt-ui/profile/profilewidget2.cpp
+++ b/qt-ui/profile/profilewidget2.cpp
@@ -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));
+}
\ No newline at end of file
diff --git a/qt-ui/profile/profilewidget2.h b/qt-ui/profile/profilewidget2.h
index b7cdbff..44f92f7 100644
--- a/qt-ui/profile/profilewidget2.h
+++ b/qt-ui/profile/profilewidget2.h
@@ -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);
-- 
1.9.0

_______________________________________________
subsurface mailing list
subsurface@hohndel.org
http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to