Refactoring.

--HG--
branch : develop
This commit is contained in:
dismine 2014-07-24 14:31:02 +03:00
parent 33ad980c91
commit 0dbd72be67
7 changed files with 83 additions and 80 deletions

View File

@ -111,6 +111,55 @@ qreal VisLine::FindLength(const QString &expression)
return qApp->toPixel(length); return qApp->toPixel(length);
} }
//---------------------------------------------------------------------------------------------------------------------
void VisLine::DrawLine(QGraphicsLineItem *lineItem, const QLineF &line, const QColor &color, Qt::PenStyle style)
{
SCASSERT (lineItem != nullptr);
lineItem->setPen(QPen(color, qApp->toPixel(qApp->widthHairLine())/factor, style));
lineItem->setLine(line);
}
//---------------------------------------------------------------------------------------------------------------------
void VisLine::DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color)
{
SCASSERT (point != nullptr);
point->setPos(pos);
point->setPen(QPen(color, qApp->toPixel(qApp->widthMainLine())/factor));
}
//---------------------------------------------------------------------------------------------------------------------
QLineF VisLine::Line(const QPointF &p1, const qreal &length, const qreal &angle)
{
QLineF line = QLineF();
line.setP1(p1);
line.setLength(length);
line.setAngle(angle);
return line;
}
//---------------------------------------------------------------------------------------------------------------------
QGraphicsEllipseItem *VisLine::InitPoint(const QColor &color)
{
QGraphicsEllipseItem *point = new QGraphicsEllipseItem(this);
point->setZValue(1);
point->setBrush(QBrush(Qt::NoBrush));
point->setPen(QPen(color, qApp->toPixel(qApp->widthMainLine())/factor));
point->setRect(PointRect());
return point;
}
//---------------------------------------------------------------------------------------------------------------------
QGraphicsLineItem *VisLine::InitLine(const QColor &color)
{
QGraphicsLineItem *line = new QGraphicsLineItem(this);
line->setPen(QPen(color, qApp->toPixel(qApp->widthHairLine())/factor));
line->setZValue(1);
line->setFlags(QGraphicsItem::ItemStacksBehindParent);
return line;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VisLine::setMainColor(const QColor &value) void VisLine::setMainColor(const QColor &value)
{ {

View File

@ -64,6 +64,13 @@ protected:
QString toolTip; QString toolTip;
QRectF PointRect(); QRectF PointRect();
qreal FindLength(const QString &expression); qreal FindLength(const QString &expression);
void DrawLine(QGraphicsLineItem *lineItem, const QLineF &line, const QColor &color,
Qt::PenStyle style = Qt::SolidLine);
void DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color);
QLineF Line(const QPointF &p1, const qreal& length, const qreal &angle);
QGraphicsEllipseItem *InitPoint(const QColor &color);
QGraphicsLineItem *InitLine(const QColor &color);
private: private:
Q_DISABLE_COPY(VisLine) Q_DISABLE_COPY(VisLine)
}; };

View File

