Added the Temperature Graph.

Added the Temperature Graph with its related classes. A Temperature Axis
is also created so the item is plotted on the right place.
Currently the Temperature Axis is just like the depth axis - top is
zero, wich means that the graph is inverted.

Also, the Temperature axis is being displayed as this helps debugging.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-01-16 18:39:13 -02:00 committed by Dirk Hohndel
parent 254beef5d4
commit 58aeb6ce40
6 changed files with 94 additions and 10 deletions

View file

@ -143,15 +143,16 @@ qreal DiveCartesianAxis::posAtValue(qreal value)
QPointF p = pos();
double size = max - min;
double percent = value / size;
double distanceFromOrigin = value - min;
double percent = (value - min) / size;
double realSize = orientation == Qt::Horizontal ?
m.x2() - m.x1() :
m.y2() - m.y1();
double retValue = realSize * percent;
retValue = (orientation == Qt::Horizontal) ?
double adjusted = (orientation == Qt::Horizontal) ?
retValue + m.x1() + p.x() :
retValue + m.y1() + p.y();
return retValue;
return adjusted;
}
qreal DiveCartesianAxis::percentAt(const QPointF& p)
@ -192,6 +193,12 @@ QString TimeAxis::textForValue(double value)
return QString::number(value / 60);
}
QString TemperatureAxis::textForValue(double value)
{
return QString::number(mkelvin_to_C( (int) value));
}
void DiveCartesianPlane::setLeftAxis(DiveCartesianAxis* axis)
{
leftAxis = axis;

View file

@ -46,12 +46,18 @@ protected:
class DepthAxis : public DiveCartesianAxis {
protected:
QString textForValue(double value);
QString textForValue(double value);
};
class TimeAxis : public DiveCartesianAxis {
protected:
QString textForValue(double value);
QString textForValue(double value);
};
class TemperatureAxis : public DiveCartesianAxis{
Q_OBJECT
protected:
QString textForValue(double value);
};
// This is a try. Maybe the CartesianPlane should have the X and Y

View file

@ -6,6 +6,7 @@
#include <QPen>
#include <QPainter>
#include <QLinearGradient>
#include <QDebug>
AbstractProfilePolygonItem::AbstractProfilePolygonItem(): QObject(), QGraphicsPolygonItem(),
hAxis(NULL), vAxis(NULL), dataModel(NULL), hDataColumn(-1), vDataColumn(-1)
@ -96,4 +97,38 @@ void DiveProfileItem::modelDataChanged(){
pat.setColorAt(1, getColor(DEPTH_BOTTOM));
pat.setColorAt(0, getColor(DEPTH_TOP));
setBrush(QBrush(pat));
}
}
DiveTemperatureItem::DiveTemperatureItem()
{
QPen pen;
pen.setBrush(QBrush(getColor(::TEMP_PLOT)));
pen.setCosmetic(true);
pen.setWidth(2);
setPen(pen);
}
void DiveTemperatureItem::modelDataChanged()
{
// We don't have enougth data to calculate things, quit.
if (!hAxis || !vAxis || !dataModel || hDataColumn == -1 || vDataColumn == -1)
return;
// Ignore empty values. things do not look good with '0' as temperature in kelvin...
QPolygonF poly;
for (int i = 0, modelDataCount = dataModel->rowCount(); i < modelDataCount; i++) {
qreal verticalValue = dataModel->index(i, vDataColumn).data().toReal();
if(!verticalValue)
continue;
qreal horizontalValue = dataModel->index(i, hDataColumn).data().toReal();
QPointF point( hAxis->posAtValue(horizontalValue), vAxis->posAtValue(verticalValue));
poly.append(point);
}
setPolygon(poly);
}
void DiveTemperatureItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->setPen(pen());
painter->drawPolyline(polygon());
}

View file

@ -47,8 +47,15 @@ protected:
class DiveProfileItem : public AbstractProfilePolygonItem{
Q_OBJECT
public:
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
virtual void modelDataChanged();
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
virtual void modelDataChanged();
};
class DiveTemperatureItem : public AbstractProfilePolygonItem{
Q_OBJECT
public:
DiveTemperatureItem();
virtual void modelDataChanged();
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
};
#endif

