Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef QCUSTOMQUADRANGLE_H
- #define QCUSTOMQUADRANGLE_H
- #include "qcustomplot.h"
- #include <QObject>
- class QCustomPlot;
- // *.h
- class QCustomQuadrangle{
- private:
- QCustomPlot *plot;
- QCPCurve *curve;
- QPointF point_1, point_2;
- QPointF point_3, point_4;
- QPen pen;
- QPen selectedPen;
- QString name;
- QVector <double> x, y;
- void fillData();
- //-------------------
- bool pointsTextVisible;
- bool pointsCoordsVisible;
- void showTextUnderPoint(QPointF p , int n);
- void setTextUnderPointVisible();
- void setCoordsUnderPointsVisible();
- //---------------
- QCPItemText *pointText;
- //---------------
- QString side;
- //---------------
- //---------------
- public:
- QCustomQuadrangle(QCustomPlot *parent);
- void setPoints(QPointF __p1, QPointF __p2,
- QPointF __p3, QPointF __p4);
- void show();
- //---------------
- void setPen(QPen __pen);
- void setName(QString __name);
- void setData(QVector <double> __x,
- QVector <double> __y);
- // видимость точек.
- void setPointsVisible( bool v = true );
- // видимость координат.
- void setCoordsVisible( bool v = true );
- //----------------
- QPen getPen();
- QString getName();
- double getLineLength( QPointF point_start,
- QPointF point_end );
- double getPerimeter();
- double getSquare();
- double getAB();
- double getBC();
- double getCD();
- double getDA();
- double getDiagonal_AC();
- double getDiagonal_BD();
- //----------------
- //----------------
- QVector <double> getXPoints();
- QVector <double> getYPoints();
- //----------------
- QPointF getPoint_1();
- QPointF getPoint_2();
- QPointF getPoint_3();
- QPointF getPoint_4();
- //----------------
- void removePointsText();
- QList < QCPItemText *> textList;
- //----------------
- //----------------
- };
- #endif // QCUSTOMQUADRANGLE_H
- // *.cpp
- #include "qcustomquadrangle.h"
- void QCustomQuadrangle::fillData()
- {
- x << this->point_1.x() << this->point_2.x()
- << this->point_3.x() << this->point_4.x();
- y << this->point_1.y() << this->point_2.y()
- << this->point_3.y() << this->point_4.y();
- }
- void QCustomQuadrangle::showTextUnderPoint(QPointF p, int n)
- {
- pointText = new QCPItemText( this->plot );
- textList.append(pointText);
- this->plot->addItem( textList.at(textList.count() - 1 ) );
- pointText->setColor( this->pen.color() );
- pointText->position->setCoords( p.x(),
- p.y() - 0.5 );
- switch (n) {
- case 1:
- side = "A";
- break;
- case 2:
- side = "B";
- break;
- case 3:
- side = "C";
- break;
- case 4:
- side = "D";
- break;
- default:
- break;
- }
- pointText->setText( side + " [" + QString::number( p.x() )
- + "; " + QString::number( p.y() ) + "]" );
- }
- void QCustomQuadrangle::setTextUnderPointVisible()
- {
- if (this->pointsTextVisible) {
- // стиль точек.
- curve->setScatterStyle(
- QCPScatterStyle(QCPScatterStyle::ssCircle,
- QPen( this->pen ),
- QBrush( this->pen.color() ), 5 ) );
- } else {
- curve->setScatterStyle(
- QCPScatterStyle(QCPScatterStyle::ssNone));
- }
- }
- void QCustomQuadrangle::setCoordsUnderPointsVisible()
- {
- if (this->pointsCoordsVisible) {
- // показываем координаты точек.
- this->showTextUnderPoint( point_1, 1 );
- this->showTextUnderPoint( point_2, 2 );
- this->showTextUnderPoint( point_3, 3 );
- this->showTextUnderPoint( point_4, 4 );
- }
- }
- QCustomQuadrangle::QCustomQuadrangle(QCustomPlot *parent)
- {
- this->plot = parent;
- }
- void QCustomQuadrangle::setPoints(QPointF __p1, QPointF __p2, QPointF __p3, QPointF __p4)
- {
- this->point_1 = __p1;
- this->point_2 = __p2;
- this->point_3 = __p3;
- this->point_4 = __p4;
- }
- //----------------------
- // show
- void QCustomQuadrangle::show()
- {
- this->fillData();
- curve = new QCPCurve(this->plot->xAxis,
- this->plot->yAxis);
- curve->setData(this->x, this->y);
- curve->setPen(this->pen);
- curve->setName(this->name);
- this->selectedPen = curve->pen();
- this->selectedPen.setWidth(3);
- curve->setSelectedPen(this->selectedPen);
- this->setCoordsUnderPointsVisible();
- this->setTextUnderPointVisible();
- //
- this->plot->addPlottable(curve);
- }
- //-------------------------
- void QCustomQuadrangle::setPen(QPen __pen)
- {
- this->pen = __pen;
- }
- void QCustomQuadrangle::setName(QString __name)
- {
- this->name = __name;
- }
- void QCustomQuadrangle::setData(QVector<double> __x, QVector<double> __y)
- {
- this->x = __x;
- this->y = __y;
- this->point_1.setX(this->x.at(0));
- this->point_2.setX(this->x.at(1));
- this->point_3.setX(this->x.at(2));
- this->point_4.setX(this->x.at(3));
- this->point_1.setY(this->y.at(0));
- this->point_2.setY(this->y.at(1));
- this->point_3.setY(this->y.at(2));
- this->point_4.setY(this->y.at(3));
- }
- void QCustomQuadrangle::setPointsVisible(bool v)
- {
- this->pointsTextVisible = v;
- }
- void QCustomQuadrangle::setCoordsVisible(bool v)
- {
- this->pointsCoordsVisible = v;
- }
- QPen QCustomQuadrangle::getPen()
- {
- return this->pen;
- }
- QString QCustomQuadrangle::getName()
- {
- return this->name;
- }
- double QCustomQuadrangle::getLineLength(QPointF point_start, QPointF point_end)
- {
- return ( qSqrt( qPow( ( point_end.x() - point_start.x() ), 2 )
- + qPow( ( point_end.y() - point_start.y() ), 2) ) );
- }
- double QCustomQuadrangle::getPerimeter()
- {
- return ( getLineLength( this->point_1, this->point_2 )
- + getLineLength( this->point_2, this->point_3 )
- + getLineLength( this->point_3, this->point_4 )
- + getLineLength( this->point_4, this->point_1 ));
- }
- double QCustomQuadrangle::getSquare()
- {
- double S, s1, s2, s3, s4;
- double p = this->getPerimeter() / 2;
- s1 = p - getLineLength( this->point_1, this->point_2 );
- s2 = p - getLineLength( this->point_2, this->point_3 );
- s3 = p - getLineLength( this->point_3, this->point_4 );
- s4 = p - getLineLength( this->point_4, this->point_1 );
- S = qSqrt( s1 * s2 * s3 * s4 );
- return ( S );
- }
- double QCustomQuadrangle::getAB()
- {
- return getLineLength( this->point_1, this->point_2 );
- }
- double QCustomQuadrangle::getBC()
- {
- return getLineLength( this->point_2, this->point_3 );
- }
- double QCustomQuadrangle::getCD()
- {
- return getLineLength( this->point_3, this->point_4 );
- }
- double QCustomQuadrangle::getDA()
- {
- return getLineLength( this->point_4, this->point_1 );
- }
- double QCustomQuadrangle::getDiagonal_AC()
- {
- return getLineLength(this->point_1,
- this->point_3);
- }
- double QCustomQuadrangle::getDiagonal_BD()
- {
- return getLineLength(this->point_2,
- this->point_4);
- }
- QVector<double> QCustomQuadrangle::getXPoints()
- {
- return this->x;
- }
- QVector<double> QCustomQuadrangle::getYPoints()
- {
- return this->y;
- }
- QPointF QCustomQuadrangle::getPoint_1()
- {
- return this->point_1;
- }
- QPointF QCustomQuadrangle::getPoint_2()
- {
- return this->point_2;
- }
- QPointF QCustomQuadrangle::getPoint_3()
- {
- return this->point_3;
- }
- QPointF QCustomQuadrangle::getPoint_4()
- {
- return this->point_4;
- }
- void QCustomQuadrangle::removePointsText()
- {
- if (this->textList.size() != 0) {
- this->plot->removeItem(this->textList.at(0));
- this->plot->removeItem(this->textList.at(1));
- this->plot->removeItem(this->textList.at(2));
- this->plot->removeItem(this->textList.at(3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement