ExpertInfoWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "ExpertInfoWidget.h"
  2. #include <dbService/ClassSet.h>
  3. #include <Widgets/LineEdit.h>
  4. #include <Widgets/Button.h>
  5. #include <Widgets/SpinBox.h>
  6. #include <QBoxLayout>
  7. #include <QGridLayout>
  8. #include <QLabel>
  9. #include <QDebug>
  10. ExpertInfoWidget::ExpertInfoWidget(QWidget *parent) : QWidget(parent)
  11. {
  12. initWindow();
  13. initialize();
  14. initLayout();
  15. connectSignalsAndSlots();
  16. updateState();
  17. }
  18. void ExpertInfoWidget::showEvent(QShowEvent *event)
  19. {
  20. QWidget::showEvent(event);
  21. clearFocus();
  22. }
  23. void ExpertInfoWidget::hideEvent(QHideEvent *event)
  24. {
  25. QWidget::hideEvent(event);
  26. }
  27. void ExpertInfoWidget::setMode(ExpertInfoWidget::Mode mode)
  28. {
  29. m_mode = mode;
  30. updateState();
  31. }
  32. void ExpertInfoWidget::clearInputs()
  33. {
  34. m_idLineEdit->clear();
  35. m_passwordLineEdit->clear();
  36. m_nameLineEdit->clear();
  37. m_companyLineEdit->clear();
  38. m_jobLineEdit->clear();
  39. m_majorLineEdit->clear();
  40. m_contactInfoLineEdit->clear();
  41. m_timeLineEdit->clear();
  42. m_noteTextEdit->clear();
  43. }
  44. void ExpertInfoWidget::setUser(QFUser *user)
  45. {
  46. m_user = user;
  47. if (m_user == nullptr) {
  48. clearInputs();
  49. return;
  50. }
  51. m_idLineEdit->setText(QString::number(user->id));
  52. m_nameLineEdit->setText(user->userName);
  53. m_companyLineEdit->setText(user->workPosition);
  54. m_jobLineEdit->setText(user->post);
  55. m_majorLineEdit->setText(user->major);
  56. m_contactInfoLineEdit->setText(user->phone);
  57. QDateTime dt = QDateTime::fromString(user->writeTime, "yyyy/M/d H:mm");
  58. m_timeLineEdit->setDateTime(dt);
  59. m_noteTextEdit->setText(user->remark);
  60. qDebug() << __FUNCTION__ << __LINE__ << user->writeTime << dt << user->remark;
  61. }
  62. void ExpertInfoWidget::initWindow()
  63. {
  64. setWindowFlags(Qt::Window);
  65. setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
  66. setFixedWidth(400);
  67. // setModal(true);
  68. // setWindowFlags(Qt::Dialog);
  69. // setWindowFlag(Qt::WindowContextHelpButtonHint, false);
  70. // resize(400, 250);
  71. }
  72. void ExpertInfoWidget::initialize()
  73. {
  74. int labelHeight = 35;
  75. int editWidth = 300;
  76. m_vBoxLayout = new QVBoxLayout(this);
  77. m_gridLayout = new QGridLayout();
  78. m_idLabel = new QLabel(this);
  79. m_idLabel->setText("账号ID:");
  80. m_idLabel->setFixedHeight(labelHeight);
  81. m_idLineEdit = new LineEdit(this);
  82. m_idLineEdit->setFixedWidth(editWidth);
  83. m_idLineEdit->setPlaceholderText("请输入a-z/A-Z/0-9数字字母组合");
  84. m_passwordLabel = new QLabel(this);
  85. m_passwordLabel->setText("用户密码:");
  86. m_passwordLabel->setFixedHeight(labelHeight);
  87. m_passwordLineEdit = new PasswordLineEdit(this);
  88. m_passwordLineEdit->setFixedWidth(editWidth);
  89. m_passwordLineEdit->setPlaceholderText("请输入密码");
  90. m_nameLabel = new QLabel(this);
  91. m_nameLabel->setText("专家名:");
  92. m_nameLabel->setFixedHeight(labelHeight);
  93. m_nameLineEdit = new LineEdit(this);
  94. m_nameLineEdit->setFixedWidth(editWidth);
  95. m_nameLineEdit->setPlaceholderText("请输入专家名");
  96. m_companyLabel = new QLabel(this);
  97. m_companyLabel->setText("单位:");
  98. m_companyLabel->setFixedHeight(labelHeight);
  99. m_companyLineEdit = new LineEdit(this);
  100. m_companyLineEdit->setFixedWidth(editWidth);
  101. m_jobLabel = new QLabel(this);
  102. m_jobLabel->setText("职务:");
  103. m_jobLabel->setFixedHeight(labelHeight);
  104. m_jobLineEdit = new LineEdit(this);
  105. m_jobLineEdit->setFixedWidth(editWidth);
  106. m_majorLabel = new QLabel(this);
  107. m_majorLabel->setText("专业:");
  108. m_majorLabel->setFixedHeight(labelHeight);
  109. m_majorLineEdit = new LineEdit(this);
  110. m_majorLineEdit->setFixedWidth(editWidth);
  111. m_contactInfoLabel = new QLabel(this);
  112. m_contactInfoLabel->setText("联系方式:");
  113. m_contactInfoLabel->setFixedHeight(labelHeight);
  114. m_contactInfoLineEdit = new LineEdit(this);
  115. m_contactInfoLineEdit->setFixedWidth(editWidth);
  116. m_timeLabel = new QLabel(this);
  117. m_timeLabel->setText("填写时间:");
  118. m_timeLabel->setFixedHeight(labelHeight);
  119. m_timeLineEdit = new DateTimeEdit(this);
  120. m_timeLineEdit->setFixedWidth(editWidth);
  121. m_noteLabel = new QLabel(this);
  122. m_noteLabel->setText("备注:");
  123. m_noteLabel->setFixedHeight(labelHeight);
  124. m_noteTextEdit = new TextEdit(this);
  125. m_noteTextEdit->setFixedWidth(editWidth);
  126. m_noteTextEdit->setFixedHeight(100);
  127. m_hBoxLayout = new QHBoxLayout();
  128. m_confirmButton = new PushButton("确认", this);
  129. m_cancelButton = new PushButton("取消", this);
  130. }
  131. void ExpertInfoWidget::initLayout()
  132. {
  133. m_vBoxLayout->setContentsMargins(20, 10, 20, 10);
  134. m_vBoxLayout->addLayout(m_gridLayout);
  135. m_gridLayout->addWidget(m_idLabel, 0, 0, 1, 1, Qt::AlignRight);
  136. m_gridLayout->addWidget(m_idLineEdit, 0, 1, 1, 2, Qt::AlignLeft);
  137. m_gridLayout->addWidget(m_passwordLabel, 1, 0, 1, 1, Qt::AlignRight);
  138. m_gridLayout->addWidget(m_passwordLineEdit, 1, 1, Qt::AlignLeft);
  139. m_gridLayout->addWidget(m_nameLabel, 2, 0, 1, 1, Qt::AlignRight);
  140. m_gridLayout->addWidget(m_nameLineEdit, 2, 1, 1, 1, Qt::AlignLeft);
  141. m_gridLayout->addWidget(m_companyLabel, 3, 0, 1, 1, Qt::AlignRight);
  142. m_gridLayout->addWidget(m_companyLineEdit, 3, 1, 1, 1, Qt::AlignLeft);
  143. m_gridLayout->addWidget(m_jobLabel, 4, 0, 1, 1, Qt::AlignRight);
  144. m_gridLayout->addWidget(m_jobLineEdit, 4, 1, 1, 1, Qt::AlignLeft);
  145. m_gridLayout->addWidget(m_majorLabel, 5, 0, 1, 1, Qt::AlignRight);
  146. m_gridLayout->addWidget(m_majorLineEdit, 5, 1, 1, 1, Qt::AlignLeft);
  147. m_gridLayout->addWidget(m_contactInfoLabel, 6, 0, 1, 1, Qt::AlignRight);
  148. m_gridLayout->addWidget(m_contactInfoLineEdit, 6, 1, 1, 1, Qt::AlignLeft);
  149. m_gridLayout->addWidget(m_timeLabel, 7, 0, 1, 1, Qt::AlignRight);
  150. m_gridLayout->addWidget(m_timeLineEdit, 7, 1, 1, 1, Qt::AlignLeft);
  151. m_gridLayout->addWidget(m_noteLabel, 8, 0, 1, 1, Qt::AlignRight | Qt::AlignTop);
  152. m_gridLayout->addWidget(m_noteTextEdit, 8, 1, 1, 1, Qt::AlignLeft);
  153. m_vBoxLayout->addStretch();
  154. m_vBoxLayout->addLayout(m_hBoxLayout);
  155. m_hBoxLayout->addStretch();
  156. m_hBoxLayout->addWidget(m_confirmButton);
  157. m_hBoxLayout->addWidget(m_cancelButton);
  158. }
  159. void ExpertInfoWidget::connectSignalsAndSlots()
  160. {
  161. connect(m_confirmButton, &PushButton::clicked, this, &ExpertInfoWidget::slotConfirm);
  162. connect(m_cancelButton, &PushButton::clicked, this, &ExpertInfoWidget::slotCancel);
  163. }
  164. /// 根据 mode 更新页面
  165. void ExpertInfoWidget::updateState()
  166. {
  167. int h = height();
  168. QString t = windowTitle();
  169. switch (m_mode) {
  170. case Create: {
  171. t = "添加用户";
  172. h = 350;
  173. break;
  174. }
  175. case Update: {
  176. t = "修改用户信息";
  177. h = 350;
  178. break;
  179. }
  180. case Info: {
  181. t = "用户信息";
  182. h = 425;
  183. break;
  184. }
  185. }
  186. setFixedHeight(h);
  187. setWindowTitle(t);
  188. setEditable(m_mode != Info);
  189. setDetailsHideen(m_mode == Create);
  190. m_passwordLabel->setHidden(m_mode == Info);
  191. m_passwordLineEdit->setHidden(m_mode == Info);
  192. m_confirmButton->setHidden(m_mode == Info);
  193. m_cancelButton->setHidden(m_mode == Info);
  194. }
  195. /// 设置用户详情是否可见
  196. /// 单位、职务、专业、联系方式
  197. void ExpertInfoWidget::setDetailsHideen(bool hidden)
  198. {
  199. m_companyLabel->setHidden(hidden);
  200. m_companyLineEdit->setHidden(hidden);
  201. m_jobLabel->setHidden(hidden);
  202. m_jobLineEdit->setHidden(hidden);
  203. m_majorLabel->setHidden(hidden);
  204. m_majorLineEdit->setHidden(hidden);
  205. m_contactInfoLabel->setHidden(hidden);
  206. m_contactInfoLineEdit->setHidden(hidden);
  207. }
  208. /// 设置所有 Edit 的可编辑状态
  209. void ExpertInfoWidget::setEditable(bool editable)
  210. {
  211. m_idLineEdit->setReadOnly(!editable);
  212. m_passwordLineEdit->setReadOnly(!editable);
  213. m_nameLineEdit->setReadOnly(!editable);
  214. m_companyLineEdit->setReadOnly(!editable);
  215. m_jobLineEdit->setReadOnly(!editable);
  216. m_majorLineEdit->setReadOnly(!editable);
  217. m_idLineEdit->setReadOnly(!editable);
  218. m_contactInfoLineEdit->setReadOnly(!editable);
  219. m_timeLineEdit->setReadOnly(!editable);
  220. m_noteTextEdit->setReadOnly(!editable);
  221. }
  222. void ExpertInfoWidget::slotConfirm()
  223. {
  224. close();
  225. }
  226. void ExpertInfoWidget::slotCancel()
  227. {
  228. close();
  229. }