Plot the Temperature Text.

I Moved the 'plot text' method of the Old Graphics to the new
layout - this one was mostly unchanged as it was already good
as is.

And used the TemperatureProfileItem to also display texts.
This was the first implementation of the new system that uses
*less* code than the original one, wich makes me happy.

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-17 17:54:47 -02:00 committed by Dirk Hohndel
parent 4ff73cf537
commit 35979537d9
5 changed files with 96 additions and 21 deletions

View file

@ -36,4 +36,11 @@ extern QMap<color_indice_t, QVector<QColor> > profile_color;
void fill_profile_color(); void fill_profile_color();
QColor getColor(const color_indice_t i); QColor getColor(const color_indice_t i);
struct text_render_options {
double size;
color_indice_t color;
double hpos, vpos;
};
typedef text_render_options text_render_options_t;
#endif #endif

View file

@ -2,12 +2,17 @@
#include "diveplotdatamodel.h" #include "diveplotdatamodel.h"
#include "divecartesianaxis.h" #include "divecartesianaxis.h"
#include "graphicsview-common.h" #include "graphicsview-common.h"
#include "divetextitem.h"
#include "profile.h" #include "profile.h"
#include "dive.h"
#include "profilegraphics.h"
#include <QPen> #include <QPen>
#include <QPainter> #include <QPainter>
#include <QLinearGradient> #include <QLinearGradient>
#include <QDebug> #include <QDebug>
#include <QApplication>
#include <QGraphicsItem>
AbstractProfilePolygonItem::AbstractProfilePolygonItem(): QObject(), QGraphicsPolygonItem(), AbstractProfilePolygonItem::AbstractProfilePolygonItem(): QObject(), QGraphicsPolygonItem(),
hAxis(NULL), vAxis(NULL), dataModel(NULL), hDataColumn(-1), vDataColumn(-1) hAxis(NULL), vAxis(NULL), dataModel(NULL), hDataColumn(-1), vDataColumn(-1)
@ -117,15 +122,46 @@ void DiveTemperatureItem::modelDataChanged()
// Ignore empty values. things do not look good with '0' as temperature in kelvin... // Ignore empty values. things do not look good with '0' as temperature in kelvin...
QPolygonF poly; QPolygonF poly;
int last = -300, last_printed_temp = 0, sec = 0;
for (int i = 0, modelDataCount = dataModel->rowCount(); i < modelDataCount; i++) { for (int i = 0, modelDataCount = dataModel->rowCount(); i < modelDataCount; i++) {
qreal verticalValue = dataModel->index(i, vDataColumn).data().toReal(); int mkelvin = dataModel->index(i, vDataColumn).data().toInt();
if(!verticalValue) if(!mkelvin)
continue; continue;
qreal horizontalValue = dataModel->index(i, hDataColumn).data().toReal(); int sec = dataModel->index(i, hDataColumn).data().toInt();
QPointF point( hAxis->posAtValue(horizontalValue), vAxis->posAtValue(verticalValue)); QPointF point( hAxis->posAtValue(sec), vAxis->posAtValue(mkelvin));
poly.append(point); poly.append(point);
/* don't print a temperature
* if it's been less than 5min and less than a 2K change OR
* if it's been less than 2min OR if the change from the
* last print is less than .4K (and therefore less than 1F) */
if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
(sec < last + 120) ||
(abs(mkelvin - last_printed_temp) < 400))
continue;
last = sec;
if (mkelvin > 200000)
createTextItem(sec,mkelvin);
last_printed_temp = mkelvin;
} }
setPolygon(poly); setPolygon(poly);
/* it would be nice to print the end temperature, if it's
* different or if the last temperature print has been more
* than a quarter of the dive back */
int last_temperature = dataModel->data(dataModel->index(dataModel->rowCount()-1, DivePlotDataModel::TEMPERATURE)).toInt();
if (last_temperature > 200000 && ((abs(last_temperature - last_printed_temp) > 500) || ((double)last / (double)sec < 0.75))){
createTextItem(sec, last_temperature);
}
}
void DiveTemperatureItem::createTextItem(int sec, int mkelvin)
{
double deg;
const char *unit;
static text_render_options_t tro = {TEMP_TEXT_SIZE, TEMP_TEXT, LEFT, TOP};
deg = get_temp_units(mkelvin, &unit);
plotText(&tro, QPointF(hAxis->posAtValue(sec), vAxis->posAtValue(mkelvin)), QString("%1%2").arg(deg, 0, 'f', 1).arg(unit), this); //"%.2g%s"
} }
void DiveTemperatureItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) void DiveTemperatureItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
@ -146,7 +182,6 @@ void DiveGasPressureItem::modelDataChanged()
QPolygonF boundingPoly; // This is the "Whole Item", but a pressure can be divided in N Polygons. QPolygonF boundingPoly; // This is the "Whole Item", but a pressure can be divided in N Polygons.
polygons.clear(); polygons.clear();
#define M_PRESSURE( ROW )
for (int i = 0; i < dataModel->rowCount(); i++) { for (int i = 0; i < dataModel->rowCount(); i++) {
int sPressure = dataModel->index(i, DivePlotDataModel::SENSOR_PRESSURE).data().toInt(); int sPressure = dataModel->index(i, DivePlotDataModel::SENSOR_PRESSURE).data().toInt();
int iPressure = dataModel->index(i, DivePlotDataModel::INTERPOLATED_PRESSURE).data().toInt(); int iPressure = dataModel->index(i, DivePlotDataModel::INTERPOLATED_PRESSURE).data().toInt();
@ -181,4 +216,36 @@ void DiveGasPressureItem::paint(QPainter* painter, const QStyleOptionGraphicsIte
painter->drawLine(poly[i-1],poly[i]); painter->drawLine(poly[i-1],poly[i]);
} }
} }
} }
QGraphicsItemGroup *plotText(text_render_options_t* tro, const QPointF& pos, const QString& text, QGraphicsItem *parent)
{
QFont fnt(qApp->font());
QFontMetrics fm(fnt);
/*
if (printMode)
fnt.setPixelSize(tro->size);
*/
QGraphicsItemGroup *group = new QGraphicsItemGroup(parent);
QPainterPath textPath;
/* addText() uses bottom-left text baseline and the -3 offset is probably slightly off
* for different font sizes. */
textPath.addText(0, fm.height() - 3, fnt, text);
QPainterPathStroker stroker;
stroker.setWidth(3);
QGraphicsPathItem *strokedItem = new QGraphicsPathItem(stroker.createStroke(textPath), group);
strokedItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
strokedItem->setPen(Qt::NoPen);
QGraphicsPathItem *textItem = new QGraphicsPathItem(textPath, group);
textItem->setBrush(QBrush(getColor(tro->color)));
textItem->setPen(Qt::NoPen);
group->setPos(pos);
//group->setPos(pos.x() + dx, pos.y() + dy);
// if (!printMode)
group->setFlag(QGraphicsItem::ItemIgnoresTransformations);
return group;
}

