~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/fileselector/kactionselector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License version 2 as published by the Free Software Foundation.
 
7
 
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU Library General Public License
 
14
   along with this library; see the file COPYING.LIB.  If not, write to
 
15
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
16
   Boston, MA 02111-1307, USA.
 
17
*/
 
18
 
 
19
 
 
20
#include "kactionselector.h"
 
21
 
 
22
#include <klocale.h>
 
23
#include <kiconloader.h>
 
24
#include <kdialog.h> // for spacingHint()
 
25
#include <kdebug.h>
 
26
#include <qapplication.h>
 
27
 
 
28
#include <qlistbox.h>
 
29
#include <qtoolbutton.h>
 
30
#include <qlabel.h>
 
31
#include <qlayout.h>
 
32
#include <qevent.h>
 
33
#include <qwhatsthis.h>
 
34
 
 
35
class KActionSelectorPrivate {
 
36
  public:
 
37
  QListBox *availableListBox, *selectedListBox;
 
38
  QToolButton *btnAdd, *btnRemove, *btnUp, *btnDown;
 
39
  QLabel *lAvailable, *lSelected;
 
40
  bool moveOnDoubleClick, keyboardEnabled;
 
41
  KActionSelector::ButtonIconSize iconSize;
 
42
  QString addIcon, removeIcon, upIcon, downIcon;
 
43
  KActionSelector::InsertionPolicy availableInsertionPolicy, selectedInsertionPolicy;
 
44
  bool showUpDownButtons;
 
45
};
 
46
 
 
47
//BEGIN Constructor/destructor
 
48
 
 
49
KActionSelector::KActionSelector( QWidget *parent, const char *name )
 
50
  : QWidget( parent, name )
 
51
{
 
52
  d = new KActionSelectorPrivate();
 
53
  d->moveOnDoubleClick = true;
 
54
  d->keyboardEnabled = true;
 
55
  d->iconSize = SmallIcon;
 
56
  d->addIcon = QApplication::reverseLayout() ? "back" : "forward";
 
57
  d->removeIcon = QApplication::reverseLayout() ? "forward" : "back";
 
58
  d->upIcon = "up";
 
59
  d->downIcon = "down";
 
60
  d->availableInsertionPolicy = Sorted;
 
61
  d->selectedInsertionPolicy = BelowCurrent;
 
62
  d->showUpDownButtons = true;
 
63
 
 
64
  //int isz = IconSize( KIcon::Small );
 
65
 
 
66
  QHBoxLayout *lo = new QHBoxLayout( this );
 
67
  lo->setSpacing( KDialog::spacingHint() );
 
68
 
 
69
  QVBoxLayout *loAv = new QVBoxLayout( lo );
 
70
  d->lAvailable = new QLabel( i18n("&Available:"), this );
 
71
  loAv->addWidget( d->lAvailable );
 
72
  d->availableListBox = new QListBox( this );
 
73
  loAv->addWidget( d->availableListBox );
 
74
  d->lAvailable->setBuddy( d->availableListBox );
 
75
 
 
76
  QVBoxLayout *loHBtns = new QVBoxLayout( lo );
 
77
  loHBtns->addStretch( 1 );
 
78
  d->btnAdd = new QToolButton( this );
 
79
  loHBtns->addWidget( d->btnAdd );
 
80
  d->btnRemove = new QToolButton( this );
 
81
  loHBtns->addWidget( d->btnRemove );
 
82
  loHBtns->addStretch( 1 );
 
83
 
 
84
  QVBoxLayout *loS = new QVBoxLayout( lo );
 
85
  d->lSelected = new QLabel( i18n("&Selected:"), this );
 
86
  loS->addWidget( d->lSelected );
 
87
  d->selectedListBox = new QListBox( this );
 
88
  loS->addWidget( d->selectedListBox );
 
89
  d->lSelected->setBuddy( d->selectedListBox );
 
90
 
 
91
  QVBoxLayout *loVBtns = new QVBoxLayout( lo );
 
92
  loVBtns->addStretch( 1 );
 
93
  d->btnUp = new QToolButton( this );
 
94
  loVBtns->addWidget( d->btnUp );
 
95
  d->btnDown = new QToolButton( this );
 
96
  loVBtns->addWidget( d->btnDown );
 
97
  loVBtns->addStretch( 1 );
 
98
 
 
99
  loadIcons();
 
100
 
 
101
  connect( d->btnAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()) );
 
