Krita Source Code Documentation
Loading...
Searching...
No Matches
KisOptionButtonStrip.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022 Deif Lou <ginoba@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <QButtonGroup>
8#include <QHBoxLayout>
9
10#include <KoGroupButton.h>
11
12#include <kis_assert.h>
14
16{
17public:
19 QButtonGroup *buttonGroup{nullptr};
20};
21
23 : QWidget(parent)
24 , m_d(new Private)
25{
26 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
27
28 QHBoxLayout *mainLayout = new QHBoxLayout;
29 mainLayout->setSpacing(0);
30 mainLayout->setContentsMargins(0, 0, 0, 0);
31 mainLayout->addStretch();
32 setLayout(mainLayout);
33
34 m_d->buttonGroup = new QButtonGroup(this);
35 m_d->buttonGroup->setExclusive(true);
36
37 connect(
38 m_d->buttonGroup,
39 QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled),
40 [this](QAbstractButton *button, bool checked) {
41 Q_EMIT buttonToggled(dynamic_cast<KoGroupButton *>(button), checked);
42 Q_EMIT buttonToggled(m_d->buttonGroup->id(button), checked);
43 });
44}
45
49
51 const QString &text)
52{
53 KoGroupButton *newButton = new KoGroupButton(this);
54 newButton->setCheckable(true);
55 newButton->setIcon(icon);
56 newButton->setText(text);
57 newButton->setMinimumSize(28, 28);
58
59 if (m_d->numberOfButtons > 0) {
60 KoGroupButton *prevButton = dynamic_cast<KoGroupButton *>(
61 m_d->buttonGroup->button(m_d->numberOfButtons - 1));
62 prevButton->setGroupPosition(m_d->numberOfButtons == 1
66 }
67
68 m_d->buttonGroup->addButton(newButton, m_d->numberOfButtons);
69
70 QHBoxLayout *mainLayout = dynamic_cast<QHBoxLayout *>(layout());
71 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mainLayout, newButton);
72 mainLayout->insertWidget(m_d->numberOfButtons, newButton);
73
74 ++m_d->numberOfButtons;
75
76 return newButton;
77}
78
80{
81 return addButton(QIcon(), text);
82}
83
85{
86 return addButton(QIcon(), QString());
87}
88
90{
91 return dynamic_cast<KoGroupButton *>(m_d->buttonGroup->button(index));
92}
93
95{
97 for (QAbstractButton *b : m_d->buttonGroup->buttons()) {
98 list.append(dynamic_cast<KoGroupButton *>(b));
99 }
100 return list;
101}
102
104{
105 return m_d->buttonGroup->exclusive();
106}
107
109{
110 m_d->buttonGroup->setExclusive(exclusive);
111}
112
114{
115 return dynamic_cast<KoGroupButton *>(m_d->buttonGroup->checkedButton());
116}
117
119{
120 return m_d->buttonGroup->checkedId();
121}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QScopedPointer< Private > m_d
KoGroupButton * checkedButton() const
KisOptionButtonStrip(QWidget *parent=nullptr)
KoGroupButton * button(int index) const
void setExclusive(bool exclusive)
QList< KoGroupButton * > buttons() const
@ GroupLeft
The button is at the left of the group, so it would have rounded the left part.
@ GroupRight
The button is at the right of the group, so it would have rounded the right part.
@ GroupCenter
The button is on the center of the group, so it would have separators on both sides.
void setGroupPosition(KoGroupButton::GroupPosition groupPosition)
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129