Qet shape item : rectangle and ellipse can be resized by the edges

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4055 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-07-19 15:52:44 +00:00
parent e953459e90
commit d55508df05
4 changed files with 82 additions and 52 deletions

View File

@@ -29,12 +29,12 @@ QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) :
{} {}
/** /**
* @brief QetGraphicsHandlerUtility::DrawHandler * @brief QetGraphicsHandlerUtility::drawHandler
* Draw the handler at pos @point, using the QPainter @painter. * Draw the handler at pos @point, using the QPainter @painter.
* @param painter : painter to use for drawing the handler * @param painter : painter to use for drawing the handler
* @param point : point to draw the handler * @param point : point to draw the handler
*/ */
void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &point, bool color2) void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2)
{ {
//Color of handler //Color of handler
QColor inner(0xFF, 0xFF, 0xFF); QColor inner(0xFF, 0xFF, 0xFF);
@@ -53,6 +53,18 @@ void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &po
painter->restore(); painter->restore();
} }
/**
* @brief QetGraphicsHandlerUtility::drawHandler
* Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2)
* @param painter
* @param points
* @param color2
*/
void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2) {
foreach(QPointF point, points)
drawHandler(painter, point, color2);
}
/** /**
* @brief QetGraphicsHandlerUtility::pointIsInHandler * @brief QetGraphicsHandlerUtility::pointIsInHandler
* @param point : point to compare * @param point : point to compare
@@ -109,15 +121,62 @@ QRectF QetGraphicsHandlerUtility::getRect(const QPointF &point) const
/** /**
* @brief QetGraphicsHandlerUtility::pointsForRect * @brief QetGraphicsHandlerUtility::pointsForRect
* Return the point of the rect in vector. * Return the keys points of the rectangle, stored in a vector.
* The point are stored like this : * The points in the vector are stored like this :
* top left, top right, bottom left, bottom right; * **********
* 0---1---2
* | |
* 3 4
* | |
* 5---6---7
* ************
* @param rect * @param rect
* @return * @return
*/ */
QVector<QPointF> QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect) QVector<QPointF> QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect)
{ {
QVector<QPointF> vector; QVector<QPointF> vector;
vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight(); QPointF point;
vector << rect.topLeft();//*****Top left
point = rect.center();
point.setY(rect.top());
vector << point;//**************Middle top
vector << rect.topRight();//****Top right
point = rect.center();
point.setX(rect.left());
vector << point;//**************Middle left
point.setX(rect.right());
vector << point;//**************Middle right
vector << rect.bottomLeft();//**Bottom left
point = rect.center();
point.setY(rect.bottom());
vector << point;//*************Middle bottom
vector << rect.bottomRight();//*Bottom right
return vector; return vector;
} }
/**
* @brief QetGraphicsHandlerUtility::rectForPosAtIndex
* Return a rectangle after modification of the point '@pos' at index '@index' of original rectangle '@old_rect'.
* @param old_rect - the rectangle befor modification
* @param pos - the new position of a key point
* @param index - the index of the key point to modifie see QetGraphicsHandlerUtility::pointsForRect to know
* the index of each keys points of a rectangle)
* @return : the rectangle with modification. If index is lower than 0 or higher than 7, this method return old_rect.
*/
QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
{
if (index < 0 || index > 7) return old_rect;
QRectF rect = old_rect;
if (index == 0) rect.setTopLeft(pos);
else if (index == 1) rect.setTop(pos.y());
else if (index == 2) rect.setTopRight(pos);
else if (index == 3) rect.setLeft(pos.x());
else if (index == 4) rect.setRight(pos.x());
else if (index == 5) rect.setBottomLeft(pos);
else if (index == 6) rect.setBottom(pos.y());
else if (index == 7) rect.setBottomRight(pos);
return rect;
}

View File

