176 lines
4.4 KiB
C++
176 lines
4.4 KiB
C++
#include "CButtonGroupWidget.h"
|
|
#include "CJsonReader.h"
|
|
#include "public/pub_utility_api/FileUtil.h"
|
|
#include <QGridLayout>
|
|
#include <QButtonGroup>
|
|
#include <QPushButton>
|
|
#include <QLabel>
|
|
#include <QProcess>
|
|
#include <QDir>
|
|
#include "pub_utility_api/FileStyle.h"
|
|
|
|
CButtonGroupWidget::CButtonGroupWidget(QWidget *parent, bool editMode)
|
|
: QWidget(parent),
|
|
m_pJsonReader(NULL),
|
|
m_isEdit(editMode)
|
|
{
|
|
initView();
|
|
initQss();
|
|
}
|
|
|
|
CButtonGroupWidget::~CButtonGroupWidget()
|
|
{
|
|
if(m_pJsonReader)
|
|
{
|
|
delete m_pJsonReader;
|
|
}
|
|
m_pJsonReader = NULL;
|
|
}
|
|
|
|
void CButtonGroupWidget::onButtonClicked(int index)
|
|
{
|
|
QList<QPushButton *> buttonList = this->findChildren<QPushButton *>();
|
|
if(index < buttonList.length())
|
|
{
|
|
emit buttonList[index]->clicked();
|
|
buttonList[index]->setChecked(true);
|
|
}
|
|
else if(buttonList.length() > 0)
|
|
{
|
|
emit buttonList[0]->clicked();
|
|
buttonList[0]->setChecked(true);
|
|
}
|
|
}
|
|
|
|
int CButtonGroupWidget::getCheckedButton()
|
|
{
|
|
QButtonGroup *group = this->findChild<QButtonGroup *>();
|
|
if(group)
|
|
{
|
|
if(group->checkedButton())
|
|
{
|
|
return group->id(group->checkedButton());
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void CButtonGroupWidget::setCheckedButton(int index)
|
|
{
|
|
QList<QPushButton *> buttonList = this->findChildren<QPushButton *>();
|
|
if(index < buttonList.length())
|
|
{
|
|
buttonList[index]->setChecked(true);
|
|
}
|
|
else if(buttonList.length() > 0)
|
|
{
|
|
buttonList[0]->setChecked(true);
|
|
}
|
|
}
|
|
|
|
void CButtonGroupWidget::slotButtonClicked()
|
|
{
|
|
QString obj = sender()->objectName();
|
|
ST_BUTTON stButton = m_pJsonReader->getStButton(obj);
|
|
switch (stButton.type)
|
|
{
|
|
case SWITCH_GRAPH:
|
|
{
|
|
emit onGraphClicked(stButton.name, stButton.data);
|
|
break;
|
|
}
|
|
case SWITCH_NAV:
|
|
{
|
|
stButton.data.replace("2", "0");
|
|
emit onNavigationClicked(stButton.name, stButton.data);
|
|
break;
|
|
}
|
|
case EXTERN_PRO:
|
|
{
|
|
emit onExternClicked(stButton.name, stButton.data);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CButtonGroupWidget::initView()
|
|
{
|
|
m_pJsonReader = new CJsonReader(this);
|
|
QMap<QString, ST_BUTTON> buttonMap = m_pJsonReader->getButtonMap();
|
|
QString separator = m_pJsonReader->getSeparatorPath();
|
|
if(buttonMap.isEmpty())
|
|
{
|
|
QGridLayout *layout = new QGridLayout(this);
|
|
layout->addWidget(new QPushButton(tr("配置错误!")));
|
|
setLayout(layout);
|
|
return;
|
|
}
|
|
|
|
std::string currentPath = iot_public::CFileUtil::getCurModuleDir();
|
|
QDir dir(QString::fromStdString(currentPath));
|
|
dir.cdUp();
|
|
dir.cdUp();
|
|
dir.cd("data");
|
|
dir.cd("back_pixmap");
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
QWidget *widget;
|
|
QButtonGroup *buttonGroup = new QButtonGroup(this);
|
|
int id = 0;
|
|
QMap<QString, ST_BUTTON>::const_iterator iter
|
|
= buttonMap.constBegin();
|
|
for(; iter != buttonMap.constEnd(); iter++)
|
|
{
|
|
if(iter.value().used == 2)
|
|
{
|
|
continue;
|
|
}
|
|
QPushButton *button = new QPushButton(QIcon(dir.absoluteFilePath(iter.value().icon)), iter.value().name);
|
|
button->setObjectName(iter.key());
|
|
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
button->setCheckable(true);
|
|
buttonGroup->addButton(button, id++);
|
|
if(!m_isEdit)
|
|
{
|
|
connect(button, &QPushButton::clicked, this, &CButtonGroupWidget::slotButtonClicked);
|
|
}
|
|
layout->addWidget(button);
|
|
|
|
if(!separator.isEmpty())
|
|
{
|
|
QPixmap pixmap(dir.absoluteFilePath(separator));
|
|
QLabel *label = new QLabel();
|
|
label->setPixmap(pixmap);
|
|
layout->addWidget(label);
|
|
widget = label;
|
|
}
|
|
}
|
|
if(!separator.isEmpty() && widget)
|
|
{
|
|
layout->removeWidget(widget);
|
|
delete widget;
|
|
}
|
|
layout->setMargin(0);
|
|
setLayout(layout);
|
|
}
|
|
|
|
void CButtonGroupWidget::initQss()
|
|
{
|
|
QString qss = QString();
|
|
std::string strFullPath = iot_public::CFileStyle::getPathOfStyleFile("cButtonGroupWidget.qss") ;
|
|
QFile qssfile2(QString::fromStdString(strFullPath));
|
|
qssfile2.open(QFile::ReadOnly);
|
|
if (qssfile2.isOpen())
|
|
{
|
|
qss += QLatin1String(qssfile2.readAll());
|
|
//setStyleSheet(qss);
|
|
qssfile2.close();
|
|
}
|
|
if(!qss.isEmpty())
|
|
{
|
|
setStyleSheet(qss);
|
|
}
|
|
}
|