#include "mechanism_1.h"#include "parts.h" #include <QtMath>#include <QTimer>#include <QDebug> Mechanism_1::Mechanism_1(QFont _font, double l1, double l2, double _xa, double _ya, double _xb, double _yb, double _xl, double _yl, double _xc, double _yc, double _xc2, double _yc2, double fi1, double fi2, double al, double _xmin, double _ymin, double _xmax, double _ymax, double _xo1, double _yo1, QWidget *parent) : QGraphicsView(parent){ scene = new Scene(); this->setScene(scene); this->resize(600, 100); this->setMinimumSize(600, 100); this->setRenderHint(QPainter::Antialiasing); // Устанавливаем сглаживание this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Отключаем скроллбар по вертикали this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Отключаем скроллбар по горизонтали //rscene->setSceneRect(0, 0, 100, 100); // Устанавливаем размеры графической сцены //QRect rect(150, 150, 300, 300); this->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio); setAlignment(Qt::AlignCenter); // QRect mainrect(0, 0, 500, 500);// //scene->addRect(rect);// scene->addRect(mainrect); //scaling_window(_xmin, _ymin, _xmax, _ymax); fi1g = -fi1; fi2g = -fi2; alg = -al; xo1 = _xo1; yo1 = -_yo1; xa = _xa; ya = -_ya; xb = _xb; yb = -_yb; xl = _xl; yl = -_yl; xc = _xc; yc = -_yc; xmin = _xmin; ymin = _ymin; xmax = _xmax; ymax = _ymax; scale = 1; Ox = new Arrow(30); scene->addItem(Ox); textX = new Text(_font, QString("x")); textX->setPos(xo1 + Ox->getl() - 3 * 0.5 * scale * textX->getHorizontalAdvance(), yo1 + 2 * scale * textX->getHeight()); textX->setScale(3*scale); scene->addItem(textX); Oy = new Arrow(30); Oy->setRotation(-90); scene->addItem(Oy); textY = new Text(_font, QString("y")); textY->setPos(xo1 - 6 * scale * textY->getHorizontalAdvance(), yo1 - Oy->getl() + 3 * 0.5 * scale * textY->getHeight()); textY->setScale(3*scale); scene->addItem(textY); O1 = new Hinge(); O1->setPos(xo1, yo1); O1->setScale(scale); scene->addItem(O1); O1A = new Crank(l1); O1A->setPos(xo1, yo1); O1A->setRotation(fi1g); scene->addItem(O1A); A = new FixedHinge(); A->setPos(xa, ya); A->setScale(scale); scene->addItem(A); B = new Slider(); B->setPos(xb, yb); B->setRotation(alg); B->setScale(scale); scene->addItem(B); L = new FixedHinge(); L->setPos(xl, yl); L->setScale(scale); scene->addItem(L); l = new DoshLine(300); l->setPos(xl, yl); l->setRotation(alg); l->setScale(scale); scene->addItem(l); ABC = new Polygon(_xc2, _yc2, l2); ABC->setPos(xa, ya); ABC->setRotation(fi2g); scene->addItem(ABC); AB = new Crank(l2); AB->setPos(xa, ya); AB->setRotation(fi2g); scene->addItem(AB); AB->hide(); textO1 = new Text(_font, QString("O1")); textO1->setPos(xo1 - 3 * scale * textO1->getHorizontalAdvance(), yo1 + 3 * scale * 0.5 * textO1->getHeight()); textO1->setScale(3*scale); scene->addItem(textO1); textA = new Text(_font, QString("A")); textA->setPos(xa - 3 * scale * textA->getHorizontalAdvance(), ya); textA->setScale(3*scale); scene->addItem(textA); textB = new Text(_font, QString("B")); textB->setPos(xb + 3 * scale * textB->getHorizontalAdvance(), yb); textB->setScale(3*scale); scene->addItem(textB); textC = new Text(_font, QString("C")); textC->setPos(xc - 3 * scale * textC->getHorizontalAdvance(), yc); textC->setScale(3*scale); scene->addItem(textC); textL = new Text(_font, QString("L")); textL->setPos(xl + 3 * scale * textL->getHorizontalAdvance(), yl + scale * textL->getHeight()); textL->setScale(3*scale); scene->addItem(textL); // scene = new Scene();// this->setScene(scene);// //this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); // if (qAbs(_xmin - _xmax) != 0 and qAbs(_ymin - _ymax) != 0) {// this->resize(500,500);//// this->setMinimumSize(qAbs(_xmin - _xmax), qAbs(_ymin - _ymax));//// scene->setSceneRect(_xmin, _ymin, qAbs(_xmin - _xmax), - qAbs(_ymin - _ymax));// scaling_window(_xmin, _ymin, _xmax, _ymax, width(), height());//// scene->setSceneRect(_xmin * (1/index), _ymax * (1/index),//// qAbs(_xmin - _xmax) * (1/index), qAbs(_ymin - _ymax) * (1/index));// scene->setSceneRect(0, 0, width(), -height());// } else {//// this->resize(2*(l1 + l2), 2*(l1 + l2));//// this->setMinimumSize(2*(l1 + l2), 2*(l1 + l2));//// scene->setSceneRect(-(l1+l2), -(l1+l2), 2*(l1 + l2), 2*(l1 + l2));// scene->setSceneRect(-(l1+l2), -(l1+l2), width(), - height());// } // QRect rect(0, 0, width(), -height());// scene->addRect(rect); // this->setRenderHint(QPainter::Antialiasing); // Устанавливаем сглаживание// this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Отключаем скроллбар по вертикали// this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Отключаем скроллбар по горизонтали // fi1g = -fi1;// fi2g = fi2;// alg = al; // xo1 = _xo1 * (1/index);// yo1 = -_yo1 * (1/index);// xa = _xa * (1/index);// ya = -_ya * (1/index);// xb = _xb * (1/index);// yb = -_yb; // xmin = _xmin * (1/index);// ymin = _ymin * (1/index);// xmax = _xmax * (1/index);// ymax = _ymax * (1/index); // O1 = new Hinge();// O1->setPos(xo1, yo1);// scene->addItem(O1); // O1A = new Crank(l1*(1/index));// O1A->setPos(xo1, yo1);// O1A->setRotation(fi1g);// scene->addItem(O1A); // A = new FixedHinge();// A->setPos(xa, ya);// scene->addItem(A); // AB = new Crank(l2*(1/index));// AB->setPos(xa, ya);// AB->setRotation(fi2g);// scene->addItem(AB); // B = new Slider();// B->setPos(xb, yb);// B->setRotation(al); // scene->addItem(B);} void Mechanism_1::scaling_window(double xmin, double ymin, double xmax, double ymax){ double relation_x = qAbs(xmin - xmax) / width(); double relation_y = qAbs(ymin - ymax) / height(); qDebug() << "relation_x = " << relation_x << ", relation_y = " << relation_y << Qt::endl; qDebug() << "dx = " << qAbs(xmin - xmax) << ", dy = " << qAbs(ymin - ymax) << Qt::endl; qDebug() << "xmin = " << xmin << ", ymin = " << ymin << ", xmax = " << xmax << ", ymax = " << ymax << Qt::endl; qDebug() << "height = " << height() << ", width = " << width() << Qt::endl; if (relation_x > relation_y) index = relation_y; else index = relation_x;}
#include "parts.h" #include <QFont>#include <QDebug>#include <QtMath> //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Hinge~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Hinge::Hinge() : QGraphicsObject() {} QRectF Hinge::boundingRect() const{ return QRectF(-100, -100, 200, 200);} void Hinge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->rotate(60.0); painter->drawLine(0, 0, 60, 0); painter->translate(60, 0); painter->rotate(120.0); painter->drawLine(0, 0, 60, 0); painter->drawLine(-10, 0, 70, 0); painter->setPen(Qt::NoPen); painter->setBrush(QBrush(Qt::black, Qt::BDiagPattern)); painter->drawRect(-10, 0, 80, -25); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->translate(60, 0); painter->rotate(120.0); painter->drawLine(0, 0, 60, 0); painter->translate(60, 0); painter->rotate(60.0); painter->drawEllipse(-40, -40, 80, 80); painter->drawEllipse(-25, -25, 50, 50);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~FixedHinge~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//FixedHinge::FixedHinge(){ } QRectF FixedHinge::boundingRect() const{ return QRectF(-250, -250, 500, 500);} void FixedHinge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawEllipse(-25, -25, 50, 50);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Slider~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Slider::Slider() { } QRectF Slider::boundingRect() const{ return QRectF(-250, -250, 500, 500);} void Slider::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawRect(-70, -50, 140, 100); painter->drawEllipse(-25, -25, 50, 50);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Crank~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Crank::Crank(double L) : QGraphicsObject(){ l = L;} QRectF Crank::boundingRect() const{ return QRectF(-250, -250, 500, 500);} void Crank::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(0, 0, l, 0);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Пунктирная прямая~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//DoshLine::DoshLine(double L){ l = L; rect = QRectF(-l*0.5, -l*0.5, l, l);} QRectF DoshLine::boundingRect() const{ return rect;} void DoshLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(Qt::black, 5, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(0, 0, l, 0);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Стрелка~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Arrow::Arrow(double L, double Al){ l = L; al = Al; rect = QRectF(-l*0.5, -l*0.5, l, l);} QRectF Arrow::boundingRect() const{ return rect;} void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(Qt::NoBrush); painter->setPen(QPen(color, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(0, 0, l, 0); painter->translate(l, 0); painter->rotate(180 - al); painter->drawLine(0, 0, l* 0.2, 0); painter->rotate(2*al); painter->drawLine(0, 0, l* 0.2, 0); painter->rotate(180 - al); painter->translate(-l, 0);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Полигон треугольник~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Polygon::Polygon(double _xc2, double _yc2, double _l2){ xc2 = _xc2; yc2 = -_yc2; l = _l2; poly.append(QPoint(0, 0)); poly.append(QPoint(l, 0)); poly.append(QPoint(xc2, yc2)); if (xc2 * l >= 0) (qAbs(xc2) >= qAbs(l)) ? rect = QRectF(0, 0, xc2, yc2) : rect = QRectF(0, 0, l, yc2); else (xc2 >= 0) ? rect = QRectF(xc2, 0, -(xc2 + qAbs(l)), yc2) : rect = QRectF(xc2, 0, qAbs(xc2) + l, yc2);} QRectF Polygon::boundingRect() const{ return rect;} void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(QBrush(Qt::blue, Qt::BDiagPattern)); painter->setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); //qDebug() << "Polygon: " << poly << Qt::endl; painter->drawPolygon(poly);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Текст~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Text::Text(QFont Font, QString Str){ font = Font; font.setBold(true); fm = new QFontMetrics(font); str = Str;} QRectF Text::boundingRect() const{ return QRectF(0, 0, fm->horizontalAdvance(str), -fm->height());} void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ Q_UNUSED(option); Q_UNUSED(widget); painter->setPen(QPen(Qt::black, fm->height(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->setFont(font); painter->drawText(0, 0, tr("%1").arg(str));} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Сцена~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//Scene::Scene(QObject *parent) : QGraphicsScene(parent) {}Scene::~Scene() {}