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