summaryrefslogtreecommitdiffstats
path: root/kbabel/commonui/kactionselector.h
blob: f9bae4669c02a5b5762b81e85cb418c9eaf376f7 (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
/***************************************************************************
                          KActionSelector.h 
                          A widget for selecting and arranging actions/objects 
                                -------------------
    begin                : Mon June 3 2002
    copyright            : (C) 2002 by Anders Lund
    email                : anders@alweb.dk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *                                                                         *
 *   In addition, as a special exception, the copyright holders give       *
 *   permission to link the code of this program with any edition of       *
 *   the Qt library by Trolltech AS, Norway (or with modified versions     *
 *   of Qt that use the same license as Qt), and distribute linked         *
 *   combinations including the two.  You must obey the GNU General        *
 *   Public License in all respects for all of the code used other than    *
 *   Qt. If you modify this file, you may extend this exception to         *
 *   your version of the file, but you are not obligated to do so.  If     *
 *   you do not wish to do so, delete this exception statement from        *
 *   your version.                                                         *
 ***************************************************************************/

#ifndef _KACTION_SELECTOR_H_
#define _KACTION_SELECTOR_H_

#include <tqwidget.h>

class TQListBox;
class TQListBoxItem;
class TQKeyEvent;
class TQEvent;
class TQIconSet;

class KActionSelectorPrivate;

/**
    @short A widget for selecting and arranging actions/objects
    This widget allows the user to select from a set of objects and arrange
    the order of the selected ones using two list boxes labeled "Available"
    and "Used" with horizontal arrows in between to move selected objects between 
    the two, and vertical arrows on the right to arrange the order of the selected 
    objects.
    
    The widget moves objects to the other listbox when doubleclicked if
    the property moveOnDoubleClick is set to true (default). See moveOnDoubleClick() 
    and setMoveOnDoubleClick().

    The user control the widget using the keyboard if enabled (default),
    see keyboardEnabled.
    
    Note that this may conflist with keyboard selection in the selected list box, 
    if you set that to anything else than TQListBox::Single (which is the default).
    
    To use it, simply construct an instance and then add items to the two listboxes,
    available through lbAvailable() and lbSelected(). Whenever you want, you can retrieve
    the selected options using TQListBox methods on lbSelected().
    
    This way, you can use your own TQListBoxItem class, allowing you to easily
    store object data in those.
    
    When an item is moved to a listbox, it is placed below the current item
    of that listbox.
                
    Standard arrow icons are used, but you can use icons of your own choice if desired,
    see setButtonIcon(). It is also possible to set tooltips and whatsthis help
    for the buttons. See setButtonTooltip() and setButtonWhatsThis().
    
    To set whatsthis or tooltips for the listboxes, access them through
    availableListbox() and selectedListBox().
    
    All the moving buttons are automatically set enabled as expected.
    
    Signals are sent each time an item is moved, allowing you to follow the
    users actions if you need to. See addedToSelection(), removedFromSelection(),
    movedUp() and movedDown()
    
    @author Anders Lund <anders@alweb.dk>
*/

class KActionSelector : public TQWidget {
  Q_OBJECT
  Q_ENUMS( ButtonIconSize InsertionPolicy )
  Q_PROPERTY( bool moveOnDoubleClick READ moveOnDoubleClick WRITE setMoveOnDoubleClick )
  Q_PROPERTY( bool keyboardEnabled READ keyboardEnabled WRITE setKeyboardEnabled )
  Q_PROPERTY( TQString availableLabel READ availableLabel WRITE setAvailableLabel )
  Q_PROPERTY( TQString selectedLabel READ selectedLabel WRITE setSelectedLabel )
  Q_PROPERTY( ButtonIconSize buttonIconSize READ buttonIconSize WRITE setButtonIconSize )
  Q_PROPERTY( InsertionPolicy availableInsertionPolicy READ availableInsertionPolicy WRITE setAvailableInsertionPolicy )
  Q_PROPERTY( InsertionPolicy selectedInsertionPolicy READ selectedInsertionPolicy WRITE setSelectedInsertionPolicy )
  Q_PROPERTY( bool showUpDownButtons READ showUpDownButtons WRITE setShowUpDownButtons )

public:
  KActionSelector( TQWidget *parent=0, const char *name=0 );
  ~KActionSelector();
    
  /**
     @return The TQListBox holding the available actions
  */
  TQListBox *availableListBox(); 
  
  /**
     @return The TQListBox holding the selected actions
  */
  TQListBox *selectedListBox();

  /**
    This enum indentifies the moving buttons
  */
  enum MoveButton {
    ButtonAdd,
    ButtonRemove,
    ButtonUp,
    ButtonDown
  };
      
  /**
    This enum identifies the icon sizes, used for the move buttons.
    The values correspond to the following pixel sizes:
    @li SmallIcon - the return value of IconSize( KIcon::Small ), the user defined size
                of a small icon in KDE. This is the default setting.
    @li Small - 16px
    @li Medium - 22px
    @li Large - 32px
    @li XLarge - 48px
  */
  enum ButtonIconSize {
    SmallIcon,
    Small,
    Medium,
    Large,
    XLarge
  };
  
  /**
    This enum defines policies for where to insert moved items in a listbox.
    The following policies are currently defined:
    @li BelowCurrent - The item is inserted below the listbox'
        currentItem() or at the end if there is no curent item.
    @li Sorted - The listbox is sort()ed after one or more items are inserted.
    @li AtTop - The item is inserted at index 0 in the listbox.
    @li AtBottom - The item is inserted at the end of the listbox.
    
    @sa availableInsertionPolicy(), setAvailableInsertionPolicy(),
    selectedInsertionPolicy(), setSelectedInsertionPolicy(). 
  */
  enum InsertionPolicy {
    BelowCurrent,
    Sorted,
    AtTop,
    AtBottom
  };
  
  /**
    @return Wheather moveOnDoubleClcik is enabled.
    
    If enabled, an item in any listbox will be moved to the other one whenever
    doubleclicked.
    @sa setMoveOnDoubleClick()
  */ 
  bool moveOnDoubleClick() const;
  
  /**
    Sets moveOnDoubleClick to @p enable
    @sa moveOnDoubleClick()
  */
  void setMoveOnDoubleClick( bool enable );
  
  /**
    @return Weather keyboard control is enabled.
    
    When Keyboard control is enabled, the widget will react to
    the following keyboard actions:
    @li CTRL + Right - simulate clicking the add button
    @li CTRL + Left - simulate clicking the remove button
    @li CTRL + Up - simulate clicking the up button
    @li CTRL + Down - simulate clicking the down button
    
    Additionally, pressing RETURN or ENTER on one of the list boxes
    will cause the current item of that listbox to be moved to the other
    listbox.
    
    The keyboard actions are enabled by default.
    
    @sa setKeyboardEnabled()
  */
  bool keyboardEnabled() const;
  
  /**
    Sets the keyboard enabled depending on @p enable.
    @sa keyboardEnabled()
  */
  void setKeyboardEnabled( bool enable );
  
  /**
    @return The text of the label for the available items listbox.
  */
  TQString availableLabel() const;
  
  /**
    Sets the label for the available items listbox to @p text.
    Note that this label has the listbox as its @e buddy, so that
    if you have a single ampersand in the text, the following character
    will become the accellerator to focus te listbox.
  */
  void setAvailableLabel( const TQString & text );
  
  /**
    @return the label of the selected items listbox.
  */
  TQString selectedLabel() const;
  
  /**
    Sets the label for the selected items listbox to @p text.
    Note that this label has the listbox as its @e buddy, so that
    if you have a single ampersand in the text, the following character
    will become the accellerator to focus te listbox.
  */
  void setSelectedLabel( const TQString & text );
    
  /**
    @return the current ButtonIconSize.
  */
  ButtonIconSize buttonIconSize() const;
  
  /**
    Sets the button icon size.
    See ButtonIconSize for the possible values and their pixel meaning.
  */
  void setButtonIconSize( ButtonIconSize size );

  /**
    @return The current insertion policy for the available listbox.
    The default policy for the available listbox is Sorted.
    See also InsertionPolicy, setAvailableInsertionPolicy().
  */
  InsertionPolicy availableInsertionPolicy();
  
  /**
    Sets the insertion policy for the available listbox.
    See also InsertionPolicy, availableInsertionPolicy().
  */
  void setAvailableInsertionPolicy( InsertionPolicy policy );

  /**
    @return The current insertion policy for the selected listbox.
    The default policy for the selected listbox is BelowCurrent.
    See also InsertionPolicy, setSelectedInsertionPolicy().
  */
  InsertionPolicy selectedInsertionPolicy();
  
  /**
    Sets the insertion policy for the selected listbox.
    See also InsertionPolicy, selectedInsertionPolicy().
  */
  void setSelectedInsertionPolicy( InsertionPolicy policy );
  
  /**
    @return wheather the Up and Down buttons should be displayed.
  */
  bool showUpDownButtons();
  
  /**
    Sets wheather the Up and Down buttons should be displayed
    according to @p show
  */
  void setShowUpDownButtons( bool show );
      
  /**
    Sets the pixmap of the button @p button to @p icon.
    It calls @ref SmallIconSet(pm) to generate the icon set.
  */
  void setButtonIcon( const TQString &icon, MoveButton button );
  
  /**
    Sets the iconset for button @p button to @p iconset.
    You can use this method to et a costum icon set. Either
    created by @ref TQIconSet, or use the application instance of
    @ref KIconLoader (recommended).
  */
  void setButtonIconSet( const TQIconSet &iconset, MoveButton button );
  
  /**
    Sets the tooltip for the button @p button to @p tip.
  */
  void setButtonTooltip( const TQString &tip, MoveButton button );
  
  /**
    Sets the whatsthis help for button @p button to @p text.
  */
  void setButtonWhatsThis( const TQString &text, MoveButton button );

  /**
     Sets the enabled state of all moving buttons to reflect the current
     options.
     
     Be sure to call this if you add or removes items to either listbox after the
     widget is show()n
  */
  void setButtonsEnabled();
    
signals:
  /**
    Emitted when an item is moved to the "selected" listbox.
  */
  void added( TQListBoxItem *item );

  /**
    Emitted when an item is moved out of the "selected" listbox.
  */
  void removed( TQListBoxItem *item );

  /**
    Emitted when an item is moved upwards in the "selected" listbox.
  */
  void movedUp( TQListBoxItem *item );

  /**
    Emitted when an item is moved downwards in the "selected" listbox.
  */
  void movedDown( TQListBoxItem *item );

  /**
    Emitted when an item is moved to the "selected" listbox.
  */
//  void addedToSelection( TQListBoxItem *item );

public slots:
  /**
    Reimplemented for internal reasons.
    (calls setButtonsEnabled())
  */
  void polish();
  
protected:
  /**
    Reimplamented for internal reasons.
  */
  void keyPressEvent( TQKeyEvent * );
  
  /**
    Reimplemented for internal reasons.
  */
  bool eventFilter( TQObject *, TQEvent * );
  
private slots:
  /** 
    Move selected item from available box to the selected box 
  */  
  void buttonAddClicked();
  
  /** 
    Move selected item from selected box to available box 
  */
  void buttonRemoveClicked();
  
  /**
    Move selected item in selected box upwards 
  */
  void buttonUpClicked();
  
  /**
    Move seleted item in selected box downwards 
  */
  void buttonDownClicked();
  
  /**
    Moves the item @p item to the other listbox if moveOnDoubleClick is enabled.
  */
  void  itemDoubleClicked( TQListBoxItem *item );
  
  /**
    connected to both list boxes to set the buttons enabled
  */
  void slotCurrentChanged( TQListBoxItem * ) { setButtonsEnabled(); };
  
private:
  
  /**
    Move item @p item to the other listbox
  */
  void moveItem( TQListBoxItem *item );
  
  /**
    loads the icons for the move buttons.
  */
  void loadIcons();
  
  /**
    @return the index to insert an item into listbox @p lb,
    given InsertionPolicy @p policy.
    
    Note that if policy is Sorted, this will return -1.
    Sort the listbox after inserting the item in that case.
  */
  int insertionIndex( TQListBox *lb, InsertionPolicy policy );
  
  /** @private
    Private data storage 
  */
  KActionSelectorPrivate *d;
};

#endif // _KACTION_SELECTOR_H_