AUI Framework  master
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
   46class API_AUI_VIEWS ARadioButton : public AViewContainerBase, public ass::ISelectable {
   47public:
   48    ARadioButton();
   49    ARadioButton(const AString& text);
   50
   51    virtual ~ARadioButton();
   52
   53    void setText(const AString& text);
   54
   55
   56    [[nodiscard]] bool isChecked() const {
   57        return mChecked;
   58    }
   59
   60    void setChecked(const bool checked) {
   61        mChecked = checked;
   62        emit customCssPropertyChanged();
   63        emit ARadioButton::mCheckedChanged(checked);
   64    }
   65
   66    [[nodiscard]]
   67    auto checked() const {
   68        return APropertyDef {
   69            this,
   70            &ARadioButton::mChecked,
   71            &ARadioButton::setChecked,
   72            mCheckedChanged,
   73        };
   74    }
   75
   76    void onPointerReleased(const APointerReleasedEvent& event) override;
   77
   78    class API_AUI_VIEWS Group: public AObject {
   79    private:
   80        AMap<int, _<ARadioButton>> mButtons;
   81        _weak<ARadioButton> mSelectedRadio;
   82        int mSelectedId = -1;
   83
   84    public:
   85        Group() = default;
   86        ~Group() override = default;
   87
   88        _<ARadioButton> addRadioButton(const _<ARadioButton>& radio, int id = -1);
   89
   90        [[nodiscard]] _<ARadioButton> getSelectedRadio() const;
   91        [[nodiscard]] int getSelectedId() const;
   92
   93        void setSelectedId(int id);
   94
   95        [[nodiscard]] bool isSelected() const {
   96            return mSelectedRadio.lock() != nullptr;
   97        }
   98
   99        void uncheckAll() {
  100            for (auto& b : mButtons) {
  101                b.second->setChecked(false);
  102            }
  103            mSelectedId = -1;
  104        }
  105
  106    signals:
  107        emits<int> selectionChanged;
  108    };
  109
  110protected:
  111    bool selectableIsSelectedImpl() override;
  112
  113private:
  114    _<ALabel> mText;
  115    bool mChecked = false;
  116    emits<bool> mCheckedChanged;
  117};
  118
  119namespace declarative {
  123    struct RadioButton: aui::ui_building::view<ARadioButton> {
  124        using aui::ui_building::view<ARadioButton>::view;
  125    };
  126}
A std::map with AUI extensions.
Definition AMap.h:218
A radio button.
Definition ARadioButton.h:46
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:577
#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
Declarative form of ARadioButton.
Definition ARadioButton.h:123