Element editor : graphic part

Diagram editor : shape item
Gain a new way to be resized: mirror resizing. Click on the item for switch the resize mode


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4583 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-07-19 17:12:30 +00:00
parent d7a2350a0f
commit b71d3a4c7d
11 changed files with 201 additions and 26 deletions

View File

@@ -24,8 +24,7 @@
* @param size : the size of the handler
*/
QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) :
m_size (size),
m_zoom_factor(1)
m_size (size)
{}
/**
@@ -34,19 +33,15 @@ QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) :
* @param painter : painter to use for drawing 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)
{
//Color of handler
QColor inner(0xFF, 0xFF, 0xFF);
QColor outer(0x00, 0x61, 0xFF);
if(color2) outer = QColor(0x1A, 0x5C, 0x14);
//Setup the zoom factor to draw the handler in the same size at screen,
//no matter the zoom of the QPainter
m_zoom_factor = 1.0/painter->transform().m11();
painter->save();
painter->setBrush(QBrush(inner));
QPen square_pen(QBrush(outer), 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
painter->setBrush(QBrush(m_inner_color));
QPen square_pen(QBrush(m_outer_color), 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
square_pen.setCosmetic(true);
painter->setPen(square_pen);
painter->drawRect(getRect(point));
@@ -55,14 +50,14 @@ void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &po
/**
* @brief QetGraphicsHandlerUtility::drawHandler
* Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point, bool color2)
* Conveniance method for void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QPointF &point)
* @param painter
* @param points
* @param color2
*/
void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2) {
void QetGraphicsHandlerUtility::drawHandler(QPainter *painter, const QVector<QPointF> &points) {
foreach(QPointF point, points)
drawHandler(painter, point, color2);
drawHandler(painter, point);
}
/**
@@ -107,6 +102,14 @@ QVector<QRectF> QetGraphicsHandlerUtility::handlerRect(const QVector<QPointF> &v
return rect_vector;
}
void QetGraphicsHandlerUtility::setInnerColor(QColor color) {
m_inner_color = color;
}
void QetGraphicsHandlerUtility::setOuterColor(QColor color) {
m_outer_color = color;
}
/**
* @brief QetGraphicsHandlerUtility::getRect
* @param point
@@ -192,6 +195,64 @@ QRectF QetGraphicsHandlerUtility::rectForPosAtIndex(const QRectF &old_rect, cons
return rect;
}
/**
* @brief QetGraphicsHandlerUtility::mirrorRectForPosAtIndex
* Return a rectangle after modification of the point '@pos' at index '@index' of original rectangle '@old_rect'.
* the opposite edge is modified inversely (like a mirror)
* @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::mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
{
if (index < 0 || index > 7) return old_rect;
QRectF rect = old_rect;
QPointF center = rect.center();
if (index == 0) {
qreal x = pos.x() + (pos.x() - rect.topLeft().x());
qreal y = pos.y() + (pos.y() - rect.topLeft().y());
rect.setTopLeft(QPointF(x,y));
}
else if (index == 1) {
qreal y = pos.y() + (pos.y() - rect.topLeft().y());
rect.setTop(y);
}
else if (index == 2) {
qreal x = pos.x() + (pos.x() - rect.topRight().x());
qreal y = pos.y() + (pos.y() - rect.topLeft().y());
rect.setTopRight(QPointF(x,y));
}
else if (index == 3) {
qreal x = pos.x() + (pos.x() - rect.left());
rect.setLeft(x);
}
else if (index == 4) {
qreal x = pos.x() + (pos.x() - rect.right());
rect.setRight(x);
}
else if (index == 5) {
qreal x = pos.x() + (pos.x() - rect.bottomLeft().x());
qreal y = pos.y() + (pos.y() - rect.bottomLeft().y());
rect.setBottomLeft(QPointF(x,y));
}
else if (index == 6) {
qreal y = pos.y() + (pos.y() - rect.bottom());
rect.setBottom(y);
}
else if (index == 7) {
qreal x = pos.x() + (pos.x() - rect.bottomRight().x());
qreal y = pos.y() + (pos.y() - rect.bottomRight().y());
rect.setBottomRight(QPointF(x,y));
}
rect.moveCenter(center);
return rect;
}
/**
* @brief QetGraphicsHandlerUtility::lineForPosAtIndex
* Return a line after modification of @pos at index @index of @old_line.

View File

@@ -20,6 +20,7 @@
#include <QRectF>
#include <QLineF>
#include <QColor>
class QPainter;
@@ -34,22 +35,27 @@ class QetGraphicsHandlerUtility
public:
QetGraphicsHandlerUtility (qreal size = 1);
void setSize(qreal size) {m_size = size;}
void drawHandler (QPainter *painter, const QPointF & point, bool color2 = false);
void drawHandler(QPainter *painter, const QVector<QPointF> &points, bool color2 = false);
void drawHandler (QPainter *painter, const QPointF & point);
void drawHandler(QPainter *painter, const QVector<QPointF> &points);
QPointF posForHandler(const QPointF &point) const;
bool pointIsInHandler (const QPointF &point, const QPointF &key_point) const;
int pointIsHoverHandler (const QPointF &point, const QVector<QPointF> &vector) const;
QVector<QRectF> handlerRect (const QVector<QPointF> &vector) const;
void setInnerColor (QColor color);
void setOuterColor (QColor color);
private:
QRectF getRect (const QPointF &point) const;
qreal m_size;
qreal m_zoom_factor;
qreal m_zoom_factor = 1;
QColor m_inner_color = Qt::white,
m_outer_color = Qt::blue;
public:
static QVector <QPointF> pointsForRect (const QRectF &rect);
static QVector <QPointF> pointsForLine (const QLineF &line);
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);
};

View File

@@ -21,11 +21,9 @@
#include "editorcommands.h"
#include "qet.h"
#include <QPainter>
#include <QDebug>
#include <QGraphicsSceneHoverEvent>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsScene>
#include <QTransform>
/**
Constructor
@@ -36,6 +34,7 @@ ElementPrimitiveDecorator::ElementPrimitiveDecorator(QGraphicsItem *parent):
m_handler(10)
{
init();
m_handler.setOuterColor(Qt::darkGreen);
}
/**
@@ -97,8 +96,9 @@ void ElementPrimitiveDecorator::paint(QPainter *painter, const QStyleOptionGraph
pen.setCosmetic(true);
painter -> setPen(pen);
painter -> drawRect(modified_bounding_rect_);
//Draw the handlers
m_handler.drawHandler(painter, getResizingsPoints(), decorated_items_.size()-1);
m_handler.drawHandler(painter, getResizingsPoints());
// uncomment to draw the real bouding rect (=adjusted internal bounding rect)
// painter -> setBrush(QBrush(QColor(240, 0, 0, 127)));

View File

@@ -232,7 +232,11 @@ void PartArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
prepareGeometryChange();
if (m_resize_mode == 1)
setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index));
else
setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index));
}
else
CustomElementGraphicPart::mouseMoveEvent(event);
@@ -245,8 +249,11 @@ void PartArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
*/
void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
if (event->button() == Qt::LeftButton) {
setCursor(Qt::OpenHandCursor);
if (event->buttonDownPos(Qt::LeftButton) == event->pos())
switchResizeMode();
}
if (m_handler_index >= 0 && m_handler_index <= 7)
{
@@ -261,3 +268,16 @@ void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
else
CustomElementGraphicPart::mouseReleaseEvent(event);
}
void PartArc::switchResizeMode()
{
if (m_resize_mode == 1) {
m_resize_mode = 2;
m_handler.setOuterColor(Qt::darkGreen);
}
else {
m_resize_mode = 1;
m_handler.setOuterColor(Qt::blue);
}
update();
}

View File

@@ -64,9 +64,13 @@ class PartArc : public AbstractPartEllipse
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
void switchResizeMode();
private:
QetGraphicsHandlerUtility m_handler;
int m_handler_index;
QPropertyUndoCommand *m_undo_command;
int m_resize_mode = 1;
};
#endif

