ConfigExpertDataWidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "ConfigExpertDataWidget.h"
  2. #include "QFDAlert.h"
  3. #include "ProjectManager.h"
  4. #include <dbService/ClassSet.h>
  5. #include <dbService/UserService.h>
  6. #include <dbService/UserConfigService.h>
  7. #include <dbService/NodeMatrixService.h>
  8. #include "common/QFDIcon.h"
  9. #include <Common/Icon.h>
  10. #include <Widgets/Button.h>
  11. #include <Widgets/SpinBox.h>
  12. #include <QBoxLayout>
  13. #include <QSplitter>
  14. #include <QLabel>
  15. #include <QListWidget>
  16. #include <QGridLayout>
  17. #include <QDebug>
  18. ConfigExpertDataWidget::ConfigExpertDataWidget(QWidget *parent) : QWidget(parent)
  19. {
  20. initialize();
  21. initLayout();
  22. connectSignalsAndSlots();
  23. }
  24. void ConfigExpertDataWidget::initialize()
  25. {
  26. QPalette pal(palette());
  27. pal.setColor(QPalette::Background, QColor("#ffffff"));
  28. setAutoFillBackground(true);
  29. setPalette(pal);
  30. m_vBoxLayout = new QVBoxLayout(this);
  31. m_titleLabel = new QLabel("导入专家数据", this);
  32. m_titleLabel->setObjectName("titleLabel");
  33. m_titleLabel->setFixedHeight(50);
  34. m_titleLabel->setContentsMargins(10, 0, 0, 0);
  35. m_splitter = new QSplitter(Qt::Vertical, this);
  36. m_splitter->setChildrenCollapsible(false);
  37. m_configWidget = new QWidget(this);
  38. m_configWidget->setObjectName("addedWidget");
  39. m_configWidgetLayout = new QVBoxLayout(m_configWidget);
  40. m_configWidgetLayout->setSpacing(0);
  41. m_configWidgetLayout->setMargin(0);
  42. m_configListHeader = new QWidget(this);
  43. m_configListHeader->setObjectName("addedListHeader");
  44. m_configListHeader->setFixedHeight(30);
  45. m_configListHeaderLayout = new QHBoxLayout(m_configListHeader);
  46. m_configListTitleLabel = new QLabel("已添加", this);
  47. m_configListTitleLabel->setObjectName("addedListTitleLabel");
  48. m_configListTitleLabel->setMargin(5);
  49. m_configListTipsLabel = new QLabel("注:已添加专家的权重之和须等于1", this);
  50. m_configListTipsLabel->setObjectName("addedListTipsLabel");
  51. m_configListTipsLabel->setFixedHeight(20);
  52. m_saveButton = new QPushButton(NEWFLICON(FluentIcon, SAVE)->icon(), "", this);
  53. m_saveButton->setObjectName("saveButton");
  54. m_saveButton->setFixedSize(QSize(28, 28));
  55. m_saveButton->setToolTip("保存");
  56. m_configListWidget = new QListWidget(this);
  57. m_configListWidget->setObjectName("addedListWidget");
  58. m_expWidget = new QWidget(this);
  59. m_expWidget->setObjectName("expWidget");
  60. m_expWidgetLayout = new QVBoxLayout(m_expWidget);
  61. m_expWidgetLayout->setSpacing(0);
  62. m_expWidgetLayout->setMargin(0);
  63. m_expListHeader = new QWidget(this);
  64. m_expListHeader->setObjectName("expListHeader");
  65. m_expListHeader->setFixedHeight(30);
  66. m_expListHeaderLayout = new QHBoxLayout(m_expListHeader);
  67. m_expListTitleLabel = new QLabel("专家列表", this);
  68. m_expListTitleLabel->setObjectName("expListTitleLabel");
  69. m_expListTitleLabel->setMargin(5);
  70. m_refreshButton = new QPushButton(NEWFLICON(FluentIcon, SYNC)->icon(), "", this);
  71. m_refreshButton->setObjectName("refreshButton");
  72. m_refreshButton->setFixedSize(QSize(28, 28));
  73. m_refreshButton->setToolTip("刷新");
  74. m_expListWidget = new QListWidget(this);
  75. m_expListWidget->setObjectName("expListWidget");
  76. }
  77. void ConfigExpertDataWidget::initLayout()
  78. {
  79. m_vBoxLayout->setMargin(0);
  80. m_vBoxLayout->setSpacing(0);
  81. m_vBoxLayout->addWidget(m_titleLabel);
  82. m_vBoxLayout->addWidget(m_splitter);
  83. m_splitter->addWidget(m_configWidget);
  84. m_splitter->addWidget(m_expWidget);
  85. m_configWidgetLayout->addWidget(m_configListHeader);
  86. m_configWidgetLayout->addWidget(m_configListWidget);
  87. m_configWidgetLayout->addWidget(m_configListTipsLabel, Qt::AlignBottom);
  88. m_expWidgetLayout->addWidget(m_expListHeader);
  89. m_expWidgetLayout->addWidget(m_expListWidget);
  90. m_configListHeaderLayout->setMargin(0);
  91. m_configListHeaderLayout->addWidget(m_configListTitleLabel, Qt::AlignLeft);
  92. m_configListHeaderLayout->addWidget(m_saveButton, Qt::AlignRight);
  93. m_expListHeaderLayout->setMargin(0);
  94. m_expListHeaderLayout->addWidget(m_expListTitleLabel, Qt::AlignLeft);
  95. m_expListHeaderLayout->addWidget(m_refreshButton, Qt::AlignRight);
  96. m_splitter->setStretchFactor(0, 2);
  97. m_splitter->setStretchFactor(1, 3);
  98. setStyleSheet("#titleLabel {color:#333333; font-size:16px}"
  99. "#addedListTipsLabel {color:#999999;}"
  100. "#addedListHeader {background-color:#f8f8f8;border: 1px solid rgba(0, 0, 0, 0.073);}"
  101. "#expListHeader {background-color:#f8f8f8;border: 1px solid rgba(0, 0, 0, 0.073);}"
  102. "QPushButton {border: 0;background-color: qlineargradient(x1: 0, y1: 0, x2: "
  103. "0, y2: 1,stop: 0 #f8f8f8, stop: 1 #f8f8f8);}"
  104. "QPushButton::hover {border: 1px solid rgba(0, 0, 0, 0.073);}"
  105. "QPushButton::pressed {background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, "
  106. "stop: 1 #f6f7fa);}"
  107. "QListWidget {border: 1px solid rgba(0, 0, 0, 0.073);}"
  108. // "QListView::item {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0
  109. // #ffffff, stop: 0.9 "
  110. // "#ffffff,stop: 1 #eeeeee);height:44;}"
  111. "QListView::item {height:44;}"
  112. "QDoubleSpinBox {border: 1px solid rgba(0, 0, 0, 0.173);}"
  113. "#addedListTitleLabel {font:bold; font-size:12px;}"
  114. "#expListTitleLabel {font:bold; font-size:12px;}");
  115. }
  116. void ConfigExpertDataWidget::connectSignalsAndSlots()
  117. {
  118. connect(m_saveButton, &QPushButton::clicked, this, &ConfigExpertDataWidget::slotSave);
  119. connect(m_refreshButton, &QPushButton::clicked, this, &ConfigExpertDataWidget::slotRefresh);
  120. connect(m_configListWidget, &QListWidget::itemSelectionChanged, this,
  121. &ConfigExpertDataWidget::slotUserConfigCurrentChanged);
  122. }
  123. void ConfigExpertDataWidget::setProcess(SchemePlanManager::SchemeProcessInfo process)
  124. {
  125. m_process = process;
  126. m_isProcessChanged = true;
  127. }
  128. void ConfigExpertDataWidget::loadData()
  129. {
  130. loadExpert();
  131. loadConfig();
  132. loadNodeData();
  133. }
  134. void ConfigExpertDataWidget::loadExpert()
  135. {
  136. // 获取专家列表数据
  137. qDeleteAll(m_expertList);
  138. m_expertList.clear();
  139. if (!UserService().QueryUserListNotAdmin(&m_expertList)) {
  140. QFDAlert::showAlertWithCode(QF_CODE_DATA_ERROR, this);
  141. }
  142. refreshExpList();
  143. }
  144. void ConfigExpertDataWidget::loadConfig()
  145. {
  146. // 获取项目专家配置
  147. qDeleteAll(m_configList);
  148. m_configList.clear();
  149. if (!UserConfigService().QueryUserConfigListInfoByEngineerId(&m_configList, m_process.projectId)) {
  150. QFDAlert::showAlertWithCode(QF_CODE_DATA_ERROR, this);
  151. }
  152. refreshExpList();
  153. refreshConfigList();
  154. }
  155. void ConfigExpertDataWidget::loadNodeData()
  156. {
  157. // 获取专家录入数据
  158. QString indexName = ProjectManager::nameOfIndexType((ProjectManager::IndexType)m_process.indexType);
  159. QList<NodeMatrixInfo *> dataList;
  160. NodeMatrixService().QueryExpertDataByProjectAndIndex(&dataList, indexName, m_process.projectId);
  161. m_nodeData.clear();
  162. for (NodeMatrixInfo *info : dataList) {
  163. if (m_nodeData.keys().contains(info->expertId) == false) {
  164. m_nodeData[info->expertId] = QList<NodeMatrixInfo *>();
  165. }
  166. m_nodeData[info->expertId].append(info);
  167. }
  168. refreshConfigList();
  169. }
  170. void ConfigExpertDataWidget::selectFirstImported()
  171. {
  172. if (m_configList.size() <= 0) {
  173. return;
  174. }
  175. int index = 0;
  176. for (int i = 0; i < m_configList.count(); i++) {
  177. UserConfig *config = m_configList[i];
  178. bool imported = m_nodeData.keys().contains(QString("%1").arg(config->userId));
  179. if (imported) {
  180. index = i;
  181. break;
  182. }
  183. }
  184. m_configListWidget->setCurrentRow(index);
  185. }
  186. void ConfigExpertDataWidget::selectConfig(UserConfig *config)
  187. {
  188. int index = m_configList.indexOf(config);
  189. if (index >= 0 && index < m_configList.count()) {
  190. m_configListWidget->setCurrentRow(index);
  191. }
  192. }
  193. QList<NodeMatrixInfo *> ConfigExpertDataWidget::selectedData()
  194. {
  195. QList<NodeMatrixInfo *> list;
  196. int row = m_configListWidget->currentRow();
  197. if (row >= 0 && row < m_configList.count()) {
  198. QString key = QString("%1").arg(m_configList[row]->userId);
  199. list = m_nodeData[key];
  200. }
  201. return list;
  202. }
  203. void ConfigExpertDataWidget::showEvent(QShowEvent *event)
  204. {
  205. Q_UNUSED(event)
  206. if (m_isProcessChanged) {
  207. loadData();
  208. selectFirstImported();
  209. }
  210. }
  211. void ConfigExpertDataWidget::hideEvent(QHideEvent *event)
  212. {
  213. Q_UNUSED(event)
  214. }
  215. void ConfigExpertDataWidget::refreshConfigList()
  216. {
  217. m_configListWidget->clear();
  218. for (int i = 0; i < m_configList.count(); i++) {
  219. QListWidgetItem *item = new QListWidgetItem;
  220. m_configListWidget->addItem(item);
  221. UserConfig *config = m_configList[i];
  222. ConfigListItemWidget *widget = new ConfigListItemWidget();
  223. connect(widget, &ConfigListItemWidget::sigImport, this, &ConfigExpertDataWidget::slotImportData);
  224. connect(widget, &ConfigListItemWidget::sigRemove, this, &ConfigExpertDataWidget::slotRemoveConfig);
  225. widget->setUserConfig(config);
  226. widget->setImported(m_nodeData.keys().contains(QString("%1").arg(config->userId)));
  227. m_configListWidget->setItemWidget(item, widget);
  228. }
  229. }
  230. void ConfigExpertDataWidget::refreshExpList()
  231. {
  232. m_expListWidget->clear();
  233. for (int i = 0; i < m_expertList.count(); i++) {
  234. QListWidgetItem *item = new QListWidgetItem;
  235. m_expListWidget->addItem(item);
  236. ExpertListItemWidget *widget = new ExpertListItemWidget();
  237. connect(widget, &ExpertListItemWidget::sigAddUser, this, &ConfigExpertDataWidget::slotAddUser);
  238. QFUser *expert = m_expertList[i];
  239. widget->setUser(expert);
  240. widget->setIndex(i + 1);
  241. m_expListWidget->setItemWidget(item, widget);
  242. for (UserConfig *config : m_configList) {
  243. if (config->userId == expert->id) {
  244. widget->setAdded(true);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. void ConfigExpertDataWidget::slotSave()
  251. {
  252. int sum = 0;
  253. for (int i = 0; i < m_configListWidget->count(); i++) {
  254. QListWidgetItem *item = m_configListWidget->item(i);
  255. ConfigListItemWidget *widget = (ConfigListItemWidget *)m_configListWidget->itemWidget(item);
  256. sum += widget->spinValue();
  257. widget->userConfig()->weight = widget->spinValue();
  258. }
  259. if (sum != 100) {
  260. QFDAlert::showAlertWithCode(QF_CODE_WEIGHTSUM_ERROR, this);
  261. return;
  262. }
  263. for (UserConfig *config : m_configList) {
  264. UserConfigService().UpdateUserConfig(*config);
  265. }
  266. loadData();
  267. }
  268. void ConfigExpertDataWidget::slotRefresh()
  269. {
  270. loadData();
  271. }
  272. void ConfigExpertDataWidget::slotAddUser(QFUser *user)
  273. {
  274. qDebug() << __FUNCTION__ << __LINE__ << user->userName << endl;
  275. for (UserConfig *config : m_configList) {
  276. if (config->userId == user->id) {
  277. return;
  278. }
  279. }
  280. UserConfig cfg;
  281. cfg.userId = user->id;
  282. cfg.userName = user->userName;
  283. cfg.engineerId = m_process.projectId;
  284. cfg.weight = m_configList.count() > 0 ? 0 : 100;
  285. UserConfigService().AddUserConfig(cfg);
  286. loadData();
  287. }
  288. void ConfigExpertDataWidget::slotRemoveConfig(UserConfig *config)
  289. {
  290. qDebug() << __FUNCTION__ << __LINE__ << config->userName << endl;
  291. bool ret = UserConfigService().DeleteUserConfigById(config->id);
  292. if (ret) {
  293. ret = NodeMatrixService().deleteExpertData(config->engineerId, config->userId);
  294. if (ret) {
  295. loadData();
  296. selectFirstImported();
  297. }
  298. }
  299. }
  300. void ConfigExpertDataWidget::slotImportData(UserConfig *config)
  301. {
  302. int index = m_configList.indexOf(config);
  303. m_configListWidget->setCurrentRow(index);
  304. emit sigImportData(config);
  305. }
  306. void ConfigExpertDataWidget::slotUserConfigCurrentChanged()
  307. {
  308. emit sigConfigCurrentChanged();
  309. }
  310. ConfigListItemWidget::ConfigListItemWidget(QWidget *parent) : QWidget(parent)
  311. {
  312. initWidget();
  313. initLayouts();
  314. connectSignalsAndSlots();
  315. }
  316. UserConfig *ConfigListItemWidget::userConfig() const
  317. {
  318. return m_config;
  319. }
  320. void ConfigListItemWidget::setUserConfig(UserConfig *config)
  321. {
  322. m_config = config;
  323. m_name->setText(m_config->userName);
  324. m_weight->setValue(m_config->weight);
  325. }
  326. void ConfigListItemWidget::setImported(bool imported)
  327. {
  328. m_import->setEnabled(!imported);
  329. m_import->setToolTip(imported ? "已导入" : "导入专家数据");
  330. m_import->setIcon(imported ? NEWFLICON(FluentIcon, ACCEPT)->icon() : NEWFLICON(FluentIcon, DOWNLOAD)->icon());
  331. }
  332. int ConfigListItemWidget::spinValue() const
  333. {
  334. return m_weight->value();
  335. }
  336. void ConfigListItemWidget::initWidget()
  337. {
  338. m_name = new QLabel(this);
  339. m_weight = new QSpinBox(this);
  340. m_weight->setPrefix("权重");
  341. m_weight->setSuffix("%");
  342. m_weight->setMaximum(100);
  343. m_weight->setMinimum(0);
  344. m_import = new QPushButton(NEWFLICON(FluentIcon, DOWNLOAD)->icon(), "", this);
  345. m_import->setFixedSize(QSize(25, 25));
  346. m_import->setIconSize(QSize(15, 15));
  347. m_import->setToolTip("导入专家数据");
  348. m_remove = new QPushButton(NEWFLICON(QFDIcon, Minus)->icon(), "", this);
  349. m_remove->setFixedSize(QSize(25, 25));
  350. m_remove->setIconSize(QSize(10, 10));
  351. m_remove->setToolTip("移除");
  352. }
  353. void ConfigListItemWidget::initLayouts()
  354. {
  355. m_layout = new QHBoxLayout(this);
  356. m_layout->addWidget(m_name);
  357. m_layout->addStretch();
  358. m_layout->addWidget(m_weight);
  359. m_layout->addSpacing(10);
  360. m_layout->addWidget(m_import);
  361. m_layout->addWidget(m_remove);
  362. }
  363. void ConfigListItemWidget::connectSignalsAndSlots()
  364. {
  365. connect(m_import, &QPushButton::clicked, this, &ConfigListItemWidget::slotImport);
  366. connect(m_remove, &QPushButton::clicked, this, &ConfigListItemWidget::slotRemove);
  367. }
  368. void ConfigListItemWidget::slotImport()
  369. {
  370. emit sigImport(m_config);
  371. }
  372. void ConfigListItemWidget::slotRemove()
  373. {
  374. emit sigRemove(m_config);
  375. }
  376. ExpertListItemWidget::ExpertListItemWidget(QWidget *parent) : QWidget(parent)
  377. {
  378. initWidget();
  379. initLayout();
  380. connectSignalsAndSlots();
  381. }
  382. QFUser *ExpertListItemWidget::user() const
  383. {
  384. return m_user;
  385. }
  386. void ExpertListItemWidget::setUser(QFUser *user)
  387. {
  388. m_user = user;
  389. m_name->setText(user->userName);
  390. }
  391. void ExpertListItemWidget::setIndex(int index)
  392. {
  393. m_index->setText(QString("%1").arg(index));
  394. }
  395. void ExpertListItemWidget::setAdded(bool added)
  396. {
  397. m_add->setHidden(added);
  398. }
  399. void ExpertListItemWidget::initWidget()
  400. {
  401. m_index = new QLabel(this);
  402. m_name = new QLabel(this);
  403. m_add = new QPushButton(NEWFLICON(FluentIcon, ADD)->icon(), "", this);
  404. m_add->setFixedSize(QSize(25, 25));
  405. m_add->setIconSize(QSize(10, 10));
  406. m_add->setToolTip("添加");
  407. }
  408. void ExpertListItemWidget::initLayout()
  409. {
  410. m_layout = new QHBoxLayout(this);
  411. m_layout->addWidget(m_index);
  412. m_index->setFixedWidth(25);
  413. m_layout->addSpacing(10);
  414. m_layout->addWidget(m_name);
  415. m_layout->addStretch();
  416. m_layout->addWidget(m_add);
  417. }
  418. void ExpertListItemWidget::connectSignalsAndSlots()
  419. {
  420. connect(m_add, &QPushButton::clicked, this, &ExpertListItemWidget::slotAdd);
  421. }
  422. void ExpertListItemWidget::slotAdd()
  423. {
  424. emit sigAddUser(m_user);
  425. }