EditNodeWidget.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "EditNodeWidget.h"
  2. #include "SchemePlanManager.h"
  3. #include <Widgets/LineEdit.h>
  4. #include <Widgets/Button.h>
  5. #include <Widgets/CheckBox.h>
  6. #include <QLabel>
  7. #include <QBoxLayout>
  8. #include <QGridLayout>
  9. #include <QComboBox>
  10. #include <QCheckBox>
  11. #include <QMetaEnum>
  12. #include <QDebug>
  13. EditNodeWidget::EditNodeWidget(QWidget *parent) : QDialog(parent)
  14. {
  15. initWindow();
  16. initWidgets();
  17. initLayout();
  18. connectSignalsAndSlots();
  19. }
  20. CNodeData EditNodeWidget::node() const
  21. {
  22. return m_node;
  23. }
  24. void EditNodeWidget::setNode(CNodeData n)
  25. {
  26. m_node = n;
  27. m_name->setText(n.name);
  28. m_remark->setText(n.remark);
  29. m_dimen->setText(n.dimension);
  30. m_costComboBox->setCurrentIndex(n.type);
  31. m_validCheckBox->setChecked(!n.isEffective);
  32. m_confirm->setEnabled(false);
  33. }
  34. void EditNodeWidget::initWindow()
  35. {
  36. setWindowTitle("编辑节点");
  37. // setWindowFlags(Qt::Window);
  38. // setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
  39. setModal(true);
  40. setWindowFlags(Qt::Dialog);
  41. setWindowFlag(Qt::WindowContextHelpButtonHint, false);
  42. setFixedSize(400, 350);
  43. }
  44. void EditNodeWidget::initWidgets()
  45. {
  46. m_nameLabel = new QLabel("名称:", this);
  47. m_name = new LineEdit(this);
  48. m_remarkLabel = new QLabel("内涵:", this);
  49. m_remarkLabel->setContentsMargins(0, 10, 0, 10);
  50. m_remark = new TextEdit(this);
  51. m_dimen = new LineEdit(this);
  52. m_dimenLabel = new QLabel("量纲:", this);
  53. m_costLabel = new QLabel("类型:", this);
  54. m_costComboBox = new QComboBox(this);
  55. setUpCostTypes();
  56. m_validLabel = new QLabel("生效:", this);
  57. m_validCheckBox = new CheckBox("", this);
  58. m_validCheckBox->setChecked(true);
  59. m_confirm = new PushButton("保存", this);
  60. m_cancel = new PushButton("取消", this);
  61. }
  62. void EditNodeWidget::initLayout()
  63. {
  64. m_layout = new QVBoxLayout(this);
  65. m_layout->setMargin(20);
  66. m_gridLayout = new QGridLayout();
  67. m_layout->addLayout(m_gridLayout);
  68. m_layout->addSpacing(20);
  69. m_btnLayout = new QHBoxLayout();
  70. m_layout->addLayout(m_btnLayout);
  71. int minW = 280;
  72. m_name->setMinimumWidth(minW);
  73. m_remark->setMinimumWidth(minW);
  74. m_dimen->setMinimumWidth(minW);
  75. m_gridLayout->addWidget(m_nameLabel, 0, 0, 1, 1, Qt::AlignRight);
  76. m_gridLayout->addWidget(m_name, 0, 1, 1, 1, Qt::AlignLeft);
  77. m_gridLayout->addWidget(m_remarkLabel, 1, 0, 1, 1, Qt::AlignRight | Qt::AlignTop);
  78. m_gridLayout->addWidget(m_remark, 1, 1, 1, 1, Qt::AlignLeft);
  79. m_gridLayout->addWidget(m_dimenLabel, 2, 0, 1, 1, Qt::AlignRight);
  80. m_gridLayout->addWidget(m_dimen, 2, 1, 1, 1, Qt::AlignLeft);
  81. m_gridLayout->addWidget(m_costLabel, 3, 0, 1, 1, Qt::AlignRight);
  82. m_gridLayout->addWidget(m_costComboBox, 3, 1, 1, 1, Qt::AlignLeft);
  83. m_gridLayout->addWidget(m_validLabel, 4, 0, 1, 1, Qt::AlignRight);
  84. m_gridLayout->addWidget(m_validCheckBox, 4, 1, 1, 1, Qt::AlignLeft);
  85. m_btnLayout->addStretch();
  86. m_btnLayout->addWidget(m_confirm);
  87. m_btnLayout->addSpacing(20);
  88. m_btnLayout->addWidget(m_cancel);
  89. }
  90. void EditNodeWidget::connectSignalsAndSlots()
  91. {
  92. connect(m_name, &LineEdit::textChanged, this, &EditNodeWidget::slotEditChanged);
  93. connect(m_remark, &TextEdit::textChanged, this, &EditNodeWidget::slotEditChanged);
  94. connect(m_dimen, &LineEdit::textChanged, this, &EditNodeWidget::slotEditChanged);
  95. connect(m_costComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCostComboChanged(int)));
  96. connect(m_validCheckBox, &QCheckBox::stateChanged, this, &EditNodeWidget::slotEditChanged);
  97. connect(m_confirm, &PushButton::clicked, this, &EditNodeWidget::slotConfirmCLicked);
  98. connect(m_cancel, &PushButton::clicked, this, &EditNodeWidget::slotConcelClicked);
  99. }
  100. void EditNodeWidget::slotEditChanged()
  101. {
  102. m_confirm->setEnabled(true);
  103. m_node.name = m_name->text();
  104. m_node.remark = m_remark->toPlainText();
  105. m_node.dimension = m_dimen->text();
  106. m_node.type = m_costComboBox->currentIndex();
  107. m_node.isEffective = !m_validCheckBox->isChecked();
  108. }
  109. void EditNodeWidget::slotCostComboChanged(int)
  110. {
  111. slotEditChanged();
  112. }
  113. void EditNodeWidget::slotConfirmCLicked()
  114. {
  115. emit sigSaveNode(m_node);
  116. close();
  117. }
  118. void EditNodeWidget::slotConcelClicked()
  119. {
  120. close();
  121. }
  122. void EditNodeWidget::setUpCostTypes()
  123. {
  124. QMetaEnum metaEnum = QMetaEnum::fromType<SchemePlanManager::IndexCostType>();
  125. for (int i = 0; i < metaEnum.keyCount(); i++) {
  126. SchemePlanManager::IndexCostType value = (SchemePlanManager::IndexCostType)metaEnum.value(i);
  127. QString str = SchemePlanManager::stringFromIndexCostType(value);
  128. m_costComboBox->addItem(str);
  129. }
  130. m_costComboBox->setCurrentIndex(0);
  131. }