File: kbuttongroup.cpp

package info (click to toggle)
kup-backup 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: cpp: 8,422; xml: 311; makefile: 6; sh: 3
file content (145 lines) | stat: -rw-r--r-- 3,840 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it>
// SPDX-FileCopyrightText: 2020 Simon Persson <simon.persson@mykolab.com>
//
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "kbuttongroup.h"

#include <QAbstractButton>
#include <QChildEvent>
#include <QHash>
#include <QSignalMapper>

class KButtonGroup::Private
{
public:
    explicit Private(KButtonGroup *q)
        : q(q)
        , currentId(-1)
        , nextId(0)
        , wantToBeId(-1)
    {
        connect(&clickedMapper, SIGNAL(mappedInt(int)), q, SLOT(slotClicked(int)));
        connect(&pressedMapper, SIGNAL(mappedInt(int)), q, SIGNAL(pressed(int)));
        connect(&releasedMapper, SIGNAL(mappedInt(int)), q, SIGNAL(released(int)));
    }

    void slotClicked(int id);

    KButtonGroup *q;
    QSignalMapper clickedMapper;
    QSignalMapper pressedMapper;
    QSignalMapper releasedMapper;

    QHash<QObject *, int> btnMap;
    int currentId;
    int nextId;
    int wantToBeId;
};

KButtonGroup::KButtonGroup(QWidget *parent)
    : QGroupBox(parent)
    , d(new Private(this))
{
}

KButtonGroup::~KButtonGroup()
{
    delete d;
}

void KButtonGroup::setSelected(int id)
{
    if (!testAttribute(Qt::WA_WState_Polished)) {
        d->wantToBeId = id;
        ensurePolished();
        return;
    }

    QHash<QObject *, int>::Iterator it = d->btnMap.begin();
    QHash<QObject *, int>::Iterator itEnd = d->btnMap.end();
    QAbstractButton *button = nullptr;

    for (; it != itEnd; ++it) {
        if ((it.value() == id) && (button = qobject_cast<QAbstractButton *>(it.key()))) {
            button->setChecked(true);
            d->currentId = id;
            emit changed(id);
            d->wantToBeId = -1;
            return;
        }
    }
    // button not found, it might still show up though, eg. because of premature polishing above
    d->wantToBeId = id;
}

int KButtonGroup::selected() const
{
    return d->currentId;
}

void KButtonGroup::childEvent(QChildEvent *event)
{
    if (event->polished()) {
        auto button = qobject_cast<QAbstractButton *>(event->child());
        if (!d->btnMap.contains(event->child()) && button) {
            connect(button, SIGNAL(clicked()), &d->clickedMapper, SLOT(map()));
            d->clickedMapper.setMapping(button, d->nextId);

            connect(button, SIGNAL(pressed()), &d->pressedMapper, SLOT(map()));
            d->pressedMapper.setMapping(button, d->nextId);

            connect(button, SIGNAL(released()), &d->releasedMapper, SLOT(map()));
            d->releasedMapper.setMapping(button, d->nextId);

            d->btnMap[button] = d->nextId;

            if (d->nextId == d->wantToBeId) {
                d->currentId = d->wantToBeId;
                d->wantToBeId = -1;
                button->setChecked(true);
                emit changed(d->currentId);
            }

            ++d->nextId;
        }
    } else if (event->removed()) {
        QObject *obj = event->child();
        QHash<QObject *, int>::ConstIterator it = d->btnMap.constFind(obj);

        if (it != d->btnMap.constEnd()) {
            d->clickedMapper.removeMappings(obj);
            d->pressedMapper.removeMappings(obj);
            d->releasedMapper.removeMappings(obj);

            if (it.value() == d->currentId) {
                d->currentId = -1;
            }

            d->btnMap.remove(obj);
        }
    }

    // be transparent
    QGroupBox::childEvent(event);
}

int KButtonGroup::id(QAbstractButton *button) const
{
    QHash<QObject *, int>::ConstIterator it = d->btnMap.constFind(button);

    if (it != d->btnMap.constEnd()) {
        return it.value();
    }

    return -1;
}

void KButtonGroup::Private::slotClicked(int id)
{
    currentId = id;
    emit q->clicked(id);
    emit q->changed(id);
}

#include "moc_kbuttongroup.cpp"