VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/include/VBoxUtils.h@ 2586

Last change on this file since 2586 was 2578, checked in by vboxsync, 18 years ago

1764: Better default size in "Create VDI" wizard when called from "Create VM" wizard:

Own QLabel implementation to adjust QLabel::sizeHint automatically just before the label to be shown to avoid setting up the minimum width for the label. This label is used for both New VM & New HD Wizards.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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 InnoTek Systemberatung 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 as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23#ifndef __VBoxUtils_h__
24#define __VBoxUtils_h__
25
26#include <qobject.h>
27#include <qevent.h>
28#include <qlistview.h>
29#include <qtextedit.h>
30#include <qlabel.h>
31
32/**
33 * Simple ListView filter to disable unselecting all items by clicking in the
34 * unused area of the list (which is actually very annoying for the Single
35 * selection mode).
36 */
37class QIListViewSelectionPreserver : protected QObject
38{
39public:
40
41 QIListViewSelectionPreserver (QObject *parent, QListView *alv)
42 : QObject (parent), lv (alv)
43 {
44 lv->viewport()->installEventFilter (this);
45 }
46
47protected:
48
49 bool eventFilter (QObject * /* o */, QEvent *e)
50 {
51 if (e->type() == QEvent::MouseButtonPress ||
52 e->type() == QEvent::MouseButtonRelease ||
53 e->type() == QEvent::MouseButtonDblClick)
54 {
55 QMouseEvent *me = (QMouseEvent *) e;
56 if (!lv->itemAt (me->pos()))
57 return true;
58 }
59
60 return false;
61 }
62
63private:
64
65 QListView *lv;
66};
67
68/**
69 * Simple class that filters out presses and releases of the given key
70 * directed to a widget (the widget acts like if it would never handle
71 * this key).
72 */
73class QIKeyFilter : protected QObject
74{
75public:
76
77 QIKeyFilter (QObject *aParent, Key aKey) : QObject (aParent), mKey (aKey) {}
78
79 void watchOn (QObject *o) { o->installEventFilter (this); }
80
81protected:
82
83 bool eventFilter (QObject * /*o*/, QEvent *e)
84 {
85 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
86 {
87 QKeyEvent *ke = (QKeyEvent *) e;
88 if (ke->key() == mKey ||
89 (mKey == Qt::Key_Enter && ke->key() == Qt::Key_Return))
90 {
91 ke->ignore();
92 return false;
93 }
94 }
95
96 return false;
97 }
98
99 Key mKey;
100};
101
102/**
103 * Simple class that filters out all key presses and releases
104 * got while the Alt key is pressed. For some very strange reason,
105 * QLineEdit accepts those combinations that are not used as accelerators,
106 * and inserts the corresponding characters to the entry field.
107 */
108class QIAltKeyFilter : protected QObject
109{
110public:
111
112 QIAltKeyFilter (QObject *aParent) : QObject (aParent) {}
113
114 void watchOn (QObject *o) { o->installEventFilter (this); }
115
116protected:
117
118 bool eventFilter (QObject * /*o*/, QEvent *e)
119 {
120 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
121 {
122 QKeyEvent *ke = (QKeyEvent *) e;
123 if (ke->state() & Qt::AltButton)
124 return true;
125 }
126 return false;
127 }
128};
129
130/**
131 * Watches the given widget and makes sure the minimum widget size set by the layout
132 * manager does never get smaller than the previous minimum size set by the
133 * layout manager. This way, widgets with dynamic contents (i.e. text on some
134 * toggle buttons) will be able only to grow, never shrink, to avoid flicker
135 * during alternate contents updates (Pause -> Resume -> Pause -> ...).
136 *
137 * @todo not finished
138 */
139class QIConstraintKeeper : public QObject
140{
141 Q_OBJECT
142
143public:
144
145 QIConstraintKeeper (QWidget *aParent) : QObject (aParent)
146 {
147 aParent->setMinimumSize (aParent->size());
148 aParent->installEventFilter (this);
149 }
150
151private:
152
153 bool eventFilter (QObject *aObject, QEvent *aEvent)
154 {
155 if (aObject == parent() && aEvent->type() == QEvent::Resize)
156 {
157 QResizeEvent *ev = static_cast<QResizeEvent*> (aEvent);
158 QSize oldSize = ev->oldSize();
159 QSize newSize = ev->size();
160 int maxWidth = newSize.width() > oldSize.width() ?
161 newSize.width() : oldSize.width();
162 int maxHeight = newSize.height() > oldSize.height() ?
163 newSize.height() : oldSize.height();
164 if (maxWidth > oldSize.width() || maxHeight > oldSize.height())
165 ((QWidget*)parent())->setMinimumSize (maxWidth, maxHeight);
166 }
167 return QObject::eventFilter (aObject, aEvent);
168 }
169};
170
171
172/**
173 * Simple QTextEdit subclass to return its minimumSizeHint() as sizeHint()
174 * for getting more compact layout.
175 */
176class QITextEdit : public QTextEdit
177{
178 Q_OBJECT
179
180public:
181
182 QITextEdit (QWidget *aParent)
183 : QTextEdit (aParent) {}
184
185 QSize sizeHint() const
186 {
187 return minimumSizeHint();
188 }
189
190 QSize minimumSizeHint() const
191 {
192 return QSize (width(), heightForWidth (width()));
193 }
194};
195
196
197/**
198 * Simple QLabel subclass to re-query and return its sizeHint()
199 * before the widget to be shown for getting more compact layout.
200 */
201class QILabel : public QLabel
202{
203 Q_OBJECT
204
205public:
206
207 QILabel (QWidget *aParent, const char *aName)
208 : QLabel (aParent, aName), mSizeHint (-1, -1)
209 {
210 /* setup default size policy and alignment */
211 setSizePolicy (QSizePolicy ((QSizePolicy::SizeType)1,
212 (QSizePolicy::SizeType)0,
213 0, 0,
214 sizePolicy().hasHeightForWidth()));
215 setAlignment (int (QLabel::WordBreak | QLabel::AlignTop));
216 /* install show-parent-widget watcher */
217 aParent->installEventFilter (this);
218 }
219
220 QSize sizeHint() const
221 {
222 /* use cashed sizeHint if mSizeHint is valid */
223 if (!mSizeHint.isValid())
224 mSizeHint = QLabel::sizeHint();
225 return mSizeHint;
226 }
227
228private:
229
230 bool eventFilter (QObject *aObject, QEvent *aEvent)
231 {
232 switch (aEvent->type())
233 {
234 case QEvent::Show:
235 {
236 /* watch for parent's show-event to recalculate sizeHint */
237 mSizeHint = QSize (width(), heightForWidth (width()));
238 updateGeometry();
239 break;
240 }
241 default:
242 break;
243 }
244 return QLabel::eventFilter (aObject, aEvent);
245 }
246
247 mutable QSize mSizeHint;
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;
257CGImageRef DarwinQImageToCGImage (const QImage *aImage);
258CGImageRef DarwinQPixmapToCGImage (const QPixmap *aPixmap);
259CGImageRef DarwinQPixmapFromMimeSourceToCGImage (const char *aSource);
260CGImageRef DarwinCreateDockBadge (const char *aSource);
261#endif /* Q_WS_MAC */
262
263#endif // __VBoxUtils_h__
264
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