1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
|
/*************************************************************************************
* Copyright (C) 2011 by Aleix Pol <aleixpol@kde.org> *
* Copyright (C) 2012-2013 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "plotter2d.h"
#include "planecurve.h"
#include "plotsmodel.h"
#include "private/utils/mathutils.h"
#include <cmath>
#include <QPalette>
#include <QPainter>
#include <qfontdatabase.h>
#include <QDebug>
#include <qnumeric.h>
#if defined(HAVE_IEEEFP_H)
#include <ieeefp.h>
// bool qIsInf(double x) { return !finite(x) && x==x; }
#endif
using namespace Analitza;
// #define DEBUG_GRAPH
QColor const Plotter2D::m_axeColor(100,100,255); //TODO convert from const to param/attr and make setAxisColor(Qt::oriantation, qcolor)
QColor const Plotter2D::m_derivativeColor(90,90,160); //TODO remove derivative logic from plotter2d, move it to other module
QString const Plotter2D::PiSymbol(QChar(0x03C0));
QString const Plotter2D::DegreeSymbol(QChar(0x00B0));
QString const Plotter2D::GradianSymbol(QChar(0x1D4D));
double const Plotter2D::Pi6 = M_PI_2/3.0;
double const Plotter2D::Pi12 = Plotter2D::Pi6*0.5;
double const Plotter2D::Pi36 = Plotter2D::Pi6/6.0;
const double Plotter2D::ZoomInFactor = 0.97/2; // (regular mouse wheel forward value) / 2
const double Plotter2D::ZoomOutFactor = 2*1.03; // 2 x (regular mouse wheel backward value)
struct Plotter2D::GridInfo
{
double inc, xini, yini, xend, yend;
int incLabelSkip;
// sub5 flag is used for draw sub 5 intervals instead of 4
int subinc; // true if inc=5*pow(10,n) (so, in this case, we draw 5 sub intervals, not 4)
// we need scale for ticks, and for labels, thicks can be minor, labels no: so
// this is necessary for minor ticks and for minir grid (but not for labels, so when draw labels we need )
int nxiniticks, nyiniticks, nxendticks, nyendticks; // nxini = floor(viewport.left()/inc), so xini is nxini*inc ... and so on
int nxinilabels, nyinilabels, nxendlabels, nyendlabels; // nxini = floor(viewport.left()/inc), so xini is nxini*inc ... and so on
};
Plotter2D::Plotter2D(const QSizeF& size)
: m_showGrid(true)
, m_showMinorGrid(false)
, m_gridColor(QColor(Qt::lightGray).lighter(120))
, m_backgroundColor(Qt::white)
, m_autoGridStyle(true)
, m_gridStyleHint(Squares)
, rang_x(0)
, rang_y(0)
, m_keepRatio(true)
, m_dirty(true)
, m_size(size)
, m_model(0)
, m_angleMode(Radian)
, m_scaleMode(Linear)
, m_showTicks(Qt::Vertical|Qt::Horizontal)
, m_showTickLabels(Qt::Vertical|Qt::Horizontal)
, m_showMinorTicks(false)
, m_showAxes(Qt::Vertical|Qt::Horizontal)
, m_showPolarAxis(false)
, m_showPolarAngles(false)
, m_axisXLabel(QStringLiteral("x"))
, m_axisYLabel(QStringLiteral("y"))
{}
Plotter2D::~Plotter2D()
{}
void Plotter2D::setGridStyleHint(GridStyle suggestedgs)
{
m_gridStyleHint = suggestedgs;
forceRepaint();
}
const QColor Plotter2D::computeSubGridColor() const
{
//impl. details: since any kde user can create any palette style, we need this hard/magic numbres
// because there is no way to guess, however, this code covers almost any case,
// it was tested with more then 35 color styles, and all give good results.
QColor col = m_gridColor;
if (m_backgroundColor.value() < 200)
{
if (m_gridColor.value() < 40)
col.setHsv(col.hsvHue(), col.hsvSaturation(), m_gridColor.value()-15);
else
col.setHsv(col.hsvHue(), col.hsvSaturation(), m_gridColor.value()>=255?120:m_gridColor.value()-10);
}
else // e.g: background color white and grid color black
if (m_backgroundColor.value() < 245)
col.setHsv(col.hsvHue(), col.hsvSaturation(), m_backgroundColor.value()-(m_backgroundColor.value()-200)/3);
else
col.setHsv(col.hsvHue(), col.hsvSaturation(), m_backgroundColor.value()-(m_backgroundColor.value()-200)/8);
return col;
}
const Plotter2D::GridInfo Plotter2D::getGridInfo() const
{
GridInfo ret;
if (m_scaleMode == Linear) {
const double val = log10(qMax(viewport.width(), -viewport.height()));
const double diff = val-floor(val);
const double magnitude = pow(10, floor(val)-1);
ret.inc = magnitude;
ret.incLabelSkip = diff < 0.5 ? 1 : 3;
} else {
ret.inc = M_PI;
ret.incLabelSkip = 1;
}
ret.subinc = 4;
ret.nxinilabels = std::floor(viewport.left()/ret.inc);
ret.nyinilabels = std::floor(viewport.bottom()/ret.inc);
ret.nxendlabels = std::ceil(viewport.right()/ret.inc);
ret.nyendlabels = std::ceil(viewport.top()/ret.inc);
ret.xini = ret.nxinilabels*ret.inc;
ret.yini = ret.nyinilabels*ret.inc;
ret.xend = ret.nxendlabels*ret.inc;
ret.yend = ret.nyendlabels*ret.inc;
const bool drawminor = m_showMinorGrid || m_showMinorTicks;
const double nfactor = drawminor ? ret.subinc : 1;
ret.nxiniticks = nfactor*ret.nxinilabels;
ret.nyiniticks = nfactor*ret.nyinilabels;
ret.nxendticks = nfactor*ret.nxendlabels;
ret.nyendticks = nfactor*ret.nyendlabels;
return ret;
}
void Plotter2D::drawGrid(QPaintDevice *qpd)
{
QPainter p;
p.begin(qpd);
int current=currentFunction();
PlotItem* plot = itemAt(current);
GridStyle t = Squares; // default for Cartesian coord. sys.
if (plot && plot->coordinateSystem() == Polar)
t = Circles;
if (!m_autoGridStyle)
t = m_gridStyleHint;
drawAxes(&p, t);
}
void Plotter2D::drawAxes(QPainter* painter, GridStyle gridStyle) const
{
GridInfo grid = getGridInfo();
switch (gridStyle)
{
case Circles: drawCircles(painter, grid, gridStyle); break;
default: drawSquares(painter, grid, gridStyle); break;
}
drawMainAxes(painter);
//NOTE always draw the ticks at the end: avoid the grid lines overlap the ticks text
drawGridTickLabels(painter, grid, gridStyle);
}
void Plotter2D::drawMainAxes(QPainter* painter) const
{
const QFontMetrics fm(painter->font());
const QPen axesPen(m_axeColor, 1, Qt::SolidLine);
const QPointF center = toWidget(QPointF(0.,0.));
painter->setPen(axesPen);
painter->setBrush(axesPen.color());
const int startAngleX = 150*16;
const int startAngleY = 240*16;
const int spanAngle = 60*16;
const QPointF Xright(this->width(), center.y());
const QPointF Ytop(center.x(), 0.);
const double arrowWidth=15., arrowHeight=4.;
const QPointF dpx(arrowWidth, arrowHeight);
const QPointF dpy(arrowHeight, arrowWidth);
const QRectF rectX(Xright+dpx, Xright-dpx);
const QRectF rectY(Ytop+dpy, Ytop-dpy);
if (m_showAxes&Qt::Horizontal)
{
painter->drawLine(QPointF(0., center.y()), Xright);
painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawPie(rectX, startAngleX, spanAngle);
}
if (m_showAxes&Qt::Vertical)
{
painter->drawLine(Ytop, QPointF(center.x(), this->height()));
painter->setRenderHint(QPainter::Antialiasing, true);
painter->drawPie(rectY, startAngleY, spanAngle);
}
painter->setRenderHint(QPainter::Antialiasing, true);
// axis labels
QFont labelsfont = painter->font();
labelsfont.setBold(true);
painter->drawText(Xright.x() - fm.width(m_axisXLabel) - 5, center.y() - 20, m_axisXLabel);
painter->drawText(center.x() + 5, Ytop.y() + fm.height() + 15, m_axisYLabel);
//write coords
QString rightBound=QString::number(viewport.right());
int width=painter->fontMetrics().width(rightBound);
painter->drawText(QPointF(3.+this->width()/2., 13. ), QString::number(viewport.top()));
painter->drawText(QPointF(3.+this->width()/2., this->height()-5. ), QString::number(viewport.bottom()));
painter->drawText(QPointF(8. , this->height()/2.-5.), QString::number(viewport.left()));
painter->drawText(QPointF(this->width()-width, this->height()/2.-5.), rightBound);
//EO write coords
}
const QString Plotter2D::computeAngleLabelByFrac(unsigned int n, unsigned int d) const
{
QString s;
switch (m_angleMode)
{
case Radian:
{
s = (n==1) ? QString() : QString::number(n);
s += PiSymbol;
s += (d==1) ? QString() : '/'+QString::number(d);
}
break;
case Degree: s = QString::number(radiansToDegrees(n*M_PI/d))+DegreeSymbol; break;
case Gradian: s = QString::number(radiansToGradians(n*M_PI/d))+GradianSymbol; break;
}
return s;
}
const QString Plotter2D::computeAngleLabelByStep(unsigned int k, unsigned int step) const
{
QString s;
switch (m_angleMode)
{
case Radian:
{
s = (k==1) ? ((step==1) ? QLatin1String("") : QString::number(step)) : QString::number(k*step);
s += PiSymbol;
}
break;
case Degree: s = QString::number(radiansToDegrees(k*step*M_PI))+DegreeSymbol; break;
case Gradian: s = QString::number(radiansToGradians(k*step*M_PI))+GradianSymbol; break;
}
return s;
}
void Plotter2D::drawCartesianTickLabels(QPainter* painter, const Plotter2D::GridInfo& gridinfo, CartesianAxis axis) const
{
Q_ASSERT(axis == XAxis || axis == Analitza::YAxis);
const bool isxaxis = (axis == XAxis);
const double fontHeight = painter->fontMetrics().height();
const bool incbig = (M_PI <= gridinfo.inc);
const unsigned int bigstep = std::floor(gridinfo.inc/M_PI);
const unsigned int step = std::ceil(M_PI/gridinfo.inc);
QString s;
const int from = (axis == Analitza::XAxis) ? gridinfo.nxinilabels : gridinfo.nyinilabels;
const int to = (axis == Analitza::XAxis) ? gridinfo.nxendlabels : gridinfo.nyendlabels;
painter->save();
QFont tickFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont);
painter->setFont(tickFont);
painter->setPen(QPen(QPalette().text().color()));
for (int i = from; i <= to; ++i)
{
if (i == 0 || i%gridinfo.incLabelSkip!=0) continue;
double newval = i*gridinfo.inc;
const QPointF p = isxaxis
? toWidget(QPointF(newval, 0.0))
: toWidget(QPointF(0.0, newval));
switch (m_scaleMode)
{
case Linear:
s = QString::number(newval);
break;
case Trigonometric:
{
s = (i < 0) ? QString(QLatin1Char('-')) : QString();
if (incbig)
s += computeAngleLabelByStep(qAbs(i), bigstep);
else
{
const QPair<unsigned int, unsigned int> frac = simplifyFraction(qAbs(i), step);
s += computeAngleLabelByFrac(frac.first, frac.second);
}
}
break;
}
const int swidth = painter->fontMetrics().width(s);
if (isxaxis)
painter->drawText(p.x() - swidth/2, p.y()+fontHeight, s);
else
painter->drawText(p.x() - swidth - fontHeight/2, p.y()+fontHeight/2, s);
}
painter->restore();
}
void Plotter2D::drawPolarTickLabels(QPainter* painter, const Plotter2D::GridInfo& gridinfo) const
{
unsigned int k = 1;
painter->setPen(m_gridColor);
//TODO if minor
const double newinc = gridinfo.inc/(gridinfo.subinc); // inc with sub intervals
//x
// we assume 0 belongs to interval [gridinfo.xini, gridinfo.xend]
double maxh = qMax(std::abs(gridinfo.xini), std::abs(gridinfo.xend));
int i = std::ceil(maxh/newinc)/2;
double x = i*newinc;
if (std::abs(gridinfo.xend) <= std::abs(gridinfo.xini))
x *= -1.0;
// if 0 doesn't belongs to interval [gridinfo.xini, gridinfo.xend]
if (((gridinfo.xend < 0.0) && (gridinfo.xini < 0.0)) ||
((gridinfo.xend > 0.0) && (gridinfo.xini > 0.0)))
{
maxh = gridinfo.xend - gridinfo.xini;
i = std::ceil(maxh/newinc)/2;
x = gridinfo.xini + i*newinc;
}
//y
// we assume 0 belongs to interval [gridinfo.yini, gridinfo.yend]
maxh = qMax(std::abs(gridinfo.yini), std::abs(gridinfo.yend));
i = std::ceil(maxh/newinc)/2;
double y = i*newinc;
if (std::abs(gridinfo.yend) <= std::abs(gridinfo.yini))
y *= -1.0;
// if 0 doesn't belongs to interval [gridinfo.xini, gridinfo.xend]
if (((gridinfo.yend < 0.0) && (gridinfo.yini < 0.0)) ||
((gridinfo.yend > 0.0) && (gridinfo.yini > 0.0)))
{
maxh = gridinfo.yend - gridinfo.yini;
i = std::ceil(maxh/newinc)/2;
y = gridinfo.yini + i*newinc;
}
const double r = qMax(std::abs(x), std::abs(y)); // radius
const unsigned short axisxseparation = 16; // distance between tick text and x-axis
const float axispolarseparation = axisxseparation*0.5;
double h = Pi6; // pi/6, then 12 rounds
double t = 0.0;
unsigned int turns = 12; // 12 turns in [0, 2pi], since h=pi/6
// if we are far from origin then sudivide angles by pi/12
if (!viewport.contains(QPointF(0.0, 0.0)))
{
h = Pi12;
turns = 24; // 24 turns in [0, 2pi], since h=pi/12
}
k = 0;
const unsigned int drawoverxaxiscount = turns/2;
const unsigned int drawnextyaxiscount = turns/4;
const unsigned int halfturns = drawoverxaxiscount; // or turns/2;
for (uint j = 0; j < turns; ++j, ++k, t += h) // Pi6 then 24 turns
{
const QPair<unsigned int, unsigned int> frac = simplifyFraction(k, halfturns);
QString s = (k != 0) ? computeAngleLabelByFrac(frac.first, frac.second) : QStringLiteral("0");
QPointF p(r*std::cos(t), r*std::sin(t));
if (viewport.contains(p))
{
if (k % drawoverxaxiscount == 0) // draw 0, pi over x
painter->drawText(toWidget(p)+QPointF(0.0, -axispolarseparation), s);
else
if (k % drawnextyaxiscount == 0) // draw pi/2, 3pi/2 next to y
painter->drawText(toWidget(p)+QPointF(axispolarseparation, 0.0), s);
else
painter->drawText(toWidget(p), s);
}
}
}
void Plotter2D::drawGridTickLabels(QPainter* painter, const GridInfo& gridinfo, GridStyle gridStyle) const
{
if (m_showTickLabels & Qt::Horizontal)
drawCartesianTickLabels(painter, gridinfo, XAxis);
if (m_showTickLabels & Qt::Vertical)
drawCartesianTickLabels(painter, gridinfo, YAxis);
if ((gridStyle == Circles) && m_showPolarAngles && m_showGrid) // draw labels for angles (polar axis)
drawPolarTickLabels(painter, gridinfo);
}
void Plotter2D::drawCircles(QPainter* painter, const GridInfo& gridinfo, GridStyle gridStyle) const
{
Q_ASSERT(gridStyle == Analitza::Circles);
painter->setRenderHint(QPainter::Antialiasing, false);
const QPen textPen = QPen(QPalette().text().color());
const QPen gridPen(m_gridColor);
const QPen subGridPen(computeSubGridColor());
const unsigned short nsubinc = gridinfo.subinc; // count for draw sub intervals
const bool drawminor = m_showMinorGrid || m_showMinorTicks;
const double inc = drawminor ? gridinfo.inc/nsubinc : gridinfo.inc; // if show minor, then inc with sub intervals
if (m_showGrid)
{
const qreal until = qMax(qMax(qAbs(viewport.left()), qAbs(viewport.right())), qMax(qAbs(viewport.top()), qAbs(viewport.bottom())))*M_SQRT2;
int k = 1;
painter->setPen(gridPen);
for (double i = inc; i < until; i += inc, ++k)
{
if (i == 0.0) continue;
QPointF p(toWidget(QPointF(i,i)));
QPointF p2(toWidget(QPointF(-i,-i)));
QRectF er(p.x(),p.y(), p2.x()-p.x(), p2.y()-p.y());
if ((k % nsubinc == 0) || !drawminor) // intervals
{
painter->setPen(gridPen);
painter->drawEllipse(er);
}
else if (m_showMinorGrid)// sub intervals
{
painter->setPen(subGridPen);
painter->drawEllipse(er);
}
}
if (m_showPolarAxis) // draw polar rays
{
double h = Pi12; // 2pi/(Pi/12) = 24 steps/turns
const double r = std::abs(until); // radius
const QPointF origin = toWidget(QPointF(0,0));
int k = 1;
unsigned int alternatesubgridcount = 2;
unsigned int dontdrawataxiscount = 5;
unsigned int steps = 24; // or turns
// if we are far from origin then sudivide angles by 5 degrees
if (!viewport.contains(QPointF(0.0, 0.0)))
{
h = Pi36; // 2pi/(Pi/36) = 72 steps
steps = 72; // or turns
alternatesubgridcount = 3;
dontdrawataxiscount = 17;
}
double t = h;
for (uint j = 1; j <= steps; ++j, ++k, t += h)
{
if (j % alternatesubgridcount == 0)
painter->setPen(gridPen);
else if (m_showMinorGrid)
{
painter->setPen(subGridPen);
QPointF p = toWidget(QPointF(r*std::cos(t), r*std::sin(t)));
painter->drawLine(origin, p);
if (k % dontdrawataxiscount == 0) // avoid draw the ray at 0,pi/2,pi,3pi/2
{
t += h;
++j;
}
}
}
}
}
//BEGIN draw ticks
if (m_showTickLabels & Qt::Horizontal)
{
painter->setPen(textPen);
const QPointF yoffset(0.,-3.);
for (int j = gridinfo.nxiniticks, i = 0; j < gridinfo.nxendticks; ++j, ++i)
{
if (j == 0) continue;
const double x = j*inc;
const QPointF p = toWidget(QPointF(x, 0.));
if ((i % nsubinc == 0) || !drawminor || m_showMinorTicks)
painter->drawLine(p, p+yoffset);
}
}
if (m_showTickLabels & Qt::Vertical)
{
painter->setPen(textPen);
const QPointF xoffset(3.,0.);
for (int j = gridinfo.nyiniticks, i = 0; j < gridinfo.nyendticks; ++j, ++i)
{
if (j == 0) continue;
const double y = j*inc;
const QPointF p = toWidget(QPointF(0., y));
if ((i % nsubinc == 0) || !(m_showMinorGrid || m_showMinorTicks) || m_showMinorTicks)
painter->drawLine(p, p+xoffset);
}
}
//END draw ticks
}
void Plotter2D::drawSquares(QPainter* painter, const GridInfo& gridinfo, GridStyle gridStyle) const
{
painter->setRenderHint(QPainter::Antialiasing, false);
const QPen textPen = QPen(QPalette().text().color());
const QPen gridPen(m_gridColor);
const QPen subGridPen(computeSubGridColor());
const QPen gridPenBold(gridPen.brush(), 2);
const QPen subGridPenBold(subGridPen.brush(), gridPenBold.widthF());
const unsigned short nsubinc = gridinfo.subinc; // count for draw sub intervals
const bool drawminor = m_showMinorGrid || m_showMinorTicks;
const double inc = drawminor? gridinfo.inc/nsubinc : gridinfo.inc; // if show minor, then inc with sub intervals
for (int n = gridinfo.nxiniticks, i = 0; n < gridinfo.nxendticks; ++n, ++i)
{
if (n == 0) continue;
const double x = n*inc;
const QPointF p = toWidget(QPointF(x, 0.));
if (m_showGrid && gridStyle == Crosses)
{
const double crossside = 5;
const double centx = p.x();
for (int j = gridinfo.nyiniticks, k = 0; j < gridinfo.nyendticks; ++j, ++k)
{
if (j == 0) continue;
const double y = j*inc;
const QPointF py = toWidget(QPointF(0., y));
const double centy = py.y();
if (((i % nsubinc == 0) && (k % nsubinc == 0)) || !drawminor) // intervals
{
painter->setPen(gridPenBold);
painter->drawLine(QPointF(centx-crossside, centy), QPointF(centx+crossside, centy)); // horizontal
painter->drawLine(QPointF(centx, centy-crossside), QPointF(centx, centy+crossside)); // vertical
}
else if (m_showMinorGrid)// sub intervals
{
painter->setPen(subGridPenBold);
painter->drawLine(QPointF(centx-crossside, centy), QPointF(centx+crossside, centy)); // horizontal
painter->drawLine(QPointF(centx, centy-crossside), QPointF(centx, centy+crossside)); // vertical
}
}
}
if ((i % nsubinc == 0) || !drawminor) // intervals
{
if (m_showGrid && (gridStyle == Squares || gridStyle == VerticalLines))
{
painter->setPen(gridPen);
painter->drawLine(QPointF(p.x(), height()), QPointF(p.x(), 0.));
}
if ((m_showTicks & Qt::Horizontal))
{
painter->setPen(QPen(QPalette().text().color()));
painter->drawLine(p, p+QPointF(0.,-3.));
}
}
else // sub intervals
{
if (m_showGrid && m_showMinorGrid && (gridStyle == Squares || gridStyle == VerticalLines))
{
painter->setPen(subGridPen);
painter->drawLine(QPointF(p.x(), height()), QPointF(p.x(), 0.));
}
if ((m_showTicks & Qt::Horizontal) && m_showMinorTicks)
{
painter->setPen(QPen(QPalette().text().color()));
painter->drawLine(p, p+QPointF(0.,-3.));
}
}
}
for (int j = gridinfo.nyiniticks, i = 0; j < gridinfo.nyendticks; ++j, ++i)
{
if (j == 0) continue;
const double y = j*inc;
const QPointF p = toWidget(QPointF(0., y));
if ((i % nsubinc == 0) || !drawminor) // intervals
{
if (m_showGrid && (gridStyle == Squares || gridStyle == HorizontalLines))
{
painter->setPen(gridPen);
painter->drawLine(QPointF(0., p.y()), QPointF(width(), p.y()));
}
if ((m_showTicks & Qt::Horizontal))
{
painter->setPen(QPen(QPalette().text().color()));
painter->drawLine(p, p+QPointF(3.,0.));
}
}
else // sub intervals
{
if (m_showGrid && m_showMinorGrid && (gridStyle == Squares || gridStyle == HorizontalLines))
{
painter->setPen(subGridPen);
painter->drawLine(QPointF(0., p.y()), QPointF(width(), p.y()));
}
if ((m_showTicks & Qt::Horizontal) && m_showMinorTicks)
{
painter->setPen(QPen(QPalette().text().color()));
painter->drawLine(p, p+QPointF(3.,0.));
}
}
}
}
PlotItem* Plotter2D::itemAt(int row) const
{
if (!m_model)
return 0;
QModelIndex pi = m_model->index(row, 0);
if (!pi.isValid())
return 0;
PlotItem* plot = pi.data(PlotsModel::PlotRole).value<PlotItem*>();
if (plot->spaceDimension() != Dim2D)
return 0;
return plot;
}
void Plotter2D::drawFunctions(QPaintDevice *qpd)
{
drawGrid(qpd);
QPen pfunc(QColor(0,150,0), 2);
QPainter p;
p.begin(qpd);
p.setPen(pfunc);
if (!m_model || m_dirty)
return;
p.setRenderHint(QPainter::Antialiasing, true);
int current=currentFunction();
int dpr = qMax<int>(1, qRound(qpd->logicalDpiX()/100.));
dpr *= dpr;
for (int k = 0; k < m_model->rowCount(); ++k )
{
PlaneCurve* curve = dynamic_cast<PlaneCurve *>(itemAt(k));
//NOTE from GSOC: not all valid plots always has points (so, we need to check if points().isEmpty() too)
if (!curve || !curve->isVisible() || curve->points().isEmpty())
continue;
pfunc.setColor(curve->color());
pfunc.setWidth((k==current)+dpr);
pfunc.setStyle(Qt::SolidLine);
p.setPen(pfunc);
const QVector<QPointF> &vect=curve->points();
QVector<int> jumps=curve->jumps();
unsigned int pointsCount = vect.count();
QPointF ultim = toWidget(vect.at(0));
int nextjump;
nextjump = jumps.isEmpty() ? -1 : jumps.first();
if (!jumps.isEmpty()) jumps.remove(0);
// #define DEBUG_GRAPH 1
#ifdef DEBUG_GRAPH
qDebug() << "---------" << jumps.count()+1;
#endif
for(unsigned int j=0; j<pointsCount; ++j)
{
const QPointF act = toWidget(vect.at(j));
// qDebug() << "xxxxx" << act << ultim << qIsNaN(act.y()) << qIsNaN(ultim.y());
if(qIsInf(act.y()) && !qIsNaN(act.y())) qDebug() << "trying to plot from a NaN value" << act << ultim;
else if(qIsInf(act.y()) && qIsNaN(act.y())) qDebug() << "trying to plot to a NaN value";
bool bothinf=(qIsInf(ultim.y()) && qIsInf(act.y())) || (qIsInf(ultim.x()) && qIsInf(act.x()));
if(!bothinf && !qIsNaN(act.y()) && !qIsNaN(ultim.y()) && nextjump!=int(j)) {
if(qIsInf(ultim.y())) {
if(act.y()<0) ultim.setY(0);
if(act.y()>0) ultim.setY(qpd->height());
}
//
QPointF act2(act);
if(qIsInf(act2.y())) {
if(ultim.y()<0) act2.setY(0);
if(ultim.y()>0) act2.setY(qpd->height());
}
// qDebug() << "xxxxx" << act2 << ultim << qIsNaN(act2.y()) << qIsNaN(ultim.y());
p.drawLine(ultim, act2);
#ifdef DEBUG_GRAPH
QPen dpen(Qt::red);
dpen.setWidth(3);
p.setPen(dpen);
p.drawPoint(ultim);
p.setPen(pfunc);
#endif
} else if(nextjump==int(j)) {
do {
if(nextjump!=int(j))
p.drawPoint(act);
nextjump = jumps.isEmpty() ? -1 : jumps.first();
if (!jumps.isEmpty()) jumps.remove(0);
} while(!jumps.isEmpty() && jumps.first()==nextjump+1);
#ifdef DEBUG_GRAPH
qDebug() << "jumpiiiiiing" << ultim << toWidget(vect.at(j));
QPen dpen(Qt::blue);
dpen.setWidth(2);
p.setPen(dpen);
p.drawLine(QLineF(QPointF(act.x(), height()/2-10), QPointF(act.x(), height()/2+10)));
p.setPen(pfunc);
#endif
}
ultim=act;
}
}
p.end();
}
void Plotter2D::updateFunctions(const QModelIndex & parent, int start, int end)
{
if (!m_model || parent.isValid())
return;
QRectF viewportFixed = viewport;
viewportFixed.setTopLeft(viewport.bottomLeft());
viewportFixed.setHeight(std::fabs(viewport.height()));
for(int i=start; i<=end; i++)
{
PlaneCurve* curve = dynamic_cast<PlaneCurve *>(itemAt(i));
if (!curve || !curve->isVisible())
continue;
curve->update(viewportFixed);
}
m_dirty = false; // m_dirty = false means that the we will not recalculate functions points
forceRepaint();
}
QPair<QPointF, QString> Plotter2D::calcImage(const QPointF& ndp) const
{
if (!m_model || currentFunction() == -1)
return QPair<QPointF, QString>();
PlaneCurve* curve = dynamic_cast<PlaneCurve*>(itemAt(currentFunction()));
if (curve && curve->isVisible())
return curve->image(ndp);
return QPair<QPointF, QString>();
}
QRectF Plotter2D::normalizeUserViewport(const QRectF uvp)
{
QRectF normalizeduvp = uvp;
rang_x = width()/normalizeduvp.width();
rang_y = height()/normalizeduvp.height();
if (m_keepRatio && rang_x != rang_y)
{
rang_y=rang_x=qMin(std::fabs(rang_x), std::fabs(rang_y));
if(rang_y>0.) rang_y=-rang_y;
if(rang_x<0.) rang_x=-rang_x;
double newW=width()/rang_x, newH=height()/rang_x;
double mx=(uvp.width()-newW)/2.;
double my=(uvp.height()-newH)/2.;
normalizeduvp.setLeft(uvp.left()+mx);
normalizeduvp.setTop(uvp.bottom()-my);
normalizeduvp.setWidth(newW);
normalizeduvp.setHeight(-newH); //WARNING why negative distance?
//Commented because precision could make the program crash
// Q_ASSERT(uvp.center() == viewport.center());
}
return normalizeduvp;
}
void Plotter2D::updateScale(bool repaint)
{
viewport = normalizeUserViewport(userViewport);
if (repaint) {
if (m_model && m_model->rowCount()>0)
updateFunctions(QModelIndex(), 0, m_model->rowCount()-1);
else
forceRepaint();
}
}
void Plotter2D::setViewport(const QRectF& vp, bool repaint)
{
userViewport = vp;
Q_ASSERT(userViewport.top()>userViewport.bottom());
Q_ASSERT(userViewport.right()>userViewport.left());
updateScale(repaint);
viewportChanged();
}
QLineF Plotter2D::slope(const QPointF& dp) const
{
if (!m_model || currentFunction() == -1)
return QLineF();
PlaneCurve* plot = dynamic_cast<PlaneCurve*>(itemAt(currentFunction()));
if (plot && plot->isVisible())
{
QLineF ret = plot->tangent(dp);
if (ret.isNull() && currentFunction()>=0)
{
QPointF a = calcImage(dp-QPointF(.1,.1)).first;
QPointF b = calcImage(dp+QPointF(.1,.1)).first;
ret = slopeToLine((a.y()-b.y())/(a.x()-b.x()));
}
return ret;
}
return QLineF();
}
QLineF Plotter2D::toWidget(const QLineF &f) const
{
return QLineF(toWidget(f.p1()), toWidget(f.p2()));
}
QPointF Plotter2D::toWidget(const QPointF& p) const
{
double left=-viewport.left(), top=-viewport.top();
return QPointF((left + p.x()) * rang_x, (top + p.y()) * rang_y);
}
QPointF Plotter2D::fromWidget(const QPoint& p) const
{
double part_negativa_x = -viewport.left();
double part_negativa_y = -viewport.top();
return QPointF(p.x()/rang_x-part_negativa_x, p.y()/rang_y-part_negativa_y);
}
QPointF Plotter2D::toViewport(const QPoint &mv) const
{
return QPointF(mv.x()/rang_x, mv.y()/rang_y);
}
void Plotter2D::moveViewport(const QPoint& delta)
{
QPointF rel = toViewport(delta);
QRectF viewport = currentViewport();
viewport.moveLeft(viewport.left() - rel.x());
viewport.moveTop(viewport.top() - rel.y());
setViewport(viewport);
}
void Plotter2D::scaleViewport(qreal scale, const QPoint& center, bool repaint)
{
QPointF p = fromWidget(center);
QSizeF ns = viewport.size()*scale;
QRectF nv(viewport.topLeft(), ns);
setViewport(nv, false); //NOTE here isn't necessary to repaint, thus false
QPointF p2 = p-fromWidget(center);
nv.translate(p2);
setViewport(nv, repaint);
}
void Plotter2D::setKeepAspectRatio(bool ar)
{
m_keepRatio=ar;
updateScale(true);
}
void Plotter2D::setModel(QAbstractItemModel* f)
{
if(m_model == f)
return;
m_model=f;
modelChanged();
forceRepaint();
}
void Plotter2D::setPaintedSize(const QSize& size)
{
m_size=size;
updateScale(true);
}
void Plotter2D::setXAxisLabel(const QString &label)
{
m_axisXLabel = label;
forceRepaint();
}
void Plotter2D::setYAxisLabel(const QString &label)
{
m_axisYLabel = label;
forceRepaint();
}
void Plotter2D::zoomIn(bool repaint)
{
scaleViewport(ZoomInFactor, QPoint(m_size.width()*0.5, m_size.height()*0.5), repaint);
}
void Plotter2D::zoomOut(bool repaint)
{
scaleViewport(ZoomOutFactor, QPoint(m_size.width()*0.5, m_size.height()*0.5), repaint);
}
void Plotter2D::setShowMinorGrid(bool mt)
{
m_showMinorGrid=mt;
forceRepaint();
}
void Plotter2D::setShowGrid(bool show)
{
if (m_showGrid != show) {
m_showGrid=show;
forceRepaint();
showGridChanged();
}
}
|