1 | /* $Id: VBoxOSTypeSelectorWidget.cpp 33781 2010-11-04 15:59:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * VBoxOSTypeSelectorWidget class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "VBoxOSTypeSelectorWidget.h"
|
---|
21 |
|
---|
22 | #include <QComboBox>
|
---|
23 | #include <QLabel>
|
---|
24 | #include <QVBoxLayout>
|
---|
25 |
|
---|
26 | enum
|
---|
27 | {
|
---|
28 | RoleTypeID = Qt::UserRole + 1
|
---|
29 | };
|
---|
30 |
|
---|
31 | VBoxOSTypeSelectorWidget::VBoxOSTypeSelectorWidget (QWidget *aParent)
|
---|
32 | : QIWithRetranslateUI <QWidget> (aParent)
|
---|
33 | , mTxFamilyName (new QLabel (this))
|
---|
34 | , mTxTypeName (new QLabel (this))
|
---|
35 | , mPxTypeIcon (new QLabel (this))
|
---|
36 | , mCbFamily (new QComboBox (this))
|
---|
37 | , mCbType (new QComboBox (this))
|
---|
38 | , mLayoutPosition (-1)
|
---|
39 | , mLayoutActivated (false)
|
---|
40 | {
|
---|
41 | /* Register CGuestOSType type */
|
---|
42 | qRegisterMetaType<CGuestOSType>();
|
---|
43 |
|
---|
44 | /* Setup widgets */
|
---|
45 | mTxFamilyName->setAlignment (Qt::AlignRight);
|
---|
46 | mTxTypeName->setAlignment (Qt::AlignRight);
|
---|
47 | mTxFamilyName->setBuddy (mCbFamily);
|
---|
48 | mTxTypeName->setBuddy (mCbType);
|
---|
49 | mTxFamilyName->setSizePolicy (QSizePolicy::Minimum, QSizePolicy::Fixed);
|
---|
50 | mTxTypeName->setSizePolicy (QSizePolicy::Minimum, QSizePolicy::Fixed);
|
---|
51 | mCbFamily->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Fixed);
|
---|
52 | mCbType->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Fixed);
|
---|
53 | mPxTypeIcon->setFixedSize (32, 32);
|
---|
54 |
|
---|
55 | /* Check if host supports (AMD-V or VT-x) and long mode */
|
---|
56 | CHost host = vboxGlobal().virtualBox().GetHost();
|
---|
57 | m_fSupportsHWVirtEx = host.GetProcessorFeature(KProcessorFeature_HWVirtEx);
|
---|
58 | m_fSupportsLongMode = host.GetProcessorFeature(KProcessorFeature_LongMode);
|
---|
59 |
|
---|
60 | /* Fill OS family selector */
|
---|
61 | int maximumSize = 0;
|
---|
62 | QFontMetrics fm (mCbFamily->font());
|
---|
63 | QList <CGuestOSType> families (vboxGlobal().vmGuestOSFamilyList());
|
---|
64 | for (int i = 0; i < families.size(); ++ i)
|
---|
65 | {
|
---|
66 | /* Search for maximum length among family names */
|
---|
67 | QString familyName (families [i].GetFamilyDescription());
|
---|
68 | maximumSize = maximumSize < fm.width (familyName) ?
|
---|
69 | fm.width (familyName) : maximumSize;
|
---|
70 | mCbFamily->insertItem (i, familyName);
|
---|
71 | mCbFamily->setItemData (i, families [i].GetFamilyId(), RoleTypeID);
|
---|
72 | /* Search for maximum length among type names */
|
---|
73 | QList <CGuestOSType> types (vboxGlobal().vmGuestOSTypeList (families [i].GetFamilyId()));
|
---|
74 | for (int j = 0; j < types.size(); ++ j)
|
---|
75 | {
|
---|
76 | QString typeName (types [j].GetDescription());
|
---|
77 | maximumSize = maximumSize < fm.width (typeName) ?
|
---|
78 | fm.width (typeName) : maximumSize;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | mCbFamily->setCurrentIndex (0);
|
---|
82 | onFamilyChanged (mCbFamily->currentIndex());
|
---|
83 |
|
---|
84 | /* Set the minimum size for OS Type & Family selectors. */
|
---|
85 | QStyleOptionComboBox options;
|
---|
86 | options.initFrom (mCbFamily);
|
---|
87 | QSize size (style()->sizeFromContents (QStyle::CT_ComboBox, &options,
|
---|
88 | QSize (maximumSize, fm.height()), mCbFamily));
|
---|
89 | mCbFamily->setMinimumWidth (size.width());
|
---|
90 | mCbType->setMinimumWidth (size.width());
|
---|
91 |
|
---|
92 | /* Slots connections */
|
---|
93 | connect (mCbFamily, SIGNAL (currentIndexChanged (int)),
|
---|
94 | this, SLOT (onFamilyChanged (int)));
|
---|
95 | connect (mCbType, SIGNAL (currentIndexChanged (int)),
|
---|
96 | this, SLOT (onTypeChanged (int)));
|
---|
97 |
|
---|
98 | /* Retranslate */
|
---|
99 | retranslateUi();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void VBoxOSTypeSelectorWidget::setType (const CGuestOSType &aType)
|
---|
103 | {
|
---|
104 | QString familyId (aType.GetFamilyId());
|
---|
105 | QString typeId (aType.GetId());
|
---|
106 |
|
---|
107 | int familyIndex = mCbFamily->findData (familyId, RoleTypeID);
|
---|
108 | AssertMsg (familyIndex != -1, ("Family ID should be valid: '%s'", familyId.toLatin1().constData()));
|
---|
109 | if (familyIndex != -1)
|
---|
110 | mCbFamily->setCurrentIndex (familyIndex);
|
---|
111 |
|
---|
112 | int typeIndex = mCbType->findData (typeId, RoleTypeID);
|
---|
113 | AssertMsg (typeIndex != -1, ("Type ID should be valid: '%s'", typeId.toLatin1().constData()));
|
---|
114 | if (typeIndex != -1)
|
---|
115 | mCbType->setCurrentIndex (typeIndex);
|
---|
116 | }
|
---|
117 |
|
---|
118 | CGuestOSType VBoxOSTypeSelectorWidget::type() const
|
---|
119 | {
|
---|
120 | return mType;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void VBoxOSTypeSelectorWidget::setLayoutPosition (int aPos)
|
---|
124 | {
|
---|
125 | mLayoutPosition = aPos;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void VBoxOSTypeSelectorWidget::activateLayout()
|
---|
129 | {
|
---|
130 | if (mLayoutActivated)
|
---|
131 | return;
|
---|
132 |
|
---|
133 | mLayoutActivated = true;
|
---|
134 |
|
---|
135 | /* Layouting widgets */
|
---|
136 | QVBoxLayout *layout1 = new QVBoxLayout();
|
---|
137 | layout1->setSpacing (0);
|
---|
138 | layout1->addWidget (mPxTypeIcon);
|
---|
139 | layout1->addStretch();
|
---|
140 |
|
---|
141 | QGridLayout *layout2 = layout() ? static_cast <QGridLayout*> (layout()) :
|
---|
142 | new QGridLayout (this);
|
---|
143 | layout2->setMargin (0);
|
---|
144 | int row = mLayoutPosition == -1 ? layout2->rowCount() : mLayoutPosition;
|
---|
145 | layout2->addWidget (mTxFamilyName, row, 0);
|
---|
146 | layout2->addWidget (mCbFamily, row, 1);
|
---|
147 | layout2->addWidget (mTxTypeName, row + 1, 0);
|
---|
148 | layout2->addWidget (mCbType, row + 1, 1);
|
---|
149 | layout2->addLayout (layout1, row, 2, 2, 1);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void VBoxOSTypeSelectorWidget::retranslateUi()
|
---|
153 | {
|
---|
154 | mTxFamilyName->setText (tr ("Operating &System:"));
|
---|
155 | mCbFamily->setWhatsThis (tr ("Displays the operating system family that "
|
---|
156 | "you plan to install into this virtual machine."));
|
---|
157 | mTxTypeName->setText (tr ("&Version:"));
|
---|
158 | mCbType->setWhatsThis (tr ("Displays the operating system type that "
|
---|
159 | "you plan to install into this virtual "
|
---|
160 | "machine (called a guest operating system)."));
|
---|
161 | }
|
---|
162 |
|
---|
163 | void VBoxOSTypeSelectorWidget::showEvent (QShowEvent *aEvent)
|
---|
164 | {
|
---|
165 | activateLayout(); /* if not yet */
|
---|
166 | QIWithRetranslateUI <QWidget>::showEvent (aEvent);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void VBoxOSTypeSelectorWidget::onFamilyChanged (int aIndex)
|
---|
170 | {
|
---|
171 | /* Lock the signals of mCbType to prevent it's reaction on clearing */
|
---|
172 | mCbType->blockSignals (true);
|
---|
173 | mCbType->clear();
|
---|
174 |
|
---|
175 | /* Populate combo-box with OS Types related to currently selected Family ID */
|
---|
176 | QString familyId (mCbFamily->itemData (aIndex, RoleTypeID).toString());
|
---|
177 | QList <CGuestOSType> types (vboxGlobal().vmGuestOSTypeList (familyId));
|
---|
178 | for (int i = 0; i < types.size(); ++ i)
|
---|
179 | {
|
---|
180 | if (types [i].GetIs64Bit() && (!m_fSupportsHWVirtEx || !m_fSupportsLongMode))
|
---|
181 | continue;
|
---|
182 | int index = mCbType->count();
|
---|
183 | mCbType->insertItem (index, types [i].GetDescription());
|
---|
184 | mCbType->setItemData (index, types [i].GetId(), RoleTypeID);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Select the most recently chosen item */
|
---|
188 | if (mCurrentIds.contains (familyId))
|
---|
189 | {
|
---|
190 | QString typeId (mCurrentIds [familyId]);
|
---|
191 | int typeIndex = mCbType->findData (typeId, RoleTypeID);
|
---|
192 | if (typeIndex != -1)
|
---|
193 | mCbType->setCurrentIndex (typeIndex);
|
---|
194 | }
|
---|
195 | /* Or select WinXP item for Windows family as default */
|
---|
196 | else if (familyId == "Windows")
|
---|
197 | {
|
---|
198 | int xpIndex = mCbType->findData ("WindowsXP", RoleTypeID);
|
---|
199 | if (xpIndex != -1)
|
---|
200 | mCbType->setCurrentIndex (xpIndex);
|
---|
201 | }
|
---|
202 | /* Or select Ubuntu item for Linux family as default */
|
---|
203 | else if (familyId == "Linux")
|
---|
204 | {
|
---|
205 | int ubIndex = mCbType->findData ("Ubuntu", RoleTypeID);
|
---|
206 | if (ubIndex != -1)
|
---|
207 | mCbType->setCurrentIndex (ubIndex);
|
---|
208 | }
|
---|
209 | /* Else simply select the first one present */
|
---|
210 | else mCbType->setCurrentIndex (0);
|
---|
211 |
|
---|
212 | /* Update all the stuff with new type */
|
---|
213 | onTypeChanged (mCbType->currentIndex());
|
---|
214 |
|
---|
215 | /* Unlock the signals of mCbType */
|
---|
216 | mCbType->blockSignals (false);
|
---|
217 | }
|
---|
218 |
|
---|
219 | void VBoxOSTypeSelectorWidget::onTypeChanged (int aIndex)
|
---|
220 | {
|
---|
221 | /* Save the new selected OS Type */
|
---|
222 | mType = vboxGlobal().vmGuestOSType (
|
---|
223 | mCbType->itemData (aIndex, RoleTypeID).toString(),
|
---|
224 | mCbFamily->itemData (mCbFamily->currentIndex(), RoleTypeID).toString());
|
---|
225 | mPxTypeIcon->setPixmap (vboxGlobal().vmGuestOSTypeIcon (mType.GetId()));
|
---|
226 |
|
---|
227 | /* Save the most recently used item */
|
---|
228 | mCurrentIds [mType.GetFamilyId()] = mType.GetId();
|
---|
229 |
|
---|
230 | /* Notify connected objects about type was changed */
|
---|
231 | emit osTypeChanged();
|
---|
232 | }
|
---|
233 |
|
---|