#include "MatrixTableTechMeasuresWidget.h" #include "MatrixTableItemDelegate.h" #include #include #include #include #include #include MatrixTableTechMeasuresWidget::MatrixTableTechMeasuresWidget(QWidget *parent) : QTableView(parent) { model = new QStandardItemModel(); this->setModel(model); paintDone = false; delegate = new SpinBoxDelegate(this); this->setItemDelegate(delegate); connect(delegate, &SpinBoxDelegate::dataChanged, this, &MatrixTableTechMeasuresWidget::itemClicked); } MatrixTableTechMeasuresWidget::~MatrixTableTechMeasuresWidget() { } void MatrixTableTechMeasuresWidget::addRowNode(QString node, QString name, QString remark) { matrixRows << MatrixTableTechMeasuresWidget::MatrixNode(node, name, remark); } void MatrixTableTechMeasuresWidget::addColNode(QString node, QString name, QString remark) { matrixCols << MatrixTableTechMeasuresWidget::MatrixNode(node, name, remark); } QList MatrixTableTechMeasuresWidget::getSource() const { return dataSource; } void MatrixTableTechMeasuresWidget::setTableName(QString name) { m_msg_name = name; } void MatrixTableTechMeasuresWidget::paintMatrixTable(QList nodeValueInfoList) { int row = matrixRows.count(); int col = matrixCols.count(); this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); this->setSelectionMode(QAbstractItemView::SingleSelection); this->setSelectionBehavior(QAbstractItemView::SelectItems); model->setHorizontalHeaderItem(0, new QStandardItem("")); for (int c = 0; c < col; ++c) { model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name)); model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].remark); } for (int r = 0; r < row; ++r) { model->setItem(r, 0, new QStandardItem(matrixRows[r].name)); model->item(r, 0)->setToolTip(matrixRows[r].remark); model->item(r, 0)->setTextAlignment(Qt::AlignCenter); model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); for (int c = 1; c < col + 1; ++c) { MatrixDataSource data; data.row = r; data.col = c; data.changed = true; // data.nodeValue = ""; // qDebug() << matrixRows[r].name << "---" << matrixCols[c - 1].name << "--" << nodeValueInfoList.size(); for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) { // qDebug() << nodeMatrixInfo->abscissa << "---" << nodeMatrixInfo->ordinate << "---" // << nodeMatrixInfo->nodeValue; if (nodeMatrixInfo->abscissa == matrixRows[r].name && nodeMatrixInfo->ordinate == matrixCols[c - 1].name) { data.nodeValue = nodeMatrixInfo->nodeValue; data.changed = true; } } QModelIndex index = model->index(r, c, QModelIndex()); QVariant initData = 0; if (data.nodeValue != nullptr && data.nodeValue != "") { initData = data.nodeValue; } model->setData(index, initData); // model->setItem(r, c, new QStandardItem(data.nodeValue)); model->item(r, c)->setTextAlignment(Qt::AlignCenter); data.node = matrixCols[c - 1].node; data.abscissa = matrixRows[r].name; data.ordinate = matrixCols[c - 1].name; dataSource << data; } } emit returnModel(model); emit returnModelName(m_msg_name); paintDone = true; } void MatrixTableTechMeasuresWidget::itemClicked(const QModelIndex &index) { // qDebug() << "itemClicked" << index << model->itemFromIndex(index)->text() << endl; // QStringList l = { "1/9", "1/7", "1/5", "1/3", "1", "3", "5", "7", "9" }; // SchemeBar *scheme = // new SchemeBar(model->item(index.row(), 0)->text(), // model->horizontalHeaderItem(index.column())->text(), l); // scheme->setModal(true); // scheme->setAttribute(Qt::WA_DeleteOnClose); // connect(scheme, &SchemeBar::setValue, [=](QString val) { editItemData(index, val); }); // scheme->show(); // QPoint p = QCursor::pos(); // if (p.x() + scheme->width() + 10 >= QApplication::desktop()->width()) { // p.setX(QApplication::desktop()->width() - 10 - scheme->width()); // } // scheme->move(p); editItemData(index, model->itemFromIndex(index)->text()); } void MatrixTableTechMeasuresWidget::editItemData(const QModelIndex &index, const QString &val) { qDebug() << "-----" << index << val; model->itemFromIndex(index)->setText(val); QStandardItem *item = model->itemFromIndex(index); // qDebug() << "dataSource.count()" << dataSource.count(); for (int i = 0; i < dataSource.count(); ++i) { // qDebug() << item->text(); if (dataSource[i].row == item->row() && dataSource[i].col == item->column()) { dataSource[i].nodeValue = item->text().trimmed(); if (!(item == NULL) && !item->text().trimmed().isEmpty()) { dataSource[i].changed = true; } else { dataSource[i].changed = false; } } } // emit dataReady(isDataReady()); } bool MatrixTableTechMeasuresWidget::isDataReady() const { if (dataSource.count() == 0) { return false; } for (MatrixDataSource data : dataSource) { if (data.changed == false) { return false; } } return true; }