ModelViewTable.cpp 16 KB

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