123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- // *****************************************************************************
- // 版权所有(C)2023~2099 上海骄成超声波技术有限公司
- // 保留所有权利
- // *****************************************************************************
- // 作者 : 杨坚
- // 版本 : 1.0
- // 功能说明:
- // 自定义lab
- // *****************************************************************************
- #ifndef __CUSTOMLABELCONTROLS_H__
- #define __CUSTOMLABELCONTROLS_H__ 1
- #include "TreeViewManagerHead.h"
- #include "Src/common/JLogAllOutput.h"
- #include <QMouseEvent>
- class IoStateLab : public QLabel {
- Q_OBJECT
- public:
- IoStateLab(QWidget* parent = nullptr)
- : QLabel(parent)
- {
- setFixedSize(30, 30);
- }
- public:
- /**设置io 轴名称
- */
- void setIoName(const QString& strName)
- {
- m_strName = strName;
- UpDataIoStatus(true);
- }
- void UpDataIoStatus(bool isInit)
- {
- if (m_pCamera == nullptr)
- {
- m_pCamera = ns_module::CViewInterface::GetInstance();
- if (m_pCamera == nullptr)
- {
- // 为了调试准备的
- JLogAllOutput::cmd_error("io 轴设置失败");
- return;
- }
- }
- if (isInit)
- {
- // 信号量
- ns_db::DIGIT_IO_LEVEL nOutput;
- m_pCamera->GetViewMotion()->GetIoStatus(m_strName.toStdString(), nOutput);
- m_bClicked = nOutput;
- }
- else
- {
- m_pCamera->GetViewMotion()->SetIoStatus(m_strName.toStdString(), m_bClicked);
- }
- }
- protected:
- void paintEvent(QPaintEvent* event) override
- {
- QLabel::paintEvent(event);
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
- int radius = qMin(width(), height()) / 2 - 5;
- int x = (width() - 2 * radius) / 2;
- int y = (height() - 2 * radius) / 2;
- painter.setPen(Qt::NoPen);
- painter.setBrush(m_bClicked ? Qt::green : Qt::red);
- painter.drawEllipse(x, y, 2 * radius, 2 * radius);
- }
- void mousePressEvent(QMouseEvent* event) override
- {
- if (event->button() == Qt::LeftButton)
- {
- m_bClicked = !m_bClicked;
- UpDataIoStatus(false);
- update();
- }
- QLabel::mousePressEvent(event);
- }
- private:
- bool m_bClicked; // 当前状态
- QString m_strName;
- ns_module::CViewInterface* m_pCamera = nullptr;
- };
- // 自适应控件
- class AutoResizeLabel : public QLabel
- {
- public:
- explicit AutoResizeLabel(QWidget* parent = nullptr) : QLabel(parent)
- {
- setWordWrap(true);
- }
- protected:
- void resizeEvent(QResizeEvent* event) override
- {
- adjustFontSize();
- QLabel::resizeEvent(event);
- }
- private:
- void adjustFontSize()
- {
- int labelWidth = width();
- int labelHeight = height();
- QString text = this->text();
- if (text.isEmpty())
- {
- return;
- }
- QFont font = this->font();
- int fontSize = font.pointSize();
- // 简单算法:逐步减小字体,直到文字适合标签
- while (fontSize > 1)
- {
- font.setPointSize(fontSize);
- this->setFont(font);
- // 测量文本的像素大小
- QFontMetrics fm(font);
- QRect rect = fm.boundingRect(this->rect(), Qt::TextWordWrap, text);
- if (rect.width() <= labelWidth && rect.height() <= labelHeight)
- {
- break;
- }
- fontSize--;
- }
- // 设置调整后的字体
- font.setPointSize(fontSize);
- this->setFont(font);
- }
- };
- // 定义一个结构体来表示你的数据项
- struct ST_DATA_ITEM
- {
- QString key;
- QString value; // "1" 表示选中, "2" 表示未选中
- };
- class JCheckBoxCont : public QWidget {
- Q_OBJECT
- public:
- JCheckBoxCont(QList<ST_DATA_ITEM>& data, QWidget* parent = nullptr) :
- QWidget(parent),
- m_data(data)
- {
- QHBoxLayout* layout = new QHBoxLayout(this);
- for (ST_DATA_ITEM& item : m_data)
- {
- // 创建 QCheckBox
- QCheckBox* checkBox = new QCheckBox(item.key, this);
- // 设置状态
- checkBox->setChecked(item.value == "1");
- // 连接信号和槽,checkbox的状态发生改变时调用指定函数
- connect(checkBox, &QCheckBox::stateChanged, this, &JCheckBoxCont::onStateChanged);
- // 存储 QCheckBox 指针,方便后续访问
- m_checkboxMap[item.key] = checkBox;
- // 添加到布局
- layout->addWidget(checkBox);
- }
- setLayout(layout);
- }
- private slots:
- void onStateChanged(int state)
- {
- QCheckBox* checkBox = qobject_cast<QCheckBox*>(sender());
- if (!checkBox)
- {
- return;
- }
- QString name = checkBox->text();
- //// 更新数据
- //for (ST_DATA_ITEM& item : m_data)
- //{
- // if (item.key == name)
- // {
- // item.value = checkBox->isChecked() ? "1" : "2";
- // //qDebug() << name << " 状态变为: " << item.value;
- // break;
- // }
- //}
- }
- private:
- QList<ST_DATA_ITEM>& m_data; // 数据的引用
- QMap<QString, QCheckBox*> m_checkboxMap; // 保存checkbox的指针,以便更新
- };
- #endif //__CUSTOMLABELCONTROLS_H__
|