switchbutton.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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(200)
  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(_background);
  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("Оригинал");
  43. _labelon->setText("Перевод");
  44. _labeloff->move(31, 3);
  45. _labelon->move(20, 2);
  46. setFixedSize(QSize(100, 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. _circle->deleteLater();
  80. _background->deleteLater();
  81. _labeloff->deleteLater();
  82. _labelon->deleteLater();
  83. __btn_move->deleteLater();
  84. __back_move->deleteLater();
  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. // Assigning new current value
  124. _value = !_value;
  125. _update();
  126. emit valueChanged(_value);
  127. }
  128. void SwitchButton::setEnabled(bool flag)
  129. {
  130. _enabled = flag;
  131. _circle->setEnabled(flag);
  132. _background->setEnabled(flag);
  133. if (flag)
  134. _labelon->show();
  135. else
  136. {
  137. if (value())
  138. _labelon->show();
  139. else
  140. _labelon->hide();
  141. }
  142. QWidget::setEnabled(flag);
  143. }
  144. void SwitchButton::setDuration(int time)
  145. {
  146. _duration = time;
  147. }
  148. void SwitchButton::setValue(bool flag)
  149. {
  150. if (flag == value()) {
  151. return;
  152. }
  153. _value = flag;
  154. _update();
  155. repaint();
  156. }
  157. bool SwitchButton::value() const
  158. {
  159. return _value;
  160. }
  161. void SwitchButton::_update()
  162. {
  163. if (!_enabled) {
  164. return;
  165. }
  166. __btn_move->stop();
  167. __back_move->stop();
  168. __btn_move->setDuration(_duration);
  169. __back_move->setDuration(_duration);
  170. int hback = 20;
  171. QSize initial_size(hback, hback);
  172. QSize final_size(width() - 4, hback);
  173. int xi = 2;
  174. int y = 2;
  175. int xf = width() - 22;
  176. if (!_value)
  177. {
  178. final_size = QSize(hback, hback);
  179. initial_size = QSize(width() - 4, hback);
  180. xi = xf;
  181. xf = 2;
  182. }
  183. __btn_move->setStartValue(QPoint(xi, y));
  184. __btn_move->setEndValue(QPoint(xf, y));
  185. __back_move->setStartValue(initial_size);
  186. __back_move->setEndValue(final_size);
  187. __btn_move->start();
  188. __back_move->start();
  189. }
  190. SwitchButton::SwitchBackground::SwitchBackground(QWidget* parent, QColor color, bool rect)
  191. : QWidget(parent)
  192. , _rect(rect)
  193. , _borderradius(12)
  194. , _color(color)
  195. , _pencolor(QColor(170, 170, 170))
  196. {
  197. setFixedHeight(20);
  198. _lg = QLinearGradient(0, 25, 70, 0);
  199. _lg.setColorAt(0, QColor(0, 170, 0));
  200. _lg.setColorAt(0.25, QColor(0, 194, 0));
  201. _lg.setColorAt(0.95, QColor(0, 170, 50));
  202. _lg_disabled = QLinearGradient(0, 25, 70, 0);
  203. _lg_disabled.setColorAt(0, QColor(190, 190, 190));
  204. _lg_disabled.setColorAt(0.25, QColor(230, 230, 230));
  205. _lg_disabled.setColorAt(0.95, QColor(190, 190, 190));
  206. if (_rect)
  207. _borderradius = 0;
  208. _enabled = true;
  209. }
  210. SwitchButton::SwitchBackground::~SwitchBackground()
  211. {
  212. }
  213. void SwitchButton::SwitchBackground::paintEvent(QPaintEvent*)
  214. {
  215. QPainter* painter = new QPainter;
  216. painter->begin(this);
  217. painter->setRenderHint(QPainter::Antialiasing, true);
  218. QPen pen(Qt::NoPen);
  219. painter->setPen(pen);
  220. if (_enabled)
  221. {
  222. painter->setBrush(QColor(154, 190, 50));
  223. painter->drawRoundedRect(0, 0
  224. , width(), height()
  225. , 10, 10);
  226. painter->setBrush(_lg);
  227. painter->drawRoundedRect(1, 1, width()-2, height()-2, 8, 8);
  228. }
  229. else
  230. {
  231. painter->setBrush(QColor(150, 150, 150));
  232. painter->drawRoundedRect(0, 0
  233. , width(), height()
  234. , 10, 10);
  235. painter->setBrush(_lg_disabled);
  236. painter->drawRoundedRect(1, 1, width() - 2, height() - 2, 8, 8);
  237. }
  238. painter->end();
  239. }
  240. void SwitchButton::SwitchBackground::setEnabled(bool flag)
  241. {
  242. _enabled = flag;
  243. }
  244. SwitchButton::SwitchCircle::SwitchCircle(QWidget* parent, QColor color, bool rect)
  245. : QWidget(parent)
  246. , _rect(rect)
  247. , _borderradius(12)
  248. , _color(color)
  249. , _pencolor(QColor(120, 120, 120))
  250. {
  251. setFixedSize(20, 20);
  252. _rg = QRadialGradient(static_cast<int>(width() / 2), static_cast<int>(height() / 2), 12);
  253. _rg.setColorAt(0, QColor(255, 255, 255));
  254. _rg.setColorAt(0.6, QColor(255, 255, 255));
  255. _rg.setColorAt(1, QColor(205, 205, 205));
  256. _lg = QLinearGradient(3, 18, 20, 4);
  257. _lg.setColorAt(0, QColor(255, 255, 255));
  258. _lg.setColorAt(0.55, QColor(230, 230, 230));
  259. _lg.setColorAt(0.72, QColor(255, 255, 255));
  260. _lg.setColorAt(1, QColor(255, 255, 255));
  261. _lg_disabled = QLinearGradient(3, 18, 20, 4);
  262. _lg_disabled.setColorAt(0, QColor(230, 230, 230));
  263. _lg_disabled.setColorAt(0.55, QColor(210, 210, 210));
  264. _lg_disabled.setColorAt(0.72, QColor(230, 230, 230));
  265. _lg_disabled.setColorAt(1, QColor(230, 230, 230));
  266. _enabled = true;
  267. }
  268. SwitchButton::SwitchCircle::~SwitchCircle()
  269. {
  270. }
  271. void SwitchButton::SwitchCircle::paintEvent(QPaintEvent*)
  272. {
  273. QPainter* painter = new QPainter;
  274. painter->begin(this);
  275. painter->setRenderHint(QPainter::Antialiasing, true);
  276. QPen pen(Qt::NoPen);
  277. painter->setPen(pen);
  278. painter->setBrush(_pencolor);
  279. painter->drawEllipse(0, 0, 20, 20);
  280. painter->setBrush(_rg);
  281. painter->drawEllipse(1, 1, 18, 18);
  282. painter->setBrush(QColor(210, 210, 210));
  283. painter->drawEllipse(2, 2, 16, 16);
  284. if (_enabled)
  285. {
  286. painter->setBrush(_lg);
  287. painter->drawEllipse(3, 3, 14, 14);
  288. }
  289. else
  290. {
  291. painter->setBrush(_lg_disabled);
  292. painter->drawEllipse(3, 3, 14, 14);
  293. }
  294. painter->end();
  295. }
  296. void SwitchButton::SwitchCircle::setEnabled(bool flag)
  297. {
  298. _enabled = flag;
  299. }