VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxHardDiskSettings.ui.h@ 8201

Last change on this file since 8201 was 8201, checked in by vboxsync, 17 years ago

FE/Qt: Hard Disk UI: Fixed OSE (No SATA so far).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.4 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * VBoxHardDiskSettings widget UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2008 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/****************************************************************************
24** ui.h extension file, included from the uic-generated form implementation.
25**
26** If you wish to add, delete or rename functions or slots use
27** Qt Designer which will update this file, preserving your code. Create an
28** init() function in place of a constructor, and a destroy() function in
29** place of a destructor.
30*****************************************************************************/
31
32/** SATA Ports count */
33static const ULONG SATAPortsCount = 30;
34
35class HDSlotItem;
36
37/** Combines the string and the numeric representation of the hard disk slot. */
38struct HDSlot
39{
40 HDSlot() : bus (KStorageBus_Null), channel (0), device (0) {}
41 HDSlot (const QString &aStr, KStorageBus aBus, LONG aChannel, LONG aDevice)
42 : str (aStr), bus (aBus), channel (aChannel), device (aDevice) {}
43
44 QString str;
45 KStorageBus bus;
46 LONG channel;
47 LONG device;
48};
49
50/**
51 * QObject class reimplementation to use for making selected IDE & SATA
52 * slots unique.
53 */
54class HDSlotUniquizer : public QObject
55{
56 Q_OBJECT
57
58public:
59
60 HDSlotUniquizer (QWidget *aParent, int aSataPortsCount = 0)
61 : QObject (aParent)
62 , mSataPortsCount (aSataPortsCount)
63 {
64 /* Compose Lists */
65 makeIDEList();
66 makeSATAList();
67 }
68
69 QValueList<HDSlot> list (HDSlotItem *aForSubscriber, bool aFilter = true);
70
71 int totalCount() { return mIDEList.size() + mSATAList.size(); }
72
73 int getSATAPortsCount()
74 {
75 return mSataPortsCount;
76 }
77
78 void setSATAPortsCount (int aSataPortsCount)
79 {
80 mSataPortsCount = aSataPortsCount;
81 makeSATAList();
82 }
83
84 void subscribe (HDSlotItem *aSubscriber)
85 {
86 bool result = mSubscribersList.resize (mSubscribersList.size() + 1);
87 if (!result)
88 return;
89
90 mSubscribersList.insert (mSubscribersList.size() - 1, aSubscriber);
91 mSubscribersList.sort();
92
93 emit listChanged();
94 }
95
96 void unsubscribe (HDSlotItem *aSubscriber)
97 {
98 int index = mSubscribersList.findRef (aSubscriber);
99 if (index == -1)
100 return;
101
102 mSubscribersList.remove (index);
103 mSubscribersList.sort();
104 mSubscribersList.resize (mSubscribersList.size() - 1);
105
106 emit listChanged();
107 }
108
109signals:
110
111 void listChanged();
112
113private:
114
115 void makeIDEList()
116 {
117 mIDEList.clear();
118
119 /* IDE Primary Master */
120 mIDEList << HDSlot (vboxGlobal().toFullString (KStorageBus_IDE, 0, 0),
121 KStorageBus_IDE, 0, 0);
122 /* IDE Primary Slave */
123 mIDEList << HDSlot (vboxGlobal().toFullString (KStorageBus_IDE, 0, 1),
124 KStorageBus_IDE, 0, 1);
125 /* IDE Secondary Slave */
126 mIDEList << HDSlot (vboxGlobal().toFullString (KStorageBus_IDE, 1, 1),
127 KStorageBus_IDE, 1, 1);
128
129 emit listChanged();
130 }
131
132 void makeSATAList()
133 {
134 mSATAList.clear();
135
136 for (int i = 0; i < mSataPortsCount; ++ i)
137 mSATAList << HDSlot (vboxGlobal().toFullString (KStorageBus_SATA, 0, i),
138 KStorageBus_SATA, 0, i);
139
140 emit listChanged();
141 }
142
143 int mSataPortsCount;
144 QValueList<HDSlot> mIDEList;
145 QValueList<HDSlot> mSATAList;
146 QPtrVector<HDSlotItem> mSubscribersList;
147};
148
149/**
150 * QComboBox class reimplementation to use as selector for IDE & SATA
151 * slots.
152 */
153class HDSlotItem : public QComboBox
154{
155 Q_OBJECT
156
157public:
158
159 HDSlotItem (QWidget *aParent, HDSlotUniquizer *aUniq)
160 : QComboBox (aParent)
161 , mUniq (aUniq)
162 {
163 /* In some qt themes embedded list-box is not used by default */
164 if (!listBox())
165 setListBox (new QListBox (this));
166
167 refresh();
168 mUniq->subscribe (this);
169 qApp->installEventFilter (this);
170 connect (mUniq, SIGNAL (listChanged()), this, SLOT (refresh()));
171 connect (this, SIGNAL (activated (int)), mUniq, SIGNAL (listChanged()));
172 connect (this, SIGNAL (textChanged()), mUniq, SIGNAL (listChanged()));
173 }
174
175 ~HDSlotItem()
176 {
177 mUniq->unsubscribe (this);
178 }
179
180 void setText (const QString &aText)
181 {
182 QComboBox::setCurrentText (aText);
183 emit textChanged();
184 }
185
186 KStorageBus currentBus() const
187 {
188 AssertReturn (currentItem() >= 0 && (size_t) currentItem() < mHDSlots.size(),
189 KStorageBus_Null);
190 return mHDSlots [currentItem()].bus;
191 }
192
193 LONG currentChannel() const
194 {
195 AssertReturn (currentItem() >= 0 && (size_t) currentItem() < mHDSlots.size(),
196 0);
197 return mHDSlots [currentItem()].channel;
198 }
199
200 LONG currentDevice() const
201 {
202 AssertReturn (currentItem() >= 0 && (size_t) currentItem() < mHDSlots.size(),
203 0);
204 return mHDSlots [currentItem()].device;
205 }
206
207private slots:
208
209 void refresh()
210 {
211 QString current = currentText();
212 mHDSlots = mUniq->list (this);
213 clear();
214
215 bool setCurrent = false;
216
217 for (QValueList<HDSlot>::const_iterator it = mHDSlots.begin();
218 it != mHDSlots.end(); ++ it)
219 {
220 insertItem ((*it).str);
221 if (!setCurrent)
222 setCurrent = (*it).str == current;
223 }
224
225 if (setCurrent)
226 setCurrentText (current);
227 }
228
229signals:
230
231 void textChanged();
232
233private:
234
235 bool eventFilter (QObject *aObject, QEvent *aEvent)
236 {
237 if (aObject != listBox())
238 return false;
239
240 if (aEvent->type() == QEvent::Hide)
241 hide();
242
243 return QComboBox::eventFilter (aObject, aEvent);
244 }
245
246 HDSlotUniquizer *mUniq;
247
248 QValueList<HDSlot> mHDSlots;
249};
250
251/**
252 * VBoxMediaComboBox class reimplementation to use as selector for VDI
253 * image.
254 */
255class HDVdiItem : public VBoxMediaComboBox
256{
257 Q_OBJECT
258
259public:
260
261 HDVdiItem (QWidget *aParent, int aType, QListViewItem *aItem)
262 : VBoxMediaComboBox (aParent, "HDVdiItem", aType)
263 , mItem (aItem)
264 {
265 qApp->installEventFilter (this);
266 connect (&vboxGlobal(),
267 SIGNAL (mediaRemoved (VBoxDefs::DiskType, const QUuid &)),
268 this, SLOT (repaintHandler()));
269 }
270
271private slots:
272
273 void repaintHandler()
274 {
275 mItem->repaint();
276 }
277
278private:
279
280 bool eventFilter (QObject *aObject, QEvent *aEvent)
281 {
282 if (aObject != listBox())
283 return false;
284
285 if (aEvent->type() == QEvent::Hide)
286 hide();
287
288 return VBoxMediaComboBox::eventFilter (aObject, aEvent);
289 }
290
291 QListViewItem *mItem;
292};
293
294QValueList<HDSlot> HDSlotUniquizer::list (HDSlotItem *aSubscriber, bool aFilter)
295{
296 QValueList<HDSlot> list = mIDEList + mSATAList;
297
298 if (!aFilter)
299 return list;
300
301 /* Compose exclude list */
302 QStringList excludeList;
303 for (uint i = 0; i < mSubscribersList.size(); ++ i)
304 if (mSubscribersList [i] != aSubscriber)
305 excludeList << mSubscribersList [i]->currentText();
306
307 /* Filter the list */
308 QValueList<HDSlot>::Iterator it = list.begin();
309 while (it != list.end())
310 {
311 if (excludeList.contains ((*it).str))
312 it = list.remove (it);
313 else
314 ++ it;
315 }
316
317 return list;
318}
319
320class HDListItem : public QListViewItem
321{
322public:
323
324 enum { HDListItemType = 1010 };
325
326 HDListItem (VBoxHardDiskSettings *aWidget, QListView *aParent,
327 QListViewItem *aAfter,
328 HDSlotUniquizer *aUniq, const CMachine &aMachine)
329 : QListViewItem (aParent, aAfter)
330 , mWidget (aWidget)
331 , mUniq (aUniq)
332 , mMachine (aMachine)
333 , mFocusColumn (-1)
334 {
335 init();
336 }
337
338 HDListItem (VBoxHardDiskSettings *aWidget, QListView *aParent,
339 HDSlotUniquizer *aUniq, const CMachine &aMachine)
340 : QListViewItem (aParent)
341 , mWidget (aWidget)
342 , mUniq (aUniq)
343 , mMachine (aMachine)
344 , mFocusColumn (-1)
345 {
346 init();
347 }
348
349 int rtti() const { return HDListItemType; }
350
351 HDListItem* nextSibling() const
352 {
353 QListViewItem *item = QListViewItem::nextSibling();
354 return item && item->rtti() == HDListItemType ?
355 static_cast<HDListItem*> (item) : 0;
356 }
357
358 void setId (const QUuid &aId) const
359 {
360 static_cast<VBoxMediaComboBox*> (mVector [1])->setCurrentItem (aId);
361 }
362
363 QUuid getId() const
364 {
365 return static_cast<VBoxMediaComboBox*> (mVector [1])->getId();
366 }
367
368 KStorageBus bus() const
369 {
370 return static_cast<HDSlotItem*> (mVector [0])->currentBus();
371 }
372
373 LONG channel() const
374 {
375 return static_cast<HDSlotItem*> (mVector [0])->currentChannel();
376 }
377
378 LONG device() const
379 {
380 return static_cast<HDSlotItem*> (mVector [0])->currentDevice();
381 }
382
383 QString text (int aColumn) const
384 {
385 return mVector [aColumn]->currentText();
386 }
387
388 void moveFocusToColumn (int aCol)
389 {
390 mFocusColumn = aCol;
391 repaint();
392 }
393
394 void showEditor()
395 {
396 if (mVector [mFocusColumn]->count())
397 {
398 mVector [mFocusColumn]->show();
399 mVector [mFocusColumn]->popup();
400 }
401 }
402
403 int focusColumn() const
404 {
405 return mFocusColumn;
406 }
407
408 void setAttachment (const CHardDiskAttachment &aHda)
409 {
410 QString device = vboxGlobal()
411 .toFullString (aHda.GetBus(), aHda.GetChannel(), aHda.GetDevice());
412
413 if (mVector [0]->listBox()->findItem (device, Qt::ExactMatch))
414 static_cast<HDSlotItem*> (mVector [0])->setText (device);
415
416 static_cast<VBoxMediaComboBox*> (mVector [1])->
417 setCurrentItem (aHda.GetHardDisk().GetId());
418
419 mVector [0]->setHidden (true);
420 mVector [1]->setHidden (true);
421 }
422
423private:
424
425 void init()
426 {
427 setSelectable (false);
428 mVector.setAutoDelete (true);
429 mVector.resize (listView()->columns());
430
431 QComboBox *cbslot = new HDSlotItem (listView()->viewport(), mUniq);
432 QObject::connect (cbslot, SIGNAL (activated (int)),
433 mWidget, SIGNAL (hddListChanged()));
434 mVector.insert (0, cbslot);
435
436 VBoxMediaComboBox *cbvdi = new HDVdiItem (listView()->viewport(),
437 VBoxDefs::HD, this);
438 QObject::connect (cbvdi, SIGNAL (activated (int)),
439 mWidget, SIGNAL (hddListChanged()));
440 mVector.insert (1, cbvdi);
441 cbvdi->setBelongsTo (mMachine.GetId());
442 cbvdi->refresh();
443 }
444
445 void paintCell (QPainter *aPainter, const QColorGroup &aColorGroup,
446 int aColumn, int aWidth, int aAlign)
447 {
448 QComboBox *cb = mVector [aColumn];
449
450 int indent = 0;
451 for (int i = 0; i < aColumn; ++ i)
452 indent = listView()->columnWidth (i);
453
454 QRect rect = listView()->itemRect (this);
455
456 int xc = rect.x() + indent;
457 int yc = rect.y();
458 int wc = listView()->columnWidth (aColumn);
459 int hc = rect.height();
460
461 cb->move (xc, yc);
462 cb->resize (wc, hc);
463
464 QColorGroup cg (aColorGroup);
465 if (aColumn == mFocusColumn)
466 {
467 cg.setColor (QColorGroup::Base, aColorGroup.highlight());
468 cg.setColor (QColorGroup::Text, aColorGroup.highlightedText());
469 }
470 QListViewItem::paintCell (aPainter, cg, aColumn, aWidth, aAlign);
471 }
472
473 void paintFocus (QPainter *aPainter, const QColorGroup &aColorGroup,
474 const QRect &aRect)
475 {
476 if (mFocusColumn != -1)
477 {
478 int indent = 0;
479 for (int i = 0; i < mFocusColumn; ++ i)
480 indent = listView()->columnWidth (i);
481
482 QRect newFocus (QPoint (aRect.x() + indent, aRect.y()),
483 QSize (listView()->columnWidth (mFocusColumn),
484 aRect.height()));
485
486 QListViewItem::paintFocus (aPainter, aColorGroup, newFocus);
487 }
488 }
489
490 void setup()
491 {
492 QListViewItem::setup();
493 /* Increasing item's height by 30% */
494 setHeight ((int) (height() * 1.3));
495 }
496
497 VBoxHardDiskSettings *mWidget;
498 HDSlotUniquizer *mUniq;
499 CMachine mMachine;
500 QPtrVector<QComboBox> mVector;
501 int mFocusColumn;
502};
503
504class OnItemChangedEvent : public QEvent
505{
506public:
507 enum { Type = QEvent::User + 10 };
508 OnItemChangedEvent (QListViewItem *aItem)
509 : QEvent ((QEvent::Type) Type), mItem (aItem) {}
510
511 QListViewItem *mItem;
512};
513
514void VBoxHardDiskSettings::init()
515{
516 mPrevItem = 0;
517
518 /* toolbar */
519
520 VBoxToolBar *toolBar = new VBoxToolBar (0, mGbHDList, "snapshotToolBar");
521
522 mAddAttachmentAct->addTo (toolBar);
523 mRemoveAttachmentAct->addTo (toolBar);
524 mSelectHardDiskAct->addTo (toolBar);
525
526 toolBar->setUsesTextLabel (false);
527 toolBar->setUsesBigPixmaps (false);
528 toolBar->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
529 toolBar->setOrientation (Qt::Vertical);
530#ifdef Q_WS_MAC
531 toolBar->setMacStyle();
532#endif
533 mToolBarLayout->insertWidget (0, toolBar);
534
535 /* context menu */
536 mContextMenu = new QPopupMenu (this);
537 mAddAttachmentAct->addTo (mContextMenu);
538 mRemoveAttachmentAct->addTo (mContextMenu);
539 mSelectHardDiskAct->addTo (mContextMenu);
540
541 /* icons */
542 mAddAttachmentAct->setIconSet
543 (VBoxGlobal::iconSet ("vdm_add_16px.png", "vdm_add_disabled_16px.png"));
544 mRemoveAttachmentAct->setIconSet
545 (VBoxGlobal::iconSet ("vdm_remove_16px.png", "vdm_remove_disabled_16px.png"));
546 mSelectHardDiskAct->setIconSet
547 (VBoxGlobal::iconSet ("select_file_16px.png", "select_file_dis_16px.png"));
548
549 /* connections */
550 connect (mCbSATA, SIGNAL (toggled (bool)),
551 this, SLOT (onToggleSATAController (bool)));
552 connect (mLvHD, SIGNAL (pressed (QListViewItem*, const QPoint&, int)),
553 this, SLOT (moveFocus (QListViewItem*, const QPoint&, int)));
554 connect (mLvHD, SIGNAL (currentChanged (QListViewItem*)),
555 this, SLOT (onCurrentChanged (QListViewItem*)));
556 connect (mLvHD, SIGNAL (contextMenuRequested (QListViewItem*, const QPoint&, int)),
557 this, SLOT (onContextMenuRequested (QListViewItem*, const QPoint&, int)));
558
559 /* rest */
560
561 mSlotUniquizer = new HDSlotUniquizer (this);
562
563 qApp->installEventFilter (this);
564}
565
566void VBoxHardDiskSettings::getFromMachine (const CMachine &aMachine)
567{
568 mMachine = aMachine;
569
570 {
571 CSATAController ctl = mMachine.GetSATAController();
572 if (ctl.isNull())
573 {
574 /* hide the SATA check box if the SATA controller is not available
575 * (i.e. in VirtualBox OSE) */
576 mCbSATA->setHidden (true);
577 }
578 else
579 {
580 mCbSATA->setChecked (ctl.GetEnabled());
581 }
582 }
583
584 CHardDiskAttachmentEnumerator en =
585 mMachine.GetHardDiskAttachments().Enumerate();
586 while (en.HasMore())
587 {
588 CHardDiskAttachment hda = en.GetNext();
589 HDListItem *item = createItem (mSlotUniquizer, mMachine);
590 item->setAttachment (hda);
591 }
592 mLvHD->setSortColumn (0);
593 mLvHD->sort();
594 mLvHD->setSorting (-1);
595 mLvHD->setCurrentItem (mLvHD->firstChild());
596 onAfterCurrentChanged (0);
597}
598
599void VBoxHardDiskSettings::putBackToMachine()
600{
601 CSATAController ctl = mMachine.GetSATAController();
602 if (!ctl.isNull())
603 {
604 ctl.SetEnabled (mCbSATA->isChecked());
605 }
606
607 /* Detach all attached Hard Disks */
608 CHardDiskAttachmentEnumerator en =
609 mMachine.GetHardDiskAttachments().Enumerate();
610 while (en.HasMore())
611 {
612 CHardDiskAttachment hda = en.GetNext();
613 mMachine.DetachHardDisk (hda.GetBus(), hda.GetChannel(), hda.GetDevice());
614 if (!mMachine.isOk())
615 vboxProblem().cannotDetachHardDisk (this, mMachine,
616 hda.GetBus(), hda.GetChannel(), hda.GetDevice());
617 }
618
619 /* Sort&Attach all listed Hard Disks */
620 mLvHD->setSortColumn (0);
621 mLvHD->sort();
622 LONG maxSATAPort = 1;
623 HDListItem *item = mLvHD->firstChild() &&
624 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
625 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
626 while (item)
627 {
628 if (item->bus() == KStorageBus_SATA)
629 maxSATAPort = maxSATAPort < item->device() ?
630 item->device() : maxSATAPort;
631 mMachine.AttachHardDisk (item->getId(),
632 item->bus(), item->channel(), item->device());
633 if (!mMachine.isOk())
634 vboxProblem().cannotAttachHardDisk (this, mMachine, item->getId(),
635 item->bus(), item->channel(), item->device());
636 item = item->nextSibling();
637 }
638
639 if (!ctl.isNull())
640 {
641 mMachine.GetSATAController().SetPortCount (maxSATAPort);
642 }
643}
644
645QString VBoxHardDiskSettings::checkValidity()
646{
647 QString result;
648 QStringList slList;
649 QStringList idList;
650
651 /* Search for coincidences through all the media-id */
652 HDListItem *item = mLvHD->firstChild() &&
653 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
654 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
655 while (item)
656 {
657 QString id = item->getId().toString();
658 if (idList.contains (id))
659 {
660 result = tr ("<i>%1</i> uses the hard disk that is already "
661 "attached to <i>%2</i>")
662 .arg (item->text (0)).arg (slList [idList.findIndex (id)]);
663 break;
664 }
665 else
666 {
667 slList << item->text (0);
668 idList << id;
669 }
670 item = item->nextSibling();
671 }
672
673 return result;
674}
675
676void VBoxHardDiskSettings::addHDItem()
677{
678 HDListItem *item = createItem (mSlotUniquizer, mMachine);
679 item->moveFocusToColumn (0);
680 mLvHD->setCurrentItem (item);
681 if (!mLvHD->hasFocus())
682 mLvHD->setFocus();
683 /* Qt3 isn't emits currentChanged() signal if first list-view item added */
684 if (mLvHD->childCount() == 1)
685 onCurrentChanged (item);
686
687 emit hddListChanged();
688}
689
690void VBoxHardDiskSettings::delHDItem()
691{
692 if (mLvHD->currentItem())
693 {
694 QListViewItem *item = mLvHD->currentItem();
695 Assert (item == mPrevItem);
696 if (item == mPrevItem)
697 {
698 delete item;
699 mPrevItem = 0;
700 }
701 }
702
703 emit hddListChanged();
704}
705
706void VBoxHardDiskSettings::showVDM()
707{
708 HDListItem *item = mLvHD->currentItem() &&
709 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
710 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
711
712 VBoxDiskImageManagerDlg dlg (this, "VBoxDiskImageManagerDlg",
713 WType_Dialog | WShowModal);
714
715 QUuid machineId = mMachine.GetId();
716 QUuid hdId = item->getId();
717
718 dlg.setup (VBoxDefs::HD, true, &machineId, true /* aRefresh */,
719 mMachine, hdId, QUuid(), QUuid());
720
721 if (dlg.exec() == VBoxDiskImageManagerDlg::Accepted)
722 item->setId (dlg.getSelectedUuid());
723}
724
725void VBoxHardDiskSettings::moveFocus (QListViewItem *aItem, const QPoint&, int aCol)
726{
727 if (aItem && aItem->rtti() == HDListItem::HDListItemType)
728 static_cast<HDListItem*> (aItem)->moveFocusToColumn (aCol);
729}
730
731void VBoxHardDiskSettings::onCurrentChanged (QListViewItem *aItem)
732{
733 /* Postpone onCurrentChanged signal to be post-processed after all others */
734 QApplication::postEvent (this, new OnItemChangedEvent (aItem));
735}
736
737void VBoxHardDiskSettings::onToggleSATAController (bool aOn)
738{
739 if (!aOn)
740 {
741 HDListItem *firstItem = mLvHD->firstChild() &&
742 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
743 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
744
745 /* Search the list for the SATA ports in */
746 HDListItem *sataItem = firstItem;
747 while (sataItem)
748 {
749 if (sataItem->bus() == KStorageBus_SATA)
750 break;
751 sataItem = sataItem->nextSibling();
752 }
753
754 /* If list contains at least one SATA port */
755 if (sataItem)
756 {
757 int rc = vboxProblem().confirmDetachSATASlots (this);
758 if (rc != QIMessageBox::Ok)
759 {
760 /* Switch check-box back to "on" */
761 mCbSATA->blockSignals (true);
762 mCbSATA->setChecked (true);
763 mCbSATA->blockSignals (false);
764 return;
765 }
766 else
767 {
768 /* Delete SATA items */
769 HDListItem *it = firstItem;
770 mLvHD->blockSignals (true);
771 while (it)
772 {
773 HDListItem *curIt = it;
774 it = it->nextSibling();
775 if (curIt->bus() == KStorageBus_SATA)
776 {
777 if (curIt == mLvHD->currentItem())
778 mPrevItem = 0;
779 delete curIt;
780 }
781 }
782 mLvHD->blockSignals (false);
783 emit hddListChanged();
784 }
785 }
786 }
787
788 int newSATAPortsCount = aOn && !mMachine.isNull() ? SATAPortsCount : 0;
789 if (mSlotUniquizer->getSATAPortsCount() != newSATAPortsCount)
790 {
791 mSlotUniquizer->setSATAPortsCount (newSATAPortsCount);
792 onAfterCurrentChanged (mLvHD->currentItem());
793 }
794}
795
796void VBoxHardDiskSettings::onAfterCurrentChanged (QListViewItem *aItem)
797{
798 /* Process postponed onCurrentChanged event */
799 mAddAttachmentAct->setEnabled (mLvHD->childCount() < mSlotUniquizer->totalCount());
800 mRemoveAttachmentAct->setEnabled (aItem != NULL);
801 mSelectHardDiskAct->setEnabled (aItem != NULL);
802
803 if (aItem == mPrevItem)
804 return;
805
806 if (aItem && aItem->rtti() == HDListItem::HDListItemType &&
807 static_cast<HDListItem*> (aItem)->focusColumn() == -1)
808 {
809 int prevFocusColumn = 0;
810 if (mPrevItem && mPrevItem->rtti() == HDListItem::HDListItemType)
811 prevFocusColumn = static_cast<HDListItem*> (mPrevItem)->focusColumn();
812 static_cast<HDListItem*> (aItem)->moveFocusToColumn (prevFocusColumn);
813 }
814
815 if (mPrevItem && mPrevItem->rtti() == HDListItem::HDListItemType)
816 static_cast<HDListItem*> (mPrevItem)->moveFocusToColumn (-1);
817
818 mPrevItem = aItem;
819}
820
821void VBoxHardDiskSettings::onContextMenuRequested (QListViewItem * /*aItem*/,
822 const QPoint &aPoint, int)
823{
824 mContextMenu->exec (aPoint);
825}
826
827HDListItem* VBoxHardDiskSettings::createItem (HDSlotUniquizer *aUniq,
828 const CMachine &aMachine)
829{
830 return mLvHD->lastItem() ?
831 new HDListItem (this, mLvHD, mLvHD->lastItem(), aUniq, aMachine) :
832 new HDListItem (this, mLvHD, aUniq, aMachine);
833}
834
835bool VBoxHardDiskSettings::event (QEvent *aEvent)
836{
837 switch (aEvent->type())
838 {
839 /* Redirect postponed onCurrentChanged event */
840 case OnItemChangedEvent::Type:
841 {
842 OnItemChangedEvent *e = static_cast<OnItemChangedEvent*> (aEvent);
843 onAfterCurrentChanged (e->mItem);
844 break;
845 }
846 default:
847 break;
848 }
849
850 return QWidget::event (aEvent);
851}
852
853void VBoxHardDiskSettings::showEvent (QShowEvent *aEvent)
854{
855 QWidget::showEvent (aEvent);
856 adjustList();
857}
858
859bool VBoxHardDiskSettings::eventFilter (QObject *aObject, QEvent *aEvent)
860{
861 if (!aObject->isWidgetType())
862 return QWidget::eventFilter (aObject, aEvent);
863
864 if (static_cast<QWidget*> (aObject)->topLevelWidget() != topLevelWidget())
865 return QWidget::eventFilter (aObject, aEvent);
866
867 switch (aEvent->type())
868 {
869 /* Process double-click as "open combo-box" action */
870 case QEvent::MouseButtonDblClick:
871 {
872 if (aObject != mLvHD->viewport())
873 break;
874
875 HDListItem *item = mLvHD->currentItem() &&
876 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
877 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
878 if (item)
879 item->showEditor();
880 break;
881 }
882 case QEvent::KeyPress:
883 {
884 if (aObject != mLvHD)
885 break;
886
887 HDListItem *item = mLvHD->currentItem() &&
888 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
889 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
890
891 QKeyEvent *e = static_cast<QKeyEvent*> (aEvent);
892 /* Process cursor-left as "move focus left" action */
893 if (e->key() == Qt::Key_Left && !e->state())
894 {
895 if (item && item->focusColumn() != -1 &&
896 item->focusColumn() > 0)
897 item->moveFocusToColumn (item->focusColumn() - 1);
898 } else
899 /* Process cursor-right as "move focus right" action */
900 if (e->key() == Qt::Key_Right && !e->state())
901 {
902 if (item && item->focusColumn() != -1 &&
903 item->focusColumn() < mLvHD->columns() - 1)
904 item->moveFocusToColumn (item->focusColumn() + 1);
905 } else
906 /* Process F2/Space as "open combo-box" actions */
907 if (!e->state() &&
908 (e->key() == Qt::Key_F2 || e->key() == Qt::Key_Space))
909 {
910 if (item)
911 item->showEditor();
912 }
913 /* Process Ctrl/Alt+Up/Down as "open combo-box" actions */
914 if ((e->state() == Qt::AltButton || e->state() == Qt::ControlButton) &&
915 (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))
916 {
917 if (item)
918 {
919 item->showEditor();
920 return true;
921 }
922 }
923 break;
924 }
925 /* Process focus event to toggle the current selection state */
926 case QEvent::FocusIn:
927 {
928 if (aObject == mLvHD)
929 onAfterCurrentChanged (mLvHD->currentItem());
930 else if (!mGbHDList->queryList (0, 0, false, true)->contains (aObject))
931 onAfterCurrentChanged (0);
932 break;
933 }
934 default:
935 break;
936 }
937
938 return QWidget::eventFilter (aObject, aEvent);
939}
940
941void VBoxHardDiskSettings::adjustList()
942{
943 /* Search through the slots list for maximum element width */
944 int minLength = 0;
945 QFontMetrics fm = mLvHD->fontMetrics();
946 QValueList<HDSlot> list = mSlotUniquizer->list (0, false);
947 for (uint i = 0; i < list.size(); ++ i)
948 {
949 int length = fm.width (list [i].str);
950 minLength = minLength < length ? length : minLength;
951 }
952 minLength = minLength > mLvHD->viewport()->width() * 0.4 ?
953 (int) (mLvHD->viewport()->width() * 0.4) : minLength;
954
955 mLvHD->setColumnWidth (0, minLength + 10 /* little spacing */);
956 mLvHD->setColumnWidth (1, mLvHD->viewport()->width() - mLvHD->columnWidth (0));
957}
958
959#include "VBoxHardDiskSettings.ui.moc"
960
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