Newer
Older
/**************************************************************************
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
** 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.
** If you are unsure which license is appropriate for your use, please
**************************************************************************/
#include "formwindoweditor.h"
#include "formclasswizardpage.h"
#include "qtcreatorintegration.h"
#include "editordata.h"
#include "codemodelhelpers.h"
#include <widgethost.h>
#include <cpptools/cpptoolsconstants.h>
#include <cplusplus/InsertionPointLocator.h>
#include <cplusplus/Symbols.h>
#include <cplusplus/Overview.h>
#include <cplusplus/CoreTypes.h>
#include <cplusplus/Name.h>
#include <cplusplus/Names.h>
#include <cplusplus/Literals.h>
#include <cplusplus/Scope.h>
#include <cplusplus/Control.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/itexteditable.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <utils/qtcassert.h>
#include <QtDesigner/QDesignerFormEditorInterface>
#include <QtGui/QMessageBox>
#include <QtCore/QFileInfo>
using namespace Designer::Internal;
using namespace CPlusPlus;
using namespace TextEditor;
static QString msgClassNotFound(const QString &uiClassName, const QList<Document::Ptr> &docList)
{
QString files;
foreach (const Document::Ptr &doc, docList) {
if (!files.isEmpty())
files += QLatin1String(", ");
files += doc->fileName();
}
return QtCreatorIntegration::tr("The class definition of '%1' could not be found in %2.").arg(uiClassName, files);
static inline CppTools::CppModelManagerInterface *cppModelManagerInstance()
{
return ExtensionSystem::PluginManager::instance()
->getObject<CppTools::CppModelManagerInterface>();
QtCreatorIntegration::QtCreatorIntegration(QDesignerFormEditorInterface *core, FormEditorW *parent) :
qdesigner_internal::QDesignerIntegration(core, ::qobject_cast<QObject*>(parent)),
m_few(parent)
{
setResourceFileWatcherBehaviour(QDesignerIntegration::ReloadSilently);
setResourceEditingEnabled(false);
setSlotNavigationEnabled(true);
connect(this, SIGNAL(navigateToSlot(QString, QString, QStringList)),
this, SLOT(slotNavigateToSlot(QString, QString, QStringList)));
connect(this, SIGNAL(helpRequested(QString,QString)),
this, SLOT(slotDesignerHelpRequested(QString,QString)));
slotSyncSettingsToDesigner();
connect(Core::ICore::instance(), SIGNAL(saveSettingsRequested()),
this, SLOT(slotSyncSettingsToDesigner()));
}
void QtCreatorIntegration::slotDesignerHelpRequested(const QString &manual, const QString &document)
{
// Pass on as URL.
emit creatorHelpRequested(QUrl(QString::fromLatin1("qthelp://com.trolltech.%1/qdoc/%2")
.arg(manual, document)));
void QtCreatorIntegration::updateSelection()
if (const EditorData ed = m_few->activeEditor())
ed.widgetHost->updateFormWindowSelectionHandles(true);
qdesigner_internal::QDesignerIntegration::updateSelection();
}
QWidget *QtCreatorIntegration::containerWindow(QWidget * /*widget*/) const
if (const EditorData ed = m_few->activeEditor())
return ed.widgetHost->integrationContainer();
static QList<Document::Ptr> findDocumentsIncluding(const CPlusPlus::Snapshot &docTable,
const QString &fileName, bool checkFileNameOnly)
foreach (const Document::Ptr &doc, docTable) { // we go through all documents
const QStringList includes = doc->includedFiles();
foreach (const QString &include, includes) {
if (checkFileNameOnly) {
const QFileInfo fi(include);
if (fi.fileName() == fileName) { // we are only interested in docs which includes fileName only
docList.append(doc);
}
} else {
if (include == fileName)
docList.append(doc);
// Does klass inherit baseClass?
static bool inherits(const Overview &o, const Class *klass, const QString &baseClass)
{
const int baseClassCount = klass->baseClassCount();
for (int b = 0; b < baseClassCount; b++)
if (o.prettyName(klass->baseClassAt(b)->name()) == baseClass)
return true;
return false;
}
// Check for a class name where haystack is a member class of an object.
// So, haystack can be shorter (can have some namespaces omitted because of a
// "using namespace" declaration, for example, comparing
// "foo::Ui::form", against "using namespace foo; Ui::form".
static bool matchMemberClassName(const QString &needle, const QString &hayStack)
{
if (needle == hayStack)
return true;
if (!needle.endsWith(hayStack))
return false;
// Check if there really is a separator "::"
const int separatorPos = needle.size() - hayStack.size() - 1;
return separatorPos > 1 && needle.at(separatorPos) == QLatin1Char(':');
}
// Find class definition in namespace (that is, the outer class
// containing a member of the desired class type) or inheriting the desired class
// in case of forms using the Multiple Inheritance approach
static const Class *findClass(const Namespace *parentNameSpace, const QString &className, QString *namespaceName)
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << className;
const Overview o;
const unsigned namespaceMemberCount = parentNameSpace->memberCount();
for (unsigned i = 0; i < namespaceMemberCount; i++) { // we go through all namespace members
const Symbol *sym = parentNameSpace->memberAt(i);
// we have found a class - we are interested in classes only
if (const Class *cl = sym->asClass()) {
// 1) we go through class members
const unsigned classMemberCount = cl->memberCount();
for (unsigned j = 0; j < classMemberCount; j++)
if (const Declaration *decl = cl->memberAt(j)->asDeclaration()) {
// we want to know if the class contains a member (so we look into
// a declaration) of uiClassName type
const NamedType *nt = decl->type()->asNamedType();
// handle pointers to member variables
if (PointerType *pt = decl->type()->asPointerType())
nt = pt->elementType()->asNamedType();
if (nt && matchMemberClassName(className, o.prettyName(nt->name())))
// 2) does it inherit the desired class
if (inherits(o, cl, className))
return cl;
} else {
// Check namespaces
if (const Namespace *ns = sym->asNamespace()) {
QString tempNS = *namespaceName;
tempNS += o.prettyName(ns->name());
tempNS += QLatin1String("::");
if (const Class *cl = findClass(ns, className, &tempNS)) {
*namespaceName = tempNS;
return cl;
} // member is namespave
} // member is no class
} // for members

Roberto Raggi
committed
static Function *findDeclaration(const Class *cl, const QString &functionName)
const QString funName = QString::fromUtf8(QMetaObject::normalizedSignature(functionName.toUtf8()));
const unsigned mCount = cl->memberCount();
// we are interested only in declarations (can be decl of method or of a field)
// we are only interested in declarations of methods
const Overview overview;
for (unsigned j = 0; j < mCount; j++) { // go through all members

Roberto Raggi
committed
if (Declaration *decl = cl->memberAt(j)->asDeclaration())
if (Function *fun = decl->type()->asFunctionType()) {
// Format signature
QString memberFunction = overview.prettyName(fun->name());
memberFunction += QLatin1Char('(');
const uint aCount = fun->argumentCount();
for (uint i = 0; i < aCount; i++) { // we build argument types string
const Argument *arg = fun->argumentAt(i)->asArgument();
if (i > 0)
memberFunction += QLatin1Char(',');
memberFunction += overview.prettyType(arg->type());
}
memberFunction += QLatin1Char(')');
// we compare normalized signatures
memberFunction = QString::fromUtf8(QMetaObject::normalizedSignature(memberFunction.toUtf8()));
if (memberFunction == funName) // we match function names and argument lists
return fun;
}
}
return 0;
}
// TODO: remove me, this is taken from cppeditor.cpp. Find some common place for this method

Roberto Raggi
committed
static Document::Ptr findDefinition(Function *functionDeclaration, int *line)

Roberto Raggi
committed
if (CppTools::CppModelManagerInterface *cppModelManager = cppModelManagerInstance()) {
const Snapshot snapshot = cppModelManager->snapshot();

Roberto Raggi
committed
if (Symbol *def = snapshot.findMatchingDefinition(functionDeclaration)) {
if (line)
*line = def->line();

Roberto Raggi
committed
return snapshot.document(QString::fromUtf8(def->fileName(), def->fileNameLength()));
static inline ITextEditable *editableAt(const QString &fileName, int line, int column)
{
return qobject_cast<ITextEditable *>(TextEditor::BaseTextEditor::openEditorAt(fileName, line, column));
}
static void addDeclaration(Document::Ptr doc, const Class *cl, const QString &functionName)
QString declaration = QLatin1String("void ");
declaration += functionName;
declaration += QLatin1String(";\n");
InsertionPointLocator find(doc);
const InsertionLocation loc = find.methodDeclarationInClass(cl, InsertionPointLocator::PrivateSlot);
//
//! \todo change this to use the Refactoring changes.
//
if (ITextEditable *editable = editableAt(docFileName, loc.line(), loc.column() - 1)) {
BaseTextEditor *editor = qobject_cast<BaseTextEditor *>(editable->widget());
if (editor) {
QTextCursor tc = editor->textCursor();
int pos = tc.position();
tc.beginEditBlock();
tc.insertText(loc.prefix() + declaration + loc.suffix());
tc.setPosition(pos, QTextCursor::KeepAnchor);
editor->indentInsertedText(tc);
tc.endEditBlock();
}
}
}
static Document::Ptr addDefinition(const CPlusPlus::Snapshot &docTable,
const QString &headerFileName, const QString &className,
const QString &functionName, int *line)
{
QString definition = QLatin1String("\nvoid ");
definition += className;
definition += QLatin1String("::");
definition += functionName;
definition += QLatin1String("\n{\n");
definition += QString(indentation, QLatin1Char(' '));
definition += QLatin1String("\n}\n");
// we find all documents which include headerFileName
const QList<Document::Ptr> docList = findDocumentsIncluding(docTable, headerFileName, false);
if (docList.isEmpty())
return Document::Ptr();
QFileInfo headerFI(headerFileName);
const QString headerBaseName = headerFI.completeBaseName();
foreach (const Document::Ptr &doc, docList) {
const QFileInfo sourceFI(doc->fileName());
// we take only those documents which have the same filename
if (headerBaseName == sourceFI.baseName()) {
//! \todo change this to use the Refactoring changes.
if (ITextEditable *editable = editableAt(doc->fileName(), 0, 0)) {
//! \todo use the InsertionPointLocator to insert at the correct place.
// (we'll have to extend that class first to do definition insertions)
const QString contents = editable->contents();
int column;
editable->convertPosition(contents.length(), line, &column);
editable->gotoLine(*line, column);
*line += 1;
}
return doc;
}
}
return Document::Ptr();
// Insert the parameter names into a signature, "void foo(bool)" ->
// "void foo(bool checked)"
static QString addParameterNames(const QString &functionSignature, const QStringList ¶meterNames)
{
const int firstParen = functionSignature.indexOf(QLatin1Char('('));
QString functionName = functionSignature.left(firstParen + 1);
QString argumentsString = functionSignature.mid(firstParen + 1);
const int lastParen = argumentsString.lastIndexOf(QLatin1Char(')'));
if (lastParen != -1)
argumentsString.truncate(lastParen);
const QStringList arguments = argumentsString.split(QLatin1Char(','), QString::SkipEmptyParts);
const int pCount = parameterNames.count();
const int aCount = arguments.count();
for (int i = 0; i < aCount; ++i) {
if (i > 0)
functionName += QLatin1String(", ");
functionName += arguments.at(i);
if (i < pCount) {
functionName += QLatin1Char(' ');
functionName += parameterNames.at(i);
}
}
functionName += QLatin1Char(')');
return functionName;
}
// Recursively find a class definition in the document passed on or in its
// included files (going down [maxIncludeDepth] includes) and return a pair
// of <Class*, Document>.
typedef QPair<const Class *, Document::Ptr> ClassDocumentPtrPair;
static ClassDocumentPtrPair
findClassRecursively(const CPlusPlus::Snapshot &docTable,
const Document::Ptr &doc, const QString &className,
unsigned maxIncludeDepth, QString *namespaceName)
{
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << doc->fileName() << className << maxIncludeDepth;
// Check document
if (const Class *cl = findClass(doc->globalNamespace(), className, namespaceName))
return ClassDocumentPtrPair(cl, doc);
if (maxIncludeDepth) {
// Check the includes
const unsigned recursionMaxIncludeDepth = maxIncludeDepth - 1u;
foreach (const QString &include, doc->includedFiles()) {
const CPlusPlus::Snapshot::const_iterator it = docTable.find(include);
if (it != docTable.end()) {
const Document::Ptr includeDoc = it.value();
const ClassDocumentPtrPair irc = findClassRecursively(docTable, it.value(), className, recursionMaxIncludeDepth, namespaceName);
if (irc.first)
return irc;
}
}
}
return ClassDocumentPtrPair(0, Document::Ptr());
}
void QtCreatorIntegration::slotNavigateToSlot(const QString &objectName, const QString &signalSignature,
const QStringList ¶meterNames)
{
QString errorMessage;
if (!navigateToSlot(objectName, signalSignature, parameterNames, &errorMessage) && !errorMessage.isEmpty()) {
QMessageBox::warning(m_few->designerEditor()->topLevel(), tr("Error finding/adding a slot."), errorMessage);
// Build name of the class as generated by uic, insert Ui namespace
// "foo::bar::form" -> "foo::bar::Ui::form"
static inline QString uiClassName(QString formObjectName)
{
const int indexOfScope = formObjectName.lastIndexOf(QLatin1String("::"));
const int uiNameSpaceInsertionPos = indexOfScope >= 0 ? indexOfScope + 2 : 0;
formObjectName.insert(uiNameSpaceInsertionPos, QLatin1String("Ui::"));
return formObjectName;
}
static Document::Ptr getParsedDocument(const QString &fileName, CppTools::CppModelManagerInterface::WorkingCopy &workingCopy, Snapshot &snapshot)
{
QString src;
if (workingCopy.contains(fileName)) {
src = workingCopy.source(fileName);
} else {
QFile file(fileName);
if (file.open(QFile::ReadOnly))
src = QTextStream(&file).readAll(); // ### FIXME
}
QByteArray source = snapshot.preprocessedCode(src, fileName);
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
doc->check();
return doc;
}
// Goto slot invoked by the designer context menu. Either navigates
// to an existing slot function or create a new one.
bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
const QString &signalSignature,
const QStringList ¶meterNames,
QString *errorMessage)
const EditorData ed = m_few->activeEditor();
QTC_ASSERT(ed, return false)
const QString currentUiFile = ed.formWindowEditor->file()->fileName();
#if 0
return Designer::Internal::navigateToSlot(currentUiFile, objectName, signalSignature, parameterNames, errorMessage);
#endif
// TODO: we should pass to findDocumentsIncluding an absolute path to generated .h file from ui.
// Currently we are guessing the name of ui_<>.h file and pass the file name only to the findDocumentsIncluding().
// The idea is that the .pro file knows if the .ui files is inside, and the .pro file knows it will
// be generating the ui_<>.h file for it, and the .pro file knows what the generated file's name and its absolute path will be.
// So we should somehow get that info from project manager (?)
const QFileInfo fi(currentUiFile);
const QString uicedName = QLatin1String("ui_") + fi.completeBaseName() + QLatin1String(".h");
// Retrieve code model snapshot restricted to project of ui file.
const ProjectExplorer::Project *uiProject = ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(currentUiFile);
if (!uiProject) {
*errorMessage = tr("Internal error: No project could be found for %1.").arg(currentUiFile);
return false;
}
CPlusPlus::Snapshot docTable = cppModelManagerInstance()->snapshot();
CPlusPlus::Snapshot newDocTable;
for (CPlusPlus::Snapshot::iterator it = docTable.begin(); it != docTable.end(); ++it) {
const ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->session()->projectForFile(it.key());
if (project == uiProject)
newDocTable.insert(it.value());
}
docTable = newDocTable;
// take all docs, find the ones that include the ui_xx.h.
QList<Document::Ptr> docList = findDocumentsIncluding(docTable, uicedName, true); // change to false when we know the absolute path to generated ui_<>.h file
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << objectName << signalSignature << "Looking for " << uicedName << " returned " << docList.size();
*errorMessage = tr("No documents matching '%1' could be found.\nRebuilding the project might help.").arg(uicedName);
QDesignerFormWindowInterface *fwi = ed.widgetHost->formWindow();
const QString uiClass = uiClassName(fwi->mainContainer()->objectName());
if (Designer::Constants::Internal::debug)
qDebug() << "Checking docs for " << uiClass;
// Find the class definition (ui class defined as member or base class)
// in the file itself or in the directly included files (order 1).
Document::Ptr doc;
foreach (const Document::Ptr &d, docList) {
const ClassDocumentPtrPair cd = findClassRecursively(docTable, d, uiClass, 1u , &namespaceName);
if (cd.first) {
cl = cd.first;
doc = cd.second;
break;
}
}
if (!cl) {
*errorMessage = msgClassNotFound(uiClass, docList);
return false;
}
Overview o;
const QString className = namespaceName + o.prettyName(cl->name());
if (Designer::Constants::Internal::debug)
qDebug() << "Found class " << className << doc->fileName();
const QString functionName = QLatin1String("on_") + objectName + QLatin1Char('_') + signalSignature;
const QString functionNameWithParameterNames = addParameterNames(functionName, parameterNames);
if (Designer::Constants::Internal::debug)
qDebug() << Q_FUNC_INFO << "Found " << uiClass << doc->fileName() << " checking " << functionName << functionNameWithParameterNames;
int line = 0;
Document::Ptr sourceDoc;

Roberto Raggi
committed
if (Function *fun = findDeclaration(cl, functionName)) {
sourceDoc = findDefinition(fun, &line);
if (!sourceDoc) {
// add function definition to cpp file
sourceDoc = addDefinition(docTable, doc->fileName(), className, functionNameWithParameterNames, &line);
} else {
// add function declaration to cl
CppTools::CppModelManagerInterface::WorkingCopy workingCopy = cppModelManagerInstance()->workingCopy();
Document::Ptr tmpDoc = getParsedDocument(doc->fileName(), workingCopy, docTable);
addDeclaration(tmpDoc, cl, functionNameWithParameterNames);
// add function definition to cpp file
sourceDoc = addDefinition(docTable, doc->fileName(), className, functionNameWithParameterNames, &line);
if (!sourceDoc) {
*errorMessage = tr("Unable to add the method definition.");
return false;
}
// jump to function definition, position within code
TextEditor::BaseTextEditor::openEditorAt(sourceDoc->fileName(), line + 2, indentation);
return true;
}
void QtCreatorIntegration::slotSyncSettingsToDesigner()
{
#if QT_VERSION >= 0x040800
// Set promotion-relevant parameters on integration.
const Core::MimeDatabase *mdb = Core::ICore::instance()->mimeDatabase();
setHeaderSuffix(mdb->preferredSuffixByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)));
setHeaderLowercase(FormClassWizardPage::lowercaseHeaderFiles());
#endif
}