ctai/src/ctaiSwitchButton.cpp

156 lines
3.8 KiB
C++

#include "ctaiSwitchButton.h"
ctaiSwitchButton::ctaiSwitchButton(QWidget *parent)
: QWidget(parent),
m_SwBtn_Status(false),
m_SwBtn_background(Qt::black),
//m_SwBtn_checkedColor(0,150,136),
m_SwBtn_checkedColor(255,000,000),
m_SwBtn_disabilityColor(190,190,190),
m_SwBtn_thumbColor(Qt::white),
m_SwBtn_radius(5),
m_SwBtn_Height(10),
m_SwBtn_Margin(10)
{
setCursor(Qt::PointingHandCursor);
connect(&m_SwBtn_timer,SIGNAL(timeout()),this,SLOT(SwBtn_Slide()));
}
bool ctaiSwitchButton::SwBtn_isChecked() const
{
return m_SwBtn_Status;
}
void ctaiSwitchButton::setSwBtn_Status(bool check)
{
m_SwBtn_Status=check;
m_SwBtn_timer.start(5);
}
void ctaiSwitchButton::setSwBtn_BackgroundColor(QColor color)
{
m_SwBtn_background=color;
}
void ctaiSwitchButton::setSwBtn_CheckedColor(QColor color)
{
m_SwBtn_checkedColor=color;
}
void ctaiSwitchButton::setSwBtn_DisabilityColor(QColor color)
{
m_SwBtn_disabilityColor=color;
}
void ctaiSwitchButton::setSwBtn_WidgetSize(int m_width, int m_height)
{
this->m_SwBtn_Height=m_width-m_height;
this->m_SwBtn_Margin=m_height-(m_width/2);
this->m_SwBtn_radius=m_SwBtn_Height/2;
update();
}
void ctaiSwitchButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
QColor background;
QColor thumbColor;
qreal pellucidity;
if(isEnabled()){
if(m_SwBtn_Status){
background=m_SwBtn_checkedColor;
thumbColor=m_SwBtn_checkedColor;
pellucidity=0.6;
}
else{
background=m_SwBtn_background;
thumbColor=m_SwBtn_thumbColor;
pellucidity=0.8;
}
}
else{
background=m_SwBtn_background;
thumbColor=m_SwBtn_disabilityColor;
pellucidity=0.2;
}
painter.setBrush(background);
painter.setOpacity(pellucidity);
path.addRoundedRect(QRectF(m_SwBtn_Margin,
m_SwBtn_Margin,
width()-2*m_SwBtn_Margin,
height()-2*m_SwBtn_Margin),
m_SwBtn_radius,
m_SwBtn_radius);
painter.drawPath(path.simplified());
painter.setBrush(thumbColor);
painter.setOpacity(1);
painter.drawEllipse(QRectF(m_SwBtn_X/*-(m_SwBtn_Height/2)*/,
m_SwBtn_Y+5/*-(m_SwBtn_Height/2)*/,
height()-10,
height()-10));
}
void ctaiSwitchButton::mousePressEvent(QMouseEvent *event)
{
if(isEnabled()){
if(event->button()==Qt::LeftButton){ //**
event->accept();
}
else
event->ignore();
}
}
void ctaiSwitchButton::mouseReleaseEvent(QMouseEvent *event)
{
if(isEnabled()){
if((event->type()==QMouseEvent::MouseButtonRelease)&&(event->button()==Qt::LeftButton)){
event->accept();
m_SwBtn_Status=!m_SwBtn_Status;
emit SwBtn_SWITCH(m_SwBtn_Status);
m_SwBtn_timer.start(5);
}
else{
event->ignore();
}
}
}
//重现大小改变事件
void ctaiSwitchButton::resizeEvent(QResizeEvent *event)
{
m_SwBtn_X=0;
m_SwBtn_Y=0;
QWidget::resizeEvent(event);
}
QSize ctaiSwitchButton::sizeHint() const
{
return minimumSizeHint();
}
QSize ctaiSwitchButton::minimumSizeHint() const
{
return QSize(2*(m_SwBtn_Height+m_SwBtn_Margin),m_SwBtn_Height+2*m_SwBtn_Margin);
}
void ctaiSwitchButton::SwBtn_Slide()
{
if(m_SwBtn_Status){
m_SwBtn_X+=1;
if(m_SwBtn_X>=width()-height())
m_SwBtn_timer.stop();
}
else{
m_SwBtn_X-=1;
if(m_SwBtn_X<=0)
m_SwBtn_timer.stop();
}
update();
}