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 |
|
---|
30 | /**
|
---|
31 | * Simple ListView filter to disable unselecting all items by clicking in the
|
---|
32 | * unused area of the list (which is actually very annoying for the Single
|
---|
33 | * selection mode).
|
---|
34 | */
|
---|
35 | class QIListViewSelectionPreserver : protected QObject
|
---|
36 | {
|
---|
37 | public:
|
---|
38 |
|
---|
39 | QIListViewSelectionPreserver (QObject *parent, QListView *alv)
|
---|
40 | : QObject (parent), lv (alv)
|
---|
41 | {
|
---|
42 | lv->viewport()->installEventFilter (this);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected:
|
---|
46 |
|
---|
47 | bool eventFilter (QObject * /* o */, QEvent *e)
|
---|
48 | {
|
---|
49 | if (e->type() == QEvent::MouseButtonPress ||
|
---|
50 | e->type() == QEvent::MouseButtonRelease ||
|
---|
51 | e->type() == QEvent::MouseButtonDblClick)
|
---|
52 | {
|
---|
53 | QMouseEvent *me = (QMouseEvent *) e;
|
---|
54 | if (!lv->itemAt (me->pos()))
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private:
|
---|
62 |
|
---|
63 | QListView *lv;
|
---|
64 | };
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Simple class that filters out presses and releases of the given key
|
---|
68 | * directed to a widget (the widget acts like if it would never handle
|
---|
69 | * this key).
|
---|
70 | */
|
---|
71 | class QIKeyFilter : protected QObject
|
---|
72 | {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | QIKeyFilter (QObject *aParent, Key aKey) : QObject (aParent), mKey (aKey) {}
|
---|
76 |
|
---|
77 | void watchOn (QObject *o) { o->installEventFilter (this); }
|
---|
78 |
|
---|
79 | protected:
|
---|
80 |
|
---|
81 | bool eventFilter (QObject * /*o*/, QEvent *e)
|
---|
82 | {
|
---|
83 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
|
---|
84 | {
|
---|
85 | QKeyEvent *ke = (QKeyEvent *) e;
|
---|
86 | if (ke->key() == mKey ||
|
---|
87 | (mKey == Qt::Key_Enter && ke->key() == Qt::Key_Return))
|
---|
88 | {
|
---|
89 | ke->ignore();
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | Key mKey;
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Simple class that filters out all key presses and releases
|
---|
102 | * got while the Alt key is pressed. For some very strange reason,
|
---|
103 | * QLineEdit accepts those combinations that are not used as accelerators,
|
---|
104 | * and inserts the corresponding characters to the entry field.
|
---|
105 | */
|
---|
106 | class QIAltKeyFilter : protected QObject
|
---|
107 | {
|
---|
108 | public:
|
---|
109 |
|
---|
110 | QIAltKeyFilter (QObject *aParent) : QObject (aParent) {}
|
---|
111 |
|
---|
112 | void watchOn (QObject *o) { o->installEventFilter (this); }
|
---|
113 |
|
---|
114 | protected:
|
---|
115 |
|
---|
116 | bool eventFilter (QObject * /*o*/, QEvent *e)
|
---|
117 | {
|
---|
118 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease)
|
---|
119 | {
|
---|
120 | QKeyEvent *ke = (QKeyEvent *) e;
|
---|
121 | if (ke->state() & Qt::AltButton)
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | };
|
---|
127 |
|
---|
128 | #endif // __VBoxUtils_h__
|
---|
129 |
|
---|