| 1 | /* |
| 2 | ---------------------------------------------------------------------------- |
| 3 | "THE BEER-WARE LICENSE" (Revision 42): |
| 4 | <[email protected]> wrote this file. As long as you retain |
| 5 | this notice you can do whatever you want with this stuff. If we meet some day, |
| 6 | and you think this stuff is worth it, you can buy me a beer in return. |
| 7 | Daniel Kratzert |
| 8 | ---------------------------------------------------------------------------- |
| 9 | */ |
| 10 | |
| 11 | #include "plotgraph.h" |
| 12 | #include <QtGui> |
| 13 | |
| 14 | |
| 15 | RenderPlot::RenderPlot(int cycles, QWidget *parent) |
| 16 | : QWidget(parent) |
| 17 | { |
| 18 | dataPointShape = Ellipse; |
| 19 | setBackgroundRole(QPalette::Base); |
| 20 | setAutoFillBackground(true); |
| 21 | originshift = 27; |
| 22 | pen.setCapStyle(Qt::RoundCap); |
| 23 | pen.setStyle(Qt::SolidLine); |
| 24 | pen.setWidth(2); |
| 25 | pen.setBrush(Qt::black); |
| 26 | pen.setJoinStyle(Qt::RoundJoin); |
| 27 | sclinespen.setCapStyle(Qt::RoundCap); |
| 28 | sclinespen.setStyle(Qt::DotLine); |
| 29 | sclinespen.setWidth(1); |
| 30 | sclinespen.setBrush(QColor(80, 80, 80, 50)); |
| 31 | datapoints.clear(); |
| 32 | mcycles = cycles; |
| 33 | startcycles = cycles; |
| 34 | pointcolor = QBrush(Qt::green); //! color of the plot points |
| 35 | } |
| 36 | |
| 37 | void RenderPlot::clearPlot() { |
| 38 | datapoints.clear(); |
| 39 | this->update(); |
| 40 | mcycles = startcycles; |
| 41 | } |
| 42 | |
| 43 | QSize RenderPlot::minimumSizeHint() const |
| 44 | { |
| 45 | return QSize(150, 130); |
| 46 | } |
| 47 | |
| 48 | QSize RenderPlot::sizeHint() const |
| 49 | { |
| 50 | return QSize(250, 130); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void RenderPlot::setPen(const QPen &pen) |
| 55 | { |
| 56 | this->pen = pen; |
| 57 | update(); |
| 58 | } |
| 59 | |
| 60 | void RenderPlot::setBrush(const QBrush &brush) |
| 61 | { |
| 62 | this->brush = brush; |
| 63 | update(); |
| 64 | } |
| 65 | |
| 66 | void RenderPlot::setCycle(int numcycle) { |
| 67 | this->mcycles = numcycle; |
| 68 | } |
| 69 | |
| 70 | void RenderPlot::addPoint(qreal percent, int cycle){ |
| 71 | //! Add a point to the list of points. |
| 72 | mypoint p; |
| 73 | p.x = cycle; |
| 74 | p.y = percent; |
| 75 | bool found = false; |
| 76 | if (datapoints.size() > 0) { |
| 77 | foreach (mypoint p, datapoints) { |
| 78 | if (p.x == cycle) { |
| 79 | found = true; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | // make sure only unique data points go into list: |
| 84 | if (found){ |
| 85 | return; |
| 86 | } else { |
| 87 | datapoints.append(p); |
| 88 | } |
| 89 | // extends the x axis if more cycles are coming |
| 90 | if ((cycle-mcycles) == 1) { |
| 91 | mcycles += 1; |
| 92 | } |
| 93 | this->update(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void RenderPlot::drawScale() { |
| 98 | //! Draws the axis scales. The origin ist shefted by "origignshift". |
| 99 | //! The scale ticks are in the distances distx and disty. |
| 100 | QPainter painter(this); |
| 101 | painter.setPen(pen); |
| 102 | painter.setBrush(brush); |
| 103 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 104 | QFont font; |
| 105 | font.setPointSize(11); |
| 106 | painter.setFont(font); |
| 107 | // The X axis line: |
| 108 | painter.save(); |
| 109 | painter.translate(originshift, height()-originshift); |
| 110 | painter.drawLine(0, 0, width(), 0); |
| 111 | painter.translate(width()-40-originshift, -27); |
| 112 | // The X axis label: |
| 113 | QTextDocument tdx; |
| 114 | tdx.setHtml("<font style=font-size:11pt> cycle </font>"); |
| 115 | tdx.drawContents(&painter); |
| 116 | painter.restore(); |
| 117 | |
| 118 | // The Y axis line: |
| 119 | painter.save(); |
| 120 | painter.translate(originshift, height()-originshift); |
| 121 | painter.drawLine(0, 0, 0, -height()); |
| 122 | painter.translate(5, -height()+originshift); |
| 123 | QTextDocument tdy; |
| 124 | // The Y axis label: |
| 125 | tdy.setHtml("<font style=font-size:11pt>% <i>wR</i><sub>2</sub></font>"); |
| 126 | tdy.drawContents(&painter); |
| 127 | painter.restore(); |
| 128 | // need highest y value to get scale right: |
| 129 | QList<qreal> ylist; |
| 130 | foreach (mypoint p, datapoints) { |
| 131 | ylist.append(p.y); |
| 132 | } |
| 133 | #if (QT_VERSION >= 0x050000) |
| 134 | std::sort(ylist.begin(), ylist.end()); |
| 135 | #else |
| 136 | qSort(ylist.begin(), ylist.end()); |
| 137 | #endif |
| 138 | distx = width() / (mcycles+1); |
| 139 | // 14 is an empirical number and defines extra space on the y scale: |
| 140 | if (ylist.length() > 1) { |
| 141 | disty = height() / ((14*ylist.last())); |
| 142 | } else { // default scale: |
| 143 | disty = height() / 10; |
| 144 | } |
| 145 | |
| 146 | // draw X scales |
| 147 | qreal cycle = 0; |
| 148 | for (qreal x = 0; x <= width(); x += distx) { |
| 149 | painter.save(); |
| 150 | font.setPointSize(10); |
| 151 | painter.setFont(font); |
| 152 | // translate to left bottom corner+originshift: |
| 153 | painter.translate(x+originshift, height()-originshift); |
| 154 | // draw scale: |
| 155 | painter.drawLine(0, 0, 0, 5); |
| 156 | // draw intersetioon lines: |
| 157 | if (x > 0 ) { |
| 158 | painter.setPen(sclinespen); |
| 159 | painter.drawLine(0, 0, 0, -height()); |
| 160 | painter.setPen(pen); |
| 161 | } |
| 162 | painter.drawText(-4, 19, QString("%1").arg(cycle)); |
| 163 | painter.restore(); |
| 164 | cycle += 1; |
| 165 | } |
| 166 | |
| 167 | // draw Y scales |
| 168 | // percent and stepsize could be defined outside to make the plotting more versatile: |
| 169 | int percent = 0; |
| 170 | int stepsize = 10; |
| 171 | for (qreal y = 0; y <= (height() - (height()/stepsize)); y += disty) { |
| 172 | painter.save(); |
| 173 | // translate to left bottom corner+originshift: |
| 174 | painter.translate(originshift, height()-y-originshift); |
| 175 | // draw scale: |
| 176 | font.setPointSize(10); |
| 177 | painter.setFont(font); |
| 178 | painter.drawLine(-5, 0, 0, 0); |
| 179 | painter.drawText(-24, 4, QString("%1").arg(percent)); |
| 180 | // draw intersection lines: |
| 181 | if (y > 0) { |
| 182 | painter.setPen(sclinespen); |
| 183 | painter.drawLine(width(), 0, 0, 0); |
| 184 | painter.setPen(pen); |
| 185 | } |
| 186 | painter.restore(); |
| 187 | percent += stepsize; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void RenderPlot::drawPoints(const QList<mypoint> points, int pointsize) { |
| 192 | //! Draws pre-defined points on the graph relative to the originshift. |
| 193 | QPainter painter(this); |
| 194 | QFont font; |
| 195 | font.setPointSize(10); |
| 196 | painter.setFont(font); |
| 197 | painter.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
| 198 | painter.setBrush(pointcolor); |
| 199 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 200 | qreal x = 0; |
| 201 | qreal y = 0; |
| 202 | foreach (mypoint p, points) { |
| 203 | painter.save(); |
| 204 | x = p.x*distx; |
| 205 | y = -p.y*disty*10; // factor 10 could be defined outside to make plotting more versatile |
| 206 | painter.translate(originshift, height()-originshift); |
| 207 | // translate from 0, 0 of plot coordinate system, |
| 208 | // therefore p.x*..., -p.y*...: |
| 209 | painter.translate(x, y); |
| 210 | // draw the y value as text: |
| 211 | painter.drawText(-23, -8, QString("%1").arg(p.y*100, 10, 'f', 2)); |
| 212 | painter.drawEllipse(QPointF(0, 0), pointsize, pointsize); |
| 213 | painter.restore(); |
| 214 | } |
| 215 | painter.setPen(palette().dark().color()); |
| 216 | painter.setBrush(Qt::NoBrush); |
| 217 | painter.drawRect(QRect(0, 0, width() - 1, height() - 1)); |
| 218 | } |
| 219 | |
| 220 | void RenderPlot::drawLines(const QList<mypoint> points) { |
| 221 | //! Draws lines between data points. |
| 222 | if (points.size() < 2) return; |
| 223 | QPainter painter(this); |
| 224 | painter.setPen(QPen(Qt::darkGray, 1, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin)); |
| 225 | painter.setRenderHint(QPainter::SmoothPixmapTransform, true); |
| 226 | qreal lastx = 0.0; |
| 227 | qreal lasty = 0.0; |
| 228 | qreal x = 0; |
| 229 | qreal y = 0; |
| 230 | foreach (mypoint p, points) { |
| 231 | x = p.x*distx; |
| 232 | y = -p.y*disty*10; |
| 233 | painter.save(); |
| 234 | painter.translate(originshift, height()-originshift); |
| 235 | if (x > distx) { // |
| 236 | painter.drawLine(int(lastx), int(lasty), int(x), int(y)); |
| 237 | } |
| 238 | lastx=x; |
| 239 | lasty=y; |
| 240 | painter.restore(); |
| 241 | } |
| 242 | printf("\n"); |
| 243 | } |
| 244 | |
| 245 | void RenderPlot::paintEvent(QPaintEvent *) |
| 246 | //! This runs every time the widget changes. |
| 247 | { |
| 248 | drawScale(); |
| 249 | drawLines(datapoints); |
| 250 | drawPoints(datapoints, 3); |
| 251 | } |
| 252 | |
| 253 |