src/radiobox.cpp

changeset 202
a027f6fc6141
parent 201
4d620d819f4f
child 203
ccde5e88f0b6
equal deleted inserted replaced
201:4d620d819f4f 202:a027f6fc6141
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 #include <QBoxLayout>
20 #include <QRadioButton>
21 #include "radiobox.h"
22
23 QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) {
24 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom;
25 }
26
27 void RadioBox::init (Qt::Orientation orient) {
28 m_dir = makeDirection (orient);
29
30 m_buttonGroup = new QButtonGroup;
31 m_oldId = m_curId = 0;
32 m_coreLayout = null;
33
34 m_coreLayout = new QBoxLayout (makeDirection (orient, true));
35 setLayout (m_coreLayout);
36
37 // Init the first row with a break
38 rowBreak ();
39
40 connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int)));
41 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int)));
42 }
43
44 RadioBox::RadioBox (const QString& title, initlist<char const*> entries, int const defaultId,
45 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId)
46 {
47 init (orient);
48 m_oldId = m_defId;
49
50 for (char const* entry : entries)
51 addButton (entry);
52 }
53
54 void RadioBox::rowBreak () {
55 QBoxLayout* newLayout = new QBoxLayout (m_dir);
56 m_currentLayout = newLayout;
57 m_layouts.push_back (newLayout);
58
59 m_coreLayout->addLayout (newLayout);
60 }
61
62 void RadioBox::addButton (const char* entry) {
63 QRadioButton* button = new QRadioButton (entry);
64 addButton (button);
65 }
66
67 void RadioBox::addButton (QRadioButton* button) {
68 bool const selectThis = (m_curId == m_defId);
69
70 m_objects.push_back (button);
71 m_buttonGroup->addButton (button, m_curId++);
72 m_currentLayout->addWidget (button);
73
74 if (selectThis)
75 button->setChecked (true);
76 }
77
78 RadioBox& RadioBox::operator<< (QRadioButton* button) {
79 addButton (button);
80 return *this;
81 }
82
83 RadioBox& RadioBox::operator<< (const char* entry) {
84 addButton (entry);
85 return *this;
86 }
87
88 void RadioBox::setCurrentRow (uint row) {
89 m_currentLayout = m_layouts[row];
90 }
91
92 void RadioBox::slot_buttonPressed (int btn) {
93 emit sig_buttonPressed (btn);
94
95 m_oldId = m_buttonGroup->checkedId ();
96 }
97
98 void RadioBox::slot_buttonReleased (int btn) {
99 emit buttonReleased (btn);
100 int newid = m_buttonGroup->checkedId ();
101
102 if (m_oldId != newid)
103 emit valueChanged (newid);
104 }

mercurial