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