28 |
28 |
29 #include "radioGroup.h" |
29 #include "radioGroup.h" |
30 |
30 |
31 // ============================================================================= |
31 // ============================================================================= |
32 // |
32 // |
33 RadioGroup::RadioGroup (const QString& title, QWidget* parent) : QGroupBox (title, parent) |
33 RadioGroup::RadioGroup(const QString& title, QWidget* parent) : QGroupBox(title, parent) |
34 { |
34 { |
35 init (Qt::Vertical); |
35 init(Qt::Vertical); |
36 } |
36 } |
37 |
37 |
38 // ============================================================================= |
38 // ============================================================================= |
39 // |
39 // |
40 QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) |
40 QBoxLayout::Direction makeDirection(Qt::Orientation orient, bool invert = false) |
41 { |
41 { |
42 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; |
42 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; |
43 } |
43 } |
44 |
44 |
45 // ============================================================================= |
45 // ============================================================================= |
46 // |
46 // |
47 bool RadioGroup::isChecked (int n) const |
47 bool RadioGroup::isChecked(int n) const |
48 { |
48 { |
49 return m_buttonGroup->checkedId() == n; |
49 return m_buttonGroup->checkedId() == n; |
50 } |
50 } |
51 |
51 |
52 // ============================================================================= |
52 // ============================================================================= |
53 // |
53 // |
54 void RadioGroup::init (Qt::Orientation orient) |
54 void RadioGroup::init(Qt::Orientation orient) |
55 { |
55 { |
56 m_vert = orient == Qt::Vertical; |
56 m_vert = orient == Qt::Vertical; |
57 |
57 |
58 m_buttonGroup = new QButtonGroup; |
58 m_buttonGroup = new QButtonGroup; |
59 m_oldId = m_curId = 0; |
59 m_oldId = m_curId = 0; |
60 m_coreLayout = nullptr; |
60 m_coreLayout = nullptr; |
61 |
61 |
62 m_coreLayout = new QBoxLayout ( (orient == Qt::Vertical) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); |
62 m_coreLayout = new QBoxLayout((orient == Qt::Vertical) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); |
63 setLayout (m_coreLayout); |
63 setLayout(m_coreLayout); |
64 |
64 |
65 // Init the first row with a break |
65 // Init the first row with a break |
66 rowBreak(); |
66 rowBreak(); |
67 |
67 |
68 connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int))); |
68 connect(m_buttonGroup, SIGNAL(buttonPressed(int)), this, SLOT(slot_buttonPressed(int))); |
69 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); |
69 connect(m_buttonGroup, SIGNAL(buttonReleased(int)), this, SLOT(slot_buttonReleased(int))); |
70 } |
70 } |
71 |
71 |
72 // ============================================================================= |
72 // ============================================================================= |
73 // |
73 // |
74 RadioGroup::RadioGroup (const QString& title, QList<char const*> entries, int const defaultId, const Qt::Orientation orient, QWidget* parent) : |
74 RadioGroup::RadioGroup(const QString& title, QList<char const*> entries, int const defaultId, const Qt::Orientation orient, QWidget* parent) : |
75 QGroupBox (title, parent), |
75 QGroupBox(title, parent), |
76 m_defId (defaultId) |
76 m_defId(defaultId) |
77 { |
77 { |
78 init (orient); |
78 init(orient); |
79 m_oldId = m_defId; |
79 m_oldId = m_defId; |
80 |
80 |
81 for (const char* entry : entries) |
81 for (const char* entry : entries) |
82 addButton (entry); |
82 addButton(entry); |
83 } |
83 } |
84 |
84 |
85 // ============================================================================= |
85 // ============================================================================= |
86 // |
86 // |
87 void RadioGroup::rowBreak() |
87 void RadioGroup::rowBreak() |
88 { |
88 { |
89 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); |
89 QBoxLayout* newLayout = new QBoxLayout(m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); |
90 m_currentLayout = newLayout; |
90 m_currentLayout = newLayout; |
91 m_layouts << newLayout; |
91 m_layouts << newLayout; |
92 |
92 |
93 m_coreLayout->addLayout (newLayout); |
93 m_coreLayout->addLayout(newLayout); |
94 } |
94 } |
95 |
95 |
96 // ============================================================================= |
96 // ============================================================================= |
97 // |
97 // |
98 void RadioGroup::addButton (QString entry) |
98 void RadioGroup::addButton(QString entry) |
99 { |
99 { |
100 QRadioButton* button = new QRadioButton (entry); |
100 QRadioButton* button = new QRadioButton(entry); |
101 addButton (button); |
101 addButton(button); |
102 } |
102 } |
103 |
103 |
104 // ============================================================================= |
104 // ============================================================================= |
105 // |
105 // |
106 void RadioGroup::addButton (QRadioButton* button) |
106 void RadioGroup::addButton(QRadioButton* button) |
107 { |
107 { |
108 bool const selectThis = (m_curId == m_defId); |
108 bool const selectThis = (m_curId == m_defId); |
109 |
109 |
110 m_objects << button; |
110 m_objects << button; |
111 m_buttonGroup->addButton (button, m_curId++); |
111 m_buttonGroup->addButton(button, m_curId++); |
112 m_currentLayout->addWidget (button); |
112 m_currentLayout->addWidget(button); |
113 |
113 |
114 if (selectThis) |
114 if (selectThis) |
115 button->setChecked (true); |
115 button->setChecked(true); |
116 } |
116 } |
117 |
117 |
118 // ============================================================================= |
118 // ============================================================================= |
119 // |
119 // |
120 RadioGroup& RadioGroup::operator<< (QRadioButton* button) |
120 RadioGroup& RadioGroup::operator<<(QRadioButton* button) |
121 { |
121 { |
122 addButton (button); |
122 addButton(button); |
123 return *this; |
123 return *this; |
124 } |
124 } |
125 |
125 |
126 // ============================================================================= |
126 // ============================================================================= |
127 // |
127 // |
128 RadioGroup& RadioGroup::operator<< (const char* entry) |
128 RadioGroup& RadioGroup::operator<<(const char* entry) |
129 { |
129 { |
130 addButton (entry); |
130 addButton(entry); |
131 return *this; |
131 return *this; |
132 } |
132 } |
133 |
133 |
134 // ============================================================================= |
134 // ============================================================================= |
135 // |
135 // |
136 void RadioGroup::setCurrentRow (int row) |
136 void RadioGroup::setCurrentRow(int row) |
137 { |
137 { |
138 m_currentLayout = m_layouts[row]; |
138 m_currentLayout = m_layouts[row]; |
139 } |
139 } |
140 |
140 |
141 // ============================================================================= |
141 // ============================================================================= |
145 return m_buttonGroup->checkedId(); |
145 return m_buttonGroup->checkedId(); |
146 } |
146 } |
147 |
147 |
148 // ============================================================================= |
148 // ============================================================================= |
149 // |
149 // |
150 void RadioGroup::setValue (int val) |
150 void RadioGroup::setValue(int val) |
151 { |
151 { |
152 m_buttonGroup->button (val)->setChecked (true); |
152 m_buttonGroup->button(val)->setChecked(true); |
153 } |
153 } |
154 |
154 |
155 // ============================================================================= |
155 // ============================================================================= |
156 // |
156 // |
157 QRadioButton* RadioGroup::operator[] (int n) const |
157 QRadioButton* RadioGroup::operator[](int n) const |
158 { |
158 { |
159 return m_objects[n]; |
159 return m_objects[n]; |
160 } |
160 } |
161 |
161 |
162 // ============================================================================= |
162 // ============================================================================= |
163 // |
163 // |
164 void RadioGroup::slot_buttonPressed (int btn) |
164 void RadioGroup::slot_buttonPressed(int btn) |
165 { |
165 { |
166 emit buttonPressed (btn); |
166 emit buttonPressed(btn); |
167 |
167 |
168 m_oldId = m_buttonGroup->checkedId(); |
168 m_oldId = m_buttonGroup->checkedId(); |
169 } |
169 } |
170 |
170 |
171 // ============================================================================= |
171 // ============================================================================= |
172 // |
172 // |
173 void RadioGroup::slot_buttonReleased (int btn) |
173 void RadioGroup::slot_buttonReleased(int btn) |
174 { |
174 { |
175 emit buttonReleased (btn); |
175 emit buttonReleased(btn); |
176 int newid = m_buttonGroup->checkedId(); |
176 int newid = m_buttonGroup->checkedId(); |
177 |
177 |
178 if (m_oldId != newid) |
178 if (m_oldId != newid) |
179 emit valueChanged (newid); |
179 emit valueChanged(newid); |
180 } |
180 } |
181 |
181 |
182 // ============================================================================= |
182 // ============================================================================= |
183 // |
183 // |
184 RadioGroup::Iterator RadioGroup::begin() |
184 RadioGroup::Iterator RadioGroup::begin() |