src/widgets.cpp

changeset 455
c5d14d112034
parent 421
7d26db0be944
child 473
2a84149fe642
equal deleted inserted replaced
454:d6b4ed3bf169 455:c5d14d112034
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 box useful... find a way to use this in Designer.
20 // I probably need to look into how to make Designer plugins.
21 //
22 // The name is quite confusing too considering there's the check box and radio
23 // button around. This widget is a group of radio buttons.
24 // TODO: rename to RadioGroup, make this usable in Designer
18 25
19 #include <QBoxLayout> 26 #include <QBoxLayout>
20 #include <QRadioButton> 27 #include <QRadioButton>
21 #include <QButtonGroup> 28 #include <QButtonGroup>
22 #include <QCheckBox> 29 #include <QCheckBox>
23 #include <map> 30 #include <map>
24 31
25 #include "widgets.h" 32 #include "widgets.h"
33 #include "build/moc_widgets.cpp"
26 34
35 // =============================================================================
36 // -----------------------------------------------------------------------------
27 RadioBox::RadioBox (const QString& title, QWidget* parent) : QGroupBox (title, parent) { 37 RadioBox::RadioBox (const QString& title, QWidget* parent) : QGroupBox (title, parent) {
28 init (Qt::Vertical); 38 init (Qt::Vertical);
29 } 39 }
30 40
41 // =============================================================================
42 // -----------------------------------------------------------------------------
31 QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) { 43 QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) {
32 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom; 44 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom;
33 } 45 }
34 46
47 // =============================================================================
48 // -----------------------------------------------------------------------------
35 bool RadioBox::isChecked (int n) const { 49 bool RadioBox::isChecked (int n) const {
36 return m_buttonGroup->checkedId() == n; 50 return m_buttonGroup->checkedId() == n;
37 } 51 }
38 52
53 // =============================================================================
54 // -----------------------------------------------------------------------------
39 void RadioBox::init (Qt::Orientation orient) { 55 void RadioBox::init (Qt::Orientation orient) {
40 m_vert = orient == Qt::Vertical; 56 m_vert = orient == Qt::Vertical;
41 57
42 m_buttonGroup = new QButtonGroup; 58 m_buttonGroup = new QButtonGroup;
43 m_oldId = m_curId = 0; 59 m_oldId = m_curId = 0;
51 67
52 connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int))); 68 connect (m_buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int)));
53 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int))); 69 connect (m_buttonGroup, SIGNAL (buttonReleased (int)), this, SLOT (slot_buttonReleased (int)));
54 } 70 }
55 71
72 // =============================================================================
73 // -----------------------------------------------------------------------------
56 RadioBox::RadioBox (const QString& title, initlist<char const*> entries, int const defaultId, 74 RadioBox::RadioBox (const QString& title, initlist<char const*> entries, int const defaultId,
57 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId) 75 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), m_defId (defaultId)
58 { 76 {
59 init (orient); 77 init (orient);
60 m_oldId = m_defId; 78 m_oldId = m_defId;
61 79
62 for (char const* entry : entries) 80 for (char const* entry : entries)
63 addButton (entry); 81 addButton (entry);
64 } 82 }
65 83
84 // =============================================================================
85 // -----------------------------------------------------------------------------
66 void RadioBox::rowBreak() { 86 void RadioBox::rowBreak() {
67 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight); 87 QBoxLayout* newLayout = new QBoxLayout (m_vert ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight);
68 m_currentLayout = newLayout; 88 m_currentLayout = newLayout;
69 m_layouts << newLayout; 89 m_layouts << newLayout;
70 90
71 m_coreLayout->addLayout (newLayout); 91 m_coreLayout->addLayout (newLayout);
72 } 92 }
73 93
94 // =============================================================================
95 // -----------------------------------------------------------------------------
74 void RadioBox::addButton (const char* entry) { 96 void RadioBox::addButton (const char* entry) {
75 QRadioButton* button = new QRadioButton (entry); 97 QRadioButton* button = new QRadioButton (entry);
76 addButton (button); 98 addButton (button);
77 } 99 }
78 100
101 // =============================================================================
102 // -----------------------------------------------------------------------------
79 void RadioBox::addButton (QRadioButton* button) { 103 void RadioBox::addButton (QRadioButton* button) {
80 bool const selectThis = (m_curId == m_defId); 104 bool const selectThis = (m_curId == m_defId);
81 105
82 m_objects << button; 106 m_objects << button;
83 m_buttonGroup->addButton (button, m_curId++); 107 m_buttonGroup->addButton (button, m_curId++);
85 109
86 if (selectThis) 110 if (selectThis)
87 button->setChecked (true); 111 button->setChecked (true);
88 } 112 }
89 113
114 // =============================================================================
115 // -----------------------------------------------------------------------------
90 RadioBox& RadioBox::operator<< (QRadioButton* button) { 116 RadioBox& RadioBox::operator<< (QRadioButton* button) {
91 addButton (button); 117 addButton (button);
92 return *this; 118 return *this;
93 } 119 }
94 120
121 // =============================================================================
122 // -----------------------------------------------------------------------------
95 RadioBox& RadioBox::operator<< (const char* entry) { 123 RadioBox& RadioBox::operator<< (const char* entry) {
96 addButton (entry); 124 addButton (entry);
97 return *this; 125 return *this;
98 } 126 }
99 127
128 // =============================================================================
129 // -----------------------------------------------------------------------------
100 void RadioBox::setCurrentRow (uint row) { 130 void RadioBox::setCurrentRow (uint row) {
101 m_currentLayout = m_layouts[row]; 131 m_currentLayout = m_layouts[row];
102 } 132 }
103 133
134 // =============================================================================
135 // -----------------------------------------------------------------------------
104 int RadioBox::value() const { 136 int RadioBox::value() const {
105 return m_buttonGroup->checkedId(); 137 return m_buttonGroup->checkedId();
106 } 138 }
107 139
140 // =============================================================================
141 // -----------------------------------------------------------------------------
108 void RadioBox::setValue (int val) { 142 void RadioBox::setValue (int val) {
109 m_buttonGroup->button (val)->setChecked (true); 143 m_buttonGroup->button (val)->setChecked (true);
110 } 144 }
111 145
146 // =============================================================================
147 // -----------------------------------------------------------------------------
112 QRadioButton* RadioBox::operator[] (uint n) const { 148 QRadioButton* RadioBox::operator[] (uint n) const {
113 return m_objects[n]; 149 return m_objects[n];
114 } 150 }
115 151
152 // =============================================================================
153 // -----------------------------------------------------------------------------
116 void RadioBox::slot_buttonPressed (int btn) { 154 void RadioBox::slot_buttonPressed (int btn) {
117 emit buttonPressed (btn); 155 emit buttonPressed (btn);
118 156
119 m_oldId = m_buttonGroup->checkedId(); 157 m_oldId = m_buttonGroup->checkedId();
120 } 158 }
121 159
160 // =============================================================================
161 // -----------------------------------------------------------------------------
122 void RadioBox::slot_buttonReleased (int btn) { 162 void RadioBox::slot_buttonReleased (int btn) {
123 emit buttonReleased (btn); 163 emit buttonReleased (btn);
124 int newid = m_buttonGroup->checkedId(); 164 int newid = m_buttonGroup->checkedId();
125 165
126 if (m_oldId != newid) 166 if (m_oldId != newid)
127 emit valueChanged (newid); 167 emit valueChanged (newid);
128 } 168 }
129 169
170 // =============================================================================
171 // -----------------------------------------------------------------------------
130 RadioBox::it RadioBox::begin() { 172 RadioBox::it RadioBox::begin() {
131 return m_objects.begin(); 173 return m_objects.begin();
132 } 174 }
133 175
176 // =============================================================================
177 // -----------------------------------------------------------------------------
134 RadioBox::it RadioBox::end() { 178 RadioBox::it RadioBox::end() {
135 return m_objects.end(); 179 return m_objects.end();
136 } 180 }
137
138 #include "build/moc_widgets.cpp"

mercurial