analyzermanager.cpp 30.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Mike McQuaid's avatar
Mike McQuaid committed
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
Mike McQuaid's avatar
Mike McQuaid committed
5
6
** Author: Andreas Hartmetz, KDAB (andreas.hartmetz@kdab.com)
**
hjk's avatar
hjk committed
7
** This file is part of Qt Creator.
Mike McQuaid's avatar
Mike McQuaid committed
8
**
hjk's avatar
hjk committed
9
10
11
12
13
14
15
** 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.
Mike McQuaid's avatar
Mike McQuaid committed
16
17
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
18
19
20
21
22
23
24
25
26
** 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
Mike McQuaid's avatar
Mike McQuaid committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Mike McQuaid's avatar
Mike McQuaid committed
30
31
32

#include "analyzermanager.h"

33
#include "analyzerconstants.h"
Mike McQuaid's avatar
Mike McQuaid committed
34
35
36
#include "analyzerplugin.h"
#include "analyzerruncontrol.h"
#include "analyzeroptionspage.h"
37
38
39
#include "analyzerstartparameters.h"
#include "analyzerutils.h"
#include "ianalyzertool.h"
40
#include "analyzersettings.h"
Mike McQuaid's avatar
Mike McQuaid committed
41

hjk's avatar
hjk committed
42
#include <coreplugin/coreconstants.h>
Mike McQuaid's avatar
Mike McQuaid committed
43
#include <coreplugin/findplaceholder.h>
hjk's avatar
hjk committed
44
45
#include <coreplugin/icore.h>
#include <coreplugin/imode.h>
Mike McQuaid's avatar
Mike McQuaid committed
46
47
48
49
50
#include <coreplugin/minisplitter.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/navigationwidget.h>
#include <coreplugin/outputpane.h>
#include <coreplugin/rightpane.h>
51
#include <coreplugin/id.h>
hjk's avatar
hjk committed
52
53
54
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
55
#include <coreplugin/editormanager/editormanager.h>
Mike McQuaid's avatar
Mike McQuaid committed
56
57
58
59
60
61
62
63

#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/iprojectmanager.h>
#include <projectexplorer/target.h>
64
#include <projectexplorer/session.h>
Mike McQuaid's avatar
Mike McQuaid committed
65
66
67
68
69
70

#include <extensionsystem/iplugin.h>

#include <utils/fancymainwindow.h>
#include <utils/styledbar.h>
#include <utils/qtcassert.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
71
#include <utils/checkablemessagebox.h>
72
#include <utils/statuslabel.h>
73
#include <ssh/sshconnection.h>
Mike McQuaid's avatar
Mike McQuaid committed
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <QVariant>
#include <QDebug>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDockWidget>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolButton>
#include <QComboBox>
#include <QStackedWidget>
#include <QDialog>
#include <QApplication>
#include <QLabel>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QPushButton>
Mike McQuaid's avatar
Mike McQuaid committed
92
93

using namespace Core;
hjk's avatar
hjk committed
94
using namespace Core::Constants;
Mike McQuaid's avatar
Mike McQuaid committed
95
using namespace Analyzer::Internal;
hjk's avatar
hjk committed
96
using namespace Analyzer::Constants;
hjk's avatar
hjk committed
97
using namespace ProjectExplorer;
Mike McQuaid's avatar
Mike McQuaid committed
98
99
100
101

