ConfigSchemeDataWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "ConfigSchemeDataWidget.h"
  2. #include "ProjectManager.h"
  3. #include "common/QFDIcon.h"
  4. #include <dbService/SchemeInfoService.h>
  5. #include <Widgets/Button.h>
  6. #include <Common/Icon.h>
  7. #include <QLabel>
  8. #include <QListWidget>
  9. #include <QBoxLayout>
  10. #include <QImageReader>
  11. #include <QTextEdit>
  12. #include <QFile>
  13. #include <QDebug>
  14. ConfigSchemeDataWidget::ConfigSchemeDataWidget(QWidget *parent) : QWidget(parent)
  15. {
  16. initWidget();
  17. initLayout();
  18. connectSignalsAndSlots();
  19. }
  20. void ConfigSchemeDataWidget::setProcess(SchemePlanManager::SchemeProcessInfo process)
  21. {
  22. m_process = process;
  23. loadData();
  24. selectFirst();
  25. }
  26. void ConfigSchemeDataWidget::loadData()
  27. {
  28. clearInfo();
  29. if (m_process.type == SchemePlanManager::ImportEvalData) {
  30. if (m_process.indexType == ProjectManager::EfficiencyIndex) {
  31. SchemeInfoService().QuerySchemeInfoByEngineerId(&m_schemeList, m_process.projectId, 1);
  32. }
  33. if (m_process.indexType == ProjectManager::OptimalIndex) {
  34. SchemeInfoService().QuerySchemeInfoByEngineerId(&m_schemeList, m_process.projectId, 0);
  35. }
  36. }
  37. refreshList();
  38. }
  39. QList<SchemaEval *> ConfigSchemeDataWidget::schemeList() const
  40. {
  41. return m_schemeList;
  42. }
  43. void ConfigSchemeDataWidget::selectFirst()
  44. {
  45. if (m_listWidget->count() <= 0) {
  46. return;
  47. }
  48. m_listWidget->setCurrentRow(0);
  49. }
  50. void ConfigSchemeDataWidget::selectLast()
  51. {
  52. if (m_listWidget->count() <= 0) {
  53. return;
  54. }
  55. m_listWidget->setCurrentRow(m_listWidget->count() - 1);
  56. }
  57. void ConfigSchemeDataWidget::initWidget()
  58. {
  59. m_titleLabel = new QLabel("添加方案数据", this);
  60. m_titleLabel->setObjectName("titleLabel");
  61. m_titleLabel->setFixedHeight(50);
  62. m_titleLabel->setContentsMargins(10, 0, 0, 0);
  63. m_listTitleLabel = new QLabel("方案列表", this);
  64. m_listTitleLabel->setObjectName("listTitleLabel");
  65. m_tipsLabel = new QLabel("已导入的数据将会显示在这里", this);
  66. m_tipsLabel->setObjectName("tipsLabel");
  67. m_tipsLabel->setHidden(true);
  68. m_addButton = new QPushButton(NEWFLICON(FluentIcon, ADD)->icon(), "", this);
  69. m_addButton->setToolTip("添加方案");
  70. m_listWidget = new QListWidget(this);
  71. m_remark = new QTextEdit(this);
  72. m_remark->setMinimumHeight(150);
  73. m_remark->setReadOnly(true);
  74. m_pic = new QLabel(this);
  75. m_pic->setFixedHeight(300);
  76. m_pic->setAlignment(Qt::AlignCenter);
  77. setStyleSheet("#titleLabel {color:#333333; font-size:16px}"
  78. "#listTitleLabel {color:#333333; font-size:12px}"
  79. "QPushButton {border: 0;background-color: qlineargradient(x1: 0, y1: 0, x2: "
  80. "0, y2: 1,stop: 0 #f8f8f8, stop: 1 #f8f8f8);}"
  81. "QPushButton::hover {border: 1px solid rgba(0, 0, 0, 0.073);}"
  82. "QPushButton::pressed {background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #dadbde, "
  83. "stop: 1 #f6f7fa);}"
  84. "QListWidget {border: 1px solid rgba(0, 0, 0, 0.073);}"
  85. "QListView::item {height:44;}");
  86. }
  87. void ConfigSchemeDataWidget::initLayout()
  88. {
  89. m_layout = new QVBoxLayout(this);
  90. m_layout->setMargin(0);
  91. m_layout->setSpacing(0);
  92. m_layout->addWidget(m_titleLabel);
  93. m_headerLayout = new QHBoxLayout();
  94. m_layout->addLayout(m_headerLayout);
  95. m_layout->addSpacing(10);
  96. m_layout->addWidget(m_tipsLabel);
  97. m_layout->addWidget(m_listWidget);
  98. m_layout->addSpacing(10);
  99. m_layout->addWidget(m_remark);
  100. m_layout->addSpacing(5);
  101. m_layout->addWidget(m_pic);
  102. m_headerLayout->addWidget(m_listTitleLabel);
  103. m_headerLayout->addStretch();
  104. m_headerLayout->addWidget(m_addButton);
  105. }
  106. void ConfigSchemeDataWidget::connectSignalsAndSlots()
  107. {
  108. connect(m_addButton, &PushButton::clicked, this, &ConfigSchemeDataWidget::slotAddDataClicked);
  109. connect(m_listWidget, &QListWidget::itemSelectionChanged, this, &ConfigSchemeDataWidget::slotSelectedChanged);
  110. }
  111. void ConfigSchemeDataWidget::refreshList()
  112. {
  113. m_listWidget->clear();
  114. for (int i = 0; i < m_schemeList.size(); ++i) {
  115. SchemaEval *eval = m_schemeList[i];
  116. QLabel *title = new QLabel;
  117. title->setText(eval->name);
  118. QPushButton *remove = new QPushButton(NEWFLICON(QFDIcon, Minus)->icon(), "", this);
  119. remove->setFixedSize(QSize(25, 25));
  120. remove->setIconSize(QSize(10, 10));
  121. remove->setToolTip("删除");
  122. connect(remove, &QPushButton::clicked, [this, i](int) { slotRemoveDataClicked(i); });
  123. QWidget *w = new QWidget;
  124. QHBoxLayout *lay = new QHBoxLayout(w);
  125. lay->addWidget(title);
  126. lay->addStretch();
  127. lay->addWidget(remove);
  128. QListWidgetItem *item = new QListWidgetItem;
  129. m_listWidget->addItem(item);
  130. m_listWidget->setItemWidget(item, w);
  131. }
  132. }
  133. void ConfigSchemeDataWidget::clearInfo()
  134. {
  135. m_schemeList.clear();
  136. m_remark->clear();
  137. m_pic->clear();
  138. }
  139. void ConfigSchemeDataWidget::resizeEvent(QResizeEvent *event)
  140. {
  141. Q_UNUSED(event)
  142. slotSelectedChanged();
  143. }
  144. void ConfigSchemeDataWidget::showEvent(QShowEvent *event)
  145. {
  146. QWidget::showEvent(event);
  147. }
  148. void ConfigSchemeDataWidget::slotAddDataClicked()
  149. {
  150. emit sigAddScheme();
  151. }
  152. void ConfigSchemeDataWidget::slotSelectedChanged()
  153. {
  154. int row = m_listWidget->currentRow();
  155. if (m_schemeList.size() <= row || row < 0) {
  156. return;
  157. }
  158. SchemaEval *scheme = m_schemeList[row];
  159. m_remark->setText(scheme->remark);
  160. QFile file(scheme->filePath);
  161. file.open(QIODevice::ReadOnly);
  162. QByteArray data = file.readAll();
  163. QPixmap pixmap;
  164. pixmap.loadFromData(data);
  165. pixmap = pixmap.scaled(m_pic->width(), m_pic->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  166. m_pic->setPixmap(pixmap);
  167. }
  168. void ConfigSchemeDataWidget::slotRemoveDataClicked(int index)
  169. {
  170. bool ret = SchemeInfoService().deleteScheme(m_schemeList[index]->id);
  171. if (ret) {
  172. m_schemeList.removeAt(index);
  173. refreshList();
  174. emit sigDeleteScheme();
  175. if (m_schemeList.size() > 0) {
  176. selectFirst();
  177. } else {
  178. clearInfo();
  179. }
  180. }
  181. }