| 1 | // QmlRadioButtonHelper.hxx - helper for QtQuick radio button impl |
| 2 | // |
| 3 | // Written by James Turner, started April 2018. |
| 4 | // |
| 5 | // Copyright (C) 2015 James Turner <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #include "QmlRadioButtonHelper.hxx" |
| 22 | |
| 23 | #include <QMetaObject> |
| 24 | #include <QDebug> |
| 25 | #include <QQmlEngine> |
| 26 | |
| 27 | QmlRadioButtonGroup::QmlRadioButtonGroup(QObject *parent) : QObject(parent) |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | |
| 33 | QmlRadioButtonGroupAttached* QmlRadioButtonGroup::qmlAttachedProperties(QObject *object) |
| 34 | { |
| 35 | return new QmlRadioButtonGroupAttached(object); |
| 36 | } |
| 37 | |
| 38 | QObject *QmlRadioButtonGroup::selected() const |
| 39 | { |
| 40 | return m_selected; |
| 41 | } |
| 42 | |
| 43 | void QmlRadioButtonGroup::setSelected(QObject *selected) |
| 44 | { |
| 45 | if (m_selected == selected) |
| 46 | return; |
| 47 | |
| 48 | m_selected = selected; |
| 49 | emit selectedChanged(m_selected); |
| 50 | } |
| 51 | |
| 52 | QmlRadioButtonGroupAttached::QmlRadioButtonGroupAttached(QObject *pr) : |
| 53 | QObject(pr) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | QmlRadioButtonGroup *QmlRadioButtonGroupAttached::group() const |
| 58 | { |
| 59 | return m_group; |
| 60 | } |
| 61 | |
| 62 | bool QmlRadioButtonGroupAttached::isSelected() const |
| 63 | { |
| 64 | if (!m_group) |
| 65 | return false; |
| 66 | |
| 67 | return (m_group->selected() == this); |
| 68 | } |
| 69 | |
| 70 | void QmlRadioButtonGroupAttached::setGroup(QmlRadioButtonGroup *group) |
| 71 | { |
| 72 | if (m_group == group) |
| 73 | return; |
| 74 | |
| 75 | if (m_group) { |
| 76 | disconnect(m_group, &QmlRadioButtonGroup::selectedChanged, |
| 77 | this, &QmlRadioButtonGroupAttached::onGroupSelectionChanged); |
| 78 | } |
| 79 | |
| 80 | m_group = group; |
| 81 | |
| 82 | if (m_group) { |
| 83 | connect(m_group, &QmlRadioButtonGroup::selectedChanged, |
| 84 | this, &QmlRadioButtonGroupAttached::onGroupSelectionChanged); |
| 85 | } |
| 86 | |
| 87 | emit groupChanged(m_group); |
| 88 | emit isSelectedChanged(isSelected()); |
| 89 | } |
| 90 | |
| 91 | void QmlRadioButtonGroupAttached::onGroupSelectionChanged() |
| 92 | { |
| 93 | emit isSelectedChanged(isSelected()); |
| 94 | } |