VirtualBox

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

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

FE/Qt4: remove import dependency

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.0 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#endif /* VBOX_WITH_E1000 */
411 editor = e;
412 break;
413 }
414 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
415 {
416 QComboBox *e = new QComboBox (aParent);
417 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX3), "PIIX3");
418 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX4), "PIIX4");
419 e->addItem (vboxGlobal().toString (KStorageControllerType_ICH6), "ICH6");
420 editor = e;
421 break;
422 }
423 case KVirtualSystemDescriptionType_HardDiskImage:
424 {
425 /* disabled for now
426 VBoxFilePathSelectorWidget *e = new VBoxFilePathSelectorWidget (aParent);
427 e->setMode (VBoxFilePathSelectorWidget::Mode_File);
428 e->setResetEnabled (false);
429 */
430 QLineEdit *e = new QLineEdit (aParent);
431 editor = e;
432 break;
433 }
434 default: break;
435 }
436 }
437 return editor;
438}
439
440bool HardwareItem::setEditorData (QWidget *aEditor, const QModelIndex & /* aIndex */) const
441{
442 bool fDone = false;
443 switch (mType)
444 {
445 case KVirtualSystemDescriptionType_OS:
446 {
447 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
448 {
449 e->setOSTypeId (mConfigValue);
450 fDone = true;
451 }
452 break;
453 }
454 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
455 {
456 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
457 {
458 int i = e->findData (mConfigValue);
459 if (i != -1)
460 e->setCurrentIndex (i);
461 fDone = true;
462 }
463 break;
464 }
465 case KVirtualSystemDescriptionType_CPU:
466 case KVirtualSystemDescriptionType_Memory:
467 {
468 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
469 {
470 e->setValue (mConfigValue.toInt());
471 fDone = true;
472 }
473 break;
474 }
475 case KVirtualSystemDescriptionType_Name:
476 case KVirtualSystemDescriptionType_Product:
477 case KVirtualSystemDescriptionType_ProductUrl:
478 case KVirtualSystemDescriptionType_Vendor:
479 case KVirtualSystemDescriptionType_VendorUrl:
480 case KVirtualSystemDescriptionType_Version:
481 {
482 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
483 {
484 e->setText (mConfigValue);
485 fDone = true;
486 }
487 break;
488 }
489 case KVirtualSystemDescriptionType_Description:
490 case KVirtualSystemDescriptionType_License:
491 {
492 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
493 {
494 e->setText (mConfigValue);
495 fDone = true;
496 }
497 break;
498 }
499 case KVirtualSystemDescriptionType_SoundCard:
500 case KVirtualSystemDescriptionType_NetworkAdapter:
501 {
502 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
503 {
504 int i = e->findData (mConfigValue.toInt());
505 if (i != -1)
506 e->setCurrentIndex (i);
507 fDone = true;
508 }
509 break;
510 }
511 case KVirtualSystemDescriptionType_HardDiskImage:
512 {
513 /* disabled for now
514 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
515 {
516 e->setPath (mConfigValue);
517 }
518 */
519 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
520 {
521 e->setText (mConfigValue);
522 fDone = true;
523 }
524 break;
525 }
526 default: break;
527 }
528 return fDone;
529}
530
531bool HardwareItem::setModelData (QWidget *aEditor, QAbstractItemModel * /* aModel */, const QModelIndex & /* aIndex */)
532{
533 bool fDone = false;
534 switch (mType)
535 {
536 case KVirtualSystemDescriptionType_OS:
537 {
538 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
539 {
540 mConfigValue = e->osTypeId();
541 fDone = true;
542 }
543 break;
544 }
545 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
546 {
547 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
548 {
549 mConfigValue = e->itemData (e->currentIndex()).toString();
550 fDone = true;
551 }
552 break;
553 }
554 case KVirtualSystemDescriptionType_CPU:
555 case KVirtualSystemDescriptionType_Memory:
556 {
557 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
558 {
559 mConfigValue = QString::number (e->value());
560 fDone = true;
561 }
562 break;
563 }
564 case KVirtualSystemDescriptionType_Name:
565 case KVirtualSystemDescriptionType_Product:
566 case KVirtualSystemDescriptionType_ProductUrl:
567 case KVirtualSystemDescriptionType_Vendor:
568 case KVirtualSystemDescriptionType_VendorUrl:
569 case KVirtualSystemDescriptionType_Version:
570 {
571 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
572 {
573 mConfigValue = e->text();
574 fDone = true;
575 }
576 break;
577 }
578 case KVirtualSystemDescriptionType_Description:
579 case KVirtualSystemDescriptionType_License:
580 {
581 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
582 {
583 mConfigValue = e->text();
584 fDone = true;
585 }
586 break;
587 }
588 case KVirtualSystemDescriptionType_SoundCard:
589 case KVirtualSystemDescriptionType_NetworkAdapter:
590 {
591 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
592 {
593 mConfigValue = e->itemData (e->currentIndex()).toString();
594 fDone = true;
595 }
596 break;
597 }
598 case KVirtualSystemDescriptionType_HardDiskImage:
599 {
600 /* disabled for now
601 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
602 {
603 mConfigValue = e->path();
604 }
605 */
606 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
607 {
608 mConfigValue = e->text();
609 fDone = true;
610 }
611 break;
612 }
613 default: break;
614 }
615 return fDone;
616}
617
618////////////////////////////////////////////////////////////////////////////////
619// VirtualSystemModel
620
621/* This class is a wrapper model for our ModelItem. It could be used with any
622 TreeView & forward mostly all calls to the methods of ModelItem. The
623 ModelItems itself are stored as internal pointers in the QModelIndex class. */
624VirtualSystemModel::VirtualSystemModel (QVector<CVirtualSystemDescription>& aVSDs, QObject *aParent /* = NULL */)
625 : QAbstractItemModel (aParent)
626{
627 mRootItem = new ModelItem (0, RootType);
628 for (int a = 0; a < aVSDs.size(); ++a)
629 {
630 CVirtualSystemDescription vs = aVSDs[a];
631
632 VirtualSystemItem *vi = new VirtualSystemItem (a, vs, mRootItem);
633 mRootItem->appendChild (vi);
634
635 /* @todo: ask Dmitry about include/COMDefs.h:232 */
636 QVector<KVirtualSystemDescriptionType> types;
637 QVector<QString> refs;
638 QVector<QString> origValues;
639 QVector<QString> configValues;
640 QVector<QString> extraConfigValues;
641
642 QList<int> hdIndizies;
643 QMap<int, HardwareItem*> controllerMap;
644 vs.GetDescription (types, refs, origValues, configValues, extraConfigValues);
645 for (int i = 0; i < types.size(); ++i)
646 {
647 /* We add the hard disk images in an second step, so save a
648 reference to them. */
649 if (types[i] == KVirtualSystemDescriptionType_HardDiskImage)
650 hdIndizies << i;
651 else
652 {
653 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], vi);
654 vi->appendChild (hi);
655 /* Save the hard disk controller types in an extra map */
656 if (types[i] == KVirtualSystemDescriptionType_HardDiskControllerIDE ||
657 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSATA ||
658 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSCSI)
659 controllerMap[i] = hi;
660 }
661 }
662 QRegExp rx ("controller=(\\d+);?");
663 /* Now process the hard disk images */
664 for (int a = 0; a < hdIndizies.size(); ++a)
665 {
666 int i = hdIndizies[a];
667 QString ecnf = extraConfigValues[i];
668 if (rx.indexIn (ecnf) != -1)
669 {
670 /* Get the controller */
671 HardwareItem *ci = controllerMap[rx.cap (1).toInt()];
672 if (ci)
673 {
674 /* New hardware item as child of the controller */
675 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], ci);
676 ci->appendChild (hi);
677 }
678 }
679 }
680 }
681}
682
683QModelIndex VirtualSystemModel::index (int aRow, int aColumn, const QModelIndex &aParent /* = QModelIndex() */) const
684{
685 if (!hasIndex (aRow, aColumn, aParent))
686 return QModelIndex();
687
688 ModelItem *parentItem;
689
690 if (!aParent.isValid())
691 parentItem = mRootItem;
692 else
693 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
694
695 ModelItem *childItem = parentItem->child (aRow);
696 if (childItem)
697 return createIndex (aRow, aColumn, childItem);
698 else
699 return QModelIndex();
700}
701
702QModelIndex VirtualSystemModel::parent (const QModelIndex &aIndex) const
703{
704 if (!aIndex.isValid())
705 return QModelIndex();
706
707 ModelItem *childItem = static_cast<ModelItem*> (aIndex.internalPointer());
708 ModelItem *parentItem = childItem->parent();
709
710 if (parentItem == mRootItem)
711 return QModelIndex();
712
713 return createIndex (parentItem->row(), 0, parentItem);
714}
715
716int VirtualSystemModel::rowCount (const QModelIndex &aParent /* = QModelIndex() */) const
717{
718 ModelItem *parentItem;
719 if (aParent.column() > 0)
720 return 0;
721
722 if (!aParent.isValid())
723 parentItem = mRootItem;
724 else
725 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
726
727 return parentItem->childCount();
728}
729
730int VirtualSystemModel::columnCount (const QModelIndex &aParent /* = QModelIndex() */) const
731{
732 if (aParent.isValid())
733 return static_cast<ModelItem*> (aParent.internalPointer())->columnCount();
734 else
735 return mRootItem->columnCount();
736}
737
738bool VirtualSystemModel::setData (const QModelIndex &aIndex, const QVariant &aValue, int aRole)
739{
740 if (!aIndex.isValid())
741 return false;
742
743 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
744
745 return item->setData (aIndex.column(), aValue, aRole);
746}
747
748QVariant VirtualSystemModel::data (const QModelIndex &aIndex, int aRole /* = Qt::DisplayRole */) const
749{
750 if (!aIndex.isValid())
751 return QVariant();
752
753 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
754
755 return item->data (aIndex.column(), aRole);
756}
757
758Qt::ItemFlags VirtualSystemModel::flags (const QModelIndex &aIndex) const
759{
760 if (!aIndex.isValid())
761 return 0;
762
763 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
764
765 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | item->itemFlags (aIndex.column());
766}
767
768QVariant VirtualSystemModel::headerData (int aSection, Qt::Orientation aOrientation, int aRole) const
769{
770 if (aRole != Qt::DisplayRole ||
771 aOrientation != Qt::Horizontal)
772 return QVariant();
773
774 QString title;
775 switch (aSection)
776 {
777 case DescriptionSection: title = VBoxApplianceEditorWgt::tr ("Description"); break;
778 case ConfigValueSection: title = VBoxApplianceEditorWgt::tr ("Configuration"); break;
779 }
780 return title;
781}
782
783void VirtualSystemModel::restoreDefaults (const QModelIndex& aParent /* = QModelIndex() */)
784{
785 ModelItem *parentItem;
786
787 if (!aParent.isValid())
788 parentItem = mRootItem;
789 else
790 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
791
792 for (int i = 0; i < parentItem->childCount(); ++i)
793 {
794 parentItem->child (i)->restoreDefaults();
795 restoreDefaults (index (i, 0, aParent));
796 }
797 emit dataChanged (index (0, 0, aParent), index (parentItem->childCount()-1, 0, aParent));
798}
799
800void VirtualSystemModel::putBack()
801{
802 QVector<BOOL> v1;
803 QVector<QString> v2;
804 QVector<QString> v3;
805 mRootItem->putBack (v1, v2, v3);
806}
807
808////////////////////////////////////////////////////////////////////////////////
809// VirtualSystemDelegate
810
811/* The delegate is used for creating/handling the different editors for the
812 various types we support. This class forward the requests to the virtual
813 methods of our different ModelItems. If this is not possible the default
814 methods of QItemDelegate are used to get some standard behavior. Note: We
815 have to handle the proxy model ourself. I really don't understand why Qt is
816 not doing this for us. */
817VirtualSystemDelegate::VirtualSystemDelegate (QAbstractProxyModel *aProxy, QObject *aParent /* = NULL */)
818 : QItemDelegate (aParent)
819 , mProxy (aProxy)
820{}
821
822QWidget * VirtualSystemDelegate::createEditor (QWidget *aParent, const QStyleOptionViewItem &aOption, const QModelIndex &aIndex) const
823{
824 if (!aIndex.isValid())
825 return QItemDelegate::createEditor (aParent, aOption, aIndex);
826
827 QModelIndex index (aIndex);
828 if (mProxy)
829 index = mProxy->mapToSource (aIndex);
830
831 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
832 QWidget *editor = item->createEditor (aParent, aOption, index);
833
834 if (editor == NULL)
835 return QItemDelegate::createEditor (aParent, aOption, index);
836 else
837 return editor;
838}
839
840void VirtualSystemDelegate::setEditorData (QWidget *aEditor, const QModelIndex &aIndex) const
841{
842 if (!aIndex.isValid())
843 return QItemDelegate::setEditorData (aEditor, aIndex);
844
845 QModelIndex index (aIndex);
846 if (mProxy)
847 index = mProxy->mapToSource (aIndex);
848
849 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
850
851 if (!item->setEditorData (aEditor, index))
852 QItemDelegate::setEditorData (aEditor, index);
853}
854
855void VirtualSystemDelegate::setModelData (QWidget *aEditor, QAbstractItemModel *aModel, const QModelIndex &aIndex) const
856{
857 if (!aIndex.isValid())
858 return QItemDelegate::setModelData (aEditor, aModel, aIndex);
859
860 QModelIndex index = aModel->index (aIndex.row(), aIndex.column());
861 if (mProxy)
862 index = mProxy->mapToSource (aIndex);
863
864 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
865 if (!item->setModelData (aEditor, aModel, index))
866 QItemDelegate::setModelData (aEditor, aModel, aIndex);
867}
868
869void VirtualSystemDelegate::updateEditorGeometry (QWidget *aEditor, const QStyleOptionViewItem &aOption, const QModelIndex & /* aIndex */) const
870{
871 if (aEditor)
872 aEditor->setGeometry (aOption.rect);
873}
874
875////////////////////////////////////////////////////////////////////////////////
876// VirtualSystemSortProxyModel
877
878/* How to sort the items in the tree view */
879KVirtualSystemDescriptionType VirtualSystemSortProxyModel::mSortList[] =
880{
881 KVirtualSystemDescriptionType_Name,
882 KVirtualSystemDescriptionType_Product,
883 KVirtualSystemDescriptionType_ProductUrl,
884 KVirtualSystemDescriptionType_Vendor,
885 KVirtualSystemDescriptionType_VendorUrl,
886 KVirtualSystemDescriptionType_Version,
887 KVirtualSystemDescriptionType_Description,
888 KVirtualSystemDescriptionType_License,
889 KVirtualSystemDescriptionType_OS,
890 KVirtualSystemDescriptionType_CPU,
891 KVirtualSystemDescriptionType_Memory,
892 KVirtualSystemDescriptionType_Floppy,
893 KVirtualSystemDescriptionType_CDROM,
894 KVirtualSystemDescriptionType_USBController,
895 KVirtualSystemDescriptionType_SoundCard,
896 KVirtualSystemDescriptionType_NetworkAdapter,
897 KVirtualSystemDescriptionType_HardDiskControllerIDE,
898 KVirtualSystemDescriptionType_HardDiskControllerSATA,
899 KVirtualSystemDescriptionType_HardDiskControllerSCSI
900};
901
902VirtualSystemSortProxyModel::VirtualSystemSortProxyModel (QObject *aParent)
903 : QSortFilterProxyModel (aParent)
904{}
905
906bool VirtualSystemSortProxyModel::filterAcceptsRow (int aSourceRow, const QModelIndex & aSourceParent) const
907{
908 /* By default enable all, we will explicitly filter out below */
909 if (aSourceParent.isValid())
910 {
911 QModelIndex i = aSourceParent.child (aSourceRow, 0);
912 if (i.isValid())
913 {
914 ModelItem *item = static_cast<ModelItem*> (i.internalPointer());
915 /* We filter hardware types only */
916 if (item->type() == HardwareType)
917 {
918 HardwareItem *hwItem = static_cast<HardwareItem*> (item);
919 /* The license type shouldn't be displayed */
920 if (mFilterList.contains (hwItem->mType))
921 return false;
922 }
923 }
924 }
925 return true;
926}
927
928bool VirtualSystemSortProxyModel::lessThan (const QModelIndex &aLeft, const QModelIndex &aRight) const
929{
930 if (!aLeft.isValid() ||
931 !aRight.isValid())
932 return false;
933
934 ModelItem *leftItem = static_cast<ModelItem*> (aLeft.internalPointer());
935 ModelItem *rightItem = static_cast<ModelItem*> (aRight.internalPointer());
936
937 /* We sort hardware types only */
938 if (!(leftItem->type() == HardwareType &&
939 rightItem->type() == HardwareType))
940 return false;
941
942 HardwareItem *hwLeft = static_cast<HardwareItem*> (leftItem);
943 HardwareItem *hwRight = static_cast<HardwareItem*> (rightItem);
944
945 for (unsigned int i = 0; i < RT_ELEMENTS (mSortList); ++i)
946 if (hwLeft->mType == mSortList[i])
947 {
948 for (unsigned int a = 0; a <= i; ++a)
949 if (hwRight->mType == mSortList[a])
950 return true;
951 return false;
952 }
953
954 return true;
955}
956
957////////////////////////////////////////////////////////////////////////////////
958// VBoxApplianceEditorWgt
959
960int VBoxApplianceEditorWgt::mMinGuestRAM = -1;
961int VBoxApplianceEditorWgt::mMaxGuestRAM = -1;
962int VBoxApplianceEditorWgt::mMinGuestCPUCount = -1;
963int VBoxApplianceEditorWgt::mMaxGuestCPUCount = -1;
964
965VBoxApplianceEditorWgt::VBoxApplianceEditorWgt (QWidget *aParent /* = NULL */)
966 : QIWithRetranslateUI<QWidget> (aParent)
967 , mAppliance (NULL)
968 , mModel (NULL)
969{
970 /* Make sure all static content is properly initialized */
971 initSystemSettings();
972
973 /* Apply UI decorations */
974 Ui::VBoxApplianceEditorWgt::setupUi (this);
975
976 /* Make the tree looking nicer */
977 mTvSettings->setRootIsDecorated (false);
978 mTvSettings->setAlternatingRowColors (true);
979 mTvSettings->header()->setStretchLastSection (true);
980 mTvSettings->header()->setResizeMode (QHeaderView::ResizeToContents);
981
982 /* Applying language settings */
983 retranslateUi();
984}
985
986void VBoxApplianceEditorWgt::restoreDefaults()
987{
988 mModel->restoreDefaults();
989}
990
991void VBoxApplianceEditorWgt::retranslateUi()
992{
993 /* Translate uic generated strings */
994 Ui::VBoxApplianceEditorWgt::retranslateUi (this);
995}
996
997/* static */
998void VBoxApplianceEditorWgt::initSystemSettings()
999{
1000 if (mMinGuestRAM == -1)
1001 {
1002 /* We need some global defaults from the current VirtualBox
1003 installation */
1004 CSystemProperties sp = vboxGlobal().virtualBox().GetSystemProperties();
1005 mMinGuestRAM = sp.GetMinGuestRAM();
1006 mMaxGuestRAM = sp.GetMaxGuestRAM();
1007 mMinGuestCPUCount = sp.GetMinGuestCPUCount();
1008 mMaxGuestCPUCount = sp.GetMaxGuestCPUCount();
1009 }
1010}
1011
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