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

#include "qbsprojectmanagerplugin.h"

#include "qbsbuildconfiguration.h"
#include "qbsbuildstep.h"
#include "qbscleanstep.h"
35
#include "qbsdeployconfigurationfactory.h"
Tobias Hunger's avatar
Tobias Hunger committed
36
#include "qbsinstallstep.h"
37
#include "qbsnodes.h"
Tobias Hunger's avatar
Tobias Hunger committed
38 39 40
#include "qbsproject.h"
#include "qbsprojectmanager.h"
#include "qbsprojectmanagerconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
41
#include "qbsrunconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
42 43 44

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
45 46 47
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
Tobias Hunger's avatar
Tobias Hunger committed
48
#include <coreplugin/icore.h>
49
#include <coreplugin/fileiconprovider.h>
Tobias Hunger's avatar
Tobias Hunger committed
50 51 52 53 54
#include <coreplugin/mimedatabase.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h>
55
#include <projectexplorer/session.h>
Tobias Hunger's avatar
Tobias Hunger committed
56
#include <projectexplorer/target.h>
57
#include <qtsupport/qtsupportconstants.h>
Tobias Hunger's avatar
Tobias Hunger committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <utils/qtcassert.h>

#include <QAction>
#include <QtPlugin>

namespace QbsProjectManager {
namespace Internal {

QbsProjectManagerPlugin::QbsProjectManagerPlugin() :
    m_manager(0),
    m_projectExplorer(0),
    m_currentProject(0),
    m_currentTarget(0),
    m_currentNode(0)
{ }

bool QbsProjectManagerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
76
    Q_UNUSED(errorMessage);
Tobias Hunger's avatar
Tobias Hunger committed
77 78 79
    m_manager = new QbsManager(this);
    m_projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
    const Core::Context projectContext(::QbsProjectManager::Constants::PROJECT_ID);
80
    const Core::Context globalcontext(Core::Constants::C_GLOBAL);
Tobias Hunger's avatar
Tobias Hunger committed
81 82 83

    Q_UNUSED(arguments);

84 85 86 87
    Core::FileIconProvider::instance()
            ->registerIconOverlayForSuffix(QIcon(QLatin1String(QtSupport::Constants::ICON_QT_PROJECT)),
                                           QLatin1String("qbs"));

Tobias Hunger's avatar
Tobias Hunger committed
88 89 90 91 92
    //create and register objects
    addAutoReleasedObject(m_manager);
    addAutoReleasedObject(new QbsBuildConfigurationFactory);
    addAutoReleasedObject(new QbsBuildStepFactory);
    addAutoReleasedObject(new QbsCleanStepFactory);
Tobias Hunger's avatar
Tobias Hunger committed
93
    addAutoReleasedObject(new QbsInstallStepFactory);
94
    addAutoReleasedObject(new QbsDeployConfigurationFactory);
Tobias Hunger's avatar
Tobias Hunger committed
95
    addAutoReleasedObject(new QbsRunConfigurationFactory);
Tobias Hunger's avatar
Tobias Hunger committed
96 97 98 99 100 101 102 103

    //menus
    // Build Menu:
    Core::ActionContainer *mbuild =
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
    // PE Context menu for projects
    Core::ActionContainer *mproject =
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
104 105
    Core::ActionContainer *msubproject =
             Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
Tobias Hunger's avatar
Tobias Hunger committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    Core::ActionContainer *mfile =
            Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);


    //register actions
    Core::Command *command;

    m_reparseQbs = new QAction(tr("Reparse Qbs"), this);
    command = Core::ActionManager::registerAction(m_reparseQbs, Constants::ACTION_REPARSE_QBS, projectContext);
    command->setAttribute(Core::Command::CA_Hide);
    mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
    connect(m_reparseQbs, SIGNAL(triggered()), this, SLOT(reparseCurrentProject()));

