123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- #include "ModelViewTable.h"
- #include "ui_ModelViewTable.h"
- #include "dbService/DBServiceSet.h"
- #include "dbService/NodeMatrixService.h"
- #include <QDebug>
- #include <QMessageBox>
- #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<NodeMatrixInfo *> &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<NodeMatrixInfo *> 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<QString> 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<QString, QSet<QString>> 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<QString, QSet<QString>> ModelViewTable::getTabWidgetHeaderLabel(int currentStackWidgetPage)
- {
- QString tabElement;
- QMap<QString, QSet<QString>> corrElements;
- for (auto temp : datas) {
- QStringList oneList = (temp->node).split(".");
- if (currentStackWidgetPage == (oneList.length() - 1)) {
- QSet<QString> 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<NodeMatrixInfo *>
- ModelViewTable::getTableViewContent(QMap<int, QMap<QString, QStandardItemModel *>> pageTabAndCorrTables)
- {
- QList<int> pages = pageTabAndCorrTables.keys();
- QList<QMap<QString, QStandardItemModel *>> tabAndTable = pageTabAndCorrTables.values();
- for (int i = 0; i < pages.length(); i++) {
- int page = pages.at(i);
- if (page == 0) {
- QMap<QString, QStandardItemModel *> 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<QString, QStandardItemModel *> tmpTabAndTable = tabAndTable[i];
- QList<QString> tabNames = tmpTabAndTable.keys();
- QList<QStandardItemModel *> 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() <<tmp->node<<tmp->abscissa << tmp->ordinate << tmp->nodeValue;
- // }
- // qDebug() << datas.length();
- return datas;
- }
- void ModelViewTable::setTableWidgetContent(QList<NodeMatrixInfo *> datas,
- QMap<int, QMap<QString, QStandardItemModel *>> pageTabAndCorrTables)
- {
- QList<int> pages = pageTabAndCorrTables.keys();
- QList<QMap<QString, QStandardItemModel *>> tabAndTable = pageTabAndCorrTables.values();
- for (int i = 0; i < pages.length(); i++) {
- int page = pages.at(i);
- if (page == 0) {
- QMap<QString, QStandardItemModel *> 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<QString, QStandardItemModel *> tmpTabAndTable = tabAndTable[i];
- QList<QString> tabNames = tmpTabAndTable.keys();
- QList<QStandardItemModel *> 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;
- }
|