profile: set text and brush of DiveTextItem concurrently

The text and the brush are the two properties of text items
that change dynamically. To avoid complexities concerning
redrawing, set them concurrently instead of in two separate
calls.

Since setting one of the properties requires a full redraw,
there is no performance advantage in setting them individually.

This fixes a theoretical bug: the colors of axis labels were not
updated appropriately. However, it seems like value-dependent
labels weren't used anyway.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-13 09:56:46 +02:00 committed by Dirk Hohndel
parent 2ebe6e3684
commit 04e0d96bae
6 changed files with 18 additions and 35 deletions

View file

@ -170,7 +170,7 @@ void DiveCartesianAxis::updateTicks(int animSpeed, color_index_t color)
begin + i * stepSize : begin + i * stepSize :
begin - i * stepSize; begin - i * stepSize;
labels[i]->setText(textForValue(currValueText)); labels[i]->set(textForValue(currValueText), colorForValue(currValueText));
if (orientation == LeftToRight || orientation == RightToLeft) { if (orientation == LeftToRight || orientation == RightToLeft) {
Animations::moveTo(labels[i], animSpeed, childPos, m.y1() + tick_size); Animations::moveTo(labels[i], animSpeed, childPos, m.y1() + tick_size);
} else { } else {
@ -201,8 +201,7 @@ void DiveCartesianAxis::updateTicks(int animSpeed, color_index_t color)
int alignFlags = orientation == RightToLeft || orientation == LeftToRight ? Qt::AlignBottom | Qt::AlignHCenter : int alignFlags = orientation == RightToLeft || orientation == LeftToRight ? Qt::AlignBottom | Qt::AlignHCenter :
Qt::AlignVCenter | Qt::AlignLeft; Qt::AlignVCenter | Qt::AlignLeft;
DiveTextItem *label = new DiveTextItem(dpr, labelScale, alignFlags, this); DiveTextItem *label = new DiveTextItem(dpr, labelScale, alignFlags, this);
label->setText(textForValue(currValueText)); label->set(textForValue(currValueText), colorForValue(currValueText));
label->setBrush(colorForValue(currValueText));
label->setZValue(1); label->setZValue(1);
labels.push_back(label); labels.push_back(label);
if (orientation == RightToLeft || orientation == LeftToRight) { if (orientation == RightToLeft || orientation == LeftToRight) {

View file

@ -143,9 +143,8 @@ void DiveProfileItem::replot(const dive *d, bool in_planner)
void DiveProfileItem::plot_depth_sample(struct plot_data *entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color) void DiveProfileItem::plot_depth_sample(struct plot_data *entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color)
{ {
DiveTextItem *item = new DiveTextItem(dpr, 1.0, flags, this); DiveTextItem *item = new DiveTextItem(dpr, 1.0, flags, this);
item->set(get_depth_string(entry->depth, true), color);
item->setPos(hAxis.posAtValue(entry->sec), vAxis.posAtValue(entry->depth)); item->setPos(hAxis.posAtValue(entry->sec), vAxis.posAtValue(entry->depth));
item->setText(get_depth_string(entry->depth, true));
item->setBrush(color);
texts.append(item); texts.append(item);
} }
@ -219,9 +218,8 @@ void DiveHeartrateItem::createTextItem(int sec, int hr, bool last)
int flags = last ? Qt::AlignLeft | Qt::AlignBottom : int flags = last ? Qt::AlignLeft | Qt::AlignBottom :
Qt::AlignRight | Qt::AlignBottom; Qt::AlignRight | Qt::AlignBottom;
DiveTextItem *text = new DiveTextItem(dpr, 0.7, flags, this); DiveTextItem *text = new DiveTextItem(dpr, 0.7, flags, this);
text->setBrush(getColor(HR_TEXT)); text->set(QString("%1").arg(hr), getColor(HR_TEXT));
text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(hr))); text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(hr)));
text->setText(QString("%1").arg(hr));
texts.append(text); texts.append(text);
} }
@ -369,9 +367,8 @@ void DiveTemperatureItem::createTextItem(int sec, int mkelvin, bool last)
int flags = last ? Qt::AlignLeft | Qt::AlignBottom : int flags = last ? Qt::AlignLeft | Qt::AlignBottom :
Qt::AlignRight | Qt::AlignBottom; Qt::AlignRight | Qt::AlignBottom;
DiveTextItem *text = new DiveTextItem(dpr, 0.8, flags, this); DiveTextItem *text = new DiveTextItem(dpr, 0.8, flags, this);
text->setBrush(getColor(TEMP_TEXT)); text->set(get_temperature_string(temp, true), getColor(TEMP_TEXT));
text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(mkelvin))); text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(mkelvin)));
text->setText(get_temperature_string(temp, true));
texts.append(text); texts.append(text);
} }
@ -435,9 +432,8 @@ void DiveMeanDepthItem::createTextItem()
qDeleteAll(texts); qDeleteAll(texts);
texts.clear(); texts.clear();
DiveTextItem *text = new DiveTextItem(dpr, 0.8, Qt::AlignRight | Qt::AlignTop, this); DiveTextItem *text = new DiveTextItem(dpr, 0.8, Qt::AlignRight | Qt::AlignTop, this);
text->setBrush(getColor(TEMP_TEXT)); text->set(get_depth_string(lrint(lastRunningSum), true), getColor(TEMP_TEXT));
text->setPos(QPointF(hAxis.posAtValue(sec) + 1, vAxis.posAtValue(lastRunningSum))); text->setPos(QPointF(hAxis.posAtValue(sec) + 1, vAxis.posAtValue(lastRunningSum)));
text->setText(get_depth_string(lrint(lastRunningSum), true));
texts.append(text); texts.append(text);
} }
@ -571,9 +567,8 @@ void DiveGasPressureItem::plotPressureValue(int mbar, int sec, QFlags<Qt::Alignm
const char *unit; const char *unit;
int pressure = get_pressure_units(mbar, &unit); int pressure = get_pressure_units(mbar, &unit);
DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this); DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this);
text->set(QString("%1%2").arg(pressure).arg(unit), getColor(PRESSURE_TEXT));
text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + pressure_offset ); text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + pressure_offset );
text->setText(QString("%1%2").arg(pressure).arg(unit));
text->setBrush(getColor(PRESSURE_TEXT));
texts.push_back(text); texts.push_back(text);
} }
@ -581,9 +576,8 @@ void DiveGasPressureItem::plotGasValue(int mbar, int sec, struct gasmix gasmix,
{ {
QString gas = get_gas_string(gasmix); QString gas = get_gas_string(gasmix);
DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this); DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this);
text->set(gas, getColor(PRESSURE_TEXT));
text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + gasname_offset ); text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + gasname_offset );
text->setText(gas);
text->setBrush(getColor(PRESSURE_TEXT));
texts.push_back(text); texts.push_back(text);
} }

