#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QString>
#include <QStringList>
#include <QListWidget>
#include <QVBoxLayout>
#include <QListWidgetItem>
#include <mainwindow.h>
#include "ui_mainwindow.h"
#include <QProcess>
#include <QUrl>
#include<QMessageBox>
#include <mythread.h>
#include <windows.h>
#include <QTimer>
bool play_state;
bool if_reload=false;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setStyleSheet("border: 3px groove gray; border-radius: 5px; background-color: white;");
ui->label_player->setStyleSheet("border: 3px groove gray; border-radius: 5px; background-color: white; ");
ui->pushButton_play_and_pause->setEnabled(false);
m_showWidget.setWindowTitle("Image Show");
m_showWidget.setAlignment(Qt::AlignCenter);
this->IsFinishVideo=false;
qApp->processEvents();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
path = QFileDialog::getOpenFileName(this, tr("选择视频"), ".", tr("Image Files(*.flv *.mp4)"));
QFile file(path);
if(!file.open(QIODevice::ReadOnly))
{
return;
}
if(if_reload)
{
delete layout_video;
delete mediaPlayer;
delete videoWidget;
}
if_reload = true;
mediaPlayer = new QMediaPlayer();
videoWidget = new QVideoWidget();
layout_video = new QVBoxLayout;
layout_video->setMargin(1);
mediaPlayer->setMedia(QUrl::fromLocalFile(path));
play_state = true;
ui->pushButton_play_and_pause->setEnabled(true);
ui->pushButton_play_and_pause->setText("暂停");
videoWidget->resize(ui->label_player->size());
layout_video->addWidget(videoWidget);
ui->label_player->setLayout(layout_video);
mediaPlayer->setVideoOutput(videoWidget);
mediaPlayer->play();
connect(mediaPlayer,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(msg()));
}
void MainWindow::msg()
{
if(mediaPlayer->state()==QMediaPlayer::StoppedState)
{
QMessageBox::information(NULL, "vedio", "视频处理完毕", QMessageBox::Ok, QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_play_and_pause_clicked()
{
if(play_state)
{
mediaPlayer->pause();
ui->pushButton_play_and_pause->setText("播放");
}
else
{
mediaPlayer->play();
ui->pushButton_play_and_pause->setText("暂停");
}
play_state = !play_state;
}
void MainWindow::ShowImage()
{
qDebug()<<"调用"<<endl;
this->setWindowTitle("image");
this->setMinimumSize(800, 600);
this->setMaximumSize(1600,1200);
const QSize IMAGE_SIZE(250, 150);
const QSize ITEM_SIZE(250, 150);
m_strPath = "E:\\img";
QDir dir(m_strPath);
if (!dir.exists()) {
QMessageBox::information(NULL, "error", "没有此路径");
return;
}
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();
if (m_imgList.count() <= 0) {
return;
}
m_listWidget = new QListWidget(this);
m_listWidget->setIconSize(IMAGE_SIZE);
m_listWidget->setResizeMode(QListView::Adjust);
m_listWidget->setViewMode(QListView::IconMode);
m_listWidget->setMovement(QListView::Static);
m_listWidget->setSpacing(8);
int nowcount = 0;
dir.mkdir(m_strPath);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();
if(m_imgList.count() != oldimgList)
{
qDebug()<<"新加入图片"<<endl;
flag = 0;
oldimgList = m_imgList.count();
}
for (int i = 0; i<m_imgList.count(); ++i)
{
QPixmap pixmap(m_strPath + "\\" + m_imgList.at(i));
QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(i));
listWidgetItem->setSizeHint(ITEM_SIZE);
m_listWidget->insertItem(i, listWidgetItem);
}
delete ui->label->layout();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_listWidget);
mainLayout->setMargin(0);
ui->label->setLayout(mainLayout);
connect(m_listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slot_itemClicked(QListWidgetItem*)));
}
void MainWindow::slot_itemClicked(QListWidgetItem * item) {
qDebug()<<"item"<<m_listWidget->row(item);
m_showWidget.setPixmap(QPixmap(m_strPath + "\\" + m_imgList.at(m_listWidget->row(item))));
m_showWidget.show();
}
void MainWindow::onTimeout()
{
Isflag();
if(flag == 0)
{
this->ShowImage();
flag = 1;
}
}
void MainWindow::Isflag()
{
m_strPath = "E:\\img";
QDir dir(m_strPath);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
filters << "*.png" << "*.jpg"<<"*.jpeg";
dir.setNameFilters(filters);
m_imgList = dir.entryList();
if(m_imgList.count() != oldimgList)
{
qDebug()<<"新加入图片"<<endl;
flag = 0;
oldimgList = m_imgList.count();
}
}
void MainWindow::on_pushButton_3_clicked()
{
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->start(1000);
}