From 75c7e3421bb2279a2d6c184253acf704104bc561 Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Tue, 29 Sep 2020 13:19:04 +0200 Subject: [PATCH] reworked grid-style representation --- lib/jkqtplotter/jkqtpcoordinateaxes.cpp | 48 +++++++-------- lib/jkqtplotter/jkqtpcoordinateaxes.h | 16 ++--- lib/jkqtplotter/jkqtpcoordinateaxesstyle.cpp | 58 ++++++++++-------- lib/jkqtplotter/jkqtpcoordinateaxesstyle.h | 64 ++++++++++++++------ 4 files changed, 113 insertions(+), 73 deletions(-) diff --git a/lib/jkqtplotter/jkqtpcoordinateaxes.cpp b/lib/jkqtplotter/jkqtpcoordinateaxes.cpp index d37325423e..3ae8ea92a8 100644 --- a/lib/jkqtplotter/jkqtpcoordinateaxes.cpp +++ b/lib/jkqtplotter/jkqtpcoordinateaxes.cpp @@ -841,37 +841,37 @@ void JKQTPCoordinateAxis::setInverted(bool __value) { } void JKQTPCoordinateAxis::setGridColor(const QColor& __value) { - this->axisStyle.gridColor = __value; + this->axisStyle.majorGridStyle.lineColor = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setMinorGridColor(const QColor& __value) { - this->axisStyle.minorGridColor = __value; + this->axisStyle.minorGridStyle.lineColor = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setGridWidth(double __value) { - this->axisStyle.gridWidth = __value; + this->axisStyle.majorGridStyle.lineWidth = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setGridStyle(Qt::PenStyle __value) { - this->axisStyle.gridStyle = __value; + this->axisStyle.majorGridStyle.lineStyle = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setMinorGridWidth(double __value) { - this->axisStyle.minorGridWidth = __value; + this->axisStyle.minorGridStyle.lineWidth = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setMinorGridStyle(Qt::PenStyle __value) { - this->axisStyle.minorGridStyle = __value; + this->axisStyle.minorGridStyle.lineStyle = __value; this->paramsChanged=true; redrawPlot(); } @@ -932,13 +932,13 @@ void JKQTPCoordinateAxis::setLabelDigits(int __value) { } void JKQTPCoordinateAxis::setDrawGrid(bool __value) { - this->axisStyle.drawGrid = __value; + this->axisStyle.majorGridStyle.enabled = __value; this->paramsChanged=true; redrawPlot(); } void JKQTPCoordinateAxis::setDrawMinorGrid(bool __value) { - this->axisStyle.drawMinorGrid = __value; + this->axisStyle.minorGridStyle.enabled = __value; this->paramsChanged=true; redrawPlot(); } @@ -1144,13 +1144,13 @@ void JKQTPVerticalAxis::drawGrids(JKQTPEnhancedPainter& painter) { //qDebug()<<" start JKQTPVerticalAxis::drawGrids("; painter.save(); auto __finalpaint=JKQTPFinally([&painter]() {painter.restore();}); QPen pg=painter.pen(); - pg.setColor(axisStyle.gridColor); - pg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.gridWidth*parent->getLineWidthMultiplier()))); - pg.setStyle(axisStyle.gridStyle); + pg.setColor(axisStyle.majorGridStyle.lineColor); + pg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.majorGridStyle.lineWidth*parent->getLineWidthMultiplier()))); + pg.setStyle(axisStyle.majorGridStyle.lineStyle); QPen pmg=painter.pen(); - pmg.setColor(axisStyle.minorGridColor); - pmg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH,parent->pt2px(painter, axisStyle.minorGridWidth*parent->getLineWidthMultiplier()))); - pmg.setStyle(axisStyle.minorGridStyle); + pmg.setColor(axisStyle.minorGridStyle.lineColor); + pmg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH,parent->pt2px(painter, axisStyle.minorGridStyle.lineWidth*parent->getLineWidthMultiplier()))); + pmg.setStyle(axisStyle.minorGridStyle.lineStyle); //double top=x2p(axismax); //double bottom=x2p(axismin); double left=0; @@ -1179,14 +1179,14 @@ void JKQTPVerticalAxis::drawGrids(JKQTPEnhancedPainter& painter) { //painter.setPen(pg); //qDebug()<<" 1"; - if (axisStyle.drawGrid && (xaxismin) && (fabs(left-right)>0)) { + if (axisStyle.majorGridStyle.enabled && (xaxismin) && (fabs(left-right)>0)) { QLineF l(left, xx, right, xx); if (l.length()>0) lines_pg.append(l);//painter.drawLine(l); } //qDebug()<<" 2"; //painter.setPen(pmg); //qDebug()<<" 3"; - if ((tickLabels.size()<=0) && axisStyle.drawMinorGrid && (axisStyle.minorTicks>0) && (fabs(right-left)>0)) { + if ((tickLabels.size()<=0) && axisStyle.minorGridStyle.enabled && (axisStyle.minorTicks>0) && (fabs(right-left)>0)) { //qDebug()<<" 3.1"; double mx=x+mtdist; //qDebug()<<" 3.2"; @@ -1777,13 +1777,13 @@ void JKQTPHorizontalAxis::drawGrids(JKQTPEnhancedPainter& painter) { bottom=getParentOtheraxisOffset()+getParentOtheraxisWidth();//; } QPen pg=painter.pen(); - pg.setColor(axisStyle.gridColor); - pg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.gridWidth*parent->getFontSizeMultiplier()))); - pg.setStyle(axisStyle.gridStyle); + pg.setColor(axisStyle.majorGridStyle.lineColor); + pg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.majorGridStyle.lineWidth*parent->getFontSizeMultiplier()))); + pg.setStyle(axisStyle.majorGridStyle.lineStyle); QPen pmg=painter.pen(); - pmg.setColor(axisStyle.minorGridColor); - pmg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.minorGridWidth*parent->getLineWidthMultiplier()))); - pmg.setStyle(axisStyle.minorGridStyle); + pmg.setColor(axisStyle.minorGridStyle.lineColor); + pmg.setWidthF(qMax(JKQTPlotterDrawingTools::ABS_MIN_LINEWIDTH, parent->pt2px(painter, axisStyle.minorGridStyle.lineWidth*parent->getLineWidthMultiplier()))); + pmg.setStyle(axisStyle.minorGridStyle.lineStyle); double x=tickStart; QString label=""; @@ -1800,12 +1800,12 @@ void JKQTPHorizontalAxis::drawGrids(JKQTPEnhancedPainter& painter) { //qDebug()<<" tick @ x="< + +JKQTPGridStyle::JKQTPGridStyle(bool isMajor): + enabled(isMajor), + lineColor(QColor("gray")), + lineWidth(isMajor?0.75:0.5), + lineStyle(isMajor?Qt::DashLine:Qt::DotLine) +{ + +} + +void JKQTPGridStyle::loadSettings(const QSettings &settings, const QString &group, const JKQTPGridStyle &defaultStyle) +{ + lineWidth = settings.value(group+"width", defaultStyle.lineWidth).toDouble(); + enabled = settings.value(group+"enabled", defaultStyle.enabled).toBool(); + lineColor=jkqtp_String2QColor(settings.value(group+"color", jkqtp_QColor2String(lineColor)).toString()); + lineStyle=jkqtp_String2QPenStyle(settings.value(group+"style", jkqtp_QPenStyle2String(lineStyle)).toString()); +} + +void JKQTPGridStyle::saveSettings(QSettings &settings, const QString &group) const +{ + settings.setValue(group+"enabled", enabled); + settings.setValue(group+"color", jkqtp_QColor2String(lineColor)); + settings.setValue(group+"width", lineWidth); + settings.setValue(group+"style", jkqtp_QPenStyle2String(lineStyle)); +} + + JKQTPCoordinateAxisStyle::JKQTPCoordinateAxisStyle(): labelDigits(3), autoLabelDigits(true), @@ -33,14 +60,8 @@ JKQTPCoordinateAxisStyle::JKQTPCoordinateAxisStyle(): tickLabelDistance(3), labelDistance(5), tickLabelAngle(0), - drawGrid(true), - gridColor(QColor("gray")), - gridWidth(0.75), - gridStyle(Qt::DashLine), - drawMinorGrid(false), - minorGridColor(QColor("gray")), - minorGridWidth(0.5), - minorGridStyle(Qt::DotLine), + majorGridStyle(true), + minorGridStyle(false), colorZeroAxis(QColor("black")), styleZeroAxis(Qt::SolidLine), axisLineOffset(0) @@ -80,23 +101,17 @@ void JKQTPCoordinateAxisStyle::loadSettings(const QSettings &settings, const QSt minorTickInsideLength = settings.value(group+"minor_tick/inside_length", defaultStyle.minorTickInsideLength).toDouble(); tickLabelDistance = settings.value(group+"ticks/label_distance", defaultStyle.tickLabelDistance).toDouble(); labelDistance = settings.value(group+"axis_label/distance", defaultStyle.labelDistance).toDouble(); - gridWidth = settings.value(group+"grid/width", defaultStyle.gridWidth).toDouble(); - minorGridWidth = settings.value(group+"minor_grid/width", defaultStyle.minorGridWidth).toDouble(); - drawGrid = settings.value(group+"grid/enabled", defaultStyle.drawGrid).toBool(); - drawMinorGrid = settings.value(group+"minor_grid/enabled", defaultStyle.drawMinorGrid).toBool(); labelPosition=String2JKQTPLabelPosition(settings.value(group+"axis_label/position", JKQTPLabelPosition2String(labelPosition)).toString()); labelType=String2JKQTPCALabelType(settings.value(group+"axis_label/type", JKQTPCALabelType2String(labelType)).toString()); axisColor=jkqtp_String2QColor(settings.value(group+"color", jkqtp_QColor2String(axisColor)).toString()); - gridColor=jkqtp_String2QColor(settings.value(group+"grid/color", jkqtp_QColor2String(gridColor)).toString()); - minorGridColor=jkqtp_String2QColor(settings.value(group+"minor_grid/color", jkqtp_QColor2String(minorGridColor)).toString()); - gridStyle=jkqtp_String2QPenStyle(settings.value(group+"grid/style", jkqtp_QPenStyle2String(gridStyle)).toString()); - minorGridStyle=jkqtp_String2QPenStyle(settings.value(group+"minor_grid/style", jkqtp_QPenStyle2String(minorGridStyle)).toString()); drawMode1=String2JKQTPCADrawMode(settings.value(group+"draw_mode1", JKQTPCADrawMode2String(drawMode1)).toString()); drawMode2=String2JKQTPCADrawMode(settings.value(group+"draw_mode2", JKQTPCADrawMode2String(drawMode2)).toString()); tickMode=String2JKQTPLabelTickMode(settings.value(group+"ticks/mode", JKQTPLabelTickMode2String(tickMode)).toString()); colorZeroAxis=jkqtp_String2QColor(settings.value(group+"zero_line/color", jkqtp_QColor2String(colorZeroAxis)).toString()); styleZeroAxis=jkqtp_String2QPenStyle(settings.value(group+"zero_line/style", jkqtp_QPenStyle2String(styleZeroAxis)).toString()); axisLineOffset = settings.value(group+"axis_lines_offset", defaultStyle.axisLineOffset).toDouble(); + majorGridStyle.loadSettings(settings, group+"grid/", defaultStyle.majorGridStyle); + minorGridStyle.loadSettings(settings, group+"minor_grid/", defaultStyle.minorGridStyle); } void JKQTPCoordinateAxisStyle::saveSettings(QSettings &settings, const QString &group) const @@ -107,18 +122,10 @@ void JKQTPCoordinateAxisStyle::saveSettings(QSettings &settings, const QString & settings.setValue(group+"line_width", lineWidth); settings.setValue(group+"axis_lines_offset", axisLineOffset); settings.setValue(group+"min_ticks", minTicks); - settings.setValue(group+"grid/enabled", drawGrid); - settings.setValue(group+"grid/color", jkqtp_QColor2String(gridColor)); - settings.setValue(group+"grid/width", gridWidth); - settings.setValue(group+"grid/style", jkqtp_QPenStyle2String(gridStyle)); settings.setValue(group+"axis_label/distance", labelDistance); settings.setValue(group+"axis_label/font_size", labelFontSize); settings.setValue(group+"axis_label/position", JKQTPLabelPosition2String(labelPosition)); settings.setValue(group+"axis_label/type", JKQTPCALabelType2String(labelType)); - settings.setValue(group+"minor_grid/enabled", drawMinorGrid); - settings.setValue(group+"minor_grid/color", jkqtp_QColor2String(minorGridColor)); - settings.setValue(group+"minor_grid/style", jkqtp_QPenStyle2String(minorGridStyle)); - settings.setValue(group+"minor_grid/width", minorGridWidth); settings.setValue(group+"minor_tick/labels_enabled", minorTickLabelsEnabled); settings.setValue(group+"minor_tick/inside_length", minorTickInsideLength); settings.setValue(group+"minor_tick/label_font_size", minorTickLabelFontSize); @@ -139,4 +146,7 @@ void JKQTPCoordinateAxisStyle::saveSettings(QSettings &settings, const QString & settings.setValue(group+"zero_line/line_width", lineWidthZeroAxis); settings.setValue(group+"zero_line/color", jkqtp_QColor2String(colorZeroAxis)); settings.setValue(group+"zero_line/style", jkqtp_QPenStyle2String(styleZeroAxis)); + majorGridStyle.saveSettings(settings, group+"grid/"); + minorGridStyle.saveSettings(settings, group+"minor_grid/"); } + diff --git a/lib/jkqtplotter/jkqtpcoordinateaxesstyle.h b/lib/jkqtplotter/jkqtpcoordinateaxesstyle.h index 1bc461b9d5..c6a4541fd0 100644 --- a/lib/jkqtplotter/jkqtpcoordinateaxesstyle.h +++ b/lib/jkqtplotter/jkqtpcoordinateaxesstyle.h @@ -28,6 +28,47 @@ class JKQTBasePlotterStyle; // forward + +/** \brief Support Class for JKQTPCoordinateAxis, and summarizes all properties that define the visual styling of a grid (minor or major), associated with a JKQTPCoordinateAxis + * \ingroup jkqtpplotter_styling + * + * \see JKQTPCoordinateAxis, \ref jkqtpplotter_styling + * + */ +class JKQTPLOTTER_LIB_EXPORT JKQTPGridStyle { + Q_GADGET +public: + JKQTPGridStyle(bool isMajor=true); + + + /** \brief loads the plot properties from a QSettings object + * + * \param settings QSettings-object to read from + * \param group Group in the QSettings-object to read from + * \param defaultStyle If a setting cannot be found in \a settings, default values are taken from this object + * By default, this is a default-constructed object + */ + void loadSettings(const QSettings &settings, const QString& group=QString("grid/"), const JKQTPGridStyle &defaultStyle=JKQTPGridStyle()); + + /** \brief saves the plot properties into a QSettings object. + * + * \param settings QSettings-object to save to + * \param group Group in the QSettings-object to save to + */ + void saveSettings(QSettings& settings, const QString& group=QString("grid/")) const; + + /** \brief indicates whether to draw the major grid lines */ + bool enabled; + /** \brief color of the grid*/ + QColor lineColor; + /** \brief width of the grid lines (in pixel) */ + double lineWidth; + /** \brief line stye of the grid lines */ + Qt::PenStyle lineStyle; +}; + + + /** \brief Support Class for JKQTPCoordinateAxis, which summarizes all properties that define the visual styling of a JKQTPCoordinateAxis * \ingroup jkqtpplotter_styling * @@ -131,27 +172,16 @@ class JKQTPLOTTER_LIB_EXPORT JKQTPCoordinateAxisStyle { /** \brief rotation angle of tick labels [-180..180], i.e. given in degrees, default is 0 (horizontal) */ double tickLabelAngle; - /** \brief indicates whether to draw the major grid lines */ - bool drawGrid; - /** \brief color of the grid*/ - QColor gridColor; - /** \brief width of the grid lines (in pixel) */ - double gridWidth; - /** \brief line stye of the grid lines */ - Qt::PenStyle gridStyle; - /** \brief indicates whether to draw the minor grid lines */ - bool drawMinorGrid; - /** \brief color of the minor grid lines */ - QColor minorGridColor; - /** \brief width of the minor grid lines (in pixel) */ - double minorGridWidth; - /** \brief line stye of the minor grid lines */ - Qt::PenStyle minorGridStyle; + /** \brief styling of the major/primary grid */ + JKQTPGridStyle majorGridStyle; + /** \brief styling of the minor/secondary grid */ + JKQTPGridStyle minorGridStyle; + /** \brief color of 0-line */ QColor colorZeroAxis; /** \brief pen style of 0-line */ Qt::PenStyle styleZeroAxis; - /** \brief if non-zero, the line of the coordinate axis is moved outside by this amount [pt] */ + /** \brief if non-zero, the line of the coordinate axis is moved outside the plot by this amount [pt]. This does not apply to the zero-axis! */ double axisLineOffset; };