#include "ModelViewTable.h" #include "ui_ModelViewTable.h" #include "dbService/DBServiceSet.h" #include "dbService/NodeMatrixService.h" #include #include #define SET_TABVIEWNAME(name) m_tableView##name //动态命名变量名 #define SET_TABMODELNAME(name) m_tableModel##name ModelViewTable::ModelViewTable(QWidget *parent) : QWidget(parent), ui(new Ui::ModelViewTable) { ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); } void ModelViewTable::init(QList &infos) { datas = infos; // new对象 m_widget = new QWidget(); m_stackWidget = new QStackedWidget(); lastPageBtn = new QPushButton("上一页"); nextPageBtn = new QPushButton("下一页"); restoreBtn = new QPushButton("保存"); m_frame = new QFrame(this); //布局 mainLayout = new QVBoxLayout(); frameLayout = new QVBoxLayout(); btnLayout = new QHBoxLayout(); btnLayout->addWidget(lastPageBtn); btnLayout->addWidget(nextPageBtn); btnLayout->addWidget(restoreBtn); frameLayout->addWidget(m_stackWidget); frameLayout->addLayout(btnLayout); m_frame->setLayout(frameLayout); mainLayout->addWidget(m_frame); m_widget->setLayout(mainLayout); this->setLayout(mainLayout); connect(nextPageBtn, &QPushButton::clicked, this, &ModelViewTable::switchToNextPage); connect(lastPageBtn, &QPushButton::clicked, this, &ModelViewTable::switchToLastPage); connect(restoreBtn, &QPushButton::clicked, this, &ModelViewTable::saveResult); stackedWidgetPages = getStackedWidgetPages(datas); // 根据节点列判断页数:节点中多有多少个分隔点,就有多少页 addStackWidgetPage(stackedWidgetPages); setTableWidgetContent(datas, pageTabAndCorrTableModels); } void ModelViewTable::saveResult() { getTableViewContent(pageTabAndCorrTableModels); if (!NodeMatrixService().AddNodeMatrixInfoList(datas)) { QMessageBox::critical(this, "异常", "数据库存储异常!"); } else { QMessageBox::information(this, "成功", "数据库存储成功!"); } } int ModelViewTable::getStackedWidgetPages(QList datas) { int pages = -1; // 异常处理 datas为空 for (auto temp : datas) { QStringList list = (temp->node).split("."); if (list.length() > pages) { pages = list.length(); } } return pages - 1; } void ModelViewTable::addStackWidgetPage(int pages) { QStringList tableNames; QSet names; // 异常处理 datas为空 for (auto temp : datas) { QStringList list = (temp->node).split("."); if (list.length() == 2) { names << temp->abscissa; } else { continue; } } for (auto temp : names) { tableNames << temp; } for (int i = 0; i < pages; i++) { if (i == 0) { QWidget *m_widget = new QWidget(); QTableView *m_tableView_page1 = new QTableView(); QStandardItemModel *m_model_page1 = createModel(tableNames, indexForMeasureMehods.isEmpty() ? tableNames : indexForMeasureMehods); m_tableView_page1->setModel(m_model_page1); QVBoxLayout *m_layout = new QVBoxLayout(); m_layout->addWidget(m_tableView_page1); m_widget->setLayout(m_layout); m_stackWidget->addWidget(m_widget); tabAndCorrTableModel.insert("", m_model_page1); pageTabAndCorrTableModels.insert(0, tabAndCorrTableModel); tabAndCorrTableModel.clear(); } else { QWidget *m_widget = new QWidget(); QTabWidget *m_tabWidget = createTabWidget(getTabWidgetHeaderLabel(i), i); QVBoxLayout *m_layout = new QVBoxLayout(); m_layout->addWidget(m_tabWidget); m_widget->setLayout(m_layout); m_stackWidget->addWidget(m_widget); } } } QTabWidget *ModelViewTable::createTabWidget(QMap> corrElements, int currentPage) { int tabPages = corrElements.keys().length(); // qDebug() << corrElements.keys().at(0); // qDebug() << corrElements.values().at(0).toList(); QTabWidget *m_tabWidget = new QTabWidget(); for (int i = 0; i < tabPages; i++) { QWidget *m_widget = new QWidget(); QVBoxLayout *m_layout = new QVBoxLayout(); QTableView *SET_TABVIEWNAME(i) = new QTableView(); QStandardItemModel *SET_TABMODELNAME(i) = createModel(corrElements.values().at(i).toList(), corrElements.values().at(i).toList()); SET_TABVIEWNAME(i)->setModel(SET_TABMODELNAME(i)); m_layout->addWidget(SET_TABVIEWNAME(i)); m_widget->setLayout(m_layout); m_tabWidget->addTab(m_widget, corrElements.keys().at(i)); tabAndCorrTableModel.insert(corrElements.keys().at(i), SET_TABMODELNAME(i)); } pageTabAndCorrTableModels.insert(currentPage, tabAndCorrTableModel); tabAndCorrTableModel.clear(); return m_tabWidget; } QMap> ModelViewTable::getTabWidgetHeaderLabel(int currentStackWidgetPage) { QString tabElement; QMap> corrElements; for (auto temp : datas) { QStringList oneList = (temp->node).split("."); if (currentStackWidgetPage == (oneList.length() - 1)) { QSet elements; tabElement = temp->abscissa; QStringList tabList = (temp->node).split("."); for (auto nextTmp : datas) { QStringList twoList = (nextTmp->node).split("."); if ((currentStackWidgetPage + 2) == twoList.length()) { QStringList nextList = (nextTmp->node).split("."); if (IsQStringListEqual(tabList, nextList)) { elements << nextTmp->abscissa; } } } corrElements.insert(tabElement, elements); } } // qDebug() << "corrElements.keys = " << corrElements.keys(); // qDebug() << "corrElements.values = " << corrElements.values(); // return elements.size(); return corrElements; } QList ModelViewTable::getTableViewContent(QMap> pageTabAndCorrTables) { QList pages = pageTabAndCorrTables.keys(); QList> tabAndTable = pageTabAndCorrTables.values(); for (int i = 0; i < pages.length(); i++) { int page = pages.at(i); if (page == 0) { QMap tmpTabAndTable = tabAndTable[0]; QStandardItemModel *tmpModel = tmpTabAndTable.values()[0]; QString rowHeaderLabels; QString colHeaderLabels; for (int rowlen = 0; rowlen < tmpModel->rowCount(); rowlen++) { rowHeaderLabels = tmpModel->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tmpModel->columnCount(); collen++) { colHeaderLabels = tmpModel->headerData(collen, Qt::Horizontal).toString(); for (int index = 0; index < datas.length(); index++) { QStringList list = (datas[index]->node).split("."); if (list.length() == 2) { if (rowHeaderLabels == datas.at(index)->abscissa && colHeaderLabels == datas.at(index)->ordinate) { datas[index]->nodeValue = tmpModel->item(rowlen, collen)->text().toDouble(); } } else { continue; } } } } } else { QMap tmpTabAndTable = tabAndTable[i]; QList tabNames = tmpTabAndTable.keys(); QList modelNames = tmpTabAndTable.values(); QString rowHeaderLabels; QString colHeaderLabels; for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) { QString tabName = tabNames[tabIndex]; QStandardItemModel *modelName = modelNames[tabIndex]; for (int rowlen = 0; rowlen < modelName->rowCount(); rowlen++) { rowHeaderLabels = modelName->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < modelName->columnCount(); collen++) { colHeaderLabels = modelName->headerData(collen, Qt::Horizontal).toString(); for (int index = 0; index < datas.length(); index++) { QStringList list = (datas[index]->node).split("."); if (list.length() == page + 2) { QStringList dataNode; QStringList tableNode; // 数据node for (int nodelen = 0; nodelen < page + 1; nodelen++) { dataNode << list[nodelen]; } // 表格node tableNode = dataNode; tableNode.replace(page, QString::number(tabIndex + 1)); if (IsQStringListEqual(dataNode, tableNode) && rowHeaderLabels == datas.at(index)->abscissa && colHeaderLabels == datas.at(index)->ordinate) { datas[index]->nodeValue = modelName->item(rowlen, collen)->text().toDouble(); } } else { continue; } } } } } } } // for(auto tmp:datas) // { // qDebug() <node<abscissa << tmp->ordinate << tmp->nodeValue; // } // qDebug() << datas.length(); return datas; } void ModelViewTable::setTableWidgetContent(QList datas, QMap> pageTabAndCorrTables) { QList pages = pageTabAndCorrTables.keys(); QList> tabAndTable = pageTabAndCorrTables.values(); for (int i = 0; i < pages.length(); i++) { int page = pages.at(i); if (page == 0) { QMap tmpTabAndTable = tabAndTable[0]; QStandardItemModel *tmpModel = tmpTabAndTable.values()[0]; QString rowHeaderLabels; QString colHeaderLabels; for (int rowlen = 0; rowlen < tmpModel->rowCount(); rowlen++) { rowHeaderLabels = tmpModel->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tmpModel->columnCount(); collen++) { colHeaderLabels = tmpModel->headerData(collen, Qt::Horizontal).toString(); for (int index = 0; index < datas.length(); index++) { QStringList list = (datas.at(index)->node).split("."); if (list.length() == 2) { if (rowHeaderLabels == datas.at(index)->abscissa && colHeaderLabels == datas.at(index)->ordinate) { QStandardItem *item = new QStandardItem(QString("%1").arg(datas[index]->nodeValue)); tmpModel->setItem(rowlen, collen, item); } } else { continue; } } } } } else { QMap tmpTabAndTable = tabAndTable[i]; QList tabNames = tmpTabAndTable.keys(); QList modelNames = tmpTabAndTable.values(); QString rowHeaderLabels; QString colHeaderLabels; for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) { QString tabName = tabNames[tabIndex]; QStandardItemModel *modelName = modelNames[tabIndex]; for (int rowlen = 0; rowlen < modelName->rowCount(); rowlen++) { rowHeaderLabels = modelName->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < modelName->columnCount(); collen++) { colHeaderLabels = modelName->headerData(collen, Qt::Horizontal).toString(); for (int index = 0; index < datas.length(); index++) { QStringList list = (datas.at(index)->node).split("."); if (list.length() == page + 2) { QStringList dataNode; QStringList tableNode; // 数据node for (int nodelen = 0; nodelen < page + 1; nodelen++) { dataNode << list[nodelen]; } // 表格node tableNode = dataNode; tableNode.replace(page, QString::number(tabIndex + 1)); if (IsQStringListEqual(dataNode, tableNode) && rowHeaderLabels == datas.at(index)->abscissa && colHeaderLabels == datas.at(index)->ordinate) { QStandardItem *item = new QStandardItem(QString("%1").arg(datas[index]->nodeValue)); modelName->setItem(rowlen, collen, item); } } else { continue; } } } } } } } return; } bool ModelViewTable::IsTableEmpty(QStandardItemModel *tableModel) { int rowCount = tableModel->rowCount(); int colCount = tableModel->columnCount(); for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { if (tableModel->item(row, col) == NULL) { QMessageBox::information(NULL, "警告", "表格有空值!!!"); return false; } } } return true; } bool ModelViewTable::IsQStringListEqual(QStringList tabList, QStringList nextList) { for (int i = 0; i < nextList.length() - 1; i++) { if (tabList[i] != nextList[i]) { return false; } } return true; } void ModelViewTable::switchToNextPage() { int index = m_stackWidget->currentIndex(); index++; if (index < stackedWidgetPages) { m_stackWidget->setCurrentIndex(index); } else { index = stackedWidgetPages - 1; QMessageBox::information(NULL, "提示:", "当前为最后一页!"); } } void ModelViewTable::switchToLastPage() { int index = m_stackWidget->currentIndex(); index--; if (index >= 0) { m_stackWidget->setCurrentIndex(index); } else { index = 0; QMessageBox::information(NULL, "提示:", "当前为第一页!"); } } ModelViewTable::~ModelViewTable() { qDeleteAll(datas); datas.clear(); delete ui; } QStandardItemModel *ModelViewTable::createModel(QStringList rowHeaderList, QStringList colHeaderList) { QStandardItemModel *m_model = new QStandardItemModel(rowHeaderList.count(), rowHeaderList.count(), this); m_model->setHorizontalHeaderLabels(rowHeaderList); m_model->setVerticalHeaderLabels(colHeaderList); return m_model; }