@@ -18,7 +18,6 @@
#ifndef QETGRAPHICSHANDLERUTILITY_H #ifndef QETGRAPHICSHANDLERUTILITY_H
#define QETGRAPHICSHANDLERUTILITY_H #define QETGRAPHICSHANDLERUTILITY_H
#include <QPointF>
#include <QRectF> #include <QRectF>
class QPainter; class QPainter;
@@ -27,13 +26,15 @@ class QPainter;
* @brief The QetGraphicsHandlerUtility class * @brief The QetGraphicsHandlerUtility class
* This class provide some methods to create and use handler for * This class provide some methods to create and use handler for
* modify graphics shape like line rectangle etc... * modify graphics shape like line rectangle etc...
* They also provide some conveniance static method.
*/ */
class QetGraphicsHandlerUtility class QetGraphicsHandlerUtility
{ {
public: public:
QetGraphicsHandlerUtility (qreal size = 1); QetGraphicsHandlerUtility (qreal size = 1);
void setSize(qreal size) {m_size = size;} void setSize(qreal size) {m_size = size;}
void DrawHandler (QPainter *painter, const QPointF & point, bool color2 = false); void drawHandler (QPainter *painter, const QPointF & point, bool color2 = false);
void drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2 = false);
QPointF posForHandler(const QPointF &point) const; QPointF posForHandler(const QPointF &point) const;
bool pointIsInHandler (const QPointF &point, const QPointF &key_point) const; bool pointIsInHandler (const QPointF &point, const QPointF &key_point) const;
int pointIsHoverHandler (const QPointF &point, const QVector<QPointF> &vector) const; int pointIsHoverHandler (const QPointF &point, const QVector<QPointF> &vector) const;
@@ -45,7 +46,8 @@ class QetGraphicsHandlerUtility
qreal m_zoom_factor; qreal m_zoom_factor;
public: public:
static QVector <QPointF> pointsForRect (const QRectF & rect); static QVector <QPointF> pointsForRect (const QRectF &rect);
static QRectF rectForPosAtIndex (const QRectF &old_rect, const QPointF &pos, int index);
}; };
#endif // QETGRAPHICSHANDLERUTILITY_H #endif // QETGRAPHICSHANDLERUTILITY_H

View File

@@ -97,9 +97,8 @@ void ElementPrimitiveDecorator::paint(QPainter *painter, const QStyleOptionGraph
pen.setCosmetic(true); pen.setCosmetic(true);
painter -> setPen(pen); painter -> setPen(pen);
painter -> drawRect(modified_bounding_rect_); painter -> drawRect(modified_bounding_rect_);
//Draw the handler //Draw the handlers
foreach (QPointF point, getResizingsPoints()) m_handler.drawHandler(painter, getResizingsPoints(), decorated_items_.size()-1);
m_handler.DrawHandler(painter, point, decorated_items_.size()-1);
// uncomment to draw the real bouding rect (=adjusted internal bounding rect) // uncomment to draw the real bouding rect (=adjusted internal bounding rect)
// painter -> setBrush(QBrush(QColor(240, 0, 0, 127))); // painter -> setBrush(QBrush(QColor(240, 0, 0, 127)));

View File

@@ -260,48 +260,33 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
painter -> setPen(pen); painter -> setPen(pen);
//vector use to draw handler if needed //Draw the shape and handlers if is selected
QVector <QPointF> point_vector;
//Draw the shape
switch (m_shapeType) switch (m_shapeType)
{ {
case Line: case Line:
painter->drawLine(QLineF(m_P1, m_P2)); painter->drawLine(QLineF(m_P1, m_P2));
if (isSelected()) if (isSelected())
point_vector << m_P1 << m_P2; m_handler.drawHandler(painter, QVector<QPointF>{m_P1, m_P2});
break; break;
case Rectangle: case Rectangle:
painter->drawRect(QRectF(m_P1, m_P2)); painter->drawRect(QRectF(m_P1, m_P2));
if (isSelected()) if (isSelected())
{ m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2)));
QRectF rect (m_P1, m_P2);
point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft();
}
break; break;
case Ellipse: case Ellipse:
painter->drawEllipse(QRectF(m_P1, m_P2)); painter->drawEllipse(QRectF(m_P1, m_P2));
if (isSelected()) if (isSelected())
{ m_handler.drawHandler(painter, m_handler.pointsForRect(QRectF(m_P1, m_P2)));
QRectF rect (m_P1, m_P2);
point_vector << rect.topLeft() << rect.topRight() << rect.bottomRight() << rect.bottomLeft();
}
break; break;
case Polyline: case Polyline:
{
painter->drawPolyline(m_polygon); painter->drawPolyline(m_polygon);
point_vector = m_polygon; if (isSelected())
} m_handler.drawHandler(painter, m_polygon);
break; break;
} }
//Draw handler if shape is selected
if (isSelected())
foreach(QPointF point, point_vector)
m_handler.DrawHandler(painter, point);
} }
/** /**
@@ -388,26 +373,11 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
} }
break; break;
case Rectangle: { case Rectangle:
QRectF rect(m_P1, m_P2); setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
if (m_vector_index == 0) rect.setTopLeft(new_pos);
else if (m_vector_index == 1) rect.setTopRight(new_pos);
else if (m_vector_index == 2) rect.setBottomLeft(new_pos);
else if (m_vector_index == 3) rect.setBottomRight(new_pos);
setRect(rect); case Ellipse:
} setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
case Ellipse: {
QRectF rect(m_P1, m_P2);
if (m_vector_index == 0) rect.setTopLeft(new_pos);
else if (m_vector_index == 1) rect.setTopRight(new_pos);
else if (m_vector_index == 2) rect.setBottomLeft(new_pos);
else if (m_vector_index == 3) rect.setBottomRight(new_pos);
setRect(rect);
}
break; break;
case Polyline: { case Polyline: {