From 4c7b9444b6a2a2664218f8d97f54adc2b44f535a Mon Sep 17 00:00:00 2001 From: peri4 Date: Fri, 19 Jan 2024 14:50:21 +0300 Subject: [PATCH] Graphic now index-safe --- libs/graphic/graphic.cpp | 108 +++++++++++++++++++++++++++++++++++---- libs/graphic/graphic.h | 49 +++++++++--------- 2 files changed, 124 insertions(+), 33 deletions(-) diff --git a/libs/graphic/graphic.cpp b/libs/graphic/graphic.cpp index 5679481..f47357d 100644 --- a/libs/graphic/graphic.cpp +++ b/libs/graphic/graphic.cpp @@ -955,7 +955,7 @@ void Graphic::setButtonsPosition(Graphic::Alignment a) { void Graphic::addPoint(const QPointF & p, int graphic, bool update_) { - if (graphic >= graphics.size() || graphic < 0) return; + if (!checkGraphicIndex(graphic)) return; GraphicType & t(graphics[graphic]); if (!t.cvrect.isNull() && !pause_) { if (t.cvrect.top() < p.y()) t.cvrect.setTop(p.y()); @@ -986,6 +986,7 @@ void Graphic::addPoint(double x, double y, bool update) { void Graphic::addPoint(double y, int graphic, bool update) { + if (!checkGraphicIndex(graphic)) return; if (graphics[graphic].polyline.isEmpty()) { addPoint(QPointF(0.0, y), graphic, update); } else { @@ -1000,7 +1001,8 @@ void Graphic::addPoint(double y, bool update) { void Graphic::addPoints(const QPolygonF & pts, int graphic, bool update_) { - if (graphic >= graphics.size() || graphic < 0 || pts.isEmpty()) return; + if (!checkGraphicIndex(graphic)) return; + if (pts.isEmpty()) return; GraphicType & t(graphics[graphic]); if (!t.cvrect.isNull() && !pause_) { for (const QPointF & p: pts) { @@ -1025,10 +1027,11 @@ void Graphic::addPoints(const QPolygonF & pts, bool update) { void Graphic::addPoints(const QVector & pts, int graphic, bool update_) { + if (!checkGraphicIndex(graphic)) return; QPolygonF ps; ps.reserve(pts.size()); double stx = 0; - if (!graphics[curGraphic].polyline.isEmpty()) stx = graphics[curGraphic].max_x; + if (!graphics[graphic].polyline.isEmpty()) stx = graphics[graphic].max_x; for (int i = 0; i < pts.size(); ++i) { ps << QPointF(stx + i * inc_x, pts[i]); } @@ -1042,7 +1045,7 @@ void Graphic::addPoints(const QVector & pts, bool update) { void Graphic::setGraphicData(const QVector & g, int graphic, bool update_) { - if (graphic >= graphics.size() || graphic < 0) return; + if (!checkGraphicIndex(graphic)) return; GraphicType & t(graphics[graphic]); t.cvrect = QRectF(); t.polyline.clear(); @@ -1076,7 +1079,7 @@ void Graphic::setGraphicProperties(int graphic, Qt::PenStyle style, double width, bool visible) { - if (graphic < 0 || graphic >= graphics.size()) return; + if (!checkGraphicIndex(graphic)) return; graphics[graphic].name = name; graphics[graphic].pen.setColor(color); graphics[graphic].pen.setStyle(style); @@ -1708,6 +1711,12 @@ void Graphic::askForExport(bool view_only) { } +bool Graphic::checkGraphicIndex(int index) const { + if (index < 0 || index >= graphics.size()) return false; + return true; +} + + void Graphic::drawGraphics() { if (isHover && ui->status->isVisible()) { ui->status->setText(tr("Cursor: ") + pointCoords(canvas2real(QPointF(curpos)))); @@ -2618,6 +2627,72 @@ QString Graphic::caption() const { } +QString Graphic::graphicName(int index) const { + if (!checkGraphicIndex(index)) return {}; + return graphics[index].name; +} + + +QColor Graphic::graphicColor(int index) const { + if (!checkGraphicIndex(index)) return Qt::black; + return graphics[index].pen.color(); +} + + +Qt::PenStyle Graphic::graphicStyle(int index) const { + if (!checkGraphicIndex(index)) return Qt::NoPen; + return graphics[index].pen.style(); +} + + +double Graphic::graphicLineWidth(int index) const { + if (!checkGraphicIndex(index)) return 0.; + return graphics[index].pen.widthF(); +} + + +double Graphic::graphicPointWidth(int index) const { + if (!checkGraphicIndex(index)) return 0.; + return graphics[index].pointWidth; +} + + +QColor Graphic::graphicFillColor(int index) const { + if (!checkGraphicIndex(index)) return Qt::black; + return graphics[index].fill_color; +} + + +bool Graphic::graphicVisible(int index) const { + if (!checkGraphicIndex(index)) return false; + return graphics[index].visible; +} + + +bool Graphic::graphicLinesEnabled(int index) const { + if (!checkGraphicIndex(index)) return false; + return graphics[index].lines; +} + + +bool Graphic::graphicPointsEnabled(int index) const { + if (!checkGraphicIndex(index)) return false; + return graphics[index].points; +} + + +bool Graphic::graphicFillEnabled(int index) const { + if (!checkGraphicIndex(index)) return false; + return graphics[index].fill; +} + + +QPen Graphic::graphicPen(int index) const { + if (!checkGraphicIndex(index)) return {}; + return graphics[index].pen; +} + + bool Graphic::borderInputsVisible() const { return ui->widgetLX->isVisible(); } @@ -2633,6 +2708,12 @@ bool Graphic::legendVisible() const { } +QVector Graphic::graphicData(int index) const { + if (!checkGraphicIndex(index)) return {}; + return graphics[index].polyline; +} + + QByteArray Graphic::save() { // QByteArray ba; // QDataStream s(&ba, QIODevice::ReadWrite); @@ -2727,7 +2808,7 @@ void Graphic::load(QByteArray ba) { GraphicType Graphic::graphic(int arg) { - if (arg < 0 || arg >= graphics.size()) return GraphicType(); + if (!checkGraphicIndex(arg)) return {}; return graphics[arg]; } @@ -2763,6 +2844,7 @@ void Graphic::setLabelY(const QString & str) { void Graphic::setGraphicName(const QString & str, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].name = str; updateLegend(); if (aupdate) repaintCanvas(); @@ -2770,9 +2852,7 @@ void Graphic::setGraphicName(const QString & str, int index) { void Graphic::setGraphicName(const QString & str) { - graphics[curGraphic].name = str; - updateLegend(); - if (aupdate) repaintCanvas(); + setGraphicName(str, curGraphic); } @@ -2790,6 +2870,7 @@ void Graphic::setTextColor(const QColor & color) { void Graphic::setGraphicColor(const QColor & color, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].pen.setColor(color); updateLegend(); if (aupdate) repaintCanvas(); @@ -2813,6 +2894,7 @@ void Graphic::setSelectionColor(const QColor & color) { void Graphic::setGraphicStyle(const Qt::PenStyle & style, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].pen.setStyle(style); updateLegend(); if (aupdate) repaintCanvas(); @@ -2836,6 +2918,7 @@ void Graphic::setSelectionStyle(const Qt::PenStyle & style) { void Graphic::setGraphicVisible(bool visible, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].visible = visible; updateLegendChecks(); if (isFit) { @@ -2852,6 +2935,7 @@ void Graphic::setGraphicVisible(bool visible) { void Graphic::setGraphicLineWidth(double w, int index) { + if (!checkGraphicIndex(index)) return; if (qRound(w) == w) graphics[index].pen.setWidth(qRound(w)); else @@ -2867,6 +2951,7 @@ void Graphic::setGraphicLineWidth(double w) { void Graphic::setGraphicPointWidth(double w, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].pointWidth = w; updateLegend(); if (aupdate) repaintCanvas(); @@ -2879,6 +2964,7 @@ void Graphic::setGraphicPointWidth(double w) { void Graphic::setGraphicFillColor(const QColor & w, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].fill_color = w; updateLegend(); if (aupdate) repaintCanvas(); @@ -2891,6 +2977,7 @@ void Graphic::setGraphicFillColor(const QColor & w) { void Graphic::setGraphicLinesEnabled(bool w, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].lines = w; updateLegend(); if (aupdate) repaintCanvas(); @@ -2903,6 +2990,7 @@ void Graphic::setGraphicLinesEnabled(bool w) { void Graphic::setGraphicPointsEnabled(bool w, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].points = w; updateLegend(); if (aupdate) repaintCanvas(); @@ -2915,6 +3003,7 @@ void Graphic::setGraphicPointsEnabled(bool w) { void Graphic::setGraphicFillEnabled(bool w, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].fill = w; updateLegend(); if (aupdate) repaintCanvas(); @@ -2927,6 +3016,7 @@ void Graphic::setGraphicFillEnabled(bool w) { void Graphic::setGraphicPen(const QPen & pen, int index) { + if (!checkGraphicIndex(index)) return; graphics[index].pen = pen; updateLegend(); if (aupdate) repaintCanvas(); diff --git a/libs/graphic/graphic.h b/libs/graphic/graphic.h index 0def15d..c293904 100644 --- a/libs/graphic/graphic.h +++ b/libs/graphic/graphic.h @@ -174,34 +174,34 @@ public: QString caption() const; QString labelX() const { return label_x; } QString labelY() const { return label_y; } - QString graphicName() const { return graphics[curGraphic].name; } - QString graphicName(int index) const { return graphics[index].name; } + QString graphicName() const { return graphicName(curGraphic); } + QString graphicName(int index) const; QColor backgroundColor() const { return back_color; } QColor textColor() const { return text_color; } - QColor graphicColor() const { return graphics[curGraphic].pen.color(); } - QColor graphicColor(int index) const { return graphics[index].pen.color(); } + QColor graphicColor() const { return graphicColor(curGraphic); } + QColor graphicColor(int index) const; QColor gridColor() const { return grid_pen.color(); } QColor selectionColor() const { return selpen.color(); } - Qt::PenStyle graphicStyle() const { return graphics[curGraphic].pen.style(); } - Qt::PenStyle graphicStyle(int index) const { return graphics[index].pen.style(); } + Qt::PenStyle graphicStyle() const { return graphicStyle(curGraphic); } + Qt::PenStyle graphicStyle(int index) const; Qt::PenStyle gridStyle() const { return grid_pen.style(); } Qt::PenStyle selectionStyle() const { return selpen.style(); } - double graphicLineWidth() const { return graphics[curGraphic].pen.widthF(); } - double graphicLineWidth(int index) const { return graphics[index].pen.widthF(); } - double graphicPointWidth() const { return graphics[curGraphic].pointWidth; } - double graphicPointWidth(int index) const { return graphics[index].pointWidth; } - QColor graphicFillColor() const { return graphics[curGraphic].fill_color; } - QColor graphicFillColor(int index) const { return graphics[index].fill_color; } - bool graphicVisible() const { return graphics[curGraphic].visible; } - bool graphicVisible(int index) const { return graphics[index].visible; } - bool graphicLinesEnabled() const { return graphics[curGraphic].lines; } - bool graphicLinesEnabled(int index) const { return graphics[index].lines; } - bool graphicPointsEnabled() const { return graphics[curGraphic].points; } - bool graphicPointsEnabled(int index) const { return graphics[index].points; } - bool graphicFillEnabled() const { return graphics[curGraphic].fill; } - bool graphicFillEnabled(int index) const { return graphics[index].fill; } - QPen graphicPen() const { return graphics[curGraphic].pen; } - QPen graphicPen(int index) const { return graphics[index].pen; } + double graphicLineWidth() const { return graphicLineWidth(curGraphic); } + double graphicLineWidth(int index) const; + double graphicPointWidth() const { return graphicPointWidth(curGraphic); } + double graphicPointWidth(int index) const; + QColor graphicFillColor() const { return graphicFillColor(curGraphic); } + QColor graphicFillColor(int index) const; + bool graphicVisible() const { return graphicVisible(curGraphic); } + bool graphicVisible(int index) const; + bool graphicLinesEnabled() const { return graphicLinesEnabled(curGraphic); } + bool graphicLinesEnabled(int index) const; + bool graphicPointsEnabled() const { return graphicPointsEnabled(curGraphic); } + bool graphicPointsEnabled(int index) const; + bool graphicFillEnabled() const { return graphicFillEnabled(curGraphic); } + bool graphicFillEnabled(int index) const; + QPen graphicPen() const { return graphicPen(curGraphic); } + QPen graphicPen(int index) const; QPen gridPen() const { return grid_pen; } QPen selectionPen() const { return selpen; } QBrush selectionBrush() const { return selbrush; } @@ -239,8 +239,8 @@ public: double graduationStepY() const { return gridy; } AxisType axisType() const { return axis_type_x; } FloatingAxisType floatingAxisType() const { return floating_axis_type; } - QVector graphicData() const { return graphics[curGraphic].polyline; } - QVector graphicData(int index) const { return graphics[index].polyline; } + QVector graphicData() const { return graphicData(curGraphic); } + QVector graphicData(int index) const; GraphicsData graphicsData() const; QByteArray graphicsDataRaw() const; QWidget * viewport() const { return canvas; } @@ -433,6 +433,7 @@ protected: QPair gridMark(double v) const; void fillDateFormats(); void askForExport(bool view_only); + bool checkGraphicIndex(int index) const; Ui::Graphic * ui; UGLWidget * canvas_gl;