NoBondPtEditDialog.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "NoBondPtEditDialog.h"
  2. // 构造函数实现
  3. NoBondPtEditDialog::NoBondPtEditDialog(int rows, int cols, const std::vector<XY_LONG_STRUCT>& selectedPts, QWidget* parent)
  4. : QDialog(parent)
  5. {
  6. this->setStyleSheet(R"(
  7. QDialog {
  8. background-color: #f0f0f0;
  9. color: black;
  10. }
  11. QPushButton {
  12. background-color: white;
  13. color: black;
  14. border: 1px solid gray;
  15. border-radius: 4px;
  16. }
  17. QPushButton:hover {
  18. background-color: #e0e0e0;
  19. }
  20. )");
  21. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  22. QGridLayout* gridLayout = new QGridLayout();
  23. // 初始化已选点集合
  24. for (const auto& pt : selectedPts) {
  25. selectedSet.insert({ pt.x, pt.y });
  26. }
  27. for (int r = 0; r < rows; ++r) {
  28. for (int c = 0; c < cols; ++c) {
  29. QPushButton* btn = new QPushButton(QString("%1,%2").arg(r).arg(c));
  30. btn->setFixedSize(40, 30);
  31. QPair<int, int> pos(r, c);
  32. buttonMap[btn] = pos;
  33. bool selected = selectedSet.contains(pos);
  34. btn->setStyleSheet(selected ? "background-color: gray;" : "background-color: lightgreen;");
  35. connect(btn, &QPushButton::clicked, this, [=]() {
  36. if (selectedSet.contains(pos)) {
  37. selectedSet.remove(pos);
  38. btn->setStyleSheet("background-color: lightgreen;");
  39. }
  40. else {
  41. selectedSet.insert(pos);
  42. btn->setStyleSheet("background-color: gray;");
  43. }
  44. });
  45. gridLayout->addWidget(btn, r, c);
  46. }
  47. }
  48. mainLayout->addLayout(gridLayout);
  49. // 底部按钮
  50. QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  51. connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  52. connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
  53. mainLayout->addWidget(buttons);
  54. }
  55. // 获取选中的点
  56. QVector<XY_LONG_STRUCT> NoBondPtEditDialog::getSelectedPoints() const {
  57. QVector<XY_LONG_STRUCT> result;
  58. for (const auto& pos : selectedSet) {
  59. XY_LONG_STRUCT pt;
  60. pt.x = pos.first;
  61. pt.y = pos.second;
  62. result.append(pt);
  63. }
  64. return result;
  65. }
  66. void NoBondPtEditDialog::mousePressEvent(QMouseEvent* event) {
  67. if (event->button() == Qt::LeftButton || event->button() == Qt::RightButton) {
  68. dragStart = event->pos();
  69. dragButton = event->button(); // 记录当前拖动按钮
  70. if (!rubberBand)
  71. rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  72. rubberBand->setGeometry(QRect(dragStart, QSize()));
  73. rubberBand->show();
  74. isDragging = true;
  75. }
  76. }
  77. void NoBondPtEditDialog::mouseMoveEvent(QMouseEvent* event) {
  78. if (isDragging && rubberBand) {
  79. rubberBand->setGeometry(QRect(dragStart, event->pos()).normalized());
  80. }
  81. }
  82. void NoBondPtEditDialog::mouseReleaseEvent(QMouseEvent* event) {
  83. if (!rubberBand || !isDragging || event->button() != dragButton) return;
  84. rubberBand->hide();
  85. QRect selectionRect = rubberBand->geometry();
  86. for (auto it = buttonMap.begin(); it != buttonMap.end(); ++it) {
  87. QPushButton* btn = it.key();
  88. QPair<int, int> pos = it.value();
  89. if (btn->geometry().intersects(selectionRect)) {
  90. if (dragButton == Qt::LeftButton) {
  91. // toggle:如果选中就取消,否则加入
  92. if (selectedSet.contains(pos)) {
  93. selectedSet.remove(pos);
  94. btn->setStyleSheet("background-color: lightgreen;");
  95. }
  96. else {
  97. selectedSet.insert(pos);
  98. btn->setStyleSheet("background-color: gray;");
  99. }
  100. }
  101. else if (dragButton == Qt::RightButton) {
  102. // 强制取消
  103. selectedSet.remove(pos);
  104. btn->setStyleSheet("background-color: lightgreen;");
  105. }
  106. }
  107. }
  108. isDragging = false;
  109. dragButton = Qt::NoButton;
  110. }