ExpertManageView.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "ExpertManageView.h"
  2. #include "QFDIcon.h"
  3. #include "MainWindow.h"
  4. #include "ExpertListWidget.h"
  5. #include "ExpertManager.h"
  6. #include "QFDAlert.h"
  7. #include <dbService/UserService.h>
  8. #include <Widgets/Button.h>
  9. #include <Widgets/LineEdit.h>
  10. #include <DialogBox/Dialog.h>
  11. #include <QFDAlert.h>
  12. #include <QBoxLayout>
  13. #include <QLabel>
  14. #include <QDebug>
  15. ExpertManageView::ExpertManageView(QWidget *parent) : QWidget(parent) { }
  16. void ExpertManageView::showEvent(QShowEvent *event)
  17. {
  18. QWidget::showEvent(event);
  19. init();
  20. }
  21. void ExpertManageView::hideEvent(QHideEvent *event)
  22. {
  23. QWidget::hideEvent(event);
  24. hideExpertInfoWidget();
  25. }
  26. void ExpertManageView::init()
  27. {
  28. if (m_initilized == false) {
  29. initialize();
  30. initLayout();
  31. connectSignalsAndSlots();
  32. m_initilized = true;
  33. }
  34. refreshList();
  35. }
  36. void ExpertManageView::initialize()
  37. {
  38. m_vBoxLayout = new QVBoxLayout(this);
  39. m_titleLabel = new QLabel(this);
  40. m_titleLabel->setText("专家列表");
  41. QFont ft("Microsoft YaHei", 12);
  42. m_titleLabel->setFont(ft);
  43. m_hBoxLayout = new QHBoxLayout();
  44. m_searchLineEdit = new LineEdit(this);
  45. m_searchLineEdit->setIsClearButtonEnabled(true);
  46. m_searchLineEdit->setPlaceholderText("搜索");
  47. m_searchLineEdit->setMinimumWidth(300);
  48. m_addExpertButton = new PushButton("添加", NEWFLICON(FluentIcon, ADD), this);
  49. m_detailButton = new ToolButton(NEWFLICON(QFDIcon, Detail), this);
  50. m_detailButton->setToolTip("详细信息");
  51. m_deleteButton = new ToolButton(NEWFLICON(FluentIcon, DELETE), this);
  52. m_deleteButton->setToolTip("删除用户");
  53. m_expertListWidget = new ExpertListWidget(this);
  54. }
  55. void ExpertManageView::initLayout()
  56. {
  57. m_vBoxLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  58. m_vBoxLayout->setContentsMargins(15, 10, 10, 15);
  59. m_vBoxLayout->addLayout(m_hBoxLayout);
  60. m_hBoxLayout->setContentsMargins(10, 0, 10, 0);
  61. m_hBoxLayout->addWidget(m_titleLabel);
  62. m_hBoxLayout->addSpacing(15);
  63. m_hBoxLayout->addWidget(m_searchLineEdit, 0, Qt::AlignLeft);
  64. m_hBoxLayout->addWidget(m_addExpertButton, 1, Qt::AlignLeft);
  65. m_hBoxLayout->addStretch();
  66. m_hBoxLayout->addWidget(m_detailButton);
  67. m_hBoxLayout->addWidget(m_deleteButton);
  68. setListButtonHidden(true);
  69. m_vBoxLayout->addWidget(m_expertListWidget);
  70. }
  71. void ExpertManageView::connectSignalsAndSlots()
  72. {
  73. connect(m_addExpertButton, &PushButton::clicked, this, &ExpertManageView::slotAddExpertClicked);
  74. connect(m_detailButton, &PushButton::clicked, this, &ExpertManageView::slotDetailClicked);
  75. connect(m_deleteButton, &PushButton::clicked, this, &ExpertManageView::slotDeleteClicked);
  76. connect(m_expertListWidget, &ExpertListWidget::signalSelectionChanged, this,
  77. &ExpertManageView::slotListSelectionChanged);
  78. connect(m_expertListWidget, &ExpertListWidget::siganlItemDoubleClicked, this,
  79. &ExpertManageView::slotListItemDoubleClicked);
  80. connect(m_searchLineEdit, &LineEdit::textChanged, this, &ExpertManageView::slotSearchTextChanged);
  81. }
  82. void ExpertManageView::setListButtonHidden(bool hidden)
  83. {
  84. m_detailButton->setHidden(hidden);
  85. m_deleteButton->setHidden(hidden);
  86. }
  87. void ExpertManageView::showExpertInfoWidget(ExpertInfoWidget::Mode mode)
  88. {
  89. if (m_expertInfoWidget == nullptr) {
  90. m_expertInfoWidget = new ExpertInfoWidget(this);
  91. connect(m_expertInfoWidget, &ExpertInfoWidget::sigConfirm, this, &ExpertManageView::slotConfirmAddExpert);
  92. }
  93. if (m_expertInfoWidget->isVisible() == false) {
  94. m_expertInfoWidget->show();
  95. m_expertInfoWidget->setFocus(Qt::NoFocusReason);
  96. }
  97. m_expertInfoWidget->setMode(mode);
  98. m_expertInfoWidget->clearInputs();
  99. }
  100. void ExpertManageView::hideExpertInfoWidget()
  101. {
  102. if (m_expertInfoWidget && m_expertInfoWidget->isVisible()) {
  103. m_expertInfoWidget->close();
  104. }
  105. }
  106. void ExpertManageView::showExpertInfo(QFUser *user)
  107. {
  108. showExpertInfoWidget();
  109. m_expertInfoWidget->setUser(user);
  110. }
  111. void ExpertManageView::refreshList()
  112. {
  113. qDeleteAll(m_userList);
  114. m_userList.clear();
  115. if (!UserService().QueryUserListNotAdmin(&m_userList)) {
  116. QFDAlert::showAlertWithCode(QF_CODE_DATA_ERROR, this);
  117. return;
  118. }
  119. m_expertListWidget->showUsers(searchResult());
  120. }
  121. QList<QFUser *> ExpertManageView::searchResult() const
  122. {
  123. QList<QFUser *> list;
  124. for (QFUser *user : m_userList) {
  125. if (user->userName.contains(m_searchLineEdit->text())) {
  126. list.append(user);
  127. }
  128. }
  129. return list;
  130. }
  131. void ExpertManageView::slotAddExpertClicked()
  132. {
  133. showExpertInfoWidget(ExpertInfoWidget::Create);
  134. m_expertListWidget->clearSelection();
  135. }
  136. void ExpertManageView::slotDetailClicked()
  137. {
  138. QList<QFUser *> list = searchResult();
  139. int row = m_expertListWidget->selectedRow();
  140. if (row < 0 || row >= list.count()) {
  141. return;
  142. }
  143. QFUser *user = list[row];
  144. showExpertInfo(user);
  145. }
  146. void ExpertManageView::slotDeleteClicked()
  147. {
  148. hideExpertInfoWidget();
  149. MessageBox *m = new MessageBox("删除专家信息", "删除后不可恢复,确认删除?", topLevelWidget());
  150. connect(m, &MessageBox::yesSignal, this, &ExpertManageView::slotConfirmDeleteExpert);
  151. m->exec();
  152. }
  153. void ExpertManageView::slotListSelectionChanged()
  154. {
  155. setListButtonHidden(!m_expertListWidget->hasSelectedItem());
  156. }
  157. void ExpertManageView::slotListItemDoubleClicked(int row)
  158. {
  159. QList<QFUser *> list = searchResult();
  160. if (row < 0 || row >= list.count()) {
  161. return;
  162. }
  163. QFUser *user = list[row];
  164. showExpertInfo(user);
  165. }
  166. void ExpertManageView::slotSearchTextChanged()
  167. {
  168. m_expertListWidget->showUsers(searchResult());
  169. }
  170. void ExpertManageView::slotConfirmAddExpert()
  171. {
  172. QFUser *user = m_expertInfoWidget->user();
  173. int code = ExpertManager::addUser(*user);
  174. QFDAlert::showAlertWithCode(code, this);
  175. if (code == QF_CODE_ADD_USER_SUCCEEDED) {
  176. hideExpertInfoWidget();
  177. refreshList();
  178. }
  179. }
  180. void ExpertManageView::slotConfirmDeleteExpert()
  181. {
  182. QList<QFUser *> list = searchResult();
  183. int row = m_expertListWidget->selectedRow();
  184. if (row < 0 || row >= list.count()) {
  185. return;
  186. }
  187. QFUser *user = list[row];
  188. int code = ExpertManager::deleteUser(user->id);
  189. QFDAlert::showAlertWithCode(code, this);
  190. if (code == QF_CODE_DELETE_USER_SUCCEEDED) {
  191. refreshList();
  192. }
  193. qDebug() << "---" << code << user->id;
  194. }