/****************************************************************
** ImageView
** 图片浏览界面
**
** ImageView.cpp
**
** Zhou Tao 2017-05-18T10:52:16
**
** QQ : 741301964
**
** 深圳市XX电子股份有限公司
**
****************************************************************/
#include "imageview.h"
#include <QPainter>
#include <QDebug>
ImageView::ImageView(QWidget *parent, int width, int height) :
QWidget(parent),m_nWidth(width),m_nHeight(height)
{
//图片加载
for(int itemNum=0;itemNum<3;itemNum++)
{
QGraphicsScene *scene = new QGraphicsScene(0,0,1024,600);
CustomGraphicsView *view = new CustomGraphicsView(scene,this,false);
view->setGeometry(-1024+itemNum*1024,0,1024,600);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->centerOn(0.0,0.0);//确保scene显示居中
BlackPixmapItem *pItem = new BlackPixmapItem(185,120,false);
scene->addItem(pItem);
QPoint point = QPoint(0,0);
pItem->setPos(view->mapToScene(point));
m_pListItem.append(pItem);
m_listWidget.append(view);
}
//底部控件
m_BottomWidget = new QWidget(this);
m_BottomWidget->setGeometry(0,515,1024,85);
m_BottomWidget->installEventFilter(this);
m_BottomWidget->close();
//自动播放按键
m_autoplaybutton = new QPushButton(m_BottomWidget);
m_autoplaybutton->setText("播放");
m_autoplaybutton->setObjectName("autoplay");
m_autoplaybutton->setGeometry(73,0,157,85);
//目录
m_filelistbutton = new QPushButton(m_BottomWidget);
m_filelistbutton->setText("目录");
m_filelistbutton->setGeometry(793,0,157,85);
//放大
m_enlargementbutton = new QPushButton(m_BottomWidget);
m_enlargementbutton->setText("放大");
m_enlargementbutton->setObjectName("enlarge");
m_enlargementbutton->setGeometry(73+157+23,0,157,85);
//缩小
m_shrinkbutton = new QPushButton(m_BottomWidget);
m_shrinkbutton->setText("缩小");
m_shrinkbutton->setObjectName("shrink");
m_shrinkbutton->setGeometry(73+157+23+157+23,0,157,85);
//预览
m_previewbutton = new QPushButton(m_BottomWidget);
m_previewbutton->setText("预览");
m_previewbutton->setObjectName("preview");
m_previewbutton->setGeometry(73+157+23+157+23+157+23,0,157,85);
m_backbutton = new QPushButton(this);//返回
m_backbutton->setText("返回");
m_backbutton->setObjectName("back");
m_backbutton->setGeometry(32+54+28,20,54,54);
//按键信号与槽
connect(m_autoplaybutton, SIGNAL(pressed()), this, SLOT(slot_buttonClick()));
connect(m_enlargementbutton, SIGNAL(pressed()), this, SLOT(slot_buttonClick()));
connect(m_shrinkbutton, SIGNAL(pressed()), this,SLOT(slot_buttonClick()));
connect(m_backbutton, SIGNAL(pressed()), this, SLOT(slot_buttonClick()));
connect(m_previewbutton, SIGNAL(pressed()), this, SLOT(slot_buttonClick()));
//底部窗口动画
m_pShowAnimation = new QPropertyAnimation(m_BottomWidget, "pos");
m_pCloseAnimation = new QPropertyAnimation(m_BottomWidget, "pos");
connect(m_pCloseAnimation,SIGNAL(finished()),m_BottomWidget,SLOT(hide()));
//定时器 定时10s关闭上下窗口
timer = new QTimer();
//设置定时器每个多少毫秒发送一个timeout()信号 10s
timer->setInterval(10000);
connect(timer, SIGNAL(timeout()), this, SLOT(slot_mouseClick()));
//图片自动播放定时器
autoplaytimer = new QTimer();
autoplaytimer->setInterval(4000);//2s
connect(autoplaytimer, SIGNAL(timeout()), this, SLOT(slot_autoplay()));
m_nMoveTotalSize = 0;//总移动距离
scaleFactor = 1.0;//缩放因子
m_isMove = false;//是否移动鼠标
m_nCurrentIndex = 0;//路径索引
}
ImageView::~ImageView()
{
for(int i = 0;i<m_listWidget.size() ;i++)
{
delete m_listWidget.value(i);
}
}
//设置可隐藏界面定时器是否开启 true开启 false关闭
void ImageView::setTimerstart(bool start)
{
start?(timer->start()):(timer->stop());
//处理自动图片播放定时器如果打开则关闭
if(autoplaytimer->isActive())
autoplaytimer->stop();
}
void ImageView::closeBottomWidget()
{
m_pCloseAnimation->setDuration(400);
m_pCloseAnimation->setStartValue(QPoint(0,515));
m_pCloseAnimation->setEndValue(QPoint(0,600));
m_pCloseAnimation->setEasingCurve(QEasingCurve::Linear);//缓和曲线
m_pCloseAnimation->start();
//如果定时器开启,则关闭
if(timer->isActive())
{
timer->stop();
}
}
void ImageView::showBottomWidget()
{
m_BottomWidget->show();
m_pShowAnimation->setDuration(400);
m_pShowAnimation->setStartValue(QPoint(0,600));
m_pShowAnimation->setEndValue(QPoint(0,515));
m_pShowAnimation->setEasingCurve(QEasingCurve::Linear);//缓和曲线
m_pShowAnimation->start();
//如果定时器关闭,则开启
if(!timer->isActive())
{
timer->start();
}
}
void ImageView::slot_autoplay()
{
if((m_nCurrentIndex+1) == m_listImagePath.size())
{
BlackPixmapItem *pItem = getPixmapItem(m_listWidget.value(2));
pItem->setVisible(true);
readImageFile(m_listImagePath.value(0),2);//显示第一个
emit sign_pageTurning(0,m_nCurrentIndex);//翻到第一页
}
else if(m_nCurrentIndex/12 != (m_nCurrentIndex+1)/12)
{
emit sign_pageTurning((m_nCurrentIndex+1)/12,m_nCurrentIndex);//预览翻页
}
//自动播放图片
//第一个不移动
for(int i = 1;i<m_listWidget.size() ;i++)
{
QPoint endPos = QPoint(m_listWidget.value(i)->x() - m_nWidth,0);
m_listWidget.value(i)->moveWidgetAnimation(m_listWidget.value(i)->pos(),endPos,400);
}
//第一个item移到最后
m_listWidget.first()->move(m_nWidth,0);
CustomGraphicsView *m_pImageLoad = m_listWidget.takeFirst();
//更新最后一个item
m_nCurrentIndex++;
m_listWidget.append(m_pImageLoad);
if(m_nCurrentIndex == m_listImagePath.size())
m_nCurrentIndex = 0;
emit sign_getItemPixmap(m_nCurrentIndex+1,2);
}
//显示顶部底部控件
void ImageView::showWidget()
{
showBottomWidget(); //显示底部窗口
timer->start();//开启定时器
}
//关闭顶部底部控件
void ImageView::closeWidget(int duration)
{
timer->stop();
closeBottomWidget();
closeViewAnimation(QRect(0,0,0,0),duration);
}
//鼠标按下
void ImageView::mousePressEvent(QMouseEvent *ev)
{
if(autoplaytimer->isActive())
autoplaytimer->stop();
mousePress(ev->pos());
}
//鼠标松开
void ImageView::mouseReleaseEvent(QMouseEvent *ev)
{
if(m_isMove)
mouseRelease(ev->pos());
else
slot_mouseClick();
m_isMove = false;//是否移动鼠标
m_releaseTime = QTime::currentTime();//鼠标释放时间
}
void ImageView::mouseMoveEvent(QMouseEvent *ev)
{
int releaseTime = m_releaseTime.msecsTo(QTime::currentTime());
if(releaseTime < 200) //两次按下时间小于300ms,不响应,防止不停滑动
{
m_isMove = true;//是否移动鼠标
return;
}
mouseMove(ev->pos());
m_isMove = true;//是否移动鼠标
}
//过滤掉底部窗口鼠标事件,防止按下的时候拖动
bool ImageView::eventFilter(QObject *watched, QEvent *event)
{
;
}
void ImageView::slot_mouseClick()
{
if(m_BottomWidget->isHidden())
{
showBottomWidget(); //显示底部窗口
}else
{
closeBottomWidget(); //关闭底部窗口
}
}
//窗口显示动画
vo