You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
VideoClient98/src/mainwindow.cpp

537 lines
19 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "mainwindow.h"
// #include <QWKWidgets/qwkwidgetsglobal.h>
#include <QWKWidgets/widgetwindowagent.h>
#include <widgetframe/windowbar.h>
#include <widgetframe/windowbutton.h>
#include <QList>
#include <QtGui/QPainter>
#include "ui_mainwindow.h"
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <QtGui/QActionGroup>
#else
#include <QtWidgets/QActionGroup>
#endif
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// this->setWindowTitle("载荷视频播放软件");
installWindowAgent();
loadStyleSheet(Dark);
// apply the qss
QFile qssfile(":/Qss/qss.qss");
if (qssfile.open(QFile::ReadOnly)) {
QString style = QLatin1String(qssfile.readAll());
qApp->setStyleSheet(style);
qssfile.close();
} else {
qDebug() << "Open file fail " << Qt::endl;
}
// QList<QString> list;
// list.append("rtmp://liteavapp.qcloud.com/live/liteavdemoplayerstreamid");
// list.append("rtsp://182.92.130.23/nmyj/video");
// list.append("rtmp://182.92.130.23/nmyj/video");
// list.append("rtmp://ns8.indexforce.com/home/mystream");
// // ui->videoWidget->udpPlay("172.10.1.199", 13001);
// ui->videoWidget->setVedioSaveFileDirPath("./video");
// ui->videoWidget->play(list.at(1));
// ui->videoWidget->pushStream("udp://172.10.1.199:8009");
QString urlFile = "networkSettingInfo.ini";
QFile file(urlFile);
if (!file.exists()) {
file.open(QIODevice::WriteOnly);
file.close();
}
g_networkSettingInfo =
new QSettings("networkSettingInfo.ini", QSettings::IniFormat);
initSignalConnection();
initNotifyManager();
initNotifyMessageConnection();
initChangeVideoLayoutConnection();
initHideButton();
setSavedVideoDir();
QApplication::instance()->installEventFilter(this);
// setFocusPolicy( Qt::StrongFocus ); // 窗口将通过鼠标点击、按tab键等方式获取焦点
// ui->stackedWidget->setCurrentIndex(1);
// ui->videoWidget1->play(list.at(0));
// ui->videoWidget2->play(list.at(0));
// ui->videoWidget3->play(list.at(0));
// ui->videoWidget4->play(list.at(0));
}
MainWindow::~MainWindow() {
QProcess m_process;
m_process.setWorkingDirectory(QCoreApplication::applicationDirPath()+"/nginx");
m_process.start("cmd.exe",QStringList()<<"/c"<<"nginx -s stop");
m_process.waitForFinished();
delete ui;
}
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message,
qintptr *result) {
#ifdef Q_OS_WIN
MSG *msg = (MSG *)message;
if (msg->message == WM_SYSCOMMAND) {
if (61587 == msg->wParam) { // 单击事件
return true;
}
if (61539 == msg->wParam) { // 双击事件
return true;
}
}
#endif
return QMainWindow::nativeEvent(eventType, message, result);
}
void MainWindow::paintEvent(QPaintEvent *ev) {
int offset = 0;
if (ui->commandWidget->isHidden()) {
offset = hideCommandWidgetBtn->width() / 2;
}
hideCommandWidgetBtn->move(ui->stackedWidget->width() - offset,
ui->commandWidget->height() / 2);
}
void MainWindow::closeEvent(QCloseEvent *event) {
if (m_mapWidget) {
m_mapWidget->close();
delete m_mapWidget;
}
if(m_dataServer){
m_dataServer->close();
delete m_dataServer;
}
// QMainWindow::close();
}
void MainWindow::initSignalConnection() {
connect(ui->videoControlWidget, &VideoControl::startConnectionSignal,
ui->videoWidget, &VideoWidget::udpPlay, Qt::UniqueConnection);
connect(ui->videoControlWidget, &VideoControl::stopConnectionSignal,
ui->videoWidget, &VideoWidget::stopPlay, Qt::UniqueConnection);
connect(&streamAddrSettingsDlg,
&StreamAddrSettingsDlg::startPullStreamSignal, this,
&MainWindow::pullStreamSlot);
connect(&streamAddrSettingsDlg,
&StreamAddrSettingsDlg::startPushStreamSignal, this,
&MainWindow::pushStreamSlot);
connect(ui->videoControlWidget, &VideoControl::stopVideoSignal, this,
&MainWindow::closeVideoSlot);
}
void MainWindow::initNotifyMessageConnection() {
connect(this, &MainWindow::sendErrorMessage, this,
&MainWindow::showMessageSlots);
connect(ui->videoWidget, &VideoWidget::sendErrorMessageSignal, this,
&MainWindow::showMessageSlots);
connect(ui->commandWidget, &CommandWidget::sendErrorMessage, this,
&MainWindow::showMessageSlots);
}
void MainWindow::initChangeVideoLayoutConnection() {
connect(ui->videoControlWidget, &VideoControl::changeVideoLayout, this,
[=](int index) {
ui->stackedWidget->setCurrentIndex(index);
});
}
void MainWindow::initNotifyManager() {
m_notifyManager = new NotifyManager(this, this);
m_notifyManager->setMaxCount(5);
m_notifyManager->setDisplayTime(2000);
m_notifyManager->setNotifyWndSize(400, 60);
}
void MainWindow::showMessageSlots(QString message, int type) {
if (m_notifyManager) {
m_notifyManager->notify(message, "", type, 3000);
}
}
void MainWindow::installWindowAgent() {
// 1. Setup window agent
windowAgent = new QWK::WidgetWindowAgent(this);
windowAgent->setup(this);
auto menuBar = new QMenuBar(this);
// QMenu *settingmenu =
// menuBar->addMenu(QIcon(":/images/settings1.png"), "设置");
QAction *settingAction = new QAction(this);
settingAction->setIcon(QIcon(":/images/settings1.png"));
settingAction->setIconText("设置");
menuBar->addAction(settingAction);
connect(settingAction, &QAction::triggered, this,
&MainWindow::showSettingDlgSlot);
QAction *playBackAction = new QAction(this);
playBackAction->setIcon(QIcon(":/images/playback.png"));
playBackAction->setIconText("回放");
menuBar->addAction(playBackAction);
connect(playBackAction, &QAction::triggered, this,
&MainWindow::openSavedVideoDirSlot);
QAction *pushStreamAction = new QAction(this);
pushStreamAction->setIcon(QIcon(":/images/pushstream.png"));
pushStreamAction->setIconText("推流");
menuBar->addAction(pushStreamAction);
connect(pushStreamAction, &QAction::triggered, this,
&MainWindow::showStreamSettingsDlgSlot);
QAction *showMap3DAction = new QAction(this);
showMap3DAction->setIcon(QIcon(":/images/map3D.png"));
showMap3DAction->setIconText("推流");
menuBar->addAction(showMap3DAction);
connect(showMap3DAction, &QAction::triggered, this,
&MainWindow::showMap3DSlot);
menuBar->setObjectName(QStringLiteral("win-menu-bar"));
auto titleLabel = new QLabel();
titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setObjectName(QStringLiteral("win-title-label"));
#ifndef Q_OS_MAC
auto iconButton = new QWK::WindowButton();
iconButton->setEnabled(false);
iconButton->setObjectName(QStringLiteral("icon-button"));
iconButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
auto pinButton = new QWK::WindowButton();
pinButton->setCheckable(true);
pinButton->setObjectName(QStringLiteral("pin-button"));
pinButton->setProperty("system-button", true);
pinButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
auto minButton = new QWK::WindowButton();
minButton->setObjectName(QStringLiteral("min-button"));
minButton->setProperty("system-button", true);
minButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
auto maxButton = new QWK::WindowButton();
maxButton->setCheckable(true);
maxButton->setObjectName(QStringLiteral("max-button"));
maxButton->setProperty("system-button", true);
maxButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
auto closeButton = new QWK::WindowButton();
closeButton->setObjectName(QStringLiteral("close-button"));
closeButton->setProperty("system-button", true);
closeButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
#endif
auto windowBar = new QWK::WindowBar();
#ifndef Q_OS_MAC
windowBar->setIconButton(iconButton);
windowBar->setPinButton(pinButton);
windowBar->setMinButton(minButton);
windowBar->setMaxButton(maxButton);
windowBar->setCloseButton(closeButton);
#endif
windowBar->setMenuBar(menuBar);
windowBar->setTitleLabel(titleLabel);
windowBar->setHostWidget(this);
windowAgent->setTitleBar(windowBar);
#ifndef Q_OS_MAC
windowAgent->setHitTestVisible(pinButton, true);
windowAgent->setSystemButton(QWK::WindowAgentBase::WindowIcon, iconButton);
windowAgent->setSystemButton(QWK::WindowAgentBase::Minimize, minButton);
windowAgent->setSystemButton(QWK::WindowAgentBase::Maximize, maxButton);
windowAgent->setSystemButton(QWK::WindowAgentBase::Close, closeButton);
#endif
windowAgent->setHitTestVisible(menuBar, true);
#ifdef Q_OS_MAC
windowAgent->setSystemButtonAreaCallback([](const QSize &size) {
static constexpr const int width = 75;
return QRect(QPoint(size.width() - width, 0),
QSize(width, size.height())); //
});
#endif
setMenuWidget(windowBar);
#ifndef Q_OS_MAC
connect(windowBar, &QWK::WindowBar::pinRequested, this,
[this, pinButton](bool pin) {
if (isHidden() || isMinimized() || isMaximized() ||
isFullScreen()) {
return;
}
setWindowFlag(Qt::WindowStaysOnTopHint, pin);
show();
pinButton->setChecked(pin);
});
connect(windowBar, &QWK::WindowBar::minimizeRequested, this,
&QWidget::showMinimized);
connect(windowBar, &QWK::WindowBar::maximizeRequested, this,
[this, maxButton](bool max) {
if (max) {
showMaximized();
} else {
showNormal();
}
// It's a Qt issue that if a QAbstractButton::clicked triggers a
// window's maximization, the button remains to be hovered until
// the mouse move. As a result, we need to manually send leave
// events to the button.
// emulateLeaveEvent(maxButton);
});
connect(windowBar, &QWK::WindowBar::closeRequested, this, &QWidget::close);
#endif
}
void MainWindow::loadStyleSheet(Theme theme) {
if (!styleSheet().isEmpty() && theme == currentTheme) return;
currentTheme = theme;
if (QFile qss(theme == Dark ? QStringLiteral(":/Qss/dark-style.qss")
: QStringLiteral(":/Qss/light-style.qss"));
qss.open(QIODevice::ReadOnly | QIODevice::Text)) {
setStyleSheet(QString::fromUtf8(qss.readAll()));
Q_EMIT themeChanged();
}
}
void MainWindow::setSavedVideoDir() {
ui->videoWidget->setVedioSaveFileDirPath("./video/main");
ui->videoWidget1->setVedioSaveFileDirPath("./video/video1");
ui->videoWidget2->setVedioSaveFileDirPath("./video/video2");
ui->videoWidget3->setVedioSaveFileDirPath("./video/video3");
ui->videoWidget4->setVedioSaveFileDirPath("./video/video4");
}
void MainWindow::initHideButton() {
hideCommandWidgetBtn = new QToolButton(this);
hideCommandWidgetBtn->setIcon(QIcon(":/images/right.png"));
hideCommandWidgetBtn->setIconSize(QSize(24, 24));
hideCommandWidgetBtn->setFixedSize(30, 30);
connect(hideCommandWidgetBtn, &QToolButton::clicked, this,
&MainWindow::hideCommandWidgetSlot);
}
void MainWindow::showSettingDlgSlot() {
settingDlg.exec();
}
void MainWindow::showStreamSettingsDlgSlot() {
streamAddrSettingsDlg.exec();
}
void MainWindow::hideCommandWidgetSlot() {
if (ui->commandWidget->isHidden()) {
ui->commandWidget->show();
hideCommandWidgetBtn->setIcon(QIcon(":/images/right.png"));
} else {
ui->commandWidget->hide();
hideCommandWidgetBtn->setIcon(QIcon(":/images/left.png"));
}
}
void MainWindow::openSavedVideoDirSlot() {
// QDesktopServices::openUrl(
// QUrl(QApplication::applicationDirPath() + "/video"));
// QFileDialog::Options options = QFileDialog::DontUseNativeDialog;
QString videoDirPath = QApplication::applicationDirPath() + "/video/";
QString filePath = QFileDialog::getOpenFileName(
this, "选择文件", videoDirPath, "*.ts", nullptr);
if (!filePath.isEmpty()) {
QString fileName = filePath;
qDebug() << fileName;
if (ui->videoWidget->play(filePath, false)) {
int duration = ui->videoWidget->getVideoDuration();
if (duration > 0) {
ui->videoControlWidget->updateVideoDuration(duration);
connect(ui->videoWidget,
&VideoWidget::updateVideoCurrentTimeSignal, this,
[=](int currentTime, int duration) {
ui->videoControlWidget->updateVideoSlider(
currentTime, duration);
});
connect(ui->videoControlWidget,
&VideoControl::seekToVideoSignal, ui->videoWidget,
&VideoWidget::seekToVideo, Qt::UniqueConnection);
connect(ui->videoControlWidget,
&VideoControl::changePlaySpeedSignal, ui->videoWidget,
&VideoWidget::setPlaySpeed, Qt::UniqueConnection);
}
}
}
}
// 拉流
void MainWindow::pullStreamSlot(bool bPull) {
if (!g_networkSettingInfo) return;
if (bPull) {
// QString podPullStreamIP =
// g_networkSettingInfo->value("NetworkStreamSettings/podPullAddress")
// .toString();
// QString pullStreamIP1 =
// g_networkSettingInfo->value("NetworkStreamSettings/pullAddress1")
// .toString();
// QString pullStreamIP2 =
// g_networkSettingInfo->value("NetworkStreamSettings/pullAddress2")
// .toString();
// QString pullStreamIP3 =
// g_networkSettingInfo->value("NetworkStreamSettings/pullAddress3")
// .toString();
// QString pullStreamIP4 =
// g_networkSettingInfo->value("NetworkStreamSettings/pullAddress4")
// .toString();
if (!ui->videoWidget->getPlayStatus()) {
ui->videoWidget->play(g_pullStreamInfoStruct.podPullIP);
} else {
emit sendErrorMessage("吊舱视频正在播放,请先暂停!",
NotifyType::WARNING);
}
if (!ui->videoWidget1->getPlayStatus()) {
ui->videoWidget1->play(g_pullStreamInfoStruct.pullIP1);
} else {
emit sendErrorMessage("备用视频1正在播放请先暂停!",
NotifyType::WARNING);
}
if (!ui->videoWidget2->getPlayStatus()) {
ui->videoWidget2->play(g_pullStreamInfoStruct.pullIP2);
} else {
emit sendErrorMessage("备用视频2正在播放请先暂停!",
NotifyType::WARNING);
}
if (!ui->videoWidget3->getPlayStatus()) {
ui->videoWidget3->play(g_pullStreamInfoStruct.pullIP3);
} else {
emit sendErrorMessage("备用视频3正在播放请先暂停!",
NotifyType::WARNING);
}
if (!ui->videoWidget4->getPlayStatus()) {
ui->videoWidget4->play(g_pullStreamInfoStruct.pullIP4);
} else {
emit sendErrorMessage("备用视频4正在播放请先暂停!",
NotifyType::WARNING);
}
} else {
ui->videoWidget->stopPlay();
ui->videoWidget1->stopPlay();
ui->videoWidget2->stopPlay();
ui->videoWidget3->stopPlay();
ui->videoWidget4->stopPlay();
}
}
// 推流
void MainWindow::pushStreamSlot(bool bPush) {
if (!g_networkSettingInfo) return;
if (bPush) {
// int uavType =
// g_networkSettingInfo->value("NetworkStreamSettings/uavType")
// .toInt();
// int uavID =
// g_networkSettingInfo->value("NetworkStreamSettings/uavID").toInt();
// int pushStreamIndex =
// g_networkSettingInfo->value("NetworkStreamSettings/pushStreamType")
// .toInt();
// if (2 == pushStreamIndex) {
// QString pushStreamIP =
// g_networkSettingInfo
// ->value("NetworkStreamSettings/pushStreamAddress")
// .toString();
// }
int pushStreamType = g_pushStreamInfoStruct.pushType;
QString pushStreamIP;
if (pushStreamType == 0) { // 公司服务器
pushStreamIP = generatePushURL2(g_pushStreamInfoStruct.uavID,
g_pushStreamInfoStruct.uavType);
qDebug() << pushStreamIP;
} else if (pushStreamType == 1) { // 阿里云服务器
pushStreamIP = generatePushURL(g_pushStreamInfoStruct.uavID,
g_pushStreamInfoStruct.uavType);
qDebug() << pushStreamIP;
} else {
pushStreamIP = g_pushStreamInfoStruct.pushStreamIP;
}
ui->videoWidget->pushStream(pushStreamIP);
} else {
ui->videoWidget->stopPushStream();
// ui->videoWidget1->stopPushStream();
// ui->videoWidget2->stopPushStream();
// ui->videoWidget3->stopPushStream();
// ui->videoWidget4->stopPushStream();
}
}
void MainWindow::closeVideoSlot() {
ui->videoWidget->stopPlay();
ui->videoWidget1->stopPlay();
ui->videoWidget2->stopPlay();
ui->videoWidget3->stopPlay();
ui->videoWidget4->stopPlay();
ui->videoControlWidget->resetVideoSlider();
emit sendErrorMessage("视频流已关闭!", NotifyType::SUCCESS);
}
void MainWindow::showMap3DSlot() {
if(m_dataServer == nullptr){
m_dataServer = new DataServerforMap();
m_dataServer->startServe();
}
if (m_mapWidget == nullptr) {
m_mapWidget = new WebMapWidget();
m_mapWidget->load(QUrl("http://127.0.0.1:80"));
m_mapWidget->resize(1920,1080);
}
if (!m_mapWidget->isVisible()) {
m_mapWidget->show();
}
}
bool MainWindow::eventFilter(QObject* watched, QEvent* event){
if (event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
// 判断是否同时按下ctrl和c键
if( (keyEvent->modifiers()&Qt::ControlModifier) != 0 && keyEvent->key()==Qt::Key_S)
{
//显示网络配置窗口
if(m_dataServer!=nullptr && !m_dataServer->isVisible()){
m_dataServer->show();
}
}
}
return QWidget::eventFilter(watched, event);
}