ProfessorInputTable.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "ProfessorInputTable.h"
  2. #include "ui_ProfessorInputTable.h"
  3. #include <QTableWidget>
  4. #include <QMessageBox>
  5. #include "dbService/ClassSet.h"
  6. #include <QDebug>
  7. #define SET_NAME(name) m_tableWidget##name //动态命名变量名
  8. ProfessorInputTable::ProfessorInputTable(QWidget *parent) : QDialog(parent), ui(new Ui::ProfessorInputTable)
  9. {
  10. ui->setupUi(this);
  11. datas = ClassSet().datas;
  12. // int pages = 2; // 根据节点列判断页数:节点中多有多少个分隔点,就有多少页
  13. stackedWidgetPages = getStackedWidgetPages();
  14. // qDebug() << "pages = " << stackedWidgetPages;
  15. InitInterface();
  16. addStackWidgetPage(stackedWidgetPages);
  17. Init();
  18. }
  19. int ProfessorInputTable::getStackedWidgetPages()
  20. {
  21. int pages = -1;
  22. // 异常处理 datas为空
  23. for (auto temp : datas) {
  24. QStringList list = (temp->node).split(".");
  25. if (list.length() > pages) { pages = list.length(); }
  26. }
  27. return pages - 1;
  28. }
  29. bool ProfessorInputTable::IsQStringListEqual(QStringList tabList, QStringList nextList)
  30. {
  31. for (int i = 0; i < nextList.length() - 1; i++) {
  32. if (tabList[i] != nextList[i]) { return false; }
  33. }
  34. return true;
  35. }
  36. QMap<QString, QSet<QString>> ProfessorInputTable::getTabWidgetHeaderLabel(int currentStackWidgetPage)
  37. {
  38. QString tabElement;
  39. QMap<QString, QSet<QString>> corrElements;
  40. for (auto temp : datas) {
  41. QStringList oneList = (temp->node).split(".");
  42. if (currentStackWidgetPage == (oneList.length() - 1)) {
  43. QSet<QString> elements;
  44. tabElement = temp->abscissa;
  45. QStringList tabList = (temp->node).split(".");
  46. for (auto nextTmp : datas) {
  47. QStringList twoList = (nextTmp->node).split(".");
  48. if ((currentStackWidgetPage + 2) == twoList.length()) {
  49. QStringList nextList = (nextTmp->node).split(".");
  50. if (IsQStringListEqual(tabList, nextList)) { elements << nextTmp->abscissa; }
  51. }
  52. }
  53. corrElements.insert(tabElement, elements);
  54. }
  55. }
  56. // qDebug() << "corrElements.keys = " << corrElements.keys();
  57. // qDebug() << "corrElements.values = " << corrElements.values();
  58. // return elements.size();
  59. return corrElements;
  60. }
  61. void ProfessorInputTable::Init()
  62. {
  63. connect(nextPageBtn, &QPushButton::clicked, this, &ProfessorInputTable::switchToNextPage);
  64. connect(lastPageBtn, &QPushButton::clicked, this, &ProfessorInputTable::switchToLastPage);
  65. connect(restoreBtn, &QPushButton::clicked, this, &ProfessorInputTable::testfunction);
  66. }
  67. ProfessorInputTable::~ProfessorInputTable()
  68. {
  69. delete ui;
  70. }
  71. void ProfessorInputTable::InitInterface()
  72. {
  73. // new对象
  74. m_widget = new QWidget();
  75. m_stackWidget = new QStackedWidget();
  76. lastPageBtn = new QPushButton("上一页");
  77. nextPageBtn = new QPushButton("下一页");
  78. restoreBtn = new QPushButton("保存");
  79. m_frame = new QFrame(this);
  80. //布局
  81. mainLayout = new QVBoxLayout();
  82. frameLayout = new QVBoxLayout();
  83. btnLayout = new QHBoxLayout();
  84. btnLayout->addWidget(lastPageBtn);
  85. btnLayout->addWidget(nextPageBtn);
  86. btnLayout->addWidget(restoreBtn);
  87. frameLayout->addWidget(m_stackWidget);
  88. frameLayout->addLayout(btnLayout);
  89. m_frame->setLayout(frameLayout);
  90. mainLayout->addWidget(m_frame);
  91. m_widget->setLayout(mainLayout);
  92. this->setLayout(mainLayout);
  93. }
  94. void ProfessorInputTable::addStackWidgetPage(int pages)
  95. {
  96. QStringList tableNames;
  97. QSet<QString> names;
  98. // 异常处理 datas为空
  99. for (auto temp : datas) {
  100. QStringList list = (temp->node).split(".");
  101. if (list.length() == 2) {
  102. names << temp->abscissa;
  103. } else {
  104. continue;
  105. }
  106. }
  107. for (auto temp : names) {
  108. tableNames << temp;
  109. }
  110. for (int i = 0; i < pages; i++) {
  111. if (i == 0) {
  112. QWidget * m_widget = new QWidget();
  113. QTableWidget *m_tableWidget_page1 = createTable(tableNames);
  114. QVBoxLayout * m_layout = new QVBoxLayout();
  115. m_layout->addWidget(m_tableWidget_page1);
  116. m_widget->setLayout(m_layout);
  117. m_stackWidget->addWidget(m_widget);
  118. tabAndCorrTable.insert("", m_tableWidget_page1);
  119. pageTabAndCorrTables.insert(0, tabAndCorrTable);
  120. tabAndCorrTable.clear();
  121. } else {
  122. QWidget * m_widget = new QWidget();
  123. QTabWidget * m_tabWidget = createTabWidget(getTabWidgetHeaderLabel(i), i);
  124. QVBoxLayout *m_layout = new QVBoxLayout();
  125. m_layout->addWidget(m_tabWidget);
  126. m_widget->setLayout(m_layout);
  127. m_stackWidget->addWidget(m_widget);
  128. }
  129. }
  130. }
  131. QTabWidget *ProfessorInputTable::createTabWidget(QMap<QString, QSet<QString>> corrElements, int currentPage)
  132. {
  133. int tabPages = corrElements.keys().length();
  134. // qDebug() << corrElements.keys().at(0);
  135. // qDebug() << corrElements.values().at(0).toList();
  136. QTabWidget *m_tabWidget = new QTabWidget();
  137. for (int i = 0; i < tabPages; i++) {
  138. QWidget * m_widget = new QWidget();
  139. QVBoxLayout * m_layout = new QVBoxLayout();
  140. QTableWidget *SET_NAME(i) = createTable(corrElements.values().at(i).toList());
  141. m_layout->addWidget(SET_NAME(i));
  142. m_widget->setLayout(m_layout);
  143. m_tabWidget->addTab(m_widget, corrElements.keys().at(i));
  144. tabAndCorrTable.insert(corrElements.keys().at(i), SET_NAME(i));
  145. }
  146. pageTabAndCorrTables.insert(currentPage, tabAndCorrTable);
  147. tabAndCorrTable.clear();
  148. return m_tabWidget;
  149. }
  150. QTableWidget *ProfessorInputTable::createTable(QStringList tableNames)
  151. {
  152. QTableWidget *m_tableWidget = new QTableWidget();
  153. m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
  154. m_tableWidget->setRowCount(tableNames.count()); // 设置行数
  155. m_tableWidget->setColumnCount(tableNames.count()); // 设置列数
  156. m_tableWidget->setHorizontalHeaderLabels(tableNames); // 水平表头
  157. m_tableWidget->setVerticalHeaderLabels(tableNames); // 垂直表头
  158. m_tableWidget->setShowGrid(true); // 网格线可见
  159. this->setFocusPolicy(Qt::NoFocus); //解决选中虚框问题
  160. return m_tableWidget;
  161. }
  162. bool ProfessorInputTable::IsTableEmpty(QTableWidget *tableWidget)
  163. {
  164. int rowCount = tableWidget->rowCount();
  165. int colCount = tableWidget->columnCount();
  166. for (int row = 0; row < rowCount; row++) {
  167. for (int col = 0; col < colCount; col++) {
  168. if (tableWidget->item(row, col) == NULL) {
  169. QMessageBox::information(NULL, "警告", "表格有空值!!!");
  170. return false;
  171. }
  172. }
  173. }
  174. return true;
  175. }
  176. void ProfessorInputTable::testfunction()
  177. {
  178. QList<NodeMatrixInfo *> datas = getTableWidgetContent(pageTabAndCorrTables);
  179. for (auto tmp : datas) {
  180. qDebug() << tmp->node << tmp->abscissa << tmp->ordinate << tmp->nodeValue;
  181. }
  182. datas[0]->nodeValue = 1;
  183. datas[1]->nodeValue = 1;
  184. datas[2]->nodeValue = 1;
  185. datas[3]->nodeValue = 1;
  186. QMap<int, QMap<QString, QTableWidget *>> temp = setTableWidgetContent(datas, pageTabAndCorrTables);
  187. // QList<int> tmpPageList = temp.keys();
  188. // QList<QMap<QString, QTableWidget *>> tmpTabList = temp.values();
  189. // for (int i = 0; i < tmpPageList.length(); i++) {
  190. // QList<QTableWidget *> tmpTableList = tmpTabList.at(i).values();
  191. // for (int j = 0; j < tmpTableList.length(); j++) {
  192. // tmpTableList.at(j)->clear();
  193. // }
  194. // }
  195. }
  196. QList<NodeMatrixInfo *>
  197. ProfessorInputTable::getTableWidgetContent(QMap<int, QMap<QString, QTableWidget *>> pageTabAndCorrTables)
  198. {
  199. QList<int> pages = pageTabAndCorrTables.keys();
  200. QList<QMap<QString, QTableWidget *>> tabAndTable = pageTabAndCorrTables.values();
  201. for (int i = 0; i < pages.length(); i++) {
  202. int page = pages.at(i);
  203. if (page == 0) {
  204. QMap<QString, QTableWidget *> tmpTabAndTable = tabAndTable.at(0);
  205. QTableWidget * tmpTable = tmpTabAndTable.values().at(0);
  206. QString rowHeaderLabels;
  207. QString colHeaderLabels;
  208. for (int rowlen = 0; rowlen < tmpTable->model()->rowCount(); rowlen++) {
  209. rowHeaderLabels = tmpTable->model()->headerData(rowlen, Qt::Vertical).toString();
  210. for (int collen = 0; collen < tmpTable->model()->columnCount(); collen++) {
  211. colHeaderLabels = tmpTable->model()->headerData(collen, Qt::Horizontal).toString();
  212. for (int index = 0; index < datas.length(); index++) {
  213. QStringList list = (datas.at(index)->node).split(".");
  214. if (list.length() == 2) {
  215. if (rowHeaderLabels == datas.at(index)->abscissa
  216. && colHeaderLabels == datas.at(index)->ordinate) {
  217. datas.at(index)->nodeValue = tmpTable->item(rowlen, collen)->text().toDouble();
  218. }
  219. } else {
  220. continue;
  221. }
  222. }
  223. }
  224. }
  225. } else {
  226. QMap<QString, QTableWidget *> tmpTabAndTable = tabAndTable.at(i);
  227. QList<QString> tabNames = tmpTabAndTable.keys();
  228. QList<QTableWidget *> tableNames = tmpTabAndTable.values();
  229. QString rowHeaderLabels;
  230. QString colHeaderLabels;
  231. for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) {
  232. QString tabName = tabNames.at(tabIndex);
  233. QTableWidget *tableName = tableNames.at(tabIndex);
  234. for (int rowlen = 0; rowlen < tableName->model()->rowCount(); rowlen++) {
  235. rowHeaderLabels = tableName->model()->headerData(rowlen, Qt::Vertical).toString();
  236. for (int collen = 0; collen < tableName->model()->columnCount(); collen++) {
  237. colHeaderLabels = tableName->model()->headerData(collen, Qt::Horizontal).toString();
  238. for (int index = 0; index < datas.length(); index++) {
  239. QStringList list = (datas.at(index)->node).split(".");
  240. if (list.length() == page + 2) {
  241. QStringList dataNode;
  242. QStringList tableNode;
  243. // 数据node
  244. for (int nodelen = 0; nodelen < page + 1; nodelen++) {
  245. dataNode << list[nodelen];
  246. }
  247. // 表格node
  248. tableNode = dataNode;
  249. tableNode.replace(page, QString::number(tabIndex + 1));
  250. if (IsQStringListEqual(dataNode, tableNode)
  251. && rowHeaderLabels == datas.at(index)->abscissa
  252. && colHeaderLabels == datas.at(index)->ordinate) {
  253. datas.at(index)->nodeValue = tableName->item(rowlen, collen)->text().toDouble();
  254. }
  255. } else {
  256. continue;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. // for(auto tmp:tmpdatas)
  265. // {
  266. // qDebug() <<tmp->node<<tmp->abscissa << tmp->ordinate << tmp->nodeValue;
  267. // }
  268. // qDebug() << datas.length();
  269. return datas;
  270. }
  271. QMap<int, QMap<QString, QTableWidget *>>
  272. ProfessorInputTable::setTableWidgetContent(QList<NodeMatrixInfo *> datas,
  273. QMap<int, QMap<QString, QTableWidget *>> pageTabAndCorrTables)
  274. {
  275. QList<int> pages = pageTabAndCorrTables.keys();
  276. QList<QMap<QString, QTableWidget *>> tabAndTable = pageTabAndCorrTables.values();
  277. for (int i = 0; i < pages.length(); i++) {
  278. int page = pages.at(i);
  279. if (page == 0) {
  280. QMap<QString, QTableWidget *> tmpTabAndTable = tabAndTable[0];
  281. QTableWidget * tmpTable = tmpTabAndTable.values()[0];
  282. QString rowHeaderLabels;
  283. QString colHeaderLabels;
  284. for (int rowlen = 0; rowlen < tmpTable->model()->rowCount(); rowlen++) {
  285. rowHeaderLabels = tmpTable->model()->headerData(rowlen, Qt::Vertical).toString();
  286. for (int collen = 0; collen < tmpTable->model()->columnCount(); collen++) {
  287. colHeaderLabels = tmpTable->model()->headerData(collen, Qt::Horizontal).toString();
  288. for (int index = 0; index < datas.length(); index++) {
  289. QStringList list = (datas.at(index)->node).split(".");
  290. if (list.length() == 2) {
  291. if (rowHeaderLabels == datas.at(index)->abscissa
  292. && colHeaderLabels == datas.at(index)->ordinate) {
  293. // qDebug() << datas.at(index)->nodeValue;
  294. QTableWidgetItem *item = new QTableWidgetItem(datas[index]->nodeValue);
  295. tmpTable->setItem(rowlen, collen, item);
  296. tmpTable->viewport()->update();
  297. }
  298. } else {
  299. continue;
  300. }
  301. }
  302. }
  303. }
  304. } else {
  305. QMap<QString, QTableWidget *> tmpTabAndTable = tabAndTable.at(i);
  306. QList<QString> tabNames = tmpTabAndTable.keys();
  307. QList<QTableWidget *> tableNames = tmpTabAndTable.values();
  308. QString rowHeaderLabels;
  309. QString colHeaderLabels;
  310. for (int tabIndex = 0; tabIndex < tabNames.length(); tabIndex++) {
  311. QString tabName = tabNames.at(tabIndex);
  312. QTableWidget *tableName = tableNames.at(tabIndex);
  313. for (int rowlen = 0; rowlen < tableName->model()->rowCount(); rowlen++) {
  314. rowHeaderLabels = tableName->model()->headerData(rowlen, Qt::Vertical).toString();
  315. for (int collen = 0; collen < tableName->model()->columnCount(); collen++) {
  316. colHeaderLabels = tableName->model()->headerData(collen, Qt::Horizontal).toString();
  317. for (int index = 0; index < datas.length(); index++) {
  318. QStringList list = (datas.at(index)->node).split(".");
  319. if (list.length() == page + 2) {
  320. QStringList dataNode;
  321. QStringList tableNode;
  322. // 数据node
  323. for (int nodelen = 0; nodelen < page + 1; nodelen++) {
  324. dataNode << list[nodelen];
  325. }
  326. // 表格node
  327. tableNode = dataNode;
  328. tableNode.replace(page, QString::number(tabIndex + 1));
  329. if (IsQStringListEqual(dataNode, tableNode)
  330. && rowHeaderLabels == datas.at(index)->abscissa
  331. && colHeaderLabels == datas.at(index)->ordinate) {
  332. QTableWidgetItem *item = new QTableWidgetItem(datas.at(index)->nodeValue);
  333. tableName->setItem(rowlen, collen, item);
  334. }
  335. } else {
  336. continue;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. // for(auto tmp:datas)
  345. // {
  346. // qDebug() <<tmp->node<<tmp->abscissa << tmp->ordinate << tmp->nodeValue;
  347. // }
  348. // qDebug() << datas.length();
  349. return pageTabAndCorrTables;
  350. }
  351. void ProfessorInputTable::switchToNextPage()
  352. {
  353. int index = m_stackWidget->currentIndex();
  354. index++;
  355. if (index < stackedWidgetPages) {
  356. m_stackWidget->setCurrentIndex(index);
  357. } else {
  358. index = stackedWidgetPages - 1;
  359. QMessageBox::information(NULL, "提示:", "当前为最后一页!");
  360. }
  361. }
  362. void ProfessorInputTable::switchToLastPage()
  363. {
  364. int index = m_stackWidget->currentIndex();
  365. index--;
  366. if (index >= 0) {
  367. m_stackWidget->setCurrentIndex(index);
  368. } else {
  369. index = 0;
  370. QMessageBox::information(NULL, "提示:", "当前为第一页!");
  371. }
  372. }