src/widgets.cc

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

mercurial