-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrolgroup.h
215 lines (190 loc) · 7.83 KB
/
controlgroup.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/******************************************************************************
**
** bits - an application to arithmeticall and logically combine and
** manipulate bit patterns and their corresponding hex numbers.
**
** Tony Camuso
** Created December, 2011
**
** bits (bitview) is free software. You can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** GNU General Public License http://www.gnu.org/licenses/gpl.html
**
** Copyright (c) 2011-2014 by Tony Camuso.
**
******************************************************************************/
#ifndef CONTROLGROUP_H
#define CONTROLGROUP_H
//#include <QtCore>
//#include <QtGui>
#include <QtCore/QSignalMapper>
#include <QtCore/QStringBuilder>
#if QT_VERSION < 0x50000
#include <QtGui/QWidget>
#include <QtGui/QRadioButton>
#include <QtGui/QButtonGroup>
#endif
#if QT_VERSION >= 0x50000
#include <QWidget>
#include <QRadioButton>
#include <QButtonGroup>
#endif
// Debugging headers
//
//#include <QtCore/QtGlobal> // qPrintable() - be careful with this, see docs
//#include <QtCore/QtDebug> // qDebug()
enum signal_t {st_clicked, st_pressed, st_released, st_activated};
typedef void (*pSlot_t)(int);
/*
** class Twidget
**
** This class is only used by instances of the ControlGroup template class
** for creation and management of the QWidget based objects that it creates.
**
** Because we don't know the type of the objects that will be created by
** ControlGroup, we use a class template and pass the object type to be
** expanded by the compliler when the template is invoked.
**
** The QList of Objects will be maintained by each instance of ControlGroup
** for the object types that it creates.
**
** These objects must all have QWidget as their base class, since ControlGroup
** will operate on member functions from that class.
**
** The following fields must be initialized by the caller.
**
** objName - Object name stem. The index of each object in the list will
** be added to the name for each object
** objCount - the number of objects to be created
** objText - QList of strings to be displayed on each object.
** sizes - QList of QSize gives height & width of each object in pixels.
** A single-item list means they are all the same size
** layout - QList of QPoint for the location of each object in pixels.
** grouped - if true, controls will be mutually exclusive
** labelText - QList of QStrings to be displayed on each label.
** An empty list indicates no labels.
** labelSizes - QList of QSize for height and width of each label
** A single-item list means they are all the same size
** labelLayout - QList of QPoint for the label layout
** slotList - QList of function pointers for the caller's slots
** (Not implemented yet)
**
** These fields will be initialized by the ControlGroup.
**
** labelList - QList of QLabel created by this class
** mapper - The signal mapper for these objects
** butonGroup - The place where these objects will be linked
** together as a group.
**
*/
class Twidget
{
public:
// These fields are must be initialized by the caller to ControlGroup
//
QString objName; // object name stem of widget
QList <QString> objText; // text displayed on objects,
QList <QSize> sizes; // A list of sizes for the objects
QList <QPoint> layout; // A list of locations for the objects
bool grouped; // group these in a QButtonGroup
QList <QString> labelText; // Text displayed labels
QList <QSize> labelSizes; // list of sizes for the labels
QList <QPoint> labelLayout; // A list of locations for the objects
QList <pSlot_t> slotList; // list of the caller's slot function
// : pointers
// These fields will be initialized by ControlGroup
//
QList <QLabel> *labelList; // List of QLabel objects
QSignalMapper *mapper; // The signal mapper for these objects
QButtonGroup *buttonGroup; // The place where these objects will be
// : linked together as a group.
};
template <typename T>
class ControlGroup : public QWidget
{
public:
explicit ControlGroup( Twidget *tw, QWidget *parent=0 )
{
init(tw, parent);
}
ControlGroup(){};
~ControlGroup(){};
QList<T*> widgetList;
void init( Twidget *tw, QWidget *parent);
Twidget* getTwidget() {return m_tw;}
private:
Twidget *m_tw;
QWidget *m_parent;
};
template <typename T>
void ControlGroup<T>::init( Twidget *tw, QWidget *parent )
{
m_tw = tw;
m_parent = parent;
// See if the caller wants a QButtonGroup. That would make these buttons
// mutually exclusive.
// The QButtonGroup emits a signal with the ID of the checked button for
// each button that is checked in an exclusive group.
//
if(tw->grouped)
tw->buttonGroup = new QButtonGroup(parent);
else
tw->mapper = new QSignalMapper(parent);
int objCount = tw->layout.size();
for (int index = 0; index < objCount; index++)
{
T *object = new T(parent);
widgetList.append((T *)object);
// Create the objectName, combining the text passed in a QString by the
// caller and the index of the object in the QList tw->layout.size.
//
object->setObjectName(QString(tw->objName % QString::number(index)));
// If the caller only passes one label text string, then be sure that
// we can only index to the zeroth element of the tw->objText QList.
//
int textIndex = (tw->objText.size()) == 1 ? 0 : index;
QString objText = tw->objText[textIndex];
object->setText(objText);
//qDebug() << "textIndex: " << textIndex << " objText: "
// << qPrintable(objText);
// If the caller only passes one size, then one size fits all.
// Be sure that we can only index to the zeroth element of the
// tw->sizes QList. When there is only one item in there, trying
// to index beyond that throws an access violation, as well it
// should.
//
int sizeIndex = (tw->sizes.size() == 1) ? 0 : index;
int width = tw->sizes[sizeIndex].width();
int height = tw->sizes[sizeIndex].height();
// Layout the objects according to the Table Configuration given by
// the "topology" field in the Twidget struct.
//
int x = tw->layout[index].x();
int y = tw->layout[index].y();
object->setGeometry(x, y, width, height);
// If the buttons are grouped, the buttonGroup will send signals.
// In this case, the mapper only needs to be programmed at the end of
// this loop.
// If they are not grouped, the buttons themselves must notify the
// mapper, and the mapper must be programmed to recognize each button
// in the group.
//
if(tw->grouped)
tw->buttonGroup->addButton(object, index);
else
{
connect(object, SIGNAL(clicked()), tw->mapper, SLOT(map()));
tw->mapper->setMapping(object, index);
}
//tw->labelList.append(new QLabel(parent));
}
}
#endif // CONTROLGROUP_H