File: kactionselector.cpp

package info (click to toggle)
kwidgetsaddons 5.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,388 kB
  • ctags: 3,611
  • sloc: cpp: 26,647; python: 682; sh: 23; makefile: 6
file content (564 lines) | stat: -rw-r--r-- 15,880 bytes parent folder | download
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 project
   Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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.
*/

#include "kactionselector.h"

#include <QApplication>
#include <QToolButton>
#include <QLabel>
#include <QLayout>
#include <QActionEvent>
#include <QListWidget>

class KActionSelectorPrivate
{
public:
    KActionSelectorPrivate(KActionSelector *q): q(q) {}

    KActionSelector *q;
    QListWidget *availableListWidget, *selectedListWidget;
    QToolButton *btnAdd, *btnRemove, *btnUp, *btnDown;
    QLabel *lAvailable, *lSelected;
    bool moveOnDoubleClick : 1;
    bool keyboardEnabled : 1;
    bool showUpDownButtons : 1;
    QString addIcon, removeIcon, upIcon, downIcon;
    KActionSelector::InsertionPolicy availableInsertionPolicy, selectedInsertionPolicy;

    /**
      Move item @p item to the other listbox
     */
    void moveItem(QListWidgetItem *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(QListWidget *lb, KActionSelector::InsertionPolicy policy);

    /**
     @return the index of the first selected item in listbox @p lb.
     If no item is selected, it will return -1.
     */
    int selectedRowIndex(QListWidget *lb);

    void buttonAddClicked();
    void buttonRemoveClicked();
    void buttonUpClicked();
    void buttonDownClicked();
    void itemDoubleClicked(QListWidgetItem *item);
    void slotCurrentChanged(QListWidgetItem *)
    {
        q->setButtonsEnabled();
    }
};

//BEGIN Constructor/destructor

KActionSelector::KActionSelector(QWidget *parent)
    : QWidget(parent)
    , d(new KActionSelectorPrivate(this))
{
    d->moveOnDoubleClick = true;
    d->keyboardEnabled = true;
    d->addIcon = QLatin1String(QApplication::isRightToLeft() ? "go-previous" : "go-next");
    d->removeIcon = QLatin1String(QApplication::isRightToLeft() ? "go-next" : "go-previous");
    d->upIcon = QStringLiteral("go-up");
    d->downIcon = QStringLiteral("go-down");
    d->availableInsertionPolicy = Sorted;
    d->selectedInsertionPolicy = BelowCurrent;
    d->showUpDownButtons = true;

    QHBoxLayout *lo = new QHBoxLayout(this);
    lo->setMargin(0);

    QVBoxLayout *loAv = new QVBoxLayout();
    lo->addLayout(loAv);
    d->lAvailable = new QLabel(tr("&Available:"), this);
    loAv->addWidget(d->lAvailable);
    d->availableListWidget = new QListWidget(this);
    loAv->addWidget(d->availableListWidget);
    d->lAvailable->setBuddy(d->availableListWidget);

    QVBoxLayout *loHBtns = new QVBoxLayout();
    lo->addLayout(loHBtns);
    loHBtns->addStretch(1);
    d->btnAdd = new QToolButton(this);
    loHBtns->addWidget(d->btnAdd);
    d->btnRemove = new QToolButton(this);
    loHBtns->addWidget(d->btnRemove);
    loHBtns->addStretch(1);

    QVBoxLayout *loS = new QVBoxLayout();
    lo->addLayout(loS);
    d->lSelected = new QLabel(tr("&Selected:"), this);
    loS->addWidget(d->lSelected);
    d->selectedListWidget = new QListWidget(this);
    loS->addWidget(d->selectedListWidget);
    d->lSelected->setBuddy(d->selectedListWidget);

    QVBoxLayout *loVBtns = new QVBoxLayout();
    lo->addLayout(loVBtns);
    loVBtns->addStretch(1);
    d->btnUp = new QToolButton(this);
    d->btnUp->setAutoRepeat(true);
    loVBtns->addWidget(d->btnUp);
    d->btnDown = new QToolButton(this);
    d->btnDown->setAutoRepeat(true);
    loVBtns->addWidget(d->btnDown);
    loVBtns->addStretch(1);

    d->loadIcons();

    connect(d->btnAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()));
    connect(d->btnRemove, SIGNAL(clicked()), this, SLOT(buttonRemoveClicked()));
    connect(d->btnUp, SIGNAL(clicked()), this, SLOT(buttonUpClicked()));
    connect(d->btnDown, SIGNAL(clicked()), this, SLOT(buttonDownClicked()));
    connect(d->availableListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
            this, SLOT(itemDoubleClicked(QListWidgetItem*)));
    connect(d->selectedListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
            this, SLOT(itemDoubleClicked(QListWidgetItem*)));
    connect(d->availableListWidget, &QListWidget::itemSelectionChanged, this, &KActionSelector::setButtonsEnabled);
    connect(d->selectedListWidget, &QListWidget::itemSelectionChanged, this, &KActionSelector::setButtonsEnabled);

