qt/LedOK/progpanel.cpp

385 lines
17 KiB
C++
Raw Normal View History

2022-08-25 18:37:24 +08:00
#include "progpanel.h"
#include "globaldefine.h"
#include "player/playwin.h"
#include "tipdialog.h"
#include <QApplication>
#include <QHeaderView>
#include <QMessageBox>
#include <QSettings>
ProgPanel::ProgPanel(QWidget *parent) : QWidget(parent) {
setAttribute(Qt::WA_DeleteOnClose);
setAutoFillBackground(true);
QPalette pal;
pal.setBrush(QPalette::Window, QColor(0xeeeeee));
setPalette(pal);
auto vBox = new QVBoxLayout(this);
vBox->setContentsMargins(0, 0, 0, 0);
vBox->setSpacing(0);
auto hBox = new QHBoxLayout();
hBox->setContentsMargins(6,6,6,6);
hBox->setSpacing(6);
vBox->addLayout(hBox);
bnNew = new QPushButton(tr("New"));
bnNew->setFixedSize(88, 38);
bnNew->setProperty("ssType", "progManageTool");
hBox->addWidget(bnNew);
connect(bnNew, SIGNAL(clicked(bool)), this, SLOT(onNewClicked(bool)));
bnEdit = new QPushButton(tr("Edit"));
bnEdit->setFixedSize(QSize(88, 38));
bnEdit->setProperty("ssType", "progManageTool");
bnEdit->setEnabled(false);
hBox->addWidget(bnEdit);
connect(bnEdit, SIGNAL(clicked(bool)), this, SLOT(onEditClicked(bool)));
bnDelete = new QPushButton(tr("Delete"));
bnDelete->setFixedSize(QSize(88, 38));
bnDelete->setProperty("ssType", "progManageTool");
bnDelete->setEnabled(false);
hBox->addWidget(bnDelete);
connect(bnDelete, SIGNAL(clicked(bool)), this, SLOT(onDeleteClicked(bool)));
bnImport = new QPushButton(tr("Import"));
bnImport->setFixedSize(QSize(88, 38));
bnImport->setProperty("ssType", "progManageTool");
hBox->addWidget(bnImport);
connect(bnImport, SIGNAL(clicked(bool)), this, SLOT(onImportClicked(bool)));
bnExport = new QPushButton(tr("Export"));
bnExport->setFixedSize(QSize(88, 38));
bnExport->setEnabled(false);
bnExport->setProperty("ssType", "progManageTool");
hBox->addWidget(bnExport);
connect(bnExport, SIGNAL(clicked(bool)), this, SLOT(onExportClicked(bool)));
bnSend = new QPushButton(tr("Send"));
bnSend->setFixedSize(QSize(88, 38));
bnSend->setProperty("ssType", "progManageTool");
bnSend->setEnabled(false);
bnSend->hide();
hBox->addWidget(bnSend);
btnPlay = new QPushButton(tr("Play")+"/"+tr("Stop"));
btnPlay->setFixedSize(QSize(88, 38));
btnPlay->setProperty("ssType", "progManageTool");
hBox->addWidget(btnPlay);
connect(btnPlay, &QPushButton::clicked, this, [this](){
if(PlayWin::self!=nullptr) PlayWin::self->close();
else {
int cnt = wProgramList->topLevelItemCount();
for(int i=0; i<cnt; i++) if(wProgramList->topLevelItem(i)->checkState(0) == Qt::Checked) {
wProgramItem *item = static_cast<wProgramItem*>(wProgramList->topLevelItem(i));
QString dir = progDir+"/"+item->name()+"_tmp";
QFile file(dir+"/program");
if(! file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
QString value = file.readAll();
file.close();
QJsonParseError jsErr;
QJsonObject prog = QJsonDocument::fromJson(value.toUtf8(), &jsErr).object();
if(jsErr.error) return;
if(PlayWin::self!=nullptr) PlayWin::self->close();
PlayWin::self = new PlayWin(dir, 0, 0, item->width(), item->height(), prog);
break;
}
}
});
hBox->addStretch();
auto txtSearch = new QLineEdit(this);
txtSearch->setFixedSize(QSize(240, 36));
QAction *search = new QAction(txtSearch);
search->setIcon(QIcon(":/res/ProgramManager/bnSearch.png"));
txtSearch->addAction(search, QLineEdit::LeadingPosition);
txtSearch->setClearButtonEnabled(true);
txtSearch->setStyleSheet("border: 2px solid #aaaaaa;");
hBox->addWidget(txtSearch);
connect(txtSearch,SIGNAL(textChanged(const QString &)),this,SLOT(FilterProgram(const QString &)));
wProgramList = new LoQTreeWidget();
wProgramList->setIndentation(10);
wProgramList->setSortingEnabled(true);
m_headerItem = new QTreeWidgetItem();
for(int i=1; i<ENUM_PROGRAMLISTHEADERITEM_END; i++) m_headerItem->setTextAlignment(i, Qt::AlignCenter);
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_CHECK, Qt::DisplayRole, "");
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_NAME, Qt::DisplayRole, tr("Name"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, Qt::DisplayRole, tr("Resolution"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, Qt::DisplayRole, tr("File Size"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, Qt::DisplayRole, tr("Last Modify"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, Qt::DisplayRole, tr("Usb playback"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_SEND, Qt::DisplayRole, tr("Publish"));
wProgramList->setHeaderItem(m_headerItem);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_CHECK, QHeaderView::Fixed);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_NAME, QHeaderView::Stretch);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, QHeaderView::Stretch);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_SIZE, QHeaderView::Stretch);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, QHeaderView::Stretch);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, QHeaderView::Fixed);
wProgramList->header()->setSectionResizeMode(ENUM_PROGRAMLISTHEADERITEM_SEND, QHeaderView::Fixed);
wProgramList->header()->setStretchLastSection(false);
wProgramList->setColumnWidth(ENUM_PROGRAMLISTHEADERITEM_CHECK, 66);
wProgramList->setColumnWidth(ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, 100);
wProgramList->setColumnWidth(ENUM_PROGRAMLISTHEADERITEM_SEND, 72);
vBox->addWidget(wProgramList);
connect(wProgramList, &LoQTreeWidget::sigCheckStateChanged, this, [this](int f){
switch(f) {
case LoQTreeWidget::CheckNone:
bnEdit->setEnabled(false);
bnDelete->setEnabled(false);
bnExport->setEnabled(false);
bnSend->setEnabled(false);
break;
case LoQTreeWidget::CheckOne:
bnEdit ->setEnabled(true);
bnDelete->setEnabled(true);
bnExport->setEnabled(true);
bnSend ->setEnabled(true);
break;
case LoQTreeWidget::CheckMulti:
bnEdit ->setEnabled(false);
bnDelete->setEnabled(true);
bnExport->setEnabled(true);
bnSend ->setEnabled(true);
break;
default: break;
}
});
QString doc_path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
if(!doc_path.isEmpty()) {
QString app_path = doc_path + "/" + QApplication::applicationName();
progDir = app_path + "/NPrograms";
if(!QFileInfo::exists(progDir)) {
QDir app_dir(app_path);
app_dir.mkdir("NPrograms");
app_dir.mkdir("ApkStore");
}
}
//connect(search, SIGNAL(triggered(bool)), this, SLOT(FilterProgram()));
//查找根路径下的项目文件夹查找文件夹下的节目pro.json信息包括节目名称大小像素备注等信息
if(!progDir.isEmpty()) {
QDir root_dir(progDir);
QStringList pro_list = root_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
foreach(QString pro_name, pro_list) {
QDir pro_dir(progDir + "/" + pro_name);
if(pro_dir.exists("pro.json")) {
QFile fPro(pro_dir.path() + "/pro.json");
fPro.open(QIODevice::ReadOnly);
QJsonDocument pro = QJsonDocument::fromJson(fPro.readAll());
fPro.close();
onRestoreProgram(pro);
}
}
}
readSettings();
// ui->wProgramList->setSortingEnabled( false ); //不使能QT的自动排序
// ui->wProgramList->header()->setSortIndicatorShown( true ); // 设置三角标识符可见
// ui->wProgramList->header()->setSectionsClickable( true ); // 设置标题栏单击响应使能
// connect( ui->wProgramList->header(), SIGNAL( sectionClicked(int) ), this, SLOT( OnClickColumn(int) ) );
}
ProgPanel::~ProgPanel() {
writeSettings();
}
void ProgPanel::OnClickColumn(int iColumn) {
if(iColumn!=ENUM_PROGRAMLISTHEADERITEM_CHECK && iColumn!=ENUM_PROGRAMLISTHEADERITEM_SEND) wProgramList->sortByColumn(iColumn, wProgramList->header()->sortIndicatorOrder());
}
void ProgPanel::writeSettings() {
QSettings settings;
settings.setValue("ProgramListSortColumn", wProgramList->sortColumn());
settings.setValue("ProgramListSortOrder", wProgramList->header()->sortIndicatorOrder());
}
void ProgPanel::readSettings() {
QSettings settings;
if(settings.value("ProgramListSortOrder").toInt()==0) wProgramList->sortByColumn(settings.value("ProgramListSortColumn").toInt(),Qt::SortOrder::AscendingOrder);
else wProgramList->sortByColumn(settings.value("ProgramListSortColumn").toInt(),Qt::SortOrder::DescendingOrder);
}
void ProgPanel::changeEvent(QEvent *event) {
QWidget::changeEvent(event);
if(event->type() == QEvent::LanguageChange) transUi();
}
void ProgPanel::transUi() {
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_CHECK, 0, "");
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_NAME, 0, tr("Name"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, 0, tr("Resolution"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, tr("File Size"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, 0, tr("Last Modify"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, 0, tr("Usb playback"));
m_headerItem->setData(ENUM_PROGRAMLISTHEADERITEM_SEND, 0, tr("Publish"));
bnNew->setText(tr("New"));
bnEdit->setText(tr("Edit"));
bnDelete->setText(tr("Delete"));
bnImport->setText(tr("Import"));
bnExport->setText(tr("Export"));
bnSend->setText(tr("Send"));
btnPlay->setText(tr("Play")+"/"+tr("Stop"));
for(int i=0;i<m_pwPorgramItemList.count();i++) m_pwPorgramItemList.at(i)->refreshLable();
}
void ProgPanel::onNewClicked(bool f){
Q_UNUSED(f);
wNewProgram *dlg = new wNewProgram(this);
connect(dlg, SIGNAL(sigAcceptData(QString,QSize,QString)), this, SLOT(onCreateNewProgram(QString,QSize,QString)));
dlg->exec();
}
void ProgPanel::onEditClicked(bool){
int cnt = wProgramList->topLevelItemCount();
for(int i=0; i<cnt; i++) {
if(wProgramList->topLevelItem(i)->checkState(0) == Qt::Checked) {
wProgramItem *item = static_cast<wProgramItem*>(wProgramList->topLevelItem(i));
emit item->sigEditProgram();
break;
}
}
}
bool ProgPanel::checkIfNameRepeated(const QString &name, QTreeWidgetItem *skip){
int cnt = wProgramList->topLevelItemCount();
for(int i=0; i<cnt; i++) {
wProgramItem *item = static_cast<wProgramItem*>(wProgramList->topLevelItem(i));
if(item == skip) continue;
if(name == item->name()) {
QMessageBox::warning(this, tr("Warning"), tr("Program name conflicted"));
return true;
}
}
return false;
}
void ProgPanel::onRestoreProgram(const QJsonDocument &pro)
{
m_pwPorgramItemList.append(new wProgramItem(progDir, pro.object(), wProgramList,this));
}
void ProgPanel::onCreateNewProgram(QString name, QSize res, QString remarks)
{
if(checkIfNameRepeated(name)) {
return;
}
auto item = new wProgramItem(progDir, name, res.width(), res.height(), remarks, wProgramList,this);
item->save();//保存pro.json
wProgramList->adjustCheckState();
}
void ProgPanel::onCreateNewProgramOnOpenEditProgramWidget(QString name, QSize res, QString remarks)
{
if(checkIfNameRepeated(name)) return;
auto item = new wProgramItem(progDir, name, res.width(), res.height(), remarks, wProgramList,this);
item->save();//保存pro.json
wProgramList->adjustCheckState();
}
void ProgPanel::onDeleteClicked(bool){
auto res = QMessageBox::information(this, tr("Tip Info"), tr("You will delete the selected solution(s),are you sure?"), QMessageBox::Ok, QMessageBox::Cancel);
if(res == QMessageBox::Ok) {
int cnt = wProgramList->topLevelItemCount();
QList<wProgramItem*> list;
for(int i=0; i<cnt; i++) {
if(wProgramList->topLevelItem(i)->checkState(0) == Qt::Checked) {
wProgramItem *item = static_cast<wProgramItem*>(wProgramList->topLevelItem(i));
list.push_back(item);
}
}
while(!list.isEmpty()) {
wProgramItem *item = list.takeFirst();
item->del();
delete item;
}
wProgramList->adjustCheckState();
}
}
void ProgPanel::onImportClicked(bool){
TipDialog *dlg = new TipDialog(this,ENUM_IMPORT_DLG);
connect(dlg, SIGNAL(sigAcceptData(QString,QString)), this, SLOT(onImportProgram(QString,QString)));
dlg->exec();
}
void ProgPanel::onImportProgram(QString strImportDir, QString){
if(!strImportDir.isEmpty()){
wProgramList->clear();
//查找根路径下的项目文件夹查找文件夹下的节目pro.json信息包括节目名称大小像素备注等信息
if(!progDir.isEmpty()) {
QDir root_dir(progDir);
QStringList pro_list = root_dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
foreach(QString pro_name, pro_list) {
QDir pro_dir(progDir + MACRO_FENGEFU + pro_name);
if(pro_dir.exists("pro.json")) {
QFile fPro(pro_dir.path() + MACRO_FENGEFU+"pro.json");
fPro.open(QIODevice::ReadOnly);
QJsonDocument pro = QJsonDocument::fromJson(fPro.readAll());
fPro.close();
onRestoreProgram(pro);
}
}
}
}
}
void ProgPanel::onExportClicked(bool){
int cnt = wProgramList->topLevelItemCount();
QStringList selectProgramlist;
for(int i=0; i<cnt; i++) {
if(wProgramList->topLevelItem(i)->checkState(0) == Qt::Checked) {
QString string =static_cast<wProgramItem*>(wProgramList->topLevelItem(i))->GetBnName();
selectProgramlist.append(string);
}
}
TipDialog *dlg = new TipDialog(this,ENUM_EXPORT_DLG,&selectProgramlist,0);
connect(dlg, SIGNAL(sigAcceptData(QString,QString)), this, SLOT(onExportProgram(QString,QString)));
dlg->exec();
}
void ProgPanel::onExportProgram(QString strImportDir,QString strTip1){
Q_UNUSED(strImportDir);
Q_UNUSED(strTip1);
}
void ProgPanel::FilterProgram(const QString &strtemp){
if (strtemp.isEmpty()) //显示全部
{
for (int i = 0; i< wProgramList->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = wProgramList->topLevelItem(i);
wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),false);
}
}
else
{
QList<QTreeWidgetItem*> resultList = wProgramList->findItems(strtemp, Qt::MatchContains,ENUM_PROGRAMLISTHEADERITEM_NAME); //搜索结果
if (resultList.size() > 0)
{
for (int i = 0; i< wProgramList->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = wProgramList->topLevelItem(i);
if (resultList.contains(topItem))
wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),false); //显示匹配的结果
else
wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
else {
QList<QTreeWidgetItem*> resultList1 = wProgramList->findItems(strtemp, Qt::MatchContains,ENUM_PROGRAMLISTHEADERITEM_RESOLUTION); //搜索结果
if (resultList1.size() > 0){
for (int i = 0; i< wProgramList->topLevelItemCount(); ++i){
QTreeWidgetItem* topItem = wProgramList->topLevelItem(i);
if (resultList1.contains(topItem)) wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),false); //显示匹配的结果
else wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
else {
for (int i = 0; i< wProgramList->topLevelItemCount(); ++i){
QTreeWidgetItem* topItem = wProgramList->topLevelItem(i);
wProgramList->setRowHidden(i,wProgramList->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
}
}
}