summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VirtualBox/src/settings/editors/UIShortcutConfigurationEditor.cpp
blob: b7afe17f21735f87168396c579ce40c579b7d95c (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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/* $Id$ */
/** @file
 * VBox Qt GUI - UIShortcutConfigurationEditor class implementation.
 */

/*
 * Copyright (C) 2006-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/* Qt includes: */
#include <QHeaderView>
#include <QItemEditorFactory>
#include <QTabWidget>
#include <QVBoxLayout>

/* GUI includes: */
#include "QIStyledItemDelegate.h"
#include "QITableView.h"
#include "UIActionPool.h"
#include "UICommon.h"
#include "UIExtraDataManager.h"
#include "UIHostComboEditor.h"
#include "UIHotKeyEditor.h"
#include "UIMessageCenter.h"
#include "UIShortcutConfigurationEditor.h"
#include "UIShortcutPool.h"

/* Namespaces: */
using namespace UIExtraDataDefs;


/** Table column indexes. */
enum TableColumnIndex
{
    TableColumnIndex_Description,
    TableColumnIndex_Sequence,
    TableColumnIndex_Max
};


/** QITableViewCell subclass for shortcut configuration editor. */
class UIShortcutTableViewCell : public QITableViewCell
{
    Q_OBJECT;

public:

    /** Constructs table cell on the basis of passed arguments.
      * @param  pParent  Brings the row this cell belongs too.
      * @param  strText  Brings the text describing this cell. */
    UIShortcutTableViewCell(QITableViewRow *pParent, const QString &strText)
        : QITableViewCell(pParent)
        , m_strText(strText)
    {}

    /** Returns the cell text. */
    virtual QString text() const RT_OVERRIDE { return m_strText; }

private:

    /** Holds the cell text. */
    QString m_strText;
};


/** QITableViewRow subclass for shortcut configuration editor. */
class UIShortcutTableViewRow : public QITableViewRow, public UIShortcutConfigurationItem
{
    Q_OBJECT;

public:

    /** Constructs table row on the basis of passed arguments.
      * @param  pParent  Brings the table this row belongs too.
      * @param  item     Brings the item this row is based on. */
    UIShortcutTableViewRow(QITableView *pParent = 0, const UIShortcutConfigurationItem &item = UIShortcutConfigurationItem())
        : QITableViewRow(pParent)
        , UIShortcutConfigurationItem(item)
    {
        createCells();
    }

    /** Constructs table row on the basis of @a another one. */
    UIShortcutTableViewRow(const UIShortcutTableViewRow &another)
        : QITableViewRow(another.table())
        , UIShortcutConfigurationItem(another)
    {
        createCells();
    }

    /** Destructs table row. */
    virtual ~UIShortcutTableViewRow() RT_OVERRIDE
    {
        destroyCells();
    }

    /** Copies a table row from @a another one. */
    UIShortcutTableViewRow &operator=(const UIShortcutTableViewRow &another)
    {
        /* Reassign variables: */
        setTable(another.table());
        UIShortcutConfigurationItem::operator=(another);

        /* Recreate cells: */
        destroyCells();
        createCells();

        /* Return this: */
        return *this;
    }

    /** Returns whether this row equals to @a another one. */
    bool operator==(const UIShortcutTableViewRow &another) const
    {
        /* Compare variables: */
        return UIShortcutConfigurationItem::operator==(another);
    }

protected:

    /** Returns the number of children. */
    virtual int childCount() const RT_OVERRIDE
    {
        return TableColumnIndex_Max;
    }

    /** Returns the child item with @a iIndex. */
    virtual QITableViewCell *childItem(int iIndex) const RT_OVERRIDE
    {
        switch (iIndex)
        {
            case TableColumnIndex_Description: return m_cells.first;
            case TableColumnIndex_Sequence: return m_cells.second;
            default: break;
        }
        return 0;
    }

private:

    /** Creates cells. */
    void createCells()
    {
        /* Create cells on the basis of description and current sequence: */
        m_cells = qMakePair(new UIShortcutTableViewCell(this, description()),
                            new UIShortcutTableViewCell(this, currentSequence()));
    }

    /** Destroys cells. */
    void destroyCells()
    {
        /* Destroy cells: */
        delete m_cells.first;
        delete m_cells.second;
        m_cells.first = 0;
        m_cells.second = 0;
    }

    /** Holds the cell instances. */
    QPair<UIShortcutTableViewCell*, UIShortcutTableViewCell*> m_cells;
};

/** Shortcut configuration editor row list. */
typedef QList<UIShortcutTableViewRow> UIShortcutTableViewContent;


/** Shortcut item sorting functor. */
class UIShortcutItemSortingFunctor
{
public:

    /** Constructs shortcut item sorting functor.
      * @param  iColumn     Brings the column sorting should be done according to.
      * @param  m_enmOrder  Brings the sorting order to be applied. */
    UIShortcutItemSortingFunctor(int iColumn, Qt::SortOrder enmOrder)
        : m_iColumn(iColumn)
        , m_enmOrder(enmOrder)
    {}

    /** Returns whether the @a item1 is more/less than the @a item2.
      * @note  Order depends on the one set through constructor, stored in m_enmOrder. */
    bool operator()(const UIShortcutTableViewRow &item1, const UIShortcutTableViewRow &item2)
    {
        switch (m_iColumn)
        {
            case TableColumnIndex_Description:
                return   m_enmOrder == Qt::AscendingOrder
                       ? item1.description() < item2.description()
                       : item1.description() > item2.description();
            case TableColumnIndex_Sequence:
                return   m_enmOrder == Qt::AscendingOrder
                       ? item1.currentSequence() < item2.currentSequence()
                       : item1.currentSequence() > item2.currentSequence();
            default:
                break;
        }
        return   m_enmOrder == Qt::AscendingOrder
               ? item1.key() < item2.key()
               : item1.key() > item2.key();
    }

private:

    /** Holds the column sorting should be done according to. */
    int            m_iColumn;
    /** Holds the sorting order to be applied. */
    Qt::SortOrder  m_enmOrder;
};


/** QAbstractTableModel subclass representing shortcut configuration model. */
class UIShortcutConfigurationModel : public QAbstractTableModel
{
    Q_OBJECT;

signals:

    /** Notifies about shortcuts loaded. */
    void sigShortcutsLoaded();
    /** Notifies about data changed. */
    void sigDataChanged();

public:

    /** Constructs model passing @a pParent to the base-class.
      * @param  enmType  Brings the action-pool type this model is related to. */
    UIShortcutConfigurationModel(QObject *pParent, UIActionPoolType enmType);

    /** Defines the parent @a pTable reference. */
    void setTable(UIShortcutConfigurationTable *pTable);

    /** Returns the number of children. */
    int childCount() const;
    /** Returns the child item with @a iIndex. */
    QITableViewRow *childItem(int iIndex);

    /** Loads a @a list of shortcuts to the model. */
    void load(const UIShortcutConfigurationList &list);
    /** Saves the model shortcuts to a @a list. */
    void save(UIShortcutConfigurationList &list);

    /** Returns whether all shortcuts unique. */
    bool isAllShortcutsUnique();

public slots:

    /** Handle filtering @a strText change. */
    void sltHandleFilterTextChange(const QString &strText);

protected:

    /** Returns the number of rows under the given @a parent. */
    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const RT_OVERRIDE;
    /** Returns the number of columns under the given @a parent. */
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const RT_OVERRIDE;

    /** Returns the item flags for the given @a index. */
    virtual Qt::ItemFlags flags(const QModelIndex &index) const RT_OVERRIDE;
    /** Returns the data for the given @a iRole and @a iSection in the header with the specified @a enmOrientation. */
    virtual QVariant headerData(int iSection, Qt::Orientation enmOrientation, int iRole = Qt::DisplayRole) const RT_OVERRIDE;
    /** Returns the data stored under the given @a iRole for the item referred to by the @a index. */
    virtual QVariant data(const QModelIndex &index, int iRole = Qt::DisplayRole) const RT_OVERRIDE;
    /** Sets the @a iRole data for the item at @a index to @a value. */
    virtual bool setData(const QModelIndex &index, const QVariant &value, int iRole = Qt::EditRole) RT_OVERRIDE;

    /** Sorts the model by @a iColumn in the given @a enmOrder. */
    virtual void sort(int iColumn, Qt::SortOrder enmOrder = Qt::AscendingOrder) RT_OVERRIDE;

private:

    /** Applies filter. */
    void applyFilter();

    /** Holds the action-pool type this model is related to. */
    UIActionPoolType  m_enmType;

    /** Holds the parent table reference. */
    UIShortcutConfigurationTable *m_pTable;

    /** Holds current filter. */
    QString  m_strFilter;

    /** Holds current shortcut list. */
    UIShortcutTableViewContent  m_shortcuts;
    /** Holds current filtered shortcut list. */
    UIShortcutTableViewContent  m_filteredShortcuts;

    /** Holds a set of currently duplicated sequences. */
    QSet<QString>  m_duplicatedSequences;
};


/** QITableView subclass representing shortcut configuration table. */
class UIShortcutConfigurationTable : public QITableView
{
    Q_OBJECT;

public:

    /** Constructs table passing @a pParent to the base-class.
      * @param  pModel         Brings the model this table is bound to.
      * @param  strObjectName  Brings the object name this table has, required for fast referencing. */
    UIShortcutConfigurationTable(QWidget *pParent, UIShortcutConfigurationModel *pModel, const QString &strObjectName);
    /** Destructs table. */
    virtual ~UIShortcutConfigurationTable() RT_OVERRIDE;

protected:

    /** Returns the number of children. */
    virtual int childCount() const RT_OVERRIDE;
    /** Returns the child item with @a iIndex. */
    virtual QITableViewRow *childItem(int iIndex) const RT_OVERRIDE;

private slots:

    /** Handles shortcuts loaded signal. */
    void sltHandleShortcutsLoaded();

private:

    /** Prepares all. */
    void prepare();
    /** Cleanups all. */
    void cleanup();

    /** Holds the item editor factory instance. */
    QItemEditorFactory *m_pItemEditorFactory;
};


/*********************************************************************************************************************************
*   Class UIShortcutConfigurationModel implementation.                                                                           *
*********************************************************************************************************************************/

UIShortcutConfigurationModel::UIShortcutConfigurationModel(QObject *pParent, UIActionPoolType enmType)
    : QAbstractTableModel(pParent)
    , m_enmType(enmType)
    , m_pTable(0)
{
}

void UIShortcutConfigurationModel::setTable(UIShortcutConfigurationTable *pTable)
{
    m_pTable = pTable;
}

int UIShortcutConfigurationModel::childCount() const
{
    /* Return row count: */
    return rowCount();
}

QITableViewRow *UIShortcutConfigurationModel::childItem(int iIndex)
{
    /* Make sure index is within the bounds: */
    AssertReturn(iIndex >= 0 && iIndex < m_filteredShortcuts.size(), 0);
    /* Return corresponding filtered row: */
    return &m_filteredShortcuts[iIndex];
}

void UIShortcutConfigurationModel::load(const UIShortcutConfigurationList &list)
{
    /* Load a list of passed shortcuts: */
    foreach (const UIShortcutConfigurationItem &item, list)
    {
        /* Filter out unnecessary items: */
        if (   (m_enmType == UIActionPoolType_Manager && item.key().startsWith(GUI_Input_MachineShortcuts))
            || (m_enmType == UIActionPoolType_Runtime && item.key().startsWith(GUI_Input_SelectorShortcuts)))
            continue;
        /* Add suitable item to the model as a new shortcut: */
        m_shortcuts << UIShortcutTableViewRow(m_pTable, item);
    }
    /* Apply filter: */
    applyFilter();
    /* Notify table: */
    emit sigShortcutsLoaded();
}

void UIShortcutConfigurationModel::save(UIShortcutConfigurationList &list)
{
    /* Save cached model shortcuts: */
    foreach (const UIShortcutTableViewRow &row, m_shortcuts)
    {
        const UIShortcutConfigurationItem &item = row;

        /* Search for corresponding item position: */
        const int iShortcutItemPosition = UIShortcutSearchFunctor<UIShortcutConfigurationItem>()(list, item);
        /* Make sure position is valid: */
        if (iShortcutItemPosition == -1)
            continue;
        /* Save cached model shortcut to a list: */
        list[iShortcutItemPosition] = item;
    }
}

bool UIShortcutConfigurationModel::isAllShortcutsUnique()
{
    /* Enumerate all the sequences: */
    QMultiMap<QString, QString> usedSequences;
    foreach (const UIShortcutTableViewRow &item, m_shortcuts)
    {
        QString strKey = item.currentSequence();
        if (!strKey.isEmpty())
        {
            const QString strScope = item.scope();
            strKey = strScope.isNull() ? strKey : QString("%1: %2").arg(strScope, strKey);
            usedSequences.insert(strKey, item.key());
        }
    }
    /* Enumerate all the duplicated sequences: */
    QSet<QString> duplicatedSequences;
    foreach (const QString &strKey, usedSequences.keys())
        if (usedSequences.count(strKey) > 1)
        {
            foreach (const QString &strValue, usedSequences.values(strKey))
                duplicatedSequences |= strValue;
        }
    /* Is there something changed? */
    if (m_duplicatedSequences != duplicatedSequences)
    {
        m_duplicatedSequences = duplicatedSequences;
        emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
    }
    /* Are there duplicated shortcuts? */
    if (!m_duplicatedSequences.isEmpty())
        return false;
    /* True by default: */
    return true;
}

void UIShortcutConfigurationModel::sltHandleFilterTextChange(const QString &strText)
{
    m_strFilter = strText;
    applyFilter();
}

int UIShortcutConfigurationModel::rowCount(const QModelIndex& /* parent = QModelIndex() */) const
{
    return m_filteredShortcuts.size();
}

int UIShortcutConfigurationModel::columnCount(const QModelIndex& /* parent = QModelIndex() */) const
{
    return TableColumnIndex_Max;
}

Qt::ItemFlags UIShortcutConfigurationModel::flags(const QModelIndex &index) const
{
    /* No flags for invalid index: */
    if (!index.isValid())
        return Qt::NoItemFlags;
    /* Switch for different columns: */
    switch (index.column())
    {
        case TableColumnIndex_Description: return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
        case TableColumnIndex_Sequence: return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
        default: break;
    }
    /* No flags by default: */
    return Qt::NoItemFlags;
}

QVariant UIShortcutConfigurationModel::headerData(int iSection,
                                                  Qt::Orientation enmOrientation,
                                                  int iRole /* = Qt::DisplayRole */) const
{
    /* Switch for different roles: */
    switch (iRole)
    {
        case Qt::DisplayRole:
        {
            /* Invalid for vertical header: */
            if (enmOrientation == Qt::Vertical)
                return QString();
            /* Switch for different columns: */
            switch (iSection)
            {
                case TableColumnIndex_Description: return UIShortcutConfigurationEditor::tr("Name");
                case TableColumnIndex_Sequence: return UIShortcutConfigurationEditor::tr("Shortcut");
                default: break;
            }
            /* Invalid for other cases: */
            return QString();
        }
        default:
            break;
    }
    /* Invalid by default: */
    return QVariant();
}

QVariant UIShortcutConfigurationModel::data(const QModelIndex &index, int iRole /* = Qt::DisplayRole */) const
{
    /* No data for invalid index: */
    if (!index.isValid())
        return QVariant();
    const int iIndex = index.row();
    /* Switch for different roles: */
    switch (iRole)
    {
        case Qt::DisplayRole:
        {
            /* Switch for different columns: */
            switch (index.column())
            {
                case TableColumnIndex_Description:
                {
                    /* Return shortcut scope and description: */
                    const QString strScope = m_filteredShortcuts[iIndex].scope();
                    const QString strDescription = m_filteredShortcuts[iIndex].description();
                    return strScope.isNull() ? strDescription : QString("%1: %2").arg(strScope, strDescription);
                }
                case TableColumnIndex_Sequence:
                {
                    /* If that is host-combo cell: */
                    if (m_filteredShortcuts[iIndex].key() == UIHostCombo::hostComboCacheKey())
                        /* We should return host-combo: */
                        return UIHostCombo::toReadableString(m_filteredShortcuts[iIndex].currentSequence());
                    /* In other cases we should return hot-combo: */
                    QString strHotCombo = m_filteredShortcuts[iIndex].currentSequence();
                    /* But if that is machine table and hot-combo is not empty: */
                    if (m_enmType == UIActionPoolType_Runtime && !strHotCombo.isEmpty())
                        /* We should prepend it with Host+ prefix: */
                        strHotCombo.prepend(UIHostCombo::hostComboModifierName());
                    /* Return what we've got: */
                    return strHotCombo;
                }
                default: break;
            }
            /* Invalid for other cases: */
            return QString();
        }
        case Qt::EditRole:
        {
            /* Switch for different columns: */
            switch (index.column())
            {
                case TableColumnIndex_Sequence:
                    return   m_filteredShortcuts[iIndex].key() == UIHostCombo::hostComboCacheKey()
                           ? QVariant::fromValue(UIHostComboWrapper(m_filteredShortcuts[iIndex].currentSequence()))
                           : QVariant::fromValue(UIHotKey(  m_enmType == UIActionPoolType_Runtime
                                                          ? UIHotKeyType_Simple
                                                          : UIHotKeyType_WithModifiers,
                                                          m_filteredShortcuts[iIndex].currentSequence(),
                                                          m_filteredShortcuts[iIndex].defaultSequence()));
                default:
                    break;
            }
            /* Invalid for other cases: */
            return QString();
        }
        case Qt::FontRole:
        {
            /* Do we have a default font? */
            QFont font(QApplication::font());
            /* Switch for different columns: */
            switch (index.column())
            {
                case TableColumnIndex_Sequence:
                {
                    if (   m_filteredShortcuts[iIndex].key() != UIHostCombo::hostComboCacheKey()
                        && m_filteredShortcuts[iIndex].currentSequence() != m_filteredShortcuts[iIndex].defaultSequence())
                        font.setBold(true);
                    break;
                }
                default: break;
            }
            /* Return resulting font: */
            return font;
        }
        case Qt::ForegroundRole:
        {
            /* Switch for different columns: */
            switch (index.column())
            {
                case TableColumnIndex_Sequence:
                {
                    if (m_duplicatedSequences.contains(m_filteredShortcuts[iIndex].key()))
                        return QBrush(Qt::red);
                    break;
                }
                default: break;
            }
            /* Default for other cases: */
            return QString();
        }
        default: break;
    }
    /* Invalid by default: */
    return QVariant();
}

bool UIShortcutConfigurationModel::setData(const QModelIndex &index, const QVariant &value, int iRole /* = Qt::EditRole */)
{
    /* Nothing to set for invalid index: */
    if (!index.isValid())
        return false;
    /* Switch for different roles: */
    switch (iRole)
    {
        case Qt::EditRole:
        {
            /* Switch for different columns: */
            switch (index.column())
            {
                case TableColumnIndex_Sequence:
                {
                    /* Get index: */
                    const int iIndex = index.row();
                    /* Set sequence to shortcut: */
                    UIShortcutTableViewRow &filteredShortcut = m_filteredShortcuts[iIndex];
                    const int iShortcutIndex = UIShortcutSearchFunctor<UIShortcutTableViewRow>()(m_shortcuts, filteredShortcut);
                    if (iShortcutIndex != -1)
                    {
                        filteredShortcut.setCurrentSequence(  filteredShortcut.key() == UIHostCombo::hostComboCacheKey()
                                                            ? value.value<UIHostComboWrapper>().toString()
                                                            : value.value<UIHotKey>().sequence());
                        m_shortcuts[iShortcutIndex] = filteredShortcut;
                        emit sigDataChanged();
                        return true;
                    }
                    break;
                }
                default:
                    break;
            }
            break;
        }
        default:
            break;
    }
    /* Nothing to set by default: */
    return false;
}

void UIShortcutConfigurationModel::sort(int iColumn, Qt::SortOrder order /* = Qt::AscendingOrder */)
{
    /* Sort whole the list: */
    std::stable_sort(m_shortcuts.begin(), m_shortcuts.end(), UIShortcutItemSortingFunctor(iColumn, order));
    /* Make sure host-combo item is always the first one: */
    UIShortcutConfigurationItem fakeHostComboItem(UIHostCombo::hostComboCacheKey(), QString(), QString(), QString(), QString());
    UIShortcutTableViewRow fakeHostComboTableViewRow(0, fakeHostComboItem);
    const int iIndexOfHostComboItem = UIShortcutSearchFunctor<UIShortcutTableViewRow>()(m_shortcuts, fakeHostComboTableViewRow);
    if (iIndexOfHostComboItem != -1)
    {
        UIShortcutTableViewRow hostComboItem = m_shortcuts.takeAt(iIndexOfHostComboItem);
        m_shortcuts.prepend(hostComboItem);
    }
    /* Apply the filter: */
    applyFilter();
    /* Notify the model: */
    emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
}

void UIShortcutConfigurationModel::applyFilter()
{
    /* Erase items first if necessary: */
    if (!m_filteredShortcuts.isEmpty())
    {
        beginRemoveRows(QModelIndex(), 0, m_filteredShortcuts.size() - 1);
        m_filteredShortcuts.clear();
        endRemoveRows();
    }

    /* If filter is empty: */
    if (m_strFilter.isEmpty())
    {
        /* Just add all the items: */
        m_filteredShortcuts = m_shortcuts;
    }
    else
    {
        /* Check if the description matches the filter: */
        foreach (const UIShortcutTableViewRow &item, m_shortcuts)
        {
            /* If neither scope nor description or sequence matches the filter, skip item: */
            if (   !item.scope().contains(m_strFilter, Qt::CaseInsensitive)
                && !item.description().contains(m_strFilter, Qt::CaseInsensitive)
                && !item.currentSequence().contains(m_strFilter, Qt::CaseInsensitive))
                continue;
            /* Add that item: */
            m_filteredShortcuts << item;
        }
    }

    /* Add items finally if necessary: */
    if (!m_filteredShortcuts.isEmpty())
    {
        beginInsertRows(QModelIndex(), 0, m_filteredShortcuts.size() - 1);
        endInsertRows();
    }
}


/*********************************************************************************************************************************
*   Class UIShortcutConfigurationTable implementation.                                                                           *
*********************************************************************************************************************************/

UIShortcutConfigurationTable::UIShortcutConfigurationTable(QWidget *pParent,
                                                           UIShortcutConfigurationModel *pModel,
                                                           const QString &strObjectName)
    : QITableView(pParent)
    , m_pItemEditorFactory(0)
{
    /* Set object name: */
    setObjectName(strObjectName);
    /* Set model: */
    setModel(pModel);

    /* Prepare all: */
    prepare();
}

UIShortcutConfigurationTable::~UIShortcutConfigurationTable()
{
    /* Cleanup all: */
    cleanup();
}

int UIShortcutConfigurationTable::childCount() const
{
    /* Redirect request to table model: */
    return qobject_cast<UIShortcutConfigurationModel*>(model())->childCount();
}

QITableViewRow *UIShortcutConfigurationTable::childItem(int iIndex) const
{
    /* Redirect request to table model: */
    return qobject_cast<UIShortcutConfigurationModel*>(model())->childItem(iIndex);
}

void UIShortcutConfigurationTable::sltHandleShortcutsLoaded()
{
    /* Resize columns to feat contents: */
    resizeColumnsToContents();

    /* Configure sorting: */
    sortByColumn(TableColumnIndex_Description, Qt::AscendingOrder);
    setSortingEnabled(true);
}

void UIShortcutConfigurationTable::prepare()
{
    /* Configure self: */
    setTabKeyNavigation(false);
    setContextMenuPolicy(Qt::CustomContextMenu);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setSelectionMode(QAbstractItemView::SingleSelection);
    setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::SelectedClicked);

    /* Configure headers: */
    verticalHeader()->hide();
    verticalHeader()->setDefaultSectionSize((int)(verticalHeader()->minimumSectionSize() * 1.33));
    horizontalHeader()->setStretchLastSection(false);
    horizontalHeader()->setSectionResizeMode(TableColumnIndex_Description, QHeaderView::Interactive);
    horizontalHeader()->setSectionResizeMode(TableColumnIndex_Sequence, QHeaderView::Stretch);

    /* Connect model: */
    UIShortcutConfigurationModel *pHotKeyTableModel = qobject_cast<UIShortcutConfigurationModel*>(model());
    if (pHotKeyTableModel)
        connect(pHotKeyTableModel, &UIShortcutConfigurationModel::sigShortcutsLoaded,
                this, &UIShortcutConfigurationTable::sltHandleShortcutsLoaded);

    /* Check if we do have proper item delegate: */
    QIStyledItemDelegate *pStyledItemDelegate = qobject_cast<QIStyledItemDelegate*>(itemDelegate());
    if (pStyledItemDelegate)
    {
        /* Configure item delegate: */
        pStyledItemDelegate->setWatchForEditorDataCommits(true);

        /* Create new item editor factory: */
        m_pItemEditorFactory = new QItemEditorFactory;
        if (m_pItemEditorFactory)
        {
            /* Register UIHotKeyEditor as the UIHotKey editor: */
            int iHotKeyTypeId = qRegisterMetaType<UIHotKey>();
            QStandardItemEditorCreator<UIHotKeyEditor> *pHotKeyItemEditorCreator = new QStandardItemEditorCreator<UIHotKeyEditor>();
            m_pItemEditorFactory->registerEditor((QVariant::Type)iHotKeyTypeId, pHotKeyItemEditorCreator);

            /* Register UIHostComboEditor as the UIHostComboWrapper editor: */
            int iHostComboTypeId = qRegisterMetaType<UIHostComboWrapper>();
            QStandardItemEditorCreator<UIHostComboEditor> *pHostComboItemEditorCreator = new QStandardItemEditorCreator<UIHostComboEditor>();
            m_pItemEditorFactory->registerEditor((QVariant::Type)iHostComboTypeId, pHostComboItemEditorCreator);

            /* Assign configured item editor factory to item delegate: */
            pStyledItemDelegate->setItemEditorFactory(m_pItemEditorFactory);
        }
    }
}

