2013-07-25 20:39:51 +02:00
|
|
|
|
#include "vabstracttool.h"
|
2013-07-29 14:55:40 +02:00
|
|
|
|
#include <QDebug>
|
2013-07-25 20:39:51 +02:00
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
VAbstractTool::VAbstractTool(VDomDocument *doc, VContainer *data, qint64 id, QObject *parent):
|
|
|
|
|
VDataTool(data, parent), doc(doc), id(id), baseColor(Qt::black), currentColor(Qt::black){
|
2013-07-25 20:39:51 +02:00
|
|
|
|
this->doc = doc;
|
|
|
|
|
this->id = id;
|
|
|
|
|
|
2013-08-13 18:48:36 +02:00
|
|
|
|
connect(this, &VAbstractTool::toolhaveChange, this->doc, &VDomDocument::haveLiteChange);
|
2013-07-25 20:39:51 +02:00
|
|
|
|
connect(this->doc, &VDomDocument::FullUpdateFromFile, this, &VAbstractTool::FullUpdateFromFile);
|
2013-07-29 14:55:40 +02:00
|
|
|
|
connect(this, &VAbstractTool::FullUpdateTree, this->doc, &VDomDocument::FullUpdateTree);
|
2013-08-15 22:39:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 20:39:51 +02:00
|
|
|
|
void VAbstractTool::AddAttribute(QDomElement &domElement, const QString &name, const qint64 &value){
|
|
|
|
|
QDomAttr domAttr = doc->createAttribute(name);
|
|
|
|
|
domAttr.setValue(QString().setNum(value));
|
|
|
|
|
domElement.setAttributeNode(domAttr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VAbstractTool::AddAttribute(QDomElement &domElement, const QString &name, const qint32 &value){
|
|
|
|
|
QDomAttr domAttr = doc->createAttribute(name);
|
|
|
|
|
domAttr.setValue(QString().setNum(value));
|
|
|
|
|
domElement.setAttributeNode(domAttr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VAbstractTool::AddAttribute(QDomElement &domElement, const QString &name, const qreal &value){
|
|
|
|
|
QDomAttr domAttr = doc->createAttribute(name);
|
|
|
|
|
domAttr.setValue(QString().setNum(value));
|
|
|
|
|
domElement.setAttributeNode(domAttr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VAbstractTool::AddAttribute(QDomElement &domElement, const QString &name, const QString &value){
|
|
|
|
|
QDomAttr domAttr = doc->createAttribute(name);
|
|
|
|
|
domAttr.setValue(value);
|
|
|
|
|
domElement.setAttributeNode(domAttr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VAbstractTool::~VAbstractTool(){
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
QPointF VAbstractTool::LineIntersectRect(QRectF rec, QLineF line){
|
|
|
|
|
qreal x1, y1, x2, y2;
|
|
|
|
|
rec.getCoords(&x1, &y1, &x2, &y2);
|
|
|
|
|
QPointF point;
|
|
|
|
|
QLineF::IntersectType type = line.intersect(QLineF(QPointF(x1,y1), QPointF(x1,y2)),&point);
|
|
|
|
|
if ( type == QLineF::BoundedIntersection ){
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
type = line.intersect(QLineF(QPointF(x1,y1), QPointF(x2,y1)),&point);
|
|
|
|
|
if ( type == QLineF::BoundedIntersection ){
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
type = line.intersect(QLineF(QPointF(x1,y2), QPointF(x2,y2)),&point);
|
|
|
|
|
if ( type == QLineF::BoundedIntersection ){
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
type = line.intersect(QLineF(QPointF(x2,y1), QPointF(x2,y2)),&point);
|
|
|
|
|
if ( type == QLineF::BoundedIntersection ){
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
Q_ASSERT_X(type != QLineF::BoundedIntersection, Q_FUNC_INFO, "Немає точки перетину.");
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint32 VAbstractTool::LineIntersectCircle(QPointF center, qreal radius, QLineF line, QPointF &p1,
|
|
|
|
|
QPointF &p2){
|
|
|
|
|
const qreal eps = 1e-8;
|
|
|
|
|
//коефіцієнти для рівняння відрізку
|
|
|
|
|
qreal a = line.p2().y() - line.p1().y();
|
|
|
|
|
qreal b = line.p1().x() - line.p2().x();
|
|
|
|
|
// В даному випадку не використовується.
|
|
|
|
|
//qreal c = - a * line.p1().x() - b * line.p1().y();
|
|
|
|
|
// проекция центра окружности на прямую
|
|
|
|
|
QPointF p = ClosestPoint (line, center);
|
|
|
|
|
// сколько всего решений?
|
|
|
|
|
qint32 flag = 0;
|
|
|
|
|
qreal d = QLineF (center, p).length();
|
|
|
|
|
if (qAbs (d - radius) <= eps){
|
|
|
|
|
flag = 1;
|
2013-08-28 10:55:11 +02:00
|
|
|
|
} else {
|
2013-09-10 14:29:06 +02:00
|
|
|
|
if (radius > d){
|
|
|
|
|
flag = 2;
|
2013-08-28 10:55:11 +02:00
|
|
|
|
} else {
|
2013-09-10 14:29:06 +02:00
|
|
|
|
return 0;
|
2013-08-15 22:39:00 +02:00
|
|
|
|
}
|
2013-07-29 14:55:40 +02:00
|
|
|
|
}
|
2013-09-10 14:29:06 +02:00
|
|
|
|
// находим расстояние от проекции до точек пересечения
|
|
|
|
|
qreal k = sqrt (radius * radius - d * d);
|
|
|
|
|
qreal t = QLineF (QPointF (0, 0), QPointF (b, - a)).length();
|
|
|
|
|
// добавляем к проекции векторы направленные к точкам пеерсечения
|
|
|
|
|
p1 = addVector (p, QPointF (0, 0), QPointF (- b, a), k / t);
|
|
|
|
|
p2 = addVector (p, QPointF (0, 0), QPointF (b, - a), k / t);
|
|
|
|
|
return flag;
|
2013-07-29 14:55:40 +02:00
|
|
|
|
}
|
2013-08-13 18:48:36 +02:00
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
QPointF VAbstractTool::ClosestPoint(QLineF line, QPointF p){
|
|
|
|
|
QLineF lineP2pointFrom = QLineF(line.p2(), p);
|
|
|
|
|
qreal angle = 180-line.angleTo(lineP2pointFrom)-90;
|
|
|
|
|
QLineF pointFromlineP2 = QLineF(p, line.p2());
|
|
|
|
|
pointFromlineP2.setAngle(pointFromlineP2.angle()+angle);
|
|
|
|
|
QPointF point;
|
|
|
|
|
QLineF::IntersectType type = pointFromlineP2.intersect(line,&point);
|
|
|
|
|
if ( type == QLineF::BoundedIntersection ){
|
|
|
|
|
return point;
|
|
|
|
|
} else{
|
|
|
|
|
if ( type == QLineF::NoIntersection || type == QLineF::UnboundedIntersection ){
|
|
|
|
|
Q_ASSERT_X(type != QLineF::BoundedIntersection, Q_FUNC_INFO, "Немає точки перетину.");
|
|
|
|
|
return point;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return point;
|
2013-08-13 18:48:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
QPointF VAbstractTool::addVector(QPointF p, QPointF p1, QPointF p2, qreal k){
|
|
|
|
|
return QPointF (p.x() + (p2.x() - p1.x()) * k, p.y() + (p2.y() - p1.y()) * k);
|
2013-08-13 18:48:36 +02:00
|
|
|
|
}
|
2013-08-15 22:39:00 +02:00
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
const VContainer *VAbstractTool::getData()const{
|
|
|
|
|
return &data;
|
2013-08-15 22:39:00 +02:00
|
|
|
|
}
|
2013-08-28 10:55:11 +02:00
|
|
|
|
|
2013-09-10 14:29:06 +02:00
|
|
|
|
qint64 VAbstractTool::getId() const{
|
|
|
|
|
return id;
|
2013-08-28 10:55:11 +02:00
|
|
|
|
}
|