summaryrefslogtreecommitdiffstats
path: root/extra/kde330/kaccelaction.h
blob: 024292d6b21096d95b83eac9b53b7c2131757b19 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/* This file is part of the KDE libraries
    Copyright (C) 2001,2002 Ellis Whitehead <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef _KACCELACTION_H
#define _KACCELACTION_H

#include <tqmap.h>
#include <tqptrvector.h>
#include <tqstring.h>
#include <tqvaluevector.h>

#include <kshortcut.h>

class KAccelBase;

class TQObject;
class KConfig;
class KConfigBase;

/**
 * @internal
 * A KAccelAction prepresents an action that can be executed using 
 * an accelerator key. Each KAccelAction has a name, a label, a 
 * "What's this" string and a KShortcut. The user can configure and 
 * enable/disable them using KKeyDialog. 
 *
 * \code
 *	1) KAccelAction = "Run Command"
 *		Default3 = "Alt+F2"
 *		Default4 = "Meta+Enter;Alt+F2"
 *		1) KShortcut = "Meta+Enter"
 *			1) KKeySequence = "Meta+Enter"
 *				1) KKey = "Meta+Enter"
 *					1) Meta+Enter
 *					2) Meta+Keypad_Enter
 *		2) KShortcut = "Alt+F2"
 *			1) KKeySequence = "Alt+F2"
 *				1) Alt+F2
 *	2) KAccelAction = "Something"
 *		Default3 = ""
 *		Default4 = ""
 *		1) KShortcut = "Meta+X,Asterisk"
 *			1) KKeySequence = "Meta+X,Asterisk"
 *				1) KKey = "Meta+X"
 *					1) Meta+X
 *				2) KKey = "Asterisk"
 *					1) Shift+8 (English layout)
 *					2) Keypad_Asterisk
 * \endcode
 * @short An accelerator action
 * @see KAccel
 * @see TDEGlobalAccel
 * @see KKeyChooser
 * @see KKeyDialog
 */
class KAccelAction
{
 public:
        /**
	 * Creates an empty KAccelAction.
	 * @see clear()
	 */
	KAccelAction();

	/**
	 * Copy constructor.
	 */
	KAccelAction( const KAccelAction& );

	/**
	 * Creates a new KAccelAction.
	 * @param sName the name of the accelerator
	 * @param sLabel the label of the accelerator (i18n!)
	 * @param sWhatsThis the What's This text (18n!)
	 * @param cutDef3 the default shortcut for 3 modifier systems
	 * @param cutDef4 the default shortcut for 4 modifier systems
	 * @param pObjSlot the receiver of a signal when the key has been 
	 *                 pressed
	 * @param psMethodSlot the slot to connect for key presses. Receives
	 *                     an int, as set by setID(), as only argument
	 * @param bConfigurable if true the user can configure the shortcut
	 * @param bEnabled true if the accelerator should be enabled
	 */
	KAccelAction( const TQString& sName, const TQString& sLabel, const TQString& sWhatsThis,
			const KShortcut& cutDef3, const KShortcut& cutDef4,
			const TQObject* pObjSlot, const char* psMethodSlot,
			bool bConfigurable, bool bEnabled );
	~KAccelAction();

	/**
	 * Clears the accelerator.
	 */
	void clear();

	/**
	 * Re-initialized the KAccelAction.
	 * @param sName the name of the accelerator
	 * @param sLabel the label of the accelerator (i18n!)
	 * @param sWhatsThis the What's This text (18n!)
	 * @param cutDef3 the default shortcut for 3 modifier systems
	 * @param cutDef4 the default shortcut for 4 modifier systems
	 * @param pObjSlot the receiver of a signal when the key has been 
	 *                 pressed
	 * @param psMethodSlot the slot to connect for key presses. Receives
	 *                     an int, as set by setID(), as only argument
	 * @param bConfigurable if true the user can configure the shortcut
	 * @param bEnabled true if the accelerator should be enabled
	 * @return true if successful, false otherwise
	 */
	bool init( const TQString& sName, const TQString& sLabel, const TQString& sWhatsThis,
			const KShortcut& cutDef3, const KShortcut& cutDef4,
			const TQObject* pObjSlot, const char* psMethodSlot,
			bool bConfigurable, bool bEnabled );

