RecordControlApplication/Menu.cpp

387 lines
11 KiB
C++
Raw Normal View History

2024-08-12 11:26:42 +08:00
#include "Menu.h"
#include "Channel.h"
2024-08-12 11:26:42 +08:00
#include "Constant.h"
#include "DatabaseManager.h"
#include "Log.h"
#include "Tool.h"
2024-08-12 11:26:42 +08:00
#include "ui_Menu.h"
#include <QDateTime>
#include <QDesktopWidget>
#include <QListView>
#include <QScreen>
2024-08-12 11:26:42 +08:00
#include <QTimer>
extern QList<Channel*> channelList;
2024-08-12 11:26:42 +08:00
Menu::Menu(QWidget* parent)
: QWidget(parent)
, FocusWindow()
, ui(new Ui::Menu)
{
ui->setupUi(this);
setAttribute(Qt::WA_WState_WindowOpacitySet);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setWindowOpacity(0.5);
setFixedSize(800, 600);
ui->stackedWidget->setCurrentIndex(0);
ui->btnChannelSetting->setFocus();
ui->cmbYear->setView(new QListView());
ui->cmbMonth->setView(new QListView());
ui->cmbDay->setView(new QListView());
ui->cmbResolutionA->setView(new QListView());
ui->cmbResolutionB->setView(new QListView());
ui->cmbResolutionInA->setView(new QListView());
ui->cmbResolutionInB->setView(new QListView());
ui->timeSlider->setOpacity(0.5);
2024-08-12 11:26:42 +08:00
// 默认设置当前操作选择主通道
curSelectChannel = DatabaseManager::MainChannel;
2024-08-12 11:26:42 +08:00
db = DatabaseManager::getInstace();
if (!db->open())
return;
// 设置ScrollArea为栅格布局
2024-08-12 11:26:42 +08:00
connect(ui->cmbYear, &QComboBox::currentTextChanged, [=](QString text) {
renderComboBoxMonth();
});
connect(ui->cmbMonth, &QComboBox::currentTextChanged, [=](QString text) {
renderComboBoxDay();
});
for (const auto& chn : channelList) {
connect(chn, SIGNAL(appendOneVideo()), this, SLOT(onAppendVideo()));
}
renderComboBoxYear();
renderTimeSlider();
2024-08-12 11:26:42 +08:00
}
Menu::~Menu()
{
delete ui;
}
/**
* @brief
* @param visible
*/
void Menu::setChannelSelectVisible(bool visible)
{
channelSelectVisible = visible;
2024-08-12 11:26:42 +08:00
ui->widget_chnSelect->setVisible(visible);
}
/**
* @brief
*/
void Menu::renderComboBoxYear()
{
QStringList years = db->getAllYears(curSelectChannel);
2024-08-12 11:26:42 +08:00
ui->cmbYear->clear();
for (auto& str : years) {
ui->cmbYear->addItem(str);
}
ui->cmbYear->setCurrentIndex(0);
}
/**
* @brief
*/
void Menu::renderComboBoxMonth()
{
QString year = ui->cmbYear->currentText();
if (year.isEmpty()) {
return;
}
QStringList months = db->getAllMonths(curSelectChannel, year);
2024-08-12 11:26:42 +08:00
ui->cmbMonth->clear();
for (auto& str : months) {
ui->cmbMonth->addItem(str);
}
ui->cmbMonth->setCurrentIndex(0);
}
/**
* @brief
*/
void Menu::renderComboBoxDay()
{
QString year = ui->cmbYear->currentText();
QString month = ui->cmbMonth->currentText();
if (year.isEmpty() || year.isEmpty()) {
return;
}
QStringList days = db->getAllDays(curSelectChannel, year, month);
2024-08-12 11:26:42 +08:00
ui->cmbDay->clear();
for (auto& str : days) {
ui->cmbDay->addItem(str);
}
ui->cmbDay->setCurrentIndex(0);
}
/**
* @brief
* @param direction
*/
void Menu::move(Direction direction)
{
QWidget* focusWidget = QApplication::focusWidget();
if (!focusWidget) {
return;
}
2024-08-12 11:26:42 +08:00
// 当前焦点是否在展开后的ComboBox上
bool isCmbPopup = (strcmp(focusWidget->metaObject()->className(), "QListView") == 0);
bool isTimeSlider = focusWidget->objectName() == "timeSlider";
2024-08-12 11:26:42 +08:00
// 下拉框展开,则只进行上下选择
if (isCmbPopup) {
// 随便赋值一个不影响功能的按键
Qt::Key key = Qt::Key_Right;
if (direction == Up) {
key = Qt::Key_Up;
2024-08-12 11:26:42 +08:00
} else if (direction == Down) {
key = Qt::Key_Down;
2024-08-12 11:26:42 +08:00
}
QKeyEvent pressEvent(QEvent::KeyPress, key, Qt::NoModifier);
QApplication::sendEvent(focusWidget, &pressEvent);
QKeyEvent releaseEvent(QEvent::KeyRelease, key, Qt::NoModifier);
QApplication::sendEvent(focusWidget, &releaseEvent);
} else if (isTimeSlider) {
switch (direction) {
case Left:
if (curSegmentIndex > 0) {
curSegmentIndex--;
int cur = segments[curSegmentIndex].startTime;
ui->timeSlider->setCurrent(cur);
}
break;
case Right:
if (curSegmentIndex < segments.length() - 1) {
curSegmentIndex++;
int cur = segments[curSegmentIndex].startTime;
ui->timeSlider->setCurrent(cur);
}
break;
case Up:
case Down:
focusNext(direction);
break;
default:
break;
}
2024-08-12 11:26:42 +08:00
} else {
focusNext(direction);
focusWidget = QApplication::focusWidget();
if (focusWidget == ui->btnChannelSetting) {
ui->btnChannelSetting->setChecked(true);
ui->stackedWidget->setCurrentIndex(0);
} else if (focusWidget == ui->btnPlayback) {
ui->btnPlayback->setChecked(true);
ui->stackedWidget->setCurrentIndex(1);
}
}
}
/**
* @brief
*/
void Menu::confirm()
{
QWidget* focusWidget = QApplication::focusWidget();
// 当前操作的是按钮
if (strcmp(focusWidget->metaObject()->className(), "QPushButton") == 0) {
2024-08-12 11:26:42 +08:00
QPushButton* btn = qobject_cast<QPushButton*>(focusWidget);
if (btn) {
// 选择通道输出类型
if (btn == ui->btn_hdmi_1) {
btn->setChecked(true);
emit btnHdmi1Checked();
} else if (btn == ui->btn_hdmi_2) {
btn->setChecked(true);
emit btnHdmi2Checked();
} else if (btn == ui->btn_vga_1) {
btn->setChecked(true);
emit btnVga1Checked();
} else if (btn == ui->btn_vga_2) {
btn->setChecked(true);
emit btnVga2Checked();
2024-08-12 11:26:42 +08:00
}
// 选择通道
else if (btn == ui->btn_channel_1 || btn == ui->btn_channel_2) {
btn->setChecked(true);
curSelectChannel = btn == ui->btn_channel_1 ? DatabaseManager::MainChannel
: DatabaseManager::SecondaryChannel;
renderComboBoxYear();
2024-08-12 11:26:42 +08:00
}
}
}
// 当前操作下拉框
else if (strcmp(focusWidget->metaObject()->className(), "QComboBox") == 0) {
QComboBox* cmb = qobject_cast<QComboBox*>(focusWidget);
cmb->showPopup();
} else if (strcmp(focusWidget->metaObject()->className(), "QListView") == 0) {
// QComboBox的层级
// QComboBox
// |- QComboBoxPrivateContainer(Popup widget)
// |- QListView(or QListWidget)
// |-QViewPort(Child of QListView)
QListView* listView = static_cast<QListView*>(focusWidget);
QComboBox* cmb = static_cast<QComboBox*>(listView->parent()->parent());
if (cmb) {
cmb->setCurrentIndex(listView->currentIndex().row());
cmb->hidePopup();
}
}
// 选中进度条
else if (focusWidget->objectName() == "timeSlider") {
QVariantMap params;
params["channel"] = curSelectChannel;
params["year"] = ui->cmbYear->currentText();
params["month"] = ui->cmbMonth->currentText();
params["day"] = ui->cmbDay->currentText();
emit btnPlaybackClicked(params, curSegmentIndex);
hide();
}
// 确认按钮按下确认
else if (focusWidget->objectName() == "btn_done") {
renderTimeSlider();
2024-08-12 11:26:42 +08:00
}
}
/**
* @brief
*/
QList<QWidget*> Menu::getAllFocusabelWidget()
{
QList<QWidget*> list = findChildren<QWidget*>();
QList<QWidget*> result;
for (int i = 0; i < list.length(); i++) {
QWidget* w = list.at(i);
if (w->focusPolicy() == Qt::NoFocus || !w->isVisible())
continue;
else {
result.push_back(w);
}
}
return result;
}
/**
* @brief
* @return
*/
DatabaseManager::Channel Menu::getCurPlayChannel()
{
return curSelectChannel;
}
/**
* @brief
*/
void Menu::renderTimeSlider()
{
QVariantMap params;
params["channel"] = curSelectChannel;
params["year"] = ui->cmbYear->currentText();
params["month"] = ui->cmbMonth->currentText();
params["day"] = ui->cmbDay->currentText();
QList<TimeSlider::TimeSegment> segs = Tool::calTimeSegments(params);
if (segs.isEmpty()) {
return;
}
ui->timeSlider->setTimeSegments(segs);
ui->timeSlider->setCurrent(segs.front().startTime);
segments = segs;
curSegmentIndex = 0;
}
/**
* @brief
*/
void Menu::onAppendVideo()
{
Channel* chn = static_cast<Channel*>(sender());
// 一路回放
if (ui->widget_chnSelect->isVisible()) {
DatabaseManager::Channel c = static_cast<DatabaseManager::Channel>(curSelectChannel);
QString channelName = c == DatabaseManager::MainChannel
? Constant::MainChannel
: Constant::SecondaryChannel;
if (chn->channelName != channelName) {
return;
}
}
// 两路回放
else {
if (chn->channelName != Constant::MainChannel) {
return;
}
}
// 刷新时间轴中时间片的显示
QVariantMap params;
params["channel"] = curSelectChannel;
params["year"] = ui->cmbYear->currentText();
params["month"] = ui->cmbMonth->currentText();
params["day"] = ui->cmbDay->currentText();
segments = Tool::calTimeSegments(params);
ui->timeSlider->setTimeSegments(segments);
}
void Menu::on_cmbResolutionB_currentIndexChanged(int index)
{
emit resolutionOutBChanged(index);
}
void Menu::on_cmbResolutionA_currentIndexChanged(int index)
{
emit resolutionOutAChanged(index);
}
void Menu::moveToCenter()
{
QWidget* p = static_cast<QWidget*>(parent());
if (p) {
// 获取父窗体的位置和尺寸
QRect parentGeometry = p->geometry();
// 计算子窗体的居中位置
int x = parentGeometry.left() + (parentGeometry.width() - width()) / 2;
int y = parentGeometry.top() + (parentGeometry.height() - height()) / 2;
// 移动子窗体到父窗体的中心
QWidget::move(x, y);
}
}
void Menu::show()
{
QWidget::show();
moveToCenter();
ui->btnChannelSetting->setFocus();
}
Menu* Menu::restartUI()
{
Menu* menu = new Menu();
close();
menu->show();
return menu;
}
void Menu::on_cmbResolutionInA_currentIndexChanged(const QString& text)
{
QStringList list = text.split("x");
if (list.count() == 2) {
emit resolutionInAChanged(list[0].toInt(), list[1].toInt());
}
}
void Menu::on_cmbResolutionInB_currentIndexChanged(const QString& text)
{
QStringList list = text.split("x");
if (list.count() == 2) {
emit resolutionInAChanged(list[0].toInt(), list[1].toInt());
}
}