View File

@@ -227,7 +227,11 @@ void PartEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
prepareGeometryChange();
if (m_resize_mode == 1)
setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index));
else
setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index));
}
else
CustomElementGraphicPart::mouseMoveEvent(event);
@@ -240,8 +244,11 @@ void PartEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
*/
void PartEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
if (event->button() == Qt::LeftButton) {
setCursor(Qt::OpenHandCursor);
if (event->buttonDownPos(Qt::LeftButton) == event->pos())
switchResizeMode();
}
if (m_handler_index >= 0 && m_handler_index <= 7)
{
@@ -256,3 +263,16 @@ void PartEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
else
CustomElementGraphicPart::mouseReleaseEvent(event);
}
void PartEllipse::switchResizeMode()
{
if (m_resize_mode == 1) {
m_resize_mode = 2;
m_handler.setOuterColor(Qt::darkGreen);
}
else {
m_resize_mode = 1;
m_handler.setOuterColor(Qt::blue);
}
update();
}

View File

@@ -66,9 +66,13 @@ class PartEllipse : public AbstractPartEllipse
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
void switchResizeMode();
private:
QetGraphicsHandlerUtility m_handler;
int m_handler_index;
QPropertyUndoCommand *m_undo_command;
int m_resize_mode = 1;
};
#endif