void UIShortcutConfigurationTable::cleanup()
{
    /* Cleanup item editor factory: */
    delete m_pItemEditorFactory;
    m_pItemEditorFactory = 0;
}


/*********************************************************************************************************************************
*   Class UIShortcutConfigurationEditor implementation.                                                                          *
*********************************************************************************************************************************/

UIShortcutConfigurationEditor::UIShortcutConfigurationEditor(QWidget *pParent /* = 0 */)
    : QIWithRetranslateUI<QWidget>(pParent)
    , m_pModelManager(0)
    , m_pModelRuntime(0)
    , m_pTabWidget(0)
    , m_pEditorFilterManager(0), m_pTableManager(0)
    , m_pEditorFilterRuntime(0), m_pTableRuntime(0)
{
    prepare();
}

void UIShortcutConfigurationEditor::load(const UIShortcutConfigurationList &value)
{
    m_pModelManager->load(value);
    m_pModelRuntime->load(value);
}

void UIShortcutConfigurationEditor::save(UIShortcutConfigurationList &value) const
{
    m_pModelManager->save(value);
    m_pModelRuntime->save(value);
}

bool UIShortcutConfigurationEditor::isShortcutsUniqueManager() const
{
    return m_pModelManager->isAllShortcutsUnique();
}

bool UIShortcutConfigurationEditor::isShortcutsUniqueRuntime() const
{
    return m_pModelRuntime->isAllShortcutsUnique();
}

