File: exclusivegroup.cpp

package info (click to toggle)
lomiri-ui-toolkit 1.3.5010%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,900 kB
  • sloc: cpp: 85,772; python: 5,528; sh: 1,364; javascript: 919; ansic: 573; makefile: 204
file content (172 lines) | stat: -rw-r--r-- 5,242 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
87
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
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright 2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "exclusivegroup_p.h"

#include <QtCore/QSignalMapper>

#include "ucaction_p.h"

#define CHECKED_PROPERTY "checked"

UT_NAMESPACE_BEGIN

static const char *checkableSignals[] = {
    "toggled(bool)",
    0
};

static bool isChecked(const QObject *o)
{
    if (!o) return false;
    QVariant checkedVariant = o->property(CHECKED_PROPERTY);
    return checkedVariant.isValid() && checkedVariant.toBool();
}

/*!
 * \qmltype ExclusiveGroup
 * \inqmlmodule Lomiri.Components
 * \since Lomiri.Components 1.3
 * \ingroup lomiri
 * \inherits ActionList
 * \brief ExclusiveGroup provides a way to declare several checkable controls as mutually exclusive.
 *
 * The ExclusiveGroup will only allow a single object to have it's checkable property set to "true"
 * at any one time. The exclusive group accepts child Actions, but objects other than Actions can be
 * used by using the \l bindCheckable function as long as they support one of the required signals,
 * and a "checked" property.
 * \qml
 * ExclusiveGroup {
 *     Action {
 *         parameterType: Action.Bool
 *         state: true
 *     }
 *     Action {
 *         parameterType: Action.Bool
 *         state: false
 *     }
 * }
 * \endqml
 */
ExclusiveGroup::ExclusiveGroup(QObject *parent)
    : ActionList(parent)
    , m_signalMapper(new QSignalMapper(this))
    , m_entranceGuard(false)
{
    connect(this, &ActionList::added, this, &ExclusiveGroup::onActionAdded);
    connect(this, &ActionList::removed, this, &ExclusiveGroup::onActionRemoved);

    int index = m_signalMapper->metaObject()->indexOfMethod("map()");
    m_updateCurrentMethod = m_signalMapper->metaObject()->method(index);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
    connect(m_signalMapper, static_cast<void(QSignalMapper::*)(QObject *)>(&QSignalMapper::mapped), this, [this](QObject *object) {
#else
    connect(m_signalMapper, static_cast<void(QSignalMapper::*)(QObject *)>(&QSignalMapper::mappedObject), this, [this](QObject *object) {
#endif
        if (isChecked(object)) {
            setCurrent(object);
        }
    });
}

void ExclusiveGroup::onActionAdded(UCAction *action)
{
    action->setExclusiveGroup(this);
}

void ExclusiveGroup::onActionRemoved(UCAction *action)
{
    action->setExclusiveGroup(nullptr);
}

/*!
 * \qmlproperty Action ExclusiveGroup::current
 * Returns the currently checked action
 */
void ExclusiveGroup::setCurrent(QObject *object)
{
    if (m_current == object)
        return;

    if (m_current)
        m_current->setProperty(CHECKED_PROPERTY, QVariant(false));
    m_current = object;
    if (m_current)
        m_current->setProperty(CHECKED_PROPERTY, QVariant(true));
    Q_EMIT currentChanged();
}

QObject *ExclusiveGroup::current() const
{
    return m_current.data();
}

/*!
 * \qmlmethod void ExclusiveGroup::bindCheckable(object object)
 * Explicitly bind an objects checkability to this exclusive group.
 * \note This only works with objects which support the following signals signals:
 * \list
 *  \li \b toggled(bool)
 * \endlist
 * \qml
 * Item {
 *     ExclusiveGroup {
 *         id: exclusiveGroup
 *     }
 *     Instantiator {
 *         model: 4
 *         onObjectAdded: exclusiveGroup.bindCheckable(object)
 *         onObjectRemoved: exclusiveGroup.unbindCheckable(object)
 *
 *         Action {
 *             checkable: true
 *         }
 *     }
 * }
 * \endqml
 * \sa ExclusiveGroup::unbindCheckable
 */
void ExclusiveGroup::bindCheckable(QObject *object)
{
    for (const char **signalName = checkableSignals; *signalName; signalName++) {
        int signalIndex = object->metaObject()->indexOfSignal(*signalName);
        if (signalIndex != -1) {
            QMetaMethod signalMethod = object->metaObject()->method(signalIndex);
            connect(object, signalMethod, m_signalMapper, m_updateCurrentMethod, Qt::UniqueConnection);
            m_signalMapper->setMapping(object, object);
            connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(unbindCheckable(QObject*)), Qt::UniqueConnection);
            if (!m_current && isChecked(object))
                setCurrent(object);
            break;
        }
    }
}

/*!
 * \qmlmethod void ExclusiveGroup::unbindCheckable(object object)
 * Explicitly unbind an objects checkability from this exclusive group.
 * \sa ExclusiveGroup::bindCheckable
 */
void ExclusiveGroup::unbindCheckable(QObject *object)
{
    if (m_current == object)
        setCurrent(0);

    disconnect(object, 0, m_signalMapper, 0);
    disconnect(object, SIGNAL(destroyed(QObject*)), this, SLOT(unbindCheckable(QObject*)));
}

UT_NAMESPACE_END