/
widget.h
198 lines (160 loc) · 5.58 KB
/
widget.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
/** @file widget.h Base class for widgets.
*
* @defgroup widgets Widget Framework
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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 2 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. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBDENG2_WIDGET_H
#define LIBDENG2_WIDGET_H
#include "../String"
#include "../Event"
#include "../Id"
#include <QList>
namespace de {
class Widget;
class RootWidget;
typedef QList<Widget *> WidgetList;
/**
* Base class for widgets.
* @ingroup widgets
*/
class DENG2_PUBLIC Widget
{
public:
/// Widget that was expected to exist was not found. @ingroup errors
DENG2_ERROR(NotFoundError);
enum Behavior
{
/// Widget is invisible: not drawn. Hidden widgets also receive no events.
Hidden = 0x1,
/// Widget is disabled. The meaning of this is Widget-specific. The
/// Widget will not be given events.
Disabled = 0x2,
/// Widget will only receive events if it has focus.
HandleEventsOnlyWhenFocused = 0x4,
/// Widget cannot be hit by a pointer device.
Unhittable = 0x8,
/// Widget's content will not extend beyoud its boundaries.
ContentClipping = 0x10,
DefaultBehavior = 0
};
Q_DECLARE_FLAGS(Behaviors, Behavior)
typedef WidgetList Children;
public:
Widget(String const &name = "");
virtual ~Widget();
/**
* Returns the automatically generated, unique identifier of the widget.
*/
Id id() const;
String name() const;
void setName(String const &name);
bool hasRoot() const;
RootWidget &root() const;
bool hasFocus() const;
void show(bool doShow = true);
inline void hide() { show(false); }
void enable(bool yes = true) { setBehavior(Disabled, yes? UnsetFlags : SetFlags); }
void disable(bool yes = true) { setBehavior(Disabled, yes? SetFlags : UnsetFlags); }
bool isHidden() const;
bool isVisible() const;
inline bool isEnabled() const { return !behavior().testFlag(Disabled); }
inline bool isDisabled() const { return behavior().testFlag(Disabled); }
/**
* Sets or clears one or more behavior flags.
*
* @param behavior Flags to modify.
* @param operation Operation to perform on the flags.
*/
void setBehavior(Behaviors behavior, FlagOp operation = SetFlags);
Behaviors behavior() const;
/**
* Sets the identifier of the widget that will receive focus when
* a forward focus navigation is requested.
*
* @param name Name of a widget for forwards navigation.
*/
void setFocusNext(String const &name);
/**
* Sets the identifier of the widget that will receive focus when
* a backwards focus navigation is requested.
*
* @param name Name of a widget for backwards navigation.
*/
void setFocusPrev(String const &name);
String focusNext() const;
String focusPrev() const;
/**
* Routines specific types of events to another widget. It will be
* the only one offered those events.
*
* @param types List of event types.
* @param routeTo Widget to route events to. Caller must ensure
* that the widget is not destroyed while the
* routing is in effect.
*/
void setEventRouting(QList<int> const &types, Widget *routeTo);
void clearEventRouting();
bool isEventRouted(int type, Widget *to) const;
// Tree organization.
void clear();
Widget &add(Widget *child);
Widget *remove(Widget &child);
Widget *find(String const &name);
Widget const *find(String const &name) const;
Children children() const;
Widget *parent() const;
// Utilities.
String uniqueName(String const &name) const;
/**
* Arguments for notifyTree() and notifyTreeReversed().
*/
struct NotifyArgs {
enum Result {
Abort,
Continue
};
void (Widget::*notifyFunc)();
bool (Widget::*conditionFunc)() const;
void (Widget::*preNotifyFunc)();
void (Widget::*postNotifyFunc)();
Widget *until;
NotifyArgs(void (Widget::*notify)()) : notifyFunc(notify),
conditionFunc(0), preNotifyFunc(0), postNotifyFunc(0),
until(0) {}
};
NotifyArgs::Result notifyTree(NotifyArgs const &args);
void notifyTreeReversed(NotifyArgs const &args);
bool dispatchEvent(Event const &event, bool (Widget::*memberFunc)(Event const &));
// Events.
virtual void initialize();
virtual void deinitialize();
virtual void viewResized();
virtual void focusGained();
virtual void focusLost();
virtual void update();
virtual void draw();
virtual void preDrawChildren();
virtual void postDrawChildren();
virtual bool handleEvent(Event const &event);
public:
static void setFocusCycle(WidgetList const &order);
private:
DENG2_PRIVATE(d)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Widget::Behaviors)
} // namespace de
#endif // LIBDENG2_WIDGET_H