-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixitem.cpp
More file actions
233 lines (206 loc) · 6.84 KB
/
pixitem.cpp
File metadata and controls
233 lines (206 loc) · 6.84 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "pixitem.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QGraphicsSceneDragDropEvent>
#include <QDrag>
#include <math.h>
#include <QMessageBox>
#include <QGraphicsObject>
#include "thresholddlg.h"
//构造函数初始化了变量pix
PixItem::PixItem(QPixmap *pixmap)
{
pix = *pixmap;
setAcceptDrops(true); //设置可拖拽
m_scaleValue = 0;//放缩值初始化
m_isMove = false;//不可移动
widx=0;
widy=0;
}
//实现自己的图元边界函数
QRectF PixItem::boundingRect() const
{
return QRectF(-pix.width()/2, -pix.height()/2,
pix.width(), pix.height());//需要对应图元,不然出现残影
}
//只需QPainter的drawPixmap()函数将图元图片绘出即可
void PixItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
painter->drawPixmap(-pix.width()/2, -pix.height()/2, pix);//需要对应边界函数
}
//鼠标点击事件
void PixItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_startPos = event->pos();
m_isMove = true;//图元是否可以移动
//////下面测试代码
// QPointF m_center;
// m_center=mapToScene(0,0);//坐标映射,图元坐标映射到窗口
// double a =m_center.x();
// QString str=QString::number(a,10,0); // 这是你的变量
// QMessageBox mesg;
// mesg.about(NULL,QString::fromLocal8Bit("图片大小") , str);
}
void PixItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// if(sceneBoundingRect().width()<widx&&sceneBoundingRect().height()<widy)
// {
// m_isMove = false;//小于窗口时候不可以移动
// }
// if(sceneBoundingRect().width()>widx-20||sceneBoundingRect().height()>widy-20)
// {
// QPointF m_center;
// m_center=mapToScene(0,0);//图元中心坐标
// QPointF points = event->pos()- m_startPos;
// int wx,wy;
// wx=sceneBoundingRect().width()-widx;
// wy=sceneBoundingRect().height()-widy;
// if(pow(1.1,wx/2)>pow(1.1,m_center.x())||pow(1.1,wy/2)>pow(1.1,m_center.y()))
// {
// moveBy(points.x(),points.y());
// }
// m_isMove = false;
// }
if(m_isMove)
{
QPointF point =mapToScene(event->pos())-mapToScene(m_startPos);
moveBy(point.x(),point.y());
// 鼠标点击后并移动则图元相应移动,进行了图元坐标映射,映射到窗口中
}
}
void PixItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
m_isMove = false;
}
//双击复位
void PixItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
// int ag=sceneBoundingRect().width();
// int bg=sceneBoundingRect().height();
// QString strag=QString::number(ag,10,0); // 这是你的变量
// QString strbg=QString::number(bg,10,0); // 这是你的变量
// QString strg = QString::fromLocal8Bit("图片宽度:%1,图片高度:%2").arg(strag).arg(strbg);
// QMessageBox mesgg;
// mesgg.about(NULL,QString::fromLocal8Bit("图片信息") ,strg);
setPos(0,0);
m_scaleValue = 0;
setScale(1);
setTransformOriginPoint(0, 0);
//做了坐标映射,将图片回复到窗口中心
QPointF m_center;
m_center=mapToScene(0,0);
if(m_center.x()!=0||m_center.y()!=0)
{
if(m_center.x()>0&&m_center.y()>0) moveBy(-m_center.x(), -m_center.y());
if(m_center.x()<0&&m_center.y()<0) moveBy(m_center.x(), m_center.y());
if(m_center.x()>0&&m_center.y()<0) moveBy(-m_center.x(), m_center.y());
if(m_center.x()<0&&m_center.y()>0) moveBy(m_center.x(), -m_center.y());
}
}
//使用滚轮整体缩放
void PixItem::wheelEvent(QGraphicsSceneWheelEvent *event)
{
setZoomState(NO_STATE);
int scaleValue = m_scaleValue;
if(event->delta() > 0) //delta()为正,滚轮向上滚
{
scaleValue++;
}
else
{
scaleValue--;
}
/////测试代码以下
// if(scaleValue<=0)//小于窗口时候自动回中央
// {
// setPos(0,0);
// QPointF m_center;
// m_center=mapToScene(0,0);
// if(m_center.x()!=0||m_center.y()!=0)
// {
// if(m_center.x()>0&&m_center.y()>0) moveBy(-m_center.x(), -m_center.y());
// if(m_center.x()<0&&m_center.y()<0) moveBy(m_center.x(), m_center.y());
// if(m_center.x()>0&&m_center.y()<0) moveBy(-m_center.x(), m_center.y());
// if(m_center.x()<0&&m_center.y()>0) moveBy(m_center.x(), -m_center.y());
// }
// }
//////////////*************以上
if (scaleValue > ZOOM_IN_TIMES || scaleValue < ZOOM_OUT_TIMES)
return;
m_scaleValue = scaleValue;
qreal s;
if(m_scaleValue > 0)
{
s = pow(1.1, m_scaleValue); //放大 计算x的y方次 参数都是double类型
}
else
{
s = pow(1 / 1.1, -m_scaleValue); //缩小
}
setScale(s);//setScale设置比例放缩,内置的处理图像放缩的方法
if(sceneBoundingRect().width()>=widx||sceneBoundingRect().height()>=widy)
{
// m_isMove = false;
setTransformOriginPoint(event->pos());//基于图元坐标内鼠标指针变换中心
}
// QPointF m_center;
// m_center=mapToScene(event->pos().x(),event->pos().y());//转换至窗口后指针变换中心
// setTransformOriginPoint(m_center);
}
//从widget获取的缩放值,用于同步滚轮和按键
void PixItem::setScaleValue(const int &scaleValue)
{
if (scaleValue > ZOOM_IN_TIMES || scaleValue < ZOOM_OUT_TIMES)
return;
m_scaleValue = scaleValue;
////////*******测试代码以下
// if(scaleValue<=0)//小于窗口时候自动回中央
// {
// setPos(0,0);
// QPointF m_center;
// m_center=mapToScene(0,0);
// if(m_center.x()!=0||m_center.y()!=0)
// {
// if(m_center.x()>0&&m_center.y()>0) moveBy(-m_center.x(), -m_center.y());
// if(m_center.x()<0&&m_center.y()<0) moveBy(m_center.x(), m_center.y());
// if(m_center.x()>0&&m_center.y()<0) moveBy(-m_center.x(), m_center.y());
// if(m_center.x()<0&&m_center.y()>0) moveBy(m_center.x(), -m_center.y());
// }
// }
qreal s;
if(m_scaleValue > 0)
{
s = pow(1.1, m_scaleValue); //放大 计算x的y方次 参数都是double类型,去除正负数
}
else
{
s = pow(1 / 1.1, -m_scaleValue); //缩小
}
setScale(s);
}
//复原图像
void PixItem::setZoomState(const int &zoomState)
{
m_zoomState = zoomState;
if (m_zoomState == RESET)
{
m_scaleValue = 0;
setScale(1);
setTransformOriginPoint(0, 0);
}
}
//获取放缩值
int PixItem::getScaleValue() const
{
return m_scaleValue;
}
//返回窗口值
void PixItem::setValue(const QPointF &pointxy)
{
QPointF p;
p=pointxy;
widx=p.x();
widy=p.y();
}