summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/profile/tankitem.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2015-09-03 14:20:19 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-30 10:36:49 -0700
commite49d6213ad129284a45d53c3fcdc03249e84efe2 (patch)
tree2946a666ab38af3375e7bb2b8c5dd887d4a7f9a1 /desktop-widgets/profile/tankitem.cpp
parent588abd019fb2ed3f607682f2b6c7fe86a7a5bb90 (diff)
downloadsubsurface-e49d6213ad129284a45d53c3fcdc03249e84efe2.tar.gz
Move qt-ui to desktop-widgets
Since we have now destkop and mobile versions, 'qt-ui' was a very poor name choice for a folder that contains only destkop-enabled widgets. Also, move the graphicsview-common.h/cpp to subsurface-core because it doesn't depend on qgraphicsview, it merely implements all the colors that we use throughout Subsurface, and we will use colors on both desktop and mobile versions Same thing applies for metrics.h/cpp Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/profile/tankitem.cpp')
-rw-r--r--desktop-widgets/profile/tankitem.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/desktop-widgets/profile/tankitem.cpp b/desktop-widgets/profile/tankitem.cpp
new file mode 100644
index 000000000..c0e75a371
--- /dev/null
+++ b/desktop-widgets/profile/tankitem.cpp
@@ -0,0 +1,120 @@
+#include "tankitem.h"
+#include "diveplotdatamodel.h"
+#include "divetextitem.h"
+#include "profile.h"
+#include <QPen>
+
+TankItem::TankItem(QObject *parent) :
+ QGraphicsRectItem(),
+ dataModel(0),
+ pInfoEntry(0),
+ pInfoNr(0)
+{
+ height = 3;
+ QColor red(PERSIANRED1);
+ QColor blue(AIR_BLUE);
+ QColor yellow(NITROX_YELLOW);
+ QColor green(NITROX_GREEN);
+ QLinearGradient nitroxGradient(QPointF(0, 0), QPointF(0, height));
+ nitroxGradient.setColorAt(0.0, green);
+ nitroxGradient.setColorAt(0.49, green);
+ nitroxGradient.setColorAt(0.5, yellow);
+ nitroxGradient.setColorAt(1.0, yellow);
+ nitrox = nitroxGradient;
+ oxygen = green;
+ QLinearGradient trimixGradient(QPointF(0, 0), QPointF(0, height));
+ trimixGradient.setColorAt(0.0, green);
+ trimixGradient.setColorAt(0.49, green);
+ trimixGradient.setColorAt(0.5, red);
+ trimixGradient.setColorAt(1.0, red);
+ trimix = trimixGradient;
+ air = blue;
+ memset(&diveCylinderStore, 0, sizeof(diveCylinderStore));
+}
+
+TankItem::~TankItem()
+{
+ // Should this be clear_dive(diveCylinderStore)?
+ for (int i = 0; i < MAX_CYLINDERS; i++)
+ free((void *)diveCylinderStore.cylinder[i].type.description);
+}
+
+void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d)
+{
+ free(pInfoEntry);
+ // the plotInfo and dive structures passed in could become invalid before we stop using them,
+ // so copy the data that we need
+ int size = plotInfo->nr * sizeof(plotInfo->entry[0]);
+ pInfoEntry = (struct plot_data *)malloc(size);
+ pInfoNr = plotInfo->nr;
+ memcpy(pInfoEntry, plotInfo->entry, size);
+ copy_cylinders(d, &diveCylinderStore, false);
+ dataModel = model;
+ connect(dataModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
+ modelDataChanged();
+}
+
+void TankItem::createBar(qreal x, qreal w, struct gasmix *gas)
+{
+ // pick the right gradient, size, position and text
+ QGraphicsRectItem *rect = new QGraphicsRectItem(x, 0, w, height, this);
+ if (gasmix_is_air(gas))
+ rect->setBrush(air);
+ else if (gas->he.permille)
+ rect->setBrush(trimix);
+ else if (gas->o2.permille == 1000)
+ rect->setBrush(oxygen);
+ else
+ rect->setBrush(nitrox);
+ rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle
+ rects.push_back(rect);
+ DiveTextItem *label = new DiveTextItem(rect);
+ label->setText(gasname(gas));
+ label->setBrush(Qt::black);
+ label->setPos(x + 1, 0);
+ label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
+ label->setZValue(101);
+}
+
+void TankItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+{
+ // We don't have enougth data to calculate things, quit.
+
+ if (!dataModel || !pInfoEntry || !pInfoNr)
+ return;
+
+ // remove the old rectangles
+ foreach (QGraphicsRectItem *r, rects) {
+ delete(r);
+ }
+ rects.clear();
+
+ // walk the list and figure out which tanks go where
+ struct plot_data *entry = pInfoEntry;
+ int cylIdx = entry->cylinderindex;
+ int i = -1;
+ int startTime = 0;
+ struct gasmix *gas = &diveCylinderStore.cylinder[cylIdx].gasmix;
+ qreal width, left;
+ while (++i < pInfoNr) {
+ entry = &pInfoEntry[i];
+ if (entry->cylinderindex == cylIdx)
+ continue;
+ width = hAxis->posAtValue(entry->sec) - hAxis->posAtValue(startTime);
+ left = hAxis->posAtValue(startTime);
+ createBar(left, width, gas);
+ cylIdx = entry->cylinderindex;
+ gas = &diveCylinderStore.cylinder[cylIdx].gasmix;
+ startTime = entry->sec;
+ }
+ width = hAxis->posAtValue(entry->sec) - hAxis->posAtValue(startTime);
+ left = hAxis->posAtValue(startTime);
+ createBar(left, width, gas);
+}
+
+void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal)
+{
+ hAxis = horizontal;
+ connect(hAxis, SIGNAL(sizeChanged()), this, SLOT(modelDataChanged()));
+ modelDataChanged();
+}