VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox4/include/VBoxUtils.h@ 7369

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

Compile VirtualBox with qt4 on linux.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/** @file
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * Declarations of utility classes and functions
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
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
19#ifndef __VBoxUtils_h__
20#define __VBoxUtils_h__
21
22#include <qobject.h>
23#include <qevent.h>
24#include <q3listview.h>
25#include <q3textedit.h>
26#include <qlabel.h>
27#include <qlayout.h>
28//Added by qt3to4:
29#include <QPixmap>
30#include <QResizeEvent>
31#include <QMouseEvent>
32#include <QKeyEvent>
33
34/**
35 * Simple ListView filter to disable unselecting all items by clicking in the
36 * unused area of the list (which is actually very annoying for the Single
37 * selection mode).
38 */
39class QIListViewSelectionPreserver : protected QObject
40{
41public:
42
43 QIListViewSelectionPreserver (QObject *parent, Q3ListView *alv)
44 : QObject (parent), lv (alv)
45 {
46 lv->viewport()->installEventFilter (this);
47 }
48
49protected:
50
51 bool eventFilter (QObject * /* o */, QEvent *e)
52 {
53 if (e->type() == QEvent::MouseButtonPress ||
54 e->type() == QEvent::MouseButtonRelease ||
55 e->type() == QEvent::MouseButtonDblClick)
56 {
57 QMouseEvent *me = (QMouseEvent *) e;
58 if (!lv->itemAt (me->pos()))
59 return true;
60 }
61
62 return false;
63 }
64
65private:
66
67 Q3ListView *lv;
68};
69
70/**
71 * Simple class that filters out presses and releases of the given key
72 * directed to a widget (the widget acts like if it would never handle
73 * this key).
74 */
75class QIKeyFilter : protected QObject
76{
77public:
78
79 QIKeyFilter (QObject *aParent, Qt::Key aKey) : QObject (aParent), mKey (aKey) {}
80
81 void watchOn (QObject *o) { o->installEventFilter (this); }
82
83protected:
84
85 bool eventFilter (QObject * /*o*/, QEvent *e)
86 {
87 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
88 {
89 QKeyEvent *ke = (QKeyEvent *) e;
90 if (ke->key() == mKey ||
91 (mKey == Qt::Key_Enter && ke->key() == Qt::Key_Return))
92 {
93 ke->ignore();
94 return false;
95 }
96 }
97
98 return false;
99 }
100
101 Qt::Key mKey;
102};
103
104/**
105 * Simple class that filters out all key presses and releases
106 * got while the Alt key is pressed. For some very strange reason,
107 * QLineEdit accepts those combinations that are not used as accelerators,
108 * and inserts the corresponding characters to the entry field.
109 */
110class QIAltKeyFilter : protected QObject
111{
112public:
113
114 QIAltKeyFilter (QObject *aParent) : QObject (aParent) {}
115
116 void watchOn (QObject *o) { o->installEventFilter (this); }
117
118protected:
119
120 bool eventFilter (QObject * /*o*/, QEvent *e)
121 {
122 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
123 {
124 QKeyEvent *ke = (QKeyEvent *) e;
125 if (ke->state() & Qt::AltButton)
126 return true;
127 }
128 return false;
129 }
130};
131
132/**
133 * Watches the given widget and makes sure the minimum widget size set by the layout
134 * manager does never get smaller than the previous minimum size set by the
135 * layout manager. This way, widgets with dynamic contents (i.e. text on some
136 * toggle buttons) will be able only to grow, never shrink, to avoid flicker
137 * during alternate contents updates (Pause -> Resume -> Pause -> ...).
138 *
139 * @todo not finished
140 */
141class QIConstraintKeeper : public QObject
142{
143 Q_OBJECT
144
145public:
146
147 QIConstraintKeeper (QWidget *aParent) : QObject (aParent)
148 {
149 aParent->setMinimumSize (aParent->size());
150 aParent->installEventFilter (this);
151 }
152
153private:
154
155 bool eventFilter (QObject *aObject, QEvent *aEvent)
156 {
157 if (aObject == parent() && aEvent->type() == QEvent::Resize)
158 {
159 QResizeEvent *ev = static_cast<QResizeEvent*> (aEvent);
160 QSize oldSize = ev->oldSize();
161 QSize newSize = ev->size();
162 int maxWidth = newSize.width() > oldSize.width() ?
163 newSize.width() : oldSize.width();
164 int maxHeight = newSize.height() > oldSize.height() ?
165 newSize.height() : oldSize.height();
166 if (maxWidth > oldSize.width() || maxHeight > oldSize.height())
167 ((QWidget*)parent())->setMinimumSize (maxWidth, maxHeight);
168 }
169 return QObject::eventFilter (aObject, aEvent);
170 }
171};
172
173
174/**
175 * Simple QTextEdit subclass to return its minimumSizeHint() as sizeHint()
176 * for getting more compact layout.
177 */
178class QITextEdit : public Q3TextEdit
179{
180 Q_OBJECT
181
182public:
183
184 QITextEdit (QWidget *aParent)
185 : Q3TextEdit (aParent) {}
186
187 QSize sizeHint() const
188 {
189 return minimumSizeHint();
190 }
191
192 QSize minimumSizeHint() const
193 {
194 return QSize (width(), heightForWidth (width()));
195 }
196};
197
198
199/**
200 * Simple QLabel subclass to re-query and return its sizeHint()
201 * before the widget to be shown for getting more compact layout.
202 */
203class QILabel : public QLabel
204{
205 Q_OBJECT
206
207public:
208
209 QILabel (QWidget *aParent, const char *aName)
210 : QLabel (aParent, aName), mShowed (false)
211 {
212 /* setup default size policy and alignment */
213 setSizePolicy (QSizePolicy ((QSizePolicy::SizeType)1,
214 (QSizePolicy::SizeType)0,
215 0, 0,
216 sizePolicy().hasHeightForWidth()));
217 setAlignment (int (Qt::AlignTop));
218 /* install show-parent-widget watcher */
219 aParent->topLevelWidget()->installEventFilter (this);
220 }
221
222 QSize sizeHint() const
223 {
224 return mShowed ?
225 QSize (width(), heightForWidth (width())) : QLabel::sizeHint();
226 }
227
228private:
229
230 bool eventFilter (QObject *aObject, QEvent *aEvent)
231 {
232 switch (aEvent->type())
233 {
234 case QEvent::Show:
235 {
236 mShowed = true;
237 if (parent() && ((QWidget*)parent())->layout())
238 ((QWidget*)parent())->layout()->activate();
239 break;
240 }
241 default:
242 break;
243 }
244 return QLabel::eventFilter (aObject, aEvent);
245 }
246
247 bool mShowed;
248};
249
250
251#ifdef Q_WS_MAC
252# undef PAGE_SIZE
253# undef PAGE_SHIFT
254# include <Carbon/Carbon.h>
255class QImage;
256class QPixmap;
257class VBoxFrameBuffer;
258CGImageRef DarwinQImageToCGImage (const QImage *aImage);
259CGImageRef DarwinQImageFromMimeSourceToCGImage (const char *aSource);
260CGImageRef DarwinQPixmapToCGImage (const QPixmap *aPixmap);
261CGImageRef DarwinQPixmapFromMimeSourceToCGImage (const char *aSource);
262CGImageRef DarwinCreateDockBadge (const char *aSource);
263void DarwinUpdateDockPreview (CGImageRef aVMImage, CGImageRef aOverlayImage, CGImageRef aStateImage = NULL);
264void DarwinUpdateDockPreview (VBoxFrameBuffer *aFrameBuffer, CGImageRef aOverlayImage);
265OSStatus DarwinRegionHandler (EventHandlerCallRef aInHandlerCallRef, EventRef aInEvent, void *aInUserData);
266#endif /* Q_WS_MAC */
267
268#endif // __VBoxUtils_h__
269
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette