#include "dialog_button.h" #include "ui_dialog_button.h" #include #include #include #include #include "cJSON.h" #include "sqlite3.h" Dialog_Button::Dialog_Button(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog_Button) { ui->setupUi(this); setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); ui->tableWidget->setColumnCount(5); ui->tableWidget->setColumnWidth(0, 20); ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tableWidget->setStyleSheet("selection-background-color:rgb(0, 120, 215)"); ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->tableWidget->horizontalHeader()->setFixedHeight(40); ui->tableWidget->horizontalHeader()->setStretchLastSection(true); ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("")); ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Device Type")); ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Button Type")); ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("Device Config")); ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("Save Param")); ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); // TODO 读取配置文件,设置m_vScanParams ui->tableWidget->setRowCount((int)m_vScanParams.size()); for (int index = 0; index < (int)m_vScanParams.size(); ++index) { ui->tableWidget->setRowHeight(index, 30); QCheckBox *checkBox = new QCheckBox(ui->tableWidget); connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(on_listItemCheckbox_stateChanged(int))); QHBoxLayout *layout = new QHBoxLayout(ui->tableWidget); layout->addWidget(checkBox, 0, Qt::AlignCenter); layout->setMargin(0); QWidget *widget = new QWidget(ui->tableWidget); widget->setLayout(layout); ui->tableWidget->setCellWidget(index, 0, widget); const char *devTypeStr[] = {NULL, "G100", "G200", "G300", "G400"}; ui->tableWidget->setItem(index, 1, new QTableWidgetItem(devTypeStr[m_vScanParams[index].deviceType])); ui->tableWidget->item(index, 1)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 1)->setFlags(ui->tableWidget->item(index, 1)->flags() & ~Qt::ItemIsEditable); const char *btnTypeStr[] = {NULL, "Button 1", "Button 2", "Button 3"}; ui->tableWidget->setItem(index, 2, new QTableWidgetItem(btnTypeStr[m_vScanParams[index].buttonType])); ui->tableWidget->item(index, 2)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 2)->setFlags(ui->tableWidget->item(index, 2)->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(index, 3, new QTableWidgetItem("-")); ui->tableWidget->item(index, 3)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 3)->setFlags(ui->tableWidget->item(index, 3)->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(index, 4, new QTableWidgetItem("-")); ui->tableWidget->item(index, 4)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 4)->setFlags(ui->tableWidget->item(index, 4)->flags() & ~Qt::ItemIsEditable); } ui->tableWidget->selectRow(0); } Dialog_Button::~Dialog_Button() { delete ui; } bool Dialog_Button::GetScanParam(DeviceType deviceType, ButtonType buttonType, ScanParam &scanParam) { for (int i = 0; i < (int)m_vScanParams.size(); ++i) { if (deviceType == m_vScanParams[i].deviceType && buttonType == m_vScanParams[i].buttonType) { scanParam = m_vScanParams[i]; return true; } } return false; } bool Dialog_Button::FindScanParam(DeviceType deviceType, ButtonType buttonType, int ignoreIndex) { for (int i = 0; i < (int)m_vScanParams.size(); ++i) { if (deviceType == m_vScanParams[i].deviceType && buttonType == m_vScanParams[i].buttonType) { if (-1 == ignoreIndex) // 表示均不忽略 { return true; } else if (i != ignoreIndex) { return true; } } } return false; } void Dialog_Button::closeEvent(QCloseEvent *e) { hide(); // 隐藏主窗口 e->ignore(); //忽略关闭事件,这样才不会关闭程序 } void Dialog_Button::on_pushButtonAdd_clicked() { Dialog_Add dlg(this); if (dlg.exec()) { ScanParam scanParam = dlg.GetScanParam(); m_vScanParams.push_back(scanParam); // TODO 保存m_vScanParams ui->tableWidget->setRowCount((int)m_vScanParams.size()); int index = (int)m_vScanParams.size() - 1; ui->tableWidget->setRowHeight(index, 30); QCheckBox *checkBox = new QCheckBox(ui->tableWidget); connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(on_listItemCheckbox_stateChanged(int))); QHBoxLayout *layout = new QHBoxLayout(ui->tableWidget); layout->addWidget(checkBox, 0, Qt::AlignCenter); layout->setMargin(0); QWidget *widget = new QWidget(ui->tableWidget); widget->setLayout(layout); ui->tableWidget->setCellWidget(index, 0, widget); const char *devTypeStr[] = {NULL, "G100", "G200", "G300", "G400"}; ui->tableWidget->setItem(index, 1, new QTableWidgetItem(devTypeStr[m_vScanParams[index].deviceType])); ui->tableWidget->item(index, 1)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 1)->setFlags(ui->tableWidget->item(index, 1)->flags() & ~Qt::ItemIsEditable); const char *btnTypeStr[] = {NULL, "Button 1", "Button 2", "Button 3"}; ui->tableWidget->setItem(index, 2, new QTableWidgetItem(btnTypeStr[m_vScanParams[index].buttonType])); ui->tableWidget->item(index, 2)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 2)->setFlags(ui->tableWidget->item(index, 2)->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(index, 3, new QTableWidgetItem("-")); ui->tableWidget->item(index, 3)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 3)->setFlags(ui->tableWidget->item(index, 3)->flags() & ~Qt::ItemIsEditable); ui->tableWidget->setItem(index, 4, new QTableWidgetItem("-")); ui->tableWidget->item(index, 4)->setTextAlignment(Qt::AlignCenter); ui->tableWidget->item(index, 4)->setFlags(ui->tableWidget->item(index, 4)->flags() & ~Qt::ItemIsEditable); ui->tableWidget->selectRow(index); } } void Dialog_Button::on_pushButtonModify_clicked() { int index = ui->tableWidget->currentRow(); if (-1 == index) { QMessageBox::information(this, tr("Tips"), tr("No item selected")); return; } Dialog_Add dlg(this, m_vScanParams[index], index); if (dlg.exec()) { ScanParam scanParam = dlg.GetScanParam(); m_vScanParams[index] = scanParam; // TODO 保存m_vScanParams const char *devTypeStr[] = {NULL, "G100", "G200", "G300", "G400"}; ui->tableWidget->item(index, 1)->setText(QString::fromStdString(devTypeStr[m_vScanParams[index].deviceType])); const char *btnTypeStr[] = {NULL, "Button 1", "Button 2", "Button 3"}; ui->tableWidget->item(index, 2)->setText(QString::fromStdString(btnTypeStr[m_vScanParams[index].buttonType])); } } void Dialog_Button::on_pushButtonRemove_clicked() { int index = ui->tableWidget->currentRow(); if (-1 == index) { QMessageBox::information(this, tr("Tips"), tr("No item selected")); return; } QMessageBox msg(QMessageBox::Question, tr("Question"), tr("Are you sure you want to remove the item?"), QMessageBox::Yes | QMessageBox::No, this); msg.exec(); if (msg.clickedButton() != msg.button(QMessageBox::Yes)) { return; } m_vScanParams.erase(m_vScanParams.begin() + index); // TODO 保存m_vScanParams ui->tableWidget->removeRow(index); }