summaryrefslogtreecommitdiffstats
path: root/generator/docgenerator.cpp
blob: aa8b84279357de4cfd4e9ea93fd102e716b86ef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Script Generator project on Qt Labs.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "docgenerator.h"
#include "fileout.h"

DocGenerator::DocGenerator()
{
}

QString DocGenerator::fileNameForClass(const AbstractMetaClass *meta_class) const
{
    return QString::fromLatin1("%0.html").arg(meta_class->name().toLower());
}

QString DocGenerator::subDirectoryForClass(const AbstractMetaClass *) const
{
    return QString::fromLatin1("doc");
}

static void writeDocumentHeader(QTextStream &s, const QString &title)
{
    s << "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" << endl
      << "<!DOCTYPE html" << endl
      << "    PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"DTD/xhtml1-strict.dtd\">" << endl
      << "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" << endl
      << "<head>" << endl
      << "  <title>" << title << "</title>" << endl
      << "  <link href=\"classic.css\" rel=\"stylesheet\" type=\"text/css\" />" << endl
      << "</head>" << endl
      << "<body>" << endl;
}

static void writeDocumentFooter(QTextStream &s)
{
    s << "</body>" << endl
      << "</html>" << endl;
}

static bool classLessThan(const AbstractMetaClass *c1, const AbstractMetaClass *c2)
{
    return c1->name() < c2->name();
}

bool DocGenerator::shouldGenerate(const AbstractMetaClass *meta_class) const
{
    uint cg = meta_class->typeEntry()->codeGeneration();
    return (cg & TypeEntry::GenerateCode) != 0;
}

void DocGenerator::generate()
{
    Generator::generate();

    QHash<QString, QList<const AbstractMetaClass*> > packHash;
    for (int i = 0; i < m_classes.size(); ++i) {
        const AbstractMetaClass *cls = m_classes.at(i);
        packHash[cls->package()].append(cls);
    }

    // package pages
    QHash<QString, QList<const AbstractMetaClass*> >::const_iterator it;
    for (it = packHash.constBegin(); it != packHash.constEnd(); ++it) {
        QString package = it.key();
        QList<const AbstractMetaClass*> classesInPackage = it.value();
        qSort(classesInPackage.begin(), classesInPackage.end(), classLessThan);

        FileOut file(m_out_dir + "/doc/" + package.split(".").join("_") + ".html");

        writeDocumentHeader(file.stream, package + " Package");

        file.stream << "<h1 align=\"center\">" << package << " Package</h1>" << endl;

        file.stream << "<h2>Classes</h2>" << endl
                    << "<p><table width=\"100%\" class=\"annotated\" cellpadding=\"2\" cellspacing=\"1\" border=\"0\">" << endl;

        for (int i = 0; i < classesInPackage.size(); ++i) {
            const AbstractMetaClass *cls = classesInPackage.at(i);
            if (cls->name() == "Global")
                continue; /// ### fixme
            file.stream << "<tr valign=\"top\" class=\"";
            if (i & 1)
                file.stream << "odd";
            else
                file.stream << "even";
            file.stream << "\"><th><a href=\"" << fileNameForClass(cls) << "\">" << cls->name()
                        << "</a></th></tr>" << endl;
        }

        file.stream << "</table></p>" << endl;

        writeDocumentFooter(file.stream);
    }

    // all classes page
    {
        FileOut file(m_out_dir + "/doc/classes.html");

        writeDocumentHeader(file.stream, "Classes");

        file.stream << "<h1 align=\"center\">Classes</h1>" << endl
                    << "<p><table width=\"100%\" class=\"annotated\" cellpadding=\"2\" cellspacing=\"1\" border=\"0\">" << endl;

        AbstractMetaClassList sortedClasses = m_classes;
        qSort(sortedClasses.begin(), sortedClasses.end(), classLessThan);

        for (int i = 0; i < sortedClasses.size(); ++i) {
            const AbstractMetaClass *cls = sortedClasses.at(i);
            if (cls->name() == "Global")
                continue; /// ### fixme
            file.stream << "<tr valign=\"top\" class=\"";
            if (i & 1)
                file.stream << "odd";
            else
                file.stream << "even";
            file.stream << "\"><th><a href=\"" << fileNameForClass(cls) << "\">" << cls->name()
                        << "</a></th></tr>" << endl;
        }

        file.stream << "</table></p>" << endl;

        writeDocumentFooter(file.stream);
    }

    // index.html
    {
        FileOut file(m_out_dir + "/doc/index.html");

        writeDocumentHeader(file.stream, "Qt Bindings Reference Documentation");

        file.stream << "<h1 align=\"center\">Qt Script Qt Bindings Reference Documentation</h1>" << endl;

        file.stream << "<h3>Packages</h3>" << endl;
        file.stream << "<ul>" << endl;
        QStringList sortedPackages = packHash.keys();
        qSort(sortedPackages.begin(), sortedPackages.end());
        for (int i = 0; i < sortedPackages.size(); ++i) {
            QString pkg = sortedPackages.at(i);
            file.stream << "<li><b><a href=\"" << pkg.split(".").join("_") << ".html\">"
                        << pkg << "</a></b></li>" << endl;
        }
        file.stream << "</ul>" << endl;

        file.stream << "<h3><a href=\"classes.html\">All Classes</a></h3>" << endl;

        file.stream << "<h3><a href=\"../examples\">Examples</a></h3>" << endl;

        file.stream << "<h3>Getting Started</h3>" << endl
                    << "<p>Using the Qt API in Qt Script is very similar to C++." << endl
                    << "<pre>var f = new QFile(\"foo.txt\");</pre>" << endl
                    << "C++ enum values are mapped to properties of the script constructor function; e.g. " << endl
                    << "QIODevice::ReadOnly becomes QIODevice.ReadOnly.</p>" << endl
                    << "<pre>f.open(new QIODevice.OpenMode(QIODevice.ReadOnly));</pre>" << endl
                    << "<p>Each C++ flag type is mapped to a property of the script constructor function; e.g. " << endl
                    << "QIODevice::OpenMode becomes QIODevice.OpenMode. Such a property is a constructor function " << endl
                    << "that takes one or more enum values and constructs a flags instance by OR'ing the arguments " << endl
                    << "together.</p>" << endl
                    << "<pre>var ts = new QTextStream(f);" << endl
                    << "ts.writeString(\"Boo\");</pre>" << endl
                    << "<p>C++ streaming operators are normally mapped to readT() and writeT() functions.</p>" << endl
                    << "<pre>f.close();</pre>" << endl
                    << "<p>In Qt Script, all objects are allocated on the heap; objects that are no longer " << endl
                    << "referenced are garbage collected sometime in the future; therefore, make sure to " << endl
                    << "explicitly free up resources if you can. (Without the call to close(), the underlying " << endl
                    << "file would remain open until the file object is garbage collected.)</p>" << endl
            ;

        file.stream << "<h3><a href=\"http://doc.trolltech.com/latest\">Qt Reference Documentation</a></h3>" << endl;

        writeDocumentFooter(file.stream);
    }
}

static bool shouldIgnoreEnum(const AbstractMetaEnum *enom)
{
    return !enom->wasPublic() || (enom->name() == "enum_1");
}

// in classgenerator.cpp
void findPrototypeAndStaticFunctions(
    const AbstractMetaClass *meta_class,
    QMap<QString, AbstractMetaFunctionList> &nameToPrototypeFunctions,
    QMap<QString, AbstractMetaFunctionList> &nameToStaticFunctions);
QList<int> uniqueEnumValueIndexes(const AbstractMetaEnumValueList &values);

static void writeFunction(QTextStream &s, const AbstractMetaFunction *fun)
{
    s << "<li><div class=\"fn\"/><b>" << fun->targetLangSignature() << "</b></li>" << endl;
}

void DocGenerator::write(QTextStream &s, const AbstractMetaClass *meta_class)
{
    QString title = meta_class->name();
    title.append(" ");
    if (meta_class->isNamespace())
        title.append("Namespace");
    else
        title.append("Class");
    title.append(" Reference");
    writeDocumentHeader(s, title);

    s << "<h1 align=\"center\">" << title << "</h1>" << endl;

    s << "<h3 align=\"center\">[<a href=\"";
    s << meta_class->package().split(".").join("_") << ".html";
    s << "\">";
    s << meta_class->package();
    s << "</a> package]</h3>" << endl;

    if (meta_class->baseClass()) {
        s << "<p>Inherits <a href=\"" << fileNameForClass(meta_class->baseClass()) << "\">"
          << meta_class->baseClass()->name() << "</a>.</p>" << endl;
    } else if (!meta_class->interfaces().isEmpty()) {
        AbstractMetaClass *iface = meta_class->interfaces().first();
        AbstractMetaClass *impl = iface->primaryInterfaceImplementor();
        if (impl != meta_class) {
            s << "<p>Inherits <a href=\"" << fileNameForClass(impl) << "\">"
              << impl->name() << "</a>.</p>" << endl;
        }
    }

    AbstractMetaFunctionList ctors;
    ctors = meta_class->queryFunctions(AbstractMetaClass::Constructors
                                       | AbstractMetaClass::WasPublic
                                       | AbstractMetaClass::NotRemovedFromTargetLang);
    QMap<QString, AbstractMetaFunctionList> nameToPrototypeFunctions;
    QMap<QString, AbstractMetaFunctionList> nameToStaticFunctions;
    findPrototypeAndStaticFunctions(meta_class, nameToPrototypeFunctions, nameToStaticFunctions);

    s << "<h3>Constructor</h3>" << endl;
    if (!ctors.isEmpty()) {
        s << "<ul>" << endl;
        for (int i = 0; i < ctors.size(); ++i) {
            writeFunction(s, ctors.at(i));
        }
        s << "</ul>" << endl;
    } else {
        s << "<p>This class has no public constructors. Calling the constructor function will cause a TypeError.</p>";
    }

    s << "<h3>Constructor Properties</h3>" << endl;
    s << "<ul>" << endl;
    s << "<li><b>prototype</b>: The " << meta_class->name() << " prototype object</li>" << endl;
    if (!nameToStaticFunctions.isEmpty()) {
        QMap<QString, AbstractMetaFunctionList>::const_iterator it;
        for (it = nameToStaticFunctions.constBegin(); it != nameToStaticFunctions.constEnd(); ++it) {
            writeFunction(s, it.value().first());
        }
    }
    {
        AbstractMetaEnumList enums = meta_class->enums();
        for (int i = 0; i < enums.size(); ++i) {
            const AbstractMetaEnum *enom = enums.at(i);
            if (shouldIgnoreEnum(enom))
                continue;
            AbstractMetaEnumValueList values = enom->values();
            QList<int> indexes = uniqueEnumValueIndexes(values);
            for (int j = 0; j < indexes.size(); ++j) {
                AbstractMetaEnumValue *val = values.at(indexes.at(j));
                s << "<li><b>" << val->name();
                if (!val->stringValue().isEmpty())
                    s << " = " << val->stringValue();
                s << "</b></li>" << endl;
            }
            s << "<li><b>" << enom->name() << "( value )</b></li>" << endl;
            FlagsTypeEntry *flags = enom->typeEntry()->flags();
            if (flags)
                s << "<li><b>" << flags->flagsName() << "( value1, value2, ... )</b></li>" << endl;
        }
    }
    s << "</ul>" << endl;

    if (!nameToPrototypeFunctions.isEmpty()) {
        s << "<h3>Prototype Object Properties</h3>" << endl;
        if (meta_class->baseClass()) {
            s << "<p>The " << meta_class->name() << " prototype object inherits properties from the "
              << "<a href=\"" << fileNameForClass(meta_class->baseClass()) << "\">"
              << meta_class->baseClass()->name() << "</a> prototype object and "
              << "also has the following properties.</p>" << endl;
        }
        s << "<ul>" << endl;
        QMap<QString, AbstractMetaFunctionList>::const_iterator it;
        for (it = nameToPrototypeFunctions.constBegin(); it != nameToPrototypeFunctions.constEnd(); ++it) {
            writeFunction(s, it.value().first());
        }
        s << "</ul>" << endl;
    }

    if (!meta_class->isNamespace()) {
        s << "<h3>Instance Properties</h3>" << endl;
        {
            QList<QPropertySpec *> props = meta_class->propertySpecs();
            if (!props.isEmpty()) {
                s << "<p>" << meta_class->name() << " objects inherit properties from the "
                  << meta_class->name() << " prototype object and also have the following properties.</p>" << endl;
                s << "<ul>" << endl;
                for (int i = 0; i < props.size(); ++i) {
                    s << "<li><div class=\"fn\"/><b>" << props.at(i)->name() << "</b></li>" << endl;
                }
                s << "</ul>" << endl;
            } else {
                s << "<p>" << meta_class->name() << " objects have no special properties beyond those "
                  << "inherited from the " << meta_class->name() << " prototype object.</p>" << endl;
            }
        }
    }

    writeDocumentFooter(s);
}