analyzermanager.cpp 23.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Mike McQuaid's avatar
Mike McQuaid committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** 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
33

#include "analyzermanager.h"

#include "analyzerplugin.h"
34
#include "ianalyzertool.h"
35
#include "analyzersettings.h"
Mike McQuaid's avatar
Mike McQuaid committed
36

hjk's avatar
hjk committed
37
#include <coreplugin/coreconstants.h>
Mike McQuaid's avatar
Mike McQuaid committed
38
#include <coreplugin/findplaceholder.h>
hjk's avatar
hjk committed
39
40
#include <coreplugin/icore.h>
#include <coreplugin/imode.h>
Mike McQuaid's avatar
Mike McQuaid committed
41
42
43
44
#include <coreplugin/modemanager.h>
#include <coreplugin/navigationwidget.h>
#include <coreplugin/outputpane.h>
#include <coreplugin/rightpane.h>
hjk's avatar
hjk committed
45
46
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
47
#include <coreplugin/editormanager/editormanager.h>
Mike McQuaid's avatar
Mike McQuaid committed
48
49
50
51
52
53
54
55
56

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/target.h>

#include <utils/fancymainwindow.h>
#include <utils/styledbar.h>
#include <utils/qtcassert.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
57
#include <utils/checkablemessagebox.h>
58
#include <utils/statuslabel.h>
Mike McQuaid's avatar
Mike McQuaid committed
59

60
61
62
63
64
65
66
67
68
69
70
#include <QVariant>
#include <QDebug>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDockWidget>
#include <QAction>
#include <QToolButton>
#include <QComboBox>
#include <QStackedWidget>
#include <QDialog>
#include <QDialogButtonBox>
71
#include <QPointer>
72
#include <QPushButton>
Mike McQuaid's avatar
Mike McQuaid committed
73
74

using namespace Core;
hjk's avatar
hjk committed
75
using namespace Core::Constants;
Mike McQuaid's avatar
Mike McQuaid committed
76
using namespace Analyzer::Internal;
hjk's avatar
hjk committed
77
using namespace Analyzer::Constants;
hjk's avatar
hjk committed
78
using namespace ProjectExplorer;
Mike McQuaid's avatar
Mike McQuaid committed
79
80
81
82