	/**
	 * Copies this KAccelAction.
	 */
	KAccelAction& operator=( const KAccelAction& );

	/**
	 * Returns the name of the accelerator action.
	 * @return the name of the accelerator action, can be null if not 
	 *         set
	 */
	const TQString& name() const                { return m_sName; }

	/**
	 * Returns the label of the accelerator action.
	 * @return the label of the accelerator action, can be null if
	 *         not set
	 */
	const TQString& label() const               { return m_sLabel; }

	/**
	 * Returns the What's This text of the accelerator action.
	 * @return the What's This text of the accelerator action, can be
	 *         null if not set
	 */
	const TQString& whatsThis() const           { return m_sWhatsThis; }
	
	/**
	 * The shortcut that is actually used (may be used configured).
	 * @return the shortcut of the KAccelAction, can be null if not set
	 * @see shortcutDefault()
	 */
	const KShortcut& shortcut() const          { return m_cut; }

	/**
	 * The default shortcut for this system.
	 * @return the default shortcut on this system, can be null if not set
	 * @see shortcut()
	 * @see shortcutDefault3()
	 * @see shortcutDefault4()
	 */
	const KShortcut& shortcutDefault() const;

	/**
	 * The default shortcut for 3 modifier systems.
	 * @return the default shortcut for 3 modifier systems, can be null
	 *           if not set
	 * @see shortcutDefault()
	 * @see shortcutDefault4()
	 * @see useFourModifierKeys()
	 */
	const KShortcut& shortcutDefault3() const  { return m_cutDefault3; }

	/**
	 * The default shortcut for 4 modifier systems.
	 * @return the default shortcut for 4 modifier systems, can be null 
	 *         if not set
	 * @see shortcutDefault()
	 * @see shortcutDefault3()
	 * @see useFourModifierKeys()
	 */
	const KShortcut& shortcutDefault4() const  { return m_cutDefault4; }

	/**
	 * Returns the receiver of signals.
	 * @return the receiver of signals (can be 0 if not set)
	 */
	const TQObject* objSlotPtr() const          { return m_pObjSlot; }

	/**
	 * Returns the slot for the signal.
	 * @return the slot for the signal
	 */
	const char* methodSlotPtr() const          { return m_psMethodSlot; }

	/**
	 * Checks whether the user can configure the action.
	 * @return true if configurable, false otherwise
	 */
	bool isConfigurable() const                { return m_bConfigurable; }

	/**
	 * Checks whether the action is enabled.
	 * @return true if enabled, false otherwise
	 */
	bool isEnabled() const                     { return m_bEnabled; }

	/**
	 * Sets the name of the accelerator action.
	 * @param name the new name
	 */
	void setName( const TQString& name );

	/**
	 * Sets the user-readable label of the accelerator action.
	 * @param label the new label (i18n!)
	 */
	void setLabel( const TQString& label );

	/**
	 * Sets the What's This text for the accelerator action.
	 * @param whatsThis the new What's This text (i18n!)
	 */
	void setWhatsThis( const TQString& whatsThis );

	/**
	 * Sets the new shortcut of the accelerator action.
	 * @param rgCuts the shortcut to set
	 * @return true if successful, false otherwise
	 */
	bool setShortcut( const KShortcut& rgCuts );

	/**
	 * Sets the slot of the accelerator action.
	 * @param pObjSlot the receiver object of the signal
	 * @param psMethodSlot the slot for the signal
	 */
	void setSlot( const TQObject* pObjSlot, const char* psMethodSlot );

	/**
	 * Enables or disabled configuring the action.
	 * @param configurable true to enable configurability, false to disable
	 */
	void setConfigurable( bool configurable );

	/**
	 * Enables or disabled the action.
	 * @param configurable true to enable the action, false to disable
	 */
	void setEnabled( bool enable );

	/**
	 * Retrieves the id set using setID.
	 * @return the id of the accelerator action
	 */
	int getID() const   { return m_nIDAccel; }

	/**
	 * Allows you to set an id that will be used as the action 
	 * signal's argument.
	 *
	 * @param n the new id
	 * @see getID()
	 */
	void setID( int n ) { m_nIDAccel = n; }

