#include "osdmaneger.h"
#include <QTreeWidget>
#include <QDebug>
#include <QList>
#include <QFontDialog>
#include <QColorDialog>
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
OsdManeger::OsdManeger(const QPoint ptMax, QVector<OSD_Type> *pOsdVect, QWidget *parent) : QWidget(parent)
{
setWindowTitle("OSD管理");
_pOsdVect = pOsdVect;
select=-1;
int size = _pOsdVect->size();
QPushButton *addBtn = new QPushButton("添加");addBtn->setFixedHeight(30); // 添加按钮
QPushButton *removeBtn = new QPushButton("删除");removeBtn->setFixedHeight(30); // 删除按钮
tree = new QTreeWidget(); // 树
//tree->setHeaderHidden(true);
tree->setHeaderLabel("OSD:");
// 项
for(int i=0; i<size; i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem(tree, QStringList(QString(tr("[%1] ").arg(i+1)+_pOsdVect->at(i).content)));
item->setData(0,Qt::UserRole,i);
}
//其他
QLabel *hideLabel = new QLabel("OSD隐藏:");
hideChkbox = new QCheckBox();
hideChkbox->setCheckState(Qt::CheckState::Unchecked);
QLabel *xPointLabel = new QLabel("X坐标:");
xPoint = new QSpinBox();
xPoint->setMinimum(0);
xPoint->setMaximum(ptMax.x());
xPoint->setFixedHeight(30);
QLabel *yPointLabel = new QLabel("Y坐标:");
yPoint = new QSpinBox();
yPoint->setMinimum(0);
yPoint->setMaximum(ptMax.y());
yPoint->setFixedHeight(30);
QLabel *colorLabel = new QLabel("颜色:");
colorBtn = new QPushButton();
QPalette pal = colorBtn->palette();
pal.setColor(QPalette::Button,Qt::blue);
colorBtn->setPalette(pal);
colorBtn->setAutoFillBackground(true);
colorBtn->setFlat(true);
QLabel *fontLabel = new QLabel("字体:");
fontBtn = new QPushButton();
QLabel *contentLabel = new QLabel("内容:");
contentLineEdit = new QLineEdit();
contentLineEdit->setMinimumWidth(200);
changeBtn = new QPushButton("更改");changeBtn->setFixedHeight(30);
allEnable(false);
QGridLayout *fuckLayout = new QGridLayout();
fuckLayout->addWidget(hideLabel,0,0,1,1);
fuckLayout->addWidget(hideChkbox,0,1,1,3);
fuckLayout->addWidget(xPointLabel,1,0,1,1);
fuckLayout->addWidget(xPoint,1,1,1,3);
fuckLayout->addWidget(yPointLabel,2,0,1,1);
fuckLayout->addWidget(yPoint,2,1,1,3);
fuckLayout->addWidget(colorLabel,3,0,1,1);
fuckLayout->addWidget(colorBtn,3,1,1,3);
fuckLayout->addWidget(fontLabel,4,0,1,1);
fuckLayout->addWidget(fontBtn,4,1,1,3);
fuckLayout->addWidget(contentLabel,5,0,1,1);
fuckLayout->addWidget(contentLineEdit,5,1,1,3);
fuckLayout->addWidget(changeBtn,6,3,1,1);
// 信号
connect(tree,SIGNAL(itemClicked (QTreeWidgetItem *,int)),this,SLOT(OnTreeWidgetClicked(QTreeWidgetItem *,int)));
//connect(tree,SIGNAL(itemPressed (QTreeWidgetItem *,int)),this,SLOT(OnTreeWidgetPressed(QTreeWidgetItem *,int)));
//connect(tree,SIGNAL(itemChanged (QTreeWidgetItem *,int)),this,SLOT(OnTreeWidgetItemChanged(QTreeWidgetItem *,int)));
connect(addBtn, &QPushButton::clicked, this, &OsdManeger::slotAddBtnclicked);
connect(removeBtn, &QPushButton::clicked, this, &OsdManeger::slotRemoveBtnclicked);
connect(colorBtn, &QPushButton::clicked, this, &OsdManeger::slotColorBtnclicked);
connect(fontBtn, &QPushButton::clicked, this, &OsdManeger::slotFontBtnclicked);
connect(changeBtn, &QPushButton::clicked, this, &OsdManeger::slotChangeBtnclicked);
// 布局
QHBoxLayout *barlayout = new QHBoxLayout();
barlayout->addWidget(addBtn);
barlayout->addWidget(removeBtn);
QVBoxLayout *treelayout = new QVBoxLayout();
treelayout->addWidget(tree);
treelayout->addLayout(barlayout);
mainlayout = new QHBoxLayout();
mainlayout->addLayout(treelayout);
mainlayout->addLayout(fuckLayout);
setLayout(mainlayout);
setFixedSize(600,600);
setAttribute(Qt::WA_QuitOnClose,false); // 子窗体随主窗体关闭
}
OsdManeger::~OsdManeger()
{
}
void OsdManeger::allEnable(const bool ena)
{
if(ena)
{
hideChkbox->setEnabled(true);
xPoint->setEnabled(true);
yPoint->setEnabled(true);
colorBtn->setEnabled(true);
fontBtn->setEnabled(true);
contentLineEdit->setEnabled(true);
changeBtn->setEnabled(true);
}
else
{
hideChkbox->setEnabled(false);
xPoint->setEnabled(false);
yPoint->setEnabled(false);
colorBtn->setEnabled(false);
fontBtn->setEnabled(false);
contentLineEdit->setEnabled(false);
changeBtn->setEnabled(false);
}
}
void OsdManeger::update(const int index)
{
if(index<0)
{
allEnable(false);
return;
}
allEnable(true);
if((*_pOsdVect)[index].bHide)
hideChkbox->setCheckState(Qt::CheckState::Checked);
else
hideChkbox->setCheckState(Qt::CheckState::Unchecked);
contentLineEdit->setText((*_pOsdVect)[index].content);
xPoint->setValue((*_pOsdVect)[index].pt.x());
yPoint->setValue((*_pOsdVect)[index].pt.y());
currColor = (*_pOsdVect)[index].color;
QPalette pal = colorBtn->palette();
pal.setColor(QPalette::Button, currColor);
colorBtn->setPalette(pal);
currFont = (*_pOsdVect)[index].font;
fontBtn->setText(currFont.toString());
}
// 选中项目
void OsdManeger::OnTreeWidgetClicked(QTreeWidgetItem *item, int column)
{
qDebug()<<"OnTreeWidgetClicked "<<item->text(column)<<item->data(column,Qt::UserRole);
int index = item->data(column,Qt::UserRole).toInt();
select = index;
update(index); // 更新右边区域
}
// 添加OSD
void OsdManeger::slotAddBtnclicked()
{
qDebug()<<"slotAddBtnclicked";
OSD_Type osd;
osd.invalid=false;
osd.bHide=false;
osd.color=QColor(Qt::black);
osd.content="无内容";
osd.font = QFont("黑体",24);
osd.pt=QPoint(0,0);
_pOsdVect->push_back(osd);
int size = _pOsdVect->size();qDebug()<<"slotAddBtnclicked size: "<<size;
QTreeWidgetItem *item = new QTreeWidgetItem(tree, QStringList(QString(tr("[%1] ").arg(size))+osd.content));
item->setData(0,Qt::UserRole,size-1);
setChange();
}
// 删除OSD
void OsdManeger::slotRemoveBtnclicked()
{
QList<QTreeWidgetItem* > items = tree->selectedItems();
QList<QTreeWidgetItem* >::iterator it;
qDebug()<<items.size();
if(items.size()==0) return;
it = items.begin();
qDebug()<<"remove:"<<(*it)->data(0,Qt::UserRole);
int index = (*it)->data(0,Qt::UserRole).toInt();
(*_pOsdVect)[index].invalid=true;
//
_pOsdVect->remove(index); // 删除osd
tree->clear(); // 清空tree
// 重构tree
int size = _pOsdVect->size();
if(size==0)
{
select=-1;
}
else
{
for(int i=0; i<size; i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem(tree, QStringList(QString(tr("[%1] ").arg(i+1)+_pOsdVect->at(i).content)));
item->setData(0,Qt::UserRole,i);
}
select=0;
}
update(select);
setChange();
}
// 设置字体
void OsdManeger::slotFontBtnclicked()
{
bool ok;
QFont f = QFontDialog::getFont(&ok, (*_pOsdVect)[select].font);
if (ok)
{
currFont = f;
fontBtn->setText(f.toString());
slotChangeBtnclicked();
}
}
// 设置字体颜色
void OsdManeger::slotColorBtnclicked()
{
QColor c = QColorDialog::getColor((*_pOsdVect)[select].color);
if(c.isValid())
{
qDebug()<<c;
currColor = c;
QPalette pal = colorBtn->palette();
pal.setColor(QPalette::Button, c);
colorBtn->setPalette(pal);
colorBtn->setAutoFillBackground(true);