123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- #include "ProfessorInputTable.h"
- #include "ui_ProfessorInputTable.h"
- #include <QTableWidget>
- #include <QMessageBox>
- #include "dbService/ClassSet.h"
- #include <QDebug>
- #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<QString, QSet<QString>> ProfessorInputTable::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;
- }
- 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<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();
- 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<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();
- 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<NodeMatrixInfo *> 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<int, QMap<QString, QTableWidget *>> temp = setTableWidgetContent(datas, pageTabAndCorrTables);
- // QList<int> tmpPageList = temp.keys();
- // QList<QMap<QString, QTableWidget *>> tmpTabList = temp.values();
- // for (int i = 0; i < tmpPageList.length(); i++) {
- // QList<QTableWidget *> tmpTableList = tmpTabList.at(i).values();
- // for (int j = 0; j < tmpTableList.length(); j++) {
- // tmpTableList.at(j)->clear();
- // }
- // }
- }
- QList<NodeMatrixInfo *>
- ProfessorInputTable::getTableWidgetContent(QMap<int, QMap<QString, QTableWidget *>> pageTabAndCorrTables)
- {
- QList<int> pages = pageTabAndCorrTables.keys();
- QList<QMap<QString, QTableWidget *>> tabAndTable = pageTabAndCorrTables.values();
- for (int i = 0; i < pages.length(); i++) {
- int page = pages.at(i);
- if (page == 0) {
- QMap<QString, QTableWidget *> 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<QString, QTableWidget *> tmpTabAndTable = tabAndTable.at(i);
- QList<QString> tabNames = tmpTabAndTable.keys();
- QList<QTableWidget *> 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() <<tmp->node<<tmp->abscissa << tmp->ordinate << tmp->nodeValue;
- // }
- // qDebug() << datas.length();
- return datas;
- }
- QMap<int, QMap<QString, QTableWidget *>>
- ProfessorInputTable::setTableWidgetContent(QList<NodeMatrixInfo *> datas,
- QMap<int, QMap<QString, QTableWidget *>> pageTabAndCorrTables)
- {
- QList<int> pages = pageTabAndCorrTables.keys();
- QList<QMap<QString, QTableWidget *>> tabAndTable = pageTabAndCorrTables.values();
- for (int i = 0; i < pages.length(); i++) {
- int page = pages.at(i);
- if (page == 0) {
- QMap<QString, QTableWidget *> 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<QString, QTableWidget *> tmpTabAndTable = tabAndTable.at(i);
- QList<QString> tabNames = tmpTabAndTable.keys();
- QList<QTableWidget *> 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() <<tmp->node<<tmp->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, "提示:", "当前为第一页!");
- }
- }
|