switchbutton.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "switchbutton.h"
  2. #include <QLabel>
  3. #include <QPainter>
  4. SwitchButton::SwitchButton(QWidget* parent, Style style)
  5. : QWidget(parent)
  6. , _value(false)
  7. , _duration(100)
  8. , _enabled(true)
  9. {
  10. _pencolor = QColor(120, 120, 120);
  11. _lg = QLinearGradient(35, 30, 35, 0);
  12. _lg.setColorAt(0, QColor(210, 210, 210));
  13. _lg.setColorAt(0.25, QColor(255, 255, 255));
  14. _lg.setColorAt(0.82, QColor(255, 255, 255));
  15. _lg.setColorAt(1, QColor(210, 210, 210));
  16. _lg2 = QLinearGradient(50, 30, 35, 0);
  17. _lg2.setColorAt(0, QColor(230, 230, 230));
  18. _lg2.setColorAt(0.25, QColor(255, 255, 255));
  19. _lg2.setColorAt(0.82, QColor(255, 255, 255));
  20. _lg2.setColorAt(1, QColor(230, 230, 230));
  21. _lg_disabled = QLinearGradient(50, 30, 35, 0);
  22. _lg_disabled.setColorAt(0, QColor(200, 200, 200));
  23. _lg_disabled.setColorAt(0.25, QColor(230, 230, 230));
  24. _lg_disabled.setColorAt(0.82, QColor(230, 230, 230));
  25. _lg_disabled.setColorAt(1, QColor(200, 200, 200));
  26. _offcolor = QColor(255, 255, 255);
  27. _oncolor = QColor(154, 205, 50);
  28. _tol = 0;
  29. _borderradius = 12;
  30. _labeloff = new QLabel(this);
  31. _background = new SwitchBackground(this, _oncolor);
  32. _labelon = new QLabel(this);
  33. _circle = new SwitchCircle(this, _offcolor);
  34. __btn_move = new QPropertyAnimation(this);
  35. __back_move = new QPropertyAnimation(this);
  36. __btn_move->setTargetObject(_circle);
  37. __btn_move->setPropertyName("pos");
  38. __back_move->setTargetObject(_background);
  39. __back_move->setPropertyName("size");
  40. setWindowFlags(Qt::FramelessWindowHint);
  41. setAttribute(Qt::WA_TranslucentBackground);
  42. _labeloff->setText("Off");
  43. _labelon->setText("On");
  44. _labeloff->move(31, 5);
  45. _labelon->move(15, 5);
  46. setFixedSize(QSize(60, 24));
  47. if (style == Style::YESNO)
  48. {
  49. _labeloff->setText("No");
  50. _labelon->setText("Yes");
  51. _labeloff->move(33, 5);
  52. _labelon->move(12, 5);
  53. setFixedSize(QSize(60, 24));
  54. }
  55. else if (style == Style::BOOL)
  56. {
  57. _labeloff->setText("False");
  58. _labelon->setText("True");
  59. _labeloff->move(37, 5);
  60. _labelon->move(12, 5);
  61. setFixedSize(QSize(75, 24));
  62. }
  63. if (style == Style::EMPTY)
  64. {
  65. _labeloff->setText("");
  66. _labelon->setText("");
  67. _labeloff->move(31, 5);
  68. _labelon->move(12, 5);
  69. setFixedSize(QSize(45, 24));
  70. }
  71. _labeloff->setStyleSheet("color: rgb(120, 120, 120); font-weight: bold;");
  72. _labelon->setStyleSheet("color: rgb(255, 255, 255); font-weight: bold;");
  73. _background->resize(20, 20);
  74. _background->move(2, 2);
  75. _circle->move(2, 2);
  76. }
  77. SwitchButton::~SwitchButton()
  78. {
  79. delete _circle;
  80. delete _background;
  81. delete _labeloff;
  82. delete _labelon;
  83. delete __btn_move;
  84. delete __back_move;
  85. }
  86. void SwitchButton::paintEvent(QPaintEvent*)
  87. {
  88. QPainter* painter = new QPainter;
  89. painter->begin(this);
  90. painter->setRenderHint(QPainter::Antialiasing, true);
  91. QPen pen(Qt::NoPen);
  92. painter->setPen(pen);
  93. painter->setBrush(_pencolor);
  94. painter->drawRoundedRect(0, 0
  95. , width(), height()
  96. , 12, 12);
  97. painter->setBrush(_lg);
  98. painter->drawRoundedRect(1, 1
  99. , width() - 2, height() - 2
  100. , 10, 10);
  101. painter->setBrush(QColor(210, 210, 210));
  102. painter->drawRoundedRect(2, 2
  103. , width() - 4, height() - 4
  104. , 10, 10);
  105. if (_enabled)
  106. {
  107. painter->setBrush(_lg2);
  108. painter->drawRoundedRect(3, 3
  109. , width() - 6, height() - 6
  110. , 7, 7);
  111. }
  112. else
  113. {
  114. painter->setBrush(_lg_disabled);
  115. painter->drawRoundedRect(3, 3
  116. , width() - 6, height() - 6
  117. , 7, 7);
  118. }
  119. painter->end();
  120. }
  121. void SwitchButton::mousePressEvent(QMouseEvent*)
  122. {
  123. if (!_enabled)
  124. return;
  125. __btn_move->stop();
  126. __back_move->stop();
  127. __btn_move->setDuration(_duration);
  128. __back_move->setDuration(_duration);
  129. int hback = 20;
  130. QSize initial_size(hback, hback);
  131. QSize final_size(width() - 4, hback);
  132. int xi = 2;
  133. int y = 2;
  134. int xf = width() - 22;
  135. if (_value)
  136. {
  137. final_size = QSize(hback, hback);
  138. initial_size = QSize(width() - 4, hback);
  139. xi = xf;
  140. xf = 2;
  141. }
  142. __btn_move->setStartValue(QPoint(xi, y));
  143. __btn_move->setEndValue(QPoint(xf, y));
  144. __back_move->setStartValue(initial_size);
  145. __back_move->setEndValue(final_size);
  146. __btn_move->start();
  147. __back_move->start();
  148. // Assigning new current value
  149. _value = !_value;
  150. emit valueChanged(_value);
  151. }
  152. void SwitchButton::setEnabled(bool flag)
  153. {
  154. _enabled = flag;
  155. _circle->setEnabled(flag);
  156. _background->setEnabled(flag);
  157. if (flag)
  158. _labelon->show();
  159. else
  160. {
  161. if (value())
  162. _labelon->show();
  163. else
  164. _labelon->hide();
  165. }
  166. QWidget::setEnabled(flag);
  167. }
  168. void SwitchButton::setDuration(int time)
  169. {
  170. _duration = time;
  171. }
  172. void SwitchButton::setValue(bool flag)
  173. {
  174. if (flag == value())
  175. return;
  176. else
  177. {
  178. _value = flag;
  179. _update();
  180. setEnabled(_enabled);
  181. }
  182. }
  183. bool SwitchButton::value() const
  184. {
  185. return _value;
  186. }
  187. void SwitchButton::_update()
  188. {
  189. int hback = 20;
  190. QSize final_size(width() - 4, hback);
  191. int y = 2;
  192. int xf = width() - 22;
  193. if (_value)
  194. {
  195. final_size = QSize(hback, hback);
  196. xf = 2;
  197. }
  198. _circle->move(QPoint(xf, y));
  199. _background->resize(final_size);
  200. }
  201. SwitchButton::SwitchBackground::SwitchBackground(QWidget* parent, QColor color, bool rect)
  202. : QWidget(parent)
  203. , _rect(rect)
  204. , _borderradius(12)
  205. , _color(color)
  206. , _pencolor(QColor(170, 170, 170))
  207. {
  208. setFixedHeight(20);
  209. _lg = QLinearGradient(0, 25, 70, 0);
  210. _lg.setColorAt(0, QColor(154, 194, 50));
  211. _lg.setColorAt(0.25, QColor(154, 210, 50));
  212. _lg.setColorAt(0.95, QColor(154, 194, 50));
  213. _lg_disabled = QLinearGradient(0, 25, 70, 0);
  214. _lg_disabled.setColorAt(0, QColor(190, 190, 190));
  215. _lg_disabled.setColorAt(0.25, QColor(230, 230, 230));
  216. _lg_disabled.setColorAt(0.95, QColor(190, 190, 190));
  217. if (_rect)
  218. _borderradius = 0;
  219. _enabled = true;
  220. }
  221. SwitchButton::SwitchBackground::~SwitchBackground()
  222. {
  223. }
  224. void SwitchButton::SwitchBackground::paintEvent(QPaintEvent*)
  225. {
  226. QPainter* painter = new QPainter;
  227. painter->begin(this);
  228. painter->setRenderHint(QPainter::Antialiasing, true);
  229. QPen pen(Qt::NoPen);
  230. painter->setPen(pen);
  231. if (_enabled)
  232. {
  233. painter->setBrush(QColor(154, 190, 50));
  234. painter->drawRoundedRect(0, 0
  235. , width(), height()
  236. , 10, 10);
  237. painter->setBrush(_lg);
  238. painter->drawRoundedRect(1, 1, width()-2, height()-2, 8, 8);
  239. }
  240. else
  241. {
  242. painter->setBrush(QColor(150, 150, 150));
  243. painter->drawRoundedRect(0, 0
  244. , width(), height()
  245. , 10, 10);
  246. painter->setBrush(_lg_disabled);
  247. painter->drawRoundedRect(1, 1, width() - 2, height() - 2, 8, 8);
  248. }
  249. painter->end();
  250. }
  251. void SwitchButton::SwitchBackground::setEnabled(bool flag)
  252. {
  253. _enabled = flag;
  254. }
  255. SwitchButton::SwitchCircle::SwitchCircle(QWidget* parent, QColor color, bool rect)
  256. : QWidget(parent)
  257. , _rect(rect)
  258. , _borderradius(12)
  259. , _color(color)
  260. , _pencolor(QColor(120, 120, 120))
  261. {
  262. setFixedSize(20, 20);
  263. _rg = QRadialGradient(static_cast<int>(width() / 2), static_cast<int>(height() / 2), 12);
  264. _rg.setColorAt(0, QColor(255, 255, 255));
  265. _rg.setColorAt(0.6, QColor(255, 255, 255));
  266. _rg.setColorAt(1, QColor(205, 205, 205));
  267. _lg = QLinearGradient(3, 18, 20, 4);
  268. _lg.setColorAt(0, QColor(255, 255, 255));
  269. _lg.setColorAt(0.55, QColor(230, 230, 230));
  270. _lg.setColorAt(0.72, QColor(255, 255, 255));
  271. _lg.setColorAt(1, QColor(255, 255, 255));
  272. _lg_disabled = QLinearGradient(3, 18, 20, 4);
  273. _lg_disabled.setColorAt(0, QColor(230, 230, 230));
  274. _lg_disabled.setColorAt(0.55, QColor(210, 210, 210));
  275. _lg_disabled.setColorAt(0.72, QColor(230, 230, 230));
  276. _lg_disabled.setColorAt(1, QColor(230, 230, 230));
  277. _enabled = true;
  278. }
  279. SwitchButton::SwitchCircle::~SwitchCircle()
  280. {
  281. }
  282. void SwitchButton::SwitchCircle::paintEvent(QPaintEvent*)
  283. {
  284. QPainter* painter = new QPainter;
  285. painter->begin(this);
  286. painter->setRenderHint(QPainter::Antialiasing, true);
  287. QPen pen(Qt::NoPen);
  288. painter->setPen(pen);
  289. painter->setBrush(_pencolor);
  290. painter->drawEllipse(0, 0, 20, 20);
  291. painter->setBrush(_rg);
  292. painter->drawEllipse(1, 1, 18, 18);
  293. painter->setBrush(QColor(210, 210, 210));
  294. painter->drawEllipse(2, 2, 16, 16);
  295. if (_enabled)
  296. {
  297. painter->setBrush(_lg);
  298. painter->drawEllipse(3, 3, 14, 14);
  299. }
  300. else
  301. {
  302. painter->setBrush(_lg_disabled);
  303. painter->drawEllipse(3, 3, 14, 14);
  304. }
  305. painter->end();
  306. }
  307. void SwitchButton::SwitchCircle::setEnabled(bool flag)
  308. {
  309. _enabled = flag;
  310. }