|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qdesigneraxwidget.h" |
|
43 |
|
44 #include <QtCore/QMetaProperty> |
|
45 #include <QtCore/QDebug> |
|
46 #include <QtGui/QIcon> |
|
47 #include <QtGui/QPainter> |
|
48 #include <QtGui/QResizeEvent> |
|
49 |
|
50 #include <ActiveQt/QAxWidget> |
|
51 |
|
52 #include <olectl.h> |
|
53 #include <qaxtypes.h> |
|
54 |
|
55 enum { debugAxWidget = 0 }; |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 /* XPM */ |
|
60 const char *widgetIconXPM[]={ |
|
61 "22 22 6 1", |
|
62 "a c #000000", |
|
63 "# c #808080", |
|
64 "+ c #aaa5a0", |
|
65 "b c #dddddd", |
|
66 "* c #d4d0c8", |
|
67 ". c none", |
|
68 ".........#aa#...#aa#..", |
|
69 ".........abba...abba..", |
|
70 ".........abba...abba..", |
|
71 ".........#aa#...#aa#..", |
|
72 "..........aa.....aa...", |
|
73 "..........aa.....aa...", |
|
74 "..........aa.....aa...", |
|
75 ".......aaaaaaaaaaaaaaa", |
|
76 ".......a*************a", |
|
77 ".......a************#a", |
|
78 ".......a***********+#a", |
|
79 ".......a***********+#a", |
|
80 ".......a***********+#a", |
|
81 "#aa#...a***********+#a", |
|
82 "abbaaaaa***********+#a", |
|
83 "abbaaaaa***********+#a", |
|
84 "#aa#...a***********+#a", |
|
85 ".......a***********+#a", |
|
86 ".......a***********+#a", |
|
87 ".......a**++++++++++#a", |
|
88 ".......a*############a", |
|
89 ".......aaaaaaaaaaaaaaa"}; |
|
90 |
|
91 QDesignerAxWidget::QDesignerAxWidget(QWidget *parent) : |
|
92 QWidget(parent), |
|
93 m_defaultSize(80, 70), |
|
94 m_drawFlags(DrawIndicator|DrawFrame|DrawControl), |
|
95 m_axobject(0), |
|
96 m_axImage(widgetIcon()) |
|
97 { |
|
98 } |
|
99 |
|
100 QDesignerAxWidget::~QDesignerAxWidget() |
|
101 { |
|
102 delete m_axobject; |
|
103 } |
|
104 |
|
105 QPixmap QDesignerAxWidget::widgetIcon() |
|
106 { |
|
107 return QPixmap(widgetIconXPM); |
|
108 } |
|
109 |
|
110 QString QDesignerAxWidget::control() const |
|
111 { |
|
112 if (m_axobject) |
|
113 return m_axobject->control(); |
|
114 return QString(); |
|
115 } |
|
116 |
|
117 void QDesignerAxWidget::setControl(const QString &clsid) |
|
118 { |
|
119 if (clsid == control()) |
|
120 return; |
|
121 if (clsid.isEmpty()) { |
|
122 resetControl(); |
|
123 } else { |
|
124 loadControl(clsid); |
|
125 } |
|
126 } |
|
127 void QDesignerAxWidget::resetControl() |
|
128 { |
|
129 if (!m_axobject) |
|
130 return; |
|
131 delete m_axobject; |
|
132 m_axobject = 0; |
|
133 update(); |
|
134 } |
|
135 |
|
136 bool QDesignerAxWidget::loadControl(const QString &clsid) |
|
137 { |
|
138 if (clsid.isEmpty()) |
|
139 return false; |
|
140 if (m_axobject) |
|
141 resetControl(); |
|
142 m_axobject = new QAxWidget(); |
|
143 |
|
144 if (!m_axobject->setControl(clsid)) { |
|
145 delete m_axobject; |
|
146 m_axobject = 0; |
|
147 return false; |
|
148 } |
|
149 update(); |
|
150 return true; |
|
151 } |
|
152 |
|
153 QSize QDesignerAxWidget::sizeHint() const |
|
154 { |
|
155 if (m_axobject) |
|
156 return m_axobject->sizeHint(); |
|
157 return m_defaultSize; |
|
158 } |
|
159 |
|
160 QSize QDesignerAxWidget::minimumSizeHint() const |
|
161 { |
|
162 if (m_axobject) |
|
163 return m_axobject->minimumSizeHint(); |
|
164 return QWidget::minimumSizeHint(); |
|
165 } |
|
166 |
|
167 void QDesignerAxWidget::paintEvent(QPaintEvent * /*event */) |
|
168 { |
|
169 QPainter p(this); |
|
170 const QRect r = contentsRect(); |
|
171 const int contentsWidth = r.width(); |
|
172 const int contentsHeight= r.height(); |
|
173 if (m_axobject) { // QAxWidget has no concept of sizeHint() |
|
174 if (m_drawFlags & DrawControl) { |
|
175 m_axobject->resize(size()); |
|
176 m_axobject->render(&p); |
|
177 } |
|
178 if (m_drawFlags & DrawIndicator) { |
|
179 static const QString loaded = tr("Control loaded"); |
|
180 QColor patternColor(Qt::green); |
|
181 if (m_drawFlags & DrawControl) |
|
182 patternColor.setAlpha(80); |
|
183 p.setBrush(QBrush(patternColor, Qt::BDiagPattern)); |
|
184 p.setPen(Qt::black); |
|
185 if (r.height() > 5) |
|
186 p.drawText(5,contentsHeight - 5, loaded); |
|
187 } |
|
188 } |
|
189 if (m_drawFlags & DrawFrame) { |
|
190 p.drawRect(r.adjusted(0, 0, -1, -1)); |
|
191 } |
|
192 if (m_drawFlags & DrawIndicator) { |
|
193 if (contentsWidth > m_axImage.width() && contentsHeight > m_axImage.height()) |
|
194 p.drawPixmap((contentsWidth - m_axImage.width()) / 2, |
|
195 (contentsHeight-m_axImage.height()) / 2, m_axImage); |
|
196 } |
|
197 } |
|
198 |
|
199 // --------- QDesignerAxPluginWidget |
|
200 QDesignerAxPluginWidget::QDesignerAxPluginWidget(QWidget *parent) : |
|
201 QDesignerAxWidget(parent) |
|
202 { |
|
203 } |
|
204 |
|
205 QDesignerAxPluginWidget::~QDesignerAxPluginWidget() |
|
206 { |
|
207 } |
|
208 |
|
209 const QMetaObject *QDesignerAxPluginWidget::metaObject() const |
|
210 { |
|
211 if (const QAxWidget *aw = axobject()) |
|
212 return aw->metaObject(); |
|
213 |
|
214 return QDesignerAxWidget::metaObject(); |
|
215 } |
|
216 |
|
217 static QString msgComException(const QObject *o, const QMetaObject::Call call, int index) |
|
218 { |
|
219 return QDesignerAxWidget::tr("A COM exception occurred when executing a meta call of type %1, index %2 of \"%3\"."). |
|
220 arg(call).arg(index).arg(o->objectName()); |
|
221 } |
|
222 |
|
223 int QDesignerAxPluginWidget::qt_metacall(QMetaObject::Call call, int signal, void **argv) |
|
224 { |
|
225 QAxWidget *aw = axobject(); |
|
226 if (!aw) |
|
227 return QDesignerAxWidget::qt_metacall(call,signal,argv); |
|
228 |
|
229 |
|
230 const QMetaObject *mo = metaObject(); |
|
231 // Have base class handle inherited stuff (geometry, enabled...) |
|
232 const bool inherited = call == QMetaObject::InvokeMetaMethod ? |
|
233 (signal < mo->methodOffset()) : (signal < mo->propertyOffset()); |
|
234 if (inherited) |
|
235 return QDesignerAxWidget::qt_metacall(call, signal, argv); |
|
236 |
|
237 int rc = -1; |
|
238 #ifndef QT_NO_EXCEPTIONS |
|
239 try { |
|
240 #endif |
|
241 if (debugAxWidget) |
|
242 if (call != QMetaObject::InvokeMetaMethod) |
|
243 qDebug() << objectName() << call << signal << mo->property(signal).name(); |
|
244 switch (call) { |
|
245 case QMetaObject::QueryPropertyStored: // Pretend all changed properties are stored for them to be saved |
|
246 if (m_propValues.contains(signal)) |
|
247 if (argv[0]) |
|
248 *reinterpret_cast< bool*>(argv[0]) = true; |
|
249 break; |
|
250 case QMetaObject::ResetProperty: |
|
251 rc = aw->qt_metacall(call, signal, argv); |
|
252 update(); |
|
253 m_propValues.remove(signal); |
|
254 break; |
|
255 case QMetaObject::WriteProperty: |
|
256 rc = aw->qt_metacall(call, signal, argv); |
|
257 update(); |
|
258 m_propValues.insert(signal, true); |
|
259 break; |
|
260 default: |
|
261 rc = aw->qt_metacall(call, signal, argv); |
|
262 break; |
|
263 } |
|
264 #ifndef QT_NO_EXCEPTIONS |
|
265 } catch(...) { |
|
266 qWarning(msgComException(this, call, signal).toUtf8()); |
|
267 } |
|
268 #endif |
|
269 return rc; |
|
270 } |
|
271 |
|
272 QT_END_NAMESPACE |