#include "singlerelay.h" #include "ui_singlerelay.h" #include "vibrationdata.h" SingleRelay::SingleRelay(int slot,int cardtype,QWidget *parent) : QDialog(parent) , ui(new Ui::SingleRelay) { ui->setupUi(this); slot_no = slot; car_type = static_cast(cardtype); ui->label_slot_no->setText(QString::number(slot_no)); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); QVBoxLayout *layout_available = new QVBoxLayout(ui->widget_available); list_widget_available = new QListWidget(); layout_available->addWidget(list_widget_available); list_widget_available->setDragEnabled(true); QVBoxLayout *layout_relay = new QVBoxLayout(ui->widget_relay); treeView_relay = new QTreeView; layout_relay->addWidget(treeView_relay); treeView_relay->setDragEnabled(true); // 启用拖动 treeView_relay->setAcceptDrops(true); // 接受放下 treeView_relay->setDropIndicatorShown(true); // 显示放置指示器 treeView_relay->setDragDropMode(QAbstractItemView::DropOnly); model_Relay = new DropTreeModel(this); //创建模型指定父类 treeView_relay->setModel(model_Relay); treeView_relay->setHeaderHidden(true); btnGroup_slot = new QButtonGroup(this); btnGroup_slot->addButton(ui->pushButton_slot1); btnGroup_slot->addButton(ui->pushButton_slot2); btnGroup_slot->addButton(ui->pushButton_slot3); btnGroup_slot->addButton(ui->pushButton_slot4); btnGroup_slot->addButton(ui->pushButton_slot5); btnGroup_slot->addButton(ui->pushButton_slot6); btnGroup_slot->addButton(ui->pushButton_slot7); btnGroup_slot->addButton(ui->pushButton_slot8); btnGroup_slot->addButton(ui->pushButton_slot9); btnGroup_slot->addButton(ui->pushButton_slot10); btnGroup_slot->addButton(ui->pushButton_slot11); btnGroup_slot->addButton(ui->pushButton_slot12); btnGroup_slot->addButton(ui->pushButton_slot13); btnGroup_slot->addButton(ui->pushButton_slot14); btnGroup_slot->addButton(ui->pushButton_slot15); btnGroup_slot->addButton(ui->pushButton_slot16); connect(btnGroup_slot, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(OnButtonGroup(QAbstractButton *))); connect(ui->comboBox_relay_ch, QOverload::of(&QComboBox::currentIndexChanged), this, &SingleRelay::onComboBoxIndexChanged); Init(); current_index = ui->comboBox_relay_ch->currentIndex(); if(single_relay_nok_data->single_relay_nok[current_index].logic_expression != ""){ QString channel_name = channelNameMap[single_relay_nok_data->single_relay_nok[current_index].logic_expression]; QStandardItem *item = new QStandardItem(channel_name); item->setData(single_relay_nok_data->single_relay_nok[current_index].logic_expression,Qt::UserRole); model_Relay->appendRow(item); } } SingleRelay::~SingleRelay() { delete ui; } void SingleRelay::Init(){ QList buttonList = btnGroup_slot->buttons(); for (int i = 1; i < buttonList.count() + 1; i++) { std::shared_ptr base_ptr = ConfigMgr::Instance()->GetSlotPtr(i); if(base_ptr != nullptr){ switch (base_ptr->card_type_) { case kCardVibSingle :{ buttonList[i - 1]->setText("振动"); break; } case kCardKeyphaseSingle:{ buttonList[i - 1]->setText("键相"); break; } case kCardSpeedSingle:{ buttonList[i - 1]->setText("转速"); break; } case kCardRelaySingle: case kCardRelaySingleNOK: case kCardRelayTMRPrimary:{ buttonList[i - 1]->setText("继电器"); break; } default: break; } } } std::shared_ptr base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no); if (base_ptr == nullptr) { // do nothing or use template to init it. single_relay_nok_data = std::make_shared(); single_relay_nok_data->card_type_ = car_type; single_relay_nok_data->slot_ = slot_no; ConfigMgr::Instance()->AddCard(single_relay_nok_data); return; } single_relay_nok_data = std::dynamic_pointer_cast(base_ptr); for(int i = 0 ; i < SLOT_NUM ; i++){ std::shared_ptr cardbase_ptr = ConfigMgr::Instance()->GetSlotPtr(i + 1); if(cardbase_ptr != nullptr && cardbase_ptr->card_type_ == kCardVibSingle){ std::shared_ptr ptr = std::dynamic_pointer_cast(cardbase_ptr); for (int var = 0; var < CHANNEL_COUNT; ++var) { QString item_data,item_str; item_str = QString("%1 (槽位 %2 通道 %3 非OK)").arg(ptr->base_config_[var].point_name).arg(ptr->base_config_[var].chan_id.mid(1,2)).arg(ptr->base_config_[var].chan_id.mid(4,2)); item_data = QString("%1P##NO").arg(ptr->base_config_[var].chan_id); channelNameMap[item_data] = item_str; } }else if(cardbase_ptr != nullptr && cardbase_ptr->card_type_ == kCardSpeedSingle){ } } } void SingleRelay::on_pushButton_cancel_clicked() { this->close(); } void SingleRelay::OnButtonGroup(QAbstractButton *slot_btn) { if (slot_btn != NULL) { list_widget_available->clear(); QString object_name = slot_btn->objectName(); qDebug() << object_name ; int button_id = object_name.right(object_name.length() - 15).toInt(); for(int var = 0; var < CHANNEL_COUNT ; ++var){ std::shared_ptr base_ptr = ConfigMgr::Instance()->GetSlotPtr(button_id); if(base_ptr->card_type_ == kCardVibSingle){ std::shared_ptr vib_data = std::dynamic_pointer_cast(base_ptr); if((vib_data->base_config_[var].standby && (var % 2))) continue; QString item_str = QString("%1 (槽位 %2 通道 %3 非OK)").arg(vib_data->base_config_[var].point_name).arg(QString::number(button_id, 10).rightJustified(2, '0')).arg(QString::number(var+1, 10).rightJustified(2, '0')); QListWidgetItem *item = new QListWidgetItem(item_str); QString item_data = QString("S%1C%2P##NO").arg(QString::number(button_id, 10).rightJustified(2, '0')).arg(QString::number(var+1, 10).rightJustified(2, '0')); item->setData(Qt::UserRole, item_data); list_widget_available->addItem(item); }else if(base_ptr->card_type_ == kCardSpeedSingle){ } } } } void SingleRelay::on_pushButton_enter_clicked() { } void SingleRelay::on_pushButton_clr_clicked() { } void SingleRelay::on_pushButton_backspace_clicked() { } void SingleRelay::keyPressEvent(QKeyEvent *event) { // if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { // removeLastElement(); // return; // } // QTextEdit::keyPressEvent(event); } void SingleRelay::on_pushButton_confirm_clicked() { QStandardItemModel *model = qobject_cast(treeView_relay->model()); if (!model) return; QStandardItem *root = model->invisibleRootItem(); QStandardItem *topItem = root->child(0); QString finalExpr; QVariant userData = topItem->data(Qt::UserRole); QString user_text = userData.toString().trimmed(); single_relay_nok_data->single_relay_nok[current_index].logic_expression = user_text; this->close(); } void SingleRelay::onComboBoxIndexChanged(int index){ qDebug()<< "[SingleRelay]:index " << index; QStandardItemModel *model = qobject_cast(treeView_relay->model()); if (!model) return; QStandardItem *root = model->invisibleRootItem(); if(root->rowCount() > 0){ QStandardItem *topItem = root->child(0); QString finalExpr; QVariant userData = topItem->data(Qt::UserRole); QString user_text = userData.toString().trimmed(); single_relay_nok_data->single_relay_nok[current_index].logic_expression = user_text; } current_index = index; if(single_relay_nok_data->single_relay_nok[index].logic_expression != ""){ model_Relay->clear(); QString channel_name = channelNameMap[single_relay_nok_data->single_relay_nok[current_index].logic_expression]; QStandardItem *item = new QStandardItem(channel_name); item->setData(single_relay_nok_data->single_relay_nok[current_index].logic_expression,Qt::UserRole); model_Relay->appendRow(item); }else{ model_Relay->clear(); } }