QString UIShortcutConfigurationEditor::tabNameManager() const
{
    return m_pTabWidget->tabText(TableIndex_Manager);
}

QString UIShortcutConfigurationEditor::tabNameRuntime() const
{
    return m_pTabWidget->tabText(TableIndex_Runtime);
}

void UIShortcutConfigurationEditor::retranslateUi()
{
    m_pTabWidget->setTabText(TableIndex_Manager, tr("&VirtualBox Manager"));
    m_pTabWidget->setTabText(TableIndex_Runtime, tr("Virtual &Machine"));
    m_pTableManager->setWhatsThis(tr("Lists all available shortcuts which can be configured."));
    m_pTableRuntime->setWhatsThis(tr("Lists all available shortcuts which can be configured."));
    m_pEditorFilterManager->setToolTip(tr("Holds a sequence to filter the shortcut list."));
    m_pEditorFilterRuntime->setToolTip(tr("Holds a sequence to filter the shortcut list."));
}

void UIShortcutConfigurationEditor::prepare()
{
    /* Prepare everything: */
    prepareWidgets();
    prepareConnections();

    /* Apply language settings: */
    retranslateUi();
}

void UIShortcutConfigurationEditor::prepareWidgets()
{
    /* Prepare main layout: */
    QVBoxLayout *pMainLayout = new QVBoxLayout(this);
    if (pMainLayout)
    {
        pMainLayout->setContentsMargins(0, 0, 0, 0);

        /* Prepare tab-widget: */
        m_pTabWidget = new QTabWidget(this);
        if (m_pTabWidget)
        {
            /* Prepare 'Manager UI' tab: */
            prepareTabManager();
            /* Prepare 'Runtime UI' tab: */
            prepareTabRuntime();

            /* Add tab-widget into layout: */
            pMainLayout->addWidget(m_pTabWidget);
        }
    }
}

