-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDLabeledWidget.h
More file actions
219 lines (169 loc) · 5.99 KB
/
DLabeledWidget.h
File metadata and controls
219 lines (169 loc) · 5.99 KB
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
216
217
218
219
#pragma once
#ifndef DLABELEDWIDGET_H
#define DLABELEDWIDGET_H
/*****************************************************************************
****************************** I N C L U D E ******************************
****************************************************************************/
#include <QLabel>
#include <QFrame>
#include <QVBoxLayout>
#include <type_traits>
#include <assert.h>
/*****************************************************************************
*
*** template <typename QWidget> class DLabeledWidget
*
* This class wraps a QWidget together with a QLabel to name it. This pattern
* occurs frequently and becomes tedious to keep reimplementing. The original
* use case was for Widgets from the QWT library.
*
* The constructors allow a case where the contained Widget is created using its
* default constuctor (basically setting the Parent to nullptr) and then the
* internal Widget instance is accessed using GetWidget() to set its properties.
* Other variations allow creating the Widget externally using the constructor of
* your choice and passing a pointer to the Widget through the constructor.
*
*****************************************************************************/
// TODO Restrict instantiation to only cases where TWIDGET is derived from base QWidget
template <typename TWIDGET>
class DLabeledWidget : public QFrame
{
static_assert(std::is_base_of_v<QWidget, TWIDGET>, "DLabeledWidget - TWIDGET must be derived from QWidget");
// Q_OBJECT
public:
enum class ELabelPosition {eTop, eBottom};
DLabeledWidget(QWidget* pParent = nullptr) : QFrame(pParent), m_pLabel{nullptr},
m_eLabelPos{ELabelPosition::eBottom}
{
m_pWidget = new TWIDGET(this);
InitLabel();
InitLayout(m_eLabelPos);
return;
}
DLabeledWidget(ELabelPosition eLabelPosition, QWidget* pParent = nullptr)
: QFrame(pParent), m_pLabel{nullptr}, m_eLabelPos{eLabelPosition}
{
m_pWidget = new TWIDGET(this);
InitLabel();
InitLayout(m_eLabelPos);
return;
}
DLabeledWidget(TWIDGET* pWidget, ELabelPosition eLabelPosition, QWidget* pParent = nullptr)
: QFrame(pParent), m_pLabel{nullptr}, m_eLabelPos{eLabelPosition}
{
pWidget->setParent(this);
m_pWidget = pWidget;
InitLabel();
InitLayout(m_eLabelPos);
return;
}
DLabeledWidget(TWIDGET* pWidget, const QString& strLabel, ELabelPosition eLabelPosition, QWidget* pParent = nullptr)
: QFrame(pParent), m_pLabel{nullptr}, m_eLabelPos{eLabelPosition}
{
pWidget->setParent(this);
m_pWidget = pWidget;
InitLabel(strLabel);
InitLayout(m_eLabelPos);
return;
}
DLabeledWidget(const QString& strLabel, ELabelPosition eLabelPosition, QWidget* pParent = nullptr)
: QFrame(pParent), m_pLabel{nullptr}, m_eLabelPos{eLabelPosition}
{
m_pWidget = new TWIDGET(this);
InitLabel(strLabel);
InitLayout(m_eLabelPos);
return;
}
DLabeledWidget(const DLabeledWidget& src) = delete;
DLabeledWidget(const DLabeledWidget&& src) = delete;
~DLabeledWidget() = default;
DLabeledWidget& operator=(const DLabeledWidget& rhs) = delete;
DLabeledWidget& operator=(const DLabeledWidget&& rhs) = delete;
TWIDGET* GetWidget()
{
return (m_pWidget);
}
const TWIDGET* GetWidget() const
{
return (m_pWidget);
}
void SetLabelText(QString strText)
{
m_pLabel->setText(strText);
return;
}
QLabel* GetLabel()
{
return (m_pLabel);
}
const QLabel* GetLabel() const
{
return (m_pLabel);
}
QVBoxLayout* GetLayout()
{
return (m_pLayout);
}
const QVBoxLayout* GetLayout() const
{
return (m_pLayout);
}
ELabelPosition GetLabelPosition() const
{
return(m_eLabelPos);
}
#if 0
void SetLabelPosition(ELabelPosition eLabelPosition)
{
m_eLabelPosition = eLabelPosition;
return;
}
#endif
virtual QSize sizeHint() const override
{
auto LabelHint = m_pLabel->sizeHint();
auto WidgetHint = m_pWidget->sizeHint();
auto Width = std::max(LabelHint.width(), WidgetHint.width());
auto Height = LabelHint.height() + WidgetHint.height();
return (QSize(Width, Height));
}
protected:
TWIDGET* m_pWidget;
QLabel* m_pLabel;
QVBoxLayout* m_pLayout;
ELabelPosition m_eLabelPos;
void SetLabelSizePolicy()
{
if (m_pLabel != nullptr)
{
m_pLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
return;
} // end if
}
void InitLabel(QString strLabel = QString())
{
m_pLabel = new QLabel(strLabel, this);
m_pLabel->setAlignment(Qt::AlignCenter);
SetLabelSizePolicy();
return;
}
void InitLayout(ELabelPosition eLabelPosition)
{
m_pLayout = new QVBoxLayout(this);
m_pLayout->setMargin(0);
m_pLayout->setSpacing(0);
if (m_eLabelPos == ELabelPosition::eTop)
{
m_pLayout->addWidget(m_pLabel);
m_pLayout->addWidget(m_pWidget);
} // end if
else
{
m_pLayout->addWidget(m_pWidget);
m_pLayout->addWidget(m_pLabel);
} // end else
return;
}
private:
}; // end of class DLabeledWidget
#endif // DLABELEDWIDGET_H