14 * |
14 * |
15 * You should have received a copy of the GNU General Public License |
15 * You should have received a copy of the GNU General Public License |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 |
18 |
19 // I still find the radio box useful... find a way to use this in Designer. |
19 // I still find the radio group useful... find a way to use this in Designer. |
20 // I probably need to look into how to make Designer plugins. |
20 // I probably need to look into how to make Designer plugins. |
21 // |
21 // TODO: try make this usable in Designer |
22 // The name is quite confusing too considering there's the check box and radio |
|
23 // button around. This widget is a group of radio buttons. |
|
24 // TODO: rename to RadioGroup, make this usable in Designer |
|
25 |
22 |
26 #include <QBoxLayout> |
23 #include <QBoxLayout> |
27 #include <QRadioButton> |
24 #include <QRadioButton> |
28 #include <QButtonGroup> |
25 #include <QButtonGroup> |
29 #include <QCheckBox> |
26 #include <QCheckBox> |
43 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; |
40 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; |
44 } |
41 } |
45 |
42 |
46 // ============================================================================= |
43 // ============================================================================= |
47 // ----------------------------------------------------------------------------- |
44 // ----------------------------------------------------------------------------- |
48 bool RadioBox::isChecked (int n) const { |
45 bool RadioGroup::isChecked (int n) const { |
49 return m_buttonGroup->checkedId() == n; |
46 return m_buttonGroup->checkedId() == n; |
50 } |
47 } |
51 |
48 |
52 // ============================================================================= |
49 // ============================================================================= |
53 // ----------------------------------------------------------------------------- |
50 // ----------------------------------------------------------------------------- |
54 void RadioBox::init (Qt::Orientation orient) { |
51 void RadioGroup::init (Qt::Orientation orient) { |
55 m_vert = orient == Qt::Vertical; |
52 m_vert = orient == Qt::Vertical; |
56 |
53 |
57 m_buttonGroup = new QButtonGroup; |
54 m_buttonGroup = new QButtonGroup; |
58 m_oldId = m_curId = 0; |
55 m_oldId = m_curId = 0; |
59 m_coreLayout = null; |
56 m_coreLayout = null; |
68 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); |
65 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); |
69 } |
66 } |
70 |
67 |
71 // ============================================================================= |
68 // ============================================================================= |
72 // ----------------------------------------------------------------------------- |
69 // ----------------------------------------------------------------------------- |
73 RadioBox::RadioBox (const QString& title, initlist<char const*> entries, int const defaultId, |
70 RadioGroup::RadioGroup (const QString& title, initlist<char const*> entries, int const defaultId, |
74 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId) |
71 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId) |
75 { |
72 { |
76 init (orient); |
73 init (orient); |
77 m_oldId = m_defId; |
74 m_oldId = m_defId; |
78 |
75 |
80 addButton (entry); |
77 addButton (entry); |
81 } |
78 } |
82 |
79 |
83 // ============================================================================= |
80 // ============================================================================= |
84 // ----------------------------------------------------------------------------- |
81 // ----------------------------------------------------------------------------- |
85 void RadioBox::rowBreak() { |
82 void RadioGroup::rowBreak() { |
86 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); |
83 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); |
87 m_currentLayout = newLayout; |
84 m_currentLayout = newLayout; |
88 m_layouts << newLayout; |
85 m_layouts << newLayout; |
89 |
86 |
90 m_coreLayout->addLayout (newLayout); |
87 m_coreLayout->addLayout (newLayout); |
91 } |
88 } |
92 |
89 |
93 // ============================================================================= |
90 // ============================================================================= |
94 // ----------------------------------------------------------------------------- |
91 // ----------------------------------------------------------------------------- |
95 void RadioBox::addButton (const char* entry) { |
92 void RadioGroup::addButton (const char* entry) { |
96 QRadioButton* button = new QRadioButton (entry); |
93 QRadioButton* button = new QRadioButton (entry); |
97 addButton (button); |
94 addButton (button); |
98 } |
95 } |
99 |
96 |
100 // ============================================================================= |
97 // ============================================================================= |
101 // ----------------------------------------------------------------------------- |
98 // ----------------------------------------------------------------------------- |
102 void RadioBox::addButton (QRadioButton* button) { |
99 void RadioGroup::addButton (QRadioButton* button) { |
103 bool const selectThis = (m_curId == m_defId); |
100 bool const selectThis = (m_curId == m_defId); |
104 |
101 |
105 m_objects << button; |
102 m_objects << button; |
106 m_buttonGroup->addButton (button, m_curId++); |
103 m_buttonGroup->addButton (button, m_curId++); |
107 m_currentLayout->addWidget (button); |
104 m_currentLayout->addWidget (button); |
110 button->setChecked (true); |
107 button->setChecked (true); |
111 } |
108 } |
112 |
109 |
113 // ============================================================================= |
110 // ============================================================================= |
114 // ----------------------------------------------------------------------------- |
111 // ----------------------------------------------------------------------------- |
115 RadioBox& RadioBox::operator<< (QRadioButton* button) { |
112 RadioGroup& RadioGroup::operator<< (QRadioButton* button) { |
116 addButton (button); |
113 addButton (button); |
117 return *this; |
114 return *this; |
118 } |
115 } |
119 |
116 |
120 // ============================================================================= |
117 // ============================================================================= |
121 // ----------------------------------------------------------------------------- |
118 // ----------------------------------------------------------------------------- |
122 RadioBox& RadioBox::operator<< (const char* entry) { |
119 RadioGroup& RadioGroup::operator<< (const char* entry) { |
123 addButton (entry); |
120 addButton (entry); |
124 return *this; |
121 return *this; |
125 } |
122 } |
126 |
123 |
127 // ============================================================================= |
124 // ============================================================================= |
128 // ----------------------------------------------------------------------------- |
125 // ----------------------------------------------------------------------------- |
129 void RadioBox::setCurrentRow (uint row) { |
126 void RadioGroup::setCurrentRow (uint row) { |
130 m_currentLayout = m_layouts[row]; |
127 m_currentLayout = m_layouts[row]; |
131 } |
128 } |
132 |
129 |
133 // ============================================================================= |
130 // ============================================================================= |
134 // ----------------------------------------------------------------------------- |
131 // ----------------------------------------------------------------------------- |
135 int RadioBox::value() const { |
132 int RadioGroup::value() const { |
136 return m_buttonGroup->checkedId(); |
133 return m_buttonGroup->checkedId(); |
137 } |
134 } |
138 |
135 |
139 // ============================================================================= |
136 // ============================================================================= |
140 // ----------------------------------------------------------------------------- |
137 // ----------------------------------------------------------------------------- |
141 void RadioBox::setValue (int val) { |
138 void RadioGroup::setValue (int val) { |
142 m_buttonGroup->button (val)->setChecked (true); |
139 m_buttonGroup->button (val)->setChecked (true); |
143 } |
140 } |
144 |
141 |
145 // ============================================================================= |
142 // ============================================================================= |
146 // ----------------------------------------------------------------------------- |
143 // ----------------------------------------------------------------------------- |
147 QRadioButton* RadioBox::operator[] (uint n) const { |
144 QRadioButton* RadioGroup::operator[] (uint n) const { |
148 return m_objects[n]; |
145 return m_objects[n]; |
149 } |
146 } |
150 |
147 |
151 // ============================================================================= |
148 // ============================================================================= |
152 // ----------------------------------------------------------------------------- |
149 // ----------------------------------------------------------------------------- |
153 void RadioBox::slot_buttonPressed (int btn) { |
150 void RadioGroup::slot_buttonPressed (int btn) { |
154 emit buttonPressed (btn); |
151 emit buttonPressed (btn); |
155 |
152 |
156 m_oldId = m_buttonGroup->checkedId(); |
153 m_oldId = m_buttonGroup->checkedId(); |
157 } |
154 } |
158 |
155 |
159 // ============================================================================= |
156 // ============================================================================= |
160 // ----------------------------------------------------------------------------- |
157 // ----------------------------------------------------------------------------- |
161 void RadioBox::slot_buttonReleased (int btn) { |
158 void RadioGroup::slot_buttonReleased (int btn) { |
162 emit buttonReleased (btn); |
159 emit buttonReleased (btn); |
163 int newid = m_buttonGroup->checkedId(); |
160 int newid = m_buttonGroup->checkedId(); |
164 |
161 |
165 if (m_oldId != newid) |
162 if (m_oldId != newid) |
166 emit valueChanged (newid); |
163 emit valueChanged (newid); |
167 } |
164 } |
168 |
165 |
169 // ============================================================================= |
166 // ============================================================================= |
170 // ----------------------------------------------------------------------------- |
167 // ----------------------------------------------------------------------------- |
171 RadioBox::it RadioBox::begin() { |
168 RadioGroup::it RadioGroup::begin() { |
172 return m_objects.begin(); |
169 return m_objects.begin(); |
173 } |
170 } |
174 |
171 |
175 // ============================================================================= |
172 // ============================================================================= |
176 // ----------------------------------------------------------------------------- |
173 // ----------------------------------------------------------------------------- |
177 RadioBox::it RadioBox::end() { |
174 RadioGroup::it RadioGroup::end() { |
178 return m_objects.end(); |
175 return m_objects.end(); |
179 } |
176 } |