فهرست منبع

Added switch button

Ivan Arkhipov 3 سال پیش
والد
کامیت
54024d9852

+ 2 - 1
Legacy_v2.pro

@@ -14,7 +14,8 @@ TARGET = Legacy_v2
 TEMPLATE = subdirs
 
 SUBDIRS += \
-    src/Launcher \
     src/Legacy \ 
 #    src/Legacy-advanced \
     src/LegacyLauncher
+
+CONFIG += qt

+ 2 - 0
src/Legacy/Legacy.pro

@@ -29,6 +29,7 @@ SOURCES += \
     widgets/newslistwidget.cpp \
     widgets/newspiece.cpp \
     widgets/serverstatuswidget.cpp \
+    widgets/switchbutton.cpp \
     widgets/chooseversiondialog.cpp \
     widgets/weeklycodewidget.cpp \
     widgets/dialogwindow.cpp \
@@ -54,6 +55,7 @@ HEADERS += \
     widgets/newslistwidget.h \
     widgets/newspiece.h \
     widgets/serverstatuswidget.h \
+    widgets/switchbutton.h \
     widgets/chooseversiondialog.h \
     widgets/weeklycodewidget.h \
     widgets/dialogwindow.h \

+ 2 - 1
src/Legacy/widgets/mainwindow.cpp

