EXDataTableView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #include "EXDataTableView.h"
  2. #include "EXDataViewDelegate.h"
  3. #include "ProjectManager.h"
  4. #include <dbService/CNodeDataService.h>
  5. #include <dbService/ClassSet.h>
  6. #include <dbService/UserService.h>
  7. #include <dbService/NodeMatrixService.h>
  8. #include <Widgets/Button.h>
  9. #include <QBoxLayout>
  10. #include <QTabWidget>
  11. #include <QLabel>
  12. #include <QTableView>
  13. #include <QTableWidget>
  14. #include <QAbstractItemModel>
  15. #include <QStandardItemModel>
  16. #include <QStandardItem>
  17. #include <QHeaderView>
  18. #include <QApplication>
  19. #include <QDesktopWidget>
  20. #include <QDateTime>
  21. #include <QFileDialog>
  22. #include <QDebug>
  23. static const char *kTabName = "tabName";
  24. EXDataItemModel::EXDataItemModel(QObject *parent) : QStandardItemModel(parent) { }
  25. QVariant EXDataItemModel::data(const QModelIndex &index, int role) const
  26. {
  27. QVariant value = QStandardItemModel::data(index, role);
  28. if (Qt::TextAlignmentRole == role) {
  29. value = Qt::AlignCenter;
  30. }
  31. return value;
  32. }
  33. EXDataTableView::EXDataTableView(SchemePlanManager::SchemeProcessInfo process, QWidget *parent)
  34. : QWidget(parent), m_process(process)
  35. {
  36. m_mind1 = new CMind(this);
  37. m_mind2 = new CMind(this);
  38. initWidget();
  39. initLayout();
  40. connectSignalsAndSlots();
  41. m_comboDelegate = new EXDataTableComboDelegate(this);
  42. m_user = QFUser::currentUser();
  43. m_export = new QXlsx::Document(this);
  44. }
  45. SchemePlanManager::SchemeProcessInfo EXDataTableView::process() const
  46. {
  47. return m_process;
  48. }
  49. void EXDataTableView::setProjectInfo(ProjectInfo *proj)
  50. {
  51. m_proj = proj;
  52. }
  53. void EXDataTableView::initWidget()
  54. {
  55. m_dataTab = new QTabWidget(this);
  56. m_dataTab->setTabPosition(QTabWidget::South);
  57. m_pageLab = new QLabel(this);
  58. m_previous = new PushButton("上一级指标", this);
  59. m_next = new PushButton("下一级指标", this);
  60. m_save = new PushButton("保存", this);
  61. m_save->setHidden(true);
  62. }
  63. void EXDataTableView::initLayout()
  64. {
  65. m_layout = new QVBoxLayout(this);
  66. m_layout->addWidget(m_dataTab);
  67. m_pageLayout = new QHBoxLayout();
  68. m_layout->addLayout(m_pageLayout);
  69. m_pageLayout->setSpacing(10);
  70. m_pageLayout->addStretch();
  71. m_pageLayout->addWidget(m_previous);
  72. m_pageLayout->addWidget(m_pageLab);
  73. m_pageLayout->addWidget(m_next);
  74. m_pageLayout->addStretch();
  75. m_pageLayout->addWidget(m_save);
  76. }
  77. void EXDataTableView::connectSignalsAndSlots()
  78. {
  79. connect(m_previous, &PushButton::clicked, this, &EXDataTableView::slotPrevious);
  80. connect(m_next, &PushButton::clicked, this, &EXDataTableView::slotNext);
  81. connect(m_dataTab, &QTabWidget::currentChanged, this, &EXDataTableView::slotTabCurrentChanged);
  82. connect(m_save, &PushButton::clicked, this, &EXDataTableView::slotSave);
  83. }
  84. void EXDataTableView::setupModels()
  85. {
  86. // 按照脑图层级创建各个表的 models
  87. m_models.clear();
  88. QList<CNodeData> hHeaders;
  89. QList<CNodeData> vHeaders;
  90. for (int i = 1; i < m_mind1->levels(); i++) {
  91. QList<EXDataItemModel *> modelList;
  92. // 方案优选和综合效能的评估数据采集页面只有一级, 不用切换页面
  93. if (m_process.type == SchemePlanManager::ImportEvalData
  94. && (m_process.indexType == ProjectManager::OptimalIndex
  95. || m_process.indexType == ProjectManager::EfficiencyIndex)) {
  96. vHeaders = m_mind1->leaves();
  97. EXDataItemModel *model = new EXDataItemModel(this);
  98. updateModel(model, hHeaders, vHeaders);
  99. modelList.append(model);
  100. m_models[i] = modelList;
  101. break;
  102. }
  103. QList<CNodeData> nodes = m_mind1->nodesInLevel(i);
  104. for (int j = 0; j < nodes.count(); j++) {
  105. CNodeData node = nodes[j];
  106. EXDataItemModel *model = new EXDataItemModel(this);
  107. connect(model, &QStandardItemModel::itemChanged, this, &EXDataTableView::itemChanged);
  108. if (m_process.type == SchemePlanManager::ImportWeightData) {
  109. hHeaders = m_mind1->subNodes(node);
  110. vHeaders = hHeaders;
  111. for (int i = 0; i < vHeaders.count(); i++) {
  112. CNodeData vNode = vHeaders[i];
  113. for (int j = 0; j < hHeaders.count(); j++) {
  114. CNodeData hNode = hHeaders[j];
  115. QStandardItem *item = new QStandardItem();
  116. item->setData(Qt::AlignCenter, Qt::TextAlignmentRole);
  117. item->setEditable(false);
  118. if (i == j) {
  119. item->setText("1");
  120. }
  121. if (i >= j) {
  122. item->setBackground(QBrush(QColor("lightgray")));
  123. }
  124. model->setItem(i, j, item);
  125. }
  126. }
  127. }
  128. if (m_process.type == SchemePlanManager::ImportEvalData) {
  129. if (m_process.indexType == ProjectManager::TechIndex) {
  130. hHeaders = m_mind1->subNodes(node);
  131. vHeaders = m_mind2->leaves();
  132. for (int i = 0; i < vHeaders.count(); i++) {
  133. CNodeData vNode = vHeaders[i];
  134. for (int j = 0; j < hHeaders.count(); j++) {
  135. CNodeData hNode = hHeaders[j];
  136. QStandardItem *item = new QStandardItem();
  137. model->setItem(i, j, item);
  138. }
  139. }
  140. }
  141. }
  142. model->setProperty(kTabName, node.name);
  143. updateModel(model, hHeaders, vHeaders);
  144. modelList.append(model);
  145. }
  146. m_models[i] = modelList;
  147. }
  148. setCurrentPage(1);
  149. }
  150. void EXDataTableView::setupTabWidget()
  151. {
  152. m_dataTab->clear();
  153. QList<EXDataItemModel *> list = m_models[m_currentPage];
  154. for (int j = 0; j < list.size(); j++) {
  155. EXDataItemModel *model = list[j];
  156. QTableView *t = new QTableView(m_dataTab);
  157. t->setAlternatingRowColors(m_mind2->nodeList().count() > 0);
  158. t->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  159. t->horizontalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  160. t->verticalHeader()->setStyleSheet("QHeaderView::section{background:rgb(244,244,244);color: black;}");
  161. t->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
  162. t->setSelectionMode(QAbstractItemView::SingleSelection);
  163. t->setModel(model);
  164. m_dataTab->addTab(t, model->property(kTabName).toString());
  165. connect(t, &QTableView::clicked, this, &EXDataTableView::slotItemClicked);
  166. if (m_process.type == SchemePlanManager::ImportEvalData) {
  167. if (m_process.indexType == ProjectManager::TechIndex) {
  168. // t->setItemDelegate(m_comboDelegate);
  169. }
  170. }
  171. }
  172. }
  173. int EXDataTableView::currentPage() const
  174. {
  175. return m_currentPage;
  176. }
  177. void EXDataTableView::setCurrentPage(int p)
  178. {
  179. if (p < 1 || p > m_models.size()) {
  180. return;
  181. }
  182. m_currentPage = p;
  183. m_previous->setEnabled(p > 1);
  184. m_next->setEnabled(p < m_models.size());
  185. setupTabWidget();
  186. m_pageLab->setText(QString("共 %1 页, 当前第 %2 页").arg(m_models.size()).arg(p));
  187. }
  188. CMind *EXDataTableView::mind1() const
  189. {
  190. return m_mind1;
  191. }
  192. CMind *EXDataTableView::mind2() const
  193. {
  194. return m_mind2;
  195. }
  196. bool EXDataTableView::checkDataComplete() const
  197. {
  198. for (QList<EXDataItemModel *> modelList : m_models) {
  199. for (EXDataItemModel *model : modelList) {
  200. for (int r = 0; r < model->rowCount(); ++r) {
  201. for (int c = 0; c < model->columnCount(); ++c) {
  202. QStandardItem *item = model->item(r, c);
  203. if (item == nullptr || item->text().length() <= 0) {
  204. return false;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. return true;
  211. }
  212. void EXDataTableView::exportData(const QString &path)
  213. {
  214. QString filePath = path;
  215. QString fileName = m_user->userName + "-"
  216. + ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType) + "-"
  217. + m_proj->projectName;
  218. QDir dirReportPath(filePath);
  219. if (!dirReportPath.exists()) {
  220. if (dirReportPath.mkpath(filePath)) {
  221. filePath = filePath + "/" + fileName + tr(".xlsx");
  222. }
  223. } else {
  224. filePath = filePath + "/" + fileName + tr(".xlsx");
  225. }
  226. QFile file(fileName);
  227. if (file.exists()) {
  228. qDebug() << "--remove";
  229. file.remove();
  230. }
  231. for (int i = 1; i <= m_models.size(); i++) {
  232. QList<EXDataItemModel *> mList = m_models[i];
  233. for (int j = 0; j < mList.count(); j++) {
  234. EXDataItemModel *model = mList[j];
  235. m_export->addSheet(model->property(kTabName).toString());
  236. int row = model->rowCount();
  237. int col = model->columnCount();
  238. m_export->setColumnWidth(1, 20);
  239. for (int c = 2; c < col + 2; c++) {
  240. QStandardItem *hHead = model->horizontalHeaderItem(c - 2);
  241. m_export->write(1, c, hHead->text());
  242. m_export->setColumnWidth(c, hHead->text().size() == 0 ? 40 : hHead->text().size() * 4);
  243. }
  244. for (int r = 2; r < row + 2; r++) {
  245. QStandardItem *vHead = model->verticalHeaderItem(r - 2);
  246. m_export->write(r, 1, vHead->text());
  247. for (int c = 2; c < col + 2; c++) {
  248. QStandardItem *item = model->item(r - 2, c - 2);
  249. m_export->write(r, c, item->text());
  250. }
  251. }
  252. }
  253. }
  254. m_export->addSheet("专家信息");
  255. m_export->write(1, 1, "ID");
  256. m_export->write(1, 2, "账号");
  257. m_export->write(1, 3, "单位");
  258. m_export->write(1, 4, "职务");
  259. m_export->write(1, 5, "专业");
  260. m_export->write(1, 6, "联系方式");
  261. m_export->write(1, 7, "填写时间");
  262. m_export->write(1, 8, "备注");
  263. m_export->write(2, 1, m_user->id);
  264. m_export->write(2, 2, m_user->userName);
  265. m_export->write(2, 3, m_user->workPosition);
  266. m_export->write(2, 4, m_user->post);
  267. m_export->write(2, 5, m_user->major);
  268. m_export->write(2, 6, m_user->phone);
  269. m_export->write(2, 7, m_user->writeTime);
  270. m_export->write(2, 8, m_user->remark);
  271. qDebug() << filePath;
  272. m_export->saveAs(filePath);
  273. }
  274. void EXDataTableView::editItemData(const QModelIndex &index, const QString &val)
  275. {
  276. int c = m_dataTab->currentIndex();
  277. QTableView *table = (QTableView *)m_dataTab->widget(c);
  278. EXDataItemModel *model = (EXDataItemModel *)table->model();
  279. model->itemFromIndex(index)->setText(val);
  280. QString symmetry;
  281. if (val.startsWith("1/")) {
  282. symmetry = val.split("/")[1];
  283. } else {
  284. if (val == "0" || val == "1") {
  285. symmetry = val;
  286. } else {
  287. symmetry = "1/" + val;
  288. }
  289. }
  290. model->item(index.column(), index.row())->setText(symmetry);
  291. }
  292. void EXDataTableView::updateModel(EXDataItemModel *model, QList<CNodeData> &hHeaders, QList<CNodeData> &vHeaders)
  293. {
  294. for (int i = 0; i < hHeaders.size(); i++) {
  295. CNodeData node = hHeaders[i];
  296. QStandardItem *item = new QStandardItem(node.name);
  297. item->setToolTip(node.remark);
  298. model->setHorizontalHeaderItem(i, item);
  299. }
  300. for (int i = 0; i < vHeaders.size(); i++) {
  301. CNodeData node = vHeaders[i];
  302. QStandardItem *item = new QStandardItem(node.name);
  303. item->setToolTip(node.remark);
  304. model->setVerticalHeaderItem(i, item);
  305. }
  306. }
  307. void EXDataTableView::slotPrevious()
  308. {
  309. setCurrentPage(m_currentPage - 1);
  310. }
  311. void EXDataTableView::slotNext()
  312. {
  313. setCurrentPage(m_currentPage + 1);
  314. }
  315. void EXDataTableView::slotTabCurrentChanged(int c)
  316. {
  317. Q_UNUSED(c)
  318. // updateCurrentTable();
  319. }
  320. void EXDataTableView::slotItemClicked(const QModelIndex &index)
  321. {
  322. if (index.row() >= index.column()) {
  323. return;
  324. }
  325. if (m_process.type != SchemePlanManager::ImportWeightData) {
  326. return;
  327. }
  328. QStringList l = { "1/9", "1/7", "1/5", "1/3", "1", "3", "5", "7", "9" };
  329. QTableView *table = (QTableView *)sender();
  330. EXDataItemModel *model = (EXDataItemModel *)table->model();
  331. // SchemeBar *scheme =
  332. // new SchemeBar(model->item(index.row(), 0)->text(),
  333. // model->horizontalHeaderItem(index.column())->text(), l);
  334. SchemeBar *scheme = new SchemeBar(model->verticalHeaderItem(index.row())->text(),
  335. model->horizontalHeaderItem(index.column())->text(), l);
  336. scheme->setModal(true);
  337. scheme->setAttribute(Qt::WA_DeleteOnClose);
  338. connect(scheme, &SchemeBar::setValue, [=](QString val) { editItemData(index, val); });
  339. scheme->show();
  340. QPoint p = QCursor::pos();
  341. if (p.x() + scheme->width() + 10 >= QApplication::desktop()->width()) {
  342. p.setX(QApplication::desktop()->width() - 10 - scheme->width());
  343. }
  344. scheme->move(p);
  345. }
  346. void EXDataTableView::slotSave()
  347. {
  348. return;
  349. int c = m_dataTab->currentIndex();
  350. QTableView *table = (QTableView *)m_dataTab->widget(c);
  351. EXDataItemModel *model = (EXDataItemModel *)table->model();
  352. QList<NodeMatrixInfo *> values;
  353. if (m_process.type == SchemePlanManager::ImportWeightData) {
  354. for (int i = 0; i < model->rowCount(); i++) {
  355. QStandardItem *row = model->verticalHeaderItem(i);
  356. for (int j = 0; j < model->columnCount(); j++) {
  357. QStandardItem *col = model->horizontalHeaderItem(j);
  358. QStandardItem *item = model->item(i, j);
  359. NodeMatrixInfo *info = new NodeMatrixInfo();
  360. info->expertName = m_user->userName;
  361. info->expertId = QString("%1").arg(m_user->id);
  362. info->engineerId = m_process.projectId;
  363. info->mindId = m_process.dSource;
  364. info->abscissa = col->text();
  365. info->ordinate = row->text();
  366. info->writeDate = QDateTime::currentDateTime();
  367. info->mark = QString("%1").arg(m_currentPage);
  368. info->nodeValue = item->text();
  369. info->tableMsg = QString("%1").arg(m_process.indexType);
  370. values.append(info);
  371. }
  372. }
  373. }
  374. NodeMatrixService().AddNodeMatrixInfoList2(values);
  375. }
  376. void EXDataTableView::itemChanged(QStandardItem *item)
  377. {
  378. emit signalItemEdited();
  379. if (m_process.type == SchemePlanManager::ImportWeightData) {
  380. return;
  381. }
  382. QString Pattern("(\\d*\\.?\\d*)"); // 匹配非负数
  383. QRegExp reg(Pattern);
  384. if (!reg.exactMatch(item->text())) {
  385. item->setText("");
  386. }
  387. }