mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-19 14:50:53 +01:00
QetShapeItem -> handler : handler is draw at the same size at screen, no matter the curent zoom
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4050 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -16,50 +16,41 @@
|
|||||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "qetgraphicshandlerutility.h"
|
#include "qetgraphicshandlerutility.h"
|
||||||
#include <QPixmapCache>
|
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
#define QetGraphicsHandlerSquareSize 10
|
/**
|
||||||
|
* @brief QetGraphicsHandlerUtility::QetGraphicsHandlerUtility
|
||||||
|
* Constructor
|
||||||
|
* @param size : the size of the handler
|
||||||
|
*/
|
||||||
|
QetGraphicsHandlerUtility::QetGraphicsHandlerUtility(qreal size) :
|
||||||
|
m_size (size),
|
||||||
|
m_zoom_factor(1)
|
||||||
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief QetGraphicsHandlerUtility::pixmapHandler
|
* @brief QetGraphicsHandlerUtility::DrawHandler
|
||||||
* @return The pixmap of an handler
|
* Draw the handler at pos @point, using the QPainter @painter.
|
||||||
|
* @param painter : painter to use for drawing the handler
|
||||||
|
* @param point : point to draw the handler
|
||||||
*/
|
*/
|
||||||
QPixmap QetGraphicsHandlerUtility::pixmapHandler()
|
void QetGraphicsHandlerUtility::DrawHandler(QPainter *painter, const QPointF &point, bool color2)
|
||||||
{
|
{
|
||||||
QPixmap handler(QetGraphicsHandlerSquareSize, QetGraphicsHandlerSquareSize);
|
//Color of handler
|
||||||
|
|
||||||
if (!QPixmapCache::find("QetGraphicsHandler", handler))
|
|
||||||
{ //Pixmap isn't store in the QPixmapCache, we create it
|
|
||||||
QColor inner(0xFF, 0xFF, 0xFF);
|
QColor inner(0xFF, 0xFF, 0xFF);
|
||||||
QColor outer(0x00, 0x61, 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();
|
||||||
|
|
||||||
QPainter painter_(&handler);
|
painter->save();
|
||||||
painter_.setBrush(QBrush(inner));
|
painter->setBrush(QBrush(inner));
|
||||||
QPen square_pen(QBrush(outer), 2.0, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
|
QPen square_pen(QBrush(outer), 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
|
||||||
square_pen.setCosmetic(true);
|
square_pen.setCosmetic(true);
|
||||||
painter_.setPen(square_pen);
|
painter->setPen(square_pen);
|
||||||
painter_.drawRect(0,0,10,10);
|
painter->drawRect(getRect(point));
|
||||||
|
painter->restore();
|
||||||
//Store the pixmap in the QPixmapCache
|
|
||||||
QPixmapCache::insert("QetGraphicsHandler", handler);
|
|
||||||
}
|
|
||||||
return handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief QetGraphicsHandlerUtility::posForHandler
|
|
||||||
* Returns a QPointF at the good coordinates
|
|
||||||
* for draw the handler pixmap centered on the point to modify
|
|
||||||
* @param point : the point to modify
|
|
||||||
* @return : point at the good coordinates to draw handler centered in @point
|
|
||||||
*/
|
|
||||||
QPointF QetGraphicsHandlerUtility::posForHandler(const QPointF &point)
|
|
||||||
{
|
|
||||||
QPointF snap_point = point;
|
|
||||||
snap_point.rx() -= QetGraphicsHandlerSquareSize/2;
|
|
||||||
snap_point.ry() -= QetGraphicsHandlerSquareSize/2;
|
|
||||||
return snap_point;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,10 +59,8 @@ QPointF QetGraphicsHandlerUtility::posForHandler(const QPointF &point)
|
|||||||
* @param key_point : point at the center of handler (the point to modify, for exemple the corner of a rectangle)
|
* @param key_point : point at the center of handler (the point to modify, for exemple the corner of a rectangle)
|
||||||
* @return true if point is in a handler. else false
|
* @return true if point is in a handler. else false
|
||||||
*/
|
*/
|
||||||
bool QetGraphicsHandlerUtility::pointIsInHandler(const QPointF &point, const QPointF &key_point)
|
bool QetGraphicsHandlerUtility::pointIsInHandler(const QPointF &point, const QPointF &key_point) const {
|
||||||
{
|
return (getRect(key_point).contains(point));
|
||||||
QRectF handler (posForHandler(key_point), QSize(QetGraphicsHandlerSquareSize, QetGraphicsHandlerSquareSize));
|
|
||||||
return handler.contains(point);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,7 +69,7 @@ bool QetGraphicsHandlerUtility::pointIsInHandler(const QPointF &point, const QPo
|
|||||||
* @param vector : vector of key_point (the point to modify, for exemple the corners of a rectangle)
|
* @param vector : vector of key_point (the point to modify, for exemple the corners of a rectangle)
|
||||||
* @return if point is hover an handler, return the index of the hovered key_point in the vector, else return -1
|
* @return if point is hover an handler, return the index of the hovered key_point in the vector, else return -1
|
||||||
*/
|
*/
|
||||||
int QetGraphicsHandlerUtility::pointIsHoverHandler(const QPointF &point, const QVector<QPointF> &vector)
|
int QetGraphicsHandlerUtility::pointIsHoverHandler(const QPointF &point, const QVector<QPointF> &vector) const
|
||||||
{
|
{
|
||||||
foreach (QPointF key_point, vector)
|
foreach (QPointF key_point, vector)
|
||||||
if (pointIsInHandler(point, key_point))
|
if (pointIsInHandler(point, key_point))
|
||||||
@@ -91,18 +80,44 @@ int QetGraphicsHandlerUtility::pointIsHoverHandler(const QPointF &point, const Q
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief QetGraphicsHandlerUtility::handlerRect
|
* @brief QetGraphicsHandlerUtility::handlerRect
|
||||||
* Return the rect of pixmap handler for all key_point in vector (the point to modify, for exemple the corners of a rectangle)
|
* Return the rect of the handler for all key_point in vector (the point to modify, for exemple the corners of a rectangle)
|
||||||
* The order of rect in the returned vector is the same as the given vector.
|
* The order of rect in the returned vector is the same as the given vector.
|
||||||
* @param vector
|
* @param vector
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
QVector<QRectF> QetGraphicsHandlerUtility::handlerRect(const QVector<QPointF> &vector)
|
QVector<QRectF> QetGraphicsHandlerUtility::handlerRect(const QVector<QPointF> &vector) const
|
||||||
{
|
{
|
||||||
QVector <QRectF> rect_vector;
|
QVector <QRectF> rect_vector;
|
||||||
QSize size(QetGraphicsHandlerSquareSize, QetGraphicsHandlerSquareSize);
|
|
||||||
|
|
||||||
foreach(QPointF point, vector)
|
foreach(QPointF point, vector)
|
||||||
rect_vector << QRectF(posForHandler(point), size);
|
rect_vector << getRect(point);
|
||||||
|
|
||||||
return rect_vector;
|
return rect_vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief QetGraphicsHandlerUtility::getRect
|
||||||
|
* @param point
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
QRectF QetGraphicsHandlerUtility::getRect(const QPointF &point) const
|
||||||
|
{
|
||||||
|
qreal rect_size = m_size * m_zoom_factor;
|
||||||
|
QRectF rect(point.x() - rect_size/2, point.y() - rect_size/2, rect_size, rect_size);
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief QetGraphicsHandlerUtility::pointsForRect
|
||||||
|
* Return the point of the rect in vector.
|
||||||
|
* The point are stored like this :
|
||||||
|
* top left, top right, bottom left, bottom right;
|
||||||
|
* @param rect
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
QVector<QPointF> QetGraphicsHandlerUtility::pointsForRect(const QRectF &rect)
|
||||||
|
{
|
||||||
|
QVector<QPointF> vector;
|
||||||
|
vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
|
||||||
|
return vector;
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,21 +18,34 @@
|
|||||||
#ifndef QETGRAPHICSHANDLERUTILITY_H
|
#ifndef QETGRAPHICSHANDLERUTILITY_H
|
||||||
#define QETGRAPHICSHANDLERUTILITY_H
|
#define QETGRAPHICSHANDLERUTILITY_H
|
||||||
|
|
||||||
#include <QPixmap>
|
#include <QPointF>
|
||||||
|
#include <QRectF>
|
||||||
|
|
||||||
|
class QPainter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The QetGraphicsHandlerUtility class
|
* @brief The QetGraphicsHandlerUtility class
|
||||||
* This class provide some static 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...
|
||||||
*/
|
*/
|
||||||
class QetGraphicsHandlerUtility
|
class QetGraphicsHandlerUtility
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static QPixmap pixmapHandler();
|
QetGraphicsHandlerUtility (qreal size = 1);
|
||||||
static QPointF posForHandler(const QPointF &point);
|
void setSize(qreal size) {m_size = size;}
|
||||||
static bool pointIsInHandler (const QPointF &point, const QPointF &key_point);
|
void DrawHandler (QPainter *painter, const QPointF & point, bool color2 = false);
|
||||||
static int pointIsHoverHandler (const QPointF &point, const QVector<QPointF> &vector);
|
QPointF posForHandler(const QPointF &point) const;
|
||||||
static QVector<QRectF> handlerRect (const QVector<QPointF> &vector);
|
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;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QRectF getRect (const QPointF &point) const;
|
||||||
|
qreal m_size;
|
||||||
|
qreal m_zoom_factor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static QVector <QPointF> pointsForRect (const QRectF & rect);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QETGRAPHICSHANDLERUTILITY_H
|
#endif // QETGRAPHICSHANDLERUTILITY_H
|
||||||
|
|||||||
@@ -21,11 +21,8 @@
|
|||||||
#include "qet.h"
|
#include "qet.h"
|
||||||
#include "shapegraphicsitempropertieswidget.h"
|
#include "shapegraphicsitempropertieswidget.h"
|
||||||
#include "PropertiesEditor/propertieseditordialog.h"
|
#include "PropertiesEditor/propertieseditordialog.h"
|
||||||
#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
|
|
||||||
#include "qetshapegeometrycommand.h"
|
#include "qetshapegeometrycommand.h"
|
||||||
|
|
||||||
typedef QetGraphicsHandlerUtility QGHU;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief QetShapeItem::QetShapeItem
|
* @brief QetShapeItem::QetShapeItem
|
||||||
* Constructor of shape item. point 1 and 2 must be in scene coordinate
|
* Constructor of shape item. point 1 and 2 must be in scene coordinate
|
||||||
@@ -42,8 +39,8 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem
|
|||||||
m_P2 (p2),
|
m_P2 (p2),
|
||||||
m_hovered(false),
|
m_hovered(false),
|
||||||
m_mouse_grab_handler(false),
|
m_mouse_grab_handler(false),
|
||||||
m_undo_command(nullptr)
|
m_undo_command(nullptr),
|
||||||
|
m_handler(10)
|
||||||
{
|
{
|
||||||
if (type == Polyline) m_polygon << m_P1 << m_P2;
|
if (type == Polyline) m_polygon << m_P1 << m_P2;
|
||||||
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
||||||
@@ -224,7 +221,7 @@ QPainterPath QetShapeItem::shape() const
|
|||||||
else
|
else
|
||||||
vector = m_polygon;
|
vector = m_polygon;
|
||||||
|
|
||||||
foreach(QRectF r, QGHU::handlerRect(vector))
|
foreach(QRectF r, m_handler.handlerRect(vector))
|
||||||
path.addRect(r);
|
path.addRect(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +301,7 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
|
|||||||
//Draw handler if shape is selected
|
//Draw handler if shape is selected
|
||||||
if (isSelected())
|
if (isSelected())
|
||||||
foreach(QPointF point, point_vector)
|
foreach(QPointF point, point_vector)
|
||||||
painter->drawPixmap(QGHU::posForHandler(point), QGHU::pixmapHandler());
|
m_handler.DrawHandler(painter, point);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -344,28 +341,13 @@ void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|||||||
QVector <QPointF> vector;
|
QVector <QPointF> vector;
|
||||||
switch (m_shapeType)
|
switch (m_shapeType)
|
||||||
{
|
{
|
||||||
case Line:
|
case Line: vector << m_P1 << m_P2; break;
|
||||||
vector << m_P1 << m_P2;
|
case Rectangle: vector = m_handler.pointsForRect(QRectF(m_P1, m_P2)); break;
|
||||||
break;
|
case Ellipse: vector = m_handler.pointsForRect(QRectF(m_P1, m_P2)); break;
|
||||||
|
case Polyline: vector = m_polygon; break;
|
||||||
case Rectangle: {
|
|
||||||
QRectF rect (m_P1, m_P2);
|
|
||||||
vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Ellipse: {
|
|
||||||
QRectF rect (m_P1, m_P2);
|
|
||||||
vector << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case Polyline:
|
|
||||||
vector = m_polygon;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_vector_index = QGHU::pointIsHoverHandler(event->pos(), vector);
|
m_vector_index = m_handler.pointIsHoverHandler(event->pos(), vector);
|
||||||
if (m_vector_index != -1)
|
if (m_vector_index != -1)
|
||||||
{
|
{
|
||||||
//User click on an handler
|
//User click on an handler
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#define QETSHAPEITEM_H
|
#define QETSHAPEITEM_H
|
||||||
|
|
||||||
#include "qetgraphicsitem.h"
|
#include "qetgraphicsitem.h"
|
||||||
|
#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
|
||||||
|
|
||||||
class QDomElement;
|
class QDomElement;
|
||||||
class QDomDocument;
|
class QDomDocument;
|
||||||
@@ -94,5 +95,6 @@ class QetShapeItem : public QetGraphicsItem
|
|||||||
m_mouse_grab_handler;
|
m_mouse_grab_handler;
|
||||||
int m_vector_index;
|
int m_vector_index;
|
||||||
QetShapeGeometryCommand *m_undo_command;
|
QetShapeGeometryCommand *m_undo_command;
|
||||||
|
QetGraphicsHandlerUtility m_handler;
|
||||||
};
|
};
|
||||||
#endif // QETSHAPEITEM_H
|
#endif // QETSHAPEITEM_H
|
||||||
|
|||||||
Reference in New Issue
Block a user