346 lines
9.3 KiB
C++
346 lines
9.3 KiB
C++
#include "MainWindow.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QVBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPainter>
|
|
#include <QCoreApplication>
|
|
|
|
#include "irrKlang.h"
|
|
|
|
#include "FileHelper.h"
|
|
#include "ImageLabel.h"
|
|
#include "MainServer.h"
|
|
|
|
#include "HomeScreen.h"
|
|
#include "HomeWidget.h"
|
|
#include "DetailedTown.h"
|
|
#include "ContentWidget.h"
|
|
#include "TokenRequest.h"
|
|
|
|
#include "ui_MainWindow.h"
|
|
|
|
MainWindow* s_instance { nullptr };
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
//setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
|
|
bool success = FileHelper::GetItemFromJsonConfig(&m_config);
|
|
if (!success) {
|
|
QMessageBox::warning(this, tr("error"), tr("read config failed"));
|
|
qApp->quit();
|
|
}
|
|
|
|
//m_mainServer = new QMainServer(this);
|
|
//if (!m_mainServer->Start(m_config.port)) {
|
|
// QMessageBox::warning(this, tr("error"), tr("config failed"));
|
|
// qApp->quit();
|
|
//}
|
|
const QString address = QString("%1:%2").arg(m_config.ip).arg(m_config.port);
|
|
if (!zmqClient_.ConnectToServer(address)) {
|
|
qDebug() << "QMainServer::Start listen failed" << m_config.port;
|
|
return;
|
|
}
|
|
|
|
assert(nullptr == s_instance);
|
|
s_instance = this;
|
|
|
|
m_tokenRequest = new TokenRequest(this);
|
|
|
|
soundEngine_ = irrklang::createIrrKlangDevice();
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
if (nullptr != soundEngine_) {
|
|
soundEngine_->drop();
|
|
soundEngine_ = nullptr;
|
|
}
|
|
|
|
assert(nullptr != s_instance);
|
|
s_instance = nullptr;
|
|
|
|
//m_mainServer->Stop();
|
|
|
|
delete ui;
|
|
}
|
|
|
|
MainWindow& MainWindow::Get() {
|
|
return *s_instance;
|
|
}
|
|
|
|
bool MainWindow::Initialize() {
|
|
|
|
PageBaseWidget* widget = new HomeScreen(this);
|
|
InitializePageWidget(widget);
|
|
|
|
widget = new HomeWidget(this);
|
|
InitializePageWidget(widget);
|
|
|
|
widget = new DetailedTown(this);
|
|
InitializePageWidget(widget);
|
|
|
|
widget = new ContentWidget(this);
|
|
InitializePageWidget(widget);
|
|
|
|
ShowHomeScreen();
|
|
PlayBackgroundMovie();
|
|
return true;
|
|
}
|
|
|
|
bool MainWindow::SaveConfig() {
|
|
return FileHelper::SaveItemToJsonConfig(&m_config);
|
|
}
|
|
|
|
void MainWindow::ShowHomeScreen() {
|
|
SwitchToPage(PageBaseWidget::PageType::PT_HomeScreen);
|
|
}
|
|
|
|
void MainWindow::ShowDetailedTown(DetailedType dt) {
|
|
PageBaseWidget::PageType pageType = PageBaseWidget::PageType::PT_DetailedTown;
|
|
|
|
DetailedTown* widget = reinterpret_cast<DetailedTown*>(m_pageWidgets[pageType]);
|
|
widget->SetCurrent(dt);
|
|
SwitchToPage(pageType);
|
|
}
|
|
|
|
bool MainWindow::SwitchToPage(PageBaseWidget::PageType pageType) {
|
|
if (!m_pageWidgets.contains(pageType)) {
|
|
return false;
|
|
}
|
|
|
|
HideAllWidget();
|
|
PageBaseWidget* widget = m_pageWidgets[pageType];
|
|
widget->Use();
|
|
|
|
return true;
|
|
}
|
|
|
|
void MainWindow::ShowContentWidget(int32_t detailedType, const QString& btnName) {
|
|
auto itor = m_config.datas.find(detailedType);
|
|
if (m_config.datas.end() == itor) {
|
|
qDebug() << __FUNCTION__ << " " << detailedType << " not find";
|
|
return;
|
|
}
|
|
|
|
const MedianItem& mediaItem = *itor;
|
|
auto infoItor = mediaItem.datas.find(btnName);
|
|
if (mediaItem.datas.end() == infoItor) {
|
|
qDebug() << __FUNCTION__ << " " << btnName << " not find";
|
|
return;
|
|
}
|
|
|
|
const MediaInfo& medieInfo = *infoItor;
|
|
if (medieInfo.name.isEmpty() && medieInfo.describe.isEmpty() && medieInfo.images.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
ContentWidget* widget = dynamic_cast<ContentWidget*>(m_pageWidgets[PageBaseWidget::PageType::PT_ContentWidget]);
|
|
if (nullptr == widget) {
|
|
qDebug() << __FUNCTION__ << " widget is nullptr";
|
|
return;
|
|
}
|
|
|
|
widget->SetContent(detailedType, btnName, medieInfo.name, medieInfo.describe, medieInfo.images);
|
|
SwitchToPage(PageBaseWidget::PageType::PT_ContentWidget);
|
|
}
|
|
|
|
bool MainWindow::GetMediaInfo(int32_t detailedType, const QString& btnName, MediaInfo& item) const {
|
|
if (!m_config.datas.contains(detailedType)) {
|
|
qDebug() << "not contains " << detailedType;
|
|
return false;
|
|
}
|
|
|
|
const auto& medianItems = m_config.datas[detailedType];
|
|
if (!medianItems.datas.contains(btnName)) {
|
|
qDebug() << "medianItems not contains " << btnName;
|
|
return false;
|
|
}
|
|
|
|
item = medianItems.datas[btnName];
|
|
return true;
|
|
}
|
|
|
|
MediaInfo* MainWindow::GetMediaInfo(int32_t detailedType, const QString& btnName) {
|
|
if (!m_config.datas.contains(detailedType)) {
|
|
qDebug() << "not contains " << detailedType;
|
|
return nullptr;
|
|
}
|
|
|
|
auto& medianItems = m_config.datas[detailedType];
|
|
if (!medianItems.datas.contains(btnName)) {
|
|
qDebug() << "medianItems not contains " << btnName;
|
|
return nullptr;
|
|
}
|
|
|
|
return &medianItems.datas[btnName];
|
|
}
|
|
|
|
bool MainWindow::UpdateConfigLandmark(int32_t detailedType, const QString& btnName, const QPoint& pos) {
|
|
if (!m_config.datas.contains(detailedType)) {
|
|
qDebug() << "not contains " << detailedType;
|
|
return false;
|
|
}
|
|
|
|
auto medianItems = m_config.datas[detailedType];
|
|
if (!medianItems.datas.contains(btnName)) {
|
|
qDebug() << "medianItems not contains " << btnName;
|
|
return false;
|
|
}
|
|
|
|
MediaInfo& info = medianItems.datas[btnName];
|
|
info.landmark = pos;
|
|
return true;
|
|
}
|
|
|
|
void MainWindow::SendCmd(const QString& cmd) {
|
|
/* Q_ASSERT(nullptr != m_mainServer);
|
|
|
|
m_mainServer->BroadcastCmd(cmd);*/
|
|
zmqClient_.Send(cmd);
|
|
}
|
|
|
|
const QString MainWindow::GetDetailedTypeName(DetailedType dt) const {
|
|
switch (dt) {
|
|
case DetailedType::DT_Matian:
|
|
return "麻田镇";
|
|
case DetailedType::DT_Yangjiao:
|
|
return "羊角乡";
|
|
case DetailedType::DT_Hanwang:
|
|
return "寒王乡";
|
|
case DetailedType::DT_Qinquan:
|
|
return "芹泉镇";
|
|
case DetailedType::DT_Guaier:
|
|
return "拐儿镇";
|
|
case DetailedType::DT_Shixia:
|
|
return "石匣乡";
|
|
case DetailedType::DT_Liaoyang:
|
|
return "辽阳镇";
|
|
case DetailedType::DT_Tongyu:
|
|
return "桐峪镇";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
bool MainWindow::PlayAudio() {
|
|
if (nullptr == soundEngine_) {
|
|
qDebug() << __FUNCTION__ << "soundEngine_ is nullptr";
|
|
return false;
|
|
}
|
|
|
|
static QString path = QApplication::applicationDirPath() + "/resouce/audio/button.mp3";
|
|
std::string audio = path.toStdString();
|
|
return nullptr != soundEngine_->play2D(audio.c_str());
|
|
}
|
|
|
|
void MainWindow::mousePressEvent(QMouseEvent* event) {
|
|
qint64 elapsed = elapsedTimer.elapsed();
|
|
|
|
constexpr qint64 sec = 1000;
|
|
constexpr qint32 count = 6;
|
|
|
|
if (elapsed > sec)
|
|
{
|
|
clickCount = 0;
|
|
elapsedTimer.restart();
|
|
}
|
|
|
|
//if (event->button() == Qt::LeftButton)
|
|
//{
|
|
// clickCount++;
|
|
|
|
// if (clickCount == count)
|
|
// {
|
|
// // 在这里执行触发功能的代码
|
|
// qDebug() << "Clicked 6 times within 1 second!";
|
|
// GenertorConfig();
|
|
// clickCount = 0;
|
|
// elapsedTimer.restart();
|
|
// }
|
|
//}
|
|
|
|
QMainWindow::mousePressEvent(event);
|
|
}
|
|
|
|
void MainWindow::closeEvent(QCloseEvent* event) {
|
|
QMessageBox messaBox(QMessageBox::Question, "询问", "确定关闭系统?", QMessageBox::Yes | QMessageBox::No, this);
|
|
messaBox.setButtonText(QMessageBox::Yes, "确定");
|
|
messaBox.setButtonText(QMessageBox::No, "取消");
|
|
int reply = messaBox.exec();
|
|
if (reply == QMessageBox::Yes) {
|
|
event->accept();
|
|
} else {
|
|
event->ignore();
|
|
}
|
|
return;
|
|
}
|
|
|
|
void MainWindow::InitializePageWidget(PageBaseWidget* widget) {
|
|
m_pageWidgets.insert(widget->GetPageType(), widget);
|
|
widget->hide();
|
|
ui->mainLayout->addWidget(widget);
|
|
}
|
|
|
|
void MainWindow::HideAllWidget() {
|
|
for (auto& item : m_pageWidgets) {
|
|
item->hide();
|
|
}
|
|
|
|
}
|
|
|
|
void MainWindow::GenertorConfig() {
|
|
FileHelper::GeneratJsonConfig() ?
|
|
QMessageBox::information(this, tr("notify"), tr("generator config success")) :
|
|
QMessageBox::warning(this, tr("notify"), tr("generator config failed"));
|
|
}
|
|
|
|
void MainWindow::ShowItemImages() {
|
|
QVBoxLayout* layout = new QVBoxLayout();
|
|
|
|
//QString resourcePath = FileHelper::GetResoucePath();
|
|
//for (auto it = items.constBegin(); it != items.constEnd(); ++it) {
|
|
// QString path = QString("%1/%2").arg(resourcePath).arg(it.value());
|
|
// QPixmap pixmap(path);
|
|
|
|
// QLabel* label = new ImageLabel(it.key(), m_mainServer, pixmap.scaled(200, 200, Qt::KeepAspectRatio));
|
|
// layout->addWidget(label);
|
|
//}
|
|
|
|
//QWidget* centralWidget = new QWidget();
|
|
//centralWidget->setLayout(layout);
|
|
//setCentralWidget(centralWidget);
|
|
}
|
|
|
|
void MainWindow::PlayBackgroundMovie() {
|
|
connect(&vlcPlayer_, &VlcMediaListPlayer::VideoDataOutput, this, &MainWindow::slotSetOneFrame, Qt::QueuedConnection);
|
|
connect(&videoUpdate_, &QTimer::timeout, [this]() {update(); });
|
|
videoUpdate_.start(30);
|
|
|
|
vlcPlayer_.SetPlayMode(VlcMediaListPlayer::Loop);
|
|
|
|
const QString filePath = QCoreApplication::applicationDirPath() + "/video/background.mp4";
|
|
QStringList paths;
|
|
paths.append(filePath);
|
|
vlcPlayer_.SetMediaList(paths);
|
|
vlcPlayer_.Play();
|
|
}
|
|
|
|
void MainWindow::paintEvent(QPaintEvent* event) {
|
|
QMainWindow::paintEvent(event);
|
|
|
|
const QRect& r = rect();
|
|
QPainter painter(this);
|
|
if (videoImage_.isNull()) {
|
|
return;
|
|
}
|
|
|
|
painter.drawImage(r, videoImage_, videoImage_.rect());
|
|
}
|