Files
gcs-nf/App/ToolsUI/ToolsUI.cpp
T
2021-04-19 22:07:09 +08:00

201 lines
4.7 KiB
C++

#include "ToolsUI.h"
#include "ui_ToolsUI.h"
ToolsUI::ToolsUI(QWidget *parent) :
QWidget(parent),
ui(new Ui::ToolsUI)
{
ui->setupUi(this);
//load qss
QFile file(":/qss/ToolsUI.qss");
file.open(QFile::ReadOnly);
QTextStream filetext(&file);
QString stylesheet = filetext.readAll();
this->setStyleSheet(stylesheet);
file.close();
//绑定按键和界面
int w = 120;
int h = 50;
ui->scrollArea->setFixedWidth(w);
ui->pushButton_ShowExtern->setFixedSize(w,h);
index0 = new Tools_Index0();
index1 = new Tools_Index1();
index2 = new Tools_Index2();
index3 = new Tools_Index3();
index4 = new tools_Index4();
//powersystem = new PowerSystem();
//servosystem = new ServoSystem();
command = new CommandBox();
//diagram = new Diagram();
senser = new Senser();
remotecontrol = new RemoteControl();
install(index0, 0,QIcon(":/img/warning.png"),tr("Inspector"));
install(index1, 1,QIcon(":/img/warning.png"),tr("Log"));
install(index2, 2,QIcon(":/img/warning.png"),tr("Replay"));
install(index3, 3,QIcon(":/img/warning.png"),tr("Terminal"));
install(index4, 4,QIcon(":/img/warning.png"),tr("Servos"));
//install(powersystem, 5,QIcon(":/img/warning.png"),tr("PowerSystem"));
//install(servosystem, 6,QIcon(":/img/warning.png"),tr("ServoSystem"));
install(command, 7,QIcon(":/img/warning.png"),tr("CommandBox"));
//install(diagram, 8,QIcon(":/img/warning.png"),tr("Diagram"));
install(senser, 9,QIcon(":/img/warning.png"),tr("Senser"));
install(remotecontrol,10,QIcon(":/img/warning.png"),tr("RC"));
connect(this,SIGNAL(IndexChanged(int)),
this,SLOT(onTabIndexChanged(int)));
CurrentIndex = 0;
emit IndexChanged(CurrentIndex);
foreach (QPushButton *b, btnList) {
if(btnList.key(b) == 0)
{
setPushButtonState(b,state::Selected);
}
else
{
setPushButtonState(b,state::unSelected);
}
}
}
ToolsUI::~ToolsUI()
{
foreach (QWidget *w, ToolsList) {
if(w)
{
w->deleteLater();
w = nullptr;
}
}
delete ui;
}
void ToolsUI::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
foreach (QWidget *w, ToolsList) {
if(w->parent())
{
w->setGeometry(0,0,ui->frame->geometry().width(),ui->frame->geometry().height());
}
}
update();
}
void ToolsUI::setPushButtonState(QWidget *w,state s)
{
w->setProperty("state",s);
w->style()->unpolish(w);
w->style()->polish(w);
}
void ToolsUI::install(ToolsWidget *widget,int key, QIcon icon, QString name)
{
if(!ToolsList.keys().contains(key))
{
//QWidget *widget = new QWidget(ui->frame);
int h = 50;
int w = 120;
QPushButton *btn = new QPushButton();
btn->setText(name);
btn->setFixedSize(w,h);
btnList.insert(key,btn);
connect(btn,&QPushButton::clicked,
this,&ToolsUI::btnClicked);
ui->gridLayout_Indexs->addWidget(btn,key,1);
widget->setParent(ui->frame);
widget->setWindowTitle(name);
widget->setWindowIcon(icon);
widget->hide();
ToolsList.insert(key,widget);
}
}
void ToolsUI::btnClicked()
{
QObject *obj = sender();
QPushButton *btn = qobject_cast<QPushButton *>(obj);
if(btn)
{
int key = btnList.key(btn);
CurrentIndex = key;
setPushButtonState(btn,state::Selected);
foreach (QPushButton *b, btnList) {
if(b != btn)
{
setPushButtonState(b,state::unSelected);
}
}
emit IndexChanged(CurrentIndex);
}
}
void ToolsUI::onTabIndexChanged(const int &index)//界面选择管理
{
foreach (int key, ToolsList.keys()) {
ToolsWidget *w = ToolsList.value(key);
if(index == key)
{
if(w->parent())
{
w->show();
}
else
{
w->showNormal();
w->activateWindow();
}
}
else {
if(w->parent())
{
w->hide();
}
}
}
}
void ToolsUI::on_pushButton_ShowExtern_clicked()
{
foreach (ToolsWidget *w, ToolsList) {
if(ToolsList.key(w,-1) == CurrentIndex)
{
if(w->parent())
{
w->setFloat();
}
}
}
}