#include "ProfessorInputTable.h" #include "ui_ProfessorInputTable.h" #include #include #include "dbService/ClassSet.h" #include #define SET_NAME(name) m_tableWidget##name //动态命名变量名 ProfessorInputTable::ProfessorInputTable(QWidget *parent) : QDialog(parent), ui(new Ui::ProfessorInputTable) { ui->setupUi(this); datas = ClassSet().datas; // int pages = 2; // 根据节点列判断页数:节点中多有多少个分隔点,就有多少页 stackedWidgetPages = getStackedWidgetPages(); // qDebug() << "pages = " << stackedWidgetPages; InitInterface(); addStackWidgetPage(stackedWidgetPages); Init(); } int ProfessorInputTable::getStackedWidgetPages() { int pages = -1; // 异常处理 datas为空 for (auto temp : datas) { QStringList list = (temp->node).split("."); if (list.length() > pages) { pages = list.length(); } } return pages - 1; } bool ProfessorInputTable::IsQStringListEqual(QStringList tabList, QStringList nextList) { for (int i = 0; i < nextList.length() - 1; i++) { if (tabList[i] != nextList[i]) { return false; } } return true; } QMap> ProfessorInputTable::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; } void ProfessorInputTable::Init() { connect(nextPageBtn, &QPushButton::clicked, this, &ProfessorInputTable::switchToNextPage); connect(lastPageBtn, &QPushButton::clicked, this, &ProfessorInputTable::switchToLastPage); connect(restoreBtn, &QPushButton::clicked, this, &ProfessorInputTable::testfunction); } ProfessorInputTable::~ProfessorInputTable() { delete ui; } void ProfessorInputTable::InitInterface() { // 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); } void ProfessorInputTable::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(); QTableWidget *m_tableWidget_page1 = createTable(tableNames); QVBoxLayout * m_layout = new QVBoxLayout(); m_layout->addWidget(m_tableWidget_page1); m_widget->setLayout(m_layout); m_stackWidget->addWidget(m_widget); tabAndCorrTable.insert("", m_tableWidget_page1); pageTabAndCorrTables.insert(0, tabAndCorrTable); tabAndCorrTable.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 *ProfessorInputTable::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(); QTableWidget *SET_NAME(i) = createTable(corrElements.values().at(i).toList()); m_layout->addWidget(SET_NAME(i)); m_widget->setLayout(m_layout); m_tabWidget->addTab(m_widget, corrElements.keys().at(i)); tabAndCorrTable.insert(corrElements.keys().at(i), SET_NAME(i)); } pageTabAndCorrTables.insert(currentPage, tabAndCorrTable); tabAndCorrTable.clear(); return m_tabWidget; } QTableWidget *ProfessorInputTable::createTable(QStringList tableNames) { QTableWidget *m_tableWidget = new QTableWidget(); m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems); m_tableWidget->setRowCount(tableNames.count()); // 设置行数 m_tableWidget->setColumnCount(tableNames.count()); // 设置列数 m_tableWidget->setHorizontalHeaderLabels(tableNames); // 水平表头 m_tableWidget->setVerticalHeaderLabels(tableNames); // 垂直表头 m_tableWidget->setShowGrid(true); // 网格线可见 this->setFocusPolicy(Qt::NoFocus); //解决选中虚框问题 return m_tableWidget; } bool ProfessorInputTable::IsTableEmpty(QTableWidget *tableWidget) { int rowCount = tableWidget->rowCount(); int colCount = tableWidget->columnCount(); for (int row = 0; row < rowCount; row++) { for (int col = 0; col < colCount; col++) { if (tableWidget->item(row, col) == NULL) { QMessageBox::information(NULL, "警告", "表格有空值!!!"); return false; } } } return true; } void ProfessorInputTable::testfunction() { QList datas = getTableWidgetContent(pageTabAndCorrTables); for (auto tmp : datas) { qDebug() << tmp->node << tmp->abscissa << tmp->ordinate << tmp->nodeValue; } datas[0]->nodeValue = 1; datas[1]->nodeValue = 1; datas[2]->nodeValue = 1; datas[3]->nodeValue = 1; QMap> temp = setTableWidgetContent(datas, pageTabAndCorrTables); // QList tmpPageList = temp.keys(); // QList> tmpTabList = temp.values(); // for (int i = 0; i < tmpPageList.length(); i++) { // QList tmpTableList = tmpTabList.at(i).values(); // for (int j = 0; j < tmpTableList.length(); j++) { // tmpTableList.at(j)->clear(); // } // } } QList ProfessorInputTable::getTableWidgetContent(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.at(0); QTableWidget * tmpTable = tmpTabAndTable.values().at(0); QString rowHeaderLabels; QString colHeaderLabels; for (int rowlen = 0; rowlen < tmpTable->model()->rowCount(); rowlen++) { rowHeaderLabels = tmpTable->model()->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tmpTable->model()->columnCount(); collen++) { colHeaderLabels = tmpTable->model()->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) { datas.at(index)->nodeValue = tmpTable->item(rowlen, collen)->text().toDouble(); } } else { continue; } } } } } else { QMap tmpTabAndTable = tabAndTable.at(i); QList tabNames = tmpTabAndTable.keys(); QList tableNames = tmpTabAndTable.values(); QString rowHeaderLabels; QString colHeaderLabels; for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) { QString tabName = tabNames.at(tabIndex); QTableWidget *tableName = tableNames.at(tabIndex); for (int rowlen = 0; rowlen < tableName->model()->rowCount(); rowlen++) { rowHeaderLabels = tableName->model()->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tableName->model()->columnCount(); collen++) { colHeaderLabels = tableName->model()->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) { datas.at(index)->nodeValue = tableName->item(rowlen, collen)->text().toDouble(); } } else { continue; } } } } } } } // for(auto tmp:tmpdatas) // { // qDebug() <node<abscissa << tmp->ordinate << tmp->nodeValue; // } // qDebug() << datas.length(); return datas; } QMap> ProfessorInputTable::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]; QTableWidget * tmpTable = tmpTabAndTable.values()[0]; QString rowHeaderLabels; QString colHeaderLabels; for (int rowlen = 0; rowlen < tmpTable->model()->rowCount(); rowlen++) { rowHeaderLabels = tmpTable->model()->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tmpTable->model()->columnCount(); collen++) { colHeaderLabels = tmpTable->model()->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) { // qDebug() << datas.at(index)->nodeValue; QTableWidgetItem *item = new QTableWidgetItem(datas[index]->nodeValue); tmpTable->setItem(rowlen, collen, item); tmpTable->viewport()->update(); } } else { continue; } } } } } else { QMap tmpTabAndTable = tabAndTable.at(i); QList tabNames = tmpTabAndTable.keys(); QList tableNames = tmpTabAndTable.values(); QString rowHeaderLabels; QString colHeaderLabels; for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) { QString tabName = tabNames.at(tabIndex); QTableWidget *tableName = tableNames.at(tabIndex); for (int rowlen = 0; rowlen < tableName->model()->rowCount(); rowlen++) { rowHeaderLabels = tableName->model()->headerData(rowlen, Qt::Vertical).toString(); for (int collen = 0; collen < tableName->model()->columnCount(); collen++) { colHeaderLabels = tableName->model()->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) { QTableWidgetItem *item = new QTableWidgetItem(datas.at(index)->nodeValue); tableName->setItem(rowlen, collen, item); } } else { continue; } } } } } } } // for(auto tmp:datas) // { // qDebug() <node<abscissa << tmp->ordinate << tmp->nodeValue; // } // qDebug() << datas.length(); return pageTabAndCorrTables; } void ProfessorInputTable::switchToNextPage() { int index = m_stackWidget->currentIndex(); index++; if (index < stackedWidgetPages) { m_stackWidget->setCurrentIndex(index); } else { index = stackedWidgetPages - 1; QMessageBox::information(NULL, "提示:", "当前为最后一页!"); } } void ProfessorInputTable::switchToLastPage() { int index = m_stackWidget->currentIndex(); index--; if (index >= 0) { m_stackWidget->setCurrentIndex(index); } else { index = 0; QMessageBox::information(NULL, "提示:", "当前为第一页!"); } }