forked from abcvincent/OpencvDevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypixitem.cpp
161 lines (142 loc) · 4 KB
/
mypixitem.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
#include "mypixitem.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QGraphicsSceneDragDropEvent>
#include <QDrag>
#include <math.h>
#include <QMessageBox>
#include <QGraphicsObject>
#pragma execution_character_set("utf-8")
//构造函数初始化了变量pix
MyPixItem::MyPixItem(QPixmap *pixmap)
{
pix = *pixmap;
setAcceptDrops(true); //设置可拖拽
m_scaleValue = 0;//放缩值初始化
m_isMove = false;//不可移动
widx=0;
widy=0;
}
//实现自己的图元边界函数
QRectF MyPixItem::boundingRect() const
{
return QRectF(-pix.width()/2, -pix.height()/2,
pix.width(), pix.height());//需要对应图元,不然出现残影
}
//只需QPainter的drawPixmap()函数将图元图片绘出即可
void MyPixItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *)
{
painter->drawPixmap(-pix.width()/2, -pix.height()/2, pix);//需要对应边界函数
}
//鼠标点击事件
void MyPixItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
m_startPos = event->pos();
m_isMove = true;//图元是否可以移动
}
void MyPixItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(m_isMove)
{
QPointF point =mapToScene(event->pos())-mapToScene(m_startPos);
moveBy(point.x(),point.y());
// 鼠标点击后并移动则图元相应移动,进行了图元坐标映射,映射到窗口中
}
}
void MyPixItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
m_isMove = false;
}
//双击复位
void MyPixItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
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 MyPixItem::wheelEvent(QGraphicsSceneWheelEvent *event)
{
setZoomState(NO_STATE);
int scaleValue = m_scaleValue;
if(event->delta() > 0) //delta()为正,滚轮向上滚
{
scaleValue++;
}
else
{
scaleValue--;
}
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());//基于图元坐标内鼠标指针变换中心
}
}
//从widget获取的缩放值,用于同步滚轮和按键
void MyPixItem::setScaleValue(const int &scaleValue)
{
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);
}
//复原图像
void MyPixItem::setZoomState(const int &zoomState)
{
m_zoomState = zoomState;
if (m_zoomState == RESET)
{
m_scaleValue = 0;
setScale(1);
setTransformOriginPoint(0, 0);
}
}
//获取放缩值
int MyPixItem::getScaleValue() const
{
return m_scaleValue;
}
//返回窗口值
void MyPixItem::setValue(const QPointF &pointxy)
{
QPointF p;
p=pointxy;
widx=p.x();
widy=p.y();
}