-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.cpp
151 lines (134 loc) · 4.46 KB
/
block.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <QtGui>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include <QPainterPath>
#include <QGraphicsScene>
#include "block.h"
#include "BlockPropertiesDialog.h"
class QGraphicsScene;
block::block(){
setFlags(ItemIsSelectable);
setZValue(-1);
}
block::block(qreal width,qreal height){
this->width=width;
this->height=height;
setFlags(ItemIsSelectable);
setZValue(-1);
}
void block::setWidth(qreal width){
prepareGeometryChange();
this->width=width;
update();
};
qreal block::getWidth(){
return width;
};
void block::setHeight(qreal height){
prepareGeometryChange();
this->height=height;
update();
};
qreal block::getHeight(){
return height;
};
//返回图形的矩形边界(函数重载)
QRectF block::boundingRect() const{
return QRectF(-width/2,-height/2,width,height);
}
//由矩形边界生成圆角矩形,并返回圆角矩形的边界路径(函数重载)
QPainterPath block::shape() const
{
QRectF rect=this->boundingRect();
QPainterPath path;
path.addRoundRect(rect,10,10);
return path;
}
//绘制街区:对圆角矩形的边界路径进行颜色填充(函数重载)
void block::paint(QPainter*painter,
const QStyleOptionGraphicsItem*option,
QWidget*)
{
if(option->state&QStyle::State_Selected){
//QRectF rect=this->boundingRect();
QPen pen(Qt::black);
pen.setStyle(Qt::DotLine);
pen.setWidth(2);
painter->setPen(pen);
painter->drawPath(this->shape());
}
QColor color=scene()->collidingItems(this).isEmpty()?
QColor(238,238,238):Qt::red;
painter->fillPath(this->shape(),color);
}
//鼠标双击:弹出对象属性框
void block::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *){
BlockPropertiesDialog Bldialog(this);
Bldialog.exec();
}
//如果检测到用户按住alt/shift键,判定beResizing是否为真;如果用户没有按键,则选中点击的图形
void block::mousePressEvent(QGraphicsSceneMouseEvent*event){
if(event->button()==Qt::LeftButton){
if(event->modifiers()==Qt::AltModifier||
event->modifiers()==Qt::ShiftModifier){
double w=boundingRect().width()/2.0;
double h=boundingRect().height()/2.0;
qDebug()<<"Item left clicked with key.";
QPointF pos=event->scenePos();
qDebug()<<boundingRect()<<this->pos()<<pos<<event->pos();
double radius=sqrt(pow(w,2)+pow(h,2));
double dist=sqrt(pow(this->x()-pos.x(), 2)
+ pow(this->y()-pos.y(), 2));
if(dist/radius>0.6){
qDebug()<<dist<<radius<<dist/radius;
beResizing=true;
}else{
beResizing=false;
}
}else{
qDebug() << "Custom item left clicked.";
setSelected(true);
}
}
}
//shift+鼠标拖动:等比例缩放
//alt+鼠标拖动:自定义缩放
void block::mouseMoveEvent(QGraphicsSceneMouseEvent*event){
if((event->modifiers()==Qt::AltModifier)&&beResizing){
QPointF pos=event->scenePos();
double dist_1=abs(this->x()-pos.x());
double dist_2=abs(this->y()-pos.y());
this->setWidth(2*dist_1);
this->setHeight(2*dist_2);
this->setPos(this->pos());
}else if((event->modifiers()==Qt::ShiftModifier)&&beResizing){
QPointF pos=event->scenePos();
double dist_1=abs(this->x()-pos.x());
this->setScale(dist_1/(boundingRect().width()/2));
}/*else{
qDebug()<<"item moves.";
QGraphicsItem::mouseMoveEvent(event);
qDebug()<<"moved"<<pos();
}*/
}
void block::mouseReleaseEvent(QGraphicsSceneMouseEvent*event){
if((event->modifiers()==Qt::AltModifier||
event->modifiers()==Qt::ShiftModifier)&&beResizing){
beResizing=false;
}else{
QGraphicsItem::mouseReleaseEvent(event);
}
}
int block::type()const{
return Type;
}
//检测街区是否与其他街区发生碰撞(函数重载)
bool block::collidesWithItem(const QGraphicsItem *other,
Qt::ItemSelectionMode mode)const{
if(other->type()==UserType+3){
return QGraphicsItem::collidesWithItem(other,mode);
}
else {
return false;
}
}