AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ARadioButton.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12//
   13// Created by alex2 on 21.09.2020.
   14//
   15
   16#pragma once
   17
   18#include "ALabel.h"
   19#include "AViewContainer.h"
   20#include <AUI/Common/AMap.h>
   21#include <AUI/ASS/Selector/Selected.h>
   22
   23
   24class ARadioButtonInner: public AView
   25{
   26public:
   27    ARadioButtonInner() = default;
   28    virtual ~ARadioButtonInner() = default;
   29
   30    void update();
   31};
   32
   33
   44class API_AUI_VIEWS ARadioButton : public AViewContainerBase, public ass::ISelectable {
   45public:
   46    ARadioButton();
   47    ARadioButton(const AString& text);
   48
   49    virtual ~ARadioButton();
   50
   51    void setText(const AString& text);
   52
   53
   54    [[nodiscard]] bool isChecked() const {
   55        return mChecked;
   56    }
   57
   58    void setChecked(const bool checked) {
   59        mChecked = checked;
   60        emit customCssPropertyChanged();
   61        emit ARadioButton::mCheckedChanged(checked);
   62    }
   63
   64    [[nodiscard]]
   65    auto checked() const {
   66        return APropertyDef {
   67            this,
   68            &ARadioButton::mChecked,
   69            &ARadioButton::setChecked,
   70            mCheckedChanged,
   71        };
   72    }
   73
   74    void onPointerReleased(const APointerReleasedEvent& event) override;
   75
   76    class API_AUI_VIEWS Group: public AObject {
   77    private:
   78        AMap<int, _<ARadioButton>> mButtons;
   79        _weak<ARadioButton> mSelectedRadio;
   80        int mSelectedId = -1;
   81
   82    public:
   83        Group() = default;
   84        ~Group() override = default;
   85
   86        _<ARadioButton> addRadioButton(const _<ARadioButton>& radio, int id = -1);
   87
   88        [[nodiscard]] _<ARadioButton> getSelectedRadio() const;
   89        [[nodiscard]] int getSelectedId() const;
   90
   91        void setSelectedId(int id);
   92
   93        [[nodiscard]] bool isSelected() const {
   94            return mSelectedRadio.lock() != nullptr;
   95        }
   96
   97        void uncheckAll() {
   98            for (auto& b : mButtons) {
   99                b.second->setChecked(false);
  100            }
  101            mSelectedId = -1;
  102        }
  103
  104    signals:
  105        emits<int> selectionChanged;
  106    };
  107
  108protected:
  109    bool selectableIsSelectedImpl() override;
  110
  111private:
  112    _<ALabel> mText;
  113    bool mChecked = false;
  114    emits<bool> mCheckedChanged;
  115};
  116
  117namespace declarative {
  118    using RadioButton = aui::ui_building::view<ARadioButton>;
  119}
A std::map with AUI extensions.
Definition AMap.h:218
void onPointerReleased(const APointerReleasedEvent &event) override
Called on pointer (mouse) released event.
Represents a Unicode character string.
Definition AString.h:38
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Interface to work with ass::Selected selector.
Definition Selected.h:21
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572
#define emit
emits the specified signal in context of this object.
Definition AObject.h:343
Pointing method press event.
Definition APointerReleasedEvent.h:19
Property implementation to use with custom getter/setter.
Definition AProperty.h:234
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:52
Declarative view trait.
Definition Declarative.h:180