OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / analyzerbase / analyzerruncontrol.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 ** Author: Andreas Hartmetz, KDAB (andreas.hartmetz@kdab.com)
8 **
9 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 **
11 **
12 ** GNU Lesser General Public License Usage
13 **
14 ** This file may be used under the terms of the GNU Lesser General Public
15 ** License version 2.1 as published by the Free Software Foundation and
16 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
17 ** Please review the following information to ensure the GNU Lesser General
18 ** Public License version 2.1 requirements will be met:
19 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 **
21 ** In addition, as a special exception, Nokia gives you certain additional
22 ** rights. These rights are described in the Nokia Qt LGPL Exception
23 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
24 **
25 ** Other Usage
26 **
27 ** Alternatively, this file may be used in accordance with the terms and
28 ** conditions contained in a signed written agreement between you and Nokia.
29 **
30 ** If you have questions regarding the use of this file, please contact
31 ** Nokia at qt-info@nokia.com.
32 **
33 **************************************************************************/
34
35 #include "analyzerruncontrol.h"
36 #include "analyzerconstants.h"
37 #include "ianalyzerengine.h"
38 #include "ianalyzertool.h"
39 #include "analyzerplugin.h"
40 #include "analyzermanager.h"
41 #include "analyzerrunconfigwidget.h"
42 #include "analyzersettings.h"
43
44 #include <extensionsystem/pluginmanager.h>
45 #include <projectexplorer/applicationrunconfiguration.h>
46 #include <projectexplorer/projectexplorerconstants.h>
47 #include <projectexplorer/task.h>
48 #include <projectexplorer/taskhub.h>
49 #include <coreplugin/ioutputpane.h>
50
51 #include <QtCore/QDebug>
52 #include <QtGui/QHBoxLayout>
53 #include <QtGui/QLabel>
54 #include <QtGui/QMessageBox>
55
56 using namespace Analyzer;
57 using namespace Analyzer::Internal;
58
59 // AnalyzerRunControlFactory ////////////////////////////////////////////////////
60 AnalyzerRunControlFactory::AnalyzerRunControlFactory(QObject *parent)
61     : IRunControlFactory(parent)
62 {
63 }
64
65 bool AnalyzerRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
66 {
67     if (!qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration *>(runConfiguration))
68         return false;
69     return mode == Constants::MODE_ANALYZE;
70 }
71
72 ProjectExplorer::RunControl *AnalyzerRunControlFactory::create(RunConfiguration *runConfiguration,
73                                                                const QString &mode)
74 {
75     if (!qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration *>(runConfiguration) ||
76          mode != Constants::MODE_ANALYZE) {
77         return 0;
78     }
79     AnalyzerRunControl *rc = new AnalyzerRunControl(runConfiguration);
80     emit runControlCreated(rc);
81     return rc;
82 }
83
84 QString AnalyzerRunControlFactory::displayName() const
85 {
86     return tr("Analyzer");
87 }
88
89 ProjectExplorer::IRunConfigurationAspect *AnalyzerRunControlFactory::createRunConfigurationAspect()
90 {
91     return new AnalyzerProjectSettings;
92 }
93
94 ProjectExplorer::RunConfigWidget *AnalyzerRunControlFactory::createConfigurationWidget(RunConfiguration
95                                                                                        *runConfiguration)
96 {
97     ProjectExplorer::LocalApplicationRunConfiguration *localRc =
98         qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration *>(runConfiguration);
99     if (!localRc)
100         return 0;
101     AnalyzerProjectSettings *settings = runConfiguration->extraAspect<AnalyzerProjectSettings>();
102     if (!settings)
103         return 0;
104
105     AnalyzerRunConfigWidget *ret = new AnalyzerRunConfigWidget;
106     ret->setRunConfiguration(runConfiguration);
107     return ret;
108 }
109
110 // AnalyzerRunControl ////////////////////////////////////////////////////
111 AnalyzerRunControl::AnalyzerRunControl(RunConfiguration *runConfiguration)
112     : RunControl(runConfiguration, Constants::MODE_ANALYZE),
113       m_isRunning(false),
114       m_engine(0)
115 {
116     IAnalyzerTool *tool = AnalyzerManager::instance()->currentTool();
117     m_engine = tool->createEngine(runConfiguration);
118
119     connect(m_engine, SIGNAL(standardErrorReceived(QString)),
120             SLOT(receiveStandardError(QString)));
121     connect(m_engine, SIGNAL(standardOutputReceived(QString)),
122             SLOT(receiveStandardOutput(QString)));
123     connect(m_engine, SIGNAL(taskToBeAdded(ProjectExplorer::Task::TaskType,QString,QString,int)),
124             SLOT(addTask(ProjectExplorer::Task::TaskType,QString,QString,int)));
125     connect(m_engine, SIGNAL(finished()),
126             SLOT(engineFinished()));
127 }
128
129 AnalyzerRunControl::~AnalyzerRunControl()
130 {
131     if (m_isRunning)
132         stop();
133 }
134
135 void AnalyzerRunControl::start()
136 {
137     // clear about-to-be-outdated tasks
138     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
139     ProjectExplorer::TaskHub *hub = pm->getObject<ProjectExplorer::TaskHub>();
140     hub->clearTasks(Constants::ANALYZERTASK_ID);
141
142     m_isRunning = true;
143     emit started();
144     m_engine->start();
145 }
146
147 ProjectExplorer::RunControl::StopResult AnalyzerRunControl::stop()
148 {
149     m_engine->stop();
150     m_isRunning = false;
151     return AsynchronousStop;
152 }
153
154 void AnalyzerRunControl::engineFinished()
155 {
156     m_isRunning = false;
157     emit finished();
158 }
159
160 bool AnalyzerRunControl::isRunning() const
161 {
162     return m_isRunning;
163 }
164
165 QString AnalyzerRunControl::displayName() const
166 {
167     return AnalyzerManager::instance()->currentTool()->displayName();
168 }
169
170 void AnalyzerRunControl::receiveStandardOutput(const QString &text)
171 {
172     appendMessage(text, ProjectExplorer::StdOutFormat);
173 }
174
175 void AnalyzerRunControl::receiveStandardError(const QString &text)
176 {
177     appendMessage(text, ProjectExplorer::StdErrFormat);
178 }
179
180 void AnalyzerRunControl::addTask(ProjectExplorer::Task::TaskType type, const QString &description,
181                                  const QString &file, int line)
182 {
183     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
184     ProjectExplorer::TaskHub *hub = pm->getObject<ProjectExplorer::TaskHub>();
185     hub->addTask(ProjectExplorer::Task(type, description, file, line, Constants::ANALYZERTASK_ID));
186
187     ///FIXME: get a better API for this into Qt Creator
188     QList<Core::IOutputPane *> panes = pm->getObjects<Core::IOutputPane>();
189     foreach (Core::IOutputPane *pane, panes) {
190         if (pane->displayName() == tr("Build Issues")) {
191             pane->popup(false);
192             break;
193         }
194     }
195 }