ui_checkbox.h 8.5 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup UI_Components
 * @{
 *
 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
 *
 * @since 1.0
 * @version 1.0
 */

/**
 * @file ui_checkbox.h
 *
 * @brief Defines the attributes and common functions of a check box.
 *
 * @since 1.0
 * @version 1.0
 */

#ifndef GRAPHIC_LITE_UI_CHECKBOX_H
#define GRAPHIC_LITE_UI_CHECKBOX_H

W
wangtiantian 已提交
38 39
#include "animator/animator.h"
#include "animator/interpolation.h"
M
mamingshuai 已提交
40 41 42 43 44 45 46 47 48 49 50 51
#include "common/image.h"
#include "components/ui_view.h"

namespace OHOS {
/**
 * @brief Represents a check box.
 *
 * A check box permits users to make a binary choice.
 *
 * @since 1.0
 * @version 1.0
 */
W
wangtiantian 已提交
52 53 54
#if DEFAULT_ANIMATION
class UICheckBox : public UIView, public AnimatorCallback {
#else
M
mamingshuai 已提交
55
class UICheckBox : public UIView {
W
wangtiantian 已提交
56
#endif
M
mamingshuai 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
public:
    /**
     * @brief Enumerates the states of a check box.
     *
     * @since 1.0
     * @version 1.0
     */
    enum UICheckBoxState : uint8_t {
        /* An enum constant representing the state selected option */
        SELECTED,
        /* An enum constant representing the state unselected option */
        UNSELECTED,
        /* Max num of state */
        MAX_STATUS_NUM,
    };

    /**
     * @brief A constructor used to create a <b>UICheckBox</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UICheckBox();

    /**
     * @brief A destructor used to delete the <b>UICheckBox</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
W
wangtiantian 已提交
87
    virtual ~UICheckBox() {};
M
mamingshuai 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    /**
     * @brief Represents a listener for changes of a check box.
     *
     * This is an inner class of <b>UICheckBox</b>. It contains a callback function to be invoked when the check box
     * state changes.
     *
     * @see UICheckBox
     * @since 1.0
     * @version 1.0
     */
    class OnChangeListener : public HeapBase {
    public:
        /**
         * @brief Called when the state of this check box is switched. This is a virtual function, which needs your
         * implementation.
         *
         * @param state Indicates the current state of this check box. For details, see {@link UICheckBoxState}.
         * @since 1.0
         * @version 1.0
         */
        virtual bool OnChange(UICheckBoxState state) = 0;

        /**
         * @brief A destructor used to delete the <b>OnChangeListener</b> instance.
         *
         * @since 1.0
         * @version 1.0
         */
        virtual ~OnChangeListener() {}
    };

    /**
     * @brief Obtains the component type.
     *
     * @return Returns the component type, as defined in {@link UIViewType}.
     * @since 1.0
     * @version 1.0
     */
    UIViewType GetViewType() const override
    {
        return UI_CHECK_BOX;
    }

    /**
     * @brief Sets the listener for this check box.
     *
     * The listener is triggered to invoke the callback function upon click events.
     *
     * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
     * @since 1.0
     * @version 1.0
     */
    void SetOnChangeListener(OnChangeListener* onStateChangeListener)
    {
        onStateChangeListener_ = onStateChangeListener;
    }

    /**
     * @fn  virtual bool UICheckBox::OnPreDraw(Rect& invalidatedArea) override
     *
     * @brief Do something before draw, this function will be invoked mainly to check if this view need
     *        to cover invalidate area so render manager can decide which layer to draw firstly.
     * @param [in] invalidate area.
     * @returns True if need cover.
     */
    bool OnPreDraw(Rect& invalidatedArea) const override
    {
        return false;
    }

    /**
N
niulihua 已提交
160
     * @fn  virtual void UICheckBox::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
161 162 163 164 165 166 167
     *
     * @brief   Executes the draw action
     *          Ondraw invokes the rendering function provided by the underlying layer to draw pictures
     *          based on the selected status of the checkbox.
     *
     * @param [in] invalidatedArea The invalidated area.
     */
N
niulihua 已提交
168
    void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

    /**
     * @fn  virtual void UICheckBox::OnClickEvent(const ClickEvent& event) override;
     *
     * @brief   Executes the click event action
     *          OnClickEvent will reverse the selected state of checkbox.
     *          Example: If the check box is selected, the checkbox status is changed to
     *          Unselected after the click action is taken.
     *
     * @param [in] event   The event that passed when OnClickEvent is invoked.
     * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
     */
    bool OnClickEvent(const ClickEvent& event) override;

    /**
     * @brief Sets the images for this check box.
     *
     * @param selectedImageSrc Indicates the image for this check box when selected.
     * @param unselectedImageSrc Indicates the image for this check box when unselected.
     * @since 1.0
     * @version 1.0
     */
    virtual void SetImages(const char* selectedImageSrc, const char* unselectedImageSrc);

    /**
     * @brief Sets the images for this check box.
     *
     * @param selectedImageSrc Indicates the image for this check box when selected.
     * @param unselectedImageSrc Indicates the image for this check box when unselected.
     * @since 1.0
     * @version 1.0
     */
    virtual void SetImages(const ImageInfo* selectedImageSrc, const ImageInfo* unselectedImageSrc);

    /**
     * @brief Obtains the state of this check box.
     *
     * @return Returns the state of this check box, as defined in {@link UICheckBoxState}.
     * @since 1.0
     * @version 1.0
     */
    UICheckBoxState GetState() const
    {
        return state_;
    };

    /**
     * @brief Sets the state for this check box.
     *
     * @param state Indicates the state of this check box. For details, see {@link UICheckBoxState}.
G
guyuanzhang 已提交
219 220
     * @param needAnimater Whether the state change process can be animated, the setting takes effect when the
     *        DEFAULT_ANIMATION is 1, {@link DEFAULT_ANIMATION}
M
mamingshuai 已提交
221 222 223
     * @since 1.0
     * @version 1.0
     */
G
guyuanzhang 已提交
224
    void SetState(UICheckBoxState state, bool needAnimater = false);
M
mamingshuai 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    /**
     * @brief Sets the selected state color for this check box.
     *
     * @param color Indicates the selected state color of this check box.
     *
     * @since 5.0
     * @version 3.0
     */
    void SetSelectedStateColor(ColorType color);

    /**
     * @brief Obtains the selected state color of this check box.
     *
     * @return Returns the selected state color of this check box
     * @since 5.0
     * @version 3.0
     */
    ColorType GetSelectedStateColor() const;

M
mamingshuai 已提交
245
protected:
G
guyuanzhang 已提交
246
    void ReverseState();
247
    virtual void CalculateSize();
N
niulihua 已提交
248 249 250 251 252 253 254 255 256 257
    void SelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
                                      Rect rect,
                                      Rect trunc,
                                      int16_t borderRadius,
                                      int16_t rectLineWidth);
    void UnSelectedStateSoftwareDrawing(BufferInfo& gfxDstBuffer,
                                        Rect rect,
                                        Rect trunc,
                                        int16_t borderRadius,
                                        int16_t rectLineWidth);
W
wangtiantian 已提交
258 259 260 261 262
#if DEFAULT_ANIMATION
    virtual void ResetCallback();
    void Callback(UIView* view) override;
    void OnStop(UIView& view) override;
#endif
263 264 265
    static constexpr int16_t DEFAULT_HOT_WIDTH = 46;
    static constexpr int16_t DEFAULT_HOT_HEIGHT = 46;
    static constexpr int16_t DEFAULT_BORDER_WIDTH = 22;
W
wangtiantian 已提交
266

M
mamingshuai 已提交
267 268
    UICheckBoxState state_;
    OnChangeListener* onStateChangeListener_;
269 270 271
    int16_t width_;
    int16_t height_;
    int16_t borderWidth_;
M
mamingshuai 已提交
272
    Image image_[MAX_STATUS_NUM];
W
wangtiantian 已提交
273 274 275 276 277
    uint8_t backgroundOpacity_;
#if DEFAULT_ANIMATION
    Animator checkBoxAnimator_;
    uint32_t runTime_;
#endif
278
    ColorType selectedStateColor_;
M
mamingshuai 已提交
279 280 281
};
} // namespace OHOS
#endif // GRAPHIC_LITE_UI_CHECKBOX_H