102
  connect( d->btnRemove, SIGNAL(clicked()), this, SLOT(buttonRemoveClicked()) );
 
103
  connect( d->btnUp, SIGNAL(clicked()), this, SLOT(buttonUpClicked()) );
 
104
  connect( d->btnDown, SIGNAL(clicked()), this, SLOT(buttonDownClicked()) );
 
105
  connect( d->availableListBox, SIGNAL(doubleClicked(QListBoxItem*)),
 
106
           this, SLOT(itemDoubleClicked(QListBoxItem*)) );
 
107
  connect( d->selectedListBox, SIGNAL(doubleClicked(QListBoxItem*)),
 
108
           this, SLOT(itemDoubleClicked(QListBoxItem*)) );
 
109
  connect( d->availableListBox, SIGNAL(currentChanged(QListBoxItem*)),
 
110
           this, SLOT(slotCurrentChanged(QListBoxItem *)) );
 
111
  connect( d->selectedListBox, SIGNAL(currentChanged(QListBoxItem*)),
 
112
           this, SLOT(slotCurrentChanged(QListBoxItem *)) );
 
113
 
 
114
  d->availableListBox->installEventFilter( this );
 
115
  d->selectedListBox->installEventFilter( this );
 
116
}
 
117
 
 
118
KActionSelector::~KActionSelector()
 
119
{
 
120
                delete d;
 
121
}
 
122
 
 
123
//END Constructor/destroctor
 
124
 
 
125
//BEGIN Public Methods
 
126
 
 
127
QListBox *KActionSelector::availableListBox()
 
128
{
 
129
  return d->availableListBox;
 
130
}
 
131
 
 
132
QListBox *KActionSelector::selectedListBox()
 
133
{
 
134
  return d->selectedListBox;
 
135
}
 
136
 
 
137
void KActionSelector::setButtonIcon( const QString &icon, MoveButton button )
 
138
{
 
139
  switch ( button )
 
140
  {
 
141
    case ButtonAdd:
 
142
    d->addIcon = icon;
 
143
    d->btnAdd->setIconSet( SmallIconSet( icon, d->iconSize ) );
 
144
    break;
 
145
    case ButtonRemove:
 
146
    d->removeIcon = icon;
 
147
    d->btnRemove->setIconSet( SmallIconSet( icon, d->iconSize ) );
 
148
    break;
 
149
    case ButtonUp:
 
150
    d->upIcon = icon;
 
151
    d->btnUp->setIconSet( SmallIconSet( icon, d->iconSize ) );
 
152
    break;
 
153
    case ButtonDown:
 
154
    d->downIcon = icon;
 
155
    d->btnDown->setIconSet( SmallIconSet( icon, d->iconSize ) );
 
156
    break;
 
157
    default:
 
158
    kdDebug()<<"KActionSelector::setButtonIcon: DAINBREAD!"<<endl;
 
159
  }
 
160
}
 
161
 
 
162
void KActionSelector::setButtonIconSet( const QIconSet &iconset, MoveButton button )
 
163
{
 
164
  switch ( button )
 
165
  {
 
166
    case ButtonAdd:
 
167
    d->btnAdd->setIconSet( iconset );
 
168
    break;
 
169
    case ButtonRemove:
 
170
    d->btnRemove->setIconSet( iconset );
 
171
    break;
 
172
    case ButtonUp:
 
173
    d->btnUp->setIconSet( iconset );
 
174
    break;
 
175
    case ButtonDown:
 
176
    d->btnDown->setIconSet( iconset );
 
177
    break;
 
178
    default:
 
179
    kdDebug()<<"KActionSelector::setButtonIconSet: DAINBREAD!"<<endl;
 
180
  }
 
181
}
 
182
 
 
183
void KActionSelector::setButtonTooltip( const QString &tip, MoveButton button )
 
