PDA

View Full Version : resize and rotate handle



Noxxik
20th February 2009, 19:38
Hi I am trying make some resize and rotate handle of each item of class QGraphicsItem, but I have problem with it. I select my item, but I dont know what I should do to rotate or resize my item :confused: Could someone give me some hint, what I can use?
I tried it with helpful QRect and rotate with QGraphicsItem::rotate() and QGraphicsItem::scaled() but It doesnt work according to my idea before

zgulser
21st February 2009, 12:18
hi,
please send the source.

Noxxik
21st February 2009, 12:57
hi i solve it, but only partly

HandleItem::HandleItem( QGraphicsItem *item, QGraphicsScene *scene,
QColor color, HandleItem::HandleRole role,
QList<HandleItem*> handles ) : QGraphicsItem( 0, scene )
{
m_role = role;
m_color = color;
m_item = item;
m_handles = handles;
m_pressed = false;
setZValue( 100 );
setFlag( ItemIsMovable );
}

QRectF HandleItem::boundingRect() const
{
QPointF point = m_item->boundingRect().center();
switch( m_role ){
case CenterHandle:
return QRectF( point-QPointF(5, 5), QSize( 10, 10 ) );
case RightHandle:
point.setX( m_item->boundingRect().right() );
return QRectF( point-QPointF(3, 5), QSize( 6, 10 ) );
case TopHandle:
point.setY( m_item->boundingRect().top() );
return QRectF( point-QPointF(5, 3), QSize( 10, 6 ) );
}
return QRectF();
}

void HandleItem::paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
paint->setPen( m_color );
paint->setBrush( m_color );
QRectF rect = boundingRect();
QVector<QPointF> points;
switch( m_role ){
case CenterHandle:
paint->drawEllipse( rect );
break;
case RightHandle:
points << rect.center()+QPointF(3,0) << rect.center()+QPointF(-3,-5) << rect.center()+QPointF(-3,5);
paint->drawConvexPolygon( QPolygonF(points) );
break;
case TopHandle:
points << rect.center()+QPointF(0,-3) << rect.center()+QPointF(-5,3) << rect.center()+QPointF(5,3);
paint->drawConvexPolygon( QPolygonF(points) );
break;
}
}

void HandleItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = true;
QGraphicsItem::mousePressEvent( event );
}
void HandleItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = false;
QGraphicsItem::mouseReleaseEvent( event );
}

QVariant HandleItem::itemChange( GraphicsItemChange change, const QVariant &data )
{
if( change == ItemPositionChange && m_pressed ){
QPointF movement = data.toPoint() - pos();
QPointF center = m_item->boundingRect().center();
switch( m_role ){
case CenterHandle:
m_item->moveBy( movement.x(), movement.y() );
foreach( HandleItem *handle, m_handles )
handle->translate( movement.x(), movement.y() );
break;
case TopHandle:
if( -2*movement.y() + m_item->sceneBoundingRect().height() <= 5 )
return QGraphicsItem::itemChange( change, pos() );
movement.setX( 0 );
m_item->translate( center.x(), center.y() );
m_item->scale( 1, 1.0-2.0*movement.y() /(m_item->sceneBoundingRect().height()) );
m_item->translate( -center.x(), -center.y() );
break;
case RightHandle:
if( 2*movement.x() + m_item->sceneBoundingRect().width() <= 5 )
return QGraphicsItem::itemChange( change, pos() );
movement.setY( 0 );
m_item->translate( center.x(), center.y() );
m_item->scale( 1.0+2.0*movement.x()/(m_item->sceneBoundingRect().width()), 1 );
m_item->translate( -center.x(), -center.y() );
break;
}
return QGraphicsItem::itemChange( change, pos()+movement );
}
return QGraphicsItem::itemChange( change, data );
}
class:
class HandleItem : public QGraphicsItem
{
public:
enum HandleRole { CenterHandle, RightHandle, TopHandle };

HandleItem( QGraphicsItem *item, QGraphicsScene *scene,
QColor color, HandleRole role = CenterHandle,
QList<HandleItem*> handles = QList<HandleItem*>() );

void paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget );
QRectF boundingRect() const;

protected:
void mousePressEvent( QGraphicsSceneMouseEvent *event );
void mouseReleaseEvent( QGraphicsSceneMouseEvent *event );
QVariant itemChange( GraphicsItemChange change, const QVariant &data );

private:
QGraphicsItem *m_item;
HandleRole m_role;
QColor m_color;
QList<HandleItem*> m_handles;
bool m_pressed;
};
It is OK, but I dont know how translate coordition system to my item has CenterHandle, TopHandle and RightHandle at same pocition...
When I get mouseClick into my item CenterHandle, TopHandle and RightHandle are set well, I get mouse click next to my item CenterHandle, TopHandle and RightHandle are removed. It is OK. If I get mouseClick into my item CenterHandle, TopHandle and RightHandle are set and then I want to move my item. It is OK too. Then I click next to my item. It is OK. Then I want to move my item again, so I click into my item, but CenterHandle, TopHandle and RightHandle are set at first position(so I isnt in my item)

So I would like to ask, how I set translate or something else my item, when I change position of item.

Thanks a lot and sorry for my simple english

aamer4yu
21st February 2009, 18:27
There is some Photo Wall application on Qt-apps.org
You can search for it and see the code. It had provided good means to rotate and resize graphics item.

Noxxik
21st February 2009, 20:58
wow it is great thanks :) I try it

Noxxik
22nd February 2009, 14:03
I solved it only partly, but when I try to resize QGraphicsTextItem, i show handles in position 0,0 of all scene :(