1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * Declarations of utility classes and functions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
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 <iprt/types.h>
|
---|
23 |
|
---|
24 | /* Qt includes */
|
---|
25 | #include <QMouseEvent>
|
---|
26 | #include <QWidget>
|
---|
27 | #include <QTextBrowser>
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Simple class that filters out all key presses and releases
|
---|
31 | * got while the Alt key is pressed. For some very strange reason,
|
---|
32 | * QLineEdit accepts those combinations that are not used as accelerators,
|
---|
33 | * and inserts the corresponding characters to the entry field.
|
---|
34 | */
|
---|
35 | class QIAltKeyFilter : protected QObject
|
---|
36 | {
|
---|
37 | Q_OBJECT;
|
---|
38 |
|
---|
39 | public:
|
---|
40 |
|
---|
41 | QIAltKeyFilter (QObject *aParent) :QObject (aParent) {}
|
---|
42 |
|
---|
43 | void watchOn (QObject *aObject) { aObject->installEventFilter (this); }
|
---|
44 |
|
---|
45 | protected:
|
---|
46 |
|
---|
47 | bool eventFilter (QObject * /* aObject */, QEvent *aEvent)
|
---|
48 | {
|
---|
49 | if (aEvent->type() == QEvent::KeyPress || aEvent->type() == QEvent::KeyRelease)
|
---|
50 | {
|
---|
51 | QKeyEvent *pEvent = static_cast<QKeyEvent *> (aEvent);
|
---|
52 | if (pEvent->modifiers() & Qt::AltModifier)
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | };
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Simple class which simulates focus-proxy rule redirecting widget
|
---|
61 | * assigned shortcut to desired widget.
|
---|
62 | */
|
---|
63 | class QIFocusProxy : protected QObject
|
---|
64 | {
|
---|
65 | Q_OBJECT;
|
---|
66 |
|
---|
67 | public:
|
---|
68 |
|
---|
69 | QIFocusProxy (QWidget *aFrom, QWidget *aTo)
|
---|
70 | : QObject (aFrom), mFrom (aFrom), mTo (aTo)
|
---|
71 | {
|
---|
72 | mFrom->installEventFilter (this);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected:
|
---|
76 |
|
---|
77 | bool eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
78 | {
|
---|
79 | if (aObject == mFrom && aEvent->type() == QEvent::Shortcut)
|
---|
80 | {
|
---|
81 | mTo->setFocus();
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | return QObject::eventFilter (aObject, aEvent);
|
---|
85 | }
|
---|
86 |
|
---|
87 | QWidget *mFrom;
|
---|
88 | QWidget *mTo;
|
---|
89 | };
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * QTextEdit reimplementation to feat some extended requirements.
|
---|
93 | */
|
---|
94 | class QRichTextEdit : public QTextEdit
|
---|
95 | {
|
---|
96 | Q_OBJECT;
|
---|
97 |
|
---|
98 | public:
|
---|
99 |
|
---|
100 | QRichTextEdit (QWidget *aParent) : QTextEdit (aParent) {}
|
---|
101 |
|
---|
102 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
103 | {
|
---|
104 | QTextEdit::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
105 | }
|
---|
106 | };
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * QTextBrowser reimplementation to feat some extended requirements.
|
---|
110 | */
|
---|
111 | class QRichTextBrowser : public QTextBrowser
|
---|
112 | {
|
---|
113 | Q_OBJECT;
|
---|
114 |
|
---|
115 | public:
|
---|
116 |
|
---|
117 | QRichTextBrowser (QWidget *aParent) : QTextBrowser (aParent) {}
|
---|
118 |
|
---|
119 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
120 | {
|
---|
121 | QTextBrowser::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
122 | }
|
---|
123 | };
|
---|
124 |
|
---|
125 | class UIProxyManager
|
---|
126 | {
|
---|
127 | public:
|
---|
128 |
|
---|
129 | UIProxyManager(const QString &strProxySettings = QString())
|
---|
130 | {
|
---|
131 | /* Parse settings: */
|
---|
132 | if (!strProxySettings.isEmpty())
|
---|
133 | {
|
---|
134 | QStringList proxySettings = strProxySettings.split(",");
|
---|
135 | if (proxySettings.size() > 0)
|
---|
136 | m_fProxyEnabled = proxySettings[0] == "proxyEnabled";
|
---|
137 | if (proxySettings.size() > 1)
|
---|
138 | m_strProxyHost = proxySettings[1];
|
---|
139 | if (proxySettings.size() > 2)
|
---|
140 | m_strProxyPort = proxySettings[2];
|
---|
141 | if (proxySettings.size() > 3)
|
---|
142 | m_fAuthEnabled = proxySettings[3] == "authEnabled";
|
---|
143 | if (proxySettings.size() > 4)
|
---|
144 | m_strAuthLogin = proxySettings[4];
|
---|
145 | if (proxySettings.size() > 5)
|
---|
146 | m_strAuthPassword = proxySettings[5];
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | QString toString() const
|
---|
151 | {
|
---|
152 | /* Serialize settings: */
|
---|
153 | QString strResult;
|
---|
154 | if (m_fProxyEnabled || !m_strProxyHost.isEmpty() || !m_strProxyPort.isEmpty() ||
|
---|
155 | m_fAuthEnabled || !m_strAuthLogin.isEmpty() || !m_strAuthPassword.isEmpty())
|
---|
156 | {
|
---|
157 | QStringList proxySettings;
|
---|
158 | proxySettings << QString(m_fProxyEnabled ? "proxyEnabled" : "proxyDisabled");
|
---|
159 | proxySettings << m_strProxyHost;
|
---|
160 | proxySettings << m_strProxyPort;
|
---|
161 | proxySettings << QString(m_fAuthEnabled ? "authEnabled" : "authDisabled");
|
---|
162 | proxySettings << m_strAuthLogin;
|
---|
163 | proxySettings << m_strAuthPassword;
|
---|
164 | strResult = proxySettings.join(",");
|
---|
165 | }
|
---|
166 | return strResult;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* Proxy attribute getters: */
|
---|
170 | bool proxyEnabled() const { return m_fProxyEnabled; }
|
---|
171 | const QString& proxyHost() const { return m_strProxyHost; }
|
---|
172 | const QString& proxyPort() const { return m_strProxyPort; }
|
---|
173 | bool authEnabled() const { return m_fAuthEnabled; }
|
---|
174 | const QString& authLogin() const { return m_strAuthLogin; }
|
---|
175 | const QString& authPassword() const { return m_strAuthPassword; }
|
---|
176 |
|
---|
177 | /* Proxy attribute setters: */
|
---|
178 | void setProxyEnabled(bool fProxyEnabled) { m_fProxyEnabled = fProxyEnabled; }
|
---|
179 | void setProxyHost(const QString &strProxyHost) { m_strProxyHost = strProxyHost; }
|
---|
180 | void setProxyPort(const QString &strProxyPort) { m_strProxyPort = strProxyPort; }
|
---|
181 | void setAuthEnabled(bool fAuthEnabled) { m_fAuthEnabled = fAuthEnabled; }
|
---|
182 | void setAuthLogin(const QString &strAuthLogin) { m_strAuthLogin = strAuthLogin; }
|
---|
183 | void setAuthPassword(const QString &strAuthPassword) { m_strAuthPassword = strAuthPassword; }
|
---|
184 |
|
---|
185 | private:
|
---|
186 |
|
---|
187 | /* Proxy attribute variables: */
|
---|
188 | bool m_fProxyEnabled;
|
---|
189 | QString m_strProxyHost;
|
---|
190 | QString m_strProxyPort;
|
---|
191 | bool m_fAuthEnabled;
|
---|
192 | QString m_strAuthLogin;
|
---|
193 | QString m_strAuthPassword;
|
---|
194 | };
|
---|
195 |
|
---|
196 | #ifdef Q_WS_MAC
|
---|
197 | # include "VBoxUtils-darwin.h"
|
---|
198 | #endif /* Q_WS_MAC */
|
---|
199 |
|
---|
200 | #endif // !___VBoxUtils_h___
|
---|
201 |
|
---|