184
{
 
185
  switch ( button )
 
186
  {
 
187
    case ButtonAdd:
 
188
    d->btnAdd->setTextLabel( tip );
 
189
    break;
 
190
    case ButtonRemove:
 
191
    d->btnRemove->setTextLabel( tip );
 
192
    break;
 
193
    case ButtonUp:
 
194
    d->btnUp->setTextLabel( tip );
 
195
    break;
 
196
    case ButtonDown:
 
197
    d->btnDown->setTextLabel( tip );
 
198
    break;
 
199
    default:
 
200
    kdDebug()<<"KActionSelector::setButtonToolTip: DAINBREAD!"<<endl;
 
201
  }
 
202
}
 
203
 
 
204
void KActionSelector::setButtonWhatsThis( const QString &text, MoveButton button )
 
205
{
 
206
  switch ( button )
 
207
  {
 
208
    case ButtonAdd:
 
209
    QWhatsThis::add( d->btnAdd, text );
 
210
    break;
 
211
    case ButtonRemove:
 
212
    QWhatsThis::add( d->btnRemove, text );
 
213
    break;
 
214
    case ButtonUp:
 
215
    QWhatsThis::add( d->btnUp, text );
 
216
    break;
 
217
    case ButtonDown:
 
218
    QWhatsThis::add( d->btnDown, text );
 
219
    break;
 
220
    default:
 
221
    kdDebug()<<"KActionSelector::setButtonWhatsThis: DAINBREAD!"<<endl;
 
222
  }
 
223
}
 
224
 
 
225
void KActionSelector::setButtonsEnabled()
 
226
{
 
227
  d->btnAdd->setEnabled( d->availableListBox->currentItem() > -1 );
 
228
  d->btnRemove->setEnabled( d->selectedListBox->currentItem() > -1 );
 
229
  d->btnUp->setEnabled( d->selectedListBox->currentItem() > 0 );
 
230
  d->btnDown->setEnabled( d->selectedListBox->currentItem() > -1 &&
 
231
                          d->selectedListBox->currentItem() < (int)d->selectedListBox->count() - 1 );
 
232
}
 
233
 
 
234
//END Public Methods
 
235
 
 
236
//BEGIN Properties
 
237
 
 
238
bool KActionSelector::moveOnDoubleClick() const
 
239
{
 
240
  return d->moveOnDoubleClick;
 
241
}
 
242
 
 
243
void KActionSelector::setMoveOnDoubleClick( bool b )
 
244
{
 
245
  d->moveOnDoubleClick = b;
 
246
}
 
247
 
 
248
bool KActionSelector::keyboardEnabled() const
 
249
{
 
250
  return d->keyboardEnabled;
 
251
}
 
252
 
 
253
void KActionSelector::setKeyboardEnabled( bool b )
 
254
{
 
255
  d->keyboardEnabled = b;
 
256
}
 
257
 
 
258
QString KActionSelector::availableLabel() const
 
259
{
 
260
  return d->lAvailable->text();
 
261
}
 
262
 
 
263
void KActionSelector::setAvailableLabel( const QString &text )
 
264
{
 
265
  d->lAvailable->setText( text );
 
266
}
 
267
 
 
268
QString KActionSelector::selectedLabel() const
 
269
{
 
270
  return d->lSelected->text();
 
271
}
 
272
 
 
273
void KActionSelector::setSelectedLabel( const QString &text )
 
274
{
 
275
  d->lSelected->setText( text );
 
276
}
 
277
 
 
278
KActionSelector::ButtonIconSize KActionSelector::buttonIconSize() const
 
279
{
 
280
  return d->iconSize;
 
281
}
 
282
 
 
283
void KActionSelector::setButtonIconSize( ButtonIconSize size )
 
284
{
 
285
  d->iconSize = size;
 
286
  // reload icons
 
287
  loadIcons();
 
288
}
 
289
 
 
290
KActionSelector::InsertionPolicy KActionSelector::availableInsertionPolicy() const
 
291
{
 
292
  return d->availableInsertionPolicy;
 
293
}
 
294
 
 
295
void KActionSelector::setAvailableInsertionPolicy( InsertionPolicy p )
 
296
{
 
297
  d->availableInsertionPolicy = p;
 
298
}
 
299
 
 
300
KActionSelector::InsertionPolicy KActionSelector::selectedInsertionPolicy() const
 
301
{
 
302
  return d->selectedInsertionPolicy;
 
303
}
 
304
 
 
305
void KActionSelector::setSelectedInsertionPolicy( InsertionPolicy p )
 
306
{
 
307
  d->selectedInsertionPolicy = p;
 
308
}
 
