Profile: support grayscale plotting

This patch adds the getColor() helper function to
ProfileGraphicsView and EventItem so that retrieving
a certain color can depend on a 'isGrayscale' flag.
This flag is private and only set by
ProfileGraphicsView::setPrintMode() at this point.
EventItem also now accepts 'grayscale' as a constructor
argument.

A couple of side modifications are:
- move setBackgroundBrush() to ProfileGraphicsView::plot()
- set the same pen color as brush color for the dot in the
'!' symbol inside EventItem::EventItem().

TODO: look for color issues when printing using the
custom grayscale table

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Lubomir I. Ivanov 2013-07-13 18:18:26 +03:00 committed by Dirk Hohndel
parent 17c97b921f
commit 9de15dec60
2 changed files with 54 additions and 38 deletions

View file

@ -1,7 +1,6 @@
#include "profilegraphics.h" #include "profilegraphics.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "divelistview.h" #include "divelistview.h"
#include "graphicsview-common.h"
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QResizeEvent> #include <QResizeEvent>
@ -52,7 +51,6 @@ ProfileGraphicsView::ProfileGraphicsView(QWidget* parent) : QGraphicsView(parent
fill_profile_color(); fill_profile_color();
setScene(new QGraphicsScene()); setScene(new QGraphicsScene());
setBackgroundBrush(profile_color[BACKGROUND].at(0));
scene()->installEventFilter(this); scene()->installEventFilter(this);
setRenderHint(QPainter::Antialiasing); setRenderHint(QPainter::Antialiasing);
@ -197,6 +195,11 @@ void ProfileGraphicsView::setPrintMode(bool mode, bool grayscale)
isGrayscale = grayscale; isGrayscale = grayscale;
} }
QColor ProfileGraphicsView::getColor(const color_indice_t i)
{
return profile_color[i].at((isGrayscale) ? 1 : 0);
}
void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw) void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
{ {
struct divecomputer *dc; struct divecomputer *dc;
@ -214,6 +217,7 @@ void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
if (!isVisible() || !dive) { if (!isVisible() || !dive) {
return; return;
} }
setBackgroundBrush(getColor(BACKGROUND));
// best place to put the focus stealer code. // best place to put the focus stealer code.
setFocusProxy(mainWindow()->dive_list()); setFocusProxy(mainWindow()->dive_list());
@ -260,7 +264,7 @@ void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
/* Bounding box */ /* Bounding box */
QPen pen = defaultPen; QPen pen = defaultPen;
pen.setColor(profile_color[TIME_GRID].at(0)); pen.setColor(getColor(TIME_GRID));
QGraphicsRectItem *rect = new QGraphicsRectItem(profile_grid_area); QGraphicsRectItem *rect = new QGraphicsRectItem(profile_grid_area);
rect->setPen(pen); rect->setPen(pen);
scene()->addItem(rect); scene()->addItem(rect);
@ -348,7 +352,7 @@ void ProfileGraphicsView::plot_depth_scale()
case units::FEET: marker = 9144; break; /* 30 ft */ case units::FEET: marker = 9144; break; /* 30 ft */
} }
QColor c(profile_color[DEPTH_GRID].first()); QColor c(getColor(DEPTH_GRID));
/* don't write depth labels all the way to the bottom as /* don't write depth labels all the way to the bottom as
* there may be other graphs below the depth plot (like * there may be other graphs below the depth plot (like
@ -374,7 +378,7 @@ void ProfileGraphicsView::plot_pp_text()
pp = floor(gc.pi.maxpp * 10.0) / 10.0 + 0.2; pp = floor(gc.pi.maxpp * 10.0) / 10.0 + 0.2;
dpp = pp > 4 ? 1.0 : 0.5; dpp = pp > 4 ? 1.0 : 0.5;
hpos = gc.pi.entry[gc.pi.nr - 1].sec; hpos = gc.pi.entry[gc.pi.nr - 1].sec;
QColor c = profile_color[PP_LINES].first(); QColor c = getColor(PP_LINES);
for (m = 0.0; m <= pp; m += dpp) { for (m = 0.0; m <= pp; m += dpp) {
QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(0, m), SCALEGC(hpos, m)); QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(0, m), SCALEGC(hpos, m));
@ -407,7 +411,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
QColor c; QColor c;
QPointF from, to; QPointF from, to;
if (prefs.pp_graphs.pn2) { if (prefs.pp_graphs.pn2) {
c = profile_color[PN2].first(); c = getColor(PN2);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->pn2)); from = QPointF(SCALEGC(entry->sec, entry->pn2));
for (i = 1; i < pi->nr; i++) { for (i = 1; i < pi->nr; i++) {
@ -418,7 +422,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
from = QPointF(SCALEGC(entry->sec, entry->pn2)); from = QPointF(SCALEGC(entry->sec, entry->pn2));
} }
c = profile_color[PN2_ALERT].first(); c = getColor(PN2_ALERT);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->pn2)); from = QPointF(SCALEGC(entry->sec, entry->pn2));
for (i = 1; i < pi->nr; i++) { for (i = 1; i < pi->nr; i++) {
@ -431,7 +435,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
} }
if (prefs.pp_graphs.phe) { if (prefs.pp_graphs.phe) {
c = profile_color[PHE].first(); c = getColor(PHE);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->phe)); from = QPointF(SCALEGC(entry->sec, entry->phe));
@ -443,7 +447,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
from = QPointF(SCALEGC(entry->sec, entry->phe)); from = QPointF(SCALEGC(entry->sec, entry->phe));
} }
c = profile_color[PHE_ALERT].first(); c = getColor(PHE_ALERT);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->phe)); from = QPointF(SCALEGC(entry->sec, entry->phe));
for (i = 1; i < pi->nr; i++) { for (i = 1; i < pi->nr; i++) {
@ -455,7 +459,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
} }
} }
if (prefs.pp_graphs.po2) { if (prefs.pp_graphs.po2) {
c = profile_color[PO2].first(); c = getColor(PO2);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->po2)); from = QPointF(SCALEGC(entry->sec, entry->po2));
for (i = 1; i < pi->nr; i++) { for (i = 1; i < pi->nr; i++) {
@ -466,7 +470,7 @@ void ProfileGraphicsView::plot_pp_gas_profile()
from = QPointF(SCALEGC(entry->sec, entry->po2)); from = QPointF(SCALEGC(entry->sec, entry->po2));
} }
c = profile_color[PO2_ALERT].first(); c = getColor(PO2_ALERT);
entry = pi->entry; entry = pi->entry;
from = QPointF(SCALEGC(entry->sec, entry->po2)); from = QPointF(SCALEGC(entry->sec, entry->po2));
for (i = 1; i < pi->nr; i++) { for (i = 1; i < pi->nr; i++) {
@ -749,9 +753,9 @@ QColor ProfileGraphicsView::get_sac_color(int sac, int avg_sac)
sac_index = 0; sac_index = 0;
if (sac_index > SAC_COLORS - 1) if (sac_index > SAC_COLORS - 1)
sac_index = SAC_COLORS - 1; sac_index = SAC_COLORS - 1;
return profile_color[ (color_indice_t) (SAC_COLORS_START_IDX + sac_index)].first(); return getColor((color_indice_t)(SAC_COLORS_START_IDX + sac_index));
} }
return profile_color[SAC_DEFAULT].first(); return getColor(SAC_DEFAULT);
} }
void ProfileGraphicsView::plot_events(struct divecomputer *dc) void ProfileGraphicsView::plot_events(struct divecomputer *dc)
@ -802,7 +806,7 @@ void ProfileGraphicsView::plot_one_event(struct event *ev)
int x = SCALEXGC(ev->time.seconds); int x = SCALEXGC(ev->time.seconds);
int y = SCALEYGC(depth); int y = SCALEYGC(depth);
EventItem *item = new EventItem(); EventItem *item = new EventItem(0, isGrayscale);
item->setPos(x, y); item->setPos(x, y);
scene()->addItem(item); scene()->addItem(item);
@ -868,7 +872,7 @@ void ProfileGraphicsView::plot_depth_profile()
last_gc = gc; last_gc = gc;
QColor c = profile_color[TIME_GRID].at(0); QColor c = getColor(TIME_GRID);
for (i = incr; i < maxtime; i += incr) { for (i = incr; i < maxtime; i += incr) {
QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(i, 0), SCALEGC(i, 1)); QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(i, 0), SCALEGC(i, 1));
QPen pen(defaultPen); QPen pen(defaultPen);
@ -905,7 +909,7 @@ void ProfileGraphicsView::plot_depth_profile()
} }
maxline = MAX(gc.pi.maxdepth + marker, maxdepth * 2 / 3); maxline = MAX(gc.pi.maxdepth + marker, maxdepth * 2 / 3);
c = profile_color[DEPTH_GRID].at(0); c = getColor(DEPTH_GRID);
for (i = marker; i < maxline; i += marker) { for (i = marker; i < maxline; i += marker) {
QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(0, i), SCALEGC(1, i)); QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(0, i), SCALEGC(1, i));
@ -916,7 +920,7 @@ void ProfileGraphicsView::plot_depth_profile()
} }
gc.leftx = 0; gc.rightx = maxtime; gc.leftx = 0; gc.rightx = maxtime;
c = profile_color[MEAN_DEPTH].at(0); c = getColor(MEAN_DEPTH);
/* Show mean depth */ /* Show mean depth */
if (! gc.printer) { if (! gc.printer) {
@ -965,8 +969,8 @@ void ProfileGraphicsView::plot_depth_profile()
} }
} }
} }
pat.setColorAt(1, profile_color[DEPTH_BOTTOM].first()); pat.setColorAt(1, getColor(DEPTH_BOTTOM));
pat.setColorAt(0, profile_color[DEPTH_TOP].first()); pat.setColorAt(0, getColor(DEPTH_TOP));
neatFill = new QGraphicsPolygonItem(); neatFill = new QGraphicsPolygonItem();
neatFill->setPolygon(p); neatFill->setPolygon(p);
@ -980,8 +984,8 @@ void ProfileGraphicsView::plot_depth_profile()
* through so far) */ * through so far) */
if (prefs.profile_dc_ceiling && prefs.profile_red_ceiling) { if (prefs.profile_dc_ceiling && prefs.profile_red_ceiling) {
p.clear(); p.clear();
pat.setColorAt(0, profile_color[CEILING_SHALLOW].first()); pat.setColorAt(0, getColor(CEILING_SHALLOW));
pat.setColorAt(1, profile_color[CEILING_DEEP].first()); pat.setColorAt(1, getColor(CEILING_DEEP));
entry = gc.pi.entry; entry = gc.pi.entry;
p.append(QPointF(SCALEGC(0, 0))); p.append(QPointF(SCALEGC(0, 0)));
@ -1006,8 +1010,8 @@ void ProfileGraphicsView::plot_depth_profile()
/* finally, plot the calculated ceiling over all this */ /* finally, plot the calculated ceiling over all this */
if (prefs.profile_calc_ceiling) { if (prefs.profile_calc_ceiling) {
pat.setColorAt(0, profile_color[CALC_CEILING_SHALLOW].first()); pat.setColorAt(0, getColor(CALC_CEILING_SHALLOW));
pat.setColorAt(1, profile_color[CALC_CEILING_DEEP].first()); pat.setColorAt(1, getColor(CALC_CEILING_DEEP));
entry = gc.pi.entry; entry = gc.pi.entry;
p.clear(); p.clear();
@ -1030,7 +1034,7 @@ void ProfileGraphicsView::plot_depth_profile()
if (prefs.profile_calc_ceiling && prefs.calc_all_tissues){ if (prefs.profile_calc_ceiling && prefs.calc_all_tissues){
int k; int k;
for (k=0; k<16; k++){ for (k=0; k<16; k++){
pat.setColorAt(0, profile_color[CALC_CEILING_SHALLOW].first()); pat.setColorAt(0, getColor(CALC_CEILING_SHALLOW));
pat.setColorAt(1, QColor(100, 100, 100, 50)); pat.setColorAt(1, QColor(100, 100, 100, 50));
entry = gc.pi.entry; entry = gc.pi.entry;
@ -1051,8 +1055,8 @@ void ProfileGraphicsView::plot_depth_profile()
} }
/* next show where we have been bad and crossed the dc's ceiling */ /* next show where we have been bad and crossed the dc's ceiling */
if (prefs.profile_dc_ceiling) { if (prefs.profile_dc_ceiling) {
pat.setColorAt(0, profile_color[CEILING_SHALLOW].first()); pat.setColorAt(0, getColor(CEILING_SHALLOW));
pat.setColorAt(1, profile_color[CEILING_DEEP].first()); pat.setColorAt(1, getColor(CEILING_DEEP));
entry = gc.pi.entry; entry = gc.pi.entry;
p.clear(); p.clear();
@ -1085,7 +1089,7 @@ void ProfileGraphicsView::plot_depth_profile()
depth = entry->depth; depth = entry->depth;
QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(entry[-1].sec, entry[-1].depth), SCALEGC(sec, depth)); QGraphicsLineItem *item = new QGraphicsLineItem(SCALEGC(entry[-1].sec, entry[-1].depth), SCALEGC(sec, depth));
QPen pen(defaultPen); QPen pen(defaultPen);
pen.setColor(profile_color[ (color_indice_t) (VELOCITY_COLORS_START_IDX + entry->velocity)].first()); pen.setColor(getColor((color_indice_t)(VELOCITY_COLORS_START_IDX + entry->velocity)));
item->setPen(pen); item->setPen(pen);
scene()->addItem(item); scene()->addItem(item);
} }
@ -1108,11 +1112,11 @@ QGraphicsItemGroup *ProfileGraphicsView::plot_text(text_render_options_t *tro,co
QPainterPathStroker stroker; QPainterPathStroker stroker;
stroker.setWidth(3); stroker.setWidth(3);
QGraphicsPathItem *strokedItem = new QGraphicsPathItem(stroker.createStroke(textPath), group); QGraphicsPathItem *strokedItem = new QGraphicsPathItem(stroker.createStroke(textPath), group);
strokedItem->setBrush(QBrush(profile_color[TEXT_BACKGROUND].first())); strokedItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
strokedItem->setPen(Qt::NoPen); strokedItem->setPen(Qt::NoPen);
QGraphicsPathItem *textItem = new QGraphicsPathItem(textPath, group); QGraphicsPathItem *textItem = new QGraphicsPathItem(textPath, group);
textItem->setBrush(QBrush(profile_color[tro->color].first())); textItem->setBrush(QBrush(getColor(tro->color)));
textItem->setPen(Qt::NoPen); textItem->setPen(Qt::NoPen);
group->setPos(point.x() + dx, point.y() + dy); group->setPos(point.x() + dx, point.y() + dy);
@ -1138,7 +1142,7 @@ void ProfileGraphicsView::plot_temperature_profile()
QPointF from; QPointF from;
QPointF to; QPointF to;
QColor color = profile_color[TEMP_PLOT].first(); QColor color = getColor(TEMP_PLOT);
for (int i = 0; i < gc.pi.nr; i++) { for (int i = 0; i < gc.pi.nr; i++) {
struct plot_data *entry = gc.pi.entry + i; struct plot_data *entry = gc.pi.entry + i;
@ -1408,8 +1412,14 @@ bool ToolTipItem::eventFilter(QObject* view, QEvent* event)
return false; return false;
} }
EventItem::EventItem(QGraphicsItem* parent): QGraphicsPolygonItem(parent) QColor EventItem::getColor(const color_indice_t i)
{ {
return profile_color[i].at((isGrayscale) ? 1 : 0);
}
EventItem::EventItem(QGraphicsItem* parent, bool grayscale): QGraphicsPolygonItem(parent)
{
isGrayscale = grayscale;
setFlag(ItemIgnoresTransformations); setFlag(ItemIgnoresTransformations);
setFlag(ItemIsFocusable); setFlag(ItemIsFocusable);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
@ -1427,17 +1437,18 @@ EventItem::EventItem(QGraphicsItem* parent): QGraphicsPolygonItem(parent)
defaultPen.setCosmetic(true); defaultPen.setCosmetic(true);
QPen pen = defaultPen; QPen pen = defaultPen;
pen.setBrush(QBrush(profile_color[ALERT_BG].first())); pen.setBrush(QBrush(getColor(ALERT_BG)));
setPolygon(poly); setPolygon(poly);
setBrush(QBrush(profile_color[ALERT_BG].first())); setBrush(QBrush(getColor(ALERT_BG)));
setPen(pen); setPen(pen);
QGraphicsLineItem *line = new QGraphicsLineItem(0,5,0,10, this); QGraphicsLineItem *line = new QGraphicsLineItem(0, 5, 0, 10, this);
line->setPen(QPen(Qt::black, 2)); line->setPen(QPen(getColor(ALERT_FG), 2));
QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(-1, 12, 2,2, this); QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(-1, 12, 2, 2, this);
ball->setBrush(QBrush(Qt::black)); ball->setBrush(QBrush(getColor(ALERT_FG)));
ball->setPen(QPen(getColor(ALERT_FG)));
} }
GraphicsTextEditor::GraphicsTextEditor(QGraphicsItem* parent): QGraphicsTextItem(parent) GraphicsTextEditor::GraphicsTextEditor(QGraphicsItem* parent): QGraphicsTextItem(parent)

View file

@ -1,6 +1,7 @@
#ifndef PROFILEGRAPHICS_H #ifndef PROFILEGRAPHICS_H
#define PROFILEGRAPHICS_H #define PROFILEGRAPHICS_H
#include "graphicsview-common.h"
#include "../display.h" #include "../display.h"
#include <QGraphicsView> #include <QGraphicsView>
#include <QGraphicsItem> #include <QGraphicsItem>
@ -60,12 +61,15 @@ private:
class EventItem : public QGraphicsPolygonItem class EventItem : public QGraphicsPolygonItem
{ {
public: public:
explicit EventItem(QGraphicsItem* parent = 0); explicit EventItem(QGraphicsItem* parent = 0, bool grayscale = FALSE);
private: private:
ToolTipItem *controller; ToolTipItem *controller;
QString text; QString text;
QIcon icon; QIcon icon;
bool isGrayscale;
QColor getColor(const color_indice_t i);
}; };
class GraphicsTextEditor : public QGraphicsTextItem{ class GraphicsTextEditor : public QGraphicsTextItem{
@ -125,6 +129,7 @@ private:
void plot_pp_text(); void plot_pp_text();
void plot_depth_scale(); void plot_depth_scale();
QColor getColor(const color_indice_t i);
QColor get_sac_color(int sac, int avg_sac); QColor get_sac_color(int sac, int avg_sac);
void scrollViewTo(const QPoint pos); void scrollViewTo(const QPoint pos);