View file

@ -3,7 +3,7 @@
#include <QObject> #include <QObject>
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include "graphicsview-common.h"
/* This is the Profile Item, it should be used for quite a lot of things /* This is the Profile Item, it should be used for quite a lot of things
on the profile view. The usage should be pretty simple: on the profile view. The usage should be pretty simple:
@ -18,6 +18,7 @@
This is a generically item and should be used as a base for others, I think... This is a generically item and should be used as a base for others, I think...
*/ */
class DiveTextItem;
class DiveCartesianAxis; class DiveCartesianAxis;
class QAbstractTableModel; class QAbstractTableModel;
@ -57,6 +58,8 @@ public:
DiveTemperatureItem(); DiveTemperatureItem();
virtual void modelDataChanged(); virtual void modelDataChanged();
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0); virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
private:
void createTextItem(int seconds, int mkelvin);
}; };
class DiveGasPressureItem : public AbstractProfilePolygonItem{ class DiveGasPressureItem : public AbstractProfilePolygonItem{
@ -67,4 +70,7 @@ public:
private: private:
QVector<QPolygonF> polygons; QVector<QPolygonF> polygons;
}; };
QGraphicsItemGroup *plotText(text_render_options_t *tro,const QPointF& pos, const QString& text, QGraphicsItem *parent);
#endif #endif

View file

@ -38,12 +38,6 @@ static struct graphics_context last_gc;
static double plot_scale = SCALE_SCREEN; static double plot_scale = SCALE_SCREEN;
#endif #endif
struct text_render_options {
double size;
color_indice_t color;
double hpos, vpos;
};
extern struct ev_select *ev_namelist; extern struct ev_select *ev_namelist;
extern int evn_allocated; extern int evn_allocated;
extern int evn_used; extern int evn_used;
@ -436,24 +430,25 @@ void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
scene()->addItem(rect); scene()->addItem(rect);
/* Depth profile */ /* Depth profile */
plot_depth_profile(); plot_depth_profile(); // TODO: PARTIALLY PORTED.
plot_events(dc); plot_events(dc); // PORTED
if (rulerEnabled && !printMode) if (rulerEnabled && !printMode) // TODO: NOT PORTED.
create_ruler(); create_ruler();
/* Temperature profile */ /* Temperature profile */
plot_temperature_profile(); plot_temperature_profile(); // PORTED
/* Cylinder pressure plot */ /* Cylinder pressure plot */
plot_cylinder_pressure(); plot_cylinder_pressure(); // PORTED
/* Text on top of all graphs.. */ /* Text on top of all graphs.. */ // TODO: NOT PORTED, ANY TEXT.
plot_temperature_text(); plot_temperature_text();
plot_depth_text(); plot_depth_text();
plot_cylinder_pressure_text(); plot_cylinder_pressure_text();
plot_deco_text(); plot_deco_text();
// NOT PORTED.
/* Put the dive computer name in the lower left corner */ /* Put the dive computer name in the lower left corner */
gc.leftx = 0; gc.rightx = 1.0; gc.leftx = 0; gc.rightx = 1.0;
gc.topy = 0; gc.bottomy = 1.0; gc.topy = 0; gc.bottomy = 1.0;
@ -463,11 +458,13 @@ void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
// The Time ruler should be right after the DiveComputer: // The Time ruler should be right after the DiveComputer:
timeMarkers->setPos(0, diveComputer->y()); timeMarkers->setPos(0, diveComputer->y());
// NOT PORTED.
if (PP_GRAPHS_ENABLED) { if (PP_GRAPHS_ENABLED) {
plot_pp_gas_profile(); plot_pp_gas_profile();
plot_pp_text(); plot_pp_text();
} }
// NOT PORTED.
plot_depth_scale(); plot_depth_scale();
#if 0 #if 0

View file

@ -10,10 +10,8 @@
#include <QPushButton> #include <QPushButton>
#include <QGraphicsProxyWidget> #include <QGraphicsProxyWidget>
struct text_render_options;
struct graphics_context; struct graphics_context;
struct plot_info; struct plot_info;
typedef struct text_render_options text_render_options_t;
/* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want /* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want
* or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView. * or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView.