cmakelocatorfilter.cpp 4.91 KB
Newer Older
1 2
/**************************************************************************
**
3
** Copyright (C) 2014 Kläralvdalens Datakonsult AB, a KDAB Group company.
Nicolas Arnaud-Cormos's avatar
Nicolas Arnaud-Cormos committed
4
** Contact: Kläralvdalens Datakonsult AB (info@kdab.com)
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
15
**
hjk's avatar
hjk committed
16 17 18 19 20 21 22 23 24 25
** 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
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32 33 34 35

#include "cmakelocatorfilter.h"
#include "cmakeproject.h"
#include "makestep.h"

#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
Tobias Hunger's avatar
Tobias Hunger committed
36
#include <projectexplorer/target.h>
37 38
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/buildsteplist.h>
39
#include <utils/fileutils.h>
40 41 42 43


using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
hjk's avatar
hjk committed
44
using namespace ProjectExplorer;
45
using namespace Utils;
46 47 48

CMakeLocatorFilter::CMakeLocatorFilter()
{
hjk's avatar
hjk committed
49 50
    setId("Build CMake target");
    setDisplayName(tr("Build CMake target"));
51 52
    setShortcutString(QLatin1String("cm"));

hjk's avatar
hjk committed
53
    connect(SessionManager::instance(), SIGNAL(projectAdded(ProjectExplorer::Project*)),
54
            this, SLOT(slotProjectListUpdated()));
hjk's avatar
hjk committed
55
    connect(SessionManager::instance(), SIGNAL(projectRemoved(ProjectExplorer::Project*)),
56
            this, SLOT(slotProjectListUpdated()));
57 58 59

    // Initialize the filter
    slotProjectListUpdated();
60 61 62 63 64 65 66
}

CMakeLocatorFilter::~CMakeLocatorFilter()
{

}

67
QList<Core::LocatorFilterEntry> CMakeLocatorFilter::matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry)
68 69
{
    Q_UNUSED(future)
70
    QList<Core::LocatorFilterEntry> result;
71

hjk's avatar
hjk committed
72
    foreach (Project *p, SessionManager::projects()) {
73 74
        CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(p);
        if (cmakeProject) {
75
            foreach (const CMakeBuildTarget &ct, cmakeProject->buildTargets()) {
76
                if (ct.title.contains(entry)) {
77 78
                    Core::LocatorFilterEntry entry(this, ct.title, cmakeProject->projectFilePath().toString());
                    entry.extraInfo = FileUtils::shortNativePath(cmakeProject->projectFilePath());
79 80 81 82 83 84 85 86 87
                    result.append(entry);
                }
            }
        }
    }

    return result;
}

88
void CMakeLocatorFilter::accept(Core::LocatorFilterEntry selection) const
89 90 91 92
{
    // Get the project containing the target selected
    CMakeProject *cmakeProject = 0;

hjk's avatar
hjk committed
93
    foreach (Project *p, SessionManager::projects()) {
94
        cmakeProject = qobject_cast<CMakeProject *>(p);
95
        if (cmakeProject && cmakeProject->projectFilePath().toString() == selection.internalData.toString())
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            break;
        cmakeProject = 0;
    }
    if (!cmakeProject)
        return;

    // Find the make step
    MakeStep *makeStep = 0;
    ProjectExplorer::BuildStepList *buildStepList = cmakeProject->activeTarget()->activeBuildConfiguration()
            ->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD);
    for (int i = 0; i < buildStepList->count(); ++i) {
        makeStep = qobject_cast<MakeStep *>(buildStepList->at(i));
        if (makeStep)
            break;
    }
    if (!makeStep)
        return;

    // Change the make step to build only the given target
    QStringList oldTargets = makeStep->buildTargets();
    makeStep->setClean(false);
    makeStep->clearBuildTargets();
    makeStep->setBuildTarget(selection.displayName, true);

    // Build
121
    ProjectExplorerPlugin::buildProject(cmakeProject);
122 123 124 125 126 127 128 129 130 131 132 133
    makeStep->setBuildTargets(oldTargets);
}

void CMakeLocatorFilter::refresh(QFutureInterface<void> &future)
{
    Q_UNUSED(future)
}

void CMakeLocatorFilter::slotProjectListUpdated()
{
    CMakeProject *cmakeProject = 0;

hjk's avatar
hjk committed
134
    foreach (Project *p, SessionManager::projects()) {
135 136 137 138 139
        cmakeProject = qobject_cast<CMakeProject *>(p);
        if (cmakeProject)
            break;
    }

140 141
    // Enable the filter if there's at least one CMake project
    setEnabled(cmakeProject);
142
}