void UIShortcutConfigurationEditor::prepareTabManager()
{
    /* Prepare Manager UI tab: */
    QWidget *pTabManager = new QWidget;
    if (pTabManager)
    {
        /* Prepare Manager UI layout: */
        QVBoxLayout *pLayoutManager = new QVBoxLayout(pTabManager);
        if (pLayoutManager)
        {
            pLayoutManager->setSpacing(1);
#ifdef VBOX_WS_MAC
            /* On Mac OS X and X11 we can do a bit of smoothness: */
            pLayoutManager->setContentsMargins(0, 0, 0, 0);
#endif

            /* Prepare Manager UI filter editor: */
            m_pEditorFilterManager = new QLineEdit(pTabManager);
            if (m_pEditorFilterManager)
                pLayoutManager->addWidget(m_pEditorFilterManager);

            /* Prepare Manager UI model: */
            m_pModelManager = new UIShortcutConfigurationModel(this, UIActionPoolType_Manager);

            /* Prepare Manager UI table: */
            m_pTableManager = new UIShortcutConfigurationTable(pTabManager, m_pModelManager, "m_pTableManager");
            if (m_pTableManager)
            {
                m_pModelManager->setTable(m_pTableManager);
                pLayoutManager->addWidget(m_pTableManager);
            }
        }

        m_pTabWidget->insertTab(TableIndex_Manager, pTabManager, QString());
    }
}

