VirtualBox

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

Last change on this file since 24766 was 24766, checked in by vboxsync, 15 years ago

FE/Qt: string fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 39.8 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 int i = tmp.indexOf ('\n');
216 if (i > -1)
217 tmp.replace (i, tmp.length(), "...");
218 v = tmp; break;
219 }
220 case KVirtualSystemDescriptionType_OS: v = vboxGlobal().vmGuestOSTypeDescription (mConfigValue); break;
221 case KVirtualSystemDescriptionType_Memory: v = mConfigValue + " " + VBoxApplianceEditorWgt::tr ("MB"); break;
222 case KVirtualSystemDescriptionType_SoundCard: v = vboxGlobal().toString (static_cast<KAudioControllerType> (mConfigValue.toInt())); break;
223 case KVirtualSystemDescriptionType_NetworkAdapter: v = vboxGlobal().toString (static_cast<KNetworkAdapterType> (mConfigValue.toInt())); break;
224 default: v = mConfigValue; break;
225 }
226 }
227 break;
228 }
229 case Qt::ToolTipRole:
230 {
231 if (aColumn == ConfigValueSection)
232 {
233 if (!mOrigValue.isEmpty())
234 v = VBoxApplianceEditorWgt::tr ("<b>Original Value:</b> %1").arg (mOrigValue);
235 }
236 break;
237 }
238 case Qt::DecorationRole:
239 {
240 if (aColumn == DescriptionSection)
241 {
242 switch (mType)
243 {
244 case KVirtualSystemDescriptionType_Name: v = QIcon (":/name_16px.png"); break;
245 case KVirtualSystemDescriptionType_Product:
246 case KVirtualSystemDescriptionType_ProductUrl:
247 case KVirtualSystemDescriptionType_Vendor:
248 case KVirtualSystemDescriptionType_VendorUrl:
249 case KVirtualSystemDescriptionType_Version:
250 case KVirtualSystemDescriptionType_Description:
251 case KVirtualSystemDescriptionType_License: v = QIcon (":/description_16px.png"); break;
252 case KVirtualSystemDescriptionType_OS: v = QIcon (":/os_type_16px.png"); break;
253 case KVirtualSystemDescriptionType_CPU: v = QIcon (":/cpu_16px.png"); break;
254 case KVirtualSystemDescriptionType_Memory: v = QIcon (":/ram_16px.png"); break;
255 case KVirtualSystemDescriptionType_HardDiskControllerIDE: v = QIcon (":/ide_16px.png"); break;
256 case KVirtualSystemDescriptionType_HardDiskControllerSATA: v = QIcon (":/sata_16px.png"); break;
257 case KVirtualSystemDescriptionType_HardDiskControllerSCSI: v = QIcon (":/scsi_16px.png"); break;
258 case KVirtualSystemDescriptionType_HardDiskImage: v = QIcon (":/hd_16px.png"); break;
259 case KVirtualSystemDescriptionType_CDROM: v = QIcon (":/cd_16px.png"); break;
260 case KVirtualSystemDescriptionType_Floppy: v = QIcon (":/fd_16px.png"); break;
261 case KVirtualSystemDescriptionType_NetworkAdapter: v = QIcon (":/nw_16px.png"); break;
262 case KVirtualSystemDescriptionType_USBController: v = QIcon (":/usb_16px.png"); break;
263 case KVirtualSystemDescriptionType_SoundCard: v = QIcon (":/sound_16px.png"); break;
264 default: break;
265 }
266 }
267 else if (aColumn == ConfigValueSection &&
268 mType == KVirtualSystemDescriptionType_OS)
269 {
270 v = vboxGlobal().vmGuestOSTypeIcon (mConfigValue).scaledToHeight (16, Qt::SmoothTransformation);
271 }
272 break;
273 }
274 case Qt::FontRole:
275 {
276 /* If the item is unchecked mark it with italic text. */
277 if (aColumn == ConfigValueSection &&
278 mCheckState == Qt::Unchecked)
279 {
280 QFont font = qApp->font();
281 font.setItalic (true);
282 v = font;
283 }
284 break;
285 }
286 case Qt::ForegroundRole:
287 {
288 /* If the item is unchecked mark it with gray text. */
289 if (aColumn == ConfigValueSection &&
290 mCheckState == Qt::Unchecked)
291 {
292 QPalette pal = qApp->palette();
293 v = pal.brush (QPalette::Disabled, QPalette::WindowText);
294 }
295 break;
296 }
297 case Qt::CheckStateRole:
298 {
299 if (aColumn == ConfigValueSection &&
300 (mType == KVirtualSystemDescriptionType_Floppy ||
301 mType == KVirtualSystemDescriptionType_CDROM ||
302 mType == KVirtualSystemDescriptionType_USBController ||
303 mType == KVirtualSystemDescriptionType_SoundCard ||
304 mType == KVirtualSystemDescriptionType_NetworkAdapter))
305 v = mCheckState;
306 break;
307 }
308 }
309 return v;
310}
311
312Qt::ItemFlags HardwareItem::itemFlags (int aColumn) const
313{
314 Qt::ItemFlags flags = 0;
315 if (aColumn == ConfigValueSection)
316 {
317 /* Some items are checkable */
318 if (mType == KVirtualSystemDescriptionType_Floppy ||
319 mType == KVirtualSystemDescriptionType_CDROM ||
320 mType == KVirtualSystemDescriptionType_USBController ||
321 mType == KVirtualSystemDescriptionType_SoundCard ||
322 mType == KVirtualSystemDescriptionType_NetworkAdapter)
323 flags |= Qt::ItemIsUserCheckable;
324 /* Some items are editable */
325 if ((mType == KVirtualSystemDescriptionType_Name ||
326 mType == KVirtualSystemDescriptionType_Product ||
327 mType == KVirtualSystemDescriptionType_ProductUrl ||
328 mType == KVirtualSystemDescriptionType_Vendor ||
329 mType == KVirtualSystemDescriptionType_VendorUrl ||
330 mType == KVirtualSystemDescriptionType_Version ||
331 mType == KVirtualSystemDescriptionType_Description ||
332 mType == KVirtualSystemDescriptionType_License ||
333 mType == KVirtualSystemDescriptionType_OS ||
334 mType == KVirtualSystemDescriptionType_CPU ||
335 mType == KVirtualSystemDescriptionType_Memory ||
336 mType == KVirtualSystemDescriptionType_SoundCard ||
337 mType == KVirtualSystemDescriptionType_NetworkAdapter ||
338 mType == KVirtualSystemDescriptionType_HardDiskControllerIDE ||
339 mType == KVirtualSystemDescriptionType_HardDiskImage) &&
340 mCheckState == Qt::Checked) /* Item has to be enabled */
341 flags |= Qt::ItemIsEditable;
342 }
343 return flags;
344}
345
346QWidget * HardwareItem::createEditor (QWidget *aParent, const QStyleOptionViewItem & /* aOption */, const QModelIndex &aIndex) const
347{
348 QWidget *editor = NULL;
349 if (aIndex.column() == ConfigValueSection)
350 {
351 switch (mType)
352 {
353 case KVirtualSystemDescriptionType_OS:
354 {
355 VBoxOSTypeSelectorButton *e = new VBoxOSTypeSelectorButton (aParent);
356 /* Fill the background with the highlight color in the case
357 * the button hasn't a rectangle shape. This prevents the
358 * display of parts from the current text on the Mac. */
359 e->setAutoFillBackground (true);
360 e->setBackgroundRole (QPalette::Highlight);
361 editor = e;
362 break;
363 }
364 case KVirtualSystemDescriptionType_Name:
365 case KVirtualSystemDescriptionType_Product:
366 case KVirtualSystemDescriptionType_ProductUrl:
367 case KVirtualSystemDescriptionType_Vendor:
368 case KVirtualSystemDescriptionType_VendorUrl:
369 case KVirtualSystemDescriptionType_Version:
370 {
371 QLineEdit *e = new QLineEdit (aParent);
372 editor = e;
373 break;
374 }
375 case KVirtualSystemDescriptionType_Description:
376 case KVirtualSystemDescriptionType_License:
377 {
378 VBoxLineTextEdit *e = new VBoxLineTextEdit (aParent);
379 editor = e;
380 break;
381 }
382 case KVirtualSystemDescriptionType_CPU:
383 {
384 QSpinBox *e = new QSpinBox (aParent);
385 e->setRange (VBoxApplianceEditorWgt::minGuestCPUCount(), VBoxApplianceEditorWgt::maxGuestCPUCount());
386 editor = e;
387 break;
388 }
389 case KVirtualSystemDescriptionType_Memory:
390 {
391 QSpinBox *e = new QSpinBox (aParent);
392 e->setRange (VBoxApplianceEditorWgt::minGuestRAM(), VBoxApplianceEditorWgt::maxGuestRAM());
393 e->setSuffix (" " + VBoxApplianceEditorWgt::tr ("MB"));
394 editor = e;
395 break;
396 }
397 case KVirtualSystemDescriptionType_SoundCard:
398 {
399 QComboBox *e = new QComboBox (aParent);
400 e->addItem (vboxGlobal().toString (KAudioControllerType_AC97), KAudioControllerType_AC97);
401 e->addItem (vboxGlobal().toString (KAudioControllerType_SB16), KAudioControllerType_SB16);
402 editor = e;
403 break;
404 }
405 case KVirtualSystemDescriptionType_NetworkAdapter:
406 {
407 QComboBox *e = new QComboBox (aParent);
408 e->addItem (vboxGlobal().toString (KNetworkAdapterType_Am79C970A), KNetworkAdapterType_Am79C970A);
409 e->addItem (vboxGlobal().toString (KNetworkAdapterType_Am79C973), KNetworkAdapterType_Am79C973);
410#ifdef VBOX_WITH_E1000
411 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82540EM), KNetworkAdapterType_I82540EM);
412 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82543GC), KNetworkAdapterType_I82543GC);
413 e->addItem (vboxGlobal().toString (KNetworkAdapterType_I82545EM), KNetworkAdapterType_I82545EM);
414#endif /* VBOX_WITH_E1000 */
415#ifdef VBOX_WITH_VIRTIO
416 e->addItem (vboxGlobal().toString (KNetworkAdapterType_Virtio), KNetworkAdapterType_Virtio);
417#endif /* VBOX_WITH_VIRTIO */
418 editor = e;
419 break;
420 }
421 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
422 {
423 QComboBox *e = new QComboBox (aParent);
424 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX3), "PIIX3");
425 e->addItem (vboxGlobal().toString (KStorageControllerType_PIIX4), "PIIX4");
426 e->addItem (vboxGlobal().toString (KStorageControllerType_ICH6), "ICH6");
427 editor = e;
428 break;
429 }
430 case KVirtualSystemDescriptionType_HardDiskImage:
431 {
432 /* disabled for now
433 VBoxFilePathSelectorWidget *e = new VBoxFilePathSelectorWidget (aParent);
434 e->setMode (VBoxFilePathSelectorWidget::Mode_File);
435 e->setResetEnabled (false);
436 */
437 QLineEdit *e = new QLineEdit (aParent);
438 editor = e;
439 break;
440 }
441 default: break;
442 }
443 }
444 return editor;
445}
446
447bool HardwareItem::setEditorData (QWidget *aEditor, const QModelIndex & /* aIndex */) const
448{
449 bool fDone = false;
450 switch (mType)
451 {
452 case KVirtualSystemDescriptionType_OS:
453 {
454 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
455 {
456 e->setOSTypeId (mConfigValue);
457 fDone = true;
458 }
459 break;
460 }
461 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
462 {
463 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
464 {
465 int i = e->findData (mConfigValue);
466 if (i != -1)
467 e->setCurrentIndex (i);
468 fDone = true;
469 }
470 break;
471 }
472 case KVirtualSystemDescriptionType_CPU:
473 case KVirtualSystemDescriptionType_Memory:
474 {
475 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
476 {
477 e->setValue (mConfigValue.toInt());
478 fDone = true;
479 }
480 break;
481 }
482 case KVirtualSystemDescriptionType_Name:
483 case KVirtualSystemDescriptionType_Product:
484 case KVirtualSystemDescriptionType_ProductUrl:
485 case KVirtualSystemDescriptionType_Vendor:
486 case KVirtualSystemDescriptionType_VendorUrl:
487 case KVirtualSystemDescriptionType_Version:
488 {
489 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
490 {
491 e->setText (mConfigValue);
492 fDone = true;
493 }
494 break;
495 }
496 case KVirtualSystemDescriptionType_Description:
497 case KVirtualSystemDescriptionType_License:
498 {
499 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
500 {
501 e->setText (mConfigValue);
502 fDone = true;
503 }
504 break;
505 }
506 case KVirtualSystemDescriptionType_SoundCard:
507 case KVirtualSystemDescriptionType_NetworkAdapter:
508 {
509 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
510 {
511 int i = e->findData (mConfigValue.toInt());
512 if (i != -1)
513 e->setCurrentIndex (i);
514 fDone = true;
515 }
516 break;
517 }
518 case KVirtualSystemDescriptionType_HardDiskImage:
519 {
520 /* disabled for now
521 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
522 {
523 e->setPath (mConfigValue);
524 }
525 */
526 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
527 {
528 e->setText (mConfigValue);
529 fDone = true;
530 }
531 break;
532 }
533 default: break;
534 }
535 return fDone;
536}
537
538bool HardwareItem::setModelData (QWidget *aEditor, QAbstractItemModel * /* aModel */, const QModelIndex & /* aIndex */)
539{
540 bool fDone = false;
541 switch (mType)
542 {
543 case KVirtualSystemDescriptionType_OS:
544 {
545 if (VBoxOSTypeSelectorButton *e = qobject_cast<VBoxOSTypeSelectorButton*> (aEditor))
546 {
547 mConfigValue = e->osTypeId();
548 fDone = true;
549 }
550 break;
551 }
552 case KVirtualSystemDescriptionType_HardDiskControllerIDE:
553 {
554 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
555 {
556 mConfigValue = e->itemData (e->currentIndex()).toString();
557 fDone = true;
558 }
559 break;
560 }
561 case KVirtualSystemDescriptionType_CPU:
562 case KVirtualSystemDescriptionType_Memory:
563 {
564 if (QSpinBox *e = qobject_cast<QSpinBox*> (aEditor))
565 {
566 mConfigValue = QString::number (e->value());
567 fDone = true;
568 }
569 break;
570 }
571 case KVirtualSystemDescriptionType_Name:
572 case KVirtualSystemDescriptionType_Product:
573 case KVirtualSystemDescriptionType_ProductUrl:
574 case KVirtualSystemDescriptionType_Vendor:
575 case KVirtualSystemDescriptionType_VendorUrl:
576 case KVirtualSystemDescriptionType_Version:
577 {
578 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
579 {
580 mConfigValue = e->text();
581 fDone = true;
582 }
583 break;
584 }
585 case KVirtualSystemDescriptionType_Description:
586 case KVirtualSystemDescriptionType_License:
587 {
588 if (VBoxLineTextEdit *e = qobject_cast<VBoxLineTextEdit*> (aEditor))
589 {
590 mConfigValue = e->text();
591 fDone = true;
592 }
593 break;
594 }
595 case KVirtualSystemDescriptionType_SoundCard:
596 case KVirtualSystemDescriptionType_NetworkAdapter:
597 {
598 if (QComboBox *e = qobject_cast<QComboBox*> (aEditor))
599 {
600 mConfigValue = e->itemData (e->currentIndex()).toString();
601 fDone = true;
602 }
603 break;
604 }
605 case KVirtualSystemDescriptionType_HardDiskImage:
606 {
607 /* disabled for now
608 if (VBoxFilePathSelectorWidget *e = qobject_cast<VBoxFilePathSelectorWidget*> (aEditor))
609 {
610 mConfigValue = e->path();
611 }
612 */
613 if (QLineEdit *e = qobject_cast<QLineEdit*> (aEditor))
614 {
615 mConfigValue = e->text();
616 fDone = true;
617 }
618 break;
619 }
620 default: break;
621 }
622 return fDone;
623}
624
625////////////////////////////////////////////////////////////////////////////////
626// VirtualSystemModel
627
628/* This class is a wrapper model for our ModelItem. It could be used with any
629 TreeView & forward mostly all calls to the methods of ModelItem. The
630 ModelItems itself are stored as internal pointers in the QModelIndex class. */
631VirtualSystemModel::VirtualSystemModel (QVector<CVirtualSystemDescription>& aVSDs, QObject *aParent /* = NULL */)
632 : QAbstractItemModel (aParent)
633{
634 mRootItem = new ModelItem (0, RootType);
635 for (int a = 0; a < aVSDs.size(); ++a)
636 {
637 CVirtualSystemDescription vs = aVSDs[a];
638
639 VirtualSystemItem *vi = new VirtualSystemItem (a, vs, mRootItem);
640 mRootItem->appendChild (vi);
641
642 /* @todo: ask Dmitry about include/COMDefs.h:232 */
643 QVector<KVirtualSystemDescriptionType> types;
644 QVector<QString> refs;
645 QVector<QString> origValues;
646 QVector<QString> configValues;
647 QVector<QString> extraConfigValues;
648
649 QList<int> hdIndizies;
650 QMap<int, HardwareItem*> controllerMap;
651 vs.GetDescription (types, refs, origValues, configValues, extraConfigValues);
652 for (int i = 0; i < types.size(); ++i)
653 {
654 /* We add the hard disk images in an second step, so save a
655 reference to them. */
656 if (types[i] == KVirtualSystemDescriptionType_HardDiskImage)
657 hdIndizies << i;
658 else
659 {
660 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], vi);
661 vi->appendChild (hi);
662 /* Save the hard disk controller types in an extra map */
663 if (types[i] == KVirtualSystemDescriptionType_HardDiskControllerIDE ||
664 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSATA ||
665 types[i] == KVirtualSystemDescriptionType_HardDiskControllerSCSI)
666 controllerMap[i] = hi;
667 }
668 }
669 QRegExp rx ("controller=(\\d+);?");
670 /* Now process the hard disk images */
671 for (int a = 0; a < hdIndizies.size(); ++a)
672 {
673 int i = hdIndizies[a];
674 QString ecnf = extraConfigValues[i];
675 if (rx.indexIn (ecnf) != -1)
676 {
677 /* Get the controller */
678 HardwareItem *ci = controllerMap[rx.cap (1).toInt()];
679 if (ci)
680 {
681 /* New hardware item as child of the controller */
682 HardwareItem *hi = new HardwareItem (i, types[i], refs[i], origValues[i], configValues[i], extraConfigValues[i], ci);
683 ci->appendChild (hi);
684 }
685 }
686 }
687 }
688}
689
690QModelIndex VirtualSystemModel::index (int aRow, int aColumn, const QModelIndex &aParent /* = QModelIndex() */) const
691{
692 if (!hasIndex (aRow, aColumn, aParent))
693 return QModelIndex();
694
695 ModelItem *parentItem;
696
697 if (!aParent.isValid())
698 parentItem = mRootItem;
699 else
700 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
701
702 ModelItem *childItem = parentItem->child (aRow);
703 if (childItem)
704 return createIndex (aRow, aColumn, childItem);
705 else
706 return QModelIndex();
707}
708
709QModelIndex VirtualSystemModel::parent (const QModelIndex &aIndex) const
710{
711 if (!aIndex.isValid())
712 return QModelIndex();
713
714 ModelItem *childItem = static_cast<ModelItem*> (aIndex.internalPointer());
715 ModelItem *parentItem = childItem->parent();
716
717 if (parentItem == mRootItem)
718 return QModelIndex();
719
720 return createIndex (parentItem->row(), 0, parentItem);
721}
722
723int VirtualSystemModel::rowCount (const QModelIndex &aParent /* = QModelIndex() */) const
724{
725 ModelItem *parentItem;
726 if (aParent.column() > 0)
727 return 0;
728
729 if (!aParent.isValid())
730 parentItem = mRootItem;
731 else
732 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
733
734 return parentItem->childCount();
735}
736
737int VirtualSystemModel::columnCount (const QModelIndex &aParent /* = QModelIndex() */) const
738{
739 if (aParent.isValid())
740 return static_cast<ModelItem*> (aParent.internalPointer())->columnCount();
741 else
742 return mRootItem->columnCount();
743}
744
745bool VirtualSystemModel::setData (const QModelIndex &aIndex, const QVariant &aValue, int aRole)
746{
747 if (!aIndex.isValid())
748 return false;
749
750 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
751
752 return item->setData (aIndex.column(), aValue, aRole);
753}
754
755QVariant VirtualSystemModel::data (const QModelIndex &aIndex, int aRole /* = Qt::DisplayRole */) const
756{
757 if (!aIndex.isValid())
758 return QVariant();
759
760 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
761
762 return item->data (aIndex.column(), aRole);
763}
764
765Qt::ItemFlags VirtualSystemModel::flags (const QModelIndex &aIndex) const
766{
767 if (!aIndex.isValid())
768 return 0;
769
770 ModelItem *item = static_cast<ModelItem*> (aIndex.internalPointer());
771
772 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | item->itemFlags (aIndex.column());
773}
774
775QVariant VirtualSystemModel::headerData (int aSection, Qt::Orientation aOrientation, int aRole) const
776{
777 if (aRole != Qt::DisplayRole ||
778 aOrientation != Qt::Horizontal)
779 return QVariant();
780
781 QString title;
782 switch (aSection)
783 {
784 case DescriptionSection: title = VBoxApplianceEditorWgt::tr ("Description"); break;
785 case ConfigValueSection: title = VBoxApplianceEditorWgt::tr ("Configuration"); break;
786 }
787 return title;
788}
789
790QModelIndex VirtualSystemModel::buddy (const QModelIndex &aIndex) const
791{
792 if (!aIndex.isValid())
793 return QModelIndex();
794
795 if (aIndex.column() == ConfigValueSection)
796 return aIndex;
797 else
798 return index (aIndex.row(), ConfigValueSection, aIndex.parent());
799}
800
801void VirtualSystemModel::restoreDefaults (const QModelIndex& aParent /* = QModelIndex() */)
802{
803 ModelItem *parentItem;
804
805 if (!aParent.isValid())
806 parentItem = mRootItem;
807 else
808 parentItem = static_cast<ModelItem*> (aParent.internalPointer());
809
810 for (int i = 0; i < parentItem->childCount(); ++i)
811 {
812 parentItem->child (i)->restoreDefaults();
813 restoreDefaults (index (i, 0, aParent));
814 }
815 emit dataChanged (index (0, 0, aParent), index (parentItem->childCount()-1, 0, aParent));
816}
817
818void VirtualSystemModel::putBack()
819{
820 QVector<BOOL> v1;
821 QVector<QString> v2;
822 QVector<QString> v3;
823 mRootItem->putBack (v1, v2, v3);
824}
825
826////////////////////////////////////////////////////////////////////////////////
827// VirtualSystemDelegate
828
829/* The delegate is used for creating/handling the different editors for the
830 various types we support. This class forward the requests to the virtual
831 methods of our different ModelItems. If this is not possible the default
832 methods of QItemDelegate are used to get some standard behavior. Note: We
833 have to handle the proxy model ourself. I really don't understand why Qt is
834 not doing this for us. */
835VirtualSystemDelegate::VirtualSystemDelegate (QAbstractProxyModel *aProxy, QObject *aParent /* = NULL */)
836 : QItemDelegate (aParent)
837 , mProxy (aProxy)
838{}
839
840QWidget * VirtualSystemDelegate::createEditor (QWidget *aParent, const QStyleOptionViewItem &aOption, const QModelIndex &aIndex) const
841{
842 if (!aIndex.isValid())
843 return QItemDelegate::createEditor (aParent, aOption, aIndex);
844
845 QModelIndex index (aIndex);
846 if (mProxy)
847 index = mProxy->mapToSource (aIndex);
848
849 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
850 QWidget *editor = item->createEditor (aParent, aOption, index);
851
852 if (editor == NULL)
853 return QItemDelegate::createEditor (aParent, aOption, index);
854 else
855 return editor;
856}
857
858void VirtualSystemDelegate::setEditorData (QWidget *aEditor, const QModelIndex &aIndex) const
859{
860 if (!aIndex.isValid())
861 return QItemDelegate::setEditorData (aEditor, aIndex);
862
863 QModelIndex index (aIndex);
864 if (mProxy)
865 index = mProxy->mapToSource (aIndex);
866
867 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
868
869 if (!item->setEditorData (aEditor, index))
870 QItemDelegate::setEditorData (aEditor, index);
871}
872
873void VirtualSystemDelegate::setModelData (QWidget *aEditor, QAbstractItemModel *aModel, const QModelIndex &aIndex) const
874{
875 if (!aIndex.isValid())
876 return QItemDelegate::setModelData (aEditor, aModel, aIndex);
877
878 QModelIndex index = aModel->index (aIndex.row(), aIndex.column());
879 if (mProxy)
880 index = mProxy->mapToSource (aIndex);
881
882 ModelItem *item = static_cast<ModelItem*> (index.internalPointer());
883 if (!item->setModelData (aEditor, aModel, index))
884 QItemDelegate::setModelData (aEditor, aModel, aIndex);
885}
886
887void VirtualSystemDelegate::updateEditorGeometry (QWidget *aEditor, const QStyleOptionViewItem &aOption, const QModelIndex & /* aIndex */) const
888{
889 if (aEditor)
890 aEditor->setGeometry (aOption.rect);
891}
892
893////////////////////////////////////////////////////////////////////////////////
894// VirtualSystemSortProxyModel
895
896/* How to sort the items in the tree view */
897KVirtualSystemDescriptionType VirtualSystemSortProxyModel::mSortList[] =
898{
899 KVirtualSystemDescriptionType_Name,
900 KVirtualSystemDescriptionType_Product,
901 KVirtualSystemDescriptionType_ProductUrl,
902 KVirtualSystemDescriptionType_Vendor,
903 KVirtualSystemDescriptionType_VendorUrl,
904 KVirtualSystemDescriptionType_Version,
905 KVirtualSystemDescriptionType_Description,
906 KVirtualSystemDescriptionType_License,
907 KVirtualSystemDescriptionType_OS,
908 KVirtualSystemDescriptionType_CPU,
909 KVirtualSystemDescriptionType_Memory,
910 KVirtualSystemDescriptionType_Floppy,
911 KVirtualSystemDescriptionType_CDROM,
912 KVirtualSystemDescriptionType_USBController,
913 KVirtualSystemDescriptionType_SoundCard,
914 KVirtualSystemDescriptionType_NetworkAdapter,
915 KVirtualSystemDescriptionType_HardDiskControllerIDE,
916 KVirtualSystemDescriptionType_HardDiskControllerSATA,
917 KVirtualSystemDescriptionType_HardDiskControllerSCSI
918};
919
920VirtualSystemSortProxyModel::VirtualSystemSortProxyModel (QObject *aParent)
921 : QSortFilterProxyModel (aParent)
922{}
923
924bool VirtualSystemSortProxyModel::filterAcceptsRow (int aSourceRow, const QModelIndex & aSourceParent) const
925{
926 /* By default enable all, we will explicitly filter out below */
927 if (aSourceParent.isValid())
928 {
929 QModelIndex i = aSourceParent.child (aSourceRow, 0);
930 if (i.isValid())
931 {
932 ModelItem *item = static_cast<ModelItem*> (i.internalPointer());
933 /* We filter hardware types only */
934 if (item->type() == HardwareType)
935 {
936 HardwareItem *hwItem = static_cast<HardwareItem*> (item);
937 /* The license type shouldn't be displayed */
938 if (mFilterList.contains (hwItem->mType))
939 return false;
940 }
941 }
942 }
943 return true;
944}
945
946bool VirtualSystemSortProxyModel::lessThan (const QModelIndex &aLeft, const QModelIndex &aRight) const
947{
948 if (!aLeft.isValid() ||
949 !aRight.isValid())
950 return false;
951
952 ModelItem *leftItem = static_cast<ModelItem*> (aLeft.internalPointer());
953 ModelItem *rightItem = static_cast<ModelItem*> (aRight.internalPointer());
954
955 /* We sort hardware types only */
956 if (!(leftItem->type() == HardwareType &&
957 rightItem->type() == HardwareType))
958 return false;
959
960 HardwareItem *hwLeft = static_cast<HardwareItem*> (leftItem);
961 HardwareItem *hwRight = static_cast<HardwareItem*> (rightItem);
962
963 for (unsigned int i = 0; i < RT_ELEMENTS (mSortList); ++i)
964 if (hwLeft->mType == mSortList[i])
965 {
966 for (unsigned int a = 0; a <= i; ++a)
967 if (hwRight->mType == mSortList[a])
968 return true;
969 return false;
970 }
971
972 return true;
973}
974
975////////////////////////////////////////////////////////////////////////////////
976// VBoxApplianceEditorWgt
977
978int VBoxApplianceEditorWgt::mMinGuestRAM = -1;
979int VBoxApplianceEditorWgt::mMaxGuestRAM = -1;
980int VBoxApplianceEditorWgt::mMinGuestCPUCount = -1;
981int VBoxApplianceEditorWgt::mMaxGuestCPUCount = -1;
982
983VBoxApplianceEditorWgt::VBoxApplianceEditorWgt (QWidget *aParent /* = NULL */)
984 : QIWithRetranslateUI<QWidget> (aParent)
985 , mAppliance (NULL)
986 , mModel (NULL)
987{
988 /* Make sure all static content is properly initialized */
989 initSystemSettings();
990
991 /* Apply UI decorations */
992 Ui::VBoxApplianceEditorWgt::setupUi (this);
993
994 /* Make the tree looking nicer */
995 mTvSettings->setRootIsDecorated (false);
996 mTvSettings->setAlternatingRowColors (true);
997 mTvSettings->setAllColumnsShowFocus (true);
998 mTvSettings->header()->setStretchLastSection (true);
999 mTvSettings->header()->setResizeMode (QHeaderView::ResizeToContents);
1000
1001 /* Applying language settings */
1002 retranslateUi();
1003}
1004
1005void VBoxApplianceEditorWgt::restoreDefaults()
1006{
1007 mModel->restoreDefaults();
1008}
1009
1010void VBoxApplianceEditorWgt::retranslateUi()
1011{
1012 /* Translate uic generated strings */
1013 Ui::VBoxApplianceEditorWgt::retranslateUi (this);
1014}
1015
1016/* static */
1017void VBoxApplianceEditorWgt::initSystemSettings()
1018{
1019 if (mMinGuestRAM == -1)
1020 {
1021 /* We need some global defaults from the current VirtualBox
1022 installation */
1023 CSystemProperties sp = vboxGlobal().virtualBox().GetSystemProperties();
1024 mMinGuestRAM = sp.GetMinGuestRAM();
1025 mMaxGuestRAM = sp.GetMaxGuestRAM();
1026 mMinGuestCPUCount = sp.GetMinGuestCPUCount();
1027 mMaxGuestCPUCount = sp.GetMaxGuestCPUCount();
1028 }
1029}
1030
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