JCustomLabelControls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // *****************************************************************************
  2. // 版权所有(C)2023~2099 上海骄成超声波技术有限公司
  3. // 保留所有权利
  4. // *****************************************************************************
  5. // 作者 : 杨坚
  6. // 版本 : 1.0
  7. // 功能说明:
  8. // 自定义lab
  9. // *****************************************************************************
  10. #ifndef __CUSTOMLABELCONTROLS_H__
  11. #define __CUSTOMLABELCONTROLS_H__ 1
  12. #include "TreeViewManagerHead.h"
  13. #include "Src/common/JLogAllOutput.h"
  14. #include <QMouseEvent>
  15. class IoStateLab : public QLabel {
  16. Q_OBJECT
  17. public:
  18. IoStateLab(QWidget* parent = nullptr)
  19. : QLabel(parent)
  20. {
  21. setFixedSize(30, 30);
  22. }
  23. public:
  24. /**设置io 轴名称
  25. */
  26. void setIoName(const QString& strName)
  27. {
  28. m_strName = strName;
  29. UpDataIoStatus(true);
  30. }
  31. void UpDataIoStatus(bool isInit)
  32. {
  33. if (m_pCamera == nullptr)
  34. {
  35. m_pCamera = ns_module::CViewInterface::GetInstance();
  36. if (m_pCamera == nullptr)
  37. {
  38. // 为了调试准备的
  39. JLogAllOutput::cmd_error("io 轴设置失败");
  40. return;
  41. }
  42. }
  43. if (isInit)
  44. {
  45. // 信号量
  46. ns_db::DIGIT_IO_LEVEL nOutput;
  47. m_pCamera->GetViewMotion()->GetIoStatus(m_strName.toStdString(), nOutput);
  48. m_bClicked = nOutput;
  49. }
  50. else
  51. {
  52. m_pCamera->GetViewMotion()->SetIoStatus(m_strName.toStdString(), m_bClicked);
  53. }
  54. }
  55. protected:
  56. void paintEvent(QPaintEvent* event) override
  57. {
  58. QLabel::paintEvent(event);
  59. QPainter painter(this);
  60. painter.setRenderHint(QPainter::Antialiasing);
  61. int radius = qMin(width(), height()) / 2 - 5;
  62. int x = (width() - 2 * radius) / 2;
  63. int y = (height() - 2 * radius) / 2;
  64. painter.setPen(Qt::NoPen);
  65. painter.setBrush(m_bClicked ? Qt::green : Qt::red);
  66. painter.drawEllipse(x, y, 2 * radius, 2 * radius);
  67. }
  68. void mousePressEvent(QMouseEvent* event) override
  69. {
  70. if (event->button() == Qt::LeftButton)
  71. {
  72. m_bClicked = !m_bClicked;
  73. UpDataIoStatus(false);
  74. update();
  75. }
  76. QLabel::mousePressEvent(event);
  77. }
  78. private:
  79. bool m_bClicked; // 当前状态
  80. QString m_strName;
  81. ns_module::CViewInterface* m_pCamera = nullptr;
  82. };
  83. // 自适应控件
  84. class AutoResizeLabel : public QLabel
  85. {
  86. public:
  87. explicit AutoResizeLabel(QWidget* parent = nullptr) : QLabel(parent)
  88. {
  89. setWordWrap(true);
  90. }
  91. protected:
  92. void resizeEvent(QResizeEvent* event) override
  93. {
  94. adjustFontSize();
  95. QLabel::resizeEvent(event);
  96. }
  97. private:
  98. void adjustFontSize()
  99. {
  100. int labelWidth = width();
  101. int labelHeight = height();
  102. QString text = this->text();
  103. if (text.isEmpty())
  104. {
  105. return;
  106. }
  107. QFont font = this->font();
  108. int fontSize = font.pointSize();
  109. // 简单算法:逐步减小字体,直到文字适合标签
  110. while (fontSize > 1)
  111. {
  112. font.setPointSize(fontSize);
  113. this->setFont(font);
  114. // 测量文本的像素大小
  115. QFontMetrics fm(font);
  116. QRect rect = fm.boundingRect(this->rect(), Qt::TextWordWrap, text);
  117. if (rect.width() <= labelWidth && rect.height() <= labelHeight)
  118. {
  119. break;
  120. }
  121. fontSize--;
  122. }
  123. // 设置调整后的字体
  124. font.setPointSize(fontSize);
  125. this->setFont(font);
  126. }
  127. };
  128. // 定义一个结构体来表示你的数据项
  129. struct ST_DATA_ITEM
  130. {
  131. QString key;
  132. QString value; // "1" 表示选中, "2" 表示未选中
  133. };
  134. class JCheckBoxCont : public QWidget {
  135. Q_OBJECT
  136. public:
  137. JCheckBoxCont(QList<ST_DATA_ITEM>& data, QWidget* parent = nullptr) :
  138. QWidget(parent),
  139. m_data(data)
  140. {
  141. QHBoxLayout* layout = new QHBoxLayout(this);
  142. for (ST_DATA_ITEM& item : m_data)
  143. {
  144. // 创建 QCheckBox
  145. QCheckBox* checkBox = new QCheckBox(item.key, this);
  146. // 设置状态
  147. checkBox->setChecked(item.value == "1");
  148. // 连接信号和槽,checkbox的状态发生改变时调用指定函数
  149. connect(checkBox, &QCheckBox::stateChanged, this, &JCheckBoxCont::onStateChanged);
  150. // 存储 QCheckBox 指针,方便后续访问
  151. m_checkboxMap[item.key] = checkBox;
  152. // 添加到布局
  153. layout->addWidget(checkBox);
  154. }
  155. setLayout(layout);
  156. }
  157. private slots:
  158. void onStateChanged(int state)
  159. {
  160. QCheckBox* checkBox = qobject_cast<QCheckBox*>(sender());
  161. if (!checkBox)
  162. {
  163. return;
  164. }
  165. QString name = checkBox->text();
  166. //// 更新数据
  167. //for (ST_DATA_ITEM& item : m_data)
  168. //{
  169. // if (item.key == name)
  170. // {
  171. // item.value = checkBox->isChecked() ? "1" : "2";
  172. // //qDebug() << name << " 状态变为: " << item.value;
  173. // break;
  174. // }
  175. //}
  176. }
  177. private:
  178. QList<ST_DATA_ITEM>& m_data; // 数据的引用
  179. QMap<QString, QCheckBox*> m_checkboxMap; // 保存checkbox的指针,以便更新
  180. };
  181. #endif //__CUSTOMLABELCONTROLS_H__