1 | /* $Id: UIBootTable.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIBootTable class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* Qt includes: */
|
---|
23 | # include <QScrollBar>
|
---|
24 |
|
---|
25 | /* GUI includes: */
|
---|
26 | # include "UIBootTable.h"
|
---|
27 | # include "UIConverter.h"
|
---|
28 | # include "UIIconPool.h"
|
---|
29 |
|
---|
30 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
31 |
|
---|
32 |
|
---|
33 | /*********************************************************************************************************************************
|
---|
34 | * Class UIBootTableItem implementation. *
|
---|
35 | *********************************************************************************************************************************/
|
---|
36 |
|
---|
37 | UIBootTableItem::UIBootTableItem(KDeviceType enmType)
|
---|
38 | : m_enmType(enmType)
|
---|
39 | {
|
---|
40 | setCheckState(Qt::Unchecked);
|
---|
41 | switch(enmType)
|
---|
42 | {
|
---|
43 | case KDeviceType_Floppy: setIcon(UIIconPool::iconSet(":/fd_16px.png")); break;
|
---|
44 | case KDeviceType_DVD: setIcon(UIIconPool::iconSet(":/cd_16px.png")); break;
|
---|
45 | case KDeviceType_HardDisk: setIcon(UIIconPool::iconSet(":/hd_16px.png")); break;
|
---|
46 | case KDeviceType_Network: setIcon(UIIconPool::iconSet(":/nw_16px.png")); break;
|
---|
47 | default: break; /* Shut up, MSC! */
|
---|
48 | }
|
---|
49 | retranslateUi();
|
---|
50 | }
|
---|
51 |
|
---|
52 | KDeviceType UIBootTableItem::type() const
|
---|
53 | {
|
---|
54 | return m_enmType;
|
---|
55 | }
|
---|
56 |
|
---|
57 | void UIBootTableItem::retranslateUi()
|
---|
58 | {
|
---|
59 | setText(gpConverter->toString(m_enmType));
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /*********************************************************************************************************************************
|
---|
64 | * Class UIBootTable implementation. *
|
---|
65 | *********************************************************************************************************************************/
|
---|
66 |
|
---|
67 | UIBootTable::UIBootTable(QWidget *pParent /* = 0 */)
|
---|
68 | : QIWithRetranslateUI<QListWidget>(pParent)
|
---|
69 | {
|
---|
70 | prepare();
|
---|
71 | }
|
---|
72 |
|
---|
73 | void UIBootTable::adjustSizeToFitContent()
|
---|
74 | {
|
---|
75 | const int iH = 2 * frameWidth();
|
---|
76 | const int iW = iH;
|
---|
77 | setFixedSize(sizeHintForColumn(0) + iW,
|
---|
78 | sizeHintForRow(0) * count() + iH);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void UIBootTable::sltMoveItemUp()
|
---|
82 | {
|
---|
83 | QModelIndex index = currentIndex();
|
---|
84 | moveItemTo(index, index.row() - 1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void UIBootTable::sltMoveItemDown()
|
---|
88 | {
|
---|
89 | QModelIndex index = currentIndex();
|
---|
90 | moveItemTo(index, index.row() + 2);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void UIBootTable::retranslateUi()
|
---|
94 | {
|
---|
95 | for (int i = 0; i < count(); ++i)
|
---|
96 | static_cast<UIBootTableItem*>(item(i))->retranslateUi();
|
---|
97 |
|
---|
98 | adjustSizeToFitContent();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void UIBootTable::dropEvent(QDropEvent *pEvent)
|
---|
102 | {
|
---|
103 | /* Call to base-class: */
|
---|
104 | QListWidget::dropEvent(pEvent);
|
---|
105 | /* Separately notify listeners: */
|
---|
106 | emit sigRowChanged(currentRow());
|
---|
107 | }
|
---|
108 |
|
---|
109 | QModelIndex UIBootTable::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers fModifiers)
|
---|
110 | {
|
---|
111 | if (fModifiers.testFlag(Qt::ControlModifier))
|
---|
112 | {
|
---|
113 | switch (cursorAction)
|
---|
114 | {
|
---|
115 | case QAbstractItemView::MoveUp:
|
---|
116 | {
|
---|
117 | QModelIndex index = currentIndex();
|
---|
118 | return moveItemTo(index, index.row() - 1);
|
---|
119 | }
|
---|
120 | case QAbstractItemView::MoveDown:
|
---|
121 | {
|
---|
122 | QModelIndex index = currentIndex();
|
---|
123 | return moveItemTo(index, index.row() + 2);
|
---|
124 | }
|
---|
125 | case QAbstractItemView::MovePageUp:
|
---|
126 | {
|
---|
127 | QModelIndex index = currentIndex();
|
---|
128 | return moveItemTo(index, qMax(0, index.row() - verticalScrollBar()->pageStep()));
|
---|
129 | }
|
---|
130 | case QAbstractItemView::MovePageDown:
|
---|
131 | {
|
---|
132 | QModelIndex index = currentIndex();
|
---|
133 | return moveItemTo(index, qMin(model()->rowCount(), index.row() + verticalScrollBar()->pageStep() + 1));
|
---|
134 | }
|
---|
135 | case QAbstractItemView::MoveHome:
|
---|
136 | return moveItemTo(currentIndex(), 0);
|
---|
137 | case QAbstractItemView::MoveEnd:
|
---|
138 | return moveItemTo(currentIndex(), model()->rowCount());
|
---|
139 | default:
|
---|
140 | break;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return QListWidget::moveCursor(cursorAction, fModifiers);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void UIBootTable::prepare()
|
---|
147 | {
|
---|
148 | setDragDropMode(QAbstractItemView::InternalMove);
|
---|
149 | setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
150 | setDropIndicatorShown(true);
|
---|
151 | setUniformItemSizes(true);
|
---|
152 | connect(this, &UIBootTable::currentRowChanged,
|
---|
153 | this, &UIBootTable::sigRowChanged);
|
---|
154 | }
|
---|
155 |
|
---|
156 | QModelIndex UIBootTable::moveItemTo(const QModelIndex &index, int row)
|
---|
157 | {
|
---|
158 | /* Check validity: */
|
---|
159 | if (!index.isValid())
|
---|
160 | return QModelIndex();
|
---|
161 |
|
---|
162 | /* Check sanity: */
|
---|
163 | if (row < 0 || row > model()->rowCount())
|
---|
164 | return QModelIndex();
|
---|
165 |
|
---|
166 | QPersistentModelIndex oldIndex(index);
|
---|
167 | UIBootTableItem *pItem = static_cast<UIBootTableItem*>(itemFromIndex(oldIndex));
|
---|
168 | insertItem(row, new UIBootTableItem(*pItem));
|
---|
169 | QPersistentModelIndex newIndex = model()->index(row, 0);
|
---|
170 | delete takeItem(oldIndex.row());
|
---|
171 | setCurrentRow(newIndex.row());
|
---|
172 | return QModelIndex(newIndex);
|
---|
173 | }
|
---|