Stop passing around gc and pi

Make the graphics_context part of the ProfileGraphicsView and remember
that the plot info is already a part of the graphics_context (we kept
passing around both of them in the Gtk code... pointless but a leftover
from before adding the pi to the gc...)

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-05-08 14:56:06 -07:00
parent 688276b6f2
commit 6f06c31d0b
5 changed files with 103 additions and 98 deletions

View file

@ -58,7 +58,7 @@ extern void plot(struct graphics_context *gc, struct dive *dive, scale_mode_t sc
extern struct divecomputer *select_dc(struct divecomputer *main);
extern void init_profile_background(struct graphics_context *gc);
extern void attach_tooltip(int x, int y, int w, int h, const char *text, struct event *event);
extern void get_plot_details(struct graphics_context *gc, int time, char *buf, size_t bufsize);
extern void get_plot_details(struct graphics_context *gc, int time, char *buf, int bufsize);
extern int x_to_time(double x);
extern int x_abs(double x);

View file

@ -1551,7 +1551,7 @@ struct divecomputer *select_dc(struct divecomputer *main)
return main;
}
static void plot_string(struct plot_data *entry, char *buf, size_t bufsize,
static void plot_string(struct plot_data *entry, char *buf, int bufsize,
int depth, int pressure, int temp, gboolean has_ndl)
{
int pressurevalue, mod, ead, end, eadd;
@ -1635,7 +1635,7 @@ static void plot_string(struct plot_data *entry, char *buf, size_t bufsize,
free(buf2);
}
void get_plot_details(struct graphics_context *gc, int time, char *buf, size_t bufsize)
void get_plot_details(struct graphics_context *gc, int time, char *buf, int bufsize)
{
struct plot_info *pi = &gc->pi;
int pressure = 0, temp = 0;

View file

@ -84,6 +84,10 @@ int get_maxdepth(struct plot_info *pi);
#define MIDDLE (0)
#define BOTTOM (-1)
#define SCALEXGC(x) (((x) - gc.leftx) / (gc.rightx - gc.leftx) * gc.maxx)
#define SCALEYGC(y) (((y) - gc.topy) / (gc.bottomy - gc.topy) * gc.maxy)
#define SCALEGC(x,y) SCALEXGC(x),SCALEYGC(y)
#define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
#define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
#define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)

View file

@ -26,7 +26,6 @@
#define VELOCITY_COLORS_START_IDX VELO_STABLE
#define VELOCITY_COLORS 5
static struct graphics_context last_gc;
static double plot_scale = SCALE_SCREEN;
@ -184,12 +183,10 @@ void ProfileGraphicsView::plot(struct dive *dive)
scene()->addItem(toolTip);
struct plot_info *pi;
struct divecomputer *dc = &dive->dc;
// This was passed around in the Cairo version / needed?
graphics_context gc;
const char *nickname;
// const char *nickname;
// Fix this for printing / screen later.
// plot_set_scale(scale_mode_t);
@ -237,15 +234,15 @@ void ProfileGraphicsView::plot(struct dive *dive)
dc = select_dc(dc);
/* This is per-dive-computer. Right now we just do the first one */
pi = create_plot_info(dive, dc, &gc);
gc.pi = *create_plot_info(dive, dc, &gc);
/* Depth profile */
plot_depth_profile(&gc, pi);
plot_depth_profile();
plot_events(&gc, pi, dc);
plot_events(dc);
/* Temperature profile */
plot_temperature_profile(&gc, pi);
plot_temperature_profile();
#if 0
/* Cylinder pressure plot */
plot_cylinder_pressure(gc, pi, dive, dc);
@ -300,7 +297,7 @@ void ProfileGraphicsView::plot(struct dive *dive)
#endif
}
void ProfileGraphicsView::plot_events(struct graphics_context *gc, struct plot_info *pi, struct divecomputer *dc)
void ProfileGraphicsView::plot_events(struct divecomputer *dc)
{
struct event *event = dc->events;
@ -309,14 +306,15 @@ void ProfileGraphicsView::plot_events(struct graphics_context *gc, struct plot_i
// }
while (event) {
plot_one_event(gc, pi, event);
plot_one_event(event);
event = event->next;
}
}
void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *ev)
void ProfileGraphicsView::plot_one_event(struct event *ev)
{
int i, depth = 0;
struct plot_info *pi = &gc.pi;
/* is plotting this event disabled? */
if (ev->name) {
@ -344,8 +342,8 @@ void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plo
/* draw a little triangular marker and attach tooltip */
int x = SCALEX(gc, ev->time.seconds);
int y = SCALEY(gc, depth);
int x = SCALEXGC(ev->time.seconds);
int y = SCALEYGC(depth);
EventItem *item = new EventItem();
item->setPos(x, y);
@ -379,7 +377,7 @@ void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plo
item->setToolTip(name);
}
void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
void ProfileGraphicsView::plot_depth_profile()
{
int i, incr;
int sec, depth;
@ -388,10 +386,10 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
int increments[8] = { 10, 20, 30, 60, 5*60, 10*60, 15*60, 30*60 };
/* Get plot scaling limits */
maxtime = get_maxtime(pi);
maxdepth = get_maxdepth(pi);
maxtime = get_maxtime(&gc.pi);
maxdepth = get_maxdepth(&gc.pi);
gc->maxtime = maxtime;
gc.maxtime = maxtime;
/* Time markers: at most every 10 seconds, but no more than 12 markers.
* We start out with 10 seconds and increment up to 30 minutes,
@ -407,15 +405,15 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
while (maxtime / incr > 12)
incr *= 2;
gc->leftx = 0; gc->rightx = maxtime;
gc->topy = 0; gc->bottomy = 1.0;
gc.leftx = 0; gc.rightx = maxtime;
gc.topy = 0; gc.bottomy = 1.0;
last_gc = *gc;
last_gc = gc;
QColor color;
color = profile_color[TIME_GRID].at(0);
for (i = incr; i < maxtime; i += incr) {
QGraphicsLineItem *line = new QGraphicsLineItem(SCALE(gc, i, 0), SCALE(gc, i, 1));
QGraphicsLineItem *line = new QGraphicsLineItem(SCALEGC(i, 0), SCALEGC(i, 1));
line->setPen(QPen(color));
scene()->addItem(line);
}
@ -425,16 +423,16 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
if (maxtime < 600) {
/* Be a bit more verbose with shorter dives */
for (i = incr; i < maxtime; i += incr)
plot_text(gc, &tro, i, 1, QString("%1:%2").arg(i/60).arg(i%60));
plot_text(&tro, i, 1, QString("%1:%2").arg(i/60).arg(i%60));
} else {
/* Only render the time on every second marker for normal dives */
for (i = incr; i < maxtime; i += 2 * incr)
plot_text(gc, &tro, i, 1, QString::number(i/60));
plot_text(&tro, i, 1, QString::number(i/60));
}
/* Depth markers: every 30 ft or 10 m*/
gc->leftx = 0; gc->rightx = 1.0;
gc->topy = 0; gc->bottomy = maxdepth;
gc.leftx = 0; gc.rightx = 1.0;
gc.topy = 0; gc.bottomy = maxdepth;
switch (prefs.units.length) {
case units::METERS:
marker = 10000;
@ -443,22 +441,23 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
marker = 9144;
break; /* 30 ft */
}
maxline = MAX(pi->maxdepth + marker, maxdepth * 2 / 3);
maxline = MAX(gc.pi.maxdepth + marker, maxdepth * 2 / 3);
color = profile_color[DEPTH_GRID].at(0);
for (i = marker; i < maxline; i += marker) {
QGraphicsLineItem *line = new QGraphicsLineItem(SCALE(gc, 0, i), SCALE(gc, 1, i));
QGraphicsLineItem *line = new QGraphicsLineItem(SCALEGC(0, i), SCALEGC(1, i));
line->setPen(QPen(color));
scene()->addItem(line);
}
gc->leftx = 0; gc->rightx = maxtime;
gc.leftx = 0; gc.rightx = maxtime;
color = profile_color[MEAN_DEPTH].at(0);
/* Show mean depth */
if (! gc->printer) {
QGraphicsLineItem *line = new QGraphicsLineItem(SCALE(gc, 0, pi->meandepth), SCALE(gc, pi->entry[pi->nr - 1].sec, pi->meandepth));
if (! gc.printer) {
QGraphicsLineItem *line = new QGraphicsLineItem(SCALEGC(0, gc.pi.meandepth),
SCALEGC(gc.pi.entry[gc.pi.nr - 1].sec, gc.pi.meandepth));
line->setPen(QPen(color));
scene()->addItem(line);
}
@ -475,27 +474,27 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
#endif
/* Do the depth profile for the neat fill */
gc->topy = 0; gc->bottomy = maxdepth;
gc.topy = 0; gc.bottomy = maxdepth;
entry = pi->entry;
entry = gc.pi.entry;
QPolygonF p;
QLinearGradient pat(0.0,0.0,0.0,scene()->height());
QGraphicsPolygonItem *neatFill = NULL;
p.append( QPointF(SCALE(gc, 0, 0) ));
for (i = 0; i < pi->nr; i++, entry++)
p.append( QPointF( SCALE(gc, entry->sec, entry->depth) ));
p.append(QPointF(SCALEGC(0, 0)));
for (i = 0; i < gc.pi.nr; i++, entry++)
p.append(QPointF(SCALEGC(entry->sec, entry->depth)));
/* Show any ceiling we may have encountered */
for (i = pi->nr - 1; i >= 0; i--, entry--) {
for (i = gc.pi.nr - 1; i >= 0; i--, entry--) {
if (entry->ndl) {
/* non-zero NDL implies this is a safety stop, no ceiling */
p.append( QPointF( SCALE(gc, entry->sec, 0) ));
p.append(QPointF(SCALEGC(entry->sec, 0)));
} else if (entry->stopdepth < entry->depth) {
p.append( QPointF( SCALE(gc, entry->sec, entry->stopdepth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->stopdepth)));
} else {
p.append( QPointF( SCALE(gc, entry->sec, entry->depth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->depth)));
}
}
pat.setColorAt(1, profile_color[DEPTH_BOTTOM].first());
@ -518,17 +517,17 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
pat.setColorAt(0, profile_color[CEILING_SHALLOW].first());
pat.setColorAt(1, profile_color[CEILING_DEEP].first());
entry = pi->entry;
p.append( QPointF(SCALE(gc, 0, 0) ));
for (i = 0; i < pi->nr; i++, entry++) {
entry = gc.pi.entry;
p.append(QPointF(SCALEGC(0, 0)));
for (i = 0; i < gc.pi.nr; i++, entry++) {
if (entry->ndl == 0 && entry->stopdepth) {
if (entry->ndl == 0 && entry->stopdepth < entry->depth) {
p.append( QPointF( SCALE(gc, entry->sec, entry->stopdepth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->stopdepth)));
} else {
p.append( QPointF( SCALE(gc, entry->sec, entry->depth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->depth)));
}
} else {
p.append( QPointF( SCALE(gc, entry->sec, 0) ));
p.append(QPointF(SCALEGC(entry->sec, 0)));
}
}
@ -546,16 +545,16 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
pat.setColorAt(0, profile_color[CALC_CEILING_SHALLOW].first());
pat.setColorAt(1, profile_color[CALC_CEILING_DEEP].first());
entry = pi->entry;
entry = gc.pi.entry;
p.clear();
p.append( QPointF(SCALE(gc, 0, 0) ));
for (i = 0; i < pi->nr; i++, entry++) {
p.append(QPointF(SCALEGC(0, 0)));
for (i = 0; i < gc.pi.nr; i++, entry++) {
if (entry->ceiling)
p.append( QPointF( SCALE(gc, entry->sec, entry->ceiling) ));
p.append(QPointF(SCALEGC(entry->sec, entry->ceiling)));
else
p.append( QPointF( SCALE(gc, entry->sec, 0) ));
p.append(QPointF(SCALEGC(entry->sec, 0)));
}
p.append( QPointF( SCALE(gc, (entry-1)->sec, 0) ));
p.append(QPointF(SCALEGC((entry-1)->sec, 0)));
neatFill = new QGraphicsPolygonItem();
neatFill->setPolygon(p);
neatFill->setPen(QPen(QBrush(),0));
@ -566,17 +565,17 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
pat.setColorAt(0, profile_color[CEILING_SHALLOW].first());
pat.setColorAt(1, profile_color[CEILING_DEEP].first());
entry = pi->entry;
entry = gc.pi.entry;
p.clear();
p.append( QPointF(SCALE(gc, 0, 0) ));
for (i = 0; i < pi->nr; i++, entry++)
p.append( QPointF( SCALE(gc, entry->sec, entry->depth) ));
p.append(QPointF(SCALEGC(0, 0)));
for (i = 0; i < gc.pi.nr; i++, entry++)
p.append(QPointF(SCALEGC(entry->sec, entry->depth)));
for (i = pi->nr - 1; i >= 0; i--, entry--) {
for (i = gc.pi.nr - 1; i >= 0; i--, entry--) {
if (entry->ndl == 0 && entry->stopdepth > entry->depth) {
p.append( QPointF( SCALE(gc, entry->sec, entry->stopdepth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->stopdepth)));
} else {
p.append( QPointF( SCALE(gc, entry->sec, entry->depth) ));
p.append(QPointF(SCALEGC(entry->sec, entry->depth)));
}
}
@ -587,21 +586,21 @@ void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct
scene()->addItem(neatFill);
/* Now do it again for the velocity colors */
entry = pi->entry;
for (i = 1; i < pi->nr; i++) {
entry = gc.pi.entry;
for (i = 1; i < gc.pi.nr; i++) {
entry++;
sec = entry->sec;
/* we want to draw the segments in different colors
* representing the vertical velocity, so we need to
* chop this into short segments */
depth = entry->depth;
QGraphicsLineItem *colorLine = new QGraphicsLineItem( SCALE(gc, entry[-1].sec, entry[-1].depth), SCALE(gc, sec, depth));
QGraphicsLineItem *colorLine = new QGraphicsLineItem(SCALEGC(entry[-1].sec, entry[-1].depth), SCALEGC(sec, depth));
colorLine->setPen(QPen(QBrush(profile_color[ (color_indice_t) (VELOCITY_COLORS_START_IDX + entry->velocity)].first()), 2));
scene()->addItem(colorLine);
}
}
void ProfileGraphicsView::plot_text(struct graphics_context *gc, text_render_options_t *tro, double x, double y, const QString& text)
void ProfileGraphicsView::plot_text(text_render_options_t *tro, double x, double y, const QString& text)
{
QFontMetrics fm(font());
@ -609,7 +608,7 @@ void ProfileGraphicsView::plot_text(struct graphics_context *gc, text_render_opt
double dy = tro->vpos * (fm.height());
QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem(text);
QPointF point( SCALE(gc, x, y) ); // This is neded because of the SCALE macro.
QPointF point(SCALEGC(x, y)); // This is neded because of the SCALE macro.
item->setPos(point.x() + dx, point.y() +dy);
item->setBrush(QBrush(profile_color[tro->color].first()));
@ -627,19 +626,19 @@ void ProfileGraphicsView::resizeEvent(QResizeEvent *event)
fitInView (r.x() - 50, r.y() -50, r.width() + 100, r.height() + 100); // do a little bit of spacing;
}
void ProfileGraphicsView::plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
void ProfileGraphicsView::plot_temperature_profile()
{
int last = 0;
if (!setup_temperature_limits(gc, pi))
if (!setup_temperature_limits(&gc, &gc.pi))
return;
QPointF from;
QPointF to;
QColor color = profile_color[TEMP_PLOT].first();
for (int i = 0; i < pi->nr; i++) {
struct plot_data *entry = pi->entry + i;
for (int i = 0; i < gc.pi.nr; i++) {
struct plot_data *entry = gc.pi.entry + i;
int mkelvin = entry->temperature;
int sec = entry->sec;
if (!mkelvin) {
@ -648,7 +647,7 @@ void ProfileGraphicsView::plot_temperature_profile(struct graphics_context *gc,
mkelvin = last;
}
if (last) {
to = QPointF(SCALE(gc, sec, mkelvin));
to = QPointF(SCALEGC(sec, mkelvin));
//qDebug() << from << to;
QGraphicsLineItem *item = new QGraphicsLineItem(from.x(), from.y(), to.x(), to.y());
item->setPen(QPen(color, 2*plot_scale));
@ -656,7 +655,7 @@ void ProfileGraphicsView::plot_temperature_profile(struct graphics_context *gc,
from = to;
}
else{
from = QPointF(SCALE(gc, sec, mkelvin));
from = QPointF(SCALEGC(sec, mkelvin));
}
last = mkelvin;
}

View file

@ -1,6 +1,7 @@
#ifndef PROFILEGRAPHICS_H
#define PROFILEGRAPHICS_H
#include "../display.h"
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QIcon>
@ -90,15 +91,16 @@ protected:
void mouseMoveEvent(QMouseEvent* event);
private:
void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi);
void plot_text(struct graphics_context *gc, text_render_options_t *tro, double x, double y, const QString &text);
void plot_events(struct graphics_context *gc, struct plot_info *pi, struct divecomputer *dc);
void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event);
void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi);
void plot_depth_profile();
void plot_text(text_render_options_t *tro, double x, double y, const QString &text);
void plot_events(struct divecomputer *dc);
void plot_one_event(struct event *event);
void plot_temperature_profile();
QPen defaultPen;
QBrush defaultBrush;
ToolTipItem *toolTip;
graphics_context gc;
};
#endif