309
 
 
310
bool KActionSelector::showUpDownButtons() const
 
311
{
 
312
  return d->showUpDownButtons;
 
313
}
 
314
 
 
315
void KActionSelector::setShowUpDownButtons( bool show )
 
316
{
 
317
  d->showUpDownButtons = show;
 
318
  if ( show )
 
319
  {
 
320
    d->btnUp->show();
 
321
    d->btnDown->show();
 
322
  }
 
323
  else
 
324
  {
 
325
    d->btnUp->hide();
 
326
    d->btnDown->hide();
 
327
  }
 
328
}
 
329
 
 
330
//END Properties
 
331
 
 
332
//BEGIN Public Slots
 
333
 
 
334
void KActionSelector::polish()
 
335
{
 
336
  setButtonsEnabled();
 
337
}
 
338
 
 
339
//END Public Slots
 
340
 
 
341
//BEGIN Protected
 
342
void KActionSelector::keyPressEvent( QKeyEvent *e )
 
343
{
 
344
  if ( ! d->keyboardEnabled ) return;
 
345
  if ( (e->state() & Qt::ControlButton) )
 
346
  {
 
347
    switch ( e->key() )
 
348
    {
 
349
      case Key_Right:
 
350
      buttonAddClicked();
 
351
      break;
 
352
      case Key_Left:
 
353
      buttonRemoveClicked();
 
354
      break;
 
355
      case Key_Up:
 
356
      buttonUpClicked();
 
357
      break;
 
358
      case Key_Down:
 
359
      buttonDownClicked();
 
360
      break;
 
361
      default:
 
362
      e->ignore();
 
363
      return;
 
364
    }
 
365
  }
 
366
}
 
367
 
 
368
bool KActionSelector::eventFilter( QObject *o, QEvent *e )
 
369
{
 
370
  if ( d->keyboardEnabled && e->type() == QEvent::KeyPress )
 
371
  {
 
372
    if  ( (((QKeyEvent*)e)->state() & Qt::ControlButton) )
 
373
    {
 
374
      switch ( ((QKeyEvent*)e)->key() )
 
375
      {
 
376
        case Key_Right:
 
377
        buttonAddClicked();
 
378
        break;
 
379
        case Key_Left:
 
380
        buttonRemoveClicked();
 
381
        break;
 
382
        case Key_Up:
 
383
        buttonUpClicked();
 
384
        break;
 
385
        case Key_Down:
 
386
        buttonDownClicked();
 
387
        break;
 
388
        default:
 
389
        return QWidget::eventFilter( o, e );
 
390
        break;
 
391
      }
 
392
      return true;
 
393
    }
 
394
    else if ( o->inherits( "QListBox" ) )
 
395
    {
 
396
      switch ( ((QKeyEvent*)e)->key() )
 
397
      {
 
398
        case Key_Return:
 
399
        case Key_Enter:
 
400
        QListBox *lb = (QListBox*)o;
 
401
        int index = lb->currentItem();
 
402
        if ( index < 0 ) break;
 
403
        moveItem( lb->item( index ) );
 
404
        return true;
 
405
      }
 
406
    }
 
407
  }
 
408
  return QWidget::eventFilter( o, e );
 
409
}
 
410
 
 
411
//END Protected
 
412
 
 
413
//BEGIN Private Slots
 
414
 
 
415
void KActionSelector::buttonAddClicked()
 
416
{
 
417
  // move all selected items from available to selected listbox
 
418
  QListBoxItem *item = d->availableListBox->firstItem();
 
419
  while ( item ) {
 
420
    if ( item->isSelected() ) {
 
421
      d->availableListBox->takeItem( item );
 
422
      d->selectedListBox->insertItem( item, insertionIndex( d->selectedListBox, d->selectedInsertionPolicy ) );
 
423
      d->selectedListBox->setCurrentItem( item );
 
424
      emit added( item );
 
425
    }
 
426
    item = item->next();
 
427
  }
 
428
  if ( d->selectedInsertionPolicy == Sorted )
 
429
    d->selectedListBox->sort();
 
430
  d->selectedListBox->setFocus();
 
431
}
 
432
 
 
433
void KActionSelector::buttonRemoveClicked()
 
