Files
qt-creator/src/plugins/qt4projectmanager/wizards/libraryparameters.cpp

160 lines
6.1 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** 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
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "libraryparameters.h"
2008-12-02 16:19:05 +01:00
#include <utils/codegeneration.h>
#include <QTextStream>
#include <QStringList>
2008-12-02 12:01:29 +01:00
// Contents of the header defining the shared library export.
#define GUARD_VARIABLE "<GUARD>"
#define EXPORT_MACRO_VARIABLE "<EXPORT_MACRO>"
#define LIBRARY_MACRO_VARIABLE "<LIBRARY_MACRO>"
static const char *globalHeaderContentsC =
"#ifndef " GUARD_VARIABLE "\n"
"#define " GUARD_VARIABLE "\n"
2008-12-02 12:01:29 +01:00
"\n"
"#include <QtCore/qglobal.h>\n"
"\n"
"#if defined(" LIBRARY_MACRO_VARIABLE ")\n"
"# define " EXPORT_MACRO_VARIABLE " Q_DECL_EXPORT\n"
2008-12-02 12:01:29 +01:00
"#else\n"
"# define " EXPORT_MACRO_VARIABLE " Q_DECL_IMPORT\n"
2008-12-02 12:01:29 +01:00
"#endif\n"
"\n"
"#endif // " GUARD_VARIABLE "\n";
2008-12-02 12:01:29 +01:00
namespace Qt4ProjectManager {
namespace Internal {
void LibraryParameters::generateCode(QtProjectParameters:: Type t,
const QString &projectTarget,
const QString &headerName,
const QString &sharedHeader,
const QString &exportMacro,
int indentation,
QString *header,
QString *source) const
{
QString rc;
QTextStream headerStr(header);
const QString indent = QString(indentation, QLatin1Char(' '));
// Do we have namespaces?
QStringList namespaceList = className.split(QLatin1String("::"));
if (namespaceList.empty()) // Paranoia!
return;
const QString unqualifiedClassName = namespaceList.takeLast();
2008-12-02 12:01:29 +01:00
// 1) Header
const QString guard = Utils::headerGuard(headerFileName, namespaceList);
2008-12-02 12:01:29 +01:00
headerStr << "#ifndef " << guard
<< "\n#define " << guard << '\n' << '\n';
if (!sharedHeader.isEmpty())
Utils::writeIncludeFileDirective(sharedHeader, false, headerStr);
2008-12-02 12:01:29 +01:00
// include base class header
if (!baseClassName.isEmpty()) {
QString include;
if (!baseClassModule.isEmpty()) {
include += baseClassModule;
include += QLatin1Char('/');
}
include += baseClassName;
Utils::writeIncludeFileDirective(include, true, headerStr);
2008-12-02 12:01:29 +01:00
headerStr << '\n';
}
const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList, indent, headerStr);
2008-12-02 12:01:29 +01:00
// Class declaraction
headerStr << '\n' << namespaceIndent << "class ";
if (t == QtProjectParameters::SharedLibrary && !exportMacro.isEmpty())
headerStr << exportMacro << ' ';
headerStr << unqualifiedClassName;
if (!baseClassName.isEmpty())
headerStr << " : public " << baseClassName;
headerStr << "\n{\n";
2008-12-02 12:01:29 +01:00
// Is this a QObject (plugin)
const bool inheritsQObject = t == QtProjectParameters::Qt4Plugin;
if (inheritsQObject)
headerStr << namespaceIndent << indent << "Q_OBJECT\n";
2008-12-02 12:01:29 +01:00
headerStr << namespaceIndent << "public:\n";
if (inheritsQObject)
headerStr << namespaceIndent << indent << unqualifiedClassName << "(QObject *parent = 0);\n";
else
2008-12-02 12:01:29 +01:00
headerStr << namespaceIndent << indent << unqualifiedClassName << "();\n";
headerStr << namespaceIndent << "};\n\n";
Utils::writeClosingNameSpaces(namespaceList, indent, headerStr);
2008-12-02 12:01:29 +01:00
headerStr << "#endif // "<< guard << '\n';
/// 2) Source
QTextStream sourceStr(source);
Utils::writeIncludeFileDirective(headerName, false, sourceStr);
2008-12-02 12:01:29 +01:00
sourceStr << '\n';
Utils::writeOpeningNameSpaces(namespaceList, indent, sourceStr);
2008-12-02 12:01:29 +01:00
// Constructor
sourceStr << '\n' << namespaceIndent << unqualifiedClassName << "::" << unqualifiedClassName;
if (inheritsQObject) {
sourceStr << "(QObject *parent) :\n"
<< namespaceIndent << indent << baseClassName << "(parent)\n";
} else {
sourceStr << "()\n";
}
sourceStr << namespaceIndent << "{\n" << namespaceIndent << "}\n";
Utils::writeClosingNameSpaces(namespaceList, indent, sourceStr);
2008-12-02 12:01:29 +01:00
if (t == QtProjectParameters::Qt4Plugin)
sourceStr << '\n' << "Q_EXPORT_PLUGIN2(" << projectTarget << ", " << className << ")\n";
}
QString LibraryParameters::generateSharedHeader(const QString &globalHeaderFileName,
const QString &projectTarget,
const QString &exportMacro)
{
QString contents = QLatin1String(globalHeaderContentsC);
contents.replace(QLatin1String(GUARD_VARIABLE), Utils::headerGuard(globalHeaderFileName));
2008-12-02 12:01:29 +01:00
contents.replace(QLatin1String(EXPORT_MACRO_VARIABLE), exportMacro);
contents.replace(QLatin1String(LIBRARY_MACRO_VARIABLE), QtProjectParameters::libraryMacro(projectTarget));
return contents;
}
2008-12-02 16:19:05 +01:00
} // namespace Internal
} // namespace Qt4ProjectManager