| 1 |
#include "rootsrenderer.h" |
| 2 |
#include "rootsmodel.h" |
| 3 |
#include <QPainter> |
| 4 |
#include <QDebug> |
| 5 |
#include <cmath> |
| 6 |
|
| 7 |
namespace xmpsolve { |
| 8 |
|
| 9 |
RootsRenderer::RootsRenderer() |
| 10 |
{ |
| 11 |
m_maxImagModule = m_maxRealModule = 0.0; |
| 12 |
m_model = NULL; |
| 13 |
mCenter = QPointF(0.0, 0.0); |
| 14 |
} |
| 15 |
|
| 16 |
void |
| 17 |
RootsRenderer::reloadRoots() |
| 18 |
{ |
| 19 |
m_roots.clear(); |
| 20 |
m_maxRealModule = m_maxImagModule = DBL_MIN; |
| 21 |
|
| 22 |
for (int i = 0; i < m_model->rowCount(); i++) |
| 23 |
{ |
| 24 |
Root * root = (Root*) (m_model->data(m_model->index(i), RootsModel::ROOT).value<void*>()); |
| 25 |
|
| 26 |
// Keep track of the maximum module that we reach in both directions, to |
| 27 |
// create a sensible plot later. |
| 28 |
m_maxRealModule = qMax (fabs(root->get_real_part()), m_maxRealModule); |
| 29 |
m_maxImagModule = qMax (fabs(root->get_imag_part()), m_maxImagModule); |
| 30 |
|
| 31 |
m_roots.append(QPointF(root->get_real_part(), root->get_imag_part())); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
void |
| 36 |
RootsRenderer::zoomIn() |
| 37 |
{ |
| 38 |
m_maxRealModule = m_maxRealModule / sqrt(2); |
| 39 |
m_maxImagModule = m_maxImagModule / sqrt(2); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
RootsRenderer::zoomOut() |
| 44 |
{ |
| 45 |
m_maxRealModule = m_maxRealModule * sqrt(2); |
| 46 |
m_maxImagModule = m_maxImagModule * sqrt(2); |
| 47 |
} |
| 48 |
|
| 49 |
void |
| 50 |
RootsRenderer::setCenter(double x, double y) |
| 51 |
{ |
| 52 |
mCenter = QPointF(x, y); |
| 53 |
} |
| 54 |
|
| 55 |
QPointF |
| 56 |
RootsRenderer::scalePoint(QPointF point, int width, int height) |
| 57 |
{ |
| 58 |
double margin = 24; |
| 59 |
double maxModule = qMax (m_maxRealModule, m_maxImagModule); |
| 60 |
|
| 61 |
double x = (.5 + .5 * (point.x() - mCenter.x()) / maxModule) * (width - 2 * margin); |
| 62 |
double y = (.5 - .5 * (point.y() - mCenter.y()) / maxModule) * (height - 2 * margin); |
| 63 |
|
| 64 |
QPointF newPoint(x + margin, y + margin); |
| 65 |
return newPoint; |
| 66 |
} |
| 67 |
|
| 68 |
QPointF |
| 69 |
RootsRenderer::scaleVectorInverse(QPointF point, int width, int height) |
| 70 |
{ |
| 71 |
double margin = 24; |
| 72 |
double maxModule = qMax (m_maxRealModule, m_maxImagModule); |
| 73 |
|
| 74 |
double x = point.x() / (width - 2 * margin) * maxModule * 2.0; |
| 75 |
double y = - point.y() / (height - 2 * margin) * maxModule * 2.0; |
| 76 |
|
| 77 |
return QPointF(x, y); |
| 78 |
} |
| 79 |
|
| 80 |
QPointF |
| 81 |
RootsRenderer::scaleVector(QPointF point, int width, int height) |
| 82 |
{ |
| 83 |
double margin = 24; |
| 84 |
double maxModule = qMax (m_maxRealModule, m_maxImagModule); |
| 85 |
|
| 86 |
double x = .5 * point.x() / maxModule * (width - 2 * margin); |
| 87 |
double y = -.5 * point.y() / maxModule * (height - 2 * margin); |
| 88 |
|
| 89 |
return QPointF(x, y); |
| 90 |
} |
| 91 |
|
| 92 |
void |
| 93 |
RootsRenderer::drawTicks(QPainter& painter, double w, double h) |
| 94 |
{ |
| 95 |
double maxModule = qMax(m_maxImagModule, m_maxRealModule); |
| 96 |
|
| 97 |
if (maxModule == 0.0 || m_roots.length() == 0) |
| 98 |
return; |
| 99 |
|
| 100 |
int tick_distance_eps = log10 (maxModule * 2); |
| 101 |
double tick_distance = pow(10, tick_distance_eps); |
| 102 |
|
| 103 |
// Adjust the tick_distance |
| 104 |
if (tick_distance > maxModule / 3) |
| 105 |
tick_distance /= 3.0; |
| 106 |
|
| 107 |
// Set a small enough font so numbers don't overlap. |
| 108 |
// painter.setFont(QFont(font().family(), 9)); |
| 109 |
painter.setPen(QColor(Qt::gray)); |
| 110 |
|
| 111 |
for(int i = -10; i <= 10; i++) { |
| 112 |
// X axis |
| 113 |
QPointF tick_center = scalePoint(QPointF(tick_distance * i, 0), w, h); |
| 114 |
painter.drawLine(tick_center - QPointF(0,4), tick_center + QPointF(0,4)); |
| 115 |
|
| 116 |
QString text; |
| 117 |
text.sprintf("%1.2e", tick_distance * i); |
| 118 |
painter.drawText(QRectF(tick_center - scaleVector(QPointF(tick_distance / 2, 0), w, h) + QPointF(0,-12), |
| 119 |
tick_center + scaleVector(QPointF(tick_distance / 2, 0), w, h)), |
| 120 |
Qt::AlignCenter, text); |
| 121 |
|
| 122 |
// No double zeros, please |
| 123 |
if (i == 0) |
| 124 |
continue; |
| 125 |
|
| 126 |
// Y axis |
| 127 |
tick_center = scalePoint(QPointF(0, tick_distance * i), w, h); |
| 128 |
painter.drawLine(tick_center - QPointF(4,0), tick_center + QPointF(4,0)); |
| 129 |
|
| 130 |
text.sprintf("%1.2e", tick_distance * i); |
| 131 |
QRectF br = painter.boundingRect(QRectF(tick_center + scaleVector(QPointF(0, tick_distance / 2), w, h), |
| 132 |
tick_center + scaleVector(QPointF(0, tick_distance / 2), w, h) + QPointF(50, 0)), |
| 133 |
Qt::AlignLeft, text); |
| 134 |
painter.drawText(QRectF(tick_center + scaleVector(QPointF(0, tick_distance / 2), w, h), |
| 135 |
tick_center + scaleVector(QPointF(0, tick_distance / 2), w, h) + |
| 136 |
QPointF(br.right() + 4, 0)), |
| 137 |
Qt::AlignCenter, text); |
| 138 |
|
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void |
| 143 |
RootsRenderer::handlePaintEvent(QPainter& painter, int w, int h, QPaintEvent *) |
| 144 |
{ |
| 145 |
double axis_margin = 6; |
| 146 |
|
| 147 |
// Draw the background |
| 148 |
painter.setBrush(QColor::fromRgb(255, 255, 255)); |
| 149 |
painter.drawRect(QRect(0, 0, w, h)); |
| 150 |
|
| 151 |
// Draw the cartesian axis |
| 152 |
painter.setPen(QColor(Qt::gray)); |
| 153 |
|
| 154 |
QPointF xMax = scalePoint(QPointF(m_maxImagModule, 0), w, h) - QPointF(axis_margin, 0.0); |
| 155 |
QPointF xMin = scalePoint(QPointF(-m_maxImagModule, 0), w, h) + QPointF(axis_margin, 0.0); |
| 156 |
QPointF yMax = scalePoint(QPointF(0, m_maxImagModule), w, h) - QPointF(0.0, axis_margin); |
| 157 |
QPointF yMin = scalePoint(QPointF(0, -m_maxImagModule), w, h) + QPointF(0.0, axis_margin); |
| 158 |
|
| 159 |
painter.drawLine(xMin, xMax); |
| 160 |
painter.drawLine(yMin, yMax); |
| 161 |
|
| 162 |
// Draw the arrows on top of them |
| 163 |
painter.drawLine(xMax - QPointF(8.0, 4.0), xMax); |
| 164 |
painter.drawLine(xMax, xMax - QPointF(8.0, -4.0)); |
| 165 |
painter.drawLine(xMin + QPointF(8.0, 4.0), xMin); |
| 166 |
painter.drawLine(xMin, xMin + QPointF(8.0, -4.0)); |
| 167 |
|
| 168 |
painter.drawLine(yMax + QPointF(4.0, 8.0), yMax); |
| 169 |
painter.drawLine(yMax + QPointF(-4.0, 8.0), yMax); |
| 170 |
painter.drawLine(yMin - QPointF(4.0, 8.0), yMin); |
| 171 |
painter.drawLine(yMin - QPointF(-4.0, 8.0), yMin); |
| 172 |
|
| 173 |
drawTicks(painter, w, h); |
| 174 |
|
| 175 |
// The default color for the point is red. |
| 176 |
painter.setBrush(QBrush("red")); |
| 177 |
painter.setPen(QColor(Qt::red)); |
| 178 |
|
| 179 |
// ...and we need this to keep track of the MARKED point, |
| 180 |
// if it exists. |
| 181 |
int markedPoint = -1; |
| 182 |
|
| 183 |
for (int i = 0; i < m_roots.length(); i++) |
| 184 |
{ |
| 185 |
QPointF p = scalePoint(m_roots[i], w, h); |
| 186 |
|
| 187 |
// We draw only not MARKED points. The MARKED one will be drawn in |
| 188 |
// green outside of this for loop. |
| 189 |
if (! m_model->data(m_model->index(i), RootsModel::MARKED).toBool()) |
| 190 |
painter.drawEllipse(p, 2, 2); |
| 191 |
else |
| 192 |
markedPoint = i; |
| 193 |
} |
| 194 |
|
| 195 |
// If we have left out the MARKED point (and this happend only if this |
| 196 |
// point exists) then draw it with green, and bigger than the others. |
| 197 |
if (markedPoint != -1) |
| 198 |
{ |
| 199 |
QPointF p = scalePoint(m_roots[markedPoint], w, h); |
| 200 |
painter.setBrush(QBrush("green")); |
| 201 |
painter.setBrush(QColor(Qt::green)); |
| 202 |
painter.setPen(QColor(Qt::green)); |
| 203 |
painter.drawEllipse(p, 3, 3); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
} // namespace xmpsolve |