ExpertInfoWidget.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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->setDateTime(QDateTime::currentDateTime());
  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(user->userNo);
  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. QFUser *ExpertInfoWidget::user() const
  63. {
  64. if (m_mode == Create) {
  65. QFUser *user = new QFUser(m_idLineEdit->text(), m_passwordLineEdit->text());
  66. user->password = m_passwordLineEdit->text();
  67. user->userName = m_nameLineEdit->text();
  68. user->writeTime = m_timeLineEdit->text();
  69. user->role = QFUser::Expert;
  70. user->remark = m_noteTextEdit->toPlainText();
  71. return user;
  72. } else {
  73. return m_user;
  74. }
  75. }
  76. void ExpertInfoWidget::initWindow()
  77. {
  78. setWindowFlags(Qt::Window);
  79. setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
  80. setFixedWidth(420);
  81. // setModal(true);
  82. // setWindowFlags(Qt::Dialog);
  83. // setWindowFlag(Qt::WindowContextHelpButtonHint, false);
  84. // resize(400, 250);
  85. }
  86. void ExpertInfoWidget::initialize()
  87. {
  88. int labelHeight = 35;
  89. int editWidth = 300;
  90. m_vBoxLayout = new QVBoxLayout(this);
  91. m_gridLayout = new QGridLayout();
  92. m_idLabel = new QLabel(this);
  93. m_idLabel->setText("账号ID:");
  94. m_idLabel->setFixedHeight(labelHeight);
  95. m_idLineEdit = new LineEdit(this);
  96. m_idLineEdit->setFixedWidth(editWidth);
  97. m_idLineEdit->setPlaceholderText("请输入a-z/A-Z/0-9数字字母组合");
  98. m_passwordLabel = new QLabel(this);
  99. m_passwordLabel->setText("用户密码:");
  100. m_passwordLabel->setFixedHeight(labelHeight);
  101. m_passwordLineEdit = new PasswordLineEdit(this);
  102. m_passwordLineEdit->setFixedWidth(editWidth);
  103. m_passwordLineEdit->setPlaceholderText("请输入密码");
  104. m_nameLabel = new QLabel(this);
  105. m_nameLabel->setText("专家名:");
  106. m_nameLabel->setFixedHeight(labelHeight);
  107. m_nameLineEdit = new LineEdit(this);
  108. m_nameLineEdit->setFixedWidth(editWidth);
  109. m_nameLineEdit->setPlaceholderText("请输入专家名");
  110. m_companyLabel = new QLabel(this);
  111. m_companyLabel->setText("单位:");
  112. m_companyLabel->setFixedHeight(labelHeight);
  113. m_companyLineEdit = new LineEdit(this);
  114. m_companyLineEdit->setFixedWidth(editWidth);
  115. m_jobLabel = new QLabel(this);
  116. m_jobLabel->setText("职务:");
  117. m_jobLabel->setFixedHeight(labelHeight);
  118. m_jobLineEdit = new LineEdit(this);
  119. m_jobLineEdit->setFixedWidth(editWidth);
  120. m_majorLabel = new QLabel(this);
  121. m_majorLabel->setText("专业:");
  122. m_majorLabel->setFixedHeight(labelHeight);
  123. m_majorLineEdit = new LineEdit(this);
  124. m_majorLineEdit->setFixedWidth(editWidth);
  125. m_contactInfoLabel = new QLabel(this);
  126. m_contactInfoLabel->setText("联系方式:");
  127. m_contactInfoLabel->setFixedHeight(labelHeight);
  128. m_contactInfoLineEdit = new LineEdit(this);
  129. m_contactInfoLineEdit->setFixedWidth(editWidth);
  130. m_timeLabel = new QLabel(this);
  131. m_timeLabel->setText("填写时间:");
  132. m_timeLabel->setFixedHeight(labelHeight);
  133. m_timeLineEdit = new DateTimeEdit(this);
  134. m_timeLineEdit->setFixedWidth(editWidth);
  135. m_noteLabel = new QLabel(this);
  136. m_noteLabel->setText("备注:");
  137. m_noteLabel->setFixedHeight(labelHeight);
  138. m_noteTextEdit = new TextEdit(this);
  139. m_noteTextEdit->setFixedWidth(editWidth);
  140. m_noteTextEdit->setFixedHeight(100);
  141. m_hBoxLayout = new QHBoxLayout();
  142. m_confirmButton = new PushButton("确认", this);
  143. m_cancelButton = new PushButton("取消", this);
  144. }
  145. void ExpertInfoWidget::initLayout()
  146. {
  147. m_vBoxLayout->setContentsMargins(20, 10, 20, 10);
  148. m_vBoxLayout->addLayout(m_gridLayout);
  149. m_gridLayout->addWidget(m_idLabel, 0, 0, 1, 1, Qt::AlignRight);
  150. m_gridLayout->addWidget(m_idLineEdit, 0, 1, 1, 2, Qt::AlignLeft);
  151. m_gridLayout->addWidget(m_passwordLabel, 1, 0, 1, 1, Qt::AlignRight);
  152. m_gridLayout->addWidget(m_passwordLineEdit, 1, 1, Qt::AlignLeft);
  153. m_gridLayout->addWidget(m_nameLabel, 2, 0, 1, 1, Qt::AlignRight);
  154. m_gridLayout->addWidget(m_nameLineEdit, 2, 1, 1, 1, Qt::AlignLeft);
  155. m_gridLayout->addWidget(m_companyLabel, 3, 0, 1, 1, Qt::AlignRight);
  156. m_gridLayout->addWidget(m_companyLineEdit, 3, 1, 1, 1, Qt::AlignLeft);
  157. m_gridLayout->addWidget(m_jobLabel, 4, 0, 1, 1, Qt::AlignRight);
  158. m_gridLayout->addWidget(m_jobLineEdit, 4, 1, 1, 1, Qt::AlignLeft);
  159. m_gridLayout->addWidget(m_majorLabel, 5, 0, 1, 1, Qt::AlignRight);
  160. m_gridLayout->addWidget(m_majorLineEdit, 5, 1, 1, 1, Qt::AlignLeft);
  161. m_gridLayout->addWidget(m_contactInfoLabel, 6, 0, 1, 1, Qt::AlignRight);
  162. m_gridLayout->addWidget(m_contactInfoLineEdit, 6, 1, 1, 1, Qt::AlignLeft);
  163. m_gridLayout->addWidget(m_timeLabel, 7, 0, 1, 1, Qt::AlignRight);
  164. m_gridLayout->addWidget(m_timeLineEdit, 7, 1, 1, 1, Qt::AlignLeft);
  165. m_gridLayout->addWidget(m_noteLabel, 8, 0, 1, 1, Qt::AlignRight | Qt::AlignTop);
  166. m_gridLayout->addWidget(m_noteTextEdit, 8, 1, 1, 1, Qt::AlignLeft);
  167. m_vBoxLayout->addStretch();
  168. m_vBoxLayout->addLayout(m_hBoxLayout);
  169. m_hBoxLayout->addStretch();
  170. m_hBoxLayout->addWidget(m_confirmButton);
  171. m_hBoxLayout->addWidget(m_cancelButton);
  172. }
  173. void ExpertInfoWidget::connectSignalsAndSlots()
  174. {
  175. connect(m_confirmButton, &PushButton::clicked, this, &ExpertInfoWidget::slotConfirm);
  176. connect(m_cancelButton, &PushButton::clicked, this, &ExpertInfoWidget::slotCancel);
  177. }
  178. /// 根据 mode 更新页面
  179. void ExpertInfoWidget::updateState()
  180. {
  181. int h = height();
  182. QString t = windowTitle();
  183. switch (m_mode) {
  184. case Create: {
  185. t = "添加专家";
  186. h = 350;
  187. break;
  188. }
  189. case Update: {
  190. t = "修改专家信息";
  191. h = 350;
  192. break;
  193. }
  194. case Info: {
  195. t = "专家信息";
  196. h = 425;
  197. break;
  198. }
  199. }
  200. setFixedHeight(h);
  201. setWindowTitle(t);
  202. setEditable(m_mode != Info);
  203. setDetailsHideen(m_mode == Create);
  204. m_passwordLabel->setHidden(m_mode == Info);
  205. m_passwordLineEdit->setHidden(m_mode == Info);
  206. m_confirmButton->setHidden(m_mode == Info);
  207. m_cancelButton->setHidden(m_mode == Info);
  208. }
  209. /// 设置用户详情是否可见
  210. /// 单位、职务、专业、联系方式
  211. void ExpertInfoWidget::setDetailsHideen(bool hidden)
  212. {
  213. m_companyLabel->setHidden(hidden);
  214. m_companyLineEdit->setHidden(hidden);
  215. m_jobLabel->setHidden(hidden);
  216. m_jobLineEdit->setHidden(hidden);
  217. m_majorLabel->setHidden(hidden);
  218. m_majorLineEdit->setHidden(hidden);
  219. m_contactInfoLabel->setHidden(hidden);
  220. m_contactInfoLineEdit->setHidden(hidden);
  221. }
  222. /// 设置所有 Edit 的可编辑状态
  223. void ExpertInfoWidget::setEditable(bool editable)
  224. {
  225. m_idLineEdit->setReadOnly(!editable);
  226. m_passwordLineEdit->setReadOnly(!editable);
  227. m_nameLineEdit->setReadOnly(!editable);
  228. m_companyLineEdit->setReadOnly(!editable);
  229. m_jobLineEdit->setReadOnly(!editable);
  230. m_majorLineEdit->setReadOnly(!editable);
  231. m_idLineEdit->setReadOnly(!editable);
  232. m_contactInfoLineEdit->setReadOnly(!editable);
  233. m_timeLineEdit->setReadOnly(!editable);
  234. m_timeLineEdit->upButton->setEnabled(editable);
  235. m_timeLineEdit->downButton->setEnabled(editable);
  236. m_noteTextEdit->setReadOnly(!editable);
  237. }
  238. void ExpertInfoWidget::slotConfirm()
  239. {
  240. emit sigConfirm();
  241. }
  242. void ExpertInfoWidget::slotCancel()
  243. {
  244. close();
  245. }