-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyQGraphicsView.cpp
214 lines (204 loc) · 6.75 KB
/
MyQGraphicsView.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "MyQGraphicsView.h"
#include <QDebug>
MyQGraphicsView::MyQGraphicsView(QWidget *parent):
QGraphicsView(parent)
{
//设置鼠标样式
// setCursor(Qt::CrossCursor);
//使用抗锯齿渲染
// setRenderHint(QPainter::Antialiasing);
//设置缓冲背景,加速渲染
// setCacheMode(QGraphicsView::CacheBackground);
// setStyleSheet("border: 1px solid black");
// setAlignment(Qt::AlignVCenter | Qt::AlignTop);
// this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_scalnum = 1.;
m_flag = 0;
setMouseTracking(true);
}
void MyQGraphicsView::SetLinkPara(double para[3])
{
double scalenum = para[0];
double posx = para[1];
double posy = para[2];
//设置缩放
if (scalenum > m_scalnum)
{
this->scale(1.3, 1.3);
m_scalnum *= 1.3;
}
else if (scalenum < m_scalnum)
{
this->scale(1/1.3, 1/1.3);
m_scalnum /= 1.3;
}
//设置移动
if (posx != m_posx && posy != m_posy)
{
this->horizontalScrollBar()->setValue(posx);
this->verticalScrollBar()->setValue(posy);
m_posx = posx;
m_posy = posy;
}
}
void MyQGraphicsView::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0)
{
this->scale(1.3, 1.3);
m_scalnum *= 1.3;
}
else
{
this->scale(1/1.3, 1/1.3);
m_scalnum /= 1.3;
}
if (m_linkflag == 1)
{
m_linkgraphicsview->SetLinkPara(this->GetLinkPara());
}
QGraphicsView::wheelEvent(event);
}
void MyQGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton)
{
this->setCursor(Qt::PointingHandCursor);
m_startpos = mapToScene(event->pos());
}
//鼠标左键选点并画线
else if (event->button() == Qt::LeftButton && m_flag == 1)
{
QPointF p = this->mapToScene(event->pos());
// qDebug()<<p;
m_points.push_back(p);
//绘制点
QPen *pen = new QPen();
pen->setColor(QColor(255, 0, 0));
pen->setWidth(2);
this->scene()->addEllipse(p.x()-1, p.y()-1, 2, 2, *pen);
int num = m_points.size();
//绘制线
if (num >= 2)
{
pen->setColor(QColor(241, 89, 42));
pen->setWidth(1);
this->scene()->addLine(m_points[num-2].x(),
m_points[num-2].y(),
m_points[num-1].x(),
m_points[num-1].y(),
*pen);
}
}
//鼠标左键只选点
else if (event->button() == Qt::LeftButton && m_flag == 2)
{
QPointF p = this->mapToScene(event->pos());
m_points.push_back(p);
//画点
QPen *pen = new QPen();
pen->setColor(QColor(255, 0, 0));
pen->setWidth(2);
this->scene()->addEllipse(p.x()-1, p.y()-1, 4, 4, *pen);
//添加文本
QFont *font = new QFont("Roman times", 20, QFont::Bold);
QGraphicsTextItem *text = new QGraphicsTextItem(QString::number(m_points.size()));
text->setPos(p.x()-10, p.y()-10);
text->setDefaultTextColor(QColor(248, 201, 0));
text->setFont(*font);
this->scene()->addItem(text);
}
//鼠标右键完成当前区域选点
else if (event->button() == Qt::RightButton && m_flag == 1)
{
m_allpoints.push_back(m_points);
//连接终点与起点
QPen *pen = new QPen();
pen->setColor(QColor(241, 89, 42));
pen->setWidth(1);
int num = m_points.size() - 1;
// qDebug()<<m_points[num];
this->scene()->addLine(m_points[num].x(),
m_points[num].y(),
m_points[0].x(),
m_points[0].y(),
*pen);
//添加文本
QFont *font = new QFont("Roman times", 20, QFont::Bold);
QGraphicsTextItem *text = new QGraphicsTextItem(QString::number(m_allpoints.size()));
QPointF mpos = GetMeanPos(m_points);
text->setPos(mpos.x()-20, mpos.y()-20);
text->setDefaultTextColor(QColor(248, 201, 0));
text->setFont(*font);
this->scene()->addItem(text);
//清除已经选好的点集
m_points.clear();
}
QGraphicsView::mousePressEvent(event);
}
void MyQGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
//鼠标中建弹起进行视图移动
if (event->button() == Qt::MiddleButton)
{
if (m_flag != 0)
this->setCursor(Qt::CrossCursor);
else
this->setCursor(Qt::ArrowCursor);
m_endpos = mapToScene(event->pos());
//获取滚动条当前位置
double oposx = this->horizontalScrollBar()->value();
double oposy = this->verticalScrollBar()->value();
//计算鼠标移动的距离
QPointF offset = m_endpos - m_startpos;
//计算新的滚轮位置
double nposx = oposx-offset.x()*m_scalnum;
double nposy = oposy-offset.y()*m_scalnum;
//设置新的滚轮位置
this->horizontalScrollBar()->setValue(nposx);
this->verticalScrollBar()->setValue(nposy);
// QPointF offset = -m_endpos + m_startpos;
// this->scene()->setSceneRect(this->scene()->sceneRect().x()+offset.x(),
// this->scene()->sceneRect().y()+offset.y(),
// this->scene()->sceneRect().width(),
// this->scene()->sceneRect().height());
this->scene()->update();
//记录备用
m_posx = nposx;
m_posy = nposy;
//进行联动
if (m_linkflag == 1)
{
m_linkgraphicsview->SetLinkPara(this->GetLinkPara());
}
}
QGraphicsView::mouseReleaseEvent(event);
}
void MyQGraphicsView::SetImage(QImage img)
{
//把影像添加到画布
QPixmap Images = QPixmap::fromImage(img);
QGraphicsPixmapItem *map = new QGraphicsPixmapItem(Images);
// map = new QGraphicsPixmapItem(Images);
map->setFlag(QGraphicsPixmapItem::ItemIsSelectable, true);
map->setFlag(QGraphicsPixmapItem::ItemIsMovable, false);
map->setFlag(QGraphicsPixmapItem::ItemSendsGeometryChanges,true);
QGraphicsScene *scene = new QGraphicsScene(); //场景 = new QGraphicsScene();
//画布添加至场景
scene->addItem(map);
//场景绑定到控件
this->setScene(scene);
this->show();
}
QPointF MyQGraphicsView::GetMeanPos(std::vector<QPointF> data)
{
double sumx(0);
double sumy(0);
for (int i(0); i<data.size(); ++i)
{
sumx += data[i].x();
sumy += data[i].y();
}
return QPointF(sumx/data.size(), sumy/data.size());
}