    d->availableListWidget->installEventFilter(this);
    d->selectedListWidget->installEventFilter(this);
    setButtonsEnabled();
}

KActionSelector::~KActionSelector()
{
    delete d;
}

//END Constructor/destroctor

//BEGIN Public Methods

QListWidget *KActionSelector::availableListWidget() const
{
    return d->availableListWidget;
}

QListWidget *KActionSelector::selectedListWidget() const
{
    return d->selectedListWidget;
}

void KActionSelector::setButtonIcon(const QString &icon, MoveButton button)
{
    switch (button) {
    case ButtonAdd:
        d->addIcon = icon;
        d->btnAdd->setIcon(QIcon::fromTheme(icon));
        break;
    case ButtonRemove:
        d->removeIcon = icon;
        d->btnRemove->setIcon(QIcon::fromTheme(icon));
        break;
    case ButtonUp:
        d->upIcon = icon;
        d->btnUp->setIcon(QIcon::fromTheme(icon));
        break;
    case ButtonDown:
        d->downIcon = icon;
        d->btnDown->setIcon(QIcon::fromTheme(icon));
        break;
    default:
//     qDebug()<<"KActionSelector::setButtonIcon: DAINBREAD!";
        break;
    }
}

void KActionSelector::setButtonIconSet(const QIcon &iconset, MoveButton button)
{
    switch (button) {
    case ButtonAdd:
        d->btnAdd->setIcon(iconset);
        break;
    case ButtonRemove:
        d->btnRemove->setIcon(iconset);
        break;
    case ButtonUp:
        d->btnUp->setIcon(iconset);
        break;
    case ButtonDown:
        d->btnDown->setIcon(iconset);
        break;
    default:
//     qDebug()<<"KActionSelector::setButtonIconSet: DAINBREAD!";
        break;
    }
}

void KActionSelector::setButtonTooltip(const QString &tip, MoveButton button)
{
    switch (button) {
    case ButtonAdd:
        d->btnAdd->setText(tip);
        d->btnAdd->setToolTip(tip);
        break;
    case ButtonRemove:
        d->btnRemove->setText(tip);
        d->btnRemove->setToolTip(tip);
        break;
    case ButtonUp:
        d->btnUp->setText(tip);
        d->btnUp->setToolTip(tip);
        break;
    case ButtonDown:
        d->btnDown->setText(tip);
        d->btnDown->setToolTip(tip);
        break;
    default:
//     qDebug()<<"KActionSelector::setButtonToolTip: DAINBREAD!";
        break;
    }
}

void KActionSelector::setButtonWhatsThis(const QString &text, MoveButton button)
{
    switch (button) {
    case ButtonAdd:
        d->btnAdd->setWhatsThis(text);
        break;
    case ButtonRemove:
        d->btnRemove->setWhatsThis(text);
        break;
    case ButtonUp:
        d->btnUp->setWhatsThis(text);
        break;
    case ButtonDown:
        d->btnDown->setWhatsThis(text);
        break;
    default:
//     qDebug()<<"KActionSelector::setButtonWhatsThis: DAINBREAD!";
        break;
    }
}

//END Public Methods

//BEGIN Properties

bool KActionSelector::moveOnDoubleClick() const
{
    return d->moveOnDoubleClick;
}

void KActionSelector::setMoveOnDoubleClick(bool b)
{
    d->moveOnDoubleClick = b;
}