	/**
	 * Checkes whether the action is connected (emits signals).
	 * @return true if connected, false otherwise
	 */
	bool isConnected() const;

	/**
	 * Sets a key sequence of the action's shortcut.
	 * @param i the position of the sequence
	 * @param keySeq the new new sequence
	 * @return true if successful, false otherwise
	 * @see KShortcut::setSeq()
	 */
	bool setKeySequence( uint i, const KKeySequence &keySeq );
	
	/**
	 * Clears the action's shortcut. It will not contain any sequences after
	 * calling this method.
	 * @see KShortcut::clear()
	 */
	void clearShortcut();
	
	/**
	 * Checks whether the action's shortcut contains the given key sequence.
	 * @param keySeq the key sequence to check
	 * @return true if the shortcut contains the given sequence
	 * @see KShortcut::contains()
	 */
	bool contains( const KKeySequence &keySeq );

	/**
	 * Returns the string representation of the action's shortcut.
	 * @return the string representation of the action's shortcut.
	 * @see KShortcut::toString()
	 */
	TQString toString() const;

	/**
	 * @internal
	 */
	TQString toStringInternal() const;

	/**
	 * Returns true if four modifier keys will be used.
	 * @return true if four modifier keys will be used.
	 */
	static bool useFourModifierKeys();

	/**
	 * Selects 3 or 4 modifier default shortcuts.
	 * @param use true to use 4 modifier shortcuts, false to use
	 *            3 modifier shortcuts
	 */
	static void useFourModifierKeys( bool use );

 protected:
	TQString m_sName,
	        m_sLabel,
	        m_sWhatsThis;
	KShortcut m_cut;
	KShortcut m_cutDefault3, m_cutDefault4;
	const TQObject* m_pObjSlot;
	const char* m_psMethodSlot;
	bool m_bConfigurable,
	     m_bEnabled;
	int m_nIDAccel;
	uint m_nConnections;

	void incConnections();
	void decConnections();

 private:
	static int g_bUseFourModifierKeys;
	class KAccelActionPrivate* d;

	friend class KAccelActions;
	friend class KAccelBase;
};

//---------------------------------------------------------------------
// KAccelActions
//---------------------------------------------------------------------

/**
 * @internal
 * This class represents a collection of KAccelAction objects.
 *
 * @short A collection of accelerator actions
 * @see KAccelAction
 */
class KAccelActions
{
 public:
       /**
	* Creates a new, empty KAccelActions object.
	*/
	KAccelActions();

	/**
	 * Copy constructor (deep copy).
	 */
	KAccelActions( const KAccelActions& );
	virtual ~KAccelActions();

	/**
	 * Removes all items from this collection.
	 */
	void clear();

	/**
	 * Initializes this object with the given actions.
	 * It will make a deep copy of all actions.
	 * @param actions the actions to copy
	 * @return true if successful, false otherwise
	 */
	bool init( const KAccelActions &actions );

	/**
	 * Loads the actions from the given configuration file.
	 *
	 * @param config the configuration file to load from
	 * @param sGroup the group in the configuration file
	 * @return true if successful, false otherwise
	 */
	bool init( KConfigBase& config, const TQString& sGroup );

	/**
	 * Updates the shortcuts of all actions in this object
	 * with the shortcuts from the given object.
	 * @param shortcuts the collection that contains the new
	 *        shortcuts
	 */
	void updateShortcuts( KAccelActions &shortcuts );

	/**
	 * Retrieves the index of the action with the given name.
	 * @param sAction the action to search
	 * @return the index of the action, or -1 if not found
	 */
	int actionIndex( const TQString& sAction ) const;

	/**
	 * Returns the action with the given @p index.
	 * @param index the index of an action. You must not
	 *         use an index that is too high.
	 * @return the KAccelAction with the given index
	 * @see count()
	 */
	KAccelAction* actionPtr( uint index );

	/**
	 * Returns the action with the given @p index.
	 * @param index the index of an action. You must not
	 *         use an index that is too high.
	 * @return the KAccelAction with the given index
	 * @see count()
	 */
	const KAccelAction* actionPtr( uint index ) const;

