1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the ActiveQt framework of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
9 | ** You may use this file under the terms of the BSD license as follows:
|
---|
10 | **
|
---|
11 | ** "Redistribution and use in source and binary forms, with or without
|
---|
12 | ** modification, are permitted provided that the following conditions are
|
---|
13 | ** met:
|
---|
14 | ** * Redistributions of source code must retain the above copyright
|
---|
15 | ** notice, this list of conditions and the following disclaimer.
|
---|
16 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
17 | ** notice, this list of conditions and the following disclaimer in
|
---|
18 | ** the documentation and/or other materials provided with the
|
---|
19 | ** distribution.
|
---|
20 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
21 | ** the names of its contributors may be used to endorse or promote
|
---|
22 | ** products derived from this software without specific prior written
|
---|
23 | ** permission.
|
---|
24 | **
|
---|
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
36 | ** $QT_END_LICENSE$
|
---|
37 | **
|
---|
38 | ****************************************************************************/
|
---|
39 |
|
---|
40 | #include "qaxbase.h"
|
---|
41 |
|
---|
42 | #ifndef QT_NO_WIN_ACTIVEQT
|
---|
43 |
|
---|
44 | #include <qmetaobject.h>
|
---|
45 | #include <quuid.h>
|
---|
46 | #include <qt_windows.h>
|
---|
47 | #include <qtextstream.h>
|
---|
48 |
|
---|
49 | #include <ctype.h>
|
---|
50 |
|
---|
51 | #include "../shared/qaxtypes.h"
|
---|
52 |
|
---|
53 | QT_BEGIN_NAMESPACE
|
---|
54 |
|
---|
55 | QString qax_docuFromName(ITypeInfo *typeInfo, const QString &name)
|
---|
56 | {
|
---|
57 | QString docu;
|
---|
58 | if (!typeInfo)
|
---|
59 | return docu;
|
---|
60 |
|
---|
61 | MEMBERID memId;
|
---|
62 | BSTR names = QStringToBSTR(name);
|
---|
63 | typeInfo->GetIDsOfNames((BSTR*)&names, 1, &memId);
|
---|
64 | SysFreeString(names);
|
---|
65 | if (memId != DISPID_UNKNOWN) {
|
---|
66 | BSTR docStringBstr, helpFileBstr;
|
---|
67 | ulong helpContext;
|
---|
68 | HRESULT hres = typeInfo->GetDocumentation(memId, 0, &docStringBstr, &helpContext, &helpFileBstr);
|
---|
69 | QString docString = QString::fromUtf16((const ushort *)docStringBstr);
|
---|
70 | QString helpFile = QString::fromUtf16((const ushort *)helpFileBstr);
|
---|
71 | SysFreeString(docStringBstr);
|
---|
72 | SysFreeString(helpFileBstr);
|
---|
73 | if (hres == S_OK) {
|
---|
74 | if (!docString.isEmpty())
|
---|
75 | docu += docString + QLatin1String("\n");
|
---|
76 | if (!helpFile.isEmpty())
|
---|
77 | docu += QString::fromLatin1("For more information, see help context %1 in %2.").arg((uint)helpContext).arg(helpFile);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return docu;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static inline QString docuFromName(ITypeInfo *typeInfo, const QString &name)
|
---|
85 | {
|
---|
86 | return QLatin1String("<p>") + qax_docuFromName(typeInfo, name) + QLatin1String("\n");
|
---|
87 | }
|
---|
88 |
|
---|
89 | static QByteArray namedPrototype(const QList<QByteArray> ¶meterTypes, const QList<QByteArray> ¶meterNames, int numDefArgs = 0)
|
---|
90 | {
|
---|
91 | QByteArray prototype("(");
|
---|
92 | for (int p = 0; p < parameterTypes.count(); ++p) {
|
---|
93 | QByteArray type(parameterTypes.at(p));
|
---|
94 | prototype += type;
|
---|
95 |
|
---|
96 | if (p < parameterNames.count())
|
---|
97 | prototype += " " + parameterNames.at(p);
|
---|
98 |
|
---|
99 | if (numDefArgs >= parameterTypes.count() - p)
|
---|
100 | prototype += " = 0";
|
---|
101 | if (p < parameterTypes.count() - 1)
|
---|
102 | prototype += ", ";
|
---|
103 | }
|
---|
104 | prototype += ")";
|
---|
105 |
|
---|
106 | return prototype;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static QByteArray toType(const QByteArray &t)
|
---|
110 | {
|
---|
111 | QByteArray type = t;
|
---|
112 | int vartype = QVariant::nameToType(type);
|
---|
113 | if (vartype == QVariant::Invalid)
|
---|
114 | type = "int";
|
---|
115 |
|
---|
116 | if (type.at(0) == 'Q')
|
---|
117 | type = type.mid(1);
|
---|
118 | type[0] = toupper(type.at(0));
|
---|
119 | if (type == "VariantList")
|
---|
120 | type = "List";
|
---|
121 | else if (type == "Map<QVariant,QVariant>")
|
---|
122 | type = "Map";
|
---|
123 | else if (type == "Uint")
|
---|
124 | type = "UInt";
|
---|
125 |
|
---|
126 | return "to" + type + "()";
|
---|
127 | }
|
---|
128 |
|
---|
129 | QString qax_generateDocumentation(QAxBase *that)
|
---|
130 | {
|
---|
131 | that->metaObject();
|
---|
132 |
|
---|
133 | if (that->isNull())
|
---|
134 | return QString();
|
---|
135 |
|
---|
136 | ITypeInfo *typeInfo = 0;
|
---|
137 | IDispatch *dispatch = 0;
|
---|
138 | that->queryInterface(IID_IDispatch, (void**)&dispatch);
|
---|
139 | if (dispatch)
|
---|
140 | dispatch->GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, &typeInfo);
|
---|
141 |
|
---|
142 | QString docu;
|
---|
143 | QTextStream stream(&docu, QIODevice::WriteOnly);
|
---|
144 |
|
---|
145 | const QMetaObject *mo = that->metaObject();
|
---|
146 | QString coClass = QLatin1String(mo->classInfo(mo->indexOfClassInfo("CoClass")).value());
|
---|
147 |
|
---|
148 | stream << "<h1 align=center>" << coClass << " Reference</h1>" << endl;
|
---|
149 | stream << "<p>The " << coClass << " COM object is a " << that->qObject()->metaObject()->className();
|
---|
150 | stream << " with the CLSID " << that->control() << ".</p>" << endl;
|
---|
151 |
|
---|
152 | stream << "<h3>Interfaces</h3>" << endl;
|
---|
153 | stream << "<ul>" << endl;
|
---|
154 | const char *inter = 0;
|
---|
155 | int interCount = 1;
|
---|
156 | while ((inter = mo->classInfo(mo->indexOfClassInfo("Interface " + QByteArray::number(interCount))).value())) {
|
---|
157 | stream << "<li>" << inter << endl;
|
---|
158 | interCount++;
|
---|
159 | }
|
---|
160 | stream << "</ul>" << endl;
|
---|
161 |
|
---|
162 | stream << "<h3>Event Interfaces</h3>" << endl;
|
---|
163 | stream << "<ul>" << endl;
|
---|
164 | interCount = 1;
|
---|
165 | while ((inter = mo->classInfo(mo->indexOfClassInfo("Event Interface " + QByteArray::number(interCount))).value())) {
|
---|
166 | stream << "<li>" << inter << endl;
|
---|
167 | interCount++;
|
---|
168 | }
|
---|
169 | stream << "</ul>" << endl;
|
---|
170 |
|
---|
171 | QList<QString> methodDetails, propDetails;
|
---|
172 |
|
---|
173 | const int slotCount = mo->methodCount();
|
---|
174 | if (slotCount) {
|
---|
175 | stream << "<h2>Public Slots:</h2>" << endl;
|
---|
176 | stream << "<ul>" << endl;
|
---|
177 |
|
---|
178 | int defArgCount = 0;
|
---|
179 | for (int islot = mo->methodOffset(); islot < slotCount; ++islot) {
|
---|
180 | const QMetaMethod slot = mo->method(islot);
|
---|
181 | if (slot.methodType() != QMetaMethod::Slot)
|
---|
182 | continue;
|
---|
183 |
|
---|
184 | if (slot.attributes() & QMetaMethod::Cloned) {
|
---|
185 | ++defArgCount;
|
---|
186 | continue;
|
---|
187 | }
|
---|
188 |
|
---|
189 | QByteArray returntype(slot.typeName());
|
---|
190 | if (returntype.isEmpty())
|
---|
191 | returntype = "void";
|
---|
192 | QByteArray prototype = namedPrototype(slot.parameterTypes(), slot.parameterNames(), defArgCount);
|
---|
193 | QByteArray signature = slot.signature();
|
---|
194 | QByteArray name = signature.left(signature.indexOf('('));
|
---|
195 | stream << "<li>" << returntype << " <a href=\"#" << name << "\"><b>" << name << "</b></a>" << prototype << ";</li>" << endl;
|
---|
196 |
|
---|
197 | prototype = namedPrototype(slot.parameterTypes(), slot.parameterNames());
|
---|
198 | QString detail = QString::fromLatin1("<h3><a name=") + QString::fromLatin1(name.constData()) + QLatin1String("></a>") +
|
---|
199 | QString::fromLatin1(returntype.constData()) + QLatin1String(" ") +
|
---|
200 | QString::fromLatin1(name.constData()) + QLatin1String(" ") +
|
---|
201 | QString::fromLatin1(prototype.constData()) + QLatin1String("<tt> [slot]</tt></h3>\n");
|
---|
202 | prototype = namedPrototype(slot.parameterTypes(), QList<QByteArray>());
|
---|
203 | detail += docuFromName(typeInfo, QString::fromLatin1(name.constData()));
|
---|
204 | detail += QLatin1String("<p>Connect a signal to this slot:<pre>\n");
|
---|
205 | detail += QString::fromLatin1("\tQObject::connect(sender, SIGNAL(someSignal") + QString::fromLatin1(prototype.constData()) +
|
---|
206 | QLatin1String("), object, SLOT(") + QString::fromLatin1(name.constData()) +
|
---|
207 | QString::fromLatin1(prototype.constData()) + QLatin1String("));");
|
---|
208 | detail += QLatin1String("</pre>\n");
|
---|
209 |
|
---|
210 | if (1) {
|
---|
211 | detail += QLatin1String("<p>Or call the function directly:<pre>\n");
|
---|
212 |
|
---|
213 | bool hasParams = slot.parameterTypes().count() != 0;
|
---|
214 | if (hasParams)
|
---|
215 | detail += QLatin1String("\tQVariantList params = ...\n");
|
---|
216 | detail += QLatin1String("\t");
|
---|
217 | QByteArray functionToCall = "dynamicCall";
|
---|
218 | if (returntype == "IDispatch*" || returntype == "IUnknown*") {
|
---|
219 | functionToCall = "querySubObject";
|
---|
220 | returntype = "QAxObject *";
|
---|
221 | }
|
---|
222 | if (returntype != "void")
|
---|
223 | detail += QString::fromLatin1(returntype.constData()) + QLatin1String(" result = ");
|
---|
224 | detail += QLatin1String("object->") + QString::fromLatin1(functionToCall.constData()) +
|
---|
225 | QLatin1String("(\"" + name + prototype + "\"");
|
---|
226 | if (hasParams)
|
---|
227 | detail += QLatin1String(", params");
|
---|
228 | detail += QLatin1String(")");
|
---|
229 | if (returntype != "void" && returntype != "QAxObject *" && returntype != "QVariant")
|
---|
230 | detail += QLatin1String(".") + QString::fromLatin1(toType(returntype));
|
---|
231 | detail += QLatin1String(";</pre>\n");
|
---|
232 | } else {
|
---|
233 | detail += QLatin1String("<p>This function has parameters of unsupported types and cannot be called directly.");
|
---|
234 | }
|
---|
235 |
|
---|
236 | methodDetails << detail;
|
---|
237 | defArgCount = 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | stream << "</ul>" << endl;
|
---|
241 | }
|
---|
242 | int signalCount = mo->methodCount();
|
---|
243 | if (signalCount) {
|
---|
244 | ITypeLib *typeLib = 0;
|
---|
245 | if (typeInfo) {
|
---|
246 | UINT index = 0;
|
---|
247 | typeInfo->GetContainingTypeLib(&typeLib, &index);
|
---|
248 | typeInfo->Release();
|
---|
249 | }
|
---|
250 | typeInfo = 0;
|
---|
251 |
|
---|
252 | stream << "<h2>Signals:</h2>" << endl;
|
---|
253 | stream << "<ul>" << endl;
|
---|
254 |
|
---|
255 | for (int isignal = mo->methodOffset(); isignal < signalCount; ++isignal) {
|
---|
256 | const QMetaMethod signal(mo->method(isignal));
|
---|
257 | if (signal.methodType() != QMetaMethod::Signal)
|
---|
258 | continue;
|
---|
259 |
|
---|
260 | QByteArray prototype = namedPrototype(signal.parameterTypes(), signal.parameterNames());
|
---|
261 | QByteArray signature = signal.signature();
|
---|
262 | QByteArray name = signature.left(signature.indexOf('('));
|
---|
263 | stream << "<li>void <a href=\"#" << name << "\"><b>" << name << "</b></a>" << prototype << ";</li>" << endl;
|
---|
264 |
|
---|
265 | QString detail = QLatin1String("<h3><a name=") + QString::fromLatin1(name.constData()) + QLatin1String("></a>void ") +
|
---|
266 | QString::fromLatin1(name.constData()) + QLatin1String(" ") +
|
---|
267 | QString::fromLatin1(prototype.constData()) + QLatin1String("<tt> [signal]</tt></h3>\n");
|
---|
268 | if (typeLib) {
|
---|
269 | interCount = 0;
|
---|
270 | do {
|
---|
271 | if (typeInfo)
|
---|
272 | typeInfo->Release();
|
---|
273 | typeInfo = 0;
|
---|
274 | typeLib->GetTypeInfo(++interCount, &typeInfo);
|
---|
275 | QString typeLibDocu = docuFromName(typeInfo, QString::fromLatin1(name.constData()));
|
---|
276 | if (!typeLibDocu.isEmpty()) {
|
---|
277 | detail += typeLibDocu;
|
---|
278 | break;
|
---|
279 | }
|
---|
280 | } while (typeInfo);
|
---|
281 | }
|
---|
282 | prototype = namedPrototype(signal.parameterTypes(), QList<QByteArray>());
|
---|
283 | detail += QLatin1String("<p>Connect a slot to this signal:<pre>\n");
|
---|
284 | detail += QLatin1String("\tQObject::connect(object, SIGNAL(") + QString::fromLatin1(name.constData()) +
|
---|
285 | QString::fromLatin1(prototype.constData()) +
|
---|
286 | QLatin1String("), receiver, SLOT(someSlot") + QString::fromLatin1(prototype.constData()) + QLatin1String("));");
|
---|
287 | detail += QLatin1String("</pre>\n");
|
---|
288 |
|
---|
289 | methodDetails << detail;
|
---|
290 | if (typeInfo)
|
---|
291 | typeInfo->Release();
|
---|
292 | typeInfo = 0;
|
---|
293 | }
|
---|
294 | stream << "</ul>" << endl;
|
---|
295 |
|
---|
296 | if (typeLib)
|
---|
297 | typeLib->Release();
|
---|
298 | }
|
---|
299 |
|
---|
300 | const int propCount = mo->propertyCount();
|
---|
301 | if (propCount) {
|
---|
302 | if (dispatch)
|
---|
303 | dispatch->GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, &typeInfo);
|
---|
304 | stream << "<h2>Properties:</h2>" << endl;
|
---|
305 | stream << "<ul>" << endl;
|
---|
306 |
|
---|
307 | for (int iprop = 0; iprop < propCount; ++iprop) {
|
---|
308 | const QMetaProperty prop = mo->property(iprop);
|
---|
309 | QByteArray name(prop.name());
|
---|
310 | QByteArray type(prop.typeName());
|
---|
311 |
|
---|
312 | stream << "<li>" << type << " <a href=\"#" << name << "\"><b>" << name << "</b></a>;</li>" << endl;
|
---|
313 | QString detail = QLatin1String("<h3><a name=") + QString::fromLatin1(name.constData()) + QLatin1String("></a>") +
|
---|
314 | QString::fromLatin1(type.constData()) +
|
---|
315 | QLatin1String(" ") + QString::fromLatin1(name.constData()) + QLatin1String("</h3>\n");
|
---|
316 | detail += docuFromName(typeInfo, QString::fromLatin1(name));
|
---|
317 | QVariant::Type vartype = QVariant::nameToType(type);
|
---|
318 | if (!prop.isReadable())
|
---|
319 | continue;
|
---|
320 |
|
---|
321 | if (prop.isEnumType())
|
---|
322 | vartype = QVariant::Int;
|
---|
323 |
|
---|
324 | if (vartype != QVariant::Invalid) {
|
---|
325 | detail += QLatin1String("<p>Read this property's value using QObject::property:<pre>\n");
|
---|
326 | if (prop.isEnumType())
|
---|
327 | detail += QLatin1String("\tint val = ");
|
---|
328 | else
|
---|
329 | detail += QLatin1String("\t") + QString::fromLatin1(type.constData()) + QLatin1String(" val = ");
|
---|
330 | detail += QLatin1String("object->property(\"") + QString::fromLatin1(name.constData()) +
|
---|
331 | QLatin1String("\").") + QString::fromLatin1(toType(type).constData()) + QLatin1String(";\n");
|
---|
332 | detail += QLatin1String("</pre>\n");
|
---|
333 | } else if (type == "IDispatch*" || type == "IUnknown*") {
|
---|
334 | detail += QLatin1String("<p>Get the subobject using querySubObject:<pre>\n");
|
---|
335 | detail += QLatin1String("\tQAxObject *") + QString::fromLatin1(name.constData()) +
|
---|
336 | QLatin1String(" = object->querySubObject(\"") + QString::fromLatin1(name.constData()) + QLatin1String("\");\n");
|
---|
337 | detail += QLatin1String("</pre>\n");
|
---|
338 | } else {
|
---|
339 | detail += QLatin1String("<p>This property is of an unsupported type.\n");
|
---|
340 | }
|
---|
341 | if (prop.isWritable()) {
|
---|
342 | detail += QLatin1String("Set this property' value using QObject::setProperty:<pre>\n");
|
---|
343 | if (prop.isEnumType()) {
|
---|
344 | detail += QLatin1String("\tint newValue = ... // string representation of values also supported\n");
|
---|
345 | } else {
|
---|
346 | detail += QLatin1String("\t") + QString::fromLatin1(type.constData()) + QLatin1String(" newValue = ...\n");
|
---|
347 | }
|
---|
348 | detail += QLatin1String("\tobject->setProperty(\"") + QString::fromLatin1(name) + QLatin1String("\", newValue);\n");
|
---|
349 | detail += QLatin1String("</pre>\n");
|
---|
350 | detail += QLatin1String("Or using the ");
|
---|
351 | QByteArray setterSlot;
|
---|
352 | if (isupper(name.at(0))) {
|
---|
353 | setterSlot = "Set" + name;
|
---|
354 | } else {
|
---|
355 | QByteArray nameUp = name;
|
---|
356 | nameUp[0] = toupper(nameUp.at(0));
|
---|
357 | setterSlot = "set" + nameUp;
|
---|
358 | }
|
---|
359 | detail += QLatin1String("<a href=\"#") + QString::fromLatin1(setterSlot) + QLatin1String("\">") +
|
---|
360 | QString::fromLatin1(setterSlot.constData()) + QLatin1String("</a> slot.\n");
|
---|
361 | }
|
---|
362 | if (prop.isEnumType()) {
|
---|
363 | detail += QLatin1String("<p>See also <a href=\"#") + QString::fromLatin1(type) +
|
---|
364 | QLatin1String("\">") + QString::fromLatin1(type) + QLatin1String("</a>.\n");
|
---|
365 | }
|
---|
366 |
|
---|
367 | propDetails << detail;
|
---|
368 | }
|
---|
369 | stream << "</ul>" << endl;
|
---|
370 | }
|
---|
371 |
|
---|
372 | const int enumCount = mo->enumeratorCount();
|
---|
373 | if (enumCount) {
|
---|
374 | stream << "<hr><h2>Member Type Documentation</h2>" << endl;
|
---|
375 | for (int i = 0; i < enumCount; ++i) {
|
---|
376 | const QMetaEnum enumdata = mo->enumerator(i);
|
---|
377 | stream << "<h3><a name=" << enumdata.name() << "></a>" << enumdata.name() << "</h3>" << endl;
|
---|
378 | stream << "<ul>" << endl;
|
---|
379 | for (int e = 0; e < enumdata.keyCount(); ++e) {
|
---|
380 | stream << "<li>" << enumdata.key(e) << "\t=" << enumdata.value(e) << "</li>" << endl;
|
---|
381 | }
|
---|
382 | stream << "</ul>" << endl;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | if (methodDetails.count()) {
|
---|
386 | stream << "<hr><h2>Member Function Documentation</h2>" << endl;
|
---|
387 | for (int i = 0; i < methodDetails.count(); ++i)
|
---|
388 | stream << methodDetails.at(i) << endl;
|
---|
389 | }
|
---|
390 | if (propDetails.count()) {
|
---|
391 | stream << "<hr><h2>Property Documentation</h2>" << endl;
|
---|
392 | for (int i = 0; i < propDetails.count(); ++i)
|
---|
393 | stream << propDetails.at(i) << endl;
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (typeInfo)
|
---|
397 | typeInfo->Release();
|
---|
398 | if (dispatch)
|
---|
399 | dispatch->Release();
|
---|
400 | return docu;
|
---|
401 | }
|
---|
402 |
|
---|
403 | QT_END_NAMESPACE
|
---|
404 | #endif // QT_NO_WIN_ACTIVEQT
|
---|