You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.6 KiB
C

#ifndef SACOLORTOOLBUTTON_H
#define SACOLORTOOLBUTTON_H
#include <QToolButton>
#include "SAColorWidgetsGlobal.h"
class QPaintEvent;
class QResizeEvent;
class QPainter;
class QStylePainter;
class SAColorMenu;
/**
* @brief toolbutton
*
*
* ToolButtonIconOnlysetIcon,
*
*
* color
*
*
* ToolButtonIconOnly
*
*
* icon
* color
*
*
* ToolButtonTextBesideIconToolButtonTextUnderIconsetIconSize
* ToolButtonTextBesideIconToolButtonTextUnderIcon
*
* setIcon(QIcon()),iconSize
*
*
*
*
*
*
* 1/4
*
*/
class SA_COLOR_WIDGETS_API SAColorToolButton : public QToolButton
{
Q_OBJECT
SA_COLOR_WIDGETS_DECLARE_PRIVATE(SAColorToolButton)
public:
/**
* @brief
* @param parent
* @return
*/
enum ColorToolButtonStyle
{
WithColorMenu, ///< 默认会构建一个SAColorMenu
NoColorMenu ///< 没有ColorMenu
};
public:
explicit SAColorToolButton(QWidget* parent = nullptr);
explicit SAColorToolButton(ColorToolButtonStyle style, QWidget* parent = nullptr);
~SAColorToolButton();
// 获取颜色
QColor color() const;
// 设置Margins
void setMargins(const QMargins& mg);
QMargins margins() const;
// 绘制无颜色
static void paintNoneColor(QPainter* p, const QRect& colorRect);
// 设置颜色按钮的样式
void setColorToolButtonStyle(ColorToolButtonStyle s);
ColorToolButtonStyle colorToolButtonStyle() const;
// 获取colorMenu注意这个函数很有可能会返回nullptr如果ColorToolButtonStyle设置为NoColorMenu或者自己设置了菜单此函数返回nullptr
SAColorMenu* colorMenu() const;
// 建立标准的颜色菜单
SAColorMenu* createColorMenu();
public slots:
// 设置颜色,会发射colorChanged信号
void setColor(const QColor& c);
protected:
// 获取关键的三个rect位置
virtual void calcRect(const QStyleOptionToolButton& opt, QRect& iconRect, QRect& textRect, QRect& colorRect);
virtual void paintButton(QStylePainter* p, const QStyleOptionToolButton& opt);
virtual void paintIcon(QStylePainter* p, const QRect& iconRect, const QStyleOptionToolButton& opt);
virtual void paintText(QStylePainter* p, const QRect& textRect, const QStyleOptionToolButton& opt);
virtual void paintColor(QStylePainter* p, const QRect& colorRect, const QColor& color, const QStyleOptionToolButton& opt);
protected:
virtual void paintEvent(QPaintEvent* e) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE;
virtual QSize sizeHint() const Q_DECL_OVERRIDE;
private slots:
void onButtonClicked(bool checked = false);
signals:
/**
* @brief
* @param color
*/
void colorClicked(const QColor& color, bool checked = false);
/**
* @brief
* @param color
*/
void colorChanged(const QColor& color);
};
#endif // SACOLORTOOLBUTTON_H