Commit df50e214 authored by Tobias Hunger's avatar Tobias Hunger

QtSupport: Add codegeneration.h/cpp

Change-Id: Iec1d4b303e6154b5d2d32e4d3ab7c02893bcc5f1
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 850276d9
......@@ -29,7 +29,10 @@
#include "codegeneration.h"
#include "algorithm.h"
#include <QTextStream>
#include <QSet>
#include <QStringList>
#include <QFileInfo>
......@@ -83,6 +86,51 @@ QTCREATOR_UTILS_EXPORT void writeBeginQtVersionCheck(QTextStream &str)
str << QLatin1String("#if QT_VERSION >= 0x050000\n");
}
static void qtSection(const QStringList &qtIncludes, QTextStream &str)
{
QStringList sorted = qtIncludes;
Utils::sort(sorted);
foreach (const QString &inc, sorted) {
if (!inc.isEmpty())
str << QStringLiteral("#include <%1>\n").arg(inc);
}
}
QTCREATOR_UTILS_EXPORT
void writeQtIncludeSection(const QStringList &qt4,
const QStringList &qt5,
bool addQtVersionCheck,
bool includeQtModule,
QTextStream &str)
{
std::function<QString(const QString &)> trans;
if (includeQtModule)
trans = [](const QString &i) { return i; };
else
trans = [](const QString &i) { return i.mid(i.indexOf(QLatin1Char('/')) + 1); };
QSet<QString> qt4Only = QSet<QString>::fromList(Utils::transform(qt4, trans));
QSet<QString> qt5Only = QSet<QString>::fromList(Utils::transform(qt5, trans));
QSet<QString> common = qt4Only;
common.intersect(qt5Only);
qt4Only.subtract(common);
qt5Only.subtract(common);
qtSection(common.toList(), str);
if (!qt4Only.isEmpty() || !qt5Only.isEmpty()) {
if (addQtVersionCheck)
writeBeginQtVersionCheck(str);
qtSection(qt5Only.toList(), str);
if (addQtVersionCheck)
str << QLatin1String("#else\n");
qtSection(qt4Only.toList(), str);
if (addQtVersionCheck)
str << QLatin1String("#endif\n");
}
}
QTCREATOR_UTILS_EXPORT
QString writeOpeningNameSpaces(const QStringList &l, const QString &indent,
QTextStream &str)
......
......@@ -53,6 +53,12 @@ void writeIncludeFileDirective(const QString &file,
QTCREATOR_UTILS_EXPORT void writeBeginQtVersionCheck(QTextStream &str);
QTCREATOR_UTILS_EXPORT void writeQtIncludeSection(const QStringList &qt4,
const QStringList &qt5,
bool addQtVersionCheck,
bool includeQtModule,
QTextStream &str);
// Write opening namespaces and return an indentation string to be used
// in the following code if there are any.
QTCREATOR_UTILS_EXPORT
......
......@@ -31,6 +31,7 @@
#include "formclasswizardpage.h"
#include "formclasswizardparameters.h"
#include <designer/formtemplatewizardpage.h>
#include <qtsupport/codegenerator.h>
#include <coreplugin/basefilewizardfactory.h>
......@@ -83,7 +84,7 @@ void FormClassWizardDialog::initializePage(int id)
m_rawFormTemplate = m_formPage->templateContents();
// Strip namespaces from the ui class and suggest it as a new class
// name
if (FormTemplateWizardPage::getUIXmlData(m_rawFormTemplate, &formBaseClass, &uiClassName))
if (QtSupport::CodeGenerator::uiData(m_rawFormTemplate, &formBaseClass, &uiClassName))
m_classPage->setClassName(FormTemplateWizardPage::stripNamespaces(uiClassName));
}
}
......@@ -93,7 +94,7 @@ FormClassWizardParameters FormClassWizardDialog::parameters() const
FormClassWizardParameters rc;
m_classPage->getParameters(&rc);
// Name the ui class in the Ui namespace after the class specified
rc.uiTemplate = FormTemplateWizardPage::changeUiClassName(m_rawFormTemplate, rc.className);
rc.uiTemplate = QtSupport::CodeGenerator::changeUiClassName(m_rawFormTemplate, rc.className);
return rc;
}
......
......@@ -100,152 +100,5 @@ QString FormTemplateWizardPage::stripNamespaces(const QString &className)
return rc;
}
bool FormTemplateWizardPage::getUIXmlData(const QString &uiXml,
QString *formBaseClass,
QString *uiClassName)
{
// Parse UI xml to determine
// 1) The ui class name from "<class>Designer::Internal::FormClassWizardPage</class>"
// 2) the base class from: widget class="QWizardPage"...
QXmlStreamReader reader(uiXml);
while (!reader.atEnd()) {
if (reader.readNext() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("class")) {
*uiClassName = reader.readElementText();
} else {
if (reader.name() == QLatin1String("widget")) {
const QXmlStreamAttributes attrs = reader.attributes();
*formBaseClass = reader.attributes().value(QLatin1String("class")).toString();
return !uiClassName->isEmpty() && !formBaseClass->isEmpty();
}
}
}
}
return false;
}
// Change the contents of a DOM element to a new value if it matches
// a predicate
template <class Predicate>
bool changeDomElementContents(const QDomElement &element,
Predicate p,
const QString &newValue,
QString *ptrToOldValue = 0)
{
// Find text in "<element>text</element>"
const QDomNodeList children = element.childNodes();
if (children.size() != 1)
return false;
const QDomNode first = children.at(0);
if (first.nodeType() != QDomNode::TextNode)
return false;
QDomCharacterData data = first.toCharacterData();
const QString oldValue = data.data();
if (p(oldValue)) {
if (ptrToOldValue)
*ptrToOldValue = oldValue;
data.setData(newValue);
return true;
}
return false;
}
namespace {
bool truePredicate(const QString &) { return true; }
// Predicate that matches a string value
class MatchPredicate {
public:
MatchPredicate(const QString &m) : m_match(m) {}
bool operator()(const QString &s) const { return s == m_match; }
private:
const QString m_match;
};
// Change <sender> and <receiver> in a Dom UI <connections> list
// if they match the class name passed on
void changeDomConnectionList(const QDomElement &connectionsNode,
const QString &oldClassName,
const QString &newClassName)
{
const MatchPredicate oldClassPredicate(oldClassName);
const QString senderTag = QLatin1String("sender");
const QString receiverTag = QLatin1String("receiver");
const QDomNodeList connections = connectionsNode.childNodes();
const int connectionsCount = connections.size();
// Loop <connection>
for (int c = 0; c < connectionsCount; c++) {
const QDomNodeList connectionElements = connections.at(c).childNodes();
const int connectionElementCount = connectionElements.count();
// Loop <sender>, <receiver>, <signal>, <slot>
for (int ce = 0; ce < connectionElementCount; ce++) {
const QDomNode connectionElementNode = connectionElements.at(ce);
if (connectionElementNode.isElement()) {
const QDomElement connectionElement = connectionElementNode.toElement();
const QString tagName = connectionElement.tagName();
if (tagName == senderTag || tagName == receiverTag)
changeDomElementContents(connectionElement, oldClassPredicate, newClassName);
}
}
}
}
}
// Change the UI class name in UI xml: This occurs several times, as contents
// of the <class> element, as name of the first <widget> element, and possibly
// in the signal/slot connections
QString FormTemplateWizardPage::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
if (Designer::Constants::Internal::debug)
qDebug() << '>' << Q_FUNC_INFO << newUiClassName;
QDomDocument domUi;
if (!domUi.setContent(uiXml)) {
qWarning("Failed to parse:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
bool firstWidgetElementFound = false;
QString oldClassName;
// Loop first level children. First child is <ui>
const QDomNodeList children = domUi.firstChildElement().childNodes();
const QString classTag = QLatin1String("class");
const QString widgetTag = QLatin1String("widget");
const QString connectionsTag = QLatin1String("connections");
const int count = children.size();
for (int i = 0; i < count; i++) {
const QDomNode node = children.at(i);
if (node.isElement()) {
// Replace <class> element text
QDomElement element = node.toElement();
const QString name = element.tagName();
if (name == classTag) {
if (!changeDomElementContents(element, truePredicate, newUiClassName, &oldClassName)) {
qWarning("Unable to change the <class> element:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
} else {
// Replace first <widget> element name attribute
if (!firstWidgetElementFound && name == widgetTag) {
firstWidgetElementFound = true;
const QString nameAttribute = QLatin1String("name");
if (element.hasAttribute(nameAttribute))
element.setAttribute(nameAttribute, newUiClassName);
} else {
// Replace <sender>, <receiver> tags of dialogs.
if (name == connectionsTag)
changeDomConnectionList(element, oldClassName, newUiClassName);
}
}
}
}
const QString rc = domUi.toString();
if (Designer::Constants::Internal::debug > 1)
qDebug() << '<' << Q_FUNC_INFO << newUiClassName << rc;
return rc;
}
} // namespace Internal
} // namespace Designer
......@@ -54,12 +54,6 @@ public:
QString templateContents() const { return m_templateContents; }
// Parse UI XML forms to determine:
// 1) The ui class name from "<class>Designer::Internal::FormClassWizardPage</class>"
// 2) the base class from: widget class="QWizardPage"...
static bool getUIXmlData(const QString &uiXml, QString *formBaseClass, QString *uiClassName);
// Change the class name in a UI XML form
static QString changeUiClassName(const QString &uiXml, const QString &newUiClassName);
static QString stripNamespaces(const QString &className);
signals:
......
......@@ -31,6 +31,7 @@
#include "formtemplatewizardpage.h"
#include <coreplugin/basefilewizardfactory.h>
#include <qtsupport/codegenerator.h>
#include <utils/filewizardpage.h>
......@@ -107,7 +108,7 @@ void FormFileWizardDialog::slotCurrentIdChanged(int id)
// the ui class
QString formBaseClass;
QString uiClassName;
if (FormTemplateWizardPage::getUIXmlData(templateContents(), &formBaseClass, &uiClassName)) {
if (QtSupport::CodeGenerator::uiData(templateContents(), &formBaseClass, &uiClassName)) {
QString fileName = FormTemplateWizardPage::stripNamespaces(uiClassName).toLower();
fileName += QLatin1String(".ui");
m_filePage->setFileName(fileName);
......
......@@ -34,6 +34,7 @@
#include <utils/codegeneration.h>
#include <coreplugin/icore.h>
#include <cpptools/abstracteditorsupport.h>
#include <qtsupport/codegenerator.h>
#include <qtsupport/codegensettings.h>
#include <QTextStream>
......@@ -76,7 +77,7 @@ bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParamete
QString formBaseClass;
QString uiClassName;
if (!Internal::FormTemplateWizardPage::getUIXmlData(parameters.uiTemplate, &formBaseClass, &uiClassName)) {
if (!QtSupport::CodeGenerator::uiData(parameters.uiTemplate, &formBaseClass, &uiClassName)) {
qWarning("Unable to determine the form base class from %s.", qPrintable(parameters.uiTemplate));
return false;
}
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "codegenerator.h"
#include "codegensettings.h"
#include <coreplugin/icore.h>
#include <utils/codegeneration.h>
#include <QDomDocument>
#include <QSettings>
#include <QTextStream>
#include <QXmlStreamReader>
namespace QtSupport {
// Change the contents of a DOM element to a new value if it matches
// a predicate
template <class Predicate>
bool changeDomElementContents(const QDomElement &element,
Predicate p,
const QString &newValue,
QString *ptrToOldValue = 0)
{
// Find text in "<element>text</element>"
const QDomNodeList children = element.childNodes();
if (children.size() != 1)
return false;
const QDomNode first = children.at(0);
if (first.nodeType() != QDomNode::TextNode)
return false;
QDomCharacterData data = first.toCharacterData();
const QString oldValue = data.data();
if (p(oldValue)) {
if (ptrToOldValue)
*ptrToOldValue = oldValue;
data.setData(newValue);
return true;
}
return false;
}
namespace {
bool truePredicate(const QString &) { return true; }
// Predicate that matches a string value
class MatchPredicate {
public:
MatchPredicate(const QString &m) : m_match(m) {}
bool operator()(const QString &s) const { return s == m_match; }
private:
const QString m_match;
};
// Change <sender> and <receiver> in a Dom UI <connections> list
// if they match the class name passed on
void changeDomConnectionList(const QDomElement &connectionsNode,
const QString &oldClassName,
const QString &newClassName)
{
const MatchPredicate oldClassPredicate(oldClassName);
const QString senderTag = QLatin1String("sender");
const QString receiverTag = QLatin1String("receiver");
const QDomNodeList connections = connectionsNode.childNodes();
const int connectionsCount = connections.size();
// Loop <connection>
for (int c = 0; c < connectionsCount; c++) {
const QDomNodeList connectionElements = connections.at(c).childNodes();
const int connectionElementCount = connectionElements.count();
// Loop <sender>, <receiver>, <signal>, <slot>
for (int ce = 0; ce < connectionElementCount; ce++) {
const QDomNode connectionElementNode = connectionElements.at(ce);
if (connectionElementNode.isElement()) {
const QDomElement connectionElement = connectionElementNode.toElement();
const QString tagName = connectionElement.tagName();
if (tagName == senderTag || tagName == receiverTag)
changeDomElementContents(connectionElement, oldClassPredicate, newClassName);
}
}
}
}
}
// Change the UI class name in UI xml: This occurs several times, as contents
// of the <class> element, as name of the first <widget> element, and possibly
// in the signal/slot connections
QString CodeGenerator::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
QDomDocument domUi;
if (!domUi.setContent(uiXml)) {
qWarning("Failed to parse:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
bool firstWidgetElementFound = false;
QString oldClassName;
// Loop first level children. First child is <ui>
const QDomNodeList children = domUi.firstChildElement().childNodes();
const QString classTag = QLatin1String("class");
const QString widgetTag = QLatin1String("widget");
const QString connectionsTag = QLatin1String("connections");
const int count = children.size();
for (int i = 0; i < count; i++) {
const QDomNode node = children.at(i);
if (node.isElement()) {
// Replace <class> element text
QDomElement element = node.toElement();
const QString name = element.tagName();
if (name == classTag) {
if (!changeDomElementContents(element, truePredicate, newUiClassName, &oldClassName)) {
qWarning("Unable to change the <class> element:\n%s", uiXml.toUtf8().constData());
return uiXml;
}
} else {
// Replace first <widget> element name attribute
if (!firstWidgetElementFound && name == widgetTag) {
firstWidgetElementFound = true;
const QString nameAttribute = QLatin1String("name");
if (element.hasAttribute(nameAttribute))
element.setAttribute(nameAttribute, newUiClassName);
} else {
// Replace <sender>, <receiver> tags of dialogs.
if (name == connectionsTag)
changeDomConnectionList(element, oldClassName, newUiClassName);
}
}
}
}
const QString rc = domUi.toString();
return rc;
}
bool CodeGenerator::uiData(const QString &uiXml, QString *formBaseClass, QString *uiClassName)
{
// Parse UI xml to determine
// 1) The ui class name from "<class>Designer::Internal::FormClassWizardPage</class>"
// 2) the base class from: widget class="QWizardPage"...
QXmlStreamReader reader(uiXml);
while (!reader.atEnd()) {
if (reader.readNext() == QXmlStreamReader::StartElement) {
if (reader.name() == QLatin1String("class")) {
*uiClassName = reader.readElementText();
} else {
if (reader.name() == QLatin1String("widget")) {
const QXmlStreamAttributes attrs = reader.attributes();
*formBaseClass = attrs.value(QLatin1String("class")).toString();
return !uiClassName->isEmpty() && !formBaseClass->isEmpty();
}
}
}
}
return false;
}
QString CodeGenerator::qtIncludes(const QStringList &qt4, const QStringList &qt5)
{
CodeGenSettings settings;
settings.fromSettings(Core::ICore::settings());
QString result;
QTextStream str(&result);
Utils::writeQtIncludeSection(qt4, qt5, settings.addQtVersionCheck, settings.includeQtModule, str);
return result;
}
} // namespace QtSupport
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CODEGENERATOR_H
#define CODEGENERATOR_H
#include "qtsupport_global.h"
#include <QStringList>
namespace QtSupport {
class QTSUPPORT_EXPORT CodeGenerator : public QObject
{
public:
CodeGenerator(QObject *parent = 0) : QObject(parent) { }
// Ui file related:
// Change the class name in a UI XML form
Q_INVOKABLE static QString changeUiClassName(const QString &uiXml, const QString &newUiClassName);
// Low level method to get everything at the same time:
static bool uiData(const QString &uiXml, QString *formBaseClass, QString *uiClassName);
// Generic Qt:
Q_INVOKABLE static QString qtIncludes(const QStringList &qt4, const QStringList &qt5);
};
} // namespace QtSupport
#endif // CODEGENERATOR_H
......@@ -7,6 +7,7 @@ DEFINES += QMAKE_LIBRARY
include(../../shared/proparser/proparser.pri)
HEADERS += \
codegenerator.h \
codegensettings.h \
codegensettingspage.h \
gettingstartedwelcomepage.h \
......@@ -38,6 +39,7 @@ HEADERS += \
winceqtversion.h
SOURCES += \
codegenerator.cpp \
codegensettings.cpp \
codegensettingspage.cpp \
gettingstartedwelcomepage.cpp \
......
......@@ -60,6 +60,8 @@ QtcPlugin {
files: [
"baseqtversion.cpp",
"baseqtversion.h",
"codegenerator.cpp",
"codegenerator.h",
"codegensettings.cpp",
"codegensettings.h",
"codegensettingspage.cpp",
......
......@@ -29,6 +29,7 @@
#include "qtsupportplugin.h"
#include "codegenerator.h"
#include "codegensettingspage.h"
#include "customexecutablerunconfiguration.h"
#include "desktopqtversionfactory.h"
......@@ -45,6 +46,7 @@
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/variablemanager.h>
#include <coreplugin/jsexpander.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>
......@@ -70,6 +72,8 @@ bool QtSupportPlugin::initialize(const QStringList &arguments, QString *errorMes
if (!MimeDatabase::addMimeTypes(QLatin1String(":qtsupport/QtSupport.mimetypes.xml"), errorMessage))
return false;
JsExpander::registerQObjectForJs(QLatin1String("QtSupport"), new CodeGenerator(this));
addAutoReleasedObject(new QtVersionManager);
addAutoReleasedObject(new DesktopQtVersionFactory);
addAutoReleasedObject(new SimulatorQtVersionFactory);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment