1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "singlechoicebox.h"
6
7#include <QRadioButton>
8#include <QVBoxLayout>
9#include <QSet>
10
11class SingleChoiceBoxPrivate
12{
13 friend class SingleChoiceBox;
14 QList<QRadioButton*> radioButtons{};
15 QGroupBox *groupBox = nullptr;
16 QVBoxLayout *vLayoutMain = nullptr;
17 QVBoxLayout *vLayoutCentral = nullptr;
18 inline SingleChoiceBoxPrivate(){}
19};
20
21SingleChoiceBox::SingleChoiceBox(QWidget *parent)
22 : QWidget (parent)
23 , d(new SingleChoiceBoxPrivate)
24{
25 d->groupBox = new QGroupBox();
26 d->vLayoutMain = new QVBoxLayout();
27 d->vLayoutCentral = new QVBoxLayout();
28 d->groupBox->setLayout(d->vLayoutMain);
29 d->vLayoutCentral->addWidget(d->groupBox);
30 setLayout(d->vLayoutCentral);
31}
32
33SingleChoiceBox::~SingleChoiceBox()
34{
35 if (d) {
36 delete d;
37 }
38}
39
40void SingleChoiceBox::setInfos(QSet<SingleChoiceBox::Info> infos)
41{
42 for (auto checkbox : d->radioButtons) {
43 delete checkbox;
44 }
45
46 for (auto info : infos) {
47 auto radioButton = new QRadioButton();
48 QObject::connect(radioButton, &QRadioButton::toggled,
49 this, &SingleChoiceBox::toggled,Qt::UniqueConnection);
50 d->radioButtons << radioButton;
51 radioButton->setIcon(info.icon);
52 radioButton->setText(info.text);
53 radioButton->setToolTip(info.tooltip);
54 d->vLayoutMain->addWidget(radioButton);
55 }
56}
57
58void SingleChoiceBox::setChoiceTitle(const QString &title)
59{
60 d->groupBox->setTitle(title);
61}
62
63void SingleChoiceBox::toggled(bool checked)
64{
65 auto toggledIns = qobject_cast<QRadioButton*>(sender());
66 if (toggledIns && checked) {
67 Info info { toggledIns->text(),
68 toggledIns->toolTip(),
69 toggledIns->icon() };
70 emit selected(info);
71 }
72}
73
74uint qHash(const SingleChoiceBox::Info &info, uint seed)
75{
76 return qHash(info.text + " " + info.tooltip, seed);
77}
78