View file

@ -13,7 +13,7 @@
#include <QPropertyAnimation>
#include <QMenu>
#include <QContextMenuEvent>
#include <QDebug>
#ifndef QT_NO_DEBUG
#include <QTableView>
@ -27,6 +27,7 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) :
stateMachine(new QStateMachine(this)),
background (new DivePixmapItem()),
profileYAxis(new DepthAxis()),
temperatureAxis(new TemperatureAxis()),
gasYAxis(new DiveCartesianAxis()),
timeAxis(new TimeAxis()),
depthController(new DiveRectItem()),
@ -60,6 +61,13 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) :
profileYAxis->setX(2);
profileYAxis->setTickSize(1);
gasYAxis->setLine(0, 0, 0, 20);
temperatureAxis->setOrientation(Qt::Vertical);
temperatureAxis->setLine(0, 60, 0, 90);
temperatureAxis->setX(3);
temperatureAxis->setTickSize(2);
temperatureAxis->setTickInterval(300);
timeAxis->setLine(0,0,96,0);
timeAxis->setX(3);
timeAxis->setTickSize(1);
@ -73,7 +81,7 @@ ProfileWidget2::ProfileWidget2(QWidget *parent) :
// insert in the same way it's declared on the Enum. This is needed so we don't use an map.
QList<QGraphicsItem*> stateItems; stateItems << background << profileYAxis << gasYAxis <<
timeAxis << depthController << timeController;
timeAxis << depthController << timeController << temperatureAxis;
Q_FOREACH(QGraphicsItem *item, stateItems) {
scene()->addItem(item);
}
@ -268,6 +276,9 @@ void ProfileWidget2::plotDives(QList<dive*> dives)
// each item, I'll mostly like to fix this in the future, but I'll keep at this for now.
profileYAxis->setMaximum(qMax<long>(pInfo.maxdepth + M_OR_FT(10,30), maxdepth * 2 / 3));
profileYAxis->updateTicks();
temperatureAxis->setMinimum(pInfo.mintemp);
temperatureAxis->setMaximum(pInfo.maxtemp);
temperatureAxis->updateTicks();
timeAxis->setMaximum(maxtime);
timeAxis->updateTicks();
dataModel->setDive(current_dive, pInfo);
@ -299,6 +310,20 @@ void ProfileWidget2::plotDives(QList<dive*> dives)
eventItems.push_back(item);
event = event->next;
}
if(temperatureItem){
scene()->removeItem(temperatureItem);
delete temperatureItem;
}
temperatureItem = new DiveTemperatureItem();
temperatureItem->setHorizontalAxis(timeAxis);
temperatureItem->setVerticalAxis(temperatureAxis);
temperatureItem->setModel(dataModel);
temperatureItem->setVerticalDataColumn(DivePlotDataModel::TEMPERATURE);
temperatureItem->setHorizontalDataColumn(DivePlotDataModel::TIME);
scene()->addItem(temperatureItem);
emit startProfileState();
}

View file

@ -15,6 +15,7 @@
// */
#include "graphicsview-common.h"
class TemperatureAxis;
class DiveEventItem;
struct DivePlotDataModel;
struct DivePixmapItem;
@ -26,6 +27,7 @@ struct TimeAxis;
struct dive;
struct QStateMachine;
struct DiveCartesianPlane;
struct DiveTemperatureItem;
struct plot_info;
class ProfileWidget2 : public QGraphicsView {
@ -66,11 +68,13 @@ private:
struct plot_info *plotInfo;
DepthAxis *profileYAxis ;
DiveCartesianAxis *gasYAxis;
TemperatureAxis *temperatureAxis;
TimeAxis *timeAxis;
DiveRectItem *depthController;
DiveRectItem *timeController;
DiveProfileItem *diveProfileItem;
DiveCartesianPlane *cartesianPlane;
DiveTemperatureItem *temperatureItem;
QList<DiveEventItem*> eventItems;
};