File indexing completed on 2025-01-05 05:14:41

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "buttonsgroup.h"
0008 
0009 #include <QAbstractButton>
0010 
0011 ButtonsGroup::ButtonsGroup(QObject *parent)
0012     : QObject(parent)
0013 {
0014 }
0015 
0016 void ButtonsGroup::addButton(QAbstractButton *btn, QObject *data)
0017 {
0018     btn->setCheckable(true);
0019     btn->setChecked(!mButtons.size());
0020     connect(btn, &QAbstractButton::clicked, this, &ButtonsGroup::buttonClicked);
0021     if (data)
0022         mData.insert(btn, data);
0023     mButtons.append(btn);
0024 }
0025 
0026 QAbstractButton *ButtonsGroup::at(int i) const
0027 {
0028     return mButtons.at(i);
0029 }
0030 
0031 void ButtonsGroup::simulateClickOn(int index)
0032 {
0033     Q_EMIT clicked(index);
0034 }
0035 
0036 void ButtonsGroup::buttonClicked()
0037 {
0038     auto btn = qobject_cast<QAbstractButton *>(sender());
0039     if (!btn)
0040         return;
0041 
0042     for (auto &b : std::as_const(mButtons))
0043         b->setChecked(b == btn);
0044 
0045     const auto index = mButtons.indexOf(btn);
0046     Q_EMIT clicked(index);
0047 }
0048 
0049 #include "moc_buttonsgroup.cpp"