    m_reparseQbsCtx = new QAction(tr("Reparse Qbs"), this);
    command = Core::ActionManager::registerAction(m_reparseQbsCtx, Constants::ACTION_REPARSE_QBS_CONTEXT, projectContext);
    command->setAttribute(Core::Command::CA_Hide);
    mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
    connect(m_reparseQbsCtx, SIGNAL(triggered()), this, SLOT(reparseCurrentProject()));

    m_buildFileContextMenu = new QAction(tr("Build"), this);
126
    command = Core::ActionManager::registerAction(m_buildFileContextMenu, Constants::ACTION_BUILD_FILE_CONTEXT, projectContext);
Tobias Hunger's avatar
Tobias Hunger committed
127 128 129 130
    command->setAttribute(Core::Command::CA_Hide);
    mfile->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
    connect(m_buildFileContextMenu, SIGNAL(triggered()), this, SLOT(buildFileContextMenu()));

131 132 133 134 135 136 137 138 139 140
    m_buildFile = new Utils::ParameterAction(tr("Build File"), tr("Build File \"%1\""),
                                                   Utils::ParameterAction::AlwaysEnabled, this);
    command = Core::ActionManager::registerAction(m_buildFile, Constants::ACTION_BUILD_FILE, globalcontext);
    command->setAttribute(Core::Command::CA_Hide);
    command->setAttribute(Core::Command::CA_UpdateText);
    command->setDescription(m_buildFile->text());
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+B")));
    mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
    connect(m_buildFile, SIGNAL(triggered()), this, SLOT(buildFile()));

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    m_buildProductContextMenu = new QAction(tr("Build"), this);
    command = Core::ActionManager::registerAction(m_buildProductContextMenu, Constants::ACTION_BUILD_PRODUCT_CONTEXT, projectContext);
    command->setAttribute(Core::Command::CA_Hide);
    msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
    connect(m_buildProductContextMenu, SIGNAL(triggered()), this, SLOT(buildProductContextMenu()));

    m_buildProduct = new Utils::ParameterAction(tr("Build Product"), tr("Build Product \"%1\""),
                                                Utils::ParameterAction::AlwaysEnabled, this);
    command = Core::ActionManager::registerAction(m_buildProduct, Constants::ACTION_BUILD_PRODUCT, globalcontext);
    command->setAttribute(Core::Command::CA_Hide);
    command->setAttribute(Core::Command::CA_UpdateText);
    command->setDescription(m_buildFile->text());
    command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+Shift+B")));
    mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
    connect(m_buildProduct, SIGNAL(triggered()), this, SLOT(buildProduct()));

Tobias Hunger's avatar
Tobias Hunger committed
157 158 159 160 161 162 163
    // Connect
    connect(m_projectExplorer, SIGNAL(currentNodeChanged(ProjectExplorer::Node*,ProjectExplorer::Project*)),
            this, SLOT(updateContextActions(ProjectExplorer::Node*,ProjectExplorer::Project*)));

    connect(m_projectExplorer->buildManager(), SIGNAL(buildStateChanged(ProjectExplorer::Project*)),
            this, SLOT(buildStateChanged(ProjectExplorer::Project*)));

164
    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
165
            this, SLOT(updateBuildActions()));
166

Tobias Hunger's avatar
Tobias Hunger committed
167 168 169
    // Run initial setup routines
    updateContextActions(0, 0);
    updateReparseQbsAction();
170
    updateBuildActions();
Tobias Hunger's avatar
Tobias Hunger committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    return true;
}

void QbsProjectManagerPlugin::extensionsInitialized()
{ }