View File

@@ -298,7 +298,11 @@ void PartRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
prepareGeometryChange();
if (m_resize_mode == 1)
setRect(m_handler.rectForPosAtIndex(m_rect, pos_, m_handler_index));
else
setRect(m_handler.mirrorRectForPosAtIndex(m_rect, pos_, m_handler_index));
}
else
CustomElementGraphicPart::mouseMoveEvent(event);
@@ -311,8 +315,11 @@ void PartRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
*/
void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
if (event->button() == Qt::LeftButton) {
setCursor(Qt::OpenHandCursor);
if (event->buttonDownPos(Qt::LeftButton) == event->pos())
switchResizeMode();
}
if (m_handler_index >= 0 && m_handler_index <= 7)
{
@@ -327,3 +334,16 @@ void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
else
CustomElementGraphicPart::mouseReleaseEvent(event);
}
void PartRectangle::switchResizeMode()
{
if (m_resize_mode == 1) {
m_resize_mode = 2;
m_handler.setOuterColor(Qt::darkGreen);
}
else {
m_resize_mode = 1;
m_handler.setOuterColor(Qt::blue);
}
update();
}

View File

@@ -80,11 +80,15 @@ class PartRectangle : public CustomElementGraphicPart
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
void switchResizeMode();
private:
QRectF m_rect;
QList<QPointF> saved_points_;
QetGraphicsHandlerUtility m_handler;
int m_handler_index;
QPropertyUndoCommand *m_undo_command;
int m_resize_mode = 1;
};
#endif

View File

@@ -435,8 +435,24 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos;
break;
case Rectangle: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
case Ellipse: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
case Rectangle:
if (m_resize_mode == 1) {
setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
}
else {
setRect(m_handler.mirrorRectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
}
case Ellipse:
if (m_resize_mode == 1) {
setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
}
else {
setRect(m_handler.mirrorRectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
}
case Polygon:
prepareGeometryChange();
@@ -456,6 +472,9 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
*/
void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if ((m_shapeType & (Rectangle | Ellipse)) && event->buttonDownPos(Qt::LeftButton) == event->pos())
switchResizeMode();
if (m_mouse_grab_handler)
{
m_mouse_grab_handler = false;
@@ -488,6 +507,19 @@ void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QetGraphicsItem::mouseReleaseEvent(event);
}
void QetShapeItem::switchResizeMode()
{
if (m_resize_mode == 1) {
m_resize_mode = 2;
m_handler.setOuterColor(Qt::darkGreen);
}
else {
m_resize_mode = 1;
m_handler.setOuterColor(Qt::blue);
}
update();
}
/**
* @brief QetShapeItem::fromXml
* Build this item from the xml description

View File

@@ -102,6 +102,9 @@ class QetShapeItem : public QetGraphicsItem
virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *event);
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
private:
void switchResizeMode();
///ATTRIBUTES
private:
ShapeType m_shapeType;
@@ -114,5 +117,6 @@ class QetShapeItem : public QetGraphicsItem
int m_vector_index;
QetGraphicsHandlerUtility m_handler;
bool m_close = false;
int m_resize_mode = 1;
};
#endif // QETSHAPEITEM_H