123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #include "NoBondPtEditDialog.h"
- // 构造函数实现
- NoBondPtEditDialog::NoBondPtEditDialog(int rows, int cols, const std::vector<XY_LONG_STRUCT>& selectedPts, QWidget* parent)
- : QDialog(parent)
- {
- this->setStyleSheet(R"(
- QDialog {
- background-color: #f0f0f0;
- color: black;
- }
- QPushButton {
- background-color: white;
- color: black;
- border: 1px solid gray;
- border-radius: 4px;
- }
- QPushButton:hover {
- background-color: #e0e0e0;
- }
- )");
- QVBoxLayout* mainLayout = new QVBoxLayout(this);
- QGridLayout* gridLayout = new QGridLayout();
- // 初始化已选点集合
- for (const auto& pt : selectedPts) {
- selectedSet.insert({ pt.x, pt.y });
- }
- for (int r = 0; r < rows; ++r) {
- for (int c = 0; c < cols; ++c) {
- QPushButton* btn = new QPushButton(QString("%1,%2").arg(r).arg(c));
- btn->setFixedSize(40, 30);
- QPair<int, int> pos(r, c);
- buttonMap[btn] = pos;
- bool selected = selectedSet.contains(pos);
- btn->setStyleSheet(selected ? "background-color: gray;" : "background-color: lightgreen;");
- connect(btn, &QPushButton::clicked, this, [=]() {
- if (selectedSet.contains(pos)) {
- selectedSet.remove(pos);
- btn->setStyleSheet("background-color: lightgreen;");
- }
- else {
- selectedSet.insert(pos);
- btn->setStyleSheet("background-color: gray;");
- }
- });
- gridLayout->addWidget(btn, r, c);
- }
- }
- mainLayout->addLayout(gridLayout);
- // 底部按钮
- QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
- connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
- connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
- mainLayout->addWidget(buttons);
- }
- // 获取选中的点
- QVector<XY_LONG_STRUCT> NoBondPtEditDialog::getSelectedPoints() const {
- QVector<XY_LONG_STRUCT> result;
- for (const auto& pos : selectedSet) {
- XY_LONG_STRUCT pt;
- pt.x = pos.first;
- pt.y = pos.second;
- result.append(pt);
- }
- return result;
- }
- void NoBondPtEditDialog::mousePressEvent(QMouseEvent* event) {
- if (event->button() == Qt::LeftButton || event->button() == Qt::RightButton) {
- dragStart = event->pos();
- dragButton = event->button(); // 记录当前拖动按钮
- if (!rubberBand)
- rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
- rubberBand->setGeometry(QRect(dragStart, QSize()));
- rubberBand->show();
- isDragging = true;
- }
- }
- void NoBondPtEditDialog::mouseMoveEvent(QMouseEvent* event) {
- if (isDragging && rubberBand) {
- rubberBand->setGeometry(QRect(dragStart, event->pos()).normalized());
- }
- }
- void NoBondPtEditDialog::mouseReleaseEvent(QMouseEvent* event) {
- if (!rubberBand || !isDragging || event->button() != dragButton) return;
- rubberBand->hide();
- QRect selectionRect = rubberBand->geometry();
- for (auto it = buttonMap.begin(); it != buttonMap.end(); ++it) {
- QPushButton* btn = it.key();
- QPair<int, int> pos = it.value();
- if (btn->geometry().intersects(selectionRect)) {
- if (dragButton == Qt::LeftButton) {
- // toggle:如果选中就取消,否则加入
- if (selectedSet.contains(pos)) {
- selectedSet.remove(pos);
- btn->setStyleSheet("background-color: lightgreen;");
- }
- else {
- selectedSet.insert(pos);
- btn->setStyleSheet("background-color: gray;");
- }
- }
- else if (dragButton == Qt::RightButton) {
- // 强制取消
- selectedSet.remove(pos);
- btn->setStyleSheet("background-color: lightgreen;");
- }
- }
- }
- isDragging = false;
- dragButton = Qt::NoButton;
- }
|