VirtualBox

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

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

The Big Sun Rebranding Header Change

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