void UIShortcutConfigurationEditor::prepareTabRuntime()
{
    /* Create Runtime UI tab: */
    QWidget *pTabMachine = new QWidget;
    if (pTabMachine)
    {
        /* Prepare Runtime UI layout: */
        QVBoxLayout *pLayoutMachine = new QVBoxLayout(pTabMachine);
        if (pLayoutMachine)
        {
            pLayoutMachine->setSpacing(1);
#ifdef VBOX_WS_MAC
            /* On Mac OS X and X11 we can do a bit of smoothness: */
            pLayoutMachine->setContentsMargins(0, 0, 0, 0);
#endif

            /* Prepare Runtime UI filter editor: */
            m_pEditorFilterRuntime = new QLineEdit(pTabMachine);
            if (m_pEditorFilterRuntime)
                pLayoutMachine->addWidget(m_pEditorFilterRuntime);

            /* Prepare Runtime UI model: */
            m_pModelRuntime = new UIShortcutConfigurationModel(this, UIActionPoolType_Runtime);

            /* Create Runtime UI table: */
            m_pTableRuntime = new UIShortcutConfigurationTable(pTabMachine, m_pModelRuntime, "m_pTableRuntime");
            if (m_pTableRuntime)
            {
                m_pModelRuntime->setTable(m_pTableRuntime);
                pLayoutMachine->addWidget(m_pTableRuntime);
            }
        }

        m_pTabWidget->insertTab(TableIndex_Runtime, pTabMachine, QString());

        /* In the VM process we start by displaying the Runtime UI tab: */
        if (uiCommon().uiType() == UICommon::UIType_RuntimeUI)
            m_pTabWidget->setCurrentWidget(pTabMachine);
    }
}

void UIShortcutConfigurationEditor::prepareConnections()
{
    /* Configure 'Manager UI' connections: */
    connect(m_pEditorFilterManager, &QLineEdit::textChanged,
            m_pModelManager, &UIShortcutConfigurationModel::sltHandleFilterTextChange);
    connect(m_pModelManager, &UIShortcutConfigurationModel::sigDataChanged,
            this, &UIShortcutConfigurationEditor::sigValueChanged);

    /* Configure 'Runtime UI' connections: */
    connect(m_pEditorFilterRuntime, &QLineEdit::textChanged,
            m_pModelRuntime, &UIShortcutConfigurationModel::sltHandleFilterTextChange);
    connect(m_pModelRuntime, &UIShortcutConfigurationModel::sigDataChanged,
            this, &UIShortcutConfigurationEditor::sigValueChanged);
}

# include "UIShortcutConfigurationEditor.moc"