void QbsProjectManagerPlugin::updateContextActions(ProjectExplorer::Node *node, ProjectExplorer::Project *project)
{
    if (m_currentProject) {
        disconnect(m_currentProject, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
                   this, SLOT(activeTargetChanged()));
        disconnect(m_currentProject, SIGNAL(projectParsingStarted()),
                   this, SLOT(parsingStateChanged()));
        disconnect(m_currentProject, SIGNAL(projectParsingDone(bool)),
                   this, SLOT(parsingStateChanged()));
    }

    m_currentNode = node;
    m_currentProject = qobject_cast<Internal::QbsProject *>(project);
    if (m_currentProject) {
        connect(m_currentProject, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
                this, SLOT(activeTargetChanged()));
        connect(m_currentProject, SIGNAL(projectParsingStarted()),
                this, SLOT(parsingStateChanged()));
        connect(m_currentProject, SIGNAL(projectParsingDone(bool)),
                this, SLOT(parsingStateChanged()));
    }

    activeTargetChanged();

    bool isBuilding = m_projectExplorer->buildManager()->isBuilding(project);
    bool isFile = m_currentProject && node && (node->nodeType() == ProjectExplorer::FileNodeType);
204
    bool isProduct = m_currentProject && node && qobject_cast<QbsProductNode *>(node->projectNode());
Tobias Hunger's avatar
Tobias Hunger committed
205 206 207 208
    bool isFileEnabled = isFile && node->isEnabled();

    m_reparseQbsCtx->setEnabled(!isBuilding && m_currentProject && !m_currentProject->isParsing());
    m_buildFileContextMenu->setEnabled(isFileEnabled);
209
    m_buildProductContextMenu->setEnabled(isProduct);
Tobias Hunger's avatar
Tobias Hunger committed
210 211 212 213 214 215 216 217 218
}

void QbsProjectManagerPlugin::updateReparseQbsAction()
{
    m_reparseQbs->setEnabled(m_currentProject
                             && !m_projectExplorer->buildManager()->isBuilding(m_currentProject)
                             && !m_currentProject->isParsing());
}

219
void QbsProjectManagerPlugin::updateBuildActions()
220
{
221 222 223 224
    bool fileVisible = false;
    bool fileEnabled = false;
    bool productVisible = false;
    bool productEnabled = false;
225 226

    QString file;
227

228 229 230 231 232 233 234 235
    if (Core::IEditor *currentEditor = Core::EditorManager::currentEditor()) {
        file = currentEditor->document()->fileName();
        ProjectExplorer::SessionManager *session = m_projectExplorer->session();
        ProjectExplorer::Node *node  = session->nodeForFile(file);
        ProjectExplorer::Project *project
                = qobject_cast<QbsProject *>(session->projectForFile(file));

        m_buildFile->setParameter(QFileInfo(file).fileName());
236 237
        fileVisible = project && node && qobject_cast<QbsBaseProjectNode *>(node->projectNode());
        fileEnabled = !m_projectExplorer->buildManager()->isBuilding(project)
238
                && m_currentProject && !m_currentProject->isParsing();
239 240 241 242 243 244

        if (QbsProductNode *productNode = qobject_cast<QbsProductNode *>(node->projectNode())) {
            productEnabled = true;
            productVisible = true;
            m_buildProduct->setParameter(productNode->displayName());
        }
245 246
    }

247 248 249 250 251
    m_buildFile->setEnabled(fileEnabled);
    m_buildFile->setVisible(fileVisible);

    m_buildProduct->setEnabled(productEnabled);
    m_buildProduct->setVisible(productVisible);
252 253
}

Tobias Hunger's avatar
Tobias Hunger committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
void QbsProjectManagerPlugin::activeTargetChanged()
{
    if (m_currentTarget)
        disconnect(m_currentTarget, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
                   this, SLOT(updateReparseQbsAction()));

    m_currentTarget = m_currentProject ? m_currentProject->activeTarget() : 0;

    if (m_currentTarget)
        connect(m_currentTarget, SIGNAL(activeBuildConfigurationChanged(ProjectExplorer::BuildConfiguration*)),
                this, SLOT(updateReparseQbsAction()));

    updateReparseQbsAction();
}

void QbsProjectManagerPlugin::buildStateChanged(ProjectExplorer::Project *project)
{
    if (project == m_currentProject) {
        updateReparseQbsAction();
        updateContextActions(m_currentNode, m_currentProject);
274
        updateBuildActions();
Tobias Hunger's avatar
Tobias Hunger committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    }
}

void QbsProjectManagerPlugin::parsingStateChanged()
{
    if (m_currentProject) {
        updateReparseQbsAction();
        updateContextActions(m_currentNode, m_currentProject);
    }
}

