140 lines
3.9 KiB
C++
140 lines
3.9 KiB
C++
#include "MainWindow.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QVBoxLayout>
|
|
#include <QLabel>
|
|
#include <QJsonDocument>
|
|
#include <QSettings>
|
|
#include <QJsonObject>
|
|
#include <QPainter>
|
|
|
|
#include "TokenRequest.h"
|
|
|
|
#include "ui_MainWindow.h"
|
|
|
|
MainWindow* s_instance = nullptr;
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow)
|
|
{
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
ui->setupUi(this);
|
|
|
|
s_instance = this;
|
|
|
|
|
|
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
|
|
QString settingPath = QApplication::applicationDirPath() + "/PhotoPrintPlayer/setting.ini";
|
|
qDebug() << "setting path" << settingPath;
|
|
QSettings settings(settingPath, QSettings::IniFormat);
|
|
QString videoPath = settings.value("DefaultImage").toString();
|
|
qDebug() << "DefaultImage: " << videoPath;
|
|
|
|
// 读取第二拍照视频名称
|
|
m_printVideo = settings.value("PrintVideoName").toString();
|
|
qDebug() << "Second Photo Video Name: " << m_printVideo;
|
|
|
|
videoPath = QApplication::applicationDirPath() + "/PhotoPrintPlayer/" + videoPath;
|
|
m_defaultImage.load(videoPath);
|
|
|
|
connect(&vlcPlayer_, &VlcMediaPlayer::VideoDataOutput, this, &MainWindow::slotSetOneFrame, Qt::QueuedConnection);
|
|
connect(&vlcPlayer_, &VlcMediaPlayer::Stopped, this, &MainWindow::OnStop);
|
|
connect(&videoUpdate_, &QTimer::timeout, [this]() {update(); });
|
|
videoUpdate_.start(30);
|
|
|
|
//PlayDefualtVideo();
|
|
|
|
int port = settings.value("port", 9530).toInt();
|
|
qDebug() << "port: " << port;
|
|
m_udpSocket.bind(QHostAddress::AnyIPv4, port);
|
|
connect(&m_udpSocket, &QUdpSocket::readyRead, this, &MainWindow::OnReadData);
|
|
|
|
|
|
m_tokenRequest = new TokenRequest(this);
|
|
}
|
|
|
|
MainWindow::~MainWindow() {
|
|
delete ui;
|
|
s_instance = nullptr;
|
|
}
|
|
|
|
MainWindow& MainWindow::Get() {
|
|
return *s_instance;
|
|
}
|
|
|
|
void MainWindow::slotSetOneFrame(QImage image) {
|
|
videoImage_ = image;
|
|
}
|
|
|
|
void MainWindow::PlayDefualtVideo() {
|
|
const QString videoPath = QApplication::applicationDirPath() + "/PhotoPrintPlayer/" + m_printVideo;
|
|
m_isPrinting = vlcPlayer_.Play(videoPath);
|
|
qDebug() << __FUNCTION__ << "play movie success:" << m_isPrinting << ",path:" << videoPath;
|
|
}
|
|
|
|
void MainWindow::OnReadData() {
|
|
while (m_udpSocket.hasPendingDatagrams()) {
|
|
QByteArray data;
|
|
data.resize(m_udpSocket.pendingDatagramSize());
|
|
m_udpSocket.readDatagram(data.data(), data.size());
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
|
|
if (!jsonDoc.isNull() && jsonDoc.isObject()) {
|
|
QJsonObject jsonObj = jsonDoc.object();
|
|
|
|
if (jsonObj.contains("cmd")) {
|
|
QString cmd = jsonObj["cmd"].toString();
|
|
|
|
if (cmd == "photo") {
|
|
OnProcessPhotoRequest();
|
|
}
|
|
else {
|
|
qDebug() << __FILE__ << __LINE__ <<" cmd" << cmd;
|
|
}
|
|
}
|
|
else {
|
|
qDebug() <<__FILE__<<__LINE__<< "not find cmd";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void MainWindow::OnProcessPhotoRequest() {
|
|
if (m_isPrinting) {
|
|
return;
|
|
}
|
|
PlayDefualtVideo();
|
|
}
|
|
|
|
void MainWindow::keyReleaseEvent(QKeyEvent* event) {
|
|
if (event->key() == Qt::Key_T) {
|
|
setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
|
|
show();
|
|
}
|
|
else if (event->key() == Qt::Key_D) {
|
|
auto flags = windowFlags();
|
|
setWindowFlags(flags & ~Qt::WindowStaysOnTopHint);
|
|
show();
|
|
}
|
|
QMainWindow::keyReleaseEvent(event);
|
|
}
|
|
|
|
void MainWindow::OnStop() {
|
|
m_isPrinting = false;
|
|
}
|
|
|
|
void MainWindow::paintEvent(QPaintEvent* event) {
|
|
QMainWindow::paintEvent(event);
|
|
|
|
|
|
QPainter painter(this);
|
|
const QRect& r = rect();
|
|
painter.drawImage(r, videoImage_);
|
|
if (!m_isPrinting || videoImage_.isNull()) {
|
|
painter.drawPixmap(r, m_defaultImage, m_defaultImage.rect());
|
|
return;
|
|
}
|
|
|
|
painter.drawImage(r, videoImage_, videoImage_.rect());
|
|
}
|