bool KActionSelector::keyboardEnabled() const
{
    return d->keyboardEnabled;
}

void KActionSelector::setKeyboardEnabled(bool b)
{
    d->keyboardEnabled = b;
}

QString KActionSelector::availableLabel() const
{
    return d->lAvailable->text();
}

void KActionSelector::setAvailableLabel(const QString &text)
{
    d->lAvailable->setText(text);
}

QString KActionSelector::selectedLabel() const
{
    return d->lSelected->text();
}

void KActionSelector::setSelectedLabel(const QString &text)
{
    d->lSelected->setText(text);
}

KActionSelector::InsertionPolicy KActionSelector::availableInsertionPolicy() const
{
    return d->availableInsertionPolicy;
}

void KActionSelector::setAvailableInsertionPolicy(InsertionPolicy p)
{
    d->availableInsertionPolicy = p;
}

KActionSelector::InsertionPolicy KActionSelector::selectedInsertionPolicy() const
{
    return d->selectedInsertionPolicy;
}

void KActionSelector::setSelectedInsertionPolicy(InsertionPolicy p)
{
    d->selectedInsertionPolicy = p;
}

bool KActionSelector::showUpDownButtons() const
{
    return d->showUpDownButtons;
}

void KActionSelector::setShowUpDownButtons(bool show)
{
    d->showUpDownButtons = show;
    if (show) {
        d->btnUp->show();
        d->btnDown->show();
    } else {
        d->btnUp->hide();
        d->btnDown->hide();
    }
}

//END Properties

//BEGIN Public Slots

void KActionSelector::setButtonsEnabled()
{
    d->btnAdd->setEnabled(d->selectedRowIndex(d->availableListWidget) > -1);
    d->btnRemove->setEnabled(d->selectedRowIndex(d->selectedListWidget) > -1);
    d->btnUp->setEnabled(d->selectedRowIndex(d->selectedListWidget) > 0);
    d->btnDown->setEnabled(d->selectedRowIndex(d->selectedListWidget) > -1 &&
                           d->selectedRowIndex(d->selectedListWidget) < d->selectedListWidget->count() - 1);
}

//END Public Slots

//BEGIN Protected
void KActionSelector::keyPressEvent(QKeyEvent *e)
{
    if (! d->keyboardEnabled) {
        return;
    }
    if ((e->modifiers() & Qt::ControlModifier)) {
        switch (e->key()) {
        case Qt::Key_Right:
            d->buttonAddClicked();
            break;
        case Qt::Key_Left:
            d->buttonRemoveClicked();
            break;
        case Qt::Key_Up:
            d->buttonUpClicked();
            break;
        case Qt::Key_Down:
            d->buttonDownClicked();
            break;
        default:
            e->ignore();
            return;
        }
    }
}

bool KActionSelector::eventFilter(QObject *o, QEvent *e)
{
    if (d->keyboardEnabled && e->type() == QEvent::KeyPress) {
        if ((((QKeyEvent *)e)->modifiers() & Qt::ControlModifier)) {
            switch (((QKeyEvent *)e)->key()) {
            case Qt::Key_Right:
                d->buttonAddClicked();
                break;
            case Qt::Key_Left:
                d->buttonRemoveClicked();
                break;
            case Qt::Key_Up:
                d->buttonUpClicked();
                break;
            case Qt::Key_Down:
                d->buttonDownClicked();
                break;
            default:
                return QWidget::eventFilter(o, e);
                break;
            }
            return true;
        } else if (QListWidget *lb = qobject_cast<QListWidget *>(o)) {
            switch (((QKeyEvent *)e)->key()) {
            case Qt::Key_Return:
            case Qt::Key_Enter:
                int index = lb->currentRow();
                if (index < 0) {
                    break;
                }
                d->moveItem(lb->item(index));
                return true;
            }
        }
    }
    return QWidget::eventFilter(o, e);
}

//END Protected

//BEGIN Private Slots

