EditNodeWidget.cpp 4.8 KB

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