This repository has been archived by the owner on Oct 5, 2019. It is now read-only.
/
picviewerwindow.cpp
277 lines (244 loc) · 9.9 KB
/
picviewerwindow.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "picviewerwindow.h"
#include "ui_picviewerwindow.h"
PicViewerWindow::PicViewerWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PicViewerWindow)
{
ui->setupUi(this);
clickOnFrame = false;//是否点击到面板上
this->setWindowFlags(Qt::FramelessWindowHint);// | Qt::Tool
this->setAttribute(Qt::WA_TranslucentBackground, true);//背景透明(需要上一行配合)
picCenterPctX = 0.5;
picCenterPctY = 0.5;
vScrollBar=ui->picScrollArea->verticalScrollBar();
hScrollBar=ui->picScrollArea->horizontalScrollBar();
movie = new QMovie(this);
movie->setCacheMode(QMovie::CacheAll);
currentMovieDirectory="movies";
//connect(movie,SIGNAL(frameChanged(int)),this,SLOT(updateFrameSlider()));
//connect(movie,SIGNAL(stateChanged(QMovie::MovieState)),this,SLOT(updateButtons()));
//connect(frameSlider,SIGNAL(valueChanged(int)),this,SLOT(goToFrame(int)));
//connect(speedSpinBox,SIGNAL(valueChanged(int)),movie,SLOT(setSpeed(int)));
//动画部分,窗口渐隐
fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity");
fadeOutAnimation->setDuration(300);
fadeOutAnimation->setStartValue(1);
fadeOutAnimation->setEndValue(0);
//窗口调节大小动画
floatUpAnimation = new QPropertyAnimation(this, "geometry");
floatUpAnimation->setDuration(300);
floatUpAnimation->setEasingCurve(QEasingCurve::OutCirc);
//上面的两个动画组合并绑定动画完成后退出程序
exitAnimationGroup = new QParallelAnimationGroup;
exitAnimationGroup->addAnimation(fadeOutAnimation);
exitAnimationGroup->addAnimation(floatUpAnimation);
connect(exitAnimationGroup, SIGNAL(finished()), this, SLOT(close()));//动画效果结束后隐藏
ui->viewerLabel->setMovie(movie);
movie->start();
}
PicViewerWindow::~PicViewerWindow()
{
delete ui;
delete fadeOutAnimation;
delete floatUpAnimation;
delete exitAnimationGroup;
}
void PicViewerWindow::mousePressEvent(QMouseEvent *event)
{
QPoint gloPoint = event->globalPos();
this->region(gloPoint);
if (event->button() == Qt::LeftButton)
{
drag_pos = event->pos();
scrollBarValue.setX(vScrollBar->value());
scrollBarValue.setY(hScrollBar->value());
clickOnFrame = true;//点击到窗口框架上才会变成true,改动之后新增功能:判断点击是否在边框右侧
event->accept();
}
}
void PicViewerWindow::mouseReleaseEvent(QMouseEvent *)
{
clickOnFrame = false;//弹起鼠标按键时,恢复
/*
scrollBarValue.setX(vScrollBar->isEnabled());
scrollBarValue.setY(hScrollBar->value());
vScrollBar->setValue(scrollBarValue.y() );
hScrollBar->setValue(scrollBarValue.x() );
*/
if(mouseDir != NONE) {
this->releaseMouse();
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
void PicViewerWindow::mouseMoveEvent(QMouseEvent *event)
{
// 无边框窗体的边缘拖动resize方案参考了如下博客的代码
// http://www.cnblogs.com/xufeiyang/p/3313104.html
QPoint gloPoint = event->globalPos();
QRect rect = this->rect();
QPoint tl = mapToGlobal(rect.topLeft());
QPoint rb = mapToGlobal(rect.bottomRight());
if (event->buttons() & Qt::LeftButton && clickOnFrame)
{
if(mouseDir != NONE) {
QRect rMove(tl, rb);
switch(mouseDir) {
case LEFT:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
break;
case RIGHT:
rMove.setWidth(gloPoint.x() - tl.x());
break;
case UP:
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case DOWN:
rMove.setHeight(gloPoint.y() - tl.y());
break;
case LEFTTOP:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case RIGHTTOP:
rMove.setWidth(gloPoint.x() - tl.x());
rMove.setY(gloPoint.y());
break;
case LEFTBOTTOM:
rMove.setX(gloPoint.x());
rMove.setHeight(gloPoint.y() - tl.y());
break;
case RIGHTBOTTOM:
rMove.setWidth(gloPoint.x() - tl.x());
rMove.setHeight(gloPoint.y() - tl.y());
break;
default:
break;
}
this->setGeometry(rMove);
} else {
//滚动条宽度都改成了1px,以是否可见滚动条为判别移动图片还是窗体
//尚不能判断拖动的是图片还是窗体
if (vScrollBar->isVisible() || hScrollBar->isVisible()) {
int xvalue = event->x()-drag_pos.x();
int yvalue = event->y()-drag_pos.y();
vScrollBar->setValue(scrollBarValue.y()-yvalue);
hScrollBar->setValue(scrollBarValue.x()-xvalue);
} else {
move(gloPoint - drag_pos);
}
}
event->accept();
}
}
void PicViewerWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void PicViewerWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
QString fileName = urls.first().toLocalFile();
if (fileName.isEmpty())
return;
if(fileName.toLower().endsWith(".jpg")||
fileName.toLower().endsWith(".jpeg")||
fileName.toLower().endsWith(".png")||
fileName.toLower().endsWith(".gif")||
fileName.toLower().endsWith(".bmp"))
{
openPic(fileName);//仅处理一个文件
}
}
void PicViewerWindow::openPic(QString fileName) {
currentMovieDirectory = QFileInfo(fileName).path();
setWindowTitle(QFileInfo(fileName).fileName());
movie->stop();
ui->viewerLabel->setMovie(movie);
movie->setFileName(fileName);
movie->start();
QPixmap img(fileName);
ui->viewerLabel->resize(img.width(),img.height());
//ui->viewerLabel->setGeometry(getPicRect(1));//载入后更新图片位置
}
void PicViewerWindow::on_closeBtn_clicked()
{
floatUpAnimation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), this->geometry().width(), this->geometry().height()));
floatUpAnimation->setEndValue(QRect(this->geometry().x(), this->geometry().y()-80, this->geometry().width(), this->geometry().height()));
exitAnimationGroup->start();//动画完成之后,程序将退出
//qApp->exit();//主窗口关闭后,关闭程序
}
void PicViewerWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
event->accept();
floatUpAnimation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), this->geometry().width(), this->geometry().height()));
floatUpAnimation->setEndValue(QRect(this->geometry().x(), this->geometry().y()-80, this->geometry().width(), this->geometry().height()));
exitAnimationGroup->start();//动画完成之后,程序将退出
//qApp->exit();//主窗口关闭后,关闭程序
}
void PicViewerWindow::region(const QPoint &cursorGlobalPoint)
{
// 获取窗体在屏幕上的位置区域,tl为topleft点,rb为rightbottom点
QRect rect = this->rect();
QPoint tl = mapToGlobal(rect.topLeft());
QPoint rb = mapToGlobal(rect.bottomRight());
int x = cursorGlobalPoint.x();
int y = cursorGlobalPoint.y();
if(tl.x() + PADDING >= x && tl.x() <= x && tl.y() + PADDING >= y && tl.y() <= y) {
mouseDir = LEFTTOP;// 左上角
this->setCursor(QCursor(Qt::SizeFDiagCursor)); // 设置鼠标形状
} else if(x >= rb.x() - PADDING && x <= rb.x() && y >= rb.y() - PADDING && y <= rb.y()) {
mouseDir = RIGHTBOTTOM;// 右下角
this->setCursor(QCursor(Qt::SizeFDiagCursor));
} else if(x <= tl.x() + PADDING && x >= tl.x() && y >= rb.y() - PADDING && y <= rb.y()) {
mouseDir = LEFTBOTTOM;//左下角
this->setCursor(QCursor(Qt::SizeBDiagCursor));
} else if(x <= rb.x() && x >= rb.x() - PADDING && y >= tl.y() && y <= tl.y() + PADDING) {
mouseDir = RIGHTTOP;// 右上角
this->setCursor(QCursor(Qt::SizeBDiagCursor));
} else if(x <= tl.x() + PADDING && x >= tl.x()) {
mouseDir = LEFT;// 左边
this->setCursor(QCursor(Qt::SizeHorCursor));
} else if( x <= rb.x() && x >= rb.x() - PADDING) {
mouseDir = RIGHT;// 右边
this->setCursor(QCursor(Qt::SizeHorCursor));
}else if(y >= tl.y() && y <= tl.y() + PADDING){
mouseDir = UP;// 上边
this->setCursor(QCursor(Qt::SizeVerCursor));
} else if(y <= rb.y() && y >= rb.y() - PADDING) {
mouseDir = DOWN;// 下边
this->setCursor(QCursor(Qt::SizeVerCursor));
} else {
mouseDir = NONE;// 默认
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
void PicViewerWindow::resizeEvent(QResizeEvent *)
{
ui->closeBtn->setGeometry(width()- 29, 5, 24, 24);
ui->optionPanel->setGeometry(width()/2 - ui->optionPanel->width()/2, height()-40, 340, 40);
ui->picScrollArea->setGeometry(0,0,width(),height());
//ui->viewerLabel->setGeometry(getPicRect(1));
}
QRect PicViewerWindow::getPicRect(double zoomPct) {
//暂时忽略缩放比例
QRect rect;
rect.setLeft(width()*picCenterPctX - ui->viewerLabel->width()/2);
rect.setTop(height()*picCenterPctY - ui->viewerLabel->height()/2);
rect.setWidth(ui->viewerLabel->width());
rect.setHeight(ui->viewerLabel->height());
return rect;
}