@@ -416,7 +416,8 @@ void MainWindow::setEventFilterRecursive(QObject *widget)
     QStringList classes_to_set = {
         "QPushButton",
         "QCheckBox",
-        "QComboBox"
+        "QComboBox",
+        "QLabel"
     };
 
     if (classes_to_set.contains(widget->metaObject()->className())) {

+ 18 - 3
src/Legacy/widgets/statuswidget.ui

@@ -636,9 +636,18 @@ border-radius: 3px;</string>
         </spacer>
        </item>
        <item>
-        <widget class="QCheckBox" name="checkBox">
-         <property name="text">
-          <string>CheckBox</string>
+        <widget class="SwitchButton" name="widget_3" native="true">
+         <property name="minimumSize">
+          <size>
+           <width>64</width>
+           <height>0</height>
+          </size>
+         </property>
+         <property name="cursor">
+          <cursorShape>PointingHandCursor</cursorShape>
+         </property>
+         <property name="focusPolicy">
+          <enum>Qt::NoFocus</enum>
          </property>
         </widget>
        </item>
@@ -676,6 +685,12 @@ border-radius: 3px;</string>
    <header>widgets/weeklycodewidget.h</header>
    <container>1</container>
   </customwidget>
+  <customwidget>
+   <class>SwitchButton</class>
+   <extends>QWidget</extends>
+   <header>widgets/switchbutton.h</header>
+   <container>1</container>
+  </customwidget>
  </customwidgets>
  <resources/>
  <connections/>

+ 372 - 0
src/Legacy/widgets/switchbutton.cpp

@@ -0,0 +1,372 @@
+#include "switchbutton.h"
+
+#include <QLabel>
+#include <QPainter>
+
+SwitchButton::SwitchButton(QWidget* parent, Style style)
+  : QWidget(parent)
+  , _value(false)
+  , _duration(100)
+  , _enabled(true)
+{
+  _pencolor = QColor(120, 120, 120);
+
+  _lg = QLinearGradient(35, 30, 35, 0);
+  _lg.setColorAt(0, QColor(210, 210, 210));
+  _lg.setColorAt(0.25, QColor(255, 255, 255));
+  _lg.setColorAt(0.82, QColor(255, 255, 255));
+  _lg.setColorAt(1, QColor(210, 210, 210));
+
+  _lg2 = QLinearGradient(50, 30, 35, 0);
+  _lg2.setColorAt(0, QColor(230, 230, 230));
+  _lg2.setColorAt(0.25, QColor(255, 255, 255));
+  _lg2.setColorAt(0.82, QColor(255, 255, 255));
+  _lg2.setColorAt(1, QColor(230, 230, 230));
+
+  _lg_disabled = QLinearGradient(50, 30, 35, 0);
+  _lg_disabled.setColorAt(0, QColor(200, 200, 200));
+  _lg_disabled.setColorAt(0.25, QColor(230, 230, 230));
+  _lg_disabled.setColorAt(0.82, QColor(230, 230, 230));
+  _lg_disabled.setColorAt(1, QColor(200, 200, 200));
+
+  _offcolor = QColor(255, 255, 255);
+  _oncolor = QColor(154, 205, 50);
+  _tol = 0;
+  _borderradius = 12;
+  _labeloff = new QLabel(this);
+  _background = new SwitchBackground(this, _oncolor);
+  _labelon = new QLabel(this);
+  _circle = new SwitchCircle(this, _offcolor);
+  __btn_move = new QPropertyAnimation(this);
+  __back_move = new QPropertyAnimation(this);
+
+  __btn_move->setTargetObject(_circle);
+  __btn_move->setPropertyName("pos");
+  __back_move->setTargetObject(_background);
+  __back_move->setPropertyName("size");
+
+  setWindowFlags(Qt::FramelessWindowHint);
+  setAttribute(Qt::WA_TranslucentBackground);
+
+  _labeloff->setText("Off");
+  _labelon->setText("On");
+  _labeloff->move(31, 5);
+  _labelon->move(15, 5);
+  setFixedSize(QSize(60, 24));
+  if (style == Style::YESNO)
+  {
+    _labeloff->setText("No");
+    _labelon->setText("Yes");
+    _labeloff->move(33, 5);
+    _labelon->move(12, 5);
+    setFixedSize(QSize(60, 24));
+  }
+  else if (style == Style::BOOL)
+  {
+    _labeloff->setText("False");
+    _labelon->setText("True");
+    _labeloff->move(37, 5);
+    _labelon->move(12, 5);
+    setFixedSize(QSize(75, 24));
+  }
+  if (style == Style::EMPTY)
+  {
+    _labeloff->setText("");
+    _labelon->setText("");
+    _labeloff->move(31, 5);
+    _labelon->move(12, 5);
+    setFixedSize(QSize(45, 24));
+  }
+
+  _labeloff->setStyleSheet("color: rgb(120, 120, 120); font-weight: bold;");
+  _labelon->setStyleSheet("color: rgb(255, 255, 255); font-weight: bold;");
+
+  _background->resize(20, 20);
+
+  _background->move(2, 2);
+  _circle->move(2, 2);
+}
+
+SwitchButton::~SwitchButton()
+{
+  delete _circle;
+  delete _background;
+  delete _labeloff;
+  delete _labelon;
+  delete __btn_move;
+  delete __back_move;
+}
+
+void SwitchButton::paintEvent(QPaintEvent*)
+{
+  QPainter* painter = new QPainter;
+  painter->begin(this);
+  painter->setRenderHint(QPainter::Antialiasing, true);
+
+  QPen pen(Qt::NoPen);
+  painter->setPen(pen);
+
+  painter->setBrush(_pencolor);
+  painter->drawRoundedRect(0, 0
+    , width(), height()
+    , 12, 12);
+
+  painter->setBrush(_lg);
+  painter->drawRoundedRect(1, 1
+    , width() - 2, height() - 2
+    , 10, 10);
+
+  painter->setBrush(QColor(210, 210, 210));
+  painter->drawRoundedRect(2, 2
+    , width() - 4, height() - 4
+    , 10, 10);
+
+  if (_enabled)
+  {
+    painter->setBrush(_lg2);
+    painter->drawRoundedRect(3, 3
+      , width() - 6, height() - 6
+      , 7, 7);
+  }
+  else
+  {
+    painter->setBrush(_lg_disabled);
+    painter->drawRoundedRect(3, 3
+      , width() - 6, height() - 6
+      , 7, 7);
+  }
+  painter->end();
+}
+
+void SwitchButton::mousePressEvent(QMouseEvent*)
+{
+  if (!_enabled)
+    return;
+
+  __btn_move->stop();
+  __back_move->stop();
+
+  __btn_move->setDuration(_duration);
+  __back_move->setDuration(_duration);
+
+  int hback = 20;
+  QSize initial_size(hback, hback);
+  QSize final_size(width() - 4, hback);
+
+  int xi = 2;
+  int y  = 2;
+  int xf = width() - 22;
+
+  if (_value)
+  {
+    final_size = QSize(hback, hback);
+    initial_size = QSize(width() - 4, hback);
+
+    xi = xf;
+    xf = 2;
+  }
+
+  __btn_move->setStartValue(QPoint(xi, y));
+  __btn_move->setEndValue(QPoint(xf, y));
+
+  __back_move->setStartValue(initial_size);
+  __back_move->setEndValue(final_size);
+
+  __btn_move->start();
+  __back_move->start();
+
+  // Assigning new current value
+  _value = !_value;
+  emit valueChanged(_value);
+}
+
+void SwitchButton::setEnabled(bool flag)
+{
+  _enabled = flag;
+  _circle->setEnabled(flag);
+  _background->setEnabled(flag);
+  if (flag)
+    _labelon->show();
+  else
+  {
+    if (value())
+      _labelon->show();
+    else
+      _labelon->hide();
+  }
+  QWidget::setEnabled(flag);
+}
+
+void SwitchButton::setDuration(int time)
+{
+  _duration = time;
+}
+
+void SwitchButton::setValue(bool flag)
+{
+  if (flag == value())
+    return;
+  else
+  {
+    _value = flag;
+    _update();
+    setEnabled(_enabled);
+  }
+}
+
+bool SwitchButton::value() const
+{
+  return _value;
+}
+
+void SwitchButton::_update()
+{
+  int hback = 20;
+  QSize final_size(width() - 4, hback);
+
+  int y = 2;
+  int xf = width() - 22;
+
+  if (_value)
+  {
+    final_size = QSize(hback, hback);
+    xf = 2;
+  }
+
+  _circle->move(QPoint(xf, y));
+  _background->resize(final_size);
+}
+
+SwitchButton::SwitchBackground::SwitchBackground(QWidget* parent, QColor color, bool rect)
+  : QWidget(parent)
+  , _rect(rect)
+  , _borderradius(12)
+  , _color(color)
+  , _pencolor(QColor(170, 170, 170))
+{
+  setFixedHeight(20);
+
+  _lg = QLinearGradient(0, 25, 70, 0);
+  _lg.setColorAt(0, QColor(154, 194, 50));
+  _lg.setColorAt(0.25, QColor(154, 210, 50));
+  _lg.setColorAt(0.95, QColor(154, 194, 50));
+
+  _lg_disabled = QLinearGradient(0, 25, 70, 0);
+  _lg_disabled.setColorAt(0, QColor(190, 190, 190));
+  _lg_disabled.setColorAt(0.25, QColor(230, 230, 230));
+  _lg_disabled.setColorAt(0.95, QColor(190, 190, 190));
+
+  if (_rect)
+    _borderradius = 0;
+
+  _enabled = true;
+}
+
+SwitchButton::SwitchBackground::~SwitchBackground()
+{
+}
+
+void SwitchButton::SwitchBackground::paintEvent(QPaintEvent*)
+{
+  QPainter* painter = new QPainter;
+  painter->begin(this);
+  painter->setRenderHint(QPainter::Antialiasing, true);
+
+  QPen pen(Qt::NoPen);
+  painter->setPen(pen);
+  if (_enabled)
+  {
+    painter->setBrush(QColor(154, 190, 50));
+    painter->drawRoundedRect(0, 0
+      , width(), height()
+      , 10, 10);
+
+    painter->setBrush(_lg);
+    painter->drawRoundedRect(1, 1, width()-2, height()-2, 8, 8);
+  }
+  else
+  {
+    painter->setBrush(QColor(150, 150, 150));
+    painter->drawRoundedRect(0, 0
+      , width(), height()
+      , 10, 10);
+
+    painter->setBrush(_lg_disabled);
+    painter->drawRoundedRect(1, 1, width() - 2, height() - 2, 8, 8);
+  }
+  painter->end();
+}
+
+void SwitchButton::SwitchBackground::setEnabled(bool flag)
+{
+  _enabled = flag;
+}
+
+SwitchButton::SwitchCircle::SwitchCircle(QWidget* parent, QColor color, bool rect)
+  : QWidget(parent)
+  , _rect(rect)
+  , _borderradius(12)
+  , _color(color)
+  , _pencolor(QColor(120, 120, 120))
+{
+  setFixedSize(20, 20);
+
+  _rg = QRadialGradient(static_cast<int>(width() / 2), static_cast<int>(height() / 2), 12);
+  _rg.setColorAt(0, QColor(255, 255, 255));
+  _rg.setColorAt(0.6, QColor(255, 255, 255));
+  _rg.setColorAt(1, QColor(205, 205, 205));
+
+  _lg = QLinearGradient(3, 18, 20, 4);
+  _lg.setColorAt(0, QColor(255, 255, 255));
+  _lg.setColorAt(0.55, QColor(230, 230, 230));
+  _lg.setColorAt(0.72, QColor(255, 255, 255));
+  _lg.setColorAt(1, QColor(255, 255, 255));
+
+  _lg_disabled = QLinearGradient(3, 18, 20, 4);
+  _lg_disabled.setColorAt(0, QColor(230, 230, 230));
+  _lg_disabled.setColorAt(0.55, QColor(210, 210, 210));
+  _lg_disabled.setColorAt(0.72, QColor(230, 230, 230));
+  _lg_disabled.setColorAt(1, QColor(230, 230, 230));
+
+  _enabled = true;
+}
+
+SwitchButton::SwitchCircle::~SwitchCircle()
+{
+}
+
+void SwitchButton::SwitchCircle::paintEvent(QPaintEvent*)
+{
+  QPainter* painter = new QPainter;
+  painter->begin(this);
+  painter->setRenderHint(QPainter::Antialiasing, true);
+
+  QPen pen(Qt::NoPen);
+  painter->setPen(pen);
+  painter->setBrush(_pencolor);
+
+  painter->drawEllipse(0, 0, 20, 20);
+  painter->setBrush(_rg);
+  painter->drawEllipse(1, 1, 18, 18);
+
+  painter->setBrush(QColor(210, 210, 210));
+  painter->drawEllipse(2, 2, 16, 16);
+
+  if (_enabled)
+  {
+    painter->setBrush(_lg);
+    painter->drawEllipse(3, 3, 14, 14);
+  }
+  else
+  {
+    painter->setBrush(_lg_disabled);
+    painter->drawEllipse(3, 3, 14, 14);
+  }
+
+  painter->end();
+}
+
+void SwitchButton::SwitchCircle::setEnabled(bool flag)
+{
+  _enabled = flag;
+}

+ 122 - 0
src/Legacy/widgets/switchbutton.h

@@ -0,0 +1,122 @@
+#ifndef SWITCHBUTTON_H
+#define SWITCHBUTTON_H
+
+#include <QWidget>
+#include <QLabel>
+#include <QPropertyAnimation>
+
+class SwitchButton : public QWidget
+{
+  Q_OBJECT
+    Q_DISABLE_COPY(SwitchButton)
+
+public:
+  enum Style
+  {
+    YESNO,
+    ONOFF,
+    BOOL,
+    EMPTY
+  };
+
+public:
+  explicit SwitchButton(QWidget* parent = nullptr, Style style = Style::ONOFF);
+  ~SwitchButton() override;
+
+  //-- QWidget methods
+  void mousePressEvent(QMouseEvent *) override;
+  void paintEvent(QPaintEvent* event) override;
+  void setEnabled(bool);
+
+  //-- Setters
+  void setDuration(int);
+  void setValue(bool);
+
+  //-- Getters
+  bool value() const;
+
+signals:
+  void valueChanged(bool newvalue);
+
+private:
+  class SwitchCircle;
+  class SwitchBackground;
+  void _update();
+
+private:
+  bool _value;
+  int  _duration;
+
+  QLinearGradient _lg;
+  QLinearGradient _lg2;
+  QLinearGradient _lg_disabled;
+
+  QColor _pencolor;
+  QColor _offcolor;
+  QColor _oncolor;
+  int    _tol;
+  int    _borderradius;
+
+  // This order for definition is important (these widgets overlap)
+  QLabel*           _labeloff;
+  SwitchBackground* _background;
+  QLabel*           _labelon;
+  SwitchCircle*     _circle;
+
+  bool _enabled;
+
+  QPropertyAnimation* __btn_move;
+  QPropertyAnimation* __back_move;
+};
+
+class SwitchButton::SwitchBackground : public QWidget
+{
+  Q_OBJECT
+    Q_DISABLE_COPY(SwitchBackground)
+
+public:
+  explicit SwitchBackground(QWidget* parent = nullptr, QColor color = QColor(154, 205, 50), bool rect = false);
+  ~SwitchBackground() override;
+
+  //-- QWidget methods
+  void paintEvent(QPaintEvent* event) override;
+  void setEnabled(bool);
+
+private:
+  bool            _rect;
+  int             _borderradius;
+  QColor          _color;
+  QColor          _pencolor;
+  QLinearGradient _lg;
+  QLinearGradient _lg_disabled;
+
+  bool _enabled;
+};
+
+
+class SwitchButton::SwitchCircle : public QWidget
+{
+  Q_OBJECT
+    Q_DISABLE_COPY(SwitchCircle)
+
+public:
+  explicit SwitchCircle(QWidget* parent = nullptr, QColor color = QColor(255, 255, 255), bool rect = false);
+  ~SwitchCircle() override;
+
+  //-- QWidget methods
+  void paintEvent(QPaintEvent* event) override;
+  void setEnabled(bool);
+
+private:
+  bool            _rect;
+  int             _borderradius;
+  QColor          _color;
+  QColor          _pencolor;
+  QRadialGradient _rg;
+  QLinearGradient _lg;
+  QLinearGradient _lg_disabled;
+
+  bool _enabled;
+};
+
+#endif // SWITCHBUTTON_H