1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VBoxVMItem, VBoxVMModel, VBoxVMListView, VBoxVMItemPainter class declarations
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-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 | #ifndef __VBoxVMListView_h__
|
---|
24 | #define __VBoxVMListView_h__
|
---|
25 |
|
---|
26 | #include "VBoxGlobal.h"
|
---|
27 | #include "QIListView.h"
|
---|
28 |
|
---|
29 | /* Qt includes */
|
---|
30 | #include <QAbstractListModel>
|
---|
31 | #include <QDateTime>
|
---|
32 |
|
---|
33 | class VBoxSelectorWnd;
|
---|
34 |
|
---|
35 | class VBoxVMItem : public QObject
|
---|
36 | {
|
---|
37 | Q_OBJECT;
|
---|
38 |
|
---|
39 | public:
|
---|
40 |
|
---|
41 | enum Action
|
---|
42 | {
|
---|
43 | Config = 0,
|
---|
44 | Delete,
|
---|
45 | Start,
|
---|
46 | Discard,
|
---|
47 | Pause,
|
---|
48 | Refresh,
|
---|
49 | ShowLogs
|
---|
50 | };
|
---|
51 |
|
---|
52 | public:
|
---|
53 |
|
---|
54 | VBoxVMItem (const CMachine &aMachine, VBoxSelectorWnd *pParent);
|
---|
55 | virtual ~VBoxVMItem();
|
---|
56 |
|
---|
57 | CMachine machine() const { return mMachine; }
|
---|
58 |
|
---|
59 | QString name() const { return mName; }
|
---|
60 | QIcon osIcon() const { return mAccessible ? vboxGlobal().vmGuestOSTypeIcon (mOSTypeId) :QPixmap (":/os_unknown.png"); }
|
---|
61 | QUuid id() const { return mId; }
|
---|
62 |
|
---|
63 | QString sessionStateName() const;
|
---|
64 | QIcon sessionStateIcon() const { return mAccessible ? vboxGlobal().toIcon (mState) : QPixmap (":/state_aborted_16px.png"); }
|
---|
65 |
|
---|
66 | QString snapshotName() const { return mSnapshotName; }
|
---|
67 | ULONG snapshotCount() const { return mSnapshotCount; }
|
---|
68 |
|
---|
69 | QString toolTipText() const;
|
---|
70 |
|
---|
71 | bool accessible() const { return mAccessible; }
|
---|
72 | bool running() const { return (sessionState() != KSessionState_Closed); }
|
---|
73 | const CVirtualBoxErrorInfo &accessError() const { return mAccessError; }
|
---|
74 | KMachineState state() const { return mState; }
|
---|
75 | KSessionState sessionState() const { return mSessionState; }
|
---|
76 |
|
---|
77 | bool recache();
|
---|
78 |
|
---|
79 | bool canSwitchTo() const;
|
---|
80 | bool switchTo();
|
---|
81 |
|
---|
82 | private:
|
---|
83 |
|
---|
84 | /* Private member vars */
|
---|
85 | VBoxSelectorWnd *mParent;
|
---|
86 | CMachine mMachine;
|
---|
87 |
|
---|
88 | /* Cached machine data (to minimize server requests) */
|
---|
89 | QUuid mId;
|
---|
90 | QString mSettingsFile;
|
---|
91 |
|
---|
92 | bool mAccessible;
|
---|
93 | CVirtualBoxErrorInfo mAccessError;
|
---|
94 |
|
---|
95 | QString mName;
|
---|
96 | QString mSnapshotName;
|
---|
97 | KMachineState mState;
|
---|
98 | QDateTime mLastStateChange;
|
---|
99 | KSessionState mSessionState;
|
---|
100 | QString mOSTypeId;
|
---|
101 | ULONG mSnapshotCount;
|
---|
102 |
|
---|
103 | ULONG mPid;
|
---|
104 | };
|
---|
105 |
|
---|
106 | /* Make the pointer of this class public to the QVariant framework */
|
---|
107 | Q_DECLARE_METATYPE(VBoxVMItem *);
|
---|
108 |
|
---|
109 | class VBoxVMModel: public QAbstractListModel
|
---|
110 | {
|
---|
111 | Q_OBJECT;
|
---|
112 |
|
---|
113 | public:
|
---|
114 | enum { SnapShotDisplayRole = Qt::UserRole,
|
---|
115 | SnapShotFontRole,
|
---|
116 | SessionStateDisplayRole,
|
---|
117 | SessionStateDecorationRole,
|
---|
118 | SessionStateFontRole,
|
---|
119 | VBoxVMItemPtrRole };
|
---|
120 |
|
---|
121 | VBoxVMModel(QObject *aParent = 0)
|
---|
122 | :QAbstractListModel (aParent) {}
|
---|
123 |
|
---|
124 | void addItem (VBoxVMItem *aItem);
|
---|
125 | void removeItem (VBoxVMItem *aItem);
|
---|
126 | void refreshItem (VBoxVMItem *aItem);
|
---|
127 |
|
---|
128 | void itemChanged (VBoxVMItem *aItem);
|
---|
129 |
|
---|
130 | void clear();
|
---|
131 |
|
---|
132 | VBoxVMItem *itemById (const QUuid &aId) const;
|
---|
133 | VBoxVMItem *itemByRow (int aRow) const;
|
---|
134 | QModelIndex indexById (const QUuid &aId) const;
|
---|
135 |
|
---|
136 | int rowById (const QUuid &aId) const;;
|
---|
137 |
|
---|
138 | void sort (Qt::SortOrder aOrder = Qt::AscendingOrder) { sort (0, aOrder); }
|
---|
139 |
|
---|
140 | /* The following are necessary model implementations */
|
---|
141 | void sort (int aColumn, Qt::SortOrder aOrder = Qt::AscendingOrder);
|
---|
142 |
|
---|
143 | int rowCount (const QModelIndex &aParent = QModelIndex()) const;
|
---|
144 |
|
---|
145 | QVariant data (const QModelIndex &aIndex, int aRole) const;
|
---|
146 | QVariant headerData (int aSection, Qt::Orientation aOrientation,
|
---|
147 | int aRole = Qt::DisplayRole) const;
|
---|
148 |
|
---|
149 | bool removeRows (int aRow, int aCount, const QModelIndex &aParent = QModelIndex());
|
---|
150 |
|
---|
151 | private:
|
---|
152 | static bool VBoxVMItemNameCompareLessThan (VBoxVMItem* aItem1, VBoxVMItem* aItem2);
|
---|
153 | static bool VBoxVMItemNameCompareGreaterThan (VBoxVMItem* aItem1, VBoxVMItem* aItem2);
|
---|
154 |
|
---|
155 | /* Private member vars */
|
---|
156 | QList<VBoxVMItem *> mVMItemList;
|
---|
157 | };
|
---|
158 |
|
---|
159 | class VBoxVMListView: public QIListView
|
---|
160 | {
|
---|
161 | Q_OBJECT;
|
---|
162 |
|
---|
163 | public:
|
---|
164 | VBoxVMListView (QWidget *aParent = 0);
|
---|
165 |
|
---|
166 | void selectItemByRow (int row);
|
---|
167 | void selectItemById (const QUuid &aID);
|
---|
168 | void ensureSomeRowSelected (int aRowHint);
|
---|
169 | VBoxVMItem * selectedItem() const;
|
---|
170 |
|
---|
171 | void ensureCurrentVisible();
|
---|
172 |
|
---|
173 | signals:
|
---|
174 | void currentChanged();
|
---|
175 | void activated();
|
---|
176 |
|
---|
177 | protected slots:
|
---|
178 | void selectionChanged (const QItemSelection &aSelected, const QItemSelection &aDeselected);
|
---|
179 | void currentChanged (const QModelIndex &aCurrent, const QModelIndex &aPrevious);
|
---|
180 | void dataChanged (const QModelIndex &aTopLeft, const QModelIndex &aBottomRight);
|
---|
181 |
|
---|
182 | protected:
|
---|
183 | bool selectCurrent();
|
---|
184 | };
|
---|
185 |
|
---|
186 | class VBoxVMItemPainter: public QIItemDelegate
|
---|
187 | {
|
---|
188 | public:
|
---|
189 | VBoxVMItemPainter (QObject *aParent = 0)
|
---|
190 | : QIItemDelegate (aParent), mMargin (8), mSpacing (mMargin * 3 / 2) {}
|
---|
191 |
|
---|
192 | QSize sizeHint (const QStyleOptionViewItem &aOption,
|
---|
193 | const QModelIndex &aIndex) const;
|
---|
194 |
|
---|
195 | void paint (QPainter *aPainter, const QStyleOptionViewItem &aOption,
|
---|
196 | const QModelIndex &aIndex) const;
|
---|
197 |
|
---|
198 | private:
|
---|
199 | inline QFontMetrics fontMetric (const QModelIndex &aIndex, int aRole) const { return QFontMetrics (aIndex.data (aRole).value<QFont>()); }
|
---|
200 | inline QIcon::Mode iconMode (QStyle::State aState) const
|
---|
201 | {
|
---|
202 | if (!(aState & QStyle::State_Enabled))
|
---|
203 | return QIcon::Disabled;
|
---|
204 | if (aState & QStyle::State_Selected)
|
---|
205 | return QIcon::Selected;
|
---|
206 | return QIcon::Normal;
|
---|
207 | }
|
---|
208 | inline QIcon::State iconState (QStyle::State aState) const { return aState & QStyle::State_Open ? QIcon::On : QIcon::Off; }
|
---|
209 |
|
---|
210 | QRect rect (const QStyleOptionViewItem &aOption,
|
---|
211 | const QModelIndex &aIndex, int aRole) const;
|
---|
212 |
|
---|
213 | void calcLayout (const QModelIndex &aIndex,
|
---|
214 | QRect *aOSType, QRect *aVMName, QRect *aShot,
|
---|
215 | QRect *aStateIcon, QRect *aState) const;
|
---|
216 |
|
---|
217 | /* Private member vars */
|
---|
218 | int mMargin;
|
---|
219 | int mSpacing;
|
---|
220 | };
|
---|
221 |
|
---|
222 | #endif /* __VBoxVMListView_h__ */
|
---|