runconfiguration.h 10.5 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34 35 36
#ifndef RUNCONFIGURATION_H
#define RUNCONFIGURATION_H

37
#include "abi.h"
38
#include "projectconfiguration.h"
con's avatar
con committed
39
#include "projectexplorer_export.h"
40
#include "outputformat.h"
con's avatar
con committed
41 42

#include <QtCore/QMetaType>
43
#include <QtCore/QWeakPointer>
44
#include <QtGui/QWidget>
45
#include <QtGui/QIcon>
con's avatar
con committed
46 47 48

namespace ProjectExplorer {

49
class BuildConfiguration;
50
class IRunConfigurationAspect;
51 52 53
class OutputFormatter;
class RunControl;
class Target;
con's avatar
con committed
54

55 56
/**
 * Base class for a run configuration. A run configuration specifies how a
Tobias Hunger's avatar
Tobias Hunger committed
57
 * target should be run, while the runner (see below) does the actual running.
con's avatar
con committed
58
 *
Tobias Hunger's avatar
Tobias Hunger committed
59 60
 * Note that all RunControls and the target hold a shared pointer to the RunConfiguration.
 * That is the lifetime of the RunConfiguration might exceed the life of the target.
con's avatar
con committed
61
 * The user might still have a RunControl running (or output tab of that RunControl open)
Tobias Hunger's avatar
Tobias Hunger committed
62
 * and yet unloaded the target.
63
 * Also a RunConfiguration might be already removed from the list of RunConfigurations
Tobias Hunger's avatar
Tobias Hunger committed
64 65
 * for a target, but stil be runnable via the output tab.
 */
66
class PROJECTEXPLORER_EXPORT RunConfiguration : public ProjectConfiguration
con's avatar
con committed
67 68
{
    Q_OBJECT
69

con's avatar
con committed
70 71 72
public:
    virtual ~RunConfiguration();

73 74 75 76 77
    /**
     * Used to find out whether a runconfiguration works with the given
     * buildconfiguration.
     * \note bc may be 0!
     */
78
    virtual bool isEnabled(BuildConfiguration *bc) const;
79 80 81 82 83

    /**
     * Used to find out whether a runconfiguration works with the active
     * buildconfiguration.
     */
84
    bool isEnabled() const;
85

86 87
    /// Returns the widget used to configure this run configuration. Ownership is transferred to the caller
    virtual QWidget *createConfigurationWidget() = 0;
con's avatar
con committed
88

Tobias Hunger's avatar
Tobias Hunger committed
89
    Target *target() const;
90

dt's avatar
dt committed
91 92
    virtual ProjectExplorer::OutputFormatter *createOutputFormatter() const;

93 94 95 96
    void setUseQmlDebugger(bool value);
    void setUseCppDebugger(bool value);
    bool useQmlDebugger() const;
    bool useCppDebugger() const;
97 98 99 100

    uint qmlDebugServerPort() const;
    void setQmlDebugServerPort(uint port);

101
    virtual QVariantMap toMap() const;
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    // aspects are a mechanism to add RunControl-specific options to a RunConfiguration without
    // subclassing the RunConfiguration for every addition, preventing a combinatorical explosion
    // of subclasses or the need to add all options to the base class.
    QList<IRunConfigurationAspect *> extraAspects() const;
    template <typename T> T *extraAspect() const
    {
        IRunConfigurationAspect *typeCheck = static_cast<T *>(0);
        Q_UNUSED(typeCheck);
        T *result = 0;
        foreach (IRunConfigurationAspect *a, m_aspects) {
            if ((result = dynamic_cast<T *>(a)) != 0)
                break;
        }
        return result;
    }

119
    virtual ProjectExplorer::Abi abi() const;
120

121 122
signals:
    void isEnabledChanged(bool value);
123
    void debuggersChanged();
124
    void qmlDebugServerPortChanged(uint port);
125

126
protected:
Tobias Hunger's avatar
Tobias Hunger committed
127 128
    RunConfiguration(Target *parent, const QString &id);
    RunConfiguration(Target *parent, RunConfiguration *source);
129

130
    /// convenience method to get current build configuration.
131
    BuildConfiguration *activeBuildConfiguration() const;
132 133 134 135

    virtual bool fromMap(const QVariantMap &map);

private:
136 137
    void addExtraAspects();

138 139
    bool m_useCppDebugger;
    bool m_useQmlDebugger;
140
    uint m_qmlDebugServerPort;
141 142 143 144 145 146 147 148 149 150 151 152
    QList<IRunConfigurationAspect *> m_aspects;
};

class PROJECTEXPLORER_EXPORT IRunConfigurationAspect
{
public:
    virtual ~IRunConfigurationAspect() {}
    virtual QVariantMap toMap() const = 0;
    virtual QString displayName() const = 0;
protected:
    friend class RunConfiguration;
    virtual bool fromMap(const QVariantMap &map) = 0;
con's avatar
con committed
153 154
};

155 156
/**
 * The run configuration factory is used for restoring run configurations from
con's avatar
con committed
157
 * settings. And used to create new runconfigurations in the "Run Settings" Dialog.
Tobias Hunger's avatar
Tobias Hunger committed
158 159 160
 * For the first case bool canRestore(Target *parent, const QString &id) and
 * RunConfiguration* create(Target *parent, const QString &id) are used.
 * For the second type the functions QStringList availableCreationIds(Target *parent) and
con's avatar
con committed
161
 * QString displayNameForType(const QString&) are used to generate a list of creatable
con's avatar
con committed
162 163
 * RunConfigurations, and create(..) is used to create it.
 */
164
class PROJECTEXPLORER_EXPORT IRunConfigurationFactory : public QObject
con's avatar
con committed
165 166
{
    Q_OBJECT
167

con's avatar
con committed
168
public:
169
    explicit IRunConfigurationFactory(QObject *parent = 0);
con's avatar
con committed
170
    virtual ~IRunConfigurationFactory();
171

172
    /// used to show the list of possible additons to a target, returns a list of types
Tobias Hunger's avatar
Tobias Hunger committed
173
    virtual QStringList availableCreationIds(Target *parent) const = 0;
174 175

    /// used to translate the types to names to display to the user
176
    virtual QString displayNameForId(const QString &id) const = 0;
177

Tobias Hunger's avatar
Tobias Hunger committed
178 179 180 181 182 183 184 185 186 187
    virtual bool canCreate(Target *parent, const QString &id) const = 0;
    virtual RunConfiguration *create(Target *parent, const QString &id) = 0;
    virtual bool canRestore(Target *parent, const QVariantMap &map) const = 0;
    virtual RunConfiguration *restore(Target *parent, const QVariantMap &map) = 0;
    virtual bool canClone(Target *parent, RunConfiguration *product) const = 0;
    virtual RunConfiguration *clone(Target *parent, RunConfiguration *product) = 0;

    static IRunConfigurationFactory *createFactory(Target *parent, const QString &id);
    static IRunConfigurationFactory *cloneFactory(Target *parent, RunConfiguration *source);
    static IRunConfigurationFactory *restoreFactory(Target *parent, const QVariantMap &map);
188 189 190

signals:
    void availableCreationIdsChanged();
con's avatar
con committed
191 192
};

193 194
class RunConfigWidget;

195
class PROJECTEXPLORER_EXPORT IRunControlFactory : public QObject
con's avatar
con committed
196 197 198
{
    Q_OBJECT
public:
199 200 201
    explicit IRunControlFactory(QObject *parent = 0);
    virtual ~IRunControlFactory();

202 203
    virtual bool canRun(RunConfiguration *runConfiguration, const QString &mode) const = 0;
    virtual RunControl* create(RunConfiguration *runConfiguration, const QString &mode) = 0;
con's avatar
con committed
204 205 206

    virtual QString displayName() const = 0;

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    /// Return an IRunConfigurationAspect to carry options for RunControls this factory can create.
    /// If no extra options are required it is allowed to return null like the default implementation does.
    /// This is intended to be called from the RunConfiguration constructor, so passing a RunConfiguration
    /// pointer makes no sense because that object is under construction at the time.
    virtual IRunConfigurationAspect *createRunConfigurationAspect();

    /// Return a widget used to configure this runner. Ownership is transferred to the caller.
    /// If @p runConfiguration is not suitable for RunControls from this factory, or no user-accesible
    /// configuration is required, return null.
    virtual RunConfigWidget *createConfigurationWidget(RunConfiguration *runConfiguration) = 0;
};

class PROJECTEXPLORER_EXPORT RunConfigWidget
    : public QWidget
{
    Q_OBJECT
public:
    RunConfigWidget()
        : QWidget(0)
    {}

    virtual QString displayName() const = 0;

signals:
    void displayNameChanged(const QString &);
con's avatar
con committed
232 233
};

234 235
/**
 * Each instance of this class represents one item that is run.
con's avatar
con committed
236
 */
237 238
class PROJECTEXPLORER_EXPORT RunControl : public QObject
{
con's avatar
con committed
239 240
    Q_OBJECT
public:
241 242
    enum StopResult {
        StoppedSynchronously, // Stopped.
243
        AsynchronousStop     // Stop sequence has been started
244 245
    };

dt's avatar
dt committed
246
    explicit RunControl(RunConfiguration *runConfiguration, QString mode);
con's avatar
con committed
247 248
    virtual ~RunControl();
    virtual void start() = 0;
249 250 251 252