@ -40,13 +40,7 @@ VisToolAlongLine::VisToolAlongLine(const VContainer *data, QGraphicsItem *parent
lineP1 = InitPoint(supportColor); lineP1 = InitPoint(supportColor);
lineP2 = InitPoint(supportColor); lineP2 = InitPoint(supportColor);
line = InitLine(supportColor);
line = new QGraphicsLineItem(this);
line->setVisible(false);
line->setPen(QPen(supportColor, qApp->toPixel(qApp->widthHairLine())/factor));
line->setZValue(1);
line->setFlags(QGraphicsItem::ItemStacksBehindParent);
point = InitPoint(mainColor); point = InitPoint(mainColor);
} }
@ -66,39 +60,6 @@ void VisToolAlongLine::setLength(const QString &expression)
length = FindLength(expression); length = FindLength(expression);
} }
//---------------------------------------------------------------------------------------------------------------------
QGraphicsEllipseItem *VisToolAlongLine::InitPoint(const QColor &color)
{
QGraphicsEllipseItem *point = new QGraphicsEllipseItem(this);
point->setVisible(false);
point->setZValue(1);
point->setBrush(QBrush(Qt::NoBrush));
point->setPen(QPen(color, qApp->toPixel(qApp->widthMainLine())/factor));
point->setRect(PointRect());
return point;
}
//---------------------------------------------------------------------------------------------------------------------
void VisToolAlongLine::DrawLine(QGraphicsLineItem *lineItem, const QLineF &line, const QColor &color,
Qt::PenStyle style)
{
SCASSERT (lineItem != nullptr);
lineItem->setPen(QPen(color, qApp->toPixel(qApp->widthHairLine())/factor, style));
lineItem->setLine(line);
lineItem->setVisible(true);
}
//---------------------------------------------------------------------------------------------------------------------
void VisToolAlongLine::DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color)
{
SCASSERT (point != nullptr);
point->setPos(pos);
point->setVisible(true);
point->setPen(QPen(color, qApp->toPixel(qApp->widthMainLine())/factor));
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VisToolAlongLine::RefreshGeometry() void VisToolAlongLine::RefreshGeometry()
{ {
@ -121,11 +82,7 @@ void VisToolAlongLine::RefreshGeometry()
if (qFuzzyCompare(1 + length, 1 + 0) == false) if (qFuzzyCompare(1 + length, 1 + 0) == false)
{ {
QLineF mainLine = QLineF(); QLineF mainLine = Line(first->toQPointF(), length, line->line().angle());
mainLine.setP1(first->toQPointF());
mainLine.setAngle(line->line().angle());
mainLine.setLength(length);
DrawLine(this, mainLine, mainColor, lineStyle); DrawLine(this, mainLine, mainColor, lineStyle);
DrawPoint(point, mainLine.p2(), mainColor); DrawPoint(point, mainLine.p2(), mainColor);

View File

@ -51,10 +51,6 @@ private:
QGraphicsEllipseItem *lineP2; QGraphicsEllipseItem *lineP2;
QGraphicsLineItem *line; QGraphicsLineItem *line;
qreal length; qreal length;
QGraphicsEllipseItem *InitPoint(const QColor &mainColor);
void DrawLine(QGraphicsLineItem *lineItem, const QLineF &line, const QColor &color,
Qt::PenStyle style = Qt::SolidLine);
void DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color);
}; };
#endif // VISTOOLALONGLINE_H #endif // VISTOOLALONGLINE_H

View File

@ -41,12 +41,7 @@ VisToolEndLine::VisToolEndLine(const VContainer *data, QGraphicsItem *parent)
{ {
this->mainColor = Qt::red; this->mainColor = Qt::red;
point = new QGraphicsEllipseItem(this); point = InitPoint(mainColor);
point->setVisible(false);
point->setZValue(1);
point->setBrush(QBrush(Qt::NoBrush));
point->setPen(QPen(mainColor, qApp->toPixel(qApp->widthHairLine())/factor));
point->setRect(PointRect());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -57,42 +52,26 @@ VisToolEndLine::~VisToolEndLine()
void VisToolEndLine::RefreshGeometry() void VisToolEndLine::RefreshGeometry()
{ {
const VPointF *first = data->GeometricObject<const VPointF *>(point1Id); const VPointF *first = data->GeometricObject<const VPointF *>(point1Id);
this->setPen(QPen(mainColor, qApp->toPixel(qApp->widthHairLine())/factor, lineStyle));
QLineF line; QLineF line;
if (qFuzzyCompare(1 + length, 1 + 0)) if (qFuzzyCompare(1 + length, 1 + 0))
{ {
QGraphicsScene *sc = this->scene();
QPointF second; QPointF second;
if(sc == nullptr) if(this->scene() == nullptr)
{ {
second = scenePos; second = scenePos;
} }
else else
{ {
line = QLineF(first->toQPointF(), scenePos); second = CorrectRay(first->toQPointF());
QRectF scRect = sc->sceneRect();
qreal diagonal = sqrt(pow(scRect.height(), 2) + pow(scRect.width(), 2));
line.setLength(diagonal);
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
line.setAngle(CorrectAngle(line.angle()));
}
second = VAbstractTool::LineIntersectRect(scRect, line);
} }
line = QLineF(first->toQPointF(), second); line = QLineF(first->toQPointF(), second);
} }
else else
{ {
line = QLineF(); line = Line(first->toQPointF(), length, angle);
line.setP1(first->toQPointF()); DrawPoint(point, line.p2(), mainColor);
line.setLength(length);
line.setAngle(angle);
point->setPos(line.p2());
point->setVisible(true);
point->setPen(QPen(mainColor, qApp->toPixel(qApp->widthHairLine())/factor));
} }
this->setLine(line); DrawLine(this, line, mainColor, lineStyle);
toolTip = QString(tr("<b>Point at distance and angle</b>: angle = %1°; <b>Shift</b> - sticking angle, " toolTip = QString(tr("<b>Point at distance and angle</b>: angle = %1°; <b>Shift</b> - sticking angle, "
"<b>Enter</b> - finish creation")).arg(this->line().angle()); "<b>Enter</b> - finish creation")).arg(this->line().angle());
} }
@ -145,6 +124,19 @@ qreal VisToolEndLine::CorrectAngle(const qreal &angle) const
} }
} }
QPointF VisToolEndLine::CorrectRay(const QPointF &firstPoint) const
{
QLineF line = QLineF(firstPoint, scenePos);
QRectF scRect = this->scene()->sceneRect();
qreal diagonal = sqrt(pow(scRect.height(), 2) + pow(scRect.width(), 2));
line.setLength(diagonal);
if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
{
line.setAngle(CorrectAngle(line.angle()));
}
return VAbstractTool::LineIntersectRect(scRect, line);
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VisToolEndLine::setAngle(const qreal &value) void VisToolEndLine::setAngle(const qreal &value)
{ {

View File

@ -47,10 +47,11 @@ public:
void setLength(const QString &expression); void setLength(const QString &expression);
private: private:
Q_DISABLE_COPY(VisToolEndLine) Q_DISABLE_COPY(VisToolEndLine)
qreal CorrectAngle(const qreal &angle) const;
qreal length; qreal length;
qreal angle; qreal angle;
QGraphicsEllipseItem *point; QGraphicsEllipseItem *point;
qreal CorrectAngle(const qreal &angle) const;
QPointF CorrectRay(const QPointF &firstPoint) const;
}; };
#endif // VISTOOLENDLINE_H #endif // VISTOOLENDLINE_H

View File

@ -46,17 +46,18 @@ VisToolLine::~VisToolLine()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VisToolLine::RefreshGeometry() void VisToolLine::RefreshGeometry()
{ {
QLineF line;
const VPointF *first = data->GeometricObject<const VPointF *>(point1Id); const VPointF *first = data->GeometricObject<const VPointF *>(point1Id);
if (point2Id == 0) if (point2Id == 0)
{ {
this->setLine(QLineF(first->toQPointF(), scenePos)); line = QLineF(first->toQPointF(), scenePos);
} }
else else
{ {
const VPointF *second = data->GeometricObject<const VPointF *>(point2Id); const VPointF *second = data->GeometricObject<const VPointF *>(point2Id);
this->setLine(QLineF(first->toQPointF(), second->toQPointF())); line = QLineF(first->toQPointF(), second->toQPointF());
} }
this->setPen(QPen(mainColor, qApp->toPixel(qApp->widthHairLine())/factor, lineStyle)); DrawLine(this, line, mainColor, lineStyle);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------