View file

@ -27,17 +27,11 @@ void DiveTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
QGraphicsItemGroup::paint(painter, option, widget); QGraphicsItemGroup::paint(painter, option, widget);
} }
void DiveTextItem::setBrush(const QBrush &b) void DiveTextItem::set(const QString &t, const QBrush &b)
{ {
textItem->setBrush(b); textItem->setBrush(b);
} internalText = t;
updateText();
void DiveTextItem::setText(const QString &t)
{
if (internalText != t) {
internalText = t;
updateText();
}
} }
const QString &DiveTextItem::text() const QString &DiveTextItem::text()

View file

@ -15,8 +15,7 @@ class DiveTextItem : public QObject, public QGraphicsItemGroup {
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity) Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
public: public:
DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsItem *parent); DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsItem *parent);
void setText(const QString &text); void set(const QString &text, const QBrush &brush);
void setBrush(const QBrush &brush);
const QString &text(); const QString &text();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
static QFont getFont(double dpr, double scale); static QFont getFont(double dpr, double scale);

View file

@ -157,9 +157,7 @@ ProfileScene::ProfileScene(double dpr, bool printMode, bool isGrayscale) :
// show the deco model parameters at the top in the center // show the deco model parameters at the top in the center
decoModelParameters->setY(0); decoModelParameters->setY(0);
decoModelParameters->setX(50); decoModelParameters->setX(50);
decoModelParameters->setBrush(getColor(PRESSURE_TEXT));
diveComputerText->setBrush(getColor(TIME_TEXT, isGrayscale));
diveComputerText->setPos(itemPos.dcLabel.on); diveComputerText->setPos(itemPos.dcLabel.on);
tankItem->setPos(itemPos.tankBar.on); tankItem->setPos(itemPos.tankBar.on);
@ -450,15 +448,15 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM
if (!plannerModel) { if (!plannerModel) {
if (decoMode(false) == VPMB) if (decoMode(false) == VPMB)
decoModelParameters->setText(QString("VPM-B +%1").arg(prefs.vpmb_conservatism)); decoModelParameters->set(QString("VPM-B +%1").arg(prefs.vpmb_conservatism), getColor(PRESSURE_TEXT));
else else
decoModelParameters->setText(QString("GF %1/%2").arg(prefs.gflow).arg(prefs.gfhigh)); decoModelParameters->set(QString("GF %1/%2").arg(prefs.gflow).arg(prefs.gfhigh), getColor(PRESSURE_TEXT));
} else { } else {
struct diveplan &diveplan = plannerModel->getDiveplan(); struct diveplan &diveplan = plannerModel->getDiveplan();
if (decoMode(inPlanner) == VPMB) if (decoMode(inPlanner) == VPMB)
decoModelParameters->setText(QString("VPM-B +%1").arg(diveplan.vpmb_conservatism)); decoModelParameters->set(QString("VPM-B +%1").arg(diveplan.vpmb_conservatism), getColor(PRESSURE_TEXT));
else else
decoModelParameters->setText(QString("GF %1/%2").arg(diveplan.gflow).arg(diveplan.gfhigh)); decoModelParameters->set(QString("GF %1/%2").arg(diveplan.gflow).arg(diveplan.gfhigh), getColor(PRESSURE_TEXT));
} }
const struct divecomputer *currentdc = get_dive_dc_const(d, dc); const struct divecomputer *currentdc = get_dive_dc_const(d, dc);
@ -660,7 +658,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM
if ((nr = number_of_computers(d)) > 1) if ((nr = number_of_computers(d)) > 1)
dcText += tr(" (#%1 of %2)").arg(dc + 1).arg(nr); dcText += tr(" (#%1 of %2)").arg(dc + 1).arg(nr);
#endif #endif
diveComputerText->setText(dcText); diveComputerText->set(dcText, getColor(TIME_TEXT, isGrayscale));
} }
QImage ProfileScene::toImage(QSize size) QImage ProfileScene::toImage(QSize size)

View file

@ -52,8 +52,7 @@ void TankItem::createBar(int startTime, int stopTime, struct gasmix gas)
rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle
rects.push_back(rect); rects.push_back(rect);
DiveTextItem *label = new DiveTextItem(dpr, 1.0, Qt::AlignBottom | Qt::AlignRight, rect); DiveTextItem *label = new DiveTextItem(dpr, 1.0, Qt::AlignBottom | Qt::AlignRight, rect);
label->setText(gasname(gas)); label->set(gasname(gas), Qt::black);
label->setBrush(Qt::black);
label->setPos(x + 1, 0); label->setPos(x + 1, 0);
#ifdef SUBSURFACE_MOBILE #ifdef SUBSURFACE_MOBILE
label->setPos(x + 1, -2.5); label->setPos(x + 1, -2.5);