    // Prompt to stop. If 'optionalPrompt' is passed, a "Do not ask again"-
    // checkbox will show and the result will be returned in '*optionalPrompt'.
    virtual bool promptToStop(bool *optionalPrompt = 0) const;
253
    virtual StopResult stop() = 0;
con's avatar
con committed
254
    virtual bool isRunning() const = 0;
255
    virtual QString displayName() const;
256 257 258 259
    /// \returns the icon to be shown in the outputwindow
    // TODO the icon differs currently only per "mode"
    // so this is more flexibel then it needs to be
    virtual QIcon icon() const = 0;
260

261
    bool sameRunConfiguration(const RunControl *other) const;
262

dt's avatar
dt committed
263
    OutputFormatter *outputFormatter();
dt's avatar
dt committed
264
    QString runMode() const;
265

266 267 268 269
public slots:
    void bringApplicationToForeground(qint64 pid);
    void appendMessage(const QString &msg, ProjectExplorer::OutputFormat format);

con's avatar
con committed
270
signals:
271 272
    void appendMessage(ProjectExplorer::RunControl *runControl,
        const QString &msg, ProjectExplorer::OutputFormat format);
con's avatar
con committed
273 274
    void started();
    void finished();
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
275

con's avatar
con committed
276 277 278
private slots:
    void bringApplicationToForegroundInternal();

279 280 281 282 283 284 285
protected:
    // Utility to prompt to terminate application with checkable box.
    bool showPromptToStopDialog(const QString &title, const QString &text,
                                const QString &stopButtonText = QString(),
                                const QString &cancelButtonText = QString(),
                                bool *prompt = 0) const;

con's avatar
con committed
286
private:
287
    QString m_displayName;
dt's avatar
dt committed
288
    QString m_runMode;
289
    const QWeakPointer<RunConfiguration> m_runConfiguration;
dt's avatar
dt committed
290
    OutputFormatter *m_outputFormatter;
con's avatar
con committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304

#ifdef Q_OS_MAC
    //these two are used to bring apps in the foreground on Mac
    qint64 m_internalPid;
    int m_foregroundCount;
#endif
};

} // namespace ProjectExplorer

// Allow a RunConfiguration to be stored in a QVariant
Q_DECLARE_METATYPE(ProjectExplorer::RunConfiguration*)

#endif // RUNCONFIGURATION_H