On 12/01/2015 22:21, Dirk Hohndel wrote:
On Mon, Jan 12, 2015 at 09:53:40PM +0200, Willem Ferguson wrote:
Dirk,
Writing from phone. The first few values of o2cyl and dilcyl are random,
unitialised values.
Bad. Really BAD. They should be initialized to something known - like
"-1".
Relying on uninitialized values being out of range is really not a way to
write good software.

This gives rise to out-of-range array indices. I think
I found a much more efficient way to solve this. Initialise o2cyl and
dilcyl to -1 where they are declared and just test whether both of the two
are >= 0. (ie point to valid cylinders). Will send improved patch tomorrow
morning.
Yes, that's EXACTLY what I'd want you to do.

Thanks

/D


Tomaz,

I need help, please.

See the attached patch. I encountered an ugly aspect of writing the text adjacent to the cylinder pressure graphs. The distance between the text and the line of the graph is dependent on the physical height of the profile widget. With a fullscreen Subsurface, the distance between text and graph is much larger than with a Subsurface in a window filling only part of the screen. So, the distance between text and graph needs to be scaled to the height of the depth profile within the profile widget. See image1 (for a flatter depth profile, around 300 pixels high) and image2 (for a much higher depth profile, maybe around 800 pixels high), attached.

I need to get the value of the height of the depth profile from within DiveGasPressureItem::modelDataChanged() in diveprofileitem.cpp in order to do the proper scaling. Where could I find this?

While I am about it, I should also scale the placement of the text with respect to the font size. Please, is there any way of getting the font size within the same method as the above?

Kind regards,
willem



>From e1a2a7166f28034a84737f46092c8c23e991ec21 Mon Sep 17 00:00:00 2001
From: willem ferguson <[email protected]>
Date: Tue, 13 Jan 2015 09:25:47 +0200
Subject: [PATCH] Patch to write cylinder pressures of CCR dives in a way as to
 not overwrite each other.

Signed-off-by: willem ferguson <[email protected]>
---
 qt-ui/profile/diveprofileitem.cpp | 56 ++++++++++++++++++++++++++++++---------
 qt-ui/profile/diveprofileitem.h   |  4 +--
 2 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/qt-ui/profile/diveprofileitem.cpp b/qt-ui/profile/diveprofileitem.cpp