434
{
 
435
  // move all selected items from selected to available listbox
 
436
  QListBoxItem *item = d->selectedListBox->firstItem();
 
437
  while ( item ) {
 
438
    if ( item->isSelected() ) {
 
439
      d->selectedListBox->takeItem( item );
 
440
      d->availableListBox->insertItem( item, insertionIndex( d->availableListBox, d->availableInsertionPolicy ) );
 
441
      d->availableListBox->setCurrentItem( item );
 
442
      emit removed( item );
 
443
    }
 
444
    item = item->next();
 
445
  }
 
446
  if ( d->availableInsertionPolicy == Sorted )
 
447
    d->availableListBox->sort();
 
448
  d->availableListBox->setFocus();
 
449
}
 
450
 
 
451
void KActionSelector::buttonUpClicked()
 
452
{
 
453
  int c = d->selectedListBox->currentItem();
 
454
  if ( c < 0 ) return;
 
455
  QListBoxItem *item = d->selectedListBox->item( c );
 
456
  d->selectedListBox->takeItem( item );
 
457
  d->selectedListBox->insertItem( item, c-1 );
 
458
  d->selectedListBox->setCurrentItem( item );
 
459
  emit movedUp( item );
 
460
}
 
461
 
 
462
void KActionSelector::buttonDownClicked()
 
463
{
 
464
  int c = d->selectedListBox->currentItem();
 
465
  if ( c < 0 ) return;
 
466
  QListBoxItem *item = d->selectedListBox->item( c );
 
467
  d->selectedListBox->takeItem( item );
 
468
  d->selectedListBox->insertItem( item, c+1 );
 
469
  d->selectedListBox->setCurrentItem( item );
 
470
  emit movedDown( item );
 
471
}
 
472
 
 
473
void KActionSelector::itemDoubleClicked( QListBoxItem *item )
 
474
{
 
475
  if ( d->moveOnDoubleClick )
 
476
    moveItem( item );
 
477
}
 
478
 
 
479
//END Private Slots
 
480
 
 
481
//BEGIN Private Methods
 
482
 
 
483
void KActionSelector::loadIcons()
 
484
{
 
485
  d->btnAdd->setIconSet( SmallIconSet( d->addIcon, d->iconSize ) );
 
486
  d->btnRemove->setIconSet( SmallIconSet( d->removeIcon, d->iconSize ) );
 
487
  d->btnUp->setIconSet( SmallIconSet( d->upIcon, d->iconSize ) );
 
488
  d->btnDown->setIconSet( SmallIconSet( d->downIcon, d->iconSize ) );
 
489
}
 
490
 
 
491
void KActionSelector::moveItem( QListBoxItem *item )
 
492
{
 
493
  QListBox *lbFrom = item->listBox();
 
494
  QListBox *lbTo;
 
495
  if ( lbFrom == d->availableListBox )
 
496
    lbTo = d->selectedListBox;
 
497
  else if ( lbFrom == d->selectedListBox )
 
498
    lbTo = d->availableListBox;
 
499
  else  //?! somewhat unlikely...
 
500
    return;
 
501
 
 
502
  InsertionPolicy p = ( lbTo == d->availableListBox ) ?
 
503
                        d->availableInsertionPolicy : d->selectedInsertionPolicy;
 
504
 
 
505
  lbFrom->takeItem( item );
 
506
  lbTo->insertItem( item, insertionIndex( lbTo, p ) );
 
507
  lbTo->setFocus();
 
508
  lbTo->setCurrentItem( item );
 
509
 
 
510
  if ( p == Sorted )
 
511
    lbTo->sort();
 
512
  if ( lbTo == d->selectedListBox )
 
513
    emit added( item );
 
514
  else
 
515
    emit removed( item );
 
516
}
 
517
 
 
518
int KActionSelector::insertionIndex( QListBox *lb, InsertionPolicy policy )
 
519
{
 
520
  int index;
 
521
  switch ( policy )
 
522
  {
 
523
    case BelowCurrent:
 
524
    index = lb->currentItem();
 
525
    if ( index > -1 ) index += 1;
 
526
    break;
 
527
    case AtTop:
 
528
    index = 0;
 
529
    break;
 
530
    default:
 
531
    index = -1;
 
532
  }
 
533
  return index;
 
534
}
 
535
 
 
536
//END Private Methods
 
537
#include "kactionselector.moc"