Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3e4acc]: libgui / MenuNode.h Maximize Restore History

Download this file

MenuNode.h    325 lines (272 with data), 10.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/***************************************************************************
MenuNode.h - generic menu node type
-------------------
begin : Mon Jan 10 2000
copyright : (C) 2000 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef _MENU_NODE_H_
#define _MENU_NODE_H_
#include "config.h"
#include <QObject>
#include <QHash>
#include <QIcon>
#include <QKeySequence>
#include <QPixmap>
#include <QString>
#include <QStringList>
class QAction;
class MenuSub;
class MenuGroup;
/**
* Base class for the MenuRoot, MenuEntry, SubMenu and
* the ToplevelMenu class.
* @author Thomas Eschenbacher
*/
class MenuNode: public QObject
{
Q_OBJECT
public:
/**
* Constructor.
* @param parent pointer to the node's parent (might be 0)
* @param name the non-localized name of the node
* @param command the command to be sent when the node is
* selected (optional, default=0)
* @param shortcut keyboard shortcut (optional, default=0)
* @param uid unique id string (optional, default=0)
*/
MenuNode(MenuNode *parent,
const QString &name,
const QString &command,
const QKeySequence &shortcut,
const QString &uid);
/**
* Destructor. Clears the menu node and cleans up.
* @see #clear()
*/
virtual ~MenuNode();
/** Returns the (non-localized) name of the node. */
inline const QString &name() const { return m_name; }
/** Returns the command of the node. */
inline const QString &command() const { return m_command; }
/** Returns the corresponding menu action */
virtual QAction *action() { return 0; }
/**
* Returns the unique id string of the node.
*/
inline const QString &uid() const { return m_uid; }
/**
* Sets the unique id string of the node
*/
void setUID(const QString &uid);
/**
* Returns the bitmask of the keyboard shortcut.
*/
inline int shortcut() const { return m_shortcut; }
/**
* Sets the bitmask of the keyboard shortcut.
*/
virtual void setShortcut(const QKeySequence &shortcut) {
m_shortcut = shortcut;
}
/**
* Returns the menu nodes' icon.
*/
virtual const QIcon icon();
/**
* Sets a new icon of a menu node.
* @param icon QIcon with the icon
*/
virtual void setIcon(const QIcon &icon);
/**
* Returns true if the node is a branch, false if it is a leaf.
* (overwritten in MenuSub etc.)
*/
virtual bool isBranch() const { return false; }
/**
* Removes all child entries from the menu node (gui) and
* deletes the MenuNode objects (memory).
*/
virtual void clear();
/** returns a pointer to the menu's parent node */
virtual MenuNode *parentNode();
/**
* Returns true if the node is enabled.
*/
virtual bool isEnabled();
/**
* Enables/disables the current menu node.
* @param enable true to enable the item, false to disable
*/
virtual void setEnabled(bool enable);
/**
* Sets/removes the checkmark from the current menu node.
* @param check true to set the mark, false to remove
*/
virtual void setChecked(bool check);
/**
* Sets the visible text of an item to a new value. (Only useful
* for a MenuItem)
* @param text the new text
*/
virtual void setText(const QString &text);
/**
* Tries to find a menu node by it's unique id string. It descends
* recursively through all child nodes if necessary.
* @param uid the unique id string to be searched
* @return pointer to the found node or 0 if not found
*/
MenuNode *findUID(const QString &uid);
/**
* Tries to find a child node by it's name.
* @param name non-localized name of the child node
* @return pointer to the found node or 0 if not found
*/
MenuNode *findChild(const QString &name);
/**
* Removes a child node of the curren node. If the child
* was not found or is already removed this does nothing.
* @param child pointer to the child node
*/
virtual void removeChild(MenuNode *child);
/**
* Inserts a new branch node into the menu structure. The new node
* normally is (derived from) MenuSub.
* @param name non-localized name of the node
* @param command the command template used for creating commands of
* submenus (leafes) that don't have an own command
* but contain data for their parent's command.
* Should contain a %s that will be replaced by some
* data from a child entry. (this is used for
* menus with data selection lists like "recent files)
* If not used, pass 0.
* @param shortcut keyboard shortcut, 0 if unused
* @param uid unique id string (might be 0)
* @return pointer to the new branch node
*/
virtual MenuSub *insertBranch(const QString &name,
const QString &command,
const QKeySequence &shortcut,
const QString &uid);
/**
* Inserts a new leaf node into the menu structure. The new node
* normally is (derived from) MenuItem.
* @param name non-localized name of the node
* @param command the command to be sent when the node is
* selected (might be 0)
* @param shortcut keyboard shortcut, 0 if unused
* @param uid unique id string (might be 0)
* @return pointer to the new leaf node
*/
virtual MenuNode *insertLeaf(const QString &name,
const QString &command,
const QKeySequence &shortcut,
const QString &uid);
/**
* Registers a node as a child of the current node.
* @param node pointer to the child node
*/
virtual void registerChild(MenuNode *node);
/**
* Inserts a new child node into the structure. If the specified
* position contains a path that doesn't completely exist, all
* missing branches will be appended.
* @param name non-localized name of the first node (might be 0)
* @param position path consiting of several node names separated
* by a '/'. All strings are non-localized.
* @param command the command to be sent when the node is
* selected (might be 0)
* @param shortcut keyboard shortcut, 0 if unused
* @param uid unique id string (might be 0)
*/
virtual void insertNode(const QString &name,
const QString &position,
const QString &command,
const QKeySequence &shortcut,
const QString &uid);
/**
* Converts a child node from leaf to branch type by removing
* the leaf and inserting a branch with the same properties
* instead.
* @param node the child node to be converted
* @return pointer to the new branch node
*/
virtual MenuNode *leafToBranch(MenuNode *node);
/**
* Handles/interpretes special menu commands.
* @param command name of a menu node or command
* @return true if the name was recognized as a command and handled
*/
virtual bool specialCommand(const QString &command);
/**
* Called to notify the node that it has been selected.
*/
virtual void actionSelected();
/**
* Returns a reference to the list of groups. It recursively calls
* all parent node's getGroupList() function until it reaches the
* root node of the menu structure that holds the list of groups and
* overwrites this function.
* @return reference to the list of groups
*/
virtual QHash<QString, MenuGroup *> &getGroupList();
/**
* Adds the node to a group. If it is already a member of the
* group this function will do nothing.
* @param group name of the group
*/
void joinGroup(const QString &group);
/**
* Removes the node from a group (opposite of joinGroup).
* @param group name of the group
*/
void leaveGroup(const QString &group);
protected:
/**
* Returns the address of the root node of the menu structure.
*/
MenuNode *getRootNode();
/**
* Emits a command if the node is the root node. If it is a client
* node it will call the root node's emitCommand() function.
* @see #emitCommand()
* @see #getRootNode()
*/
void emitCommand(const QString &command);
signals:
/**
* Will be emitted if the command of the menu node
* should be executed. It will only be emitted by the
* root node, client nodes call the root node's emitCommand()
* function.
* @see #emitCommand()
* @see #getRootNode()
*/
void sigCommand(const QString &command);
protected:
/** list with pointers to child menus */
QList<MenuNode *> m_children;
/** list of group names the item belongs to */
QStringList m_groups;
private:
/** unique id string */
QString m_uid;
/** bitmask of the keyboard shortcut */
QKeySequence m_shortcut;
/** name of the node (non-localized) */
QString m_name;
/** command to be sent when the node is activated (optional) */
QString m_command;
/** parent of this entry */
MenuNode *m_parentNode;
};
#endif // _MENU_NODE_H_