|
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 #ifndef LABELEDWIDGET_H |
|
20 #define LABELEDWIDGET_H |
|
21 |
|
22 #include <QLabel> |
|
23 #include <QBoxLayout> |
|
24 |
|
25 // ============================================================================= |
|
26 // LabeledWidget |
|
27 // |
|
28 // Convenience class for a widget with a label beside it. |
|
29 // ============================================================================= |
|
30 template<class R> class LabeledWidget : public QWidget { |
|
31 public: |
|
32 explicit LabeledWidget (const char* labelstr, QWidget* parent = null) : QWidget (parent) { |
|
33 m_widget = new R (this); |
|
34 commonInit (labelstr); |
|
35 } |
|
36 |
|
37 explicit LabeledWidget (const char* labelstr, R* widget, QWidget* parent = null) : |
|
38 QWidget (parent), m_widget (widget) |
|
39 { |
|
40 commonInit (labelstr); |
|
41 } |
|
42 |
|
43 explicit LabeledWidget (QWidget* parent = 0, Qt::WindowFlags f = 0) { |
|
44 m_widget = new R (this); |
|
45 commonInit (""); |
|
46 } |
|
47 |
|
48 R* widget () const { return m_widget; } |
|
49 R* w () const { return m_widget; } |
|
50 QLabel* label () const { return m_label; } |
|
51 QLabel* l () const { return m_label; } |
|
52 void setWidget (R* widget) { m_widget = widget; } |
|
53 void setLabel (QLabel* label) { m_label = label; } |
|
54 operator R* () { return m_widget; } |
|
55 |
|
56 private: |
|
57 Q_DISABLE_COPY (LabeledWidget<R>) |
|
58 |
|
59 void commonInit (const char* labelstr) { |
|
60 m_label = new QLabel (labelstr, this); |
|
61 m_layout = new QHBoxLayout; |
|
62 m_layout->addWidget (m_label); |
|
63 m_layout->addWidget (m_widget); |
|
64 setLayout (m_layout); |
|
65 } |
|
66 |
|
67 R* m_widget; |
|
68 QLabel* m_label; |
|
69 QHBoxLayout* m_layout; |
|
70 }; |
|
71 |
|
72 #endif // LABELEDWIDGET_H |