makeCell

make cell form

头文件:

#include <QLineEdit>          
#include <QCheckBox>          
#include <QRegExpValidator>   
#include <QPushButton>        
#include <QDoubleSpinBox>     
#include <QRadioButton>       
#include <QFrame>             
#include <QWidget>            
#include <QDialog>
#include <QVBoxLayout>

class makeCellForm : public QDialog
{
  Q_OBJECT
  ;
 public:
  makeCellForm();
  ~makeCellForm();
  void                init();
  QString             getName() { return m_nameLE->text(); }
  bool                isKeep() { return m_keepCB->isChecked(); }

 private slots:
  void                updateUI();

 private:
  QPixmap             createDotPixmap(Qt::Alignment align);
  void                initRow1();
  void                initRow2();
  void                initRow3();
  void                initRow4();
  void                initRow5();

 private:
  QLineEdit*          m_nameLE;
  QCheckBox*          m_keepCB;
  QRegExpValidator*   m_validatorReg;
  QPushButton*        m_pushBtn;
  QDoubleSpinBox*     m_xSB;
  QDoubleSpinBox*     m_ySB;
  QRadioButton*       m_radio1;
  QRadioButton*       m_radio2;
  QCheckBox*          m_checkBoxs[9];
  QFrame*             m_gridFrame;
  QWidget*            m_xyWidget;
  QVBoxLayout *       mainLayout;

};
#include "makeCellForm.h"
#include <QLabel>
#include <QButtonGroup>


makeCellForm::makeCellForm() {
  mainLayout = new QVBoxLayout(this);
  mainLayout->setContentsMargins(0, 0, 0, 0);
  initRow1();
  initRow2();
  initRow3();
  initRow4();
  initRow5();

  // init row6
  m_keepCB = new QCheckBox("Keep Object");
  mainLayout->addWidget(m_keepCB);
  mainLayout->setSpacing(0);

  connect(m_radio1, SIGNAL(toggled(bool)), this, SLOT(updateUI()));
  connect(m_radio2, SIGNAL(toggled(bool)), this, SLOT(updateUI()));

  updateUI();
}

void
makeCellForm::updateUI() {
  bool aSelected = m_radio1->isChecked();
  bool bSelected = m_radio2->isChecked();

  m_gridFrame->setEnabled(!bSelected);
  m_xyWidget->setEnabled(!aSelected);
}

makeCellForm::~makeCellForm()
{
  delete m_validatorReg;
}

void makeCellForm::init()
{
  double step = 0.001;
  m_xSB->setSingleStep(step);
  m_ySB->setSingleStep(step);
  m_xSB->setValue(0);
  m_ySB->setValue(0);
  m_checkBox[6]->setChecked(true);
}

void makeCellForm::initRow1() {
  QHBoxLayout *nameLayout = new QHBoxLayout;
  QLabel *nameLabel = new QLabel("Cell Name:");
  m_validatorReg = new QRegExpValidator(QRegExp("[\\w\\d_\\?\\$]+"), NULL);
  m_nameLE = new QLineEdit;
  m_nameLE->setValidator(m_validatorReg);
  nameLayout->addWidget(nameLabel);
  nameLayout->addWidget(m_nameLE);
  mainLayout->addLayout(nameLayout);
}

void makeCellForm::initRow2() {
  m_radio2 = new QRadioButton("Set origin:");
  m_radio2->setToolTip("Set coordinate of the current cell to the origin");
  m_radio2->setChecked(true);
  mainLayout->addWidget(m_radio2);
}

void makeCellForm::initRow3() {
  QHBoxLayout *xyLayout = new QHBoxLayout;
  xyLayout->setContentsMargins(0, 0, 0, 0);
  xyLayout->setSpacing(1);
  QLabel *xLabel = new QLabel("X:");
  m_xSB = new QDoubleSpinBox;
  m_xSB->setMinimum(-std::numeric_limits<double>::max());
  QLabel *yLabel = new QLabel("Y:");
  m_ySB = new QDoubleSpinBox;
  m_ySB->setMinimum(-std::numeric_limits<double>::max());
  m_pushBtn = new QPushButton("By Cursor");
  xyLayout->addWidget(xLabel);
  xyLayout->addWidget(m_xSB);
  xyLayout->addWidget(yLabel);
  xyLayout->addWidget(m_ySB);
  xyLayout->addWidget(m_pushBtn);
  m_xyWidget = new QWidget();
  m_xyWidget->setLayout(xyLayout);
  m_xyWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

  mainLayout->addWidget(m_xyWidget);
}

void makeCellForm::initRow4() {
  m_radio1 = new QRadioButton("Align:");
  m_radio1->setToolTip("Set a position in the cell box as the origin");
  mainLayout->addWidget(m_radio1);

  // Group A and B are mutually exclusive
  QButtonGroup *exclusiveGroup = new QButtonGroup(this);
  exclusiveGroup->addButton(m_radio1);
  exclusiveGroup->addButton(m_radio2);
  exclusiveGroup->setExclusive(true);
}

void makeCellForm::initRow5() {
  m_gridFrame = new QFrame(this);
  m_gridFrame->setFrameShape(QFrame::Box);
  m_gridFrame->setLineWidth(1);
  m_gridFrame->setStyleSheet(
    "QFrame {"
    "   border: 1px solid grey;"
    "}"
  );

  QGridLayout *gridLayout = new QGridLayout(m_gridFrame);
  gridLayout->setSpacing(0);
  gridLayout->setHorizontalSpacing(0);  // 显式设置水平间距
  gridLayout->setVerticalSpacing(0);    // 显式设置垂直间距
  gridLayout->setContentsMargins(1, 1, 1, 1);
  gridLayout->setSizeConstraint(QLayout::SetFixedSize);

  QButtonGroup *buttonGroup = new QButtonGroup(this);
  buttonGroup->setExclusive(true);

  for (int i = 0; i < 9; ++i) {
    m_checkBoxs[i] = new QCheckBox;
    m_checkBoxs[i]->setCheckable(true);
    m_checkBoxs[i]->setContentsMargins(0, 0, 0, 0);
    m_checkBoxs[i]->setFixedSize(15, 15);
    m_checkBoxs[i]->setStyleSheet(
        "QCheckBox { margin: 0px; padding: 0px; border: none; }"
        "QCheckBox::indicator { margin: 0px; padding: 0px; width: 15px; height: 15px; }"
    );
    buttonGroup->addButton(m_checkBoxs[i], i);
    gridLayout->addWidget(m_checkBoxs[i], i / 3, i % 3);
  }
  m_checkBoxs[6]->setChecked(true); // Default: bottom-left
  mainLayout->addWidget(m_gridFrame);
}
posted @ 2025-05-14 17:52  卑以自牧lq  阅读(2)  评论(0)    收藏  举报