/* * @file ParameterTableWidget.cpp * @brief 参数编辑面板中的表格控件,它对应的数据模型是一个组合的参数项,用户可以添加删除组合参数项中的子参数项 * @date 2011-12-21 * @author sinopec */ #include #include #include #include #include #include #include #include #include #include #include "ParameterTableWidget.h" #include "ParameterItem.h" #include "PaiToolButton.h" // #include "ConsoleGUIService.h" #include "ParameterLimits.h" #include "WorkflowConst.h" #include "TableParameterItemDelegate.h" // #include "Log.h" #include "Utils.h" #include "PaiListDialog.h" #include "PaiRadioButton.h" using namespace pai; using namespace pai::gui; using namespace pai::module; using namespace pai::graphics2d; //ParameterTableCombobox显示的列表最大数量 const int MAX_VISIBLE_ITEM = 10; CParameterTableActionIcon::CParameterTableActionIcon(const QString& strIconPath, int iRow, QWidget* pParent) : QLabel(pParent), m_iRow(iRow) { setPixmap(QPixmap(strIconPath)); setAlignment(Qt::AlignRight|Qt::AlignVCenter); } void CParameterTableActionIcon::mouseReleaseEvent(QMouseEvent *pEvent) { QLabel::mouseReleaseEvent(pEvent); QRect iconRect(width()-pixmap()->width()-10,height()/2-pixmap()->height()/2,pixmap()->width(),pixmap()->height()); if(iconRect.contains(pEvent->pos())) { emit signalIconClicked(m_iRow); } } CParameterTableLineEdit::CParameterTableLineEdit(CParameterItem* pParameterItem, QTableWidgetItem* pTableItem, QWidget* pParent) : PaiLineEdit(pParent), m_pParameterItem(pParameterItem), m_pTableItem(pTableItem) { assert(pParameterItem != NULL && pTableItem != NULL); setText(QString::fromStdString(m_pParameterItem->GetStringValue())); connect(this, SIGNAL(editingFinished()), this, SLOT(slotOnEditingFinished())); SetValidator(pParameterItem->GetType(), this); //如果输入类型为NONEINPUT类型,则不允许编辑。 if(pParameterItem->GetInputType() == pai::module::NONEINPUT) { setReadOnly(true); } } void CParameterTableLineEdit::slotOnEditingFinished(const QString& text) { if(m_pParameterItem) { m_pParameterItem->SetStringValue(text.toStdString()); } if(m_pTableItem) { m_pTableItem->setText(text); } } void CParameterTableLineEdit::slotOnEditingFinished() { if(m_pParameterItem) { m_pParameterItem->SetStringValue(text().toStdString()); } if(m_pTableItem) { m_pTableItem->setText(text()); } } void CParameterTableLineEdit::focusOutEvent(QFocusEvent *pEvent) { QLineEdit::focusOutEvent(pEvent); if (m_pParameterItem == NULL) { return; } if (m_pParameterItem->GetType() == pai::module::ParmType_INT || m_pParameterItem->GetType() == pai::module::ParmType_LONG || m_pParameterItem->GetType() == pai::module::ParmType_FLOAT || m_pParameterItem->GetType() == pai::module::ParmType_DOUBLE) { QString currentText = text(); if (currentText.compare("+") == 0 || currentText.compare("-") == 0) { m_pParameterItem->SetStringValue(currentText.toStdString()); emit editFocuseOut(); } else if (currentText.isEmpty()) { m_pParameterItem->SetStringValue(""); emit editFocuseOut(); } } } CParameterTableCombobox::CParameterTableCombobox(CParameterItem* pParameterItem, QTableWidgetItem* pTableItem, QWidget* pParent) : PaiComboBox(pParent), m_pParameterItem(pParameterItem), m_pTableItem(pTableItem) { setMaxCount(MAX_VISIBLE_ITEM); QStringList popViewText = GetVisibleText(); addItems(popViewText); QString strInputMetaData = QString::fromStdString(pParameterItem->GetInputMetaData()); QStringList strComboboxTexts = strInputMetaData.split('/'); if(strComboboxTexts.count() > MAX_VISIBLE_ITEM-1) { model()->setData(model()->index(count()-1,0),QVariant(QColor("blue")),Qt::ForegroundRole); model()->setData(model()->index(count()-1,0),QVariant(Qt::AlignCenter),Qt::TextAlignmentRole); } if(QString::fromStdString(pParameterItem->GetInputData()).contains("Editable")) { setEditable(true); } assert(pParameterItem != NULL); pai::module::ParameterType eParamType = pParameterItem->GetType(); if (eParamType == pai::module::ParmType_BOOL) { setCurrentIndex(pParameterItem->GetValue ()); } else if (eParamType == pai::module::ParmType_INT || eParamType == pai::module::ParmType_LONG) { if (isEditable()) { if(pParameterItem->GetDefault()== g_szMin) { setEditText(g_szMin); pParameterItem->SetStringValue(GetMinValue(pParameterItem->GetType())); } else if(pParameterItem->GetDefault()== g_szMax) { setEditText(g_szMax); pParameterItem->SetStringValue(GetMaxValue(pParameterItem->GetType())); } else { setEditText(QString::fromStdString(pParameterItem->GetStringValue())); } } else { setCurrentIndex(pParameterItem->GetValue ()); } } else { int iIndex = findText(QString::fromStdString(pParameterItem->GetStringValue())); setCurrentIndex(iIndex); if (-1 == iIndex) { setEditText(QString::fromStdString(pParameterItem->GetStringValue())); } } if (isEditable()) { setCompleter(NULL); connect(this->lineEdit(),SIGNAL(editingFinished()),this,SLOT(slotOnIndexChanged())); QLineEdit * pLineEdit = lineEdit(); //目前可编辑的Combobox只是数字加上Min/Max QRegExp regExp("(^-?[1-9]\\d*$)|(^0$)|(^Max$)|(^Min$)"); QRegExpValidator *dator = new QRegExpValidator(regExp,pLineEdit); pLineEdit->setValidator(dator); } connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(slotOnIndexChanged())); } CParameterTableCombobox::~CParameterTableCombobox() { } void CParameterTableCombobox::ShowMoreDialog(int index) { if(index == MAX_VISIBLE_ITEM-1) { if(m_pTableItem) { m_pTableItem->setText(QString::fromStdString(m_pParameterItem->GetStringValue())); } setEditText(QString::fromStdString(m_pParameterItem->GetStringValue())); setCurrentIndex(findText(QString::fromStdString(m_pParameterItem->GetStringValue()))); //弹出搜索框 PaiListDialog listDlg(NULL); QString inputMetaData = QString::fromStdString(m_pParameterItem->GetInputMetaData()); QStringList comboBoxTexts = inputMetaData.split('/'); listDlg.SetTextList(comboBoxTexts); listDlg.move(mapToGlobal(pos())); connect(&listDlg,SIGNAL(SelectedAccepted(const QString&)),this,SLOT(SetText(const QString&)),Qt::UniqueConnection); listDlg.exec(); } } QStringList CParameterTableCombobox::GetVisibleText() { QStringList popViewText; if(m_pParameterItem) { QString strInputMetaData = QString::fromStdString(m_pParameterItem->GetInputMetaData()); QStringList strComboboxTexts = strInputMetaData.split('/'); if(strComboboxTexts.count() > MAX_VISIBLE_ITEM-1) { for(int i = 0; i < MAX_VISIBLE_ITEM; ++ i) { if(i == MAX_VISIBLE_ITEM-1) { popViewText << QString("Search all..."); break; } popViewText <GetDefault() == g_szMax) { m_pParameterItem->SetStringValue(GetMaxValue(m_pParameterItem->GetType())); text = g_szMax; } else { m_pParameterItem->SetStringValue(GetMinValue(m_pParameterItem->GetType())); text = g_szMin; } } else if (currentText() == "Ma") { m_pParameterItem->SetStringValue(GetMaxValue(m_pParameterItem->GetType())); text = g_szMax; } else if (currentText() == "Mi") { m_pParameterItem->SetStringValue(GetMinValue(m_pParameterItem->GetType())); text = g_szMin; } if(m_pTableItem != NULL) { m_pTableItem->setText(text); } setEditText(text); } void CParameterTableCombobox::SetText(const QString& text) { int index = findText(text); if(-1 != index) { setCurrentIndex(index); } else { removeItem(MAX_VISIBLE_ITEM-2); insertItem(0,text); setCurrentIndex(0); if(m_pTableItem) { m_pTableItem->setText(text); } setEditText(text); if(m_pParameterItem) { m_pParameterItem->SetStringValue(currentText().toStdString()); m_pParameterItem->SetDefault(currentText().toStdString()); } } } void CParameterTableCombobox::slotOnIndexChanged() { int nCurrentIndex = currentIndex(); if(m_pParameterItem != NULL) { if (m_pParameterItem->GetType() == pai::module::ParmType_BOOL) { m_pParameterItem->SetValue (nCurrentIndex == 0 ? false : true); } else if (m_pParameterItem->GetType() == pai::module::ParmType_INT) { if (nCurrentIndex != -1 && !isEditable()) { m_pParameterItem->SetValue (nCurrentIndex); } else { m_pParameterItem->SetStringValue(currentText().toStdString()); } } else { if(nCurrentIndex == MAX_VISIBLE_ITEM-1) { ShowMoreDialog(nCurrentIndex); return; } else { m_pParameterItem->SetStringValue(currentText().toStdString()); } } m_pParameterItem->SetDefault(currentText().toStdString()); if(currentText() == g_szMin) { m_pParameterItem->SetStringValue(GetMinValue(m_pParameterItem->GetType())); } else if(currentText() == g_szMax) { m_pParameterItem->SetStringValue(GetMaxValue(m_pParameterItem->GetType())); } else { } } if(m_pTableItem != NULL) { m_pTableItem->setText(currentText()); } emit stateChanged(); } /** * @class PaiParaTableDelegate * @brief workflow风格 item delegate */ class PaiParaTableItemDelegate: public QStyledItemDelegate { public: PaiParaTableItemDelegate(QTableView* tableView) : m_GridPen(QColor("#C9D5DC"), 0, tableView->gridStyle()), m_Hor(true), m_Ver(true) { } /** * @brief 是否显示表格线 * @param[in] h 水平方向线 * @param[in] v 垂直方向线 */ void setShowGrid(bool h, bool v) { m_Hor = h; m_Ver = v; } protected: /** * @brief 重新实现重绘函数 */ void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 opt = option; initStyleOption(&opt, index); opt.state = opt.state & ~QStyle::State_Selected; if((option.state & QStyle::State_Selected) && (option.state & QStyle::State_Enabled)) { opt.backgroundBrush = QBrush("#FEFEDC"); } else { opt.backgroundBrush = QBrush(Qt::white); } const QWidget *widget = opt.widget; Q_ASSERT(widget!=NULL); QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); painter->save(); painter->setPen(m_GridPen); if(m_Ver) // paint vertical lines { // painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); } if(m_Hor) // paint horizontal lines { painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); } painter->restore(); } private: QPen m_GridPen; bool m_Hor; bool m_Ver; }; ParamTableWgt::ParamTableWgt(QWidget* parent) : PaiTableWidget(parent), m_row(-1), m_col(-1), m_bError(false) { horizontalHeader()->setMinimumHeight(30); horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); setSortingEnabled(false); setItemDelegate(new PaiParaTableItemDelegate(this)); horizontalScrollBar()->installEventFilter(this); } ParamTableWgt::ParamTableWgt(int rows, int columns, QWidget* parent) : PaiTableWidget(parent), m_row(-1), m_col(-1), m_bError(false) { setRowCount(rows); setColumnCount(columns); horizontalHeader()->setMinimumHeight(30); horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | Qt::AlignVCenter); setSortingEnabled(false); setItemDelegate(new PaiParaTableItemDelegate(this)); horizontalScrollBar()->installEventFilter(this); } QSize ParamTableWgt::sizeHint() const { QSize newSizeHint(QTableWidget::sizeHint()); int height = horizontalHeader()->height() + contentsMargins().top() + contentsMargins().bottom(); int preferredRowCount = rowCount() > 5 ? 5 : rowCount(); for (int a = 0; a < preferredRowCount; ++a) { height += rowHeight(a); } QScrollBar *pHScroll = horizontalScrollBar(); if (pHScroll != NULL) { if (pHScroll->minimum() < pHScroll->maximum()) { height += pHScroll->sizeHint().height(); } } newSizeHint.setHeight(height); return newSizeHint; } void ParamTableWgt::setError(int row, int column, bool bError) { m_row = row; m_col = column; m_bError = bError; update(); } void ParamTableWgt::paintEvent(QPaintEvent* pEvent) { QTableWidget::paintEvent(pEvent); if(0 > m_row || 0 > m_col) return; QPainter painter(viewport()); painter.save(); painter.setRenderHint(QPainter::Antialiasing); const QPen penErr(Qt::red); painter.setPen(penErr); painter.drawRoundedRect(visualItemRect(item(m_row, m_col)), 2, 2); painter.restore(); } CParameterTableWidget::CParameterTableWidget(CParameterItem* pCompositeItem,QWidget* pParent) : QFrame(pParent), CParameterItemControl(pCompositeItem), m_iOldSelectRow(-1), m_pRootItem(dynamic_cast(pCompositeItem)), m_pRootItemDelegate(new pai::module::CTableParameterItemDelegate(m_pRootItem)) { setFrameStyle(QFrame::Box); setFrameShadow(QFrame::Raised); QVBoxLayout* pLayout = new QVBoxLayout; pLayout->setMargin(0); pLayout->setSpacing(0); m_pTable = new ParamTableWgt(this); m_pTable->setSelectionBehavior(QAbstractItemView::SelectRows); m_pTable->setSelectionMode(QAbstractItemView::SingleSelection); m_pTable->setShowGrid(true, false); m_pTable->setFrameStyle(QFrame::NoFrame); pLayout->addWidget(m_pTable); QHBoxLayout* pBottomLayout = new QHBoxLayout; m_pBtnAdd = new QToolButton(this); m_pBtnAdd->setText("Add Key"); m_pBtnAdd->setIcon(QIcon(":/add_02.png")); m_pBtnAdd->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); m_pBtnAdd->setAutoRaise(true); m_bBtnAddShouldShow = true; pBottomLayout->addWidget(m_pBtnAdd); pBottomLayout->addStretch(1); pLayout->addLayout(pBottomLayout); connect(m_pBtnAdd, SIGNAL(clicked()), this, SLOT(slotOnAddRow())); connect(m_pTable, SIGNAL(itemSelectionChanged()), this, SLOT(slotOnActiveRow())); setLayout(pLayout); } CParameterTableWidget::~CParameterTableWidget() { // TODO Auto-generated destructor stub if(m_pRootItemDelegate) { delete m_pRootItemDelegate; m_pRootItemDelegate = NULL; } } QSize CParameterTableWidget::sizeHint() const { if (m_pTable == NULL || m_pBtnAdd == NULL) return QFrame::sizeHint(); if (m_bBtnAddShouldShow == true) { return m_pTable->sizeHint() + QSize(0, m_pBtnAdd->height() + 2); } else { return m_pTable->sizeHint(); } } void CParameterTableWidget::paintEvent(QPaintEvent *pEvent) { QFrame::paintEvent(pEvent); QPainter painter(this); painter.save(); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(QColor("#D6DFE3")); painter.setBrush(QColor(Qt::white)); painter.drawRect(rect()); painter.restore(); } QTableWidget* CParameterTableWidget::GetTable() const { return m_pTable; } void CParameterTableWidget::SelectRow(bool toggled) { if(toggled) { bool stop = false; for(int col = 0; col < m_pTable->columnCount(); ++col) { for(int row = 0; row < m_pTable->rowCount(); ++row) { QWidget *pWidget = m_pTable->cellWidget(row,col); PaiRadioButton *pRadioButton = pWidget->findChild(); if(pRadioButton && pRadioButton->isChecked()) { CCompositeParameterItem* pRowItem = dynamic_cast (m_pRootItem->GetParameterItem(row)); if(pRowItem && pRowItem->GetParameterItem(col)) { if(sender() == pRadioButton) { pRadioButton->setChecked(true); pRowItem->GetParameterItem(col)->SetStringValue("1"); m_pTable->selectRow(row); stop = true; } else { pRadioButton->setChecked(false); pRowItem->GetParameterItem(col)->SetStringValue("0"); } } } } if(stop) { break; } } } } void CParameterTableWidget::SetCellRadioButton(int row, int col) { QWidget *pWidget = new QWidget(m_pTable); QHBoxLayout *hlayout=new QHBoxLayout(pWidget); //PaiRadioButton的构造函数,第一个参数必须传递,如果不显示文本就传个空进去,否则radioButton不显示 PaiRadioButton *pRadioButton = new PaiRadioButton(" ", pWidget); CCompositeParameterItem* pRowItem = dynamic_cast (m_pRootItem->GetParameterItem(row)); if(pRowItem && pRowItem->GetParameterItem(col)) { pRadioButton->setChecked(pRowItem->GetParameterItem(col)->GetStringValue() == "1" ? true:false); } hlayout->addStretch(); hlayout->addWidget(pRadioButton); hlayout->addStretch(); hlayout->setMargin(0); m_pTable->setCellWidget(row,col, pWidget); connect(pRadioButton,SIGNAL(toggled(bool)),this,SLOT(SelectRow(bool))); connect(pRadioButton,SIGNAL(toggled(bool)),this,SIGNAL(signalOnFinishEditingRowOrCell()),Qt::UniqueConnection); } void CParameterTableWidget::SetData(CCompositeParameterItem* pCompositeItem) { if (pCompositeItem == NULL) { return; } m_pTable->clear(); int iColumnCount = m_pRootItemDelegate->GetColumnCount(); if(iColumnCount == 0) { return; } m_pTable->setColumnCount(iColumnCount+1); m_pTable->setRowCount(m_pRootItemDelegate->GetRowCount()); QStringList lstColumnNames; for (int i =0; i< iColumnCount; ++i) { lstColumnNames << QString::fromStdString(m_pRootItemDelegate->GetColumnName(i)); } lstColumnNames << ""; m_pTable->setHorizontalHeaderLabels(lstColumnNames); bool bHasCheckBoxColumn = false; for (int i = 0; i < pCompositeItem->GetChildCount(); ++i) { CCompositeParameterItem* pRowItem = dynamic_cast (pCompositeItem->GetParameterItem(i)); assert(pRowItem != NULL); if (pRowItem != NULL) { assert(pRowItem->GetChildCount()+1 == m_pTable->columnCount()); for (int j = 0; j < pRowItem->GetChildCount(); ++j) { std::string strParameterItemValue = pRowItem->GetParameterItem(j)->GetStringValue(); if(pRowItem->GetParameterItem(j)->GetInputType() == pai::module::COMBOX) { strParameterItemValue = pRowItem->GetParameterItem(j)->GetDefault();//TODO 暂时用缺省值来代表combox表面的文本 pai::module::ParameterType type = pRowItem->GetParameterItem(j)->GetType(); std:: string inputData = pRowItem->GetParameterItem(j)->GetInputData(); if (type == pai::module::ParmType_BOOL || (type == pai::module::ParmType_INT && inputData.find(g_szEditable)==std::string::npos)) { QString strInputMetaData = QString::fromStdString(pRowItem->GetParameterItem(j)->GetInputMetaData()); QStringList strComboboxTexts = strInputMetaData.split('/'); if (!strComboboxTexts.contains(QString::fromStdString(strParameterItemValue))) { int index = atoi(strParameterItemValue.c_str()); if ((index > 0) && (index < strComboboxTexts.count()) ) { strParameterItemValue = strComboboxTexts.at(index).toStdString(); } else { strParameterItemValue = strComboboxTexts.at(0).toStdString(); } } } } QTableWidgetItem* pCellItem = new QTableWidgetItem(QString::fromStdString(strParameterItemValue)); if (pRowItem->GetParameterItem(j)->GetInputType() == pai::module::CHECKBOX) { m_pTable->resizeColumnToContents(j); bHasCheckBoxColumn = true; pCellItem->setFlags(pCellItem->flags() | Qt::ItemIsUserCheckable); pCellItem->setText(""); pCellItem->setCheckState(pRowItem->GetParameterItem(j)->GetStringValue() == "1" ? Qt::Checked : Qt::Unchecked); pCellItem->setData(Qt::UserRole, QString::fromStdString(pRowItem->GetParameterItem(j)->GetId())); } if (pRowItem->GetParameterItem(j)->GetInputType() == pai::module::RADIOBUTTON) { SetCellRadioButton(i,j); } else { m_pTable->setItem(i, j, pCellItem); } } } } m_pTable->horizontalHeader()->setStretchLastSection(true); if (m_pRootItem->GetInputData().find("NoneRowEdit") != std::string::npos) { m_pBtnAdd->setVisible(false); m_bBtnAddShouldShow = false; m_pTable->setColumnHidden(iColumnCount,true); } if (bHasCheckBoxColumn) { connect(m_pTable,SIGNAL(itemChanged (QTableWidgetItem*)),this,SLOT(slotOnCheckItem(QTableWidgetItem*)), Qt::UniqueConnection); } } void CParameterTableWidget::slotOnAddRow() { m_pTable->setRowCount(m_pTable->rowCount() + 1); CCompositeParameterItem* pRowItem = m_pRootItemDelegate->AddRow(); if(NULL == pRowItem) { return; } for(int col = 0; col < pRowItem->GetChildCount(); col++) { if (pRowItem->GetParameterItem(col)->GetInputType() == pai::module::RADIOBUTTON) { SetCellRadioButton(m_pTable->rowCount()-1,col); } else { m_pTable->setItem(m_pTable->rowCount()-1, col, new QTableWidgetItem(QString::fromStdString( pRowItem->GetParameterItem(col)->GetDefault()))); } } //设置删除列 m_pTable->setItem(m_pTable->rowCount()-1, m_pTable->columnCount()-1, new QTableWidgetItem()); m_pTable->selectRow(m_pTable->rowCount()-1); m_pTable->updateGeometry(); emit signalOnFinishEditingRowOrCell();//通知外界结构发生了改变,需要重新进行参数校验了。 } void CParameterTableWidget::slotOnRemoveRow(int iRow) { if(iRow>=m_pTable->rowCount())//TODO 现在程序中有些bug导致传入的iRow不在范围之内,这里采用临时做法改变这个输入值 { iRow = m_pTable->rowCount()-1; } if(iRow == m_pTable->rowCount()-1) { m_iOldSelectRow = iRow -1; } else { m_iOldSelectRow = iRow; } if(m_pRootItemDelegate->DeleteRow(iRow)) { m_pTable->removeRow(iRow); } //使删除按钮disable if(m_pTable->rowCount() == 1) { QList lstSelectItems = m_pTable->selectedItems(); if (lstSelectItems.count() > 0) { int nSelectRow = lstSelectItems[0]->row(); for(int col = 0;col columnCount(); ++col) { QWidget *pWidget = m_pTable->cellWidget(nSelectRow,col); CParameterTableActionIcon* pDeleteRowIcon = dynamic_cast(pWidget); if(pDeleteRowIcon) { pDeleteRowIcon->setEnabled(false); break; } } } } m_pTable->updateGeometry(); emit signalOnFinishEditingRowOrCell();//通知外界结构发生了改变,需要重新进行参数校验了。 } void CParameterTableWidget::slotOnActiveRow() { //删除上次选择行的编辑控件 if (m_iOldSelectRow != -1) { CCompositeParameterItem* pRowItem = dynamic_cast (m_pRootItem->GetParameterItem( m_iOldSelectRow)); if (pRowItem == NULL) { return; } for (int i = 0; i < m_pRootItemDelegate->GetColumnCount(); ++i) { if (pRowItem->GetParameterItem(i)->GetInputType() == pai::module::RADIOBUTTON) { //do nothing } else { m_pTable->setCellWidget(m_iOldSelectRow, i, NULL); } } m_pTable->setCellWidget(m_iOldSelectRow,m_pTable->columnCount()-1,NULL); } QList lstSelectItems = m_pTable->selectedItems(); if (lstSelectItems.count() > 0) { int nSelectRow = lstSelectItems[0]->row(); // pai::log::Debug(_FLF("select row: " + pai::utils::CUtils::IntToStr(nSelectRow))); CCompositeParameterItem* pRowItem = dynamic_cast (m_pRootItem->GetParameterItem( nSelectRow)); if (pRowItem == NULL) { return; } assert(pRowItem->GetChildCount()+1 == m_pTable->columnCount()); for (int i = 0; i < pRowItem->GetChildCount(); ++i) { CParameterItem* pCellItem = pRowItem->GetParameterItem(i); assert(pCellItem != NULL); //根据子参数项的控件类型生成合适的单元格编辑控件 if (pCellItem->GetInputType() == pai::module::COMBOX) { CParameterTableCombobox *pEdit = new CParameterTableCombobox(pCellItem, m_pTable->item(nSelectRow, i), m_pTable); pEdit->setStyleSheet(QString::fromUtf8("QComboBox{margin:4px;}")); // 设置单元格内边距 m_pTable->setCellWidget(nSelectRow, i, pEdit); connect(pEdit, SIGNAL(stateChanged()),this,SIGNAL(signalOnFinishEditingRowOrCell())); } else if (pCellItem->GetInputType() == pai::module::CHECKBOX) { // pEdit->setStyleSheet(QString::fromUtf8("margin:4px;")); } else if (pCellItem->GetInputType() == pai::module::RADIOBUTTON) { //do nothing } else { CParameterTableLineEdit *pEdit = new CParameterTableLineEdit(pCellItem, m_pTable->item(nSelectRow, i), m_pTable); pEdit->setStyleSheet(QString::fromUtf8("margin:4px;")); // 设置单元格内边距 m_pTable->setCellWidget(nSelectRow, i, pEdit); connect(pEdit, SIGNAL(editingFinished()),this,SIGNAL(signalOnFinishEditingRowOrCell())); connect(pEdit, SIGNAL(editFocuseOut()),this,SIGNAL(signalOnFinishEditingRowOrCell())); } } //添加行删除图标 CParameterTableActionIcon* pDeleteRowIcon = new CParameterTableActionIcon(":/c.png",nSelectRow,this); pDeleteRowIcon->setAlignment(Qt::AlignRight|Qt::AlignVCenter); pDeleteRowIcon->setFrameShape(QFrame::NoFrame); pDeleteRowIcon->setStyleSheet(QString::fromUtf8("margin-right:10px;")); // 设置单元格内边距 if(1 == m_pTable->rowCount()) { pDeleteRowIcon->setEnabled(false); } else { pDeleteRowIcon->setEnabled(true); connect(pDeleteRowIcon,SIGNAL(signalIconClicked(int)),this,SLOT(slotOnRemoveRow(int))); } m_pTable->setCellWidget(nSelectRow, m_pTable->columnCount()-1, pDeleteRowIcon); m_iOldSelectRow = nSelectRow; } else { m_iOldSelectRow = -1; } } void CParameterTableWidget::slotOnCheckItem(QTableWidgetItem* pCheckItem) { if (pCheckItem != NULL && (pCheckItem->flags() & Qt::ItemIsUserCheckable)) { QString strParameterItemID = pCheckItem->data(Qt::UserRole).toString(); if (strParameterItemID != "") { CParameterItem* pBoolParameterItem = m_pRootItem->GetParameterItem(strParameterItemID.toStdString()); if (NULL != pBoolParameterItem) { pBoolParameterItem->SetValue (pCheckItem->checkState() == Qt::Checked ? true : false); emit signalOnFinishEditingRowOrCell(); } } } } QString CParameterTableWidget::ToJsonString() const { return m_pRootItem==NULL?"":QString::fromStdString(m_pRootItem->GetStringValue()); } void CParameterTableWidget::setError(const QString &errMsg) { if(errMsg.isEmpty()) { m_pTable->setError(-1, -1, false); return; } int col = 0, row = errMsg.lastIndexOf('['); row = errMsg.mid(row+1, errMsg.lastIndexOf(']') - row-1).toInt(); std::string strMsg(errMsg.right(errMsg.size() - 1 - errMsg.lastIndexOf('.')).toStdString()); if (m_pRootItemDelegate == NULL || m_pTable == NULL) return; int iColumnCount = m_pRootItemDelegate->GetColumnCount(); for(int i=0;iGetColumnName(i) == strMsg) break; ++ col; } m_pTable->setError(row, col, !errMsg.isEmpty()); } void CParameterTableWidget::ShowAddButton(bool bShow) { m_pBtnAdd->setVisible(bShow); m_bBtnAddShouldShow = bShow; } QVariant CParameterTableWidget::GetDisplayValue() const { return QVariant(); } QVariant CParameterTableWidget::GetValue() const { return QVariant(); } void CParameterTableWidget::SetDisplayValue(const QVariant& /*varDisplayValue*/) { SetData(dynamic_cast(m_pParameterItem)); } void CParameterTableWidget::SetValue(const QVariant& /*varValue*/) { } SortParamTableWidget::SortParamTableWidget(CParameterItem* pCompositeItem, QWidget* pParent) : CParameterTableWidget(pCompositeItem, pParent) { } SortParamTableWidget::~SortParamTableWidget() { } void SortParamTableWidget::SetData(pai::module::CCompositeParameterItem* pCompositeItem) { CParameterTableWidget::SetData(pCompositeItem); if (pCompositeItem == NULL) { return; } for (int i = 0; i < pCompositeItem->GetChildCount(); ++i) { CCompositeParameterItem* pRowItem = dynamic_cast(pCompositeItem->GetParameterItem(i)); if (pRowItem != NULL) { for (int j = 0; j < pRowItem->GetChildCount(); ++j) { QString strParameterItemValue = QString::fromStdString( pRowItem->GetParameterItem(j)->GetStringValue()); if (IsKeyValueItem(i, j)) { if (strParameterItemValue.isEmpty()) { strParameterItemValue = QObject::tr("Select..."); QTableWidgetItem* pCellItem = m_pTable->item(i, j); if (pCellItem) { pCellItem->setText(strParameterItemValue); } } } } } } } void SortParamTableWidget::slotOnAddRow() { m_pTable->setRowCount(m_pTable->rowCount() + 1); CCompositeParameterItem* pRowItem = m_pRootItemDelegate->AddRow(); if(NULL == pRowItem) { return; } for(int col = 0; col < pRowItem->GetChildCount(); col++) { QTableWidgetItem *pItem = new QTableWidgetItem( QString::fromStdString(pRowItem->GetParameterItem(col)->GetDefault())); QString strColumnName = QString::fromStdString(m_pRootItemDelegate->GetColumnName(col)); if (pRowItem->GetParameterItem(col)->GetInputType() == pai::module::CHECKBOX) { pItem->setFlags(pItem->flags() | Qt::ItemIsUserCheckable); pItem->setText(""); pItem->setCheckState(Qt::Unchecked); pItem->setData(Qt::UserRole, QString::fromStdString(pRowItem->GetParameterItem(col)->GetId())); } else if (IsKeyValueItem(m_pTable->rowCount()-1, col)) { pItem->setText(QObject::tr("Select...")); } m_pTable->setItem(m_pTable->rowCount()-1, col, pItem); } //设置删除列 m_pTable->setItem(m_pTable->rowCount()-1, m_pTable->columnCount()-1, new QTableWidgetItem()); m_pTable->selectRow(m_pTable->rowCount()-1); //m_pTable->updateGeometry(); emit signalOnFinishEditingRowOrCell();//通知外界结构发生了改变,需要重新进行参数校验了。 } void SortParamTableWidget::slotOnActiveRow() { CParameterTableWidget::slotOnActiveRow(); QList lstSelectItems = m_pTable->selectedItems(); if (lstSelectItems.count() <= 0) { return; } int nSelectRow = lstSelectItems[0]->row(); CCompositeParameterItem* pRowItem = dynamic_cast( m_pRootItem->GetParameterItem(nSelectRow)); if (pRowItem != NULL) { bool disableRow = false; for (int i = 0; i < m_pRootItemDelegate->GetColumnCount(); i++) { CParameterItem* pCellItem = pRowItem->GetParameterItem(i); if (IsKeyValueItem(nSelectRow, i)) { disableRow = pCellItem->GetStringValue().empty() ? true : false; CParameterTableCombobox *pEdit = dynamic_cast(m_pTable->cellWidget(nSelectRow, i)); if (pEdit) { connect(pEdit, SIGNAL(stateChanged()), this, SLOT(slotOnComboxStateChanged()), Qt::UniqueConnection); } break; } } if (disableRow) { for (int i = 0; i < pRowItem->GetChildCount(); ++i) { if (!IsKeyValueItem(nSelectRow, i)) { QWidget *pEdit = m_pTable->cellWidget(nSelectRow, i); if (pEdit) { pEdit->setDisabled(true); } } } } } } void SortParamTableWidget::slotOnCheckItem(QTableWidgetItem* pCheckItem) { CParameterTableWidget::slotOnCheckItem(pCheckItem); if (pCheckItem != NULL && (pCheckItem->flags() & Qt::ItemIsUserCheckable)) { if (pCheckItem->checkState() == Qt::Checked) { for (int rowNum = 0; rowNum < m_pTable->rowCount(); rowNum++) { if (rowNum == pCheckItem->row()) continue; QTableWidgetItem *pItem = m_pTable->item(rowNum, pCheckItem->column()); if (pItem) { pItem->setCheckState(Qt::Unchecked); QString strParamItemID = pItem->data(Qt::UserRole).toString(); if (strParamItemID != "") { CParameterItem* pBoolParamItem = m_pRootItem->GetParameterItem(strParamItemID.toStdString()); if (pBoolParamItem) { pBoolParamItem->SetValue (false); } } } } emit signalOnFinishEditingRowOrCell(); } } } void SortParamTableWidget::slotOnComboxStateChanged() { QList lstSelectItems = m_pTable->selectedItems(); if (lstSelectItems.count() <= 0) { return; } int selectRow = lstSelectItems[0]->row(); bool enable = false; CParameterTableCombobox *pEdit = dynamic_cast(sender()); if (pEdit) { for (int col = 0; col < m_pTable->columnCount(); col++) { if (pEdit == m_pTable->cellWidget(selectRow, col) && !pEdit->currentText().isEmpty()) { enable = true; break; } } if (enable) { for (int col = 0; col < m_pTable->columnCount(); col++) { CParameterTableCombobox *pParamCombox = dynamic_cast( m_pTable->cellWidget(selectRow, col)); if (pParamCombox && pParamCombox != pEdit) { pParamCombox->setEnabled(true); } } } } } bool SortParamTableWidget::IsKeyValueItem(int row, int column) { CCompositeParameterItem* pRowItem = dynamic_cast(m_pRootItem->GetParameterItem(row)); if (!pRowItem) { return false; } QString sColumnName = QString::fromStdString(m_pRootItemDelegate->GetColumnName(column)); CParameterItem* pCellItem = pRowItem->GetParameterItem(column); if (pCellItem && pCellItem->GetInputType() == pai::module::COMBOX && sColumnName == QObject::tr("Key")) { return true; } return false; }