1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2015 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
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/>. |
|
17 */ |
|
18 |
|
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. |
|
21 // TODO: try make this usable in Designer |
|
22 |
|
23 #include <QBoxLayout> |
|
24 #include <QRadioButton> |
|
25 #include <QButtonGroup> |
|
26 #include <QCheckBox> |
|
27 #include <map> |
|
28 |
|
29 #include "radioGroup.h" |
|
30 |
|
31 // ============================================================================= |
|
32 // |
|
33 RadioGroup::RadioGroup (const QString& title, QWidget* parent) : QGroupBox (title, parent) |
|
34 { |
|
35 init (Qt::Vertical); |
|
36 } |
|
37 |
|
38 // ============================================================================= |
|
39 // |
|
40 QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) |
|
41 { |
|
42 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; |
|
43 } |
|
44 |
|
45 // ============================================================================= |
|
46 // |
|
47 bool RadioGroup::isChecked (int n) const |
|
48 { |
|
49 return m_buttonGroup->checkedId() == n; |
|
50 } |
|
51 |
|
52 // ============================================================================= |
|
53 // |
|
54 void RadioGroup::init (Qt::Orientation orient) |
|
55 { |
|
56 m_vert = orient == Qt::Vertical; |
|
57 |
|
58 m_buttonGroup = new QButtonGroup; |
|
59 m_oldId = m_curId = 0; |
|
60 m_coreLayout = null; |
|
61 |
|
62 m_coreLayout = new QBoxLayout ( (orient == Qt::Vertical) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); |
|
63 setLayout (m_coreLayout); |
|
64 |
|
65 // Init the first row with a break |
|
66 rowBreak(); |
|
67 |
|
68 connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int))); |
|
69 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); |
|
70 } |
|
71 |
|
72 // ============================================================================= |
|
73 // |
|
74 RadioGroup::RadioGroup (const QString& title, QList<char const*> entries, int const defaultId, const Qt::Orientation orient, QWidget* parent) : |
|
75 QGroupBox (title, parent), |
|
76 m_defId (defaultId) |
|
77 { |
|
78 init (orient); |
|
79 m_oldId = m_defId; |
|
80 |
|
81 for (const char* entry : entries) |
|
82 addButton (entry); |
|
83 } |
|
84 |
|
85 // ============================================================================= |
|
86 // |
|
87 void RadioGroup::rowBreak() |
|
88 { |
|
89 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); |
|
90 m_currentLayout = newLayout; |
|
91 m_layouts << newLayout; |
|
92 |
|
93 m_coreLayout->addLayout (newLayout); |
|
94 } |
|
95 |
|
96 // ============================================================================= |
|
97 // |
|
98 void RadioGroup::addButton (const char* entry) |
|
99 { |
|
100 QRadioButton* button = new QRadioButton (entry); |
|
101 addButton (button); |
|
102 } |
|
103 |
|
104 // ============================================================================= |
|
105 // |
|
106 void RadioGroup::addButton (QRadioButton* button) |
|
107 { |
|
108 bool const selectThis = (m_curId == m_defId); |
|
109 |
|
110 m_objects << button; |
|
111 m_buttonGroup->addButton (button, m_curId++); |
|
112 m_currentLayout->addWidget (button); |
|
113 |
|
114 if (selectThis) |
|
115 button->setChecked (true); |
|
116 } |
|
117 |
|
118 // ============================================================================= |
|
119 // |
|
120 RadioGroup& RadioGroup::operator<< (QRadioButton* button) |
|
121 { |
|
122 addButton (button); |
|
123 return *this; |
|
124 } |
|
125 |
|
126 // ============================================================================= |
|
127 // |
|
128 RadioGroup& RadioGroup::operator<< (const char* entry) |
|
129 { |
|
130 addButton (entry); |
|
131 return *this; |
|
132 } |
|
133 |
|
134 // ============================================================================= |
|
135 // |
|
136 void RadioGroup::setCurrentRow (int row) |
|
137 { |
|
138 m_currentLayout = m_layouts[row]; |
|
139 } |
|
140 |
|
141 // ============================================================================= |
|
142 // |
|
143 int RadioGroup::value() const |
|
144 { |
|
145 return m_buttonGroup->checkedId(); |
|
146 } |
|
147 |
|
148 // ============================================================================= |
|
149 // |
|
150 void RadioGroup::setValue (int val) |
|
151 { |
|
152 m_buttonGroup->button (val)->setChecked (true); |
|
153 } |
|
154 |
|
155 // ============================================================================= |
|
156 // |
|
157 QRadioButton* RadioGroup::operator[] (int n) const |
|
158 { |
|
159 return m_objects[n]; |
|
160 } |
|
161 |
|
162 // ============================================================================= |
|
163 // |
|
164 void RadioGroup::slot_buttonPressed (int btn) |
|
165 { |
|
166 emit buttonPressed (btn); |
|
167 |
|
168 m_oldId = m_buttonGroup->checkedId(); |
|
169 } |
|
170 |
|
171 // ============================================================================= |
|
172 // |
|
173 void RadioGroup::slot_buttonReleased (int btn) |
|
174 { |
|
175 emit buttonReleased (btn); |
|
176 int newid = m_buttonGroup->checkedId(); |
|
177 |
|
178 if (m_oldId != newid) |
|
179 emit valueChanged (newid); |
|
180 } |
|
181 |
|
182 // ============================================================================= |
|
183 // |
|
184 RadioGroup::Iterator RadioGroup::begin() |
|
185 { |
|
186 return m_objects.begin(); |
|
187 } |
|
188 |
|
189 // ============================================================================= |
|
190 // |
|
191 RadioGroup::Iterator RadioGroup::end() |
|
192 { |
|
193 return m_objects.end(); |
|
194 } |
|