-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyitem.cpp
More file actions
63 lines (51 loc) · 1.27 KB
/
myitem.cpp
File metadata and controls
63 lines (51 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "myitem.h"
#include <QMimeData>
#include <QDrag>
#include <QDebug>
#include <QGraphicsView>
#include <QVarLengthArray>
#include <QPointF>
#include "math.h"
MyItem::MyItem(double radius, double Radius)
{
m_colorFlag = true;
m_radius = radius;
m_Radius = Radius;
setFlags(ItemIsSelectable | ItemIsMovable);
}
MyItem::~MyItem()
{
}
QRectF MyItem::boundingRect() const
{
return QRectF(-m_radius, -m_radius,
2*m_radius, 2*m_radius);
}
void MyItem::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
{
if(m_colorFlag)
painter->setBrush(Qt::blue);
else
painter->setBrush(Qt::red);
painter->drawEllipse(QPointF(0, 0), m_radius, m_radius);
}
void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qreal x = pos().x();
qreal y = pos().y();
double D = m_Radius - m_radius - 2;
// limit circle position
if(x*x + y*y >D*D)
{
double tmpX = x*D/sqrt(x*x + y*y);
double tmpY = y*D/sqrt(x*x + y*y);
setPos(tmpX, tmpY);
}
QGraphicsItem::mouseReleaseEvent(event);
}
void MyItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
m_colorFlag = !m_colorFlag;
QGraphicsItem::mouseDoubleClickEvent(event);
update();
}