profile: remove AbstractProfilePolygonItem::modelDataChanged()

The old mechanism to replot the profile items was to listen
to model-change signals. Then the code checked whether it
actually had to update anything by looking at the changed
model-indices.

However, the crucial replot was always initialized with
emitDataChanged(), which simple invalidated the full model
and therefore shouldCalculateStuff() always returned true.

Since now the replot() is called explicitly, remove the whole
logic and simply rename modelDataChanged() to replot().

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-12-29 23:31:54 +01:00 committed by Dirk Hohndel
parent 0104b0a915
commit f5e60b9618
2 changed files with 26 additions and 84 deletions

View file

@ -26,32 +26,12 @@ void AbstractProfilePolygonItem::clear()
texts.clear(); texts.clear();
} }
void AbstractProfilePolygonItem::replot()
{
modelDataChanged();
}
void AbstractProfilePolygonItem::setVisible(bool visible) void AbstractProfilePolygonItem::setVisible(bool visible)
{ {
QGraphicsPolygonItem::setVisible(visible); QGraphicsPolygonItem::setVisible(visible);
} }
bool AbstractProfilePolygonItem::shouldCalculateStuff(const QModelIndex &topLeft, const QModelIndex &bottomRight) void AbstractProfilePolygonItem::replot()
{
if (dataModel.rowCount() == 0)
return false;
if (hDataColumn == -1 || vDataColumn == -1)
return false;
if (topLeft.isValid() && bottomRight.isValid()) {
if ((topLeft.column() >= vDataColumn || topLeft.column() >= hDataColumn) &&
(bottomRight.column() <= vDataColumn || topLeft.column() <= hDataColumn)) {
return true;
}
}
return true;
}
void AbstractProfilePolygonItem::modelDataChanged(const QModelIndex&, const QModelIndex&)
{ {
// Calculate the polygon. This is the polygon that will be painted on screen // Calculate the polygon. This is the polygon that will be painted on screen
// on the ::paint method. Here we calculate the correct position of the points // on the ::paint method. Here we calculate the correct position of the points
@ -123,13 +103,11 @@ int DiveProfileItem::maxCeiling(int row)
return max; return max;
} }
void DiveProfileItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveProfileItem::replot()
{ {
bool eventAdded = false; bool eventAdded = false;
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
AbstractProfilePolygonItem::modelDataChanged(topLeft, bottomRight); AbstractProfilePolygonItem::replot();
if (polygon().isEmpty()) if (polygon().isEmpty())
return; return;
@ -226,7 +204,7 @@ DiveHeartrateItem::DiveHeartrateItem(const DivePlotDataModel &model, const DiveC
connect(qPrefTechnicalDetails::instance(), &qPrefTechnicalDetails::hrgraphChanged, this, &DiveHeartrateItem::setVisible); connect(qPrefTechnicalDetails::instance(), &qPrefTechnicalDetails::hrgraphChanged, this, &DiveHeartrateItem::setVisible);
} }
void DiveHeartrateItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveHeartrateItem::replot()
{ {
int last = -300, last_printed_hr = 0, sec = 0; int last = -300, last_printed_hr = 0, sec = 0;
struct { struct {
@ -234,10 +212,6 @@ void DiveHeartrateItem::modelDataChanged(const QModelIndex &topLeft, const QMode
int hr; int hr;
} hist[3] = {}; } hist[3] = {};
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
qDeleteAll(texts); qDeleteAll(texts);
texts.clear(); texts.clear();
// Ignore empty values. a heart rate of 0 would be a bad sign. // Ignore empty values. a heart rate of 0 would be a bad sign.
@ -309,14 +283,10 @@ DivePercentageItem::DivePercentageItem(const DivePlotDataModel &model, const Div
connect(qPrefTechnicalDetails::instance(), &qPrefTechnicalDetails::percentagegraphChanged, this, &DivePercentageItem::setVisible); connect(qPrefTechnicalDetails::instance(), &qPrefTechnicalDetails::percentagegraphChanged, this, &DivePercentageItem::setVisible);
} }
void DivePercentageItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DivePercentageItem::replot()
{ {
int sec = 0; int sec = 0;
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
// Ignore empty values. a heart rate of 0 would be a bad sign. // Ignore empty values. a heart rate of 0 would be a bad sign.
QPolygonF poly; QPolygonF poly;
for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) { for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) {
@ -389,14 +359,10 @@ DiveAmbPressureItem::DiveAmbPressureItem(const DivePlotDataModel &model, const D
setPen(pen); setPen(pen);
} }
void DiveAmbPressureItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveAmbPressureItem::replot()
{ {
int sec = 0; int sec = 0;
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
// Ignore empty values. a heart rate of 0 would be a bad sign. // Ignore empty values. a heart rate of 0 would be a bad sign.
QPolygonF poly; QPolygonF poly;
for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) { for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) {
@ -434,14 +400,10 @@ DiveGFLineItem::DiveGFLineItem(const DivePlotDataModel &model, const DiveCartesi
setPen(pen); setPen(pen);
} }
void DiveGFLineItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveGFLineItem::replot()
{ {
int sec = 0; int sec = 0;
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
// Ignore empty values. a heart rate of 0 would be a bad sign. // Ignore empty values. a heart rate of 0 would be a bad sign.
QPolygonF poly; QPolygonF poly;
for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) { for (int i = 0, modelDataCount = dataModel.rowCount(); i < modelDataCount; i++) {
@ -479,12 +441,9 @@ DiveTemperatureItem::DiveTemperatureItem(const DivePlotDataModel &model, const D
setPen(pen); setPen(pen);
} }
void DiveTemperatureItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveTemperatureItem::replot()
{ {
int last = -300, last_printed_temp = 0, sec = 0, last_valid_temp = 0; int last = -300, last_printed_temp = 0, sec = 0, last_valid_temp = 0;
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
qDeleteAll(texts); qDeleteAll(texts);
texts.clear(); texts.clear();
@ -560,12 +519,9 @@ DiveMeanDepthItem::DiveMeanDepthItem(const DivePlotDataModel &model, const DiveC
lastRunningSum = 0.0; lastRunningSum = 0.0;
} }
void DiveMeanDepthItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveMeanDepthItem::replot()
{ {
double meandepthvalue = 0.0; double meandepthvalue = 0.0;
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
QPolygonF poly; QPolygonF poly;
plot_data *entry = dataModel.data().entry; plot_data *entry = dataModel.data().entry;
@ -610,12 +566,8 @@ void DiveMeanDepthItem::createTextItem()
texts.append(text); texts.append(text);
} }
void DiveGasPressureItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveGasPressureItem::replot()
{ {
// We don't have enougth data to calculate things, quit.
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
const struct plot_info *pInfo = &dataModel.data(); const struct plot_info *pInfo = &dataModel.data();
std::vector<int> plotted_cyl(pInfo->nr_cylinders, false); std::vector<int> plotted_cyl(pInfo->nr_cylinders, false);
std::vector<int> last_plotted(pInfo->nr_cylinders, 0); std::vector<int> last_plotted(pInfo->nr_cylinders, 0);
@ -787,12 +739,9 @@ DiveCalculatedCeiling::DiveCalculatedCeiling(const DivePlotDataModel &model, con
setVisible(prefs.calcceiling); setVisible(prefs.calcceiling);
} }
void DiveCalculatedCeiling::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveCalculatedCeiling::replot()
{ {
// We don't have enougth data to calculate things, quit. AbstractProfilePolygonItem::replot();
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
AbstractProfilePolygonItem::modelDataChanged(topLeft, bottomRight);
// Add 2 points to close the polygon. // Add 2 points to close the polygon.
QPolygonF poly = polygon(); QPolygonF poly = polygon();
if (poly.isEmpty()) if (poly.isEmpty())
@ -838,11 +787,8 @@ DiveReportedCeiling::DiveReportedCeiling(const DivePlotDataModel &model, const D
setVisible(prefs.dcceiling); setVisible(prefs.dcceiling);
} }
void DiveReportedCeiling::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void DiveReportedCeiling::replot()
{ {
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
QPolygonF p; QPolygonF p;
p.append(QPointF(hAxis.posAtValue(0), vAxis.posAtValue(0))); p.append(QPointF(hAxis.posAtValue(0), vAxis.posAtValue(0)));
plot_data *entry = dataModel.data().entry; plot_data *entry = dataModel.data().entry;
@ -874,11 +820,8 @@ void DiveReportedCeiling::paint(QPainter *painter, const QStyleOptionGraphicsIte
QGraphicsPolygonItem::paint(painter, option, widget); QGraphicsPolygonItem::paint(painter, option, widget);
} }
void PartialPressureGasItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) void PartialPressureGasItem::replot()
{ {
if (!shouldCalculateStuff(topLeft, bottomRight))
return;
plot_data *entry = dataModel.data().entry; plot_data *entry = dataModel.data().entry;
QPolygonF poly; QPolygonF poly;
QPolygonF alertpoly; QPolygonF alertpoly;

View file

@ -38,10 +38,9 @@ public:
AbstractProfilePolygonItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); AbstractProfilePolygonItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) = 0; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) = 0;
void clear(); void clear();
virtual void replot();
public public
slots: slots:
virtual void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex());
void replot();
void setVisible(bool visible); void setVisible(bool visible);
protected: protected:
@ -68,7 +67,7 @@ class DiveProfileItem : public AbstractProfilePolygonItem {
public: public:
DiveProfileItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveProfileItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void settingsToggled(bool toggled); void settingsToggled(bool toggled);
void plot_depth_sample(struct plot_data *entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color); void plot_depth_sample(struct plot_data *entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color);
int maxCeiling(int row); int maxCeiling(int row);
@ -83,7 +82,7 @@ class DiveMeanDepthItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DiveMeanDepthItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveMeanDepthItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
private: private:
@ -96,7 +95,7 @@ class DiveTemperatureItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DiveTemperatureItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveTemperatureItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
private: private:
@ -107,7 +106,7 @@ class DiveHeartrateItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DiveHeartrateItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveHeartrateItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private: private:
@ -119,7 +118,7 @@ class DivePercentageItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DivePercentageItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn, int i); DivePercentageItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn, int i);
void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private: private:
@ -133,7 +132,7 @@ class DiveAmbPressureItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DiveAmbPressureItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveAmbPressureItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private: private:
@ -144,7 +143,7 @@ class DiveGFLineItem : public AbstractProfilePolygonItem {
Q_OBJECT Q_OBJECT
public: public:
DiveGFLineItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveGFLineItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private: private:
@ -156,7 +155,7 @@ class DiveGasPressureItem : public AbstractProfilePolygonItem {
public: public:
using AbstractProfilePolygonItem::AbstractProfilePolygonItem; using AbstractProfilePolygonItem::AbstractProfilePolygonItem;
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
private: private:
@ -171,7 +170,7 @@ class DiveCalculatedCeiling : public AbstractProfilePolygonItem {
public: public:
DiveCalculatedCeiling(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, DiveCalculatedCeiling(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn,
const DiveCartesianAxis &vAxis, int vColumn, ProfileWidget2 *profileWidget); const DiveCartesianAxis &vAxis, int vColumn, ProfileWidget2 *profileWidget);
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
private: private:
@ -183,7 +182,7 @@ class DiveReportedCeiling : public AbstractProfilePolygonItem {
public: public:
DiveReportedCeiling(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); DiveReportedCeiling(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
}; };
@ -200,7 +199,7 @@ class PartialPressureGasItem : public AbstractProfilePolygonItem {
public: public:
PartialPressureGasItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn); PartialPressureGasItem(const DivePlotDataModel &model, const DiveCartesianAxis &hAxis, int hColumn, const DiveCartesianAxis &vAxis, int vColumn);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) override;
void modelDataChanged(const QModelIndex &topLeft = QModelIndex(), const QModelIndex &bottomRight = QModelIndex()) override; void replot() override;
void setThresholdSettingsKey(const double *prefPointerMin, const double *prefPointerMax); void setThresholdSettingsKey(const double *prefPointerMin, const double *prefPointerMax);
void setVisibilitySettingsKey(const QString &setVisibilitySettingsKey); void setVisibilitySettingsKey(const QString &setVisibilitySettingsKey);
void setColors(const QColor &normalColor, const QColor &alertColor); void setColors(const QColor &normalColor, const QColor &alertColor);