JCustomLabelControls.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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(), (ns_db::DIGIT_IO_LEVEL)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. Q_OBJECT
  87. public:
  88. explicit AutoResizeLabel(QWidget* parent = nullptr) : QLabel(parent)
  89. {
  90. setWordWrap(false);
  91. }
  92. void setText(const QString& text)
  93. {
  94. QLabel::setText(text);
  95. emit textChanged(text);
  96. }
  97. signals:
  98. void textChanged(const QString& text);
  99. protected:
  100. void resizeEvent(QResizeEvent* event) override
  101. {
  102. adjustFontSize();
  103. QLabel::resizeEvent(event);
  104. }
  105. private:
  106. void adjustFontSize()
  107. {
  108. int labelWidth = width();
  109. int labelHeight = height();
  110. QString text = this->text();
  111. if (text.isEmpty())
  112. {
  113. return;
  114. }
  115. QFont font = this->font();
  116. int fontSize = font.pointSize();
  117. // 简单算法:逐步减小字体,直到文字适合标签
  118. while (fontSize > 1)
  119. {
  120. font.setPointSize(fontSize);
  121. this->setFont(font);
  122. // 测量文本的像素大小
  123. QFontMetrics fm(font);
  124. QRect rect = fm.boundingRect(this->rect(), Qt::TextWordWrap, text);
  125. if (rect.width() <= labelWidth && rect.height() <= labelHeight)
  126. {
  127. break;
  128. }
  129. fontSize--;
  130. }
  131. // 设置调整后的字体
  132. font.setPointSize(fontSize);
  133. this->setFont(font);
  134. }
  135. };
  136. // 定义一个结构体来表示你的数据项
  137. struct ST_DATA_ITEM
  138. {
  139. QString key;
  140. QString value; // "1" 表示选中, "2" 表示未选中
  141. };
  142. class JCheckBoxCont : public QWidget {
  143. Q_OBJECT
  144. public:
  145. JCheckBoxCont(QList<ST_DATA_ITEM>& data, QWidget* parent = nullptr) :
  146. QWidget(parent),
  147. m_data(data)
  148. {
  149. QHBoxLayout* layout = new QHBoxLayout(this);
  150. for (ST_DATA_ITEM& item : m_data)
  151. {
  152. // 创建 QCheckBox
  153. QCheckBox* checkBox = new QCheckBox(item.key, this);
  154. // 设置状态
  155. checkBox->setChecked(item.value == "1");
  156. // 连接信号和槽,checkbox的状态发生改变时调用指定函数
  157. connect(checkBox, &QCheckBox::stateChanged, this, &JCheckBoxCont::onStateChanged);
  158. // 存储 QCheckBox 指针,方便后续访问
  159. m_checkboxMap[item.key] = checkBox;
  160. // 添加到布局
  161. layout->addWidget(checkBox);
  162. }
  163. setLayout(layout);
  164. }
  165. private slots:
  166. void onStateChanged(int state)
  167. {
  168. QCheckBox* checkBox = qobject_cast<QCheckBox*>(sender());
  169. if (!checkBox)
  170. {
  171. return;
  172. }
  173. QString name = checkBox->text();
  174. //// 更新数据
  175. //for (ST_DATA_ITEM& item : m_data)
  176. //{
  177. // if (item.key == name)
  178. // {
  179. // item.value = checkBox->isChecked() ? "1" : "2";
  180. // //qDebug() << name << " 状态变为: " << item.value;
  181. // break;
  182. // }
  183. //}
  184. }
  185. private:
  186. QList<ST_DATA_ITEM>& m_data; // 数据的引用
  187. QMap<QString, QCheckBox*> m_checkboxMap; // 保存checkbox的指针,以便更新
  188. };
  189. class YourClass : public QWidget
  190. {
  191. private:
  192. QLineEdit* lineEdit;
  193. QString lastValidText; // 保存上一次的有效值
  194. public:
  195. void setupLineEdit()
  196. {
  197. lineEdit = new QLineEdit(this);
  198. QIntValidator* validator = new QIntValidator(0, 3, this);
  199. lineEdit->setValidator(validator);
  200. // 保存初始值
  201. lastValidText = "0";
  202. // 连接信号
  203. connect(lineEdit, &QLineEdit::textChanged, this, &YourClass::onTextChanged);
  204. }
  205. private:
  206. void onTextChanged(const QString& text)
  207. {
  208. bool ok;
  209. int value = text.toInt(&ok);
  210. if (ok && value >= 0 && value <= 3)
  211. {
  212. lastValidText = text; // 保存有效值
  213. }
  214. else
  215. {
  216. // 恢复上一次的有效值
  217. lineEdit->setText(lastValidText);
  218. // 将光标移到末尾
  219. lineEdit->setCursorPosition(lineEdit->text().length());
  220. }
  221. }
  222. };
  223. #endif //__CUSTOMLABELCONTROLS_H__