qdoc: Revised logic for which example file to open in Qt Creator

qdoc stores the 'files to open' into the example manifest. These
files are opened in Qt Creator's editor when an example is selected
from the Welcome / Examples list.

This change uses the following criteria (case insensitive), in
order of preference:
    - .qml file matching the project name
    - .cpp file matching the project name
    - .h file matching the project name
    - main.qml
    - main.cpp

A 'mainFile = "true"' argument is written for the file that is
preferred to be the top-most file.

Having a main.qml file take precedence over main.cpp ensures that
most Qt Quick examples open into the relevant QML code instead of
the boilerplate C++ used for launching the application.

Task-number: QTBUG-37203
Change-Id: I2ea58a31b1284f4f7d424dd35d49a84a23a88c23
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
Reviewed-by: Martin Smith <martin.smith@digia.com>
This commit is contained in:
Topi Reinio 2014-06-03 13:23:38 +02:00 committed by The Qt Project
parent 360fd4a278
commit 1a3d149174

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the tools applications of the Qt Toolkit.
@ -4399,36 +4399,43 @@ void HtmlGenerator::generateManifestFile(QString manifest, QString element)
}
QString ename = en->name().mid(en->name().lastIndexOf('/')+1);
QSet<QString> usedNames;
QMap<int, const Node*> filesToOpen;
foreach (const Node* child, en->childNodes()) {
if (child->subType() == Node::File) {
QString file = child->name();
QString fileName = file.mid(file.lastIndexOf('/')+1);
QString baseName = fileName;
if ((fileName.count(QChar('.')) > 0) &&
(fileName.endsWith(".cpp") ||
fileName.endsWith(".h") ||
fileName.endsWith(".qml")))
baseName.truncate(baseName.lastIndexOf(QChar('.')));
if (baseName.compare(ename, Qt::CaseInsensitive) == 0) {
if (!usedNames.contains(fileName)) {
writer.writeStartElement("fileToOpen");
writer.writeCharacters(examplesPath + file);
writer.writeEndElement(); // fileToOpen
usedNames.insert(fileName);
}
QFileInfo fileInfo(child->name());
QString fileName = fileInfo.fileName().toLower();
// open .qml, .cpp and .h files with a
// basename matching the example (project) name
// QMap key indicates the priority -
// the lowest value will be the top-most file
if ((fileInfo.baseName().compare(ename, Qt::CaseInsensitive) == 0)) {
if (fileName.endsWith(".qml"))
filesToOpen.insert(0, child);
else if (fileName.endsWith(".cpp"))
filesToOpen.insert(1, child);
else if (fileName.endsWith(".h"))
filesToOpen.insert(2, child);
}
else if (fileName.toLower().endsWith("main.cpp") ||
fileName.toLower().endsWith("main.qml")) {
if (!usedNames.contains(fileName)) {
writer.writeStartElement("fileToOpen");
writer.writeCharacters(examplesPath + file);
writer.writeEndElement(); // fileToOpen
usedNames.insert(fileName);
}
// main.qml takes precedence over main.cpp
else if (fileName.endsWith("main.qml")) {
filesToOpen.insert(3, child);
}
else if (fileName.endsWith("main.cpp")) {
filesToOpen.insert(4, child);
}
}
}
QMap<int, const Node*>::const_iterator it = filesToOpen.constEnd();
while (it != filesToOpen.constBegin()) {
writer.writeStartElement("fileToOpen");
if (--it == filesToOpen.constBegin()) {
writer.writeAttribute(QStringLiteral("mainFile"), QStringLiteral("true"));
}
writer.writeCharacters(examplesPath + it.value()->name());
writer.writeEndElement();
}
writer.writeEndElement(); // example
++i;
}