src/widgets.cc

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

mercurial