	/**
	 * Returns the action with the given name.
	 * @param aAction the name of the action to search
	 * @return the KAccelAction with the given name, or 0
	 *          if not found
	 */
	KAccelAction* actionPtr( const TQString& sAction );

	/**
	 * Returns the action with the given name.
	 * @param aAction the name of the action to search
	 * @return the KAccelAction with the given name, or 0
	 *          if not found
	 */
	const KAccelAction* actionPtr( const TQString& sAction ) const;

	/**
	 * Returns the action with the given key sequence.
	 * @param cut the sequence to search for
	 * @return the KAccelAction with the given sequence, or 0
	 *          if not found
	 */
	KAccelAction* actionPtr( KKeySequence cut );

	/**
	 * Returns the action with the given @p index.
	 * @param index the index of an action. You must not
	 *         use an index that is too high.
	 * @return the KAccelAction with the given index
	 * @see actionPtr()
	 * @see count()
	 */
	KAccelAction& operator []( uint index );

	/**
	 * Returns the action with the given @p index.
	 * @param index the index of an action. You must not
	 *         use an index that is too high.
	 * @return the KAccelAction with the given index
	 * @see actionPtr()
	 * @see count()
	 */
	const KAccelAction& operator []( uint index ) const;

	/**
	 * Inserts an action into the collection.
	 * @param sName the name of the accelerator
	 * @param sLabel the label of the accelerator (i18n!)
	 * @param sWhatsThis the What's This text (18n!)
	 * @param cutDef3 the default shortcut for 3 modifier systems
	 * @param cutDef4 the default shortcut for 4 modifier systems
	 * @param pObjSlot the receiver of a signal when the key has been 
	 *                 pressed
	 * @param psMethodSlot the slot to connect for key presses. Receives
	 *                     an int, as set by setID(), as only argument
	 * @param bConfigurable if true the user can configure the shortcut
	 * @param bEnabled true if the accelerator should be enabled
	 * @return the new action
	 */
	KAccelAction* insert( const TQString& sAction, const TQString& sLabel, const TQString& sWhatsThis,
			const KShortcut& rgCutDefaults3, const KShortcut& rgCutDefaults4,
			const TQObject* pObjSlot = 0, const char* psMethodSlot = 0,
			bool bConfigurable = true, bool bEnabled = true );

	/**
	 * Inserts an action into the collection.
	 * @param sName the name of the accelerator
	 * @param sLabel the label of the accelerator (i18n!)
	 * @return the new action
	 */
	KAccelAction* insert( const TQString& sName, const TQString& sLabel );

	/**
	 * Removes the given action.
	 * @param sAction the name of the action.
	 * @return true if successful, false otherwise
	 */
	bool remove( const TQString& sAction );

	/**
	 * Loads the actions from the given configuration file.
	 *
	 * @param sConfigGroup the group in the configuration file
	 * @param pConfig the configuration file to load from
	 * @return true if successful, false otherwise
	 */
	bool readActions( const TQString& sConfigGroup = "Shortcuts", KConfigBase* pConfig = 0 );

	/**
	 * Writes the actions to the given configuration file.
	 *
	 * @param sConfigGroup the group in the configuration file
	 * @param pConfig the configuration file to save to
	 * @param bWriteAll true to write all actions
	 * @param bGlobal true to write to the global configuration file
	 * @return true if successful, false otherwise
	 */
	bool writeActions( const TQString& sConfigGroup = "Shortcuts", KConfigBase* pConfig = 0,
			bool bWriteAll = false, bool bGlobal = false ) const;

	/**
	 * Emit a keycodeChanged signal.
	 */
	void emitKeycodeChanged();

	/**
	 * Returns the number of actions in the collection.
	 * @return the number of actions
	 */
	uint count() const;

 protected:
	KAccelBase* m_pKAccelBase;
	KAccelAction** m_prgActions;
	uint m_nSizeAllocated, m_nSize;

	void resize( uint );
	void insertPtr( KAccelAction* );

 private:
	class KAccelActionsPrivate* d;

	KAccelActions( KAccelBase* );
	void initPrivate( KAccelBase* );
	KAccelActions& operator =( KAccelActions& );

	friend class KAccelBase;
};

#endif // _KACCELACTION_H