index c049d91..c32d468 100644
--- a/qt-ui/profile/diveprofileitem.cpp
+++ b/qt-ui/profile/diveprofileitem.cpp
@@ -635,7 +635,7 @@ void DiveGasPressureItem::modelDataChanged(const QModelIndex &topLeft, const QMo
 			o2mbar = GET_O2CYLINDER_PRESSURE(entry);
 
 		if (entry->cylinderindex != last_index) {
-			polygons.append(QPolygonF()); // this is the polygon that will be actually drawned on screen.
+			polygons.append(QPolygonF()); // this is the polygon that will be actually drawn on screen.
 			last_index = entry->cylinderindex;
 		}
 		if (!mbar) {
@@ -662,30 +662,54 @@ void DiveGasPressureItem::modelDataChanged(const QModelIndex &topLeft, const QMo
 
 	cyl = -1;
 	o2mbar = 0;
+
+	double print_y_offset[8][3] = { { 0, -0.5, -0.5 }, { 0, -0.5, -0.5 }, { 0, -0.5, -0.5 }, { 0, -0.5, -0.5 },
+					{ 0, -0.5, -0.5 }, { 0, -0.5, -0.5 }, { 0, -0.5, -0.5 }, { 0, -0.5, -0.5 }, };
+	// CCR dives: These are offset values used to print the gas lables and pressures on a CCR dive profile at
+	// appropriate Y-coordinates: One triplet of values for each of 8 cylinders.
+	// Order within a triplet: start gas lable, start gas pressure, end gas pressure.
+	// The array is initialised with default values that apply to non-CCR dives.
+
+	bool offsets_initialised = false;
+	int o2cyl = -1, dilcyl = -1;
 	for (int i = 0, count = dataModel->rowCount(); i < count; i++) {
 		entry = dataModel->data().entry + i;
 		mbar = GET_PRESSURE(entry);
 		if (displayed_dive.dc.dctype == CCR && displayed_dive.oxygen_cylinder_index >= 0)
 			o2mbar = GET_O2CYLINDER_PRESSURE(entry);
-
 		if (o2mbar) {
+			// The first time an o2 value is detected, see if the oxygen cyl pressure graph starts above or below the dil graph
+			if (!offsets_initialised) {
+				o2cyl = displayed_dive.oxygen_cylinder_index;
+				dilcyl = displayed_dive.diluent_cylinder_index;
+				if ((o2mbar > mbar)) {	// If above, write o2 start cyl pressure above graph and diluent pressure below graph:
+					print_y_offset[o2cyl][0] = -5; // y offset for oxygen gas lable
+					print_y_offset[o2cyl][1] = -3; // y offset for oxygen start pressure value
+					print_y_offset[dilcyl][0] = 2; // y offset for diluent gas lable
+					print_y_offset[dilcyl][1] = 2; // y offset for diluent start pressure value
+				} else {
+					print_y_offset[o2cyl][0]  = 2; // write o2 start cyl pressure below graph
+					print_y_offset[o2cyl][1]  = 0;
+					print_y_offset[dilcyl][0] = -5; // and diluent pressure above graph.
+				}
+			offsets_initialised = true;
+			}
 			if (!seen_cyl[displayed_dive.oxygen_cylinder_index]) {
-				plotPressureValue(o2mbar, entry->sec, Qt::AlignRight | Qt::AlignBottom);
-				plotGasValue(o2mbar, entry->sec, Qt::AlignRight | Qt::AlignBottom, displayed_dive.cylinder[displayed_dive.oxygen_cylinder_index].gasmix);
+				plotPressureValue(o2mbar, print_y_offset[o2cyl][1], entry->sec, Qt::AlignRight | Qt::AlignBottom);
+				plotGasValue(o2mbar, print_y_offset[o2cyl][0], entry->sec, Qt::AlignRight | Qt::AlignBottom, displayed_dive.cylinder[displayed_dive.oxygen_cylinder_index].gasmix);
 				seen_cyl[displayed_dive.oxygen_cylinder_index] = true;
 			}
 			last_pressure[displayed_dive.oxygen_cylinder_index] = o2mbar;
 			last_time[displayed_dive.oxygen_cylinder_index] = entry->sec;
 		}
 
-
 		if (!mbar)
 			continue;
 		if (cyl != entry->cylinderindex) {
 			cyl = entry->cylinderindex;
 			if (!seen_cyl[cyl]) {
-				plotPressureValue(mbar, entry->sec, Qt::AlignRight | Qt::AlignTop);
-				plotGasValue(mbar, entry->sec, Qt::AlignRight | Qt::AlignBottom,
+				plotPressureValue(mbar, print_y_offset[cyl][1], entry->sec, Qt::AlignRight | Qt::AlignTop);
+				plotGasValue(mbar, print_y_offset[cyl][0], entry->sec, Qt::AlignRight | Qt::AlignBottom,
 					     displayed_dive.cylinder[cyl].gasmix);
 				seen_cyl[cyl] = true;
 			}
@@ -694,30 +718,38 @@ void DiveGasPressureItem::modelDataChanged(const QModelIndex &topLeft, const QMo
 		last_time[cyl] = entry->sec;
 	}
 
+	if ((o2cyl >= 0) && (dilcyl >= 0)) {  // If valid cylinder indices have been assigned to both cylinders:
+		if (last_pressure[o2cyl] > last_pressure[dilcyl]) { // If oxygen cyl pressure graph ends above diluent graph:
+			print_y_offset[dilcyl][2] = 2;	// offset to write diluent cyl end pressure underneath the graph
+		} else {
+			print_y_offset[o2cyl][2]  = 2;	// else to write oxygen cyl end pressure underneath the graph
+		}
+	}
+
 	for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
 		if (last_time[cyl]) {
-			plotPressureValue(last_pressure[cyl], last_time[cyl], Qt::AlignLeft | Qt::AlignTop);
+			plotPressureValue(last_pressure[cyl], print_y_offset[cyl][2], last_time[cyl], Qt::AlignLeft | Qt::AlignTop);
 		}
 	}
 }
 
-void DiveGasPressureItem::plotPressureValue(int mbar, int sec, QFlags<Qt::AlignmentFlag> flags)
+void DiveGasPressureItem::plotPressureValue(int mbar, double pressure_offset, int sec, QFlags<Qt::AlignmentFlag> flags)
 {
 	const char *unit;
 	int pressure = get_pressure_units(mbar, &unit);
 	DiveTextItem *text = new DiveTextItem(this);
-	text->setPos(hAxis->posAtValue(sec), vAxis->posAtValue(mbar) - 0.5);
+	text->setPos(hAxis->posAtValue(sec), vAxis->posAtValue(mbar) + pressure_offset);
 	text->setText(QString("%1 %2").arg(pressure).arg(unit));
 	text->setAlignment(flags);
 	text->setBrush(getColor(PRESSURE_TEXT));
 	texts.push_back(text);
 }
 
-void DiveGasPressureItem::plotGasValue(int mbar, int sec, QFlags<Qt::AlignmentFlag> flags, struct gasmix gasmix)
+void DiveGasPressureItem::plotGasValue(int mbar, double gasname_offset, int sec, QFlags<Qt::AlignmentFlag> flags, struct gasmix gasmix)
 {
 	QString gas = gasToStr(gasmix);
 	DiveTextItem *text = new DiveTextItem(this);
-	text->setPos(hAxis->posAtValue(sec), vAxis->posAtValue(mbar));
+	text->setPos(hAxis->posAtValue(sec), vAxis->posAtValue(mbar) + gasname_offset);
 	text->setText(gas);
 	text->setAlignment(flags);
 	text->setBrush(getColor(PRESSURE_TEXT));
diff --git a/qt-ui/profile/diveprofileitem.h b/qt-ui/profile/diveprofileitem.h
index b0693cd..a9c11ed 100644
--- a/qt-ui/profile/diveprofileitem.h
+++ b/qt-ui/profile/diveprofileitem.h
@@ -165,8 +165,8 @@ public:
 	virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
 
 private:
-	void plotPressureValue(int mbar, int sec, QFlags<Qt::AlignmentFlag> align);
-	void plotGasValue(int mbar, int sec, QFlags<Qt::AlignmentFlag> align, struct gasmix gasmix);
+	void plotPressureValue(int mbar, double offset, int sec, QFlags<Qt::AlignmentFlag> align);
+	void plotGasValue(int mbar, double offset, int sec, QFlags<Qt::AlignmentFlag> align, struct gasmix gasmix);
 	QVector<QPolygonF> polygons;
 };
 
-- 
1.9.1

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to