Advertisement
sNow_32

QCustomQuadrangle

Oct 4th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.96 KB | None | 0 0
  1. #ifndef QCUSTOMQUADRANGLE_H
  2. #define QCUSTOMQUADRANGLE_H
  3.  
  4. #include "qcustomplot.h"
  5. #include <QObject>
  6. class QCustomPlot;
  7.  
  8. // *.h
  9. class QCustomQuadrangle{
  10.  
  11. private:
  12.     QCustomPlot *plot;
  13.     QCPCurve *curve;
  14.     QPointF point_1, point_2;
  15.     QPointF point_3, point_4;
  16.     QPen pen;
  17.     QPen selectedPen;
  18.     QString name;
  19.     QVector <double> x, y;
  20.     void fillData();
  21.     //-------------------
  22.     bool pointsTextVisible;
  23.     bool pointsCoordsVisible;
  24.  
  25.     void showTextUnderPoint(QPointF p , int n);
  26.     void setTextUnderPointVisible();
  27.     void setCoordsUnderPointsVisible();
  28.     //---------------
  29.     QCPItemText *pointText;
  30.     //---------------
  31.     QString side;
  32.     //---------------
  33.     //---------------
  34.  
  35. public:
  36.     QCustomQuadrangle(QCustomPlot *parent);
  37.     void setPoints(QPointF __p1, QPointF __p2,
  38.                    QPointF __p3, QPointF __p4);
  39.     void show();
  40.     //---------------
  41.     void setPen(QPen __pen);
  42.     void setName(QString __name);
  43.     void setData(QVector <double> __x,
  44.                  QVector <double> __y);
  45.     // видимость точек.
  46.     void setPointsVisible( bool v = true );
  47.     // видимость координат.
  48.     void setCoordsVisible( bool v = true );
  49.  
  50.  
  51.     //----------------
  52.     QPen getPen();
  53.     QString getName();
  54.     double getLineLength( QPointF point_start,
  55.                           QPointF point_end );
  56.     double getPerimeter();
  57.     double getSquare();
  58.     double getAB();
  59.     double getBC();
  60.     double getCD();
  61.     double getDA();
  62.     double getDiagonal_AC();
  63.     double getDiagonal_BD();
  64.     //----------------
  65.     //----------------
  66.     QVector <double> getXPoints();
  67.     QVector <double> getYPoints();
  68.     //----------------
  69.     QPointF getPoint_1();
  70.     QPointF getPoint_2();
  71.     QPointF getPoint_3();
  72.     QPointF getPoint_4();
  73.     //----------------
  74.     void removePointsText();
  75.     QList < QCPItemText *> textList;
  76.     //----------------
  77.     //----------------
  78. };
  79.  
  80. #endif // QCUSTOMQUADRANGLE_H
  81.  
  82. // *.cpp
  83. #include "qcustomquadrangle.h"
  84.  
  85. void QCustomQuadrangle::fillData()
  86. {
  87.     x << this->point_1.x() <<  this->point_2.x()
  88.       << this->point_3.x() << this->point_4.x();
  89.     y << this->point_1.y() <<  this->point_2.y()
  90.       << this->point_3.y() << this->point_4.y();
  91. }
  92.  
  93. void QCustomQuadrangle::showTextUnderPoint(QPointF p, int n)
  94. {
  95.     pointText = new QCPItemText( this->plot );
  96.     textList.append(pointText);
  97.     this->plot->addItem( textList.at(textList.count() - 1 ) );
  98.     pointText->setColor( this->pen.color() );
  99.     pointText->position->setCoords( p.x(),
  100.                                     p.y() - 0.5 );
  101.     switch (n) {
  102.     case 1:
  103.         side = "A";
  104.         break;
  105.     case 2:
  106.         side = "B";
  107.         break;
  108.     case 3:
  109.         side = "C";
  110.         break;
  111.     case 4:
  112.         side = "D";
  113.         break;
  114.     default:
  115.         break;
  116.     }
  117.     pointText->setText( side + " [" + QString::number( p.x() )
  118.                         + "; " + QString::number( p.y() ) + "]" );
  119. }
  120.  
  121. void QCustomQuadrangle::setTextUnderPointVisible()
  122. {
  123.     if (this->pointsTextVisible) {
  124.         // стиль точек.
  125.         curve->setScatterStyle(
  126.                     QCPScatterStyle(QCPScatterStyle::ssCircle,
  127.                                     QPen( this->pen ),
  128.                                     QBrush( this->pen.color() ), 5 ) );
  129.     } else {
  130.         curve->setScatterStyle(
  131.                     QCPScatterStyle(QCPScatterStyle::ssNone));
  132.     }
  133. }
  134.  
  135. void QCustomQuadrangle::setCoordsUnderPointsVisible()
  136. {
  137.     if (this->pointsCoordsVisible) {
  138.         // показываем координаты точек.
  139.         this->showTextUnderPoint( point_1, 1 );
  140.         this->showTextUnderPoint( point_2, 2 );
  141.         this->showTextUnderPoint( point_3, 3 );
  142.         this->showTextUnderPoint( point_4, 4 );
  143.     }
  144. }
  145.  
  146.  
  147. QCustomQuadrangle::QCustomQuadrangle(QCustomPlot *parent)
  148. {
  149.     this->plot = parent;
  150. }
  151.  
  152. void QCustomQuadrangle::setPoints(QPointF __p1, QPointF __p2, QPointF __p3, QPointF __p4)
  153. {
  154.     this->point_1 = __p1;
  155.     this->point_2 = __p2;
  156.     this->point_3 = __p3;
  157.     this->point_4 = __p4;
  158. }
  159. //----------------------
  160. // show
  161. void QCustomQuadrangle::show()
  162. {
  163.     this->fillData();
  164.     curve = new QCPCurve(this->plot->xAxis,
  165.                          this->plot->yAxis);
  166.     curve->setData(this->x, this->y);
  167.     curve->setPen(this->pen);
  168.     curve->setName(this->name);
  169.     this->selectedPen = curve->pen();
  170.     this->selectedPen.setWidth(3);
  171.     curve->setSelectedPen(this->selectedPen);
  172.     this->setCoordsUnderPointsVisible();
  173.     this->setTextUnderPointVisible();
  174.     //
  175.     this->plot->addPlottable(curve);
  176. }
  177. //-------------------------
  178.  
  179. void QCustomQuadrangle::setPen(QPen __pen)
  180. {
  181.     this->pen = __pen;
  182. }
  183.  
  184. void QCustomQuadrangle::setName(QString __name)
  185. {
  186.     this->name = __name;
  187. }
  188.  
  189. void QCustomQuadrangle::setData(QVector<double> __x, QVector<double> __y)
  190. {
  191.     this->x = __x;
  192.     this->y = __y;
  193.  
  194.     this->point_1.setX(this->x.at(0));
  195.     this->point_2.setX(this->x.at(1));
  196.     this->point_3.setX(this->x.at(2));
  197.     this->point_4.setX(this->x.at(3));
  198.  
  199.     this->point_1.setY(this->y.at(0));
  200.     this->point_2.setY(this->y.at(1));
  201.     this->point_3.setY(this->y.at(2));
  202.     this->point_4.setY(this->y.at(3));
  203. }
  204.  
  205. void QCustomQuadrangle::setPointsVisible(bool v)
  206. {
  207.     this->pointsTextVisible = v;
  208. }
  209.  
  210. void QCustomQuadrangle::setCoordsVisible(bool v)
  211. {
  212.  
  213.     this->pointsCoordsVisible = v;
  214. }
  215.  
  216. QPen QCustomQuadrangle::getPen()
  217. {
  218.     return this->pen;
  219. }
  220.  
  221. QString QCustomQuadrangle::getName()
  222. {
  223.     return this->name;
  224. }
  225.  
  226. double QCustomQuadrangle::getLineLength(QPointF point_start, QPointF point_end)
  227. {
  228.     return ( qSqrt( qPow( ( point_end.x() - point_start.x() ), 2 )
  229.                     + qPow( ( point_end.y() - point_start.y() ), 2) ) );
  230. }
  231.  
  232. double QCustomQuadrangle::getPerimeter()
  233. {
  234.     return (   getLineLength( this->point_1, this->point_2 )
  235.                + getLineLength( this->point_2, this->point_3 )
  236.                + getLineLength( this->point_3, this->point_4 )
  237.                + getLineLength( this->point_4, this->point_1 ));
  238. }
  239.  
  240. double QCustomQuadrangle::getSquare()
  241. {
  242.     double S, s1, s2, s3, s4;
  243.     double p = this->getPerimeter() / 2;
  244.     s1 = p - getLineLength( this->point_1, this->point_2 );
  245.     s2 = p - getLineLength( this->point_2, this->point_3 );
  246.     s3 = p - getLineLength( this->point_3, this->point_4 );
  247.     s4 = p - getLineLength( this->point_4, this->point_1 );
  248.     S = qSqrt( s1 * s2 * s3 * s4 );
  249.     return ( S );
  250. }
  251.  
  252. double QCustomQuadrangle::getAB()
  253. {
  254.     return getLineLength( this->point_1, this->point_2 );
  255. }
  256.  
  257. double QCustomQuadrangle::getBC()
  258. {
  259.     return getLineLength( this->point_2, this->point_3 );
  260. }
  261.  
  262. double QCustomQuadrangle::getCD()
  263. {
  264.     return getLineLength( this->point_3, this->point_4 );
  265. }
  266.  
  267. double QCustomQuadrangle::getDA()
  268. {
  269.     return getLineLength( this->point_4, this->point_1 );
  270. }
  271.  
  272. double QCustomQuadrangle::getDiagonal_AC()
  273. {
  274.     return getLineLength(this->point_1,
  275.                          this->point_3);
  276. }
  277.  
  278. double QCustomQuadrangle::getDiagonal_BD()
  279. {
  280.     return getLineLength(this->point_2,
  281.                          this->point_4);
  282. }
  283.  
  284. QVector<double> QCustomQuadrangle::getXPoints()
  285. {
  286.     return this->x;
  287. }
  288.  
  289. QVector<double> QCustomQuadrangle::getYPoints()
  290. {
  291.     return this->y;
  292. }
  293.  
  294. QPointF QCustomQuadrangle::getPoint_1()
  295. {
  296.     return this->point_1;
  297. }
  298.  
  299. QPointF QCustomQuadrangle::getPoint_2()
  300. {
  301.     return this->point_2;
  302. }
  303.  
  304. QPointF QCustomQuadrangle::getPoint_3()
  305. {
  306.     return this->point_3;
  307. }
  308.  
  309. QPointF QCustomQuadrangle::getPoint_4()
  310. {
  311.     return this->point_4;
  312. }
  313.  
  314. void QCustomQuadrangle::removePointsText()
  315. {
  316.     if (this->textList.size() != 0) {
  317.         this->plot->removeItem(this->textList.at(0));
  318.         this->plot->removeItem(this->textList.at(1));
  319.         this->plot->removeItem(this->textList.at(2));
  320.         this->plot->removeItem(this->textList.at(3));
  321.     }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement