src/widgets.cpp

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

mercurial