namespace Analyzer {
namespace Internal {

102
const char LAST_ACTIVE_TOOL[] = "Analyzer.Plugin.LastActiveTool";
hjk's avatar
hjk committed
103
const char INITIAL_DOCK_AREA[] = "initial_dock_area";
hjk's avatar
hjk committed
104

hjk's avatar
hjk committed
105
106
107
108
109
////////////////////////////////////////////////////////////////////
//
// AnalyzerMode
//
////////////////////////////////////////////////////////////////////
Mike McQuaid's avatar
Mike McQuaid committed
110

hjk's avatar
hjk committed
111
class AnalyzerMode : public IMode
112
113
114
115
116
{
    Q_OBJECT

public:
    AnalyzerMode(QObject *parent = 0)
hjk's avatar
hjk committed
117
        : IMode(parent)
118
    {
hjk's avatar
hjk committed
119
        setContext(Context(C_EDITORMANAGER, C_ANALYZEMODE, C_NAVIGATION_PANE));
120
        setDisplayName(tr("Analyze"));
121
        setIcon(QIcon(QLatin1String(":/images/analyzer_mode.png")));
hjk's avatar
hjk committed
122
        setPriority(P_MODE_ANALYZE);
123
124
        setId(MODE_ANALYZE);
        setType(MODE_EDIT_TYPE);
125
    }
126
127
128
129

    ~AnalyzerMode()
    {
        // Make sure the editor manager does not get deleted.
hjk's avatar
hjk committed
130
131
132
        delete m_widget;
        m_widget = 0;
        EditorManager::instance()->setParent(0);
133
134
135
    }
};

Mike McQuaid's avatar
Mike McQuaid committed
136
137
} // namespace Internal

hjk's avatar
hjk committed
138
139
140
141
142
143
////////////////////////////////////////////////////////////////////
//
// AnalyzerManagerPrivate
//
////////////////////////////////////////////////////////////////////

144
class AnalyzerManagerPrivate : public QObject
Mike McQuaid's avatar
Mike McQuaid committed
145
{
hjk's avatar
hjk committed
146
147
    Q_OBJECT

Mike McQuaid's avatar
Mike McQuaid committed
148
public:
149
150
    typedef QHash<QString, QVariant> FancyMainWindowSettings;

Mike McQuaid's avatar
Mike McQuaid committed
151
152
153
    AnalyzerManagerPrivate(AnalyzerManager *qq);
    ~AnalyzerManagerPrivate();

154
    /**
hjk's avatar
hjk committed
155
     * After calling this, a proper instance of IMode is initialized
156
157
158
159
160
161
     * It is delayed since an analyzer mode makes no sense without analyzer tools
     *
     * \note Call this before adding a tool to the manager
     */
    void delayedInit();

Mike McQuaid's avatar
Mike McQuaid committed
162
    void setupActions();
hjk's avatar
hjk committed
163
    void createModeMainWindow();
hjk's avatar
hjk committed
164
165
    bool showPromptDialog(const QString &title, const QString &text,
        const QString &stopButtonText, const QString &cancelButtonText) const;
Mike McQuaid's avatar
Mike McQuaid committed
166

167
168
    void activateDock(Qt::DockWidgetArea area, QDockWidget *dockWidget);
    void deactivateDock(QDockWidget *dockWidget);
169
    void addTool(IAnalyzerTool *tool, const StartModes &modes);
170
    void selectSavedTool();
171
    void selectTool(IAnalyzerTool *tool, StartMode mode);
hjk's avatar
hjk committed
172
173
    void handleToolStarted();
    void handleToolFinished();
174
175
176
    void saveToolSettings(IAnalyzerTool *tool, StartMode mode);
    void loadToolSettings(IAnalyzerTool *tool);
    QAction *actionFromToolAndMode(IAnalyzerTool *tool, StartMode mode);
177

178
    // Convenience.
179
    void startLocalTool(IAnalyzerTool *tool);
180
    bool isActionRunnable(QAction *action) const;
181

hjk's avatar
hjk committed
182
public slots:
183
    void startTool();
hjk's avatar
hjk committed
184
185
    void selectToolboxAction(int);
    void selectMenuAction();
186
    void modeChanged(Core::IMode *mode);
hjk's avatar
hjk committed
187
188
    void resetLayout();
    void updateRunActions();
Mike McQuaid's avatar
Mike McQuaid committed
189

hjk's avatar
hjk committed
190
public:
Mike McQuaid's avatar
Mike McQuaid committed
191
192
    AnalyzerManager *q;
    AnalyzerMode *m_mode;
hjk's avatar
hjk committed
193
    bool m_isRunning;
Mike McQuaid's avatar
Mike McQuaid committed
194
    Utils::FancyMainWindow *m_mainWindow;
195
    IAnalyzerTool *m_currentTool;
196
    StartMode m_currentMode;
197
    QAction *m_currentAction;
198
    QHash<QAction *, IAnalyzerTool *> m_toolFromAction;
199
    QHash<QAction *, StartMode> m_modeFromAction;
200
    QList<IAnalyzerTool *> m_tools;
201
    QList<QAction *> m_actions;
Mike McQuaid's avatar
Mike McQuaid committed
202
203
    QAction *m_startAction;
    QAction *m_stopAction;
204
    ActionContainer *m_menu;
Mike McQuaid's avatar
Mike McQuaid committed
205
    QComboBox *m_toolBox;
206
    QStackedWidget *m_controlsStackWidget;
207
    Utils::StatusLabel *m_statusLabel;
208
    typedef QMap<IAnalyzerTool *, FancyMainWindowSettings> MainWindowSettingsMap;
209
    QHash<IAnalyzerTool *, QList<QDockWidget *> > m_toolWidgets;
210
    QHash<IAnalyzerTool *, QWidget *> m_controlsWidgetFromTool;
211
    MainWindowSettingsMap m_defaultSettings;
Milian Wolff's avatar
Milian Wolff committed
212
213
214
215

    // list of dock widgets to prevent memory leak
    typedef QWeakPointer<QDockWidget> DockPtr;
    QList<DockPtr> m_dockWidgets;
Mike McQuaid's avatar
Mike McQuaid committed
216
217
};

218
AnalyzerManagerPrivate::AnalyzerManagerPrivate(AnalyzerManager *qq):
Mike McQuaid's avatar
Mike McQuaid committed
219
220
    q(qq),
    m_mode(0),
hjk's avatar
hjk committed
221
    m_isRunning(false),
Mike McQuaid's avatar
Mike McQuaid committed
222
    m_mainWindow(0),
223
    m_currentTool(0),
224
    m_currentMode(),
225
    m_currentAction(0),
Mike McQuaid's avatar
Mike McQuaid committed
226
227
228
    m_startAction(0),
    m_stopAction(0),
    m_menu(0),
Friedemann Kleint's avatar
Friedemann Kleint committed
229
    m_toolBox(new QComboBox),
230
    m_controlsStackWidget(new QStackedWidget),
231
    m_statusLabel(new Utils::StatusLabel)
Mike McQuaid's avatar
Mike McQuaid committed
232
{
Friedemann Kleint's avatar
Friedemann Kleint committed
233
    m_toolBox->setObjectName(QLatin1String("AnalyzerManagerToolBox"));
234
    connect(m_toolBox, SIGNAL(activated(int)), SLOT(selectToolboxAction(int)));
Mike McQuaid's avatar
Mike McQuaid committed
235
236

    setupActions();
hjk's avatar
hjk committed
237

238
239
    connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*)),
            this, SLOT(modeChanged(Core::IMode*)));
hjk's avatar
hjk committed
240
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
hjk's avatar
hjk committed
241
    connect(pe, SIGNAL(updateRunActions()), SLOT(updateRunActions()));
Mike McQuaid's avatar
Mike McQuaid committed
242
243
}

244
AnalyzerManagerPrivate::~AnalyzerManagerPrivate()
Mike McQuaid's avatar
Mike McQuaid committed
245
{
Milian Wolff's avatar
Milian Wolff committed
246
247
    // as we have to setParent(0) on dock widget that are not selected,
    // we keep track of all and make sure we don't leak any
248
    foreach (const DockPtr &ptr, m_dockWidgets) {
Milian Wolff's avatar
Milian Wolff committed
249
250
251
        if (ptr)
            delete ptr.data();
    }
Mike McQuaid's avatar
Mike McQuaid committed
252
253
}

254
void AnalyzerManagerPrivate::setupActions()
Mike McQuaid's avatar
Mike McQuaid committed
255
{
hjk's avatar
hjk committed
256
257
    const Context globalcontext(C_GLOBAL);
    Command *command = 0;
258

Mike McQuaid's avatar
Mike McQuaid committed
259
    // Menus
Eike Ziller's avatar
Eike Ziller committed
260
    m_menu = Core::ActionManager::createMenu(M_DEBUG_ANALYZER);
hjk's avatar
hjk committed
261
    m_menu->menu()->setTitle(tr("&Analyze"));
262
263
    m_menu->menu()->setEnabled(true);

hjk's avatar
hjk committed
264
265
266
267
    m_menu->appendGroup(G_ANALYZER_CONTROL);
    m_menu->appendGroup(G_ANALYZER_TOOLS);
    m_menu->appendGroup(G_ANALYZER_REMOTE_TOOLS);
    m_menu->appendGroup(G_ANALYZER_OPTIONS);
268

Eike Ziller's avatar
Eike Ziller committed
269
270
    ActionContainer *menubar = Core::ActionManager::actionContainer(MENU_BAR);
    ActionContainer *mtools = Core::ActionManager::actionContainer(M_TOOLS);
hjk's avatar
hjk committed
271
    menubar->addMenu(mtools, m_menu);
Mike McQuaid's avatar
Mike McQuaid committed
272
273

    m_startAction = new QAction(tr("Start"), m_menu);
274
    m_startAction->setIcon(QIcon(QLatin1String(ANALYZER_CONTROL_START_ICON)));
Eike Ziller's avatar
Eike Ziller committed
275
    command = Core::ActionManager::registerAction(m_startAction, "Analyzer.Start", globalcontext);
276
    connect(m_startAction, SIGNAL(triggered()), this, SLOT(startTool()));
277

Mike McQuaid's avatar
Mike McQuaid committed
278
279
    m_stopAction = new QAction(tr("Stop"), m_menu);
    m_stopAction->setEnabled(false);
280
    m_stopAction->setIcon(QIcon(QLatin1String(ANALYZER_CONTROL_STOP_ICON)));
Eike Ziller's avatar
Eike Ziller committed
281
    command = Core::ActionManager::registerAction(m_stopAction, "Analyzer.Stop", globalcontext);
hjk's avatar
hjk committed
282
    m_menu->addAction(command, G_ANALYZER_CONTROL);
hjk's avatar
hjk committed
283

284
285
286
    m_menu->addSeparator(globalcontext, G_ANALYZER_TOOLS);
    m_menu->addSeparator(globalcontext, G_ANALYZER_REMOTE_TOOLS);
    m_menu->addSeparator(globalcontext, G_ANALYZER_OPTIONS);
Mike McQuaid's avatar
Mike McQuaid committed
287
288
}

289
void AnalyzerManagerPrivate::delayedInit()
290
{
hjk's avatar
hjk committed
291
    if (m_mode)
292
293
        return;

294
    m_mode = new AnalyzerMode(q);
hjk's avatar
hjk committed
295
    createModeMainWindow();
296

hjk's avatar
hjk committed
297
    // Right-side window with editor, output etc.
Mike McQuaid's avatar
Mike McQuaid committed
298
    MiniSplitter *mainWindowSplitter = new MiniSplitter;
hjk's avatar
hjk committed
299
    mainWindowSplitter->addWidget(m_mainWindow);
Mike McQuaid's avatar
Mike McQuaid committed
300
301
302
303
304
    mainWindowSplitter->addWidget(new OutputPanePlaceHolder(m_mode, mainWindowSplitter));
    mainWindowSplitter->setStretchFactor(0, 10);
    mainWindowSplitter->setStretchFactor(1, 0);
    mainWindowSplitter->setOrientation(Qt::Vertical);

hjk's avatar
hjk committed
305
    // Navigation + right-side window.
Mike McQuaid's avatar
Mike McQuaid committed
306
307
308
309
310
    MiniSplitter *splitter = new MiniSplitter;
    splitter->addWidget(new NavigationWidgetPlaceHolder(m_mode));
    splitter->addWidget(mainWindowSplitter);
    splitter->setStretchFactor(0, 0);
    splitter->setStretchFactor(1, 1);
hjk's avatar
hjk committed
311
312
313
    m_mode->setWidget(splitter);

    AnalyzerPlugin::instance()->addAutoReleasedObject(m_mode);
314
315

    // Populate Windows->Views menu with standard actions.
hjk's avatar
hjk committed
316
    Context analyzerContext(C_ANALYZEMODE);
Eike Ziller's avatar
Eike Ziller committed
317
318
    ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Id(M_WINDOW_VIEWS));
    Command *cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator1(),
hjk's avatar
hjk committed
319
        Id("Analyzer.Views.Separator1"), analyzerContext);
320
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
321
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
322
    cmd = Core::ActionManager::registerAction(m_mainWindow->toggleLockedAction(),
hjk's avatar
hjk committed
323
        Id("Analyzer.Views.ToggleLocked"), analyzerContext);
324
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
325
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
326
    cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator2(),
hjk's avatar
hjk committed
327
        Id("Analyzer.Views.Separator2"), analyzerContext);
328
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
329
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
330
    cmd = Core::ActionManager::registerAction(m_mainWindow->resetLayoutAction(),
hjk's avatar
hjk committed
331
        Id("Analyzer.Views.ResetSimple"), analyzerContext);
332
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
333
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Mike McQuaid's avatar
Mike McQuaid committed
334
335
336
337
338
339
340
341
342
}

static QToolButton *toolButton(QAction *action)
{
    QToolButton *button = new QToolButton;
    button->setDefaultAction(action);
    return button;
}

hjk's avatar
hjk committed
343
void AnalyzerManagerPrivate::createModeMainWindow()
Friedemann Kleint's avatar
Friedemann Kleint committed
344
{
Mike McQuaid's avatar
Mike McQuaid committed
345
    m_mainWindow = new Utils::FancyMainWindow();
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    m_mainWindow->setObjectName(QLatin1String("AnalyzerManagerMainWindow"));
Mike McQuaid's avatar
Mike McQuaid committed
347
348
    m_mainWindow->setDocumentMode(true);
    m_mainWindow->setDockNestingEnabled(true);
hjk's avatar
hjk committed
349
    m_mainWindow->setDockActionsVisible(false);
hjk's avatar
hjk committed
350
    connect(m_mainWindow, SIGNAL(resetLayout()), SLOT(resetLayout()));
Mike McQuaid's avatar
Mike McQuaid committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

    QBoxLayout *editorHolderLayout = new QVBoxLayout;
    editorHolderLayout->setMargin(0);
    editorHolderLayout->setSpacing(0);

    QWidget *editorAndFindWidget = new QWidget;
    editorAndFindWidget->setLayout(editorHolderLayout);
    editorHolderLayout->addWidget(new EditorManagerPlaceHolder(m_mode));
    editorHolderLayout->addWidget(new FindToolBarPlaceHolder(editorAndFindWidget));

    MiniSplitter *documentAndRightPane = new MiniSplitter;
    documentAndRightPane->addWidget(editorAndFindWidget);
    documentAndRightPane->addWidget(new RightPanePlaceHolder(m_mode));
    documentAndRightPane->setStretchFactor(0, 1);
    documentAndRightPane->setStretchFactor(1, 0);

367
368
369
370
371
    Utils::StyledBar *analyzeToolBar = new Utils::StyledBar;
    analyzeToolBar->setProperty("topBorder", true);
    QHBoxLayout *analyzeToolBarLayout = new QHBoxLayout(analyzeToolBar);
    analyzeToolBarLayout->setMargin(0);
    analyzeToolBarLayout->setSpacing(0);
hjk's avatar
hjk committed
372
    analyzeToolBarLayout->addWidget(toolButton(m_startAction));
373
374
375
    analyzeToolBarLayout->addWidget(toolButton(m_stopAction));
    analyzeToolBarLayout->addWidget(new Utils::StyledSeparator);
    analyzeToolBarLayout->addWidget(m_toolBox);
376
    analyzeToolBarLayout->addWidget(m_controlsStackWidget);
377
378
379
380
381
382
383
384
385
386
387
388
389
    analyzeToolBarLayout->addWidget(m_statusLabel);
    analyzeToolBarLayout->addStretch();

    QDockWidget *dock = new QDockWidget(tr("Analyzer Toolbar"));
    dock->setObjectName(QLatin1String("Analyzer Toolbar"));
    dock->setWidget(analyzeToolBar);
    dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
    dock->setProperty("managed_dockwidget", QLatin1String("true"));
    dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    // hide title bar
    dock->setTitleBarWidget(new QWidget(dock));
    m_mainWindow->addDockWidget(Qt::BottomDockWidgetArea, dock);
    m_mainWindow->setToolBarDockWidget(dock);
Mike McQuaid's avatar
Mike McQuaid committed
390
391
392
393
394
395
396
397
398
399
400
401
402

    QWidget *centralWidget = new QWidget;
    m_mainWindow->setCentralWidget(centralWidget);

    QVBoxLayout *centralLayout = new QVBoxLayout(centralWidget);
    centralWidget->setLayout(centralLayout);
    centralLayout->setMargin(0);
    centralLayout->setSpacing(0);
    centralLayout->addWidget(documentAndRightPane);
    centralLayout->setStretch(0, 1);
    centralLayout->setStretch(1, 0);
}

403
void AnalyzerManagerPrivate::activateDock(Qt::DockWidgetArea area, QDockWidget *dockWidget)
Mike McQuaid's avatar
Mike McQuaid committed
404
405
406
407
{
    dockWidget->setParent(m_mainWindow);
    m_mainWindow->addDockWidget(area, dockWidget);

hjk's avatar
hjk committed
408
    Context globalContext(C_GLOBAL);
Mike McQuaid's avatar
Mike McQuaid committed
409
410
411

    QAction *toggleViewAction = dockWidget->toggleViewAction();
    toggleViewAction->setText(dockWidget->windowTitle());
Eike Ziller's avatar
Eike Ziller committed
412
    Command *cmd = Core::ActionManager::registerAction(toggleViewAction,
413
        Id(QLatin1String("Analyzer.") + dockWidget->objectName()), globalContext);
Mike McQuaid's avatar
Mike McQuaid committed
414
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
415

Eike Ziller's avatar
Eike Ziller committed
416
    ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Id(M_WINDOW_VIEWS));
hjk's avatar
hjk committed
417
    viewsMenu->addAction(cmd);
Mike McQuaid's avatar
Mike McQuaid committed
418
419
}

420
421
422
void AnalyzerManagerPrivate::deactivateDock(QDockWidget *dockWidget)
{
    QAction *toggleViewAction = dockWidget->toggleViewAction();
Eike Ziller's avatar
Eike Ziller committed
423
    Core::ActionManager::unregisterAction(toggleViewAction, Id(QLatin1String("Analyzer.") + dockWidget->objectName()));
424
425
426
427
428
429
    m_mainWindow->removeDockWidget(dockWidget);
    dockWidget->hide();
    // Prevent saveState storing the data of the wrong children.
    dockWidget->setParent(0);
}

430
bool buildTypeAccepted(IAnalyzerTool::ToolMode toolMode,
hjk's avatar
hjk committed
431
                       BuildConfiguration::BuildType buildType)
Mike McQuaid's avatar
Mike McQuaid committed
432
433
434
{
    if (toolMode == IAnalyzerTool::AnyMode)
        return true;
hjk's avatar
hjk committed
435
    if (buildType == BuildConfiguration::Unknown)
Mike McQuaid's avatar
Mike McQuaid committed
436
        return true;
hjk's avatar
hjk committed
437
    if (buildType == BuildConfiguration::Debug
hjk's avatar
hjk committed
438
            && toolMode == IAnalyzerTool::DebugMode)
Mike McQuaid's avatar
Mike McQuaid committed
439
        return true;
hjk's avatar
hjk committed
440
    if (buildType == BuildConfiguration::Release
hjk's avatar
hjk committed
441
            && toolMode == IAnalyzerTool::ReleaseMode)
Mike McQuaid's avatar
Mike McQuaid committed
442
        return true;
hjk's avatar
hjk committed
443
    return false;
Mike McQuaid's avatar
Mike McQuaid committed
444
445
}

hjk's avatar
hjk committed
446
447
bool AnalyzerManagerPrivate::showPromptDialog(const QString &title, const QString &text,
    const QString &stopButtonText, const QString &cancelButtonText) const
448
{
hjk's avatar
hjk committed
449
    Utils::CheckableMessageBox messageBox(ICore::mainWindow());
450
451
452
453
454
455
456
457
458
459
460
461
462
    messageBox.setWindowTitle(title);
    messageBox.setText(text);
    messageBox.setStandardButtons(QDialogButtonBox::Yes|QDialogButtonBox::Cancel);
    if (!stopButtonText.isEmpty())
        messageBox.button(QDialogButtonBox::Yes)->setText(stopButtonText);
    if (!cancelButtonText.isEmpty())
        messageBox.button(QDialogButtonBox::Cancel)->setText(cancelButtonText);
    messageBox.setDefaultButton(QDialogButtonBox::Yes);
    messageBox.setCheckBoxVisible(false);
    messageBox.exec();;
    return messageBox.clickedStandardButton() == QDialogButtonBox::Yes;
}

463
void AnalyzerManagerPrivate::startLocalTool(IAnalyzerTool *tool)
hjk's avatar
hjk committed
464
{
hjk's avatar
hjk committed
465
    int index = m_tools.indexOf(tool);
hjk's avatar
hjk committed
466
    QTC_ASSERT(index >= 0, return);
467
468
    QTC_ASSERT(index < m_tools.size(), return);
    QTC_ASSERT(tool == m_currentTool, return);
469

470
    // Make sure mode is shown.
471
    q->showMode();
Mike McQuaid's avatar
Mike McQuaid committed
472

hjk's avatar
hjk committed
473
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
Mike McQuaid's avatar
Mike McQuaid committed
474
475

    // ### not sure if we're supposed to check if the RunConFiguration isEnabled
hjk's avatar
hjk committed
476
477
    Project *pro = pe->startupProject();
    BuildConfiguration::BuildType buildType = BuildConfiguration::Unknown;
478
    if (pro) {
hjk's avatar
hjk committed
479
        if (const Target *target = pro->activeTarget()) {
480
            // Build configuration is 0 for QML projects.
hjk's avatar
hjk committed
481
            if (const BuildConfiguration *buildConfig = target->activeBuildConfiguration())
482
483
484
                buildType = buildConfig->buildType();
        }
    }
485

486
    IAnalyzerTool::ToolMode toolMode = tool->toolMode();
Mike McQuaid's avatar
Mike McQuaid committed
487

hjk's avatar
hjk committed
488
489
490
    // Check the project for whether the build config is in the correct mode
    // if not, notify the user and urge him to use the correct mode.
    if (!buildTypeAccepted(toolMode, buildType)) {
491
        const QString toolName = tool->displayName();
hjk's avatar
hjk committed
492
493
        const QString currentMode =
            buildType == BuildConfiguration::Debug ? tr("Debug") : tr("Release");
Mike McQuaid's avatar
Mike McQuaid committed
494

hjk's avatar
hjk committed
495
        QSettings *settings = ICore::settings();
hjk's avatar
hjk committed
496
        const QString configKey = QLatin1String("Analyzer.AnalyzeCorrectMode");
Mike McQuaid's avatar
Mike McQuaid committed
497
498
499
500
        int ret;
        if (settings->contains(configKey)) {
            ret = settings->value(configKey, QDialog::Accepted).toInt();
        } else {
501
502
503
504
            QString toolModeString;
            switch (toolMode) {
                case IAnalyzerTool::DebugMode:
                    toolModeString = tr("Debug");
505
                    break;
506
507
508
                case IAnalyzerTool::ReleaseMode:
                    toolModeString = tr("Release");
                    break;
509
510
                default:
                    QTC_CHECK(false);
511
            }
Friedemann Kleint's avatar
Friedemann Kleint committed
512
            const QString title = tr("Run %1 in %2 Mode?").arg(toolName).arg(currentMode);
hjk's avatar
hjk committed
513
514
515
            const QString message = tr("<html><head/><body><p>You are trying "
                "to run the tool \"%1\" on an application in %2 mode. "
                "The tool is designed to be used in %3 mode.</p><p>"
516
517
518
519
                "Debug and Release mode run-time characteristics differ "
                "significantly, analytical findings for one mode may or "
                "may not be relevant for the other.</p><p>"
                "Do you want to continue and run the tool in %2 mode?</p></body></html>")
hjk's avatar
hjk committed
520
                    .arg(toolName).arg(currentMode).arg(toolModeString);
Friedemann Kleint's avatar
Friedemann Kleint committed
521
522
523
            const QString checkBoxText = tr("&Do not ask again");
            bool checkBoxSetting = false;
            const QDialogButtonBox::StandardButton button =
hjk's avatar
hjk committed
524
                Utils::CheckableMessageBox::question(ICore::mainWindow(),
hjk's avatar
hjk committed
525
526
527
                    title, message, checkBoxText,
                    &checkBoxSetting, QDialogButtonBox::Yes|QDialogButtonBox::Cancel,
                    QDialogButtonBox::Cancel);
Friedemann Kleint's avatar
Friedemann Kleint committed
528
529
530
            ret = button == QDialogButtonBox::Yes ? QDialog::Accepted : QDialog::Rejected;

            if (checkBoxSetting && ret == QDialog::Accepted)
Mike McQuaid's avatar
Mike McQuaid committed
531
532
533
534
535
536
                settings->setValue(configKey, ret);
        }
        if (ret == QDialog::Rejected)
            return;
    }

537
    pe->runProject(pro, tool->runMode());
Mike McQuaid's avatar
Mike McQuaid committed
538
539
}

540
541
542
543
544
545
546
547
548
bool AnalyzerManagerPrivate::isActionRunnable(QAction *action) const
{
    if (!action || m_isRunning)
        return false;
    if (m_modeFromAction.value(action) == StartRemote)
        return true;

    IAnalyzerTool *tool = m_toolFromAction.value(action);
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
549
    return pe->canRun(pe->startupProject(), tool->runMode());
550
551
}

552
void AnalyzerManagerPrivate::startTool()
553
{
554
    m_currentTool->startTool(m_currentMode);
555
}
556

557
void AnalyzerManagerPrivate::modeChanged(IMode *mode)
Mike McQuaid's avatar
Mike McQuaid committed
558
{
hjk's avatar
hjk committed
559
560
561
562
563
564
565
566
567
568
    if (mode && mode == m_mode) {
        m_mainWindow->setDockActionsVisible(true);
        static bool firstTime = true;
        if (firstTime)
            selectSavedTool();
        firstTime = false;
        updateRunActions();
    } else {
        m_mainWindow->setDockActionsVisible(false);
    }
Mike McQuaid's avatar
Mike McQuaid committed
569
570
}

571
572
573
QAction *AnalyzerManagerPrivate::actionFromToolAndMode(IAnalyzerTool *tool, StartMode mode)
{
    foreach (QAction *action, m_actions)
hjk's avatar
hjk committed
574
        if (m_toolFromAction.value(action) == tool && m_modeFromAction[action] == mode)
575
            return action;
576
    QTC_CHECK(false);
577
578
579
580
581
    return 0;
}

void AnalyzerManagerPrivate::selectSavedTool()
{
hjk's avatar
hjk committed
582
    const QSettings *settings = ICore::settings();
583
584
585
586
587
588
589
590
591
592

    if (settings->contains(QLatin1String(LAST_ACTIVE_TOOL))) {
        const Id lastActiveAction(settings->value(QLatin1String(LAST_ACTIVE_TOOL)).toString());
        foreach (QAction *action, m_actions) {
            IAnalyzerTool *tool = m_toolFromAction.value(action);
            StartMode mode = m_modeFromAction.value(action);
            if (tool->actionId(mode) == lastActiveAction) {
                selectTool(tool, mode);
                return;
            }
593
594
        }
    }
595
596
597
598
599
600
    // fallback to first available tool
    if (!m_actions.isEmpty()) {
        IAnalyzerTool *tool = m_toolFromAction.value(m_actions.first());
        StartMode mode = m_modeFromAction.value(m_actions.first());
        selectTool(tool, mode);
    }
601
602
}

hjk's avatar
hjk committed
603
void AnalyzerManagerPrivate::selectMenuAction()
604
{
hjk's avatar
hjk committed
605
606
607
608
    QAction *action = qobject_cast<QAction *>(sender());
    QTC_ASSERT(action, return);
    IAnalyzerTool *tool = m_toolFromAction.value(action);
    StartMode mode = m_modeFromAction.value(action);
609
610

    AnalyzerManager::showMode();
hjk's avatar
hjk committed
611
612
    selectTool(tool, mode);
    tool->startTool(mode);
613
614
}

hjk's avatar
hjk committed
615
void AnalyzerManagerPrivate::selectToolboxAction(int index)
616
{
hjk's avatar
hjk committed
617
618
    QAction *action = m_actions[index];
    selectTool(m_toolFromAction.value(action), m_modeFromAction.value(action));
619
620
}

621
void AnalyzerManagerPrivate::selectTool(IAnalyzerTool *tool, StartMode mode)
622
{
623
    if (m_currentTool == tool && m_currentMode == mode)
Mike McQuaid's avatar
Mike McQuaid committed
624
        return;
625

626
627
628
629
630
631
    QAction *action = actionFromToolAndMode(tool, mode);
    const int actionIndex = m_actions.indexOf(action);
    QTC_ASSERT(actionIndex >= 0, return);

    // Clean up old tool.
    if (m_currentTool) {
632
633
634
        saveToolSettings(m_currentTool, m_currentMode);
        foreach (QDockWidget *widget, m_toolWidgets.value(m_currentTool))
            deactivateDock(widget);
635
        m_currentTool->toolDeselected();
Mike McQuaid's avatar
Mike McQuaid committed
636
637
    }

638
    // Now change the tool.
639
    m_currentTool = tool;
640
    m_currentMode = mode;
641
    m_currentAction = action;
642

643
    if (!m_defaultSettings.contains(tool)) {
644
        QWidget *widget = tool->createWidgets();
645
        QTC_CHECK(widget);
646
        m_defaultSettings.insert(tool, m_mainWindow->saveSettings());
647
        QTC_CHECK(!m_controlsWidgetFromTool.contains(tool));
648
649
        m_controlsWidgetFromTool[tool] = widget;
        m_controlsStackWidget->addWidget(widget);
650
    }
651
652
    foreach (QDockWidget *widget, m_toolWidgets.value(tool))
        activateDock(Qt::DockWidgetArea(widget->property(INITIAL_DOCK_AREA).toInt()), widget);
653

654
    loadToolSettings(tool);
655

656
    QTC_CHECK(m_controlsWidgetFromTool.contains(tool));
657
    m_controlsStackWidget->setCurrentWidget(m_controlsWidgetFromTool.value(tool));
658
    m_toolBox->setCurrentIndex(actionIndex);
Mike McQuaid's avatar
Mike McQuaid committed
659

660
    updateRunActions();
hjk's avatar
hjk committed
661
662
}

663
void AnalyzerManagerPrivate::addTool(IAnalyzerTool *tool, const StartModes &modes)
hjk's avatar
hjk committed
664
{
hjk's avatar
hjk committed
665
    delayedInit(); // Make sure that there is a valid IMode instance.
666
667
668
669

    const bool blocked = m_toolBox->blockSignals(true); // Do not make current.
    foreach (StartMode mode, modes) {
        QString actionName = tool->actionName(mode);
670
671
        Id menuGroup = tool->menuGroup(mode);
        Id actionId = tool->actionId(mode);
Orgad Shaneh's avatar
Orgad Shaneh committed
672
        QAction *action = new QAction(actionName, this);
Eike Ziller's avatar
Eike Ziller committed
673
        Command *command = Core::ActionManager::registerAction(action, actionId, Context(C_GLOBAL));
674
        m_menu->addAction(command, menuGroup);
675
676
677
678
        command->action()->setData(int(StartLocal));
        // Assuming this happens before project loading.
        if (mode == StartLocal)
            command->action()->setEnabled(false);
679
680
        m_actions.append(action);
        m_toolFromAction[action] = tool;
681
        m_modeFromAction[action] = mode;
682
683
        m_toolBox->addItem(actionName);
        m_toolBox->blockSignals(blocked);
hjk's avatar
hjk committed
684
        connect(action, SIGNAL(triggered()), SLOT(selectMenuAction()));
685
    }
686
    m_tools.append(tool);
687
    m_toolBox->setEnabled(true);
hjk's avatar
hjk committed
688
}
Mike McQuaid's avatar
Mike McQuaid committed
689

hjk's avatar
hjk committed
690
void AnalyzerManagerPrivate::handleToolStarted()
hjk's avatar
hjk committed
691
{
hjk's avatar
hjk committed
692
    m_isRunning = true; // FIXME: Make less global.
693
    updateRunActions();
hjk's avatar
hjk committed
694
695
696
697
698
}

void AnalyzerManagerPrivate::handleToolFinished()
{
    m_isRunning = false;
699
    updateRunActions();
Mike McQuaid's avatar
Mike McQuaid committed
700
701
}

702
void AnalyzerManagerPrivate::loadToolSettings(IAnalyzerTool *tool)
Mike McQuaid's avatar
Mike McQuaid committed
703
{
hjk's avatar
hjk committed
704
    QTC_ASSERT(m_mainWindow, return);
hjk's avatar
hjk committed
705
    QSettings *settings = ICore::settings();
706
    settings->beginGroup(QLatin1String("AnalyzerViewSettings_") + tool->id().toString());
707
    if (settings->value(QLatin1String("ToolSettingsSaved"), false).toBool())
hjk's avatar
hjk committed
708
        m_mainWindow->restoreSettings(settings);
709
710
    else
        m_mainWindow->restoreSettings(m_defaultSettings.value(tool));
hjk's avatar
hjk committed
711
    settings->endGroup();
Mike McQuaid's avatar
Mike McQuaid committed
712
713
}

714
void AnalyzerManagerPrivate::saveToolSettings(IAnalyzerTool *tool, StartMode mode)
Mike McQuaid's avatar
Mike McQuaid committed
715
{
hjk's avatar
hjk committed
716
717
718
719
    if (!tool)
        return; // no active tool, do nothing
    QTC_ASSERT(m_mainWindow, return);

hjk's avatar
hjk committed
720
    QSettings *settings = ICore::settings();
721
    settings->beginGroup(QLatin1String("AnalyzerViewSettings_") + tool->id().toString());
hjk's avatar
hjk committed
722
    m_mainWindow->saveSettings(settings);
723
    settings->setValue(QLatin1String("ToolSettingsSaved"), true);
hjk's avatar
hjk committed
724
    settings->endGroup();
725
    settings->setValue(QLatin1String(LAST_ACTIVE_TOOL), tool->actionId(mode).toString());
Mike McQuaid's avatar
Mike McQuaid committed
726
727
}

728
void AnalyzerManagerPrivate::updateRunActions()
Mike McQuaid's avatar
Mike McQuaid committed
729
{
hjk's avatar
hjk committed
730
731
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
    Project *project = pe->startupProject();
hjk's avatar
hjk committed
732

733
    bool startEnabled = isActionRunnable(m_currentAction);
hjk's avatar
hjk committed
734
735

    QString disabledReason;
hjk's avatar
hjk committed
736
    if (m_isRunning)
hjk's avatar
hjk committed
737
        disabledReason = tr("An analysis is still in progress.");
738
    else if (!m_currentTool)
hjk's avatar
hjk committed
739
740
        disabledReason = tr("No analyzer tool selected.");
    else
741
        disabledReason = pe->cannotRunReason(project, m_currentTool->runMode());
hjk's avatar
hjk committed
742
743
744

    m_startAction->setEnabled(startEnabled);
    m_startAction->setToolTip(disabledReason);
hjk's avatar
hjk committed
745
746
    m_toolBox->setEnabled(!m_isRunning);
    m_stopAction->setEnabled(m_isRunning);
hjk's avatar
hjk committed
747
    foreach (QAction *action, m_actions)
748
        action->setEnabled(isActionRunnable(action));
Mike McQuaid's avatar
Mike McQuaid committed
749
750
}

hjk's avatar
hjk committed
751
752
753
754
755
////////////////////////////////////////////////////////////////////
//
// AnalyzerManager
//
////////////////////////////////////////////////////////////////////
756

hjk's avatar
hjk committed
757
static AnalyzerManager *m_instance = 0;
758

hjk's avatar
hjk committed
759
760
761
762
763
AnalyzerManager::AnalyzerManager(QObject *parent)
  : QObject(parent),
    d(new AnalyzerManagerPrivate(this))
{
    m_instance = this;
764
765
}

hjk's avatar
hjk committed
766
AnalyzerManager::~AnalyzerManager()
Mike McQuaid's avatar
Mike McQuaid committed
767
{
hjk's avatar
hjk committed
768
    delete d;
Mike McQuaid's avatar
Mike McQuaid committed
769
770
}

hjk's avatar
hjk committed
771
void AnalyzerManager::extensionsInitialized()
Mike McQuaid's avatar
Mike McQuaid committed
772
{
773
    if (m_instance->d->m_tools.isEmpty())
Mike McQuaid's avatar
Mike McQuaid committed
774
775
        return;

776
    foreach (IAnalyzerTool *tool, m_instance->d->m_tools)
hjk's avatar
hjk committed
777
        tool->extensionsInitialized();
Mike McQuaid's avatar
Mike McQuaid committed
778
779
}

hjk's avatar
hjk committed
780
void AnalyzerManager::shutdown()
Mike McQuaid's avatar
Mike McQuaid committed
781
{
782
    m_instance->d->saveToolSettings(m_instance->d->m_currentTool, m_instance->d->m_currentMode);
Mike McQuaid's avatar
Mike McQuaid committed
783
784
}

785
void AnalyzerManager::addTool(IAnalyzerTool *tool, const StartModes &modes)
Mike McQuaid's avatar
Mike McQuaid committed
786
{
787
    m_instance->d->addTool(tool, modes);
788
    AnalyzerGlobalSettings::instance()->registerTool(tool);
Mike McQuaid's avatar
Mike McQuaid committed
789
790
}

hjk's avatar
hjk committed
791
792
QDockWidget *AnalyzerManager::createDockWidget(IAnalyzerTool *tool, const QString &title,
                                               QWidget *widget, Qt::DockWidgetArea area)
Mike McQuaid's avatar
Mike McQuaid committed
793
{
794
795
    QTC_ASSERT(!widget->objectName().isEmpty(), return 0);
    AnalyzerManagerPrivate *d = m_instance->d;
hjk's avatar
hjk committed
796
797
    QDockWidget *dockWidget = d->m_mainWindow->addDockForWidget(widget);
    dockWidget->setProperty(INITIAL_DOCK_AREA, int(area));
798
    d->m_dockWidgets.append(AnalyzerManagerPrivate::DockPtr(dockWidget));
hjk's avatar
hjk committed
799
800
801
    dockWidget->setWindowTitle(title);
    d->m_toolWidgets[tool].push_back(dockWidget);
    return dockWidget;
Mike McQuaid's avatar
Mike McQuaid committed
802
803
}

804
805
806
807
808
IAnalyzerTool *AnalyzerManager::currentSelectedTool()
{
    return m_instance->d->m_currentTool;
}

809
810
811
812
813
QList<IAnalyzerTool *> AnalyzerManager::tools()
{
    return m_instance->d->m_tools;
}

814
void AnalyzerManager::selectTool(IAnalyzerTool *tool, StartMode mode)
hjk's avatar
hjk committed
815
{
816
    m_instance->d->selectTool(tool, mode);
hjk's avatar
hjk committed
817
818
}

819
void AnalyzerManager::startTool(IAnalyzerTool *tool, StartMode mode)
hjk's avatar
hjk committed
820
{
821
    QTC_ASSERT(tool == m_instance->d->m_currentTool, return);
822
    tool->startTool(mode);
hjk's avatar
hjk committed
823
}
dt_'s avatar
dt_ committed
824

825
Utils::FancyMainWindow *AnalyzerManager::mainWindow()
hjk's avatar
hjk committed
826
{
827
    return m_instance->d->m_mainWindow;
hjk's avatar
hjk committed
828
}
829

830
void AnalyzerManagerPrivate::resetLayout()
hjk's avatar
hjk committed
831
{
832
    m_mainWindow->restoreSettings(m_defaultSettings.value(m_currentTool));
Mike McQuaid's avatar
Mike McQuaid committed
833
}
834

835
836
void AnalyzerManager::showStatusMessage(const QString &message, int timeoutMS)
{
837
    m_instance->d->m_statusLabel->showStatusMessage(message, timeoutMS);
838
839
840
841
842
843
844
845
846
}

void AnalyzerManager::showPermanentStatusMessage(const QString &message)
{
    showStatusMessage(message, -1);
}

QString AnalyzerManager::msgToolStarted(const QString &name)
{
hjk's avatar
hjk committed
847
    return tr("Tool \"%1\" started...").arg(name);
848
849
850
851
852
}

QString AnalyzerManager::msgToolFinished(const QString &name, int issuesFound)
{
    return issuesFound ?