VirtualBox

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

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

FE/Qt: Hard Disk UI: Use QAction and a tool bar for actions to avoid inconsistent duplication in context menu.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.0 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 mCbSATA->setChecked (mMachine.GetSATAController().GetEnabled());
571
572 CHardDiskAttachmentEnumerator en =
573 mMachine.GetHardDiskAttachments().Enumerate();
574 while (en.HasMore())
575 {
576 CHardDiskAttachment hda = en.GetNext();
577 HDListItem *item = createItem (mSlotUniquizer, mMachine);
578 item->setAttachment (hda);
579 }
580 mLvHD->setSortColumn (0);
581 mLvHD->sort();
582 mLvHD->setSorting (-1);
583 mLvHD->setCurrentItem (mLvHD->firstChild());
584 onAfterCurrentChanged (0);
585}
586
587void VBoxHardDiskSettings::putBackToMachine()
588{
589 mMachine.GetSATAController().SetEnabled (mCbSATA->isChecked());
590
591 /* Detach all attached Hard Disks */
592 CHardDiskAttachmentEnumerator en =
593 mMachine.GetHardDiskAttachments().Enumerate();
594 while (en.HasMore())
595 {
596 CHardDiskAttachment hda = en.GetNext();
597 mMachine.DetachHardDisk (hda.GetBus(), hda.GetChannel(), hda.GetDevice());
598 if (!mMachine.isOk())
599 vboxProblem().cannotDetachHardDisk (this, mMachine,
600 hda.GetBus(), hda.GetChannel(), hda.GetDevice());
601 }
602
603 /* Sort&Attach all listed Hard Disks */
604 mLvHD->setSortColumn (0);
605 mLvHD->sort();
606 LONG maxSATAPort = 1;
607 HDListItem *item = mLvHD->firstChild() &&
608 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
609 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
610 while (item)
611 {
612 if (item->bus() == KStorageBus_SATA)
613 maxSATAPort = maxSATAPort < item->device() ?
614 item->device() : maxSATAPort;
615 mMachine.AttachHardDisk (item->getId(),
616 item->bus(), item->channel(), item->device());
617 if (!mMachine.isOk())
618 vboxProblem().cannotAttachHardDisk (this, mMachine, item->getId(),
619 item->bus(), item->channel(), item->device());
620 item = item->nextSibling();
621 }
622
623 mMachine.GetSATAController().SetPortCount (maxSATAPort);
624}
625
626QString VBoxHardDiskSettings::checkValidity()
627{
628 QString result;
629 QStringList slList;
630 QStringList idList;
631
632 /* Search for coincidences through all the media-id */
633 HDListItem *item = mLvHD->firstChild() &&
634 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
635 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
636 while (item)
637 {
638 QString id = item->getId().toString();
639 if (idList.contains (id))
640 {
641 result = tr ("<i>%1</i> uses the hard disk that is already "
642 "attached to <i>%2</i>")
643 .arg (item->text (0)).arg (slList [idList.findIndex (id)]);
644 break;
645 }
646 else
647 {
648 slList << item->text (0);
649 idList << id;
650 }
651 item = item->nextSibling();
652 }
653
654 return result;
655}
656
657void VBoxHardDiskSettings::addHDItem()
658{
659 HDListItem *item = createItem (mSlotUniquizer, mMachine);
660 item->moveFocusToColumn (0);
661 mLvHD->setCurrentItem (item);
662 if (!mLvHD->hasFocus())
663 mLvHD->setFocus();
664 /* Qt3 isn't emits currentChanged() signal if first list-view item added */
665 if (mLvHD->childCount() == 1)
666 onCurrentChanged (item);
667
668 emit hddListChanged();
669}
670
671void VBoxHardDiskSettings::delHDItem()
672{
673 if (mLvHD->currentItem())
674 {
675 QListViewItem *item = mLvHD->currentItem();
676 Assert (item == mPrevItem);
677 if (item == mPrevItem)
678 {
679 delete item;
680 mPrevItem = 0;
681 }
682 }
683
684 emit hddListChanged();
685}
686
687void VBoxHardDiskSettings::showVDM()
688{
689 HDListItem *item = mLvHD->currentItem() &&
690 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
691 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
692
693 VBoxDiskImageManagerDlg dlg (this, "VBoxDiskImageManagerDlg",
694 WType_Dialog | WShowModal);
695
696 QUuid machineId = mMachine.GetId();
697 QUuid hdId = item->getId();
698
699 dlg.setup (VBoxDefs::HD, true, &machineId, true /* aRefresh */,
700 mMachine, hdId, QUuid(), QUuid());
701
702 if (dlg.exec() == VBoxDiskImageManagerDlg::Accepted)
703 item->setId (dlg.getSelectedUuid());
704}
705
706void VBoxHardDiskSettings::moveFocus (QListViewItem *aItem, const QPoint&, int aCol)
707{
708 if (aItem && aItem->rtti() == HDListItem::HDListItemType)
709 static_cast<HDListItem*> (aItem)->moveFocusToColumn (aCol);
710}
711
712void VBoxHardDiskSettings::onCurrentChanged (QListViewItem *aItem)
713{
714 /* Postpone onCurrentChanged signal to be post-processed after all others */
715 QApplication::postEvent (this, new OnItemChangedEvent (aItem));
716}
717
718void VBoxHardDiskSettings::onToggleSATAController (bool aOn)
719{
720 if (!aOn)
721 {
722 HDListItem *firstItem = mLvHD->firstChild() &&
723 mLvHD->firstChild()->rtti() == HDListItem::HDListItemType ?
724 static_cast<HDListItem*> (mLvHD->firstChild()) : 0;
725
726 /* Search the list for the SATA ports in */
727 HDListItem *sataItem = firstItem;
728 while (sataItem)
729 {
730 if (sataItem->bus() == KStorageBus_SATA)
731 break;
732 sataItem = sataItem->nextSibling();
733 }
734
735 /* If list contains at least one SATA port */
736 if (sataItem)
737 {
738 int rc = vboxProblem().confirmDetachSATASlots (this);
739 if (rc != QIMessageBox::Ok)
740 {
741 /* Switch check-box back to "on" */
742 mCbSATA->blockSignals (true);
743 mCbSATA->setChecked (true);
744 mCbSATA->blockSignals (false);
745 return;
746 }
747 else
748 {
749 /* Delete SATA items */
750 HDListItem *it = firstItem;
751 mLvHD->blockSignals (true);
752 while (it)
753 {
754 HDListItem *curIt = it;
755 it = it->nextSibling();
756 if (curIt->bus() == KStorageBus_SATA)
757 {
758 if (curIt == mLvHD->currentItem())
759 mPrevItem = 0;
760 delete curIt;
761 }
762 }
763 mLvHD->blockSignals (false);
764 emit hddListChanged();
765 }
766 }
767 }
768
769 int newSATAPortsCount = aOn && !mMachine.isNull() ? SATAPortsCount : 0;
770 if (mSlotUniquizer->getSATAPortsCount() != newSATAPortsCount)
771 {
772 mSlotUniquizer->setSATAPortsCount (newSATAPortsCount);
773 onAfterCurrentChanged (mLvHD->currentItem());
774 }
775}
776
777void VBoxHardDiskSettings::onAfterCurrentChanged (QListViewItem *aItem)
778{
779 /* Process postponed onCurrentChanged event */
780 mAddAttachmentAct->setEnabled (mLvHD->childCount() < mSlotUniquizer->totalCount());
781 mRemoveAttachmentAct->setEnabled (aItem != NULL);
782 mSelectHardDiskAct->setEnabled (aItem != NULL);
783
784 if (aItem == mPrevItem)
785 return;
786
787 if (aItem && aItem->rtti() == HDListItem::HDListItemType &&
788 static_cast<HDListItem*> (aItem)->focusColumn() == -1)
789 {
790 int prevFocusColumn = 0;
791 if (mPrevItem && mPrevItem->rtti() == HDListItem::HDListItemType)
792 prevFocusColumn = static_cast<HDListItem*> (mPrevItem)->focusColumn();
793 static_cast<HDListItem*> (aItem)->moveFocusToColumn (prevFocusColumn);
794 }
795
796 if (mPrevItem && mPrevItem->rtti() == HDListItem::HDListItemType)
797 static_cast<HDListItem*> (mPrevItem)->moveFocusToColumn (-1);
798
799 mPrevItem = aItem;
800}
801
802void VBoxHardDiskSettings::onContextMenuRequested (QListViewItem *aItem,
803 const QPoint &aPoint, int)
804{
805 mContextMenu->exec (aPoint);
806}
807
808HDListItem* VBoxHardDiskSettings::createItem (HDSlotUniquizer *aUniq,
809 const CMachine &aMachine)
810{
811 return mLvHD->lastItem() ?
812 new HDListItem (this, mLvHD, mLvHD->lastItem(), aUniq, aMachine) :
813 new HDListItem (this, mLvHD, aUniq, aMachine);
814}
815
816bool VBoxHardDiskSettings::event (QEvent *aEvent)
817{
818 switch (aEvent->type())
819 {
820 /* Redirect postponed onCurrentChanged event */
821 case OnItemChangedEvent::Type:
822 {
823 OnItemChangedEvent *e = static_cast<OnItemChangedEvent*> (aEvent);
824 onAfterCurrentChanged (e->mItem);
825 break;
826 }
827 default:
828 break;
829 }
830
831 return QWidget::event (aEvent);
832}
833
834void VBoxHardDiskSettings::showEvent (QShowEvent *aEvent)
835{
836 QWidget::showEvent (aEvent);
837 adjustList();
838}
839
840bool VBoxHardDiskSettings::eventFilter (QObject *aObject, QEvent *aEvent)
841{
842 if (!aObject->isWidgetType())
843 return QWidget::eventFilter (aObject, aEvent);
844
845 if (static_cast<QWidget*> (aObject)->topLevelWidget() != topLevelWidget())
846 return QWidget::eventFilter (aObject, aEvent);
847
848 switch (aEvent->type())
849 {
850 /* Process double-click as "open combo-box" action */
851 case QEvent::MouseButtonDblClick:
852 {
853 if (aObject != mLvHD->viewport())
854 break;
855
856 HDListItem *item = mLvHD->currentItem() &&
857 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
858 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
859 if (item)
860 item->showEditor();
861 break;
862 }
863 case QEvent::KeyPress:
864 {
865 if (aObject != mLvHD)
866 break;
867
868 HDListItem *item = mLvHD->currentItem() &&
869 mLvHD->currentItem()->rtti() == HDListItem::HDListItemType ?
870 static_cast<HDListItem*> (mLvHD->currentItem()) : 0;
871
872 QKeyEvent *e = static_cast<QKeyEvent*> (aEvent);
873 /* Process cursor-left as "move focus left" action */
874 if (e->key() == Qt::Key_Left && !e->state())
875 {
876 if (item && item->focusColumn() != -1 &&
877 item->focusColumn() > 0)
878 item->moveFocusToColumn (item->focusColumn() - 1);
879 } else
880 /* Process cursor-right as "move focus right" action */
881 if (e->key() == Qt::Key_Right && !e->state())
882 {
883 if (item && item->focusColumn() != -1 &&
884 item->focusColumn() < mLvHD->columns() - 1)
885 item->moveFocusToColumn (item->focusColumn() + 1);
886 } else
887 /* Process F2/Space as "open combo-box" actions */
888 if (!e->state() &&
889 (e->key() == Qt::Key_F2 || e->key() == Qt::Key_Space))
890 {
891 if (item)
892 item->showEditor();
893 }
894 /* Process Ctrl/Alt+Up/Down as "open combo-box" actions */
895 if ((e->state() == Qt::AltButton || e->state() == Qt::ControlButton) &&
896 (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))
897 {
898 if (item)
899 {
900 item->showEditor();
901 return true;
902 }
903 }
904 break;
905 }
906 /* Process focus event to toggle the current selection state */
907 case QEvent::FocusIn:
908 {
909 if (aObject == mLvHD)
910 onAfterCurrentChanged (mLvHD->currentItem());
911 else if (!mGbHDList->queryList (0, 0, false, true)->contains (aObject))
912 onAfterCurrentChanged (0);
913 break;
914 }
915 default:
916 break;
917 }
918
919 return QWidget::eventFilter (aObject, aEvent);
920}
921
922void VBoxHardDiskSettings::adjustList()
923{
924 /* Search through the slots list for maximum element width */
925 int minLength = 0;
926 QFontMetrics fm = mLvHD->fontMetrics();
927 QValueList<HDSlot> list = mSlotUniquizer->list (0, false);
928 for (uint i = 0; i < list.size(); ++ i)
929 {
930 int length = fm.width (list [i].str);
931 minLength = minLength < length ? length : minLength;
932 }
933 minLength = minLength > mLvHD->viewport()->width() * 0.4 ?
934 (int) (mLvHD->viewport()->width() * 0.4) : minLength;
935
936 mLvHD->setColumnWidth (0, minLength + 10 /* little spacing */);
937 mLvHD->setColumnWidth (1, mLvHD->viewport()->width() - mLvHD->columnWidth (0));
938}
939
940#include "VBoxHardDiskSettings.ui.moc"
941
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