Mutualize code

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5427 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2018-07-03 14:25:46 +00:00
parent ed7090f6fb
commit 57e5ccd943
4 changed files with 73 additions and 94 deletions

View File

@ -186,3 +186,58 @@ QLineF QetGraphicsHandlerUtility::lineForPosAtIndex(const QLineF &old_line, cons
index == 0 ? line.setP1(pos) : line.setP2(pos);
return line;
}
/**
* @brief QetGraphicsHandlerUtility::polygonForInsertPoint
* @param old_polygon : the polygon which we insert a new point.
* @param closed : polygon is closed or not
* @param pos : the pos where the new point must be added
* @return the new polygon
*/
QPolygonF QetGraphicsHandlerUtility::polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos)
{
qreal max_angle = 0;
int index = 0;
for (int i=1 ; i<old_polygon.size() ; i++)
{
QPointF A = old_polygon.at(i-1);
QPointF B = old_polygon.at(i);
QLineF line_a(A, pos);
QLineF line_b(pos, B);
qreal angle = line_a.angleTo(line_b);
if(angle<180)
angle = 360-angle;
if (i==1)
{
max_angle = angle;
index=i;
}
if (angle > max_angle)
{
max_angle = angle;
index=i;
}
}
//Special case when polygon is close
if (closed)
{
QLineF line_a(old_polygon.last(), pos);
QLineF line_b(pos, old_polygon.first());
qreal angle = line_a.angleTo(line_b);
if (angle<180)
angle = 360-angle;
if (angle > max_angle)
{
max_angle = angle;
index=old_polygon.size();
}
}
QPolygonF polygon = old_polygon;
polygon.insert(index, pos);
return polygon;
}

View File

@ -21,6 +21,7 @@
#include <QRectF>
#include <QLineF>
#include <QColor>
#include <QPolygonF>
class QPainter;
@ -39,6 +40,7 @@ class QetGraphicsHandlerUtility
static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index);
static QRectF mirrorRectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index);
static QLineF lineForPosAtIndex (const QLineF &old_line, const QPointF &pos, int index);
static QPolygonF polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos);
};
#endif // QETGRAPHICSHANDLERUTILITY_H

View File

@ -21,6 +21,7 @@
#include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
#include "qetelementeditor.h"
#include "qeticons.h"
#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
/**
@ -483,54 +484,15 @@ void PartPolygon::removeHandler()
*/
void PartPolygon::insertPoint()
{
qreal max_angle = 0;
int index = 0;
QPolygonF new_polygon = QetGraphicsHandlerUtility::polygonForInsertPoint(m_polygon, m_closed, elementScene()->snapToGrid(m_context_menu_pos));
for (int i=1 ; i<m_polygon.size() ; i++)
if(new_polygon != m_polygon)
{
QPointF A = m_polygon.at(i-1);
QPointF B = m_polygon.at(i);
QLineF line_a(A, m_context_menu_pos);
QLineF line_b(m_context_menu_pos, B);
qreal angle = line_a.angleTo(line_b);
if(angle<180)
angle = 360-angle;
if (i==1)
{
max_angle = angle;
index=i;
}
if (angle > max_angle)
{
max_angle = angle;
index=i;
}
}
//Special case when polygon is close
if (m_closed)
{
QLineF line_a(m_polygon.last(), m_context_menu_pos);
QLineF line_b(m_context_menu_pos, m_polygon.first());
qreal angle = line_a.angleTo(line_b);
if (angle<180)
angle = 360-angle;
if (angle > max_angle)
{
max_angle = angle;
index=m_polygon.size();
}
}
QPolygonF polygon = this->polygon();
polygon.insert(index, elementScene()->snapToGrid(m_context_menu_pos));
//Wrap the undo for avoid to merge the undo commands when user add several points.
QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo);
elementScene()->undoStack().push(undo);
}
}
/**

View File

@ -291,7 +291,7 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
{
painter->save();
QColor color(Qt::darkBlue);
color.setAlpha(25);
color.setAlpha(50);
painter -> setBrush (QBrush (color));
painter -> setPen (Qt::NoPen);
painter -> drawPath (shape());
@ -561,58 +561,18 @@ void QetShapeItem::adjusteHandlerPos()
void QetShapeItem::insertPoint()
{
if (m_shapeType != QetShapeItem::Polygon) {
return;
}
qreal max_angle = 0;
int index = 0;
for (int i=1 ; i<m_polygon.size() ; i++)
if (m_shapeType == QetShapeItem::Polygon)
{
QPointF A = m_polygon.at(i-1);
QPointF B = m_polygon.at(i);
QLineF line_a(A, m_context_menu_pos);
QLineF line_b(m_context_menu_pos, B);
qreal angle = line_a.angleTo(line_b);
if(angle<180)
angle = 360-angle;
QPolygonF new_polygon = QetGraphicsHandlerUtility::polygonForInsertPoint(this->polygon(), m_closed, Diagram::snapToGrid(m_context_menu_pos));
if (i==1)
if(new_polygon != m_polygon)
{
max_angle = angle;
index=i;
}
if (angle > max_angle)
{
max_angle = angle;
index=i;
}
}
//Special case when polygon is close
if (m_closed)
{
QLineF line_a(m_polygon.last(), m_context_menu_pos);
QLineF line_b(m_context_menu_pos, m_polygon.first());
qreal angle = line_a.angleTo(line_b);
if (angle<180)
angle = 360-angle;
if (angle > max_angle)
{
max_angle = angle;
index=m_polygon.size();
}
}
QPolygonF polygon = this->polygon();
polygon.insert(index, Diagram::snapToGrid(m_context_menu_pos));
//Wrap the undo for avoid to merge the undo commands when user add several points.
QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
new QPropertyUndoCommand(this, "polygon", m_polygon, new_polygon, undo);
diagram()->undoStack().push(undo);
}
}
}
void QetShapeItem::removePoint()