Rename file imageswitch.h & imageswitch.cpp to ImageSwitch.h & ImageSwitch.cpp, and refactor.
This commit is contained in:
54
src/components/ImageSwitch.cpp
Normal file
54
src/components/ImageSwitch.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "ImageSwitch.h"
|
||||
|
||||
#include <qpainter.h>
|
||||
#include <qdebug.h>
|
||||
|
||||
|
||||
namespace{
|
||||
const int DEFAULT_WIDTH = 100;
|
||||
const int DEFAULT_HEIGHT = 50;
|
||||
const char * const OFF_FILE_PATH = ":/icons/imageswitch/btncheckoff1.png";
|
||||
const char * const ON_FILE_PATH = ":/icons/imageswitch/btncheckon1.png";
|
||||
}
|
||||
|
||||
ImageSwitch::ImageSwitch(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
, mIsChecked(false)
|
||||
, mImgOffFile(OFF_FILE_PATH)
|
||||
, mImgOnFile(ON_FILE_PATH)
|
||||
{
|
||||
this->resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
||||
this->setMinimumSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
||||
}
|
||||
|
||||
void ImageSwitch::mousePressEvent(QMouseEvent*)
|
||||
{
|
||||
mIsChecked = !mIsChecked;
|
||||
this->update();
|
||||
emit clicked();
|
||||
}
|
||||
|
||||
void ImageSwitch::paintEvent(QPaintEvent*)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.setRenderHints(QPainter::SmoothPixmapTransform);
|
||||
QImage img(mIsChecked ? mImgOffFile : mImgOnFile);
|
||||
img = img.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
//按照比例自动居中绘制
|
||||
int pixX = rect().center().x() - img.width() / 2;
|
||||
int pixY = rect().center().y() - img.height() / 2;
|
||||
QPoint point(pixX, pixY);
|
||||
painter.drawImage(point, img);
|
||||
}
|
||||
|
||||
bool ImageSwitch::getChecked() const
|
||||
{
|
||||
return mIsChecked;
|
||||
}
|
||||
|
||||
|
||||
void ImageSwitch::setChecked(bool value) {
|
||||
if (this->mIsChecked == value) return;
|
||||
this->mIsChecked = value;
|
||||
this->update();
|
||||
}
|
||||
28
src/components/ImageSwitch.h
Normal file
28
src/components/ImageSwitch.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef IMAGESWITCH_H
|
||||
#define IMAGESWITCH_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class ImageSwitch : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ImageSwitch(QWidget *parent = nullptr);
|
||||
~ImageSwitch() override = default;
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *) override;
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
private:
|
||||
bool mIsChecked;
|
||||
QString mImgOffFile;
|
||||
QString mImgOnFile;
|
||||
|
||||
public:
|
||||
bool getChecked() const;
|
||||
signals:
|
||||
void clicked();
|
||||
public Q_SLOTS:
|
||||
void setChecked(bool isChecked);
|
||||
};
|
||||
|
||||
#endif // IMAGESWITCH_H
|
||||
@@ -1,91 +0,0 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "imageswitch.h"
|
||||
#include "qpainter.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
ImageSwitch::ImageSwitch(QWidget* parent) : QWidget(parent)
|
||||
{
|
||||
isChecked = false;
|
||||
setButtonStyle(ButtonStyle_1);
|
||||
this->setFixedSize(sizeHint());
|
||||
}
|
||||
|
||||
void ImageSwitch::mousePressEvent(QMouseEvent*)
|
||||
{
|
||||
imgFile = isChecked ? imgOffFile : imgOnFile;
|
||||
isChecked = !isChecked;
|
||||
this->update();
|
||||
emit clicked();
|
||||
}
|
||||
|
||||
void ImageSwitch::paintEvent(QPaintEvent*)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.setRenderHints(QPainter::SmoothPixmapTransform);
|
||||
QImage img(imgFile);
|
||||
img = img.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
|
||||
//按照比例自动居中绘制
|
||||
int pixX = rect().center().x() - img.width() / 2;
|
||||
int pixY = rect().center().y() - img.height() / 2;
|
||||
QPoint point(pixX, pixY);
|
||||
painter.drawImage(point, img);
|
||||
}
|
||||
|
||||
bool ImageSwitch::getChecked() const
|
||||
{
|
||||
return isChecked;
|
||||
}
|
||||
|
||||
ImageSwitch::ButtonStyle ImageSwitch::getButtonStyle() const
|
||||
{
|
||||
return this->buttonStyle;
|
||||
}
|
||||
|
||||
QSize ImageSwitch::sizeHint() const
|
||||
{
|
||||
return {100, 50};
|
||||
}
|
||||
|
||||
QSize ImageSwitch::minimumSizeHint() const
|
||||
{
|
||||
return sizeHint();
|
||||
}
|
||||
|
||||
void ImageSwitch::setChecked(bool value)
|
||||
{
|
||||
if (this->isChecked != value) {
|
||||
this->isChecked = value;
|
||||
imgFile = isChecked ? imgOnFile : imgOffFile;
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
|
||||
void ImageSwitch::setButtonStyle(const ImageSwitch::ButtonStyle& style)
|
||||
{
|
||||
if (this->buttonStyle != style) {
|
||||
this->buttonStyle = style;
|
||||
|
||||
if (buttonStyle == ButtonStyle_1) {
|
||||
imgOffFile = ":/icons/imageswitch/btncheckoff1.png";
|
||||
imgOnFile = ":/icons/imageswitch/btncheckon1.png";
|
||||
this->resize(sizeHint());
|
||||
}
|
||||
else if (buttonStyle == ButtonStyle_2) {
|
||||
imgOffFile = ":/icons/imageswitch/btncheckoff2.png";
|
||||
imgOnFile = ":/icons/imageswitch/btncheckon2.png";
|
||||
this->resize(sizeHint());
|
||||
}
|
||||
else if (buttonStyle == ButtonStyle_3) {
|
||||
imgOffFile = ":/icons/imageswitch/btncheckoff3.png";
|
||||
imgOnFile = ":/icons/imageswitch/btncheckon3.png";
|
||||
this->resize(sizeHint());
|
||||
}
|
||||
|
||||
imgFile = isChecked ? imgOnFile : imgOffFile;
|
||||
setChecked(isChecked);
|
||||
this->update();
|
||||
updateGeometry();
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
#ifndef IMAGESWITCH_H
|
||||
#define IMAGESWITCH_H
|
||||
|
||||
/**
|
||||
* 图片开关控件 作者:feiyangqingyun(QQ:517216493) 2016-11-25
|
||||
* 1. 自带三种开关按钮样式。
|
||||
* 2. 可自定义开关图片。
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT ImageSwitch : public QWidget
|
||||
#else
|
||||
class ImageSwitch : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_ENUMS(ButtonStyle)
|
||||
|
||||
Q_PROPERTY(bool isChecked READ getChecked WRITE setChecked)
|
||||
Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
|
||||
|
||||
public:
|
||||
enum ButtonStyle {
|
||||
ButtonStyle_1 = 0, //开关样式1
|
||||
ButtonStyle_2 = 1, //开关样式2
|
||||
ButtonStyle_3 = 2, //开关样式3
|
||||
None = 3 //开关样式3
|
||||
};
|
||||
|
||||
explicit ImageSwitch(QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *);
|
||||
void paintEvent(QPaintEvent *event);
|
||||
|
||||
private:
|
||||
bool isChecked;
|
||||
ButtonStyle buttonStyle = None;
|
||||
|
||||
QString imgOffFile;
|
||||
QString imgOnFile;
|
||||
QString imgFile;
|
||||
|
||||
public:
|
||||
bool getChecked() const;
|
||||
ButtonStyle getButtonStyle() const;
|
||||
QSize sizeHint() const;
|
||||
QSize minimumSizeHint() const;
|
||||
signals:
|
||||
void clicked();
|
||||
public Q_SLOTS:
|
||||
//设置是否选中
|
||||
void setChecked(bool isChecked);
|
||||
//设置按钮样式
|
||||
void setButtonStyle(const ImageSwitch::ButtonStyle &buttonStyle);
|
||||
};
|
||||
|
||||
#endif // IMAGESWITCH_H
|
||||
Reference in New Issue
Block a user