VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/VBoxApplianceEditorWgt.cpp@ 18591

Last change on this file since 18591 was 18591, checked in by vboxsync, 16 years ago

E1000: Added support for 82545EM (MT Server)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.2 KB
Line 
1/** @file
2 *
3 * VBox frontends: Qt4 GUI ("VirtualBox"):
4 * VBoxApplianceEditorWgt class implementation
5 */
6
7/*
8 * Copyright (C) 2009 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23/* VBox includes */
24#include "VBoxApplianceEditorWgt.h"
25#include "VBoxGlobal.h"
26#include "VBoxProblemReporter.h"
27#include "VBoxOSTypeSelectorButton.h"
28#include "VBoxLineTextEdit.h"
29
30/* Qt includes */
31#include <QItemDelegate>
32#include <QSortFilterProxyModel>
33#include <QHeaderView>
34#include <QLineEdit>
35#include <QTextEdit>
36#include <QSpinBox>
37#include <QComboBox>
38
39////////////////////////////////////////////////////////////////////////////////
40// ModelItem
41
42/* This & the following derived classes represent the data items of a Virtual
43 System. All access/manipulation is done with the help of virtual functions
44 to keep the interface clean. ModelItem is able to handle tree structures
45 with a parent & several children's. */
46ModelItem::ModelItem (int aNumber, ModelItemType aType, ModelItem *aParent /* = NULL */)
47 : mNumber (aNumber)
48 , mType (aType)
49 , mParentItem (aParent)
50{}
51
52ModelItem::~ModelItem()
53{
54 qDeleteAll (mChildItems);
55}
56
57void ModelItem::appendChild (ModelItem *aChild)
58{
59 AssertPtr (aChild);
60 mChildItems << aChild;
61}
62
63ModelItem * ModelItem::child (int aRow) const
64{
65 return mChildItems.value (aRow);
66}
67
68int ModelItem::row() const
69{
70 if (mParentItem)
71 return mParentItem->mChildItems.indexOf (const_cast<ModelItem*> (this));
72
73 return 0;
74}
75
76int ModelItem::childCount() const
77{
78 return mChildItems.count();
79}
80
81void ModelItem::putBack (QVector<BOOL>& aFinalStates, QVector<QString>& aFinalValues, QVector<QString>& aFinalExtraValues)
82{
83 for (int i = 0; i < childCount(); ++i)
84 child (i)->putBack (aFinalStates, aFinalValues, aFinalExtraValues);
85}
86
87////////////////////////////////////////////////////////////////////////////////
88// VirtualSystemItem
89
90VirtualSystemItem::VirtualSystemItem (int aNumber, CVirtualSystemDescription aDesc, ModelItem *aParent)
91 : ModelItem (aNumber, VirtualSystemType, aParent)
92 , mDesc (aDesc)
93{}
94
95QVariant VirtualSystemItem::data (int aColumn, int aRole) const
96{
97 QVariant v;
98 if (aColumn == DescriptionSection &&
99 aRole == Qt::DisplayRole)
100 v = VBoxApplianceEditorWgt::tr ("Virtual System %1").arg (mNumber + 1);
101 return v;
102}
103
104void VirtualSystemItem::putBack (QVector<BOOL>& aFinalStates, QVector<QString>& aFinalValues, QVector<QString>& aFinalExtraValues)
105{
106 /* Resize the vectors */
107 unsigned long count = mDesc.GetCount();
108 aFinalStates.resize (count);
109 aFinalValues.resize (count);
110 aFinalExtraValues.resize (count);
111 /* Recursively fill the vectors */
112 ModelItem::putBack (aFinalStates, aFinalValues, aFinalExtraValues);
113 /* Set all final values at once */
114 mDesc.SetFinalValues (aFinalStates, aFinalValues, aFinalExtraValues);
115}
116
117////////////////////////////////////////////////////////////////////////////////
118// HardwareItem
119
120HardwareItem::HardwareItem (int aNumber,
121 KVirtualSystemDescriptionType aType,
122 const QString &aRef,
123 const QString &aOrigValue,
124 const QString &aConfigValue,
125 const QString &aExtraConfigValue,
126 ModelItem *aParent)
127 : ModelItem (aNumber, HardwareType, aParent)
128 , mType (aType)
129 , mRef (aRef)
130 , mOrigValue (aOrigValue)
131 , mConfigValue (aConfigValue)
132 , mConfigDefaultValue (aConfigValue)
133 , mExtraConfigValue (aExtraConfigValue)
134 , mCheckState (Qt::Checked)
135{}
136
137void HardwareItem::putBack (QVector<BOOL>& aFinalStates, QVector<QString>& aFinalValues, QVector<QString>& aFinalExtraValues)
138{
139 aFinalStates[mNumber] = mCheckState == Qt::Checked;
140 aFinalValues[mNumber] = mConfigValue;
141 aFinalExtraValues[mNumber] = mExtraConfigValue;
142 ModelItem::putBack (aFinalStates, aFinalValues, aFinalExtraValues);
143}
144
145bool HardwareItem::setData (int aColumn, const QVariant &aValue, int aRole)
146{
147 bool fDone = false;
148 switch (aRole)
149 {
150 case Qt::CheckStateRole:
151 {
152 if (aColumn == ConfigValueSection &&
153 (mType == KVirtualSystemDescriptionType_Floppy ||
154 mType == KVirtualSystemDescriptionType_CDROM ||
155 mType == KVirtualSystemDescriptionType_USBController ||
156 mType == KVirtualSystemDescriptionType_SoundCard ||
157 mType == KVirtualSystemDescriptionType_NetworkAdapter))
158 {
159 mCheckState = static_cast<Qt::CheckState> (aValue.toInt());
160 fDone = true;
161 }
162 break;
163 }
164 default: break;
165 }
166 return fDone;
167}
168
169QVariant HardwareItem::data (int aColumn, int aRole) const
170{
171 QVariant v;
172 switch (aRole)
173 {
174 case Qt::DisplayRole:
175 {
176 if (aColumn == DescriptionSection)
177 {
178 switch (mType)
179 {
180 case KVirtualSystemDescriptionType_Name: v = VBoxApplianceEditorWgt::tr ("Name"); break;
181 case KVirtualSystemDescriptionType_Product: v = VBoxApplianceEditorWgt::tr ("Product"); break;
182 case KVirtualSystemDescriptionType_ProductUrl: v = VBoxApplianceEditorWgt::tr ("Product-URL"); break;
183 case KVirtualSystemDescriptionType_Vendor: v = VBoxApplianceEditorWgt::tr ("Vendor"); break;
184 case KVirtualSystemDescriptionType_VendorUrl: v = VBoxApplianceEditorWgt::tr ("Vendor-URL"); break;
185 case KVirtualSystemDescriptionType_Version: v = VBoxApplianceEditorWgt::tr ("Version"); break;
186 case KVirtualSystemDescriptionType_Description: v = VBoxApplianceEditorWgt::tr ("Description"); break;
187 case KVirtualSystemDescriptionType_License: v = VBoxApplianceEditorWgt::tr ("License"); break;
188 case KVirtualSystemDescriptionType_OS: v = VBoxApplianceEditorWgt::tr ("Guest OS Type"); break;
189 case KVirtualSystemDescriptionType_CPU: v = VBoxApplianceEditorWgt::tr ("CPU"); break;
190 case KVirtualSystemDescriptionType_Memory: v = VBoxApplianceEditorWgt::tr ("RAM"); break;
191 case KVirtualSystemDescriptionType_HardDiskControllerIDE: v = VBoxApplianceEditorWgt::tr ("Hard Disk Controller IDE"); break;
192 case KVirtualSystemDescriptionType_HardDiskControllerSATA: v = VBoxApplianceEditorWgt::tr ("Hard Disk Controller SATA"); break;
193 case KVirtualSystemDescriptionType_HardDiskControllerSCSI: v = VBoxApplianceEditorWgt::tr ("Hard Disk Controller SCSI"); break;
194 case KVirtualSystemDescriptionType_CDROM: v = VBoxApplianceEditorWgt::tr ("DVD"); break;
195 case KVirtualSystemDescriptionType_Floppy: v = VBoxApplianceEditorWgt::tr ("Floppy"); break;
196 case KVirtualSystemDescriptionType_NetworkAdapter: v = VBoxApplianceEditorWgt::tr ("Network Adapter"); break;
197 case KVirtualSystemDescriptionType_USBController: v = VBoxApplianceEditorWgt::tr ("USB Controller"); break;
198 case KVirtualSystemDescriptionType_SoundCard: v = VBoxApplianceEditorWgt::tr ("Sound Card"); break;
199 case KVirtualSystemDescriptionType_HardDiskImage: v = VBoxApplianceEditorWgt::tr ("Virtual Disk Image"); break;
200 default: v = VBoxApplianceEditorWgt::tr ("Unknown Hardware Item"); break;
201 }
202 }
203 else if (aColumn == OriginalValueSection)
204 v = mOrigValue;
205 else if (aColumn == ConfigValueSection)
206 {
207 switch (mType)
208 {
209 case KVirtualSystemDescriptionType_Description:
210 case KVirtualSystemDescriptionType_License:
211 {
212 /* Shorten the big text if there is more than
213 * one line */
214 QString tmp (mConfigValue);
215 tmp.replace (tmp.indexOf ('\n'), tmp.length(), "...");
216 v = tmp; break;
217 }
218 case KVirtualSystemDescriptionType_OS: v = vboxGlobal().vmGuestOSTypeDescription (mConfigValue); break;
219 case KVirtualSystemDescriptionType_Memory: v = mConfigValue + " " + VBoxApplianceEditorWgt::tr ("MB"); break;
220 case KVirtualSystemDescriptionType_SoundCard: v = vboxGlobal().toString (static_cast<KAudioControllerType> (mConfigValue.toInt())); break;
221 case KVirtualSystemDescriptionType_NetworkAdapter: v = vboxGlobal().toString (static_cast<KNetworkAdapterType> (mConfigValue.toInt())); break;
222 default: v = mConfigValue; break;
223 }
224 }
225 break;
226 }
227 case Qt::ToolTipRole:
228 {
229 if (aColumn == ConfigValueSection)
230 {
231 if (!mOrigValue.isEmpty())
232 v = VBoxApplianceEditorWgt::tr ("<b>Original Value:</b> %1").arg (mOrigValue);
233 }
234 break;
235 }
236 case Qt::DecorationRole:
237 {
238 if (aColumn == DescriptionSection)
239 {
240 switch (mType)
241 {
242 case KVirtualSystemDescriptionType_Name: v = QIcon (":/name_16px.png"); break;
243 case KVirtualSystemDescriptionType_Product:
244 case KVirtualSystemDescriptionType_ProductUrl:
245 case KVirtualSystemDescriptionType_Vendor:
246 case KVirtualSystemDescriptionType_VendorUrl:
247 case KVirtualSystemDescriptionType_Version:
248 case KVirtualSystemDescriptionType_Description:
249 case KVirtualSystemDescriptionType_License: v = QIcon (":/description_16px.png"); break;
250 case KVirtualSystemDescriptionType_OS: v = QIcon (":/os_type_16px.png"); break;
251 case KVirtualSystemDescriptionType_CPU: v = QIcon (":/cpu_16px.png"); break;
252 case KVirtualSystemDescriptionType_Memory: v = QIcon (":/ram_16px.png"); break;
253 case KVirtualSystemDescriptionType_HardDiskControllerIDE: v = QIcon (":/ide_16px.png"); break;
254 case KVirtualSystemDescriptionType_HardDiskControllerSATA: v = QIcon (":/sata_16px.png"); break;
255 case KVirtualSystemDescriptionType_HardDiskControllerSCSI: v = QIcon (":/scsi_16px.png"); break;
256 case KVirtualSystemDescriptionType_HardDiskImage: v = QIcon (":/hd_16px.png"); break;
257 case KVirtualSystemDescriptionType_CDROM: v = QIcon (":/cd_16px.png"); break;
258 case KVirtualSystemDescriptionType_Floppy: v = QIcon (":/fd_16px.png"); break;
259 case KVirtualSystemDescriptionType_NetworkAdapter: v = QIcon (":/nw_16px.png"); break;
260 case KVirtualSystemDescriptionType_USBController: v = QIcon (":/usb_16px.png"); break;
261 case KVirtualSystemDescriptionType_SoundCard: v = QIcon (":/sound_16px.png"); break;
262 default: break;
263 }
264 }
265 else if (aColumn == ConfigValueSection &&
266 mType == KVirtualSystemDescriptionType_OS)
267 {
268 v = vboxGlobal().vmGuestOSTypeIcon (mConfigValue).scaledToHeight (16, Qt::SmoothTransformation);
269 }
270 break;
271 }
272 case Qt::FontRole:
273 {
274 /* If the item is unchecked mark it with italic text. */
275 if (aColumn == ConfigValueSection &&
276 mCheckState == Qt::Unchecked)
277 {
278 QFont font = qApp->font();
279 font.setItalic (true);
280 v = font;
281 }
282 break;
283 }
284 case Qt::ForegroundRole:
285 {
286 /* If the item is unchecked mark it with gray text. */
287 if (aColumn == ConfigValueSection &&
288 mCheckState == Qt::Unchecked)
289 {
290 QPalette pal = qApp->palette();
291 v = pal.brush (QPalette::Disabled, QPalette::WindowText);
292 }
293 break;
294 }
295 case Qt::CheckStateRole:
296 {
297 if (aColumn == ConfigValueSection &&
298 (mType == KVirtualSystemDescriptionType_Floppy ||
299 mType == KVirtualSystemDescriptionType_CDROM ||
300 mType == KVirtualSystemDescriptionType_USBController ||
301 mType == KVirtualSystemDescriptionType_SoundCard ||
302 mType == KVirtualSystemDescriptionType_NetworkAdapter))
303 v = mCheckState;
304 break;
305 }
306 }
307 return v;
308}
309
310Qt::ItemFlags HardwareItem::itemFlags (int aColumn) const
311{
312 Qt::ItemFlags flags = 0;
313 if (aColumn == ConfigValueSection)
314 {
315 /* Some items are checkable */
316 if (mType == KVirtualSystemDescriptionType_Floppy ||
317 mType == KVirtualSystemDescriptionType_CDROM ||
318 mType == KVirtualSystemDescriptionType_USBController ||
319 mType == KVirtualSystemDescriptionType_SoundCard ||
320 mType == KVirtualSystemDescriptionType_NetworkAdapter)
321 flags |= Qt::ItemIsUserCheckable;
322 /* Some items are editable */
323 if ((mType == KVirtualSystemDescriptionType_Name ||
324 mType == KVirtualSystemDescriptionType_Product ||
325 mType == KVirtualSystemDescriptionType_ProductUrl ||
326 mType == KVirtualSystemDescriptionType_Vendor ||
327 mType == KVirtualSystemDescriptionType_VendorUrl ||
328 mType == KVirtualSystemDescriptionType_Version ||
329 mType == KVirtualSystemDescriptionType_Description ||
330 mType == KVirtualSystemDescriptionType_License ||
331 mType == KVirtualSystemDescriptionType_OS ||
332 mType == KVirtualSystemDescriptionType_Memory ||
333 mType == KVirtualSystemDescriptionType_SoundCard ||
334 mType == KVirtualSystemDescriptionType_NetworkAdapter ||
335 mType == KVirtualSystemDescriptionType_HardDiskControllerIDE ||
336 mType == KVirtualSystemDescriptionType_HardDiskImage) &&
337 mCheckState == Qt::Checked) /* Item has to be enabled */
338 flags |= Qt::ItemIsEditable;
339 }
340 return flags;
341}
342
343QWidget * HardwareItem::createEditor (QWidget *aParent, const QStyleOptionViewItem & /* aOption */, const QModelIndex &aIndex) const
344{
345 QWidget *editor = NULL;
346 if (aIndex.column() == ConfigValueSection)
347 {
348 switch (mType)
349 {
350 case KVirtualSystemDescriptionType_OS:
351 {
352 VBoxOSTypeSelectorButton *e = new VBoxOSTypeSelectorButton (aParent);
353 /* Fill the background with the highlight color in the case
354 * the button hasn't a rectangle shape. This prevents the
355 * display of parts from the current text on the Mac. */
356 e->setAutoFillBackground (true);
357 e->setBackgroundRole (QPalette::Highlight);
358 editor = e;
359 break;
360 }
361 case KVirtualSystemDescriptionType_Name:
362 case KVirtualSystemDescriptionType_Product:
363 case KVirtualSystemDescriptionType_ProductUrl:
364 case KVirtualSystemDescriptionType_Vendor:
365 case KVirtualSystemDescriptionType_VendorUrl:
366 case KVirtualSystemDescriptionType_Version:
367 {
368 QLineEdit *e = new QLineEdit (aParent);
369 editor = e;
370 break;
371 }
372 case KVirtualSystemDescriptionType_Description:
373 case KVirtualSystemDescriptionType_License:
374 {
375 VBoxLineTextEdit *e = new VBoxLineTextEdit (aParent);
376 editor = e;
377 break;
378 }
379 case KVirtualSystemDescriptionType_CPU:
380 {
381 QSpinBox *e = new QSpinBox (aParent);
382 e->setRange (VBoxApplianceEditorWgt::minGuestCPUCount(), VBoxApplianceEditorWgt::maxGuestCPUCount());
383 editor = e;
384 break;
385 }
386 case KVirtualSystemDescriptionType_Memory:
387 {
388 QSpinBox *e = new QSpinBox (aParent);
389 e->setRange (VBoxApplianceEditorWgt::minGuestRAM(), VBoxApplianceEditorWgt::maxGuestRAM());
390 e->setSuffix (" " + VBoxApplianceEditorWgt::tr ("MB"));
391 editor = e;
392 break;
393 }
394 case KVirtualSystemDescriptionType_SoundCard:
395 {
396 QComboBox *e = new QComboBox (aParent);
397 e->addItem (vboxGlobal().toString (KAudioControllerType_AC97), KAudioControllerType_AC97);
398 e->addItem (vboxGlobal().toString (KAudioControllerType_SB16), KAudioControllerType_SB16);
399 editor = e;
400 break;
401 }
402 case KVirtualSystemDescriptionType_NetworkAdapter:
403 {
404 QComboBox *e = new QComboBox (aParent);
405 e->addItem (vboxGlobal().toString (KNetworkAdapterType_Am79C970A), KNetworkAdapterType_Am79C970A);
406 e->addItem (vboxGlobal().toString (KNetworkAdapterType_Am79C973), KNetworkAdapterType_Am79C973);
407#ifdef VBOX_WITH_E1000
408 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82540EM), KNetworkAdapterType_I82540EM);
409 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82543GC), KNetworkAdapterType_I82543GC);
410 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82545EM), KNetworkAdapterType_I82545EM);
411#endif /* VBOX_WITH_E1000 */
412 editor = e;
413 break;
414 }
415 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
416 {
417 QComboBox *e = new QComboBox (aParent);
418 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX3), "PIIX3");
419 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX4), "PIIX4");
420 e->addItem (vboxGlobal().toString (KStorageControllerType_ICH6), "ICH6");
421 editor = e;
422 break;
423 }
424 case KVirtualSystemDescriptionType_HardDiskImage:
425 {
426 /* disabled for now
427 VBoxFilePathSelectorWidget *e = new VBoxFilePathSelectorWidget (aParent);
428 e->setMode (VBoxFilePathSelectorWidget::Mode_File);
429 e->setResetEnabled (false);
430 */
431 QLineEdit *e = new QLineEdit (aParent);
432 editor = e;
433 break;
434 }
435 default: break;
436 }
437 }
438 return editor;
439}
440
441bool HardwareItem::setEditorData (QWidget *aEditor, const QModelIndex & /* aIndex */) const
442{
443 bool fDone = false;
444 switch (mType)
445 {
446 case KVirtualSystemDescriptionType_OS:
447 {
448 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
449 {
450 e->setOSTypeId (mConfigValue);
451 fDone = true;
452 }
453 break;
454 }
455 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
456 {
457 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
458 {
459 int i = e->findData (mConfigValue);
460 if (i != -1)
461 e->setCurrentIndex (i);
462 fDone = true;
463 }
464 break;
465 }
466 case KVirtualSystemDescriptionType_CPU:
467 case KVirtualSystemDescriptionType_Memory:
468 {
469 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
470 {
471 e->setValue (mConfigValue.toInt());
472 fDone = true;
473 }
474 break;
475 }
476 case KVirtualSystemDescriptionType_Name:
477 case KVirtualSystemDescriptionType_Product:
478 case KVirtualSystemDescriptionType_ProductUrl:
479 case KVirtualSystemDescriptionType_Vendor:
480 case KVirtualSystemDescriptionType_VendorUrl:
481 case KVirtualSystemDescriptionType_Version:
482 {
483 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
484 {
485 e->setText (mConfigValue);
486 fDone = true;
487 }
488 break;
489 }
490 case KVirtualSystemDescriptionType_Description:
491 case KVirtualSystemDescriptionType_License:
492 {
493 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
494 {
495 e->setText (mConfigValue);
496 fDone = true;
497 }
498 break;
499 }
500 case KVirtualSystemDescriptionType_SoundCard:
501 case KVirtualSystemDescriptionType_NetworkAdapter:
502 {
503 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
504 {
505 int i = e->findData (mConfigValue.toInt());
506 if (i != -1)
507 e->setCurrentIndex (i);
508 fDone = true;
509 }
510 break;
511 }
512 case KVirtualSystemDescriptionType_HardDiskImage:
513 {
514 /* disabled for now
515 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
516 {
517 e->setPath (mConfigValue);
518 }
519 */
520 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
521 {
522 e->setText (mConfigValue);
523 fDone = true;
524 }
525 break;
526 }
527 default: break;
528 }
529 return fDone;
530}
531
532bool HardwareItem::setModelData (QWidget *aEditor, QAbstractItemModel * /* aModel */, const QModelIndex & /* aIndex */)
533{
534 bool fDone = false;
535 switch (mType)
536 {
537 case KVirtualSystemDescriptionType_OS:
538 {
539 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
540 {
541 mConfigValue = e->osTypeId();
542 fDone = true;
543 }
544 break;
545 }
546 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
547 {
548 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
549 {
550 mConfigValue = e->itemData (e->currentIndex()).toString();
551 fDone = true;
552 }
553 break;
554 }
555 case KVirtualSystemDescriptionType_CPU:
556 case KVirtualSystemDescriptionType_Memory:
557 {
558 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
559 {
560 mConfigValue = QString::number (e->value());
561 fDone = true;
562 }
563 break;
564 }
565 case KVirtualSystemDescriptionType_Name:
566 case KVirtualSystemDescriptionType_Product:
567 case KVirtualSystemDescriptionType_ProductUrl:
568 case KVirtualSystemDescriptionType_Vendor:
569 case KVirtualSystemDescriptionType_VendorUrl:
570 case KVirtualSystemDescriptionType_Version:
571 {
572 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
573 {
574 mConfigValue = e->text();
575 fDone = true;
576 }
577 break;
578 }
579 case KVirtualSystemDescriptionType_Description:
580 case KVirtualSystemDescriptionType_License:
581 {
582 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
583 {
584 mConfigValue = e->text();
585 fDone = true;
586 }
587 break;
588 }
589 case KVirtualSystemDescriptionType_SoundCard:
590 case KVirtualSystemDescriptionType_NetworkAdapter:
591 {
592 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
593 {
594 mConfigValue = e->itemData (e->currentIndex()).toString();
595 fDone = true;
596 }
597 break;
598 }
599 case KVirtualSystemDescriptionType_HardDiskImage:
600 {
601 /* disabled for now
602 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
603 {
604 mConfigValue = e->path();
605 }
606 */
607 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
608 {
609 mConfigValue = e->text();
610 fDone = true;
611 }
612 break;
613 }
614 default: break;
615 }
616 return fDone;
617}
618
619////////////////////////////////////////////////////////////////////////////////
620// VirtualSystemModel
621
622/* This class is a wrapper model for our ModelItem. It could be used with any
623 TreeView & forward mostly all calls to the methods of ModelItem. The
624 ModelItems itself are stored as internal pointers in the QModelIndex class. */
625VirtualSystemModel::VirtualSystemModel (QVector<CVirtualSystemDescription>& aVSDs, QObject *aParent /* = NULL */)
626 : QAbstractItemModel (aParent)
627{
628 mRootItem = new ModelItem (0, RootType);
629 for (int a = 0; a < aVSDs.size(); ++a)
630 {
631 CVirtualSystemDescription vs = aVSDs[a];
632
633 VirtualSystemItem *vi = new VirtualSystemItem (a, vs, mRootItem);
634 mRootItem->appendChild (vi);
635
636 /* @todo: ask Dmitry about include/COMDefs.h:232 */
637 QVector<KVirtualSystemDescriptionType> types;
638 QVector<QString> refs;
639 QVector<QString> origValues;
640 QVector<QString> configValues;
641 QVector<QString> extraConfigValues;
642
643 QList<int> hdIndizies;
644 QMap<int, HardwareItem*> controllerMap;
645 vs.GetDescription (types, refs, origValues, configValues, extraConfigValues);
646 for (int i = 0; i < types.size(); ++i)
647 {
648 /* We add the hard disk images in an second step, so save a
649 reference to them. */
650 if (types[i] == KVirtualSystemDescriptionType_HardDiskImage)
651 hdIndizies << i;
652 else
653 {
654 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], vi);
655 vi->appendChild (hi);
656 /* Save the hard disk controller types in an extra map */
657 if (types[i] == KVirtualSystemDescriptionType_HardDiskControllerIDE ||
658 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSATA ||
659 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSCSI)
660 controllerMap[i] = hi;
661 }
662 }
663 QRegExp rx ("controller=(\\d+);?");
664 /* Now process the hard disk images */
665 for (int a = 0; a < hdIndizies.size(); ++a)
666 {
667 int i = hdIndizies[a];
668 QString ecnf = extraConfigValues[i];
669 if (rx.indexIn (ecnf) != -1)
670 {
671 /* Get the controller */
672 HardwareItem *ci = controllerMap[rx.cap (1).toInt()];
673 if (ci)
674 {
675 /* New hardware item as child of the controller */
676 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], ci);
677 ci->appendChild (hi);
678 }
679 }
680 }
681 }
682}
683
684QModelIndex VirtualSystemModel::index (int aRow, int aColumn, const QModelIndex &aParent /* = QModelIndex() */) const
685{
686 if (!hasIndex (aRow, aColumn, aParent))
687 return QModelIndex();
688
689 ModelItem *parentItem;
690
691 if (!aParent.isValid())
692 parentItem = mRootItem;
693 else
694 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
695
696 ModelItem *childItem = parentItem->child (aRow);
697 if (childItem)
698 return createIndex (aRow, aColumn, childItem);
699 else
700 return QModelIndex();
701}
702
703QModelIndex VirtualSystemModel::parent (const QModelIndex &aIndex) const
704{
705 if (!aIndex.isValid())
706 return QModelIndex();
707
708 ModelItem *childItem = static_cast<ModelItem*> (aIndex.internalPointer());
709 ModelItem *parentItem = childItem->parent();
710
711 if (parentItem == mRootItem)
712 return QModelIndex();
713
714 return createIndex (parentItem->row(), 0, parentItem);
715}
716
717int VirtualSystemModel::rowCount (const QModelIndex &aParent /* = QModelIndex() */) const
718{
719 ModelItem *parentItem;
720 if (aParent.column() > 0)
721 return 0;
722
723 if (!aParent.isValid())
724 parentItem = mRootItem;
725 else
726 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
727
728 return parentItem->childCount();
729}
730
731int VirtualSystemModel::columnCount (const QModelIndex &aParent /* = QModelIndex() */) const
732{
733 if (aParent.isValid())
734 return static_cast<ModelItem*> (aParent.internalPointer())->columnCount();
735 else
736 return mRootItem->columnCount();
737}
738
739bool VirtualSystemModel::setData (const QModelIndex &aIndex, const QVariant &aValue, int aRole)
740{
741 if (!aIndex.isValid())
742 return false;
743
744 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
745
746 return item->setData (aIndex.column(), aValue, aRole);
747}
748
749QVariant VirtualSystemModel::data (const QModelIndex &aIndex, int aRole /* = Qt::DisplayRole */) const
750{
751 if (!aIndex.isValid())
752 return QVariant();
753
754 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
755
756 return item->data (aIndex.column(), aRole);
757}
758
759Qt::ItemFlags VirtualSystemModel::flags (const QModelIndex &aIndex) const
760{
761 if (!aIndex.isValid())
762 return 0;
763
764 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
765
766 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | item->itemFlags (aIndex.column());
767}
768
769QVariant VirtualSystemModel::headerData (int aSection, Qt::Orientation aOrientation, int aRole) const
770{
771 if (aRole != Qt::DisplayRole ||
772 aOrientation != Qt::Horizontal)
773 return QVariant();
774
775 QString title;
776 switch (aSection)
777 {
778 case DescriptionSection: title = VBoxApplianceEditorWgt::tr ("Description"); break;
779 case ConfigValueSection: title = VBoxApplianceEditorWgt::tr ("Configuration"); break;
780 }
781 return title;
782}
783
784void VirtualSystemModel::restoreDefaults (const QModelIndex& aParent /* = QModelIndex() */)
785{
786 ModelItem *parentItem;
787
788 if (!aParent.isValid())
789 parentItem = mRootItem;
790 else
791 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
792
793 for (int i = 0; i < parentItem->childCount(); ++i)
794 {
795 parentItem->child (i)->restoreDefaults();
796 restoreDefaults (index (i, 0, aParent));
797 }
798 emit dataChanged (index (0, 0, aParent), index (parentItem->childCount()-1, 0, aParent));
799}
800
801void VirtualSystemModel::putBack()
802{
803 QVector<BOOL> v1;
804 QVector<QString> v2;
805 QVector<QString> v3;
806 mRootItem->putBack (v1, v2, v3);
807}
808
809////////////////////////////////////////////////////////////////////////////////
810// VirtualSystemDelegate
811
812/* The delegate is used for creating/handling the different editors for the
813 various types we support. This class forward the requests to the virtual
814 methods of our different ModelItems. If this is not possible the default
815 methods of QItemDelegate are used to get some standard behavior. Note: We
816 have to handle the proxy model ourself. I really don't understand why Qt is
817 not doing this for us. */
818VirtualSystemDelegate::VirtualSystemDelegate (QAbstractProxyModel *aProxy, QObject *aParent /* = NULL */)
819 : QItemDelegate (aParent)
820 , mProxy (aProxy)
821{}
822
823QWidget * VirtualSystemDelegate::createEditor (QWidget *aParent, const QStyleOptionViewItem &aOption, const QModelIndex &aIndex) const
824{
825 if (!aIndex.isValid())
826 return QItemDelegate::createEditor (aParent, aOption, aIndex);
827
828 QModelIndex index (aIndex);
829 if (mProxy)
830 index = mProxy->mapToSource (aIndex);
831
832 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
833 QWidget *editor = item->createEditor (aParent, aOption, index);
834
835 if (editor == NULL)
836 return QItemDelegate::createEditor (aParent, aOption, index);
837 else
838 return editor;
839}
840
841void VirtualSystemDelegate::setEditorData (QWidget *aEditor, const QModelIndex &aIndex) const
842{
843 if (!aIndex.isValid())
844 return QItemDelegate::setEditorData (aEditor, aIndex);
845
846 QModelIndex index (aIndex);
847 if (mProxy)
848 index = mProxy->mapToSource (aIndex);
849
850 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
851
852 if (!item->setEditorData (aEditor, index))
853 QItemDelegate::setEditorData (aEditor, index);
854}
855
856void VirtualSystemDelegate::setModelData (QWidget *aEditor, QAbstractItemModel *aModel, const QModelIndex &aIndex) const
857{
858 if (!aIndex.isValid())
859 return QItemDelegate::setModelData (aEditor, aModel, aIndex);
860
861 QModelIndex index = aModel->index (aIndex.row(), aIndex.column());
862 if (mProxy)
863 index = mProxy->mapToSource (aIndex);
864
865 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
866 if (!item->setModelData (aEditor, aModel, index))
867 QItemDelegate::setModelData (aEditor, aModel, aIndex);
868}
869
870void VirtualSystemDelegate::updateEditorGeometry (QWidget *aEditor, const QStyleOptionViewItem &aOption, const QModelIndex & /* aIndex */) const
871{
872 if (aEditor)
873 aEditor->setGeometry (aOption.rect);
874}
875
876////////////////////////////////////////////////////////////////////////////////
877// VirtualSystemSortProxyModel
878
879/* How to sort the items in the tree view */
880KVirtualSystemDescriptionType VirtualSystemSortProxyModel::mSortList[] =
881{
882 KVirtualSystemDescriptionType_Name,
883 KVirtualSystemDescriptionType_Product,
884 KVirtualSystemDescriptionType_ProductUrl,
885 KVirtualSystemDescriptionType_Vendor,
886 KVirtualSystemDescriptionType_VendorUrl,
887 KVirtualSystemDescriptionType_Version,
888 KVirtualSystemDescriptionType_Description,
889 KVirtualSystemDescriptionType_License,
890 KVirtualSystemDescriptionType_OS,
891 KVirtualSystemDescriptionType_CPU,
892 KVirtualSystemDescriptionType_Memory,
893 KVirtualSystemDescriptionType_Floppy,
894 KVirtualSystemDescriptionType_CDROM,
895 KVirtualSystemDescriptionType_USBController,
896 KVirtualSystemDescriptionType_SoundCard,
897 KVirtualSystemDescriptionType_NetworkAdapter,
898 KVirtualSystemDescriptionType_HardDiskControllerIDE,
899 KVirtualSystemDescriptionType_HardDiskControllerSATA,
900 KVirtualSystemDescriptionType_HardDiskControllerSCSI
901};
902
903VirtualSystemSortProxyModel::VirtualSystemSortProxyModel (QObject *aParent)
904 : QSortFilterProxyModel (aParent)
905{}
906
907bool VirtualSystemSortProxyModel::filterAcceptsRow (int aSourceRow, const QModelIndex & aSourceParent) const
908{
909 /* By default enable all, we will explicitly filter out below */
910 if (aSourceParent.isValid())
911 {
912 QModelIndex i = aSourceParent.child (aSourceRow, 0);
913 if (i.isValid())
914 {
915 ModelItem *item = static_cast<ModelItem*> (i.internalPointer());
916 /* We filter hardware types only */
917 if (item->type() == HardwareType)
918 {
919 HardwareItem *hwItem = static_cast<HardwareItem*> (item);
920 /* The license type shouldn't be displayed */
921 if (mFilterList.contains (hwItem->mType))
922 return false;
923 }
924 }
925 }
926 return true;
927}
928
929bool VirtualSystemSortProxyModel::lessThan (const QModelIndex &aLeft, const QModelIndex &aRight) const
930{
931 if (!aLeft.isValid() ||
932 !aRight.isValid())
933 return false;
934
935 ModelItem *leftItem = static_cast<ModelItem*> (aLeft.internalPointer());
936 ModelItem *rightItem = static_cast<ModelItem*> (aRight.internalPointer());
937
938 /* We sort hardware types only */
939 if (!(leftItem->type() == HardwareType &&
940 rightItem->type() == HardwareType))
941 return false;
942
943 HardwareItem *hwLeft = static_cast<HardwareItem*> (leftItem);
944 HardwareItem *hwRight = static_cast<HardwareItem*> (rightItem);
945
946 for (unsigned int i = 0; i < RT_ELEMENTS (mSortList); ++i)
947 if (hwLeft->mType == mSortList[i])
948 {
949 for (unsigned int a = 0; a <= i; ++a)
950 if (hwRight->mType == mSortList[a])
951 return true;
952 return false;
953 }
954
955 return true;
956}
957
958////////////////////////////////////////////////////////////////////////////////
959// VBoxApplianceEditorWgt
960
961int VBoxApplianceEditorWgt::mMinGuestRAM = -1;
962int VBoxApplianceEditorWgt::mMaxGuestRAM = -1;
963int VBoxApplianceEditorWgt::mMinGuestCPUCount = -1;
964int VBoxApplianceEditorWgt::mMaxGuestCPUCount = -1;
965
966VBoxApplianceEditorWgt::VBoxApplianceEditorWgt (QWidget *aParent /* = NULL */)
967 : QIWithRetranslateUI<QWidget> (aParent)
968 , mAppliance (NULL)
969 , mModel (NULL)
970{
971 /* Make sure all static content is properly initialized */
972 initSystemSettings();
973
974 /* Apply UI decorations */
975 Ui::VBoxApplianceEditorWgt::setupUi (this);
976
977 /* Make the tree looking nicer */
978 mTvSettings->setRootIsDecorated (false);
979 mTvSettings->setAlternatingRowColors (true);
980 mTvSettings->header()->setStretchLastSection (true);
981 mTvSettings->header()->setResizeMode (QHeaderView::ResizeToContents);
982
983 /* Applying language settings */
984 retranslateUi();
985}
986
987void VBoxApplianceEditorWgt::restoreDefaults()
988{
989 mModel->restoreDefaults();
990}
991
992void VBoxApplianceEditorWgt::retranslateUi()
993{
994 /* Translate uic generated strings */
995 Ui::VBoxApplianceEditorWgt::retranslateUi (this);
996}
997
998/* static */
999void VBoxApplianceEditorWgt::initSystemSettings()
1000{
1001 if (mMinGuestRAM == -1)
1002 {
1003 /* We need some global defaults from the current VirtualBox
1004 installation */
1005 CSystemProperties sp = vboxGlobal().virtualBox().GetSystemProperties();
1006 mMinGuestRAM = sp.GetMinGuestRAM();
1007 mMaxGuestRAM = sp.GetMaxGuestRAM();
1008 mMinGuestCPUCount = sp.GetMinGuestCPUCount();
1009 mMaxGuestCPUCount = sp.GetMaxGuestCPUCount();
1010 }
1011}
1012
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette