OSDN Git Service

c93f49a2a0cd9a7acf985f75992a7a9efe817fa5
[qt-creator-jp/qt-creator-jp.git] / src / plugins / designer / codemodelhelpers.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "codemodelhelpers.h"
35
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>
45
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>
51
52 #include <QtCore/QCoreApplication>
53 #include <QtCore/QDebug>
54
55 // Debug helpers for code model. @todo: Move to some CppTools library?
56
57 typedef QMap<QString, QStringList> DependencyMap;
58 typedef CPlusPlus::Document::Ptr DocumentPtr;
59 typedef QList<CPlusPlus::Symbol *> SymbolList;
60 typedef QList<DocumentPtr> DocumentPtrList;
61
62 static const char setupUiC[] = "setupUi";
63
64 // Find the generated "ui_form.h" header of the form via project.
65 static QString generatedHeaderOf(const QString &uiFileName)
66 {
67     const ProjectExplorer::SessionManager *sessionMgr = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
68     if (const ProjectExplorer::Project *uiProject = sessionMgr->projectForFile(uiFileName))
69         return uiProject->generatedUiHeader(uiFileName);
70     return QString();
71 }
72
73 namespace {
74 // Find function symbols in a document by name.
75 class SearchFunction : public CPlusPlus::SymbolVisitor {
76 public:
77     typedef QList<CPlusPlus::Function *> FunctionList;
78
79     explicit SearchFunction(const char *name);
80     FunctionList operator()(const DocumentPtr &doc);
81
82     virtual bool visit(CPlusPlus::Function * f);
83
84 private:
85     const size_t m_length;
86     const char *m_name;
87
88     FunctionList m_matches;
89 };
90
91 SearchFunction::SearchFunction(const char *name) :
92     m_length(qstrlen(name)),
93     m_name(name)
94 {
95 }
96
97 SearchFunction::FunctionList SearchFunction::operator()(const DocumentPtr &doc)
98 {
99     m_matches.clear();
100     const unsigned globalSymbolCount = doc->globalSymbolCount();
101     for (unsigned i = 0; i < globalSymbolCount; i++)
102         accept(doc->globalSymbolAt(i));
103     return m_matches;
104 }
105
106 bool SearchFunction::visit(CPlusPlus::Function * f)
107 {
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);
113     return true;
114 }
115
116 } // anonymous namespace
117
118 namespace Designer {
119 namespace Internal {
120
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)
128 {
129
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);
134         return false;
135     }
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);
140         return false;
141     }
142
143     // Look for setupUi
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);
148         return false;
149     }
150     return true;
151 }
152
153 } // namespace Internal
154 } // namespace Designer