void QbsProjectManagerPlugin::buildFileContextMenu()
{
    QTC_ASSERT(m_currentNode, return);
    QTC_ASSERT(m_currentProject, return);

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    buildFiles(m_currentProject, QStringList(m_currentNode->path()));
}

void QbsProjectManagerPlugin::buildFile()
{
    QString file;
    QbsProject *project = 0;
    if (Core::IEditor *currentEditor = Core::EditorManager::currentEditor()) {
        file = currentEditor->document()->fileName();
        project = qobject_cast<QbsProject *>(m_projectExplorer->session()->projectForFile(file));
    }

    if (!project || file.isEmpty())
        return;

    buildFiles(project, QStringList(file));
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
void QbsProjectManagerPlugin::buildProductContextMenu()
{
    QTC_ASSERT(m_currentNode, return);
    QTC_ASSERT(m_currentProject, return);

    buildProducts(m_currentProject, QStringList(m_currentNode->displayName()));
}

void QbsProjectManagerPlugin::buildProduct()
{
    QbsProject *project = 0;
    QbsProductNode *product = 0;
    if (Core::IEditor *currentEditor = Core::EditorManager::currentEditor()) {
        const QString file = currentEditor->document()->fileName();
        ProjectExplorer::SessionManager *session = m_projectExplorer->session();

        project = qobject_cast<QbsProject *>(session->projectForFile(file));
        product = qobject_cast<QbsProductNode *>(session->nodeForFile(file)->projectNode());
    }

    if (!project || !product)
        return;

    buildProducts(project, QStringList(product->displayName()));
}

335 336 337 338 339 340
void QbsProjectManagerPlugin::buildFiles(QbsProject *project, const QStringList &files)
{
    QTC_ASSERT(project, return);
    QTC_ASSERT(!files.isEmpty(), return);

    ProjectExplorer::Target *t = project->activeTarget();
Tobias Hunger's avatar
Tobias Hunger committed
341 342 343 344 345 346 347 348 349 350
    if (!t)
        return;
    QbsBuildConfiguration *bc = qobject_cast<QbsBuildConfiguration *>(t->activeBuildConfiguration());
    if (!bc)
        return;

    ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
    if (!pe->saveModifiedFiles())
        return;

351
    bc->setChangedFiles(files);
352
    bc->setProducts(QStringList());
Tobias Hunger's avatar
Tobias Hunger committed
353 354 355 356 357 358 359 360 361

    const Core::Id buildStep = Core::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD);

    const QString name = ProjectExplorer::ProjectExplorerPlugin::displayNameForStepId(buildStep);
    pe->buildManager()->buildList(bc->stepList(buildStep), name);

    bc->setChangedFiles(QStringList());
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
void QbsProjectManagerPlugin::buildProducts(QbsProject *project, const QStringList &products)
{
    QTC_ASSERT(project, return);
    QTC_ASSERT(!products.isEmpty(), return);

    ProjectExplorer::Target *t = project->activeTarget();
    if (!t)
        return;
    QbsBuildConfiguration *bc = qobject_cast<QbsBuildConfiguration *>(t->activeBuildConfiguration());
    if (!bc)
        return;

    ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
    if (!pe->saveModifiedFiles())
        return;

    bc->setChangedFiles(QStringList());
    bc->setProducts(products);

    const Core::Id buildStep = Core::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD);

    const QString name = ProjectExplorer::ProjectExplorerPlugin::displayNameForStepId(buildStep);
    pe->buildManager()->buildList(bc->stepList(buildStep), name);

    bc->setProducts(QStringList());
}

Tobias Hunger's avatar
Tobias Hunger committed
389 390 391 392 393 394 395 396 397 398
void QbsProjectManagerPlugin::reparseCurrentProject()
{
    if (m_currentProject)
        m_currentProject->parseCurrentBuildConfiguration();
}

} // namespace Internal
} // namespace QbsProjectManager

Q_EXPORT_PLUGIN(QbsProjectManager::Internal::QbsProjectManagerPlugin)