跳转至

ui/widget/material/widget/checkbox/checkbox.h

Material Design 3 CheckBox widget. More...

Namespaces

Name
cf
cf::ui
cf::ui::widget
cf::ui::widget::material
cf::ui::widget::material::base

Classes

Name
class cf::ui::widget::material::CheckBox
Material Design 3 CheckBox widget.

Detailed Description

Material Design 3 CheckBox widget.

Author: CFDesktop Team

Version: 0.1

Since: 0.1

Date: 2026-03-01

Implements Material Design 3 checkbox with support for unchecked, checked, and indeterminate states. Includes ripple effects, state layers, and focus indicators following Material Design 3 specifications.

Source code

#pragma once

#include "base/color.h"
#include "base/include/base/weak_ptr/weak_ptr.h"
#include "cfmaterial_animation_factory.h"
#include "export.h"
#include <QCheckBox>
#include <QWidget>

namespace cf::ui::widget::material {

// Forward declarations
namespace base {
class StateMachine;
class RippleHelper;
class MdFocusIndicator;
} // namespace base

using CFColor = cf::ui::base::CFColor;

class CF_UI_EXPORT CheckBox : public QCheckBox {
    Q_OBJECT
    Q_PROPERTY(bool error READ hasError WRITE setError NOTIFY errorChanged)

  public:
    explicit CheckBox(QWidget* parent = nullptr);

    explicit CheckBox(const QString& text, QWidget* parent = nullptr);

    ~CheckBox() override;

    void setChecked(bool checked);

    void setCheckState(Qt::CheckState state);

    bool hasError() const;

    void setError(bool error);

    QSize sizeHint() const override;

    QSize minimumSizeHint() const override;

  signals:
    void errorChanged(bool error);

  protected:
    void paintEvent(QPaintEvent* event) override;

    void enterEvent(QEnterEvent* event) override;

    void leaveEvent(QEvent* event) override;

    void mousePressEvent(QMouseEvent* event) override;

    void mouseReleaseEvent(QMouseEvent* event) override;

    void focusInEvent(QFocusEvent* event) override;

    void focusOutEvent(QFocusEvent* event) override;

    void changeEvent(QEvent* event) override;

    void nextCheckState() override;

    bool hitButton(const QPoint& pos) const override;

  private:
    // Drawing helpers - Material Design paint pipeline
    QRectF checkboxRect() const;
    QRectF textRect() const;
    void drawBackground(QPainter& p, const QRectF& rect);
    void drawBorder(QPainter& p, const QRectF& rect);
    void drawCheckMark(QPainter& p, const QRectF& rect);
    void drawIndeterminateMark(QPainter& p, const QRectF& rect);
    void drawRipple(QPainter& p, const QRectF& rect);
    void drawText(QPainter& p, const QRectF& rect);
    void drawFocusIndicator(QPainter& p, const QRectF& rect);

    // Animation helper
    void updateAnimationProgress(float progress, bool checked);
    void startCheckMarkAnimation(float target);

    // Color access methods
    CFColor checkmarkColor() const;
    CFColor markDrawColor() const;  // Color for check/indeterminate mark on background
    CFColor borderColor() const;
    CFColor backgroundColor() const;
    CFColor stateLayerColor() const;
    float cornerRadius() const;

    // Helper to get checkbox size in pixels
    float checkboxSize() const;
    float strokeWidth() const;

    // Behavior components
    cf::WeakPtr<components::material::CFMaterialAnimationFactory> m_animationFactory;
    base::StateMachine* m_stateMachine;
    base::RippleHelper* m_ripple;
    base::MdFocusIndicator* m_focusIndicator;

    // Check mark animation progress (0.0 to 1.0)
    float m_checkAnimationProgress = 0.0f;

    // Error state
    bool m_error = false;
};

} // namespace cf::ui::widget::material

Updated on 2026-03-09 at 10:14:01 +0000