VirtualBox

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

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

FE/Qt: Naming/placing corrections.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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 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
31/**
32 * Simple ListView filter to disable unselecting all items by clicking in the
33 * unused area of the list (which is actually very annoying for the Single
34 * selection mode).
35 */
36class QIListViewSelectionPreserver : protected QObject
37{
38public:
39
40 QIListViewSelectionPreserver (QObject *parent, QListView *alv)
41 : QObject (parent), lv (alv)
42 {
43 lv->viewport()->installEventFilter (this);
44 }
45
46protected:
47
48 bool eventFilter (QObject * /* o */, QEvent *e)
49 {
50 if (e->type() == QEvent::MouseButtonPress ||
51 e->type() == QEvent::MouseButtonRelease ||
52 e->type() == QEvent::MouseButtonDblClick)
53 {
54 QMouseEvent *me = (QMouseEvent *) e;
55 if (!lv->itemAt (me->pos()))
56 return true;
57 }
58
59 return false;
60 }
61
62private:
63
64 QListView *lv;
65};
66
67/**
68 * Simple class that filters out presses and releases of the given key
69 * directed to a widget (the widget acts like if it would never handle
70 * this key).
71 */
72class QIKeyFilter : protected QObject
73{
74public:
75
76 QIKeyFilter (QObject *aParent, Key aKey) : QObject (aParent), mKey (aKey) {}
77
78 void watchOn (QObject *o) { o->installEventFilter (this); }
79
80protected:
81
82 bool eventFilter (QObject * /*o*/, QEvent *e)
83 {
84 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
85 {
86 QKeyEvent *ke = (QKeyEvent *) e;
87 if (ke->key() == mKey ||
88 (mKey == Qt::Key_Enter && ke->key() == Qt::Key_Return))
89 {
90 ke->ignore();
91 return false;
92 }
93 }
94
95 return false;
96 }
97
98 Key mKey;
99};
100
101/**
102 * Simple class that filters out all key presses and releases
103 * got while the Alt key is pressed. For some very strange reason,
104 * QLineEdit accepts those combinations that are not used as accelerators,
105 * and inserts the corresponding characters to the entry field.
106 */
107class QIAltKeyFilter : protected QObject
108{
109public:
110
111 QIAltKeyFilter (QObject *aParent) : QObject (aParent) {}
112
113 void watchOn (QObject *o) { o->installEventFilter (this); }
114
115protected:
116
117 bool eventFilter (QObject * /*o*/, QEvent *e)
118 {
119 if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
120 {
121 QKeyEvent *ke = (QKeyEvent *) e;
122 if (ke->state() & Qt::AltButton)
123 return true;
124 }
125 return false;
126 }
127};
128
129/**
130 * Watches the given widget and makes sure the minimum widget size set by the layout
131 * manager does never get smaller than the previous minimum size set by the
132 * layout manager. This way, widgets with dynamic contents (i.e. text on some
133 * toggle buttons) will be able only to grow, never shrink, to avoid flicker
134 * during alternate contents updates (Pause -> Resume -> Pause -> ...).
135 *
136 * @todo not finished
137 */
138class QIConstraintKeeper : public QObject
139{
140 Q_OBJECT
141
142public:
143
144 QIConstraintKeeper (QWidget *aParent) : QObject (aParent)
145 {
146 aParent->setMinimumSize (aParent->size());
147 aParent->installEventFilter (this);
148 }
149
150private:
151
152 bool eventFilter (QObject *aObject, QEvent *aEvent)
153 {
154 if (aObject == parent() && aEvent->type() == QEvent::Resize)
155 {
156 QResizeEvent *ev = static_cast<QResizeEvent*> (aEvent);
157 QSize oldSize = ev->oldSize();
158 QSize newSize = ev->size();
159 int maxWidth = newSize.width() > oldSize.width() ?
160 newSize.width() : oldSize.width();
161 int maxHeight = newSize.height() > oldSize.height() ?
162 newSize.height() : oldSize.height();
163 if (maxWidth > oldSize.width() || maxHeight > oldSize.height())
164 ((QWidget*)parent())->setMinimumSize (maxWidth, maxHeight);
165 }
166 return QObject::eventFilter (aObject, aEvent);
167 }
168};
169
170
171/**
172 * Simple QTextEdit subclass to return its minimumSizeHint() as sizeHint()
173 * for getting more compact layout.
174 */
175class QITextEdit : public QTextEdit
176{
177public:
178
179 QITextEdit (QWidget *aParent)
180 : QTextEdit (aParent) {}
181
182 QSize sizeHint() const
183 {
184 return minimumSizeHint();
185 }
186
187 QSize minimumSizeHint() const
188 {
189 /// @todo (r=dmik) this looks a bit meanignless. any comment?
190 int w = 0;
191 int h = heightForWidth (w);
192 return QSize (w, h);
193 }
194};
195
196#endif // __VBoxUtils_h__
197
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