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,
41 [this](QAbstractButton *button, bool checked) {
42 Q_EMIT buttonToggled(dynamic_cast<KoGroupButton *>(button), checked);
43 Q_EMIT buttonToggled(m_d->buttonGroup->id(button), checked);
44 });
45}
46
50
52 const QString &text)
53{
54 KoGroupButton *newButton = new KoGroupButton(this);
55 newButton->setCheckable(true);
56 newButton->setIcon(icon);
57 newButton->setText(text);
58 newButton->setMinimumSize(28, 28);
59
60 if (m_d->numberOfButtons > 0) {
61 KoGroupButton *prevButton = dynamic_cast<KoGroupButton *>(
62 m_d->buttonGroup->button(m_d->numberOfButtons - 1));
63 prevButton->setGroupPosition(m_d->numberOfButtons == 1
67 }
68
69 m_d->buttonGroup->addButton(newButton, m_d->numberOfButtons);
70
71 QHBoxLayout *mainLayout = dynamic_cast<QHBoxLayout *>(layout());
72 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mainLayout, newButton);
73 mainLayout->insertWidget(m_d->numberOfButtons, newButton);
74
75 ++m_d->numberOfButtons;
76
77 return newButton;
78}
79
81{
82 return addButton(QIcon(), text);
83}
84
86{
87 return addButton(QIcon(), QString());
88}
89
91{
92 return dynamic_cast<KoGroupButton *>(m_d->buttonGroup->button(index));
93}
94
96{
98 for (QAbstractButton *b : m_d->buttonGroup->buttons()) {
99 list.append(dynamic_cast<KoGroupButton *>(b));
100 }
101 return list;
102}
103
105{
106 return m_d->buttonGroup->exclusive();
107}
108
110{
111 m_d->buttonGroup->setExclusive(exclusive);
112}
113
115{
116 return dynamic_cast<KoGroupButton *>(m_d->buttonGroup->checkedButton());
117}
118
120{
121 return m_d->buttonGroup->checkedId();
122}
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