void KActionSelectorPrivate::buttonAddClicked()
{
    // move all selected items from available to selected listbox
    QList<QListWidgetItem *> list = availableListWidget->selectedItems();
    Q_FOREACH (QListWidgetItem *item, list) {
        availableListWidget->takeItem(availableListWidget->row(item));
        selectedListWidget->insertItem(insertionIndex(selectedListWidget, selectedInsertionPolicy), item);
        selectedListWidget->setCurrentItem(item);
        emit q->added(item);
    }
    if (selectedInsertionPolicy == KActionSelector::Sorted) {
        selectedListWidget->sortItems();
    }
    selectedListWidget->setFocus();
}

void KActionSelectorPrivate::buttonRemoveClicked()
{
    // move all selected items from selected to available listbox
    QList<QListWidgetItem *> list = selectedListWidget->selectedItems();
    Q_FOREACH (QListWidgetItem *item, list) {
        selectedListWidget->takeItem(selectedListWidget->row(item));
        availableListWidget->insertItem(insertionIndex(availableListWidget, availableInsertionPolicy), item);
        availableListWidget->setCurrentItem(item);
        emit q->removed(item);
    }
    if (availableInsertionPolicy == KActionSelector::Sorted) {
        availableListWidget->sortItems();
    }
    availableListWidget->setFocus();
}

void KActionSelectorPrivate::buttonUpClicked()
{
    int c = selectedRowIndex(selectedListWidget);
    if (c < 1) {
        return;
    }
    QListWidgetItem *item = selectedListWidget->item(c);
    selectedListWidget->takeItem(c);
    selectedListWidget->insertItem(c - 1, item);
    selectedListWidget->setCurrentItem(item);
    emit q->movedUp(item);
}

void KActionSelectorPrivate::buttonDownClicked()
{
    int c = selectedRowIndex(selectedListWidget);
    if (c < 0 || c == selectedListWidget->count() - 1) {
        return;
    }
    QListWidgetItem *item = selectedListWidget->item(c);
    selectedListWidget->takeItem(c);
    selectedListWidget->insertItem(c + 1, item);
    selectedListWidget->setCurrentItem(item);
    emit q->movedDown(item);
}

void KActionSelectorPrivate::itemDoubleClicked(QListWidgetItem *item)
{
    if (moveOnDoubleClick) {
        moveItem(item);
    }
}

//END Private Slots

//BEGIN Private Methods

void KActionSelectorPrivate::loadIcons()
{
    btnAdd->setIcon(QIcon::fromTheme(addIcon));
    btnRemove->setIcon(QIcon::fromTheme(removeIcon));
    btnUp->setIcon(QIcon::fromTheme(upIcon));
    btnDown->setIcon(QIcon::fromTheme(downIcon));
}

void KActionSelectorPrivate::moveItem(QListWidgetItem *item)
{
    QListWidget *lbFrom = item->listWidget();
    QListWidget *lbTo;
    if (lbFrom == availableListWidget) {
        lbTo = selectedListWidget;
    } else if (lbFrom == selectedListWidget) {
        lbTo = availableListWidget;
    } else { //?! somewhat unlikely...
        return;
    }

    KActionSelector::InsertionPolicy p = (lbTo == availableListWidget) ?
                                         availableInsertionPolicy : selectedInsertionPolicy;

    lbFrom->takeItem(lbFrom->row(item));
    lbTo->insertItem(insertionIndex(lbTo, p), item);
    lbTo->setFocus();
    lbTo->setCurrentItem(item);

    if (p == KActionSelector::Sorted) {
        lbTo->sortItems();
    }
    if (lbTo == selectedListWidget) {
        emit q->added(item);
    } else {
        emit q->removed(item);
    }
}

int KActionSelectorPrivate::insertionIndex(QListWidget *lb, KActionSelector::InsertionPolicy policy)
{
    int index;
    switch (policy) {
    case KActionSelector::BelowCurrent:
        index = lb->currentRow();
        if (index > -1) {
            index += 1;
        }
        break;
    case KActionSelector::AtTop:
        index = 0;
        break;
    default:
        index = -1;
    }
    return index;
}

int KActionSelectorPrivate::selectedRowIndex(QListWidget *lb)
{
    QList<QListWidgetItem *> list = lb->selectedItems();
    if (list.isEmpty()) {
        return -1;
    }
    return lb->row(list.at(0));
}

//END Private Methods
#include "moc_kactionselector.cpp"