radiobox.cpp

changeset 183
f1b8cb53d2a2
parent 182
9374fea8f77f
child 184
fae3bc9ce319
equal deleted inserted replaced
182:9374fea8f77f 183:f1b8cb53d2a2
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.h>
20 #include <qradiobutton.h>
21 #include "radiobox.h"
22
23 static 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 dir = makeDirection (orient);
29
30 buttonGroup = new QButtonGroup;
31 currentId = 0;
32 coreLayout = null;
33
34 coreLayout = new QBoxLayout (makeDirection (orient, true));
35 setLayout (coreLayout);
36
37 // Init the first row with a break
38 rowBreak ();
39
40 connect (buttonGroup, SIGNAL (buttonPressed (QAbstractButton*)), this, SLOT (slot_buttonPressed (QAbstractButton*)));
41 connect (buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (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), defaultId (defaultId)
46 {
47 init (orient);
48
49 for (char const* entry : entries)
50 addButton (entry);
51 }
52
53 void RadioBox::rowBreak () {
54 QBoxLayout* newLayout = new QBoxLayout (dir);
55 currentLayout = newLayout;
56 layouts.push_back (newLayout);
57
58 coreLayout->addLayout (newLayout);
59 }
60
61 void RadioBox::addButton (const char* entry) {
62 QRadioButton* button = new QRadioButton (entry);
63 addButton (button);
64 }
65
66 void RadioBox::addButton (QRadioButton* button) {
67 bool const selectThis = (currentId == defaultId);
68
69 objects.push_back (button);
70 buttonGroup->addButton (button, currentId++);
71 currentLayout->addWidget (button);
72
73 if (selectThis)
74 button->setChecked (true);
75 }
76
77 RadioBox& RadioBox::operator<< (QRadioButton* button) {
78 addButton (button);
79 return *this;
80 }
81
82 RadioBox& RadioBox::operator<< (const char* entry) {
83 addButton (entry);
84 return *this;
85 }
86
87 void RadioBox::setCurrentRow (uint row) {
88 currentLayout = layouts[row];
89 }
90
91 void RadioBox::slot_buttonPressed (int btn) {
92 emit sig_buttonPressed (btn);
93 }
94
95 void RadioBox::slot_buttonPressed (QAbstractButton* btn) {
96 emit sig_buttonPressed (btn);
97 }

mercurial