namespace Analyzer {
namespace Internal {

83
const char LAST_ACTIVE_TOOL[] = "Analyzer.Plugin.LastActiveTool";
hjk's avatar
hjk committed
84
const char INITIAL_DOCK_AREA[] = "initial_dock_area";
hjk's avatar
hjk committed
85

hjk's avatar
hjk committed
86
87
88
89
90
////////////////////////////////////////////////////////////////////
//
// AnalyzerMode
//
////////////////////////////////////////////////////////////////////
Mike McQuaid's avatar
Mike McQuaid committed
91

hjk's avatar
hjk committed
92
class AnalyzerMode : public IMode
93
94
95
96
97
{
    Q_OBJECT

public:
    AnalyzerMode(QObject *parent = 0)
hjk's avatar
hjk committed
98
        : IMode(parent)
99
    {
hjk's avatar
hjk committed
100
        setContext(Context(C_EDITORMANAGER, C_ANALYZEMODE, C_NAVIGATION_PANE));
101
        setDisplayName(tr("Analyze"));
102
        setIcon(QIcon(QLatin1String(":/images/analyzer_mode.png")));
hjk's avatar
hjk committed
103
        setPriority(P_MODE_ANALYZE);
104
        setId(MODE_ANALYZE);
105
    }
106
107
108
109

    ~AnalyzerMode()
    {
        // Make sure the editor manager does not get deleted.
hjk's avatar
hjk committed
110
111
112
        delete m_widget;
        m_widget = 0;
        EditorManager::instance()->setParent(0);
113
114
115
    }
};

Mike McQuaid's avatar
Mike McQuaid committed
116
117
} // namespace Internal

hjk's avatar
hjk committed
118
119
120
121
122
123
////////////////////////////////////////////////////////////////////
//
// AnalyzerManagerPrivate
//
////////////////////////////////////////////////////////////////////

124
class AnalyzerManagerPrivate : public QObject
Mike McQuaid's avatar
Mike McQuaid committed
125
{
hjk's avatar
hjk committed
126
127
    Q_OBJECT

Mike McQuaid's avatar
Mike McQuaid committed
128
public:
129
130
    typedef QHash<QString, QVariant> FancyMainWindowSettings;

Mike McQuaid's avatar
Mike McQuaid committed
131
132
133
    AnalyzerManagerPrivate(AnalyzerManager *qq);
    ~AnalyzerManagerPrivate();

134
    /**
hjk's avatar
hjk committed
135
     * After calling this, a proper instance of IMode is initialized
136
137
138
139
140
141
     * 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
142
    void setupActions();
hjk's avatar
hjk committed
143
    void createModeMainWindow();
hjk's avatar
hjk committed
144
145
    bool showPromptDialog(const QString &title, const QString &text,
        const QString &stopButtonText, const QString &cancelButtonText) const;
Mike McQuaid's avatar
Mike McQuaid committed
146

147
148
    void activateDock(Qt::DockWidgetArea area, QDockWidget *dockWidget);
    void deactivateDock(QDockWidget *dockWidget);
hjk's avatar
hjk committed
149
    void addAction(AnalyzerAction *action);
150
    void selectSavedTool();
hjk's avatar
hjk committed
151
    void selectAction(AnalyzerAction *action);
hjk's avatar
hjk committed
152
153
    void handleToolStarted();
    void handleToolFinished();
hjk's avatar
hjk committed
154
155
    void saveToolSettings(AnalyzerAction *action);
    void loadToolSettings(AnalyzerAction *action);
156

157
    // Convenience.
hjk's avatar
hjk committed
158
    bool isActionRunnable(AnalyzerAction *action) const;
159

hjk's avatar
hjk committed
160
public slots:
161
    void startTool();
hjk's avatar
hjk committed
162
163
    void selectToolboxAction(int);
    void selectMenuAction();
164
    void modeChanged(Core::IMode *mode);
hjk's avatar
hjk committed
165
166
    void resetLayout();
    void updateRunActions();
Mike McQuaid's avatar
Mike McQuaid committed
167

hjk's avatar
hjk committed
168
public:
Mike McQuaid's avatar
Mike McQuaid committed
169
170
    AnalyzerManager *q;
    AnalyzerMode *m_mode;
hjk's avatar
hjk committed
171
    bool m_isRunning;
Mike McQuaid's avatar
Mike McQuaid committed
172
    Utils::FancyMainWindow *m_mainWindow;
hjk's avatar
hjk committed
173
174
    AnalyzerAction *m_currentAction;
    QList<AnalyzerAction *> m_actions;
Mike McQuaid's avatar
Mike McQuaid committed
175
176
    QAction *m_startAction;
    QAction *m_stopAction;
177
    ActionContainer *m_menu;
Mike McQuaid's avatar
Mike McQuaid committed
178
    QComboBox *m_toolBox;
179
    QStackedWidget *m_controlsStackWidget;
180
    Utils::StatusLabel *m_statusLabel;
181
    typedef QMap<IAnalyzerTool *, FancyMainWindowSettings> MainWindowSettingsMap;
182
    QHash<IAnalyzerTool *, QList<QDockWidget *> > m_toolWidgets;
183
    QHash<IAnalyzerTool *, QWidget *> m_controlsWidgetFromTool;
184
    MainWindowSettingsMap m_defaultSettings;
Milian Wolff's avatar
Milian Wolff committed
185
186

    // list of dock widgets to prevent memory leak
187
    typedef QPointer<QDockWidget> DockPtr;
Milian Wolff's avatar
Milian Wolff committed
188
    QList<DockPtr> m_dockWidgets;
Mike McQuaid's avatar
Mike McQuaid committed
189
190
};

191
AnalyzerManagerPrivate::AnalyzerManagerPrivate(AnalyzerManager *qq):
Mike McQuaid's avatar
Mike McQuaid committed
192
193
    q(qq),
    m_mode(0),
hjk's avatar
hjk committed
194
    m_isRunning(false),
Mike McQuaid's avatar
Mike McQuaid committed
195
    m_mainWindow(0),
196
    m_currentAction(0),
Mike McQuaid's avatar
Mike McQuaid committed
197
198
199
    m_startAction(0),
    m_stopAction(0),
    m_menu(0),
Friedemann Kleint's avatar
Friedemann Kleint committed
200
    m_toolBox(new QComboBox),
201
    m_controlsStackWidget(new QStackedWidget),
202
    m_statusLabel(new Utils::StatusLabel)
Mike McQuaid's avatar
Mike McQuaid committed
203
{
Friedemann Kleint's avatar
Friedemann Kleint committed
204
    m_toolBox->setObjectName(QLatin1String("AnalyzerManagerToolBox"));
205
    connect(m_toolBox, SIGNAL(activated(int)), SLOT(selectToolboxAction(int)));
Mike McQuaid's avatar
Mike McQuaid committed
206
207

    setupActions();
hjk's avatar
hjk committed
208

hjk's avatar
hjk committed
209
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
hjk's avatar
hjk committed
210
    connect(pe, SIGNAL(updateRunActions()), SLOT(updateRunActions()));
Mike McQuaid's avatar
Mike McQuaid committed
211
212
}

213
AnalyzerManagerPrivate::~AnalyzerManagerPrivate()
Mike McQuaid's avatar
Mike McQuaid committed
214
{
Milian Wolff's avatar
Milian Wolff committed
215
216
    // 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
217
    foreach (const DockPtr &ptr, m_dockWidgets) {
Milian Wolff's avatar
Milian Wolff committed
218
219
220
        if (ptr)
            delete ptr.data();
    }
Mike McQuaid's avatar
Mike McQuaid committed
221
222
}

223
void AnalyzerManagerPrivate::setupActions()
Mike McQuaid's avatar
Mike McQuaid committed
224
{
hjk's avatar
hjk committed
225
226
    const Context globalcontext(C_GLOBAL);
    Command *command = 0;
227

Mike McQuaid's avatar
Mike McQuaid committed
228
    // Menus
Eike Ziller's avatar
Eike Ziller committed
229
    m_menu = Core::ActionManager::createMenu(M_DEBUG_ANALYZER);
hjk's avatar
hjk committed
230
    m_menu->menu()->setTitle(tr("&Analyze"));
231
232
    m_menu->menu()->setEnabled(true);

hjk's avatar
hjk committed
233
234
235
236
    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);
237

Eike Ziller's avatar
Eike Ziller committed
238
239
    ActionContainer *menubar = Core::ActionManager::actionContainer(MENU_BAR);
    ActionContainer *mtools = Core::ActionManager::actionContainer(M_TOOLS);
hjk's avatar
hjk committed
240
    menubar->addMenu(mtools, m_menu);
Mike McQuaid's avatar
Mike McQuaid committed
241
242

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

Mike McQuaid's avatar
Mike McQuaid committed
247
248
    m_stopAction = new QAction(tr("Stop"), m_menu);
    m_stopAction->setEnabled(false);
249
    m_stopAction->setIcon(QIcon(QLatin1String(ANALYZER_CONTROL_STOP_ICON)));
Eike Ziller's avatar
Eike Ziller committed
250
    command = Core::ActionManager::registerAction(m_stopAction, "Analyzer.Stop", globalcontext);
hjk's avatar
hjk committed
251
    m_menu->addAction(command, G_ANALYZER_CONTROL);
hjk's avatar
hjk committed
252

253
254
255
    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
256
257
}

258
void AnalyzerManagerPrivate::delayedInit()
259
{
hjk's avatar
hjk committed
260
    if (m_mode)
261
262
        return;

263
    m_mode = new AnalyzerMode(q);
hjk's avatar
hjk committed
264
    createModeMainWindow();
265

266
267
268
    connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*)),
            this, SLOT(modeChanged(Core::IMode*)));

hjk's avatar
hjk committed
269
    // Right-side window with editor, output etc.
Mike McQuaid's avatar
Mike McQuaid committed
270
    MiniSplitter *mainWindowSplitter = new MiniSplitter;
hjk's avatar
hjk committed
271
    mainWindowSplitter->addWidget(m_mainWindow);
Mike McQuaid's avatar
Mike McQuaid committed
272
273
274
275
276
    mainWindowSplitter->addWidget(new OutputPanePlaceHolder(m_mode, mainWindowSplitter));
    mainWindowSplitter->setStretchFactor(0, 10);
    mainWindowSplitter->setStretchFactor(1, 0);
    mainWindowSplitter->setOrientation(Qt::Vertical);

hjk's avatar
hjk committed
277
    // Navigation + right-side window.
Mike McQuaid's avatar
Mike McQuaid committed
278
279
280
281
282
    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
283
284
285
    m_mode->setWidget(splitter);

    AnalyzerPlugin::instance()->addAutoReleasedObject(m_mode);
286
287

    // Populate Windows->Views menu with standard actions.
hjk's avatar
hjk committed
288
    Context analyzerContext(C_ANALYZEMODE);
Eike Ziller's avatar
Eike Ziller committed
289
290
    ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Id(M_WINDOW_VIEWS));
    Command *cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator1(),
hjk's avatar
hjk committed
291
        Id("Analyzer.Views.Separator1"), analyzerContext);
292
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
293
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
294
    cmd = Core::ActionManager::registerAction(m_mainWindow->toggleLockedAction(),
hjk's avatar
hjk committed
295
        Id("Analyzer.Views.ToggleLocked"), analyzerContext);
296
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
297
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
298
    cmd = Core::ActionManager::registerAction(m_mainWindow->menuSeparator2(),
hjk's avatar
hjk committed
299
        Id("Analyzer.Views.Separator2"), analyzerContext);
300
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
301
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Eike Ziller's avatar
Eike Ziller committed
302
    cmd = Core::ActionManager::registerAction(m_mainWindow->resetLayoutAction(),
hjk's avatar
hjk committed
303
        Id("Analyzer.Views.ResetSimple"), analyzerContext);
304
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
305
    viewsMenu->addAction(cmd, G_DEFAULT_THREE);
Mike McQuaid's avatar
Mike McQuaid committed
306
307
308
309
310
311
312
313
314
}

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

hjk's avatar
hjk committed
315
void AnalyzerManagerPrivate::createModeMainWindow()
Friedemann Kleint's avatar
Friedemann Kleint committed
316
{
Mike McQuaid's avatar
Mike McQuaid committed
317
    m_mainWindow = new Utils::FancyMainWindow();
Friedemann Kleint's avatar
Friedemann Kleint committed
318
    m_mainWindow->setObjectName(QLatin1String("AnalyzerManagerMainWindow"));
Mike McQuaid's avatar
Mike McQuaid committed
319
320
    m_mainWindow->setDocumentMode(true);
    m_mainWindow->setDockNestingEnabled(true);
hjk's avatar
hjk committed
321
    m_mainWindow->setDockActionsVisible(false);
hjk's avatar
hjk committed
322
    connect(m_mainWindow, SIGNAL(resetLayout()), SLOT(resetLayout()));
Mike McQuaid's avatar
Mike McQuaid committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

    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);

339
340
341
342
343
    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
344
    analyzeToolBarLayout->addWidget(toolButton(m_startAction));
345
346
347
    analyzeToolBarLayout->addWidget(toolButton(m_stopAction));
    analyzeToolBarLayout->addWidget(new Utils::StyledSeparator);
    analyzeToolBarLayout->addWidget(m_toolBox);
348
    analyzeToolBarLayout->addWidget(m_controlsStackWidget);
349
350
351
352
353
354
355
356
357
358
359
360
361
    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
362
363
364
365
366
367
368
369
370
371
372
373
374

    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);
}

375
void AnalyzerManagerPrivate::activateDock(Qt::DockWidgetArea area, QDockWidget *dockWidget)
Mike McQuaid's avatar
Mike McQuaid committed
376
377
378
379
{
    dockWidget->setParent(m_mainWindow);
    m_mainWindow->addDockWidget(area, dockWidget);

hjk's avatar
hjk committed
380
    Context globalContext(C_GLOBAL);
Mike McQuaid's avatar
Mike McQuaid committed
381
382
383

    QAction *toggleViewAction = dockWidget->toggleViewAction();
    toggleViewAction->setText(dockWidget->windowTitle());
384
385
    Command *cmd = ActionManager::registerAction(toggleViewAction,
        Id("Analyzer.").withSuffix(dockWidget->objectName()), globalContext);
Mike McQuaid's avatar
Mike McQuaid committed
386
    cmd->setAttribute(Command::CA_Hide);
hjk's avatar
hjk committed
387

Eike Ziller's avatar
Eike Ziller committed
388
    ActionContainer *viewsMenu = Core::ActionManager::actionContainer(Id(M_WINDOW_VIEWS));
hjk's avatar
hjk committed
389
    viewsMenu->addAction(cmd);
Mike McQuaid's avatar
Mike McQuaid committed
390
391
}

392
393
394
void AnalyzerManagerPrivate::deactivateDock(QDockWidget *dockWidget)
{
    QAction *toggleViewAction = dockWidget->toggleViewAction();
395
396
    ActionManager::unregisterAction(toggleViewAction,
        Id("Analyzer.").withSuffix(dockWidget->objectName()));
397
398
399
400
401
402
    m_mainWindow->removeDockWidget(dockWidget);
    dockWidget->hide();
    // Prevent saveState storing the data of the wrong children.
    dockWidget->setParent(0);
}

hjk's avatar
hjk committed
403
404
bool AnalyzerManagerPrivate::showPromptDialog(const QString &title, const QString &text,
    const QString &stopButtonText, const QString &cancelButtonText) const
405
{
hjk's avatar
hjk committed
406
    Utils::CheckableMessageBox messageBox(ICore::mainWindow());
407
408
409
410
411
412
413
414
415
416
417
418
419
    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;
}

hjk's avatar
hjk committed
420
bool AnalyzerManagerPrivate::isActionRunnable(AnalyzerAction *action) const
421
422
423
{
    if (!action || m_isRunning)
        return false;
hjk's avatar
hjk committed
424
    if (action->startMode() == StartRemote)
425
426
427
        return true;

    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
hjk's avatar
hjk committed
428
    return pe->canRun(pe->startupProject(), action->tool()->runMode());
429
430
}

431
void AnalyzerManagerPrivate::startTool()
432
{
hjk's avatar
hjk committed
433
434
    QTC_ASSERT(m_currentAction, return);
    m_currentAction->tool()->startTool(m_currentAction->startMode());
435
}
436

437
void AnalyzerManagerPrivate::modeChanged(IMode *mode)
Mike McQuaid's avatar
Mike McQuaid committed
438
{
hjk's avatar
hjk committed
439
440
441
442
443
444
445
446
447
448
    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
449
450
}

451
452
void AnalyzerManagerPrivate::selectSavedTool()
{
hjk's avatar
hjk committed
453
    const QSettings *settings = ICore::settings();
454
455

    if (settings->contains(QLatin1String(LAST_ACTIVE_TOOL))) {
hjk's avatar
hjk committed
456
457
458
459
        const Id lastAction = Id::fromSetting(settings->value(QLatin1String(LAST_ACTIVE_TOOL)));
        foreach (AnalyzerAction *action, m_actions) {
            if (action->id() == lastAction) {
                selectAction(action);
460
461
                return;
            }
462
463
        }
    }
464
    // fallback to first available tool
465
    if (!m_actions.isEmpty())
hjk's avatar
hjk committed
466
        selectAction(m_actions.first());
467
468
}

hjk's avatar
hjk committed
469
void AnalyzerManagerPrivate::selectMenuAction()
470
{
471
    AnalyzerManager::showMode();
hjk's avatar
hjk committed
472
    AnalyzerAction *action = qobject_cast<AnalyzerAction *>(sender());
hjk's avatar
hjk committed
473
    QTC_ASSERT(action, return);
hjk's avatar
hjk committed
474
    selectAction(action);
475
    startTool();
476
477
}

hjk's avatar
hjk committed
478
void AnalyzerManagerPrivate::selectToolboxAction(int index)
479
{
hjk's avatar
hjk committed
480
    selectAction(m_actions[index]);
481
482
}

hjk's avatar
hjk committed
483
void AnalyzerManagerPrivate::selectAction(AnalyzerAction *action)
484
{
485
    if (m_currentAction == action)
Mike McQuaid's avatar
Mike McQuaid committed
486
        return;
487

488
489
490
491
    const int actionIndex = m_actions.indexOf(action);
    QTC_ASSERT(actionIndex >= 0, return);

    // Clean up old tool.
hjk's avatar
hjk committed
492
    if (m_currentAction) {
493
        saveToolSettings(m_currentAction);
hjk's avatar
hjk committed
494
        foreach (QDockWidget *widget, m_toolWidgets.value(m_currentAction->tool()))
495
            deactivateDock(widget);
Mike McQuaid's avatar
Mike McQuaid committed
496
497
    }

498
    // Now change the tool.
499
    m_currentAction = action;
500

hjk's avatar
hjk committed
501
    IAnalyzerTool *tool = action->tool();
502
    if (!m_defaultSettings.contains(tool)) {
503
        QWidget *widget = tool->createWidgets();
504
        QTC_CHECK(widget);
505
        m_defaultSettings.insert(tool, m_mainWindow->saveSettings());
506
        QTC_CHECK(!m_controlsWidgetFromTool.contains(tool));
507
508
        m_controlsWidgetFromTool[tool] = widget;
        m_controlsStackWidget->addWidget(widget);
509
    }
510
511
    foreach (QDockWidget *widget, m_toolWidgets.value(tool))
        activateDock(Qt::DockWidgetArea(widget->property(INITIAL_DOCK_AREA).toInt()), widget);
512

hjk's avatar
hjk committed
513
    loadToolSettings(action);
514

515
    QTC_CHECK(m_controlsWidgetFromTool.contains(tool));
516
    m_controlsStackWidget->setCurrentWidget(m_controlsWidgetFromTool.value(tool));
517
    m_toolBox->setCurrentIndex(actionIndex);
Mike McQuaid's avatar
Mike McQuaid committed
518

519
    updateRunActions();
hjk's avatar
hjk committed
520
521
}

hjk's avatar
hjk committed
522
void AnalyzerManagerPrivate::addAction(AnalyzerAction *action)
hjk's avatar
hjk committed
523
{
hjk's avatar
hjk committed
524
    delayedInit(); // Make sure that there is a valid IMode instance.
525

hjk's avatar
hjk committed
526
527
    IAnalyzerTool *tool = action->tool();

528
    const bool blocked = m_toolBox->blockSignals(true); // Do not make current.
529

hjk's avatar
hjk committed
530
531
532
533
    Id menuGroup = action->menuGroup();
    if (menuGroup.isValid()) {
        Command *command = ActionManager::registerAction(action, action->id(), Context(C_GLOBAL));
        m_menu->addAction(command, menuGroup);
534
    }
hjk's avatar
hjk committed
535

536
    m_actions.append(action);
hjk's avatar
hjk committed
537
    m_toolBox->addItem(action->text());
538
539
    m_toolBox->blockSignals(blocked);
    connect(action, SIGNAL(triggered()), SLOT(selectMenuAction()));
540
    m_toolBox->setEnabled(true);
hjk's avatar
hjk committed
541
}
Mike McQuaid's avatar
Mike McQuaid committed
542

hjk's avatar
hjk committed
543
void AnalyzerManagerPrivate::handleToolStarted()
hjk's avatar
hjk committed
544
{
hjk's avatar
hjk committed
545
    m_isRunning = true; // FIXME: Make less global.
546
    updateRunActions();
hjk's avatar
hjk committed
547
548
549
550
551
}

void AnalyzerManagerPrivate::handleToolFinished()
{
    m_isRunning = false;
552
    updateRunActions();
Mike McQuaid's avatar
Mike McQuaid committed
553
554
}

hjk's avatar
hjk committed
555
void AnalyzerManagerPrivate::loadToolSettings(AnalyzerAction *action)
Mike McQuaid's avatar
Mike McQuaid committed
556
{
hjk's avatar
hjk committed
557
    QTC_ASSERT(m_mainWindow, return);
hjk's avatar
hjk committed
558
    QSettings *settings = ICore::settings();
hjk's avatar
hjk committed
559
    settings->beginGroup(QLatin1String("AnalyzerViewSettings_") + action->id().toString());
560
    if (settings->value(QLatin1String("ToolSettingsSaved"), false).toBool())
hjk's avatar
hjk committed
561
        m_mainWindow->restoreSettings(settings);
562
    else
hjk's avatar
hjk committed
563
        m_mainWindow->restoreSettings(m_defaultSettings.value(action->tool()));
hjk's avatar
hjk committed
564
    settings->endGroup();
Mike McQuaid's avatar
Mike McQuaid committed
565
566
}

hjk's avatar
hjk committed
567
void AnalyzerManagerPrivate::saveToolSettings(AnalyzerAction *action)
Mike McQuaid's avatar
Mike McQuaid committed
568
{
hjk's avatar
hjk committed
569
    QTC_ASSERT(action, return);
hjk's avatar
hjk committed
570
571
    QTC_ASSERT(m_mainWindow, return);

hjk's avatar
hjk committed
572
    QSettings *settings = ICore::settings();
hjk's avatar
hjk committed
573
    settings->beginGroup(QLatin1String("AnalyzerViewSettings_") + action->id().toString());
hjk's avatar
hjk committed
574
    m_mainWindow->saveSettings(settings);
575
    settings->setValue(QLatin1String("ToolSettingsSaved"), true);
hjk's avatar
hjk committed
576
    settings->endGroup();
hjk's avatar
hjk committed
577
    settings->setValue(QLatin1String(LAST_ACTIVE_TOOL), action->id().toString());
Mike McQuaid's avatar
Mike McQuaid committed
578
579
}

580
void AnalyzerManagerPrivate::updateRunActions()
Mike McQuaid's avatar
Mike McQuaid committed
581
{
hjk's avatar
hjk committed
582
583
    ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
    Project *project = pe->startupProject();
hjk's avatar
hjk committed
584
585

    QString disabledReason;
hjk's avatar
hjk committed
586
    if (m_isRunning)
hjk's avatar
hjk committed
587
        disabledReason = tr("An analysis is still in progress.");
hjk's avatar
hjk committed
588
    else if (!m_currentAction)
hjk's avatar
hjk committed
589
590
        disabledReason = tr("No analyzer tool selected.");
    else
hjk's avatar
hjk committed
591
        disabledReason = pe->cannotRunReason(project, m_currentAction->tool()->runMode());
hjk's avatar
hjk committed
592

hjk's avatar
hjk committed
593
    m_startAction->setEnabled(isActionRunnable(m_currentAction));
hjk's avatar
hjk committed
594
    m_startAction->setToolTip(disabledReason);
hjk's avatar
hjk committed
595
596
    m_toolBox->setEnabled(!m_isRunning);
    m_stopAction->setEnabled(m_isRunning);
hjk's avatar
hjk committed
597
    foreach (AnalyzerAction *action, m_actions)
598
        action->setEnabled(isActionRunnable(action));
Mike McQuaid's avatar
Mike McQuaid committed
599
600
}

hjk's avatar
hjk committed
601
602
603
604
605
////////////////////////////////////////////////////////////////////
//
// AnalyzerManager
//
////////////////////////////////////////////////////////////////////
606

607
static AnalyzerManagerPrivate *d = 0;
608

hjk's avatar
hjk committed
609
AnalyzerManager::AnalyzerManager(QObject *parent)
610
  : QObject(parent)
hjk's avatar
hjk committed
611
{
612
613
    QTC_CHECK(d == 0);
    d = new AnalyzerManagerPrivate(this);
614
615
}

hjk's avatar
hjk committed
616
AnalyzerManager::~AnalyzerManager()
Mike McQuaid's avatar
Mike McQuaid committed
617
{
618
    QTC_CHECK(d);
hjk's avatar
hjk committed
619
    delete d;
620
    d = 0;
Mike McQuaid's avatar
Mike McQuaid committed
621
622
}

hjk's avatar
hjk committed
623
void AnalyzerManager::shutdown()
Mike McQuaid's avatar
Mike McQuaid committed
624
{
hjk's avatar
hjk committed
625
626
    if (d->m_currentAction)
        d->saveToolSettings(d->m_currentAction);
Mike McQuaid's avatar
Mike McQuaid committed
627
628
}

hjk's avatar
hjk committed
629
void AnalyzerManager::addAction(AnalyzerAction *action)
Mike McQuaid's avatar
Mike McQuaid committed
630
{
hjk's avatar
hjk committed
631
    d->addAction(action);
Mike McQuaid's avatar
Mike McQuaid committed
632
633
}

hjk's avatar
hjk committed
634
635
QDockWidget *AnalyzerManager::createDockWidget(IAnalyzerTool *tool, const QString &title,
                                               QWidget *widget, Qt::DockWidgetArea area)
Mike McQuaid's avatar
Mike McQuaid committed
636
{
637
    QTC_ASSERT(!widget->objectName().isEmpty(), return 0);
hjk's avatar
hjk committed
638
639
    QDockWidget *dockWidget = d->m_mainWindow->addDockForWidget(widget);
    dockWidget->setProperty(INITIAL_DOCK_AREA, int(area));
640
    d->m_dockWidgets.append(AnalyzerManagerPrivate::DockPtr(dockWidget));
hjk's avatar
hjk committed
641
642
643
    dockWidget->setWindowTitle(title);
    d->m_toolWidgets[tool].push_back(dockWidget);
    return dockWidget;
Mike McQuaid's avatar
Mike McQuaid committed
644
645
}

646
void AnalyzerManager::selectTool(IAnalyzerTool *tool, StartMode mode)
hjk's avatar
hjk committed
647
{
hjk's avatar
hjk committed
648
649
650
    foreach (AnalyzerAction *action, d->m_actions)
        if (action->tool() == tool && action->startMode() == mode)
            d->selectAction(action);
hjk's avatar
hjk committed
651
652
}

653
void AnalyzerManager::startTool()
hjk's avatar
hjk committed
654
{
655
    d->startTool();
hjk's avatar
hjk committed
656
}
dt_'s avatar
dt_ committed
657

658
Utils::FancyMainWindow *AnalyzerManager::mainWindow()
hjk's avatar
hjk committed
659
{
660
    return d->m_mainWindow;
hjk's avatar
hjk committed
661
}
662

663
void AnalyzerManagerPrivate::resetLayout()
hjk's avatar
hjk committed
664
{
hjk's avatar
hjk committed
665
666
    QTC_ASSERT(m_currentAction, return);
    m_mainWindow->restoreSettings(m_defaultSettings.value(m_currentAction->tool()));
Mike McQuaid's avatar
Mike McQuaid committed
667
}
668

669
670
void AnalyzerManager::showStatusMessage(const QString &message, int timeoutMS)
{
671
    d->m_statusLabel->showStatusMessage(message, timeoutMS);
672
673
674
675
676
677
678
}

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

679
680
void AnalyzerManager::showMode()
{
681
682
    if (d->m_mode)
        ModeManager::activateMode(d->m_mode->id());
683
684
}

hjk's avatar
hjk committed
685
void AnalyzerManager::stopTool()
686
{
hjk's avatar
hjk committed
687
    stopAction()->trigger();
688
689
}

hjk's avatar
hjk committed
690
QAction *AnalyzerManager::stopAction()
691
{
692
    return d->m_stopAction;
693
694
}

695
696
void AnalyzerManager::handleToolStarted()
{
697
    d->handleToolStarted();
698
699
}

hjk's avatar
hjk committed
700
void AnalyzerManager::handleToolFinished()
701
{
702
    d->handleToolFinished();
703
704
}

705
AnalyzerRunControl *AnalyzerManager::createRunControl(
706
    const AnalyzerStartParameters &sp, RunConfiguration *runConfiguration)
hjk's avatar
hjk committed
707
{
hjk's avatar
hjk committed
708
709
710
    foreach (AnalyzerAction *action, d->m_actions) {
        IAnalyzerTool *tool = action->tool();
        if (tool->runMode() == sp.runMode && action->startMode() == sp.startMode)
hjk's avatar
hjk committed
711
            return tool->createRunControl(sp, runConfiguration);
hjk's avatar
hjk committed
712
    }
hjk's avatar
hjk committed
713
    QTC_CHECK(false);
714
    return 0;
hjk's avatar
hjk committed
715
716
}

717
718
} // namespace Analyzer

719
#include "analyzermanager.moc"