1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "codemodelhelpers.h"
36 #include <cplusplus/ModelManagerInterface.h>
37 #include <cplusplus/Symbols.h>
38 #include <cplusplus/CoreTypes.h>
39 #include <cplusplus/Name.h>
40 #include <cplusplus/Names.h>
41 #include <cplusplus/Literals.h>
42 #include <cplusplus/Scope.h>
43 #include <cplusplus/Control.h>
44 #include <SymbolVisitor.h>
46 #include <coreplugin/icore.h>
47 #include <projectexplorer/projectexplorer.h>
48 #include <projectexplorer/project.h>
49 #include <projectexplorer/session.h>
50 #include <utils/qtcassert.h>
52 #include <QtCore/QCoreApplication>
53 #include <QtCore/QDebug>
55 // Debug helpers for code model. @todo: Move to some CppTools library?
57 typedef QMap<QString, QStringList> DependencyMap;
58 typedef CPlusPlus::Document::Ptr DocumentPtr;
59 typedef QList<CPlusPlus::Symbol *> SymbolList;
60 typedef QList<DocumentPtr> DocumentPtrList;
62 static const char setupUiC[] = "setupUi";
64 // Find the generated "ui_form.h" header of the form via project.
65 static QString generatedHeaderOf(const QString &uiFileName)
67 const ProjectExplorer::SessionManager *sessionMgr = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
68 if (const ProjectExplorer::Project *uiProject = sessionMgr->projectForFile(uiFileName))
69 return uiProject->generatedUiHeader(uiFileName);
74 // Find function symbols in a document by name.
75 class SearchFunction : public CPlusPlus::SymbolVisitor {
77 typedef QList<CPlusPlus::Function *> FunctionList;
79 explicit SearchFunction(const char *name);
80 FunctionList operator()(const DocumentPtr &doc);
82 virtual bool visit(CPlusPlus::Function * f);
85 const size_t m_length;
88 FunctionList m_matches;
91 SearchFunction::SearchFunction(const char *name) :
92 m_length(qstrlen(name)),
97 SearchFunction::FunctionList SearchFunction::operator()(const DocumentPtr &doc)
100 const unsigned globalSymbolCount = doc->globalSymbolCount();
101 for (unsigned i = 0; i < globalSymbolCount; i++)
102 accept(doc->globalSymbolAt(i));
106 bool SearchFunction::visit(CPlusPlus::Function * f)
108 if (const CPlusPlus::Name *name = f->name())
109 if (const CPlusPlus::Identifier *id = name->identifier())
110 if (id->size() == m_length)
111 if (!qstrncmp(m_name, id->chars(), m_length))
112 m_matches.push_back(f);
116 } // anonymous namespace
121 // Goto slot invoked by the designer context menu. Either navigates
122 // to an existing slot function or create a new one.
123 bool navigateToSlot(const QString &uiFileName,
124 const QString & /* objectName */,
125 const QString & /* signalSignature */,
126 const QStringList & /* parameterNames */,
127 QString *errorMessage)
130 // Find the generated header.
131 const QString generatedHeaderFile = generatedHeaderOf(uiFileName);
132 if (generatedHeaderFile.isEmpty()) {
133 *errorMessage = QCoreApplication::translate("Designer", "The generated header of the form '%1' could not be found.\nRebuilding the project might help.").arg(uiFileName);
136 const CPlusPlus::Snapshot snapshot = CPlusPlus::CppModelManagerInterface::instance()->snapshot();
137 const DocumentPtr generatedHeaderDoc = snapshot.document(generatedHeaderFile);
138 if (!generatedHeaderDoc) {
139 *errorMessage = QCoreApplication::translate("Designer", "The generated header '%1' could not be found in the code model.\nRebuilding the project might help.").arg(generatedHeaderFile);
144 SearchFunction searchFunc(setupUiC);
145 const SearchFunction::FunctionList funcs = searchFunc(generatedHeaderDoc);
146 if (funcs.size() != 1) {
147 *errorMessage = QString::fromLatin1("Internal error: The function '%1' could not be found in in %2").arg(QLatin1String(setupUiC), generatedHeaderFile);
153 } // namespace Internal
154 } // namespace Designer