targetsetuppage.cpp 14.9 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "targetsetuppage.h"

#include "qt4project.h"
Tobias Hunger's avatar
Tobias Hunger committed
33
#include "qt4projectmanagerconstants.h"
34
35
#include "qt4target.h"

36
#include <QtGui/QFileDialog>
37
38
39
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLayout>
40
#include <QtGui/QMessageBox>
41
#include <QtGui/QPushButton>
42
43
44
45
46
#include <QtGui/QTreeWidget>

using namespace Qt4ProjectManager::Internal;

TargetSetupPage::TargetSetupPage(QWidget *parent) :
Tobias Hunger's avatar
Tobias Hunger committed
47
48
    QWizardPage(parent),
    m_preferMobile(false)
49
50
51
52
53
54
{
    resize(500, 400);
    setTitle(tr("Set up targets for your project"));

    QVBoxLayout *vbox = new QVBoxLayout(this);

55
56
57
58
    m_importLabel = new QLabel(this);
    m_importLabel->setText(tr("Qt Creator can set up the following targets:"));
    m_importLabel->setWordWrap(true);
    vbox->addWidget(m_importLabel);
59
60
61
62
63
64
65
66

    m_treeWidget = new QTreeWidget(this);
    m_treeWidget->setColumnCount(3);
    m_treeWidget->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    m_treeWidget->header()->setResizeMode(1, QHeaderView::ResizeToContents);
    m_treeWidget->setHeaderLabels(QStringList() << tr("Qt Version") << tr("Status") << tr("Directory"));
    vbox->addWidget(m_treeWidget);

Tobias Hunger's avatar
Tobias Hunger committed
67
    m_addDirectoryButton = new QPushButton(tr("Add shadow build location..."));
68
    vbox->addWidget(m_addDirectoryButton);
69

70
71
    connect(m_addDirectoryButton, SIGNAL(clicked()),
            this, SLOT(addShadowBuildLocation()));
72
73
74
75
76
77
78
79
80
81
82
83
}

TargetSetupPage::~TargetSetupPage()
{
    resetInfos();
}

void TargetSetupPage::setImportInfos(const QList<ImportInfo> &infos)
{
    disconnect(m_treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
               this, SLOT(itemWasChanged()));

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    // Create a list of all temporary Qt versions we need to delete in our existing list
    QList<QtVersion *> toDelete;
    foreach (const ImportInfo &info, m_infos) {
        if (info.isTemporary)
            toDelete.append(info.version);
    }
    // Remove those that got copied into the new list to set up
    foreach (const ImportInfo &info, infos) {
        if (info.isTemporary)
            toDelete.removeAll(info.version);
    }
    // Delete the rest
    qDeleteAll(toDelete);
    // ... and clear the list
    m_infos.clear();
99
100
101
102

    // Find possible targets:
    QStringList targets;
    foreach (const ImportInfo &i, infos) {
103
        // Make sure we have no duplicate directories:
104
105
        bool skip = false;
        foreach (const ImportInfo &j, m_infos) {
106
            if (j.isExistingBuild && i.isExistingBuild && (j.directory == i.directory)) {
107
108
109
110
                skip = true;
                break;
            }
        }
111
112
113
        if (skip) {
            if (i.isTemporary)
                delete i.version;
114
            continue;
115
        }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

        m_infos.append(i);

        QSet<QString> versionTargets = i.version->supportedTargetIds();
        foreach (const QString &t, versionTargets) {
            if (!targets.contains(t))
                targets.append(t);
        }
    }
    qSort(targets.begin(), targets.end());

    Qt4TargetFactory factory;
    foreach (const QString &t, targets) {
        QTreeWidgetItem *targetItem = new QTreeWidgetItem(m_treeWidget);
        targetItem->setText(0, factory.displayNameForId(t));
        targetItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        targetItem->setData(0, Qt::UserRole, t);
        targetItem->setExpanded(true);

        int pos = -1;
136
        foreach (const ImportInfo &i, m_infos) {
137
138
139
140
141
142
143
144
145
146
147
            ++pos;

            if (!i.version->supportsTargetId(t))
                continue;
            QTreeWidgetItem *versionItem = new QTreeWidgetItem(targetItem);
            // Column 0:
            versionItem->setText(0, i.version->displayName());
            versionItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
            versionItem->setData(0, Qt::UserRole, pos);
            // Prefer imports to creating new builds, but precheck any
            // Qt that exists (if there is no import with that version)
Tobias Hunger's avatar
Tobias Hunger committed
148
            bool shouldCheck = true;
149
150
151
            if (!i.isExistingBuild) {
                foreach (const ImportInfo &j, m_infos) {
                    if (j.isExistingBuild && j.version == i.version) {
Tobias Hunger's avatar
Tobias Hunger committed
152
                        shouldCheck = false;
153
154
155
156
                        break;
                    }
                }
            }
Tobias Hunger's avatar
Tobias Hunger committed
157
158
            shouldCheck = shouldCheck && (m_preferMobile == i.version->supportsMobileTarget());
            shouldCheck = shouldCheck || i.isExistingBuild; // always check imports
159
            shouldCheck = shouldCheck || m_infos.count() == 1; // always check only option
Tobias Hunger's avatar
Tobias Hunger committed
160
            versionItem->setCheckState(0, shouldCheck ? Qt::Checked : Qt::Unchecked);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

            // Column 1 (status):
            versionItem->setText(1, i.isExistingBuild ? tr("Import", "Is this an import of an existing build or a new one?") :
                                                        tr("New", "Is this an import of an existing build or a new one?"));

            // Column 2 (directory):
            versionItem->setText(2, QDir::toNativeSeparators(i.directory));
        }
    }

    connect(m_treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
            this, SLOT(itemWasChanged()));

    emit completeChanged();
}

QList<TargetSetupPage::ImportInfo> TargetSetupPage::importInfos() const
{
    return m_infos;
}

bool TargetSetupPage::hasSelection() const
{
    for (int i = 0; i < m_treeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem * current = m_treeWidget->topLevelItem(i);
        for (int j = 0; j < current->childCount(); ++j) {
            QTreeWidgetItem * child = current->child(j);
Tobias Hunger's avatar
Tobias Hunger committed
188
            if (child->checkState(0) == Qt::Checked)
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                return true;
        }
    }
    return false;
}

bool TargetSetupPage::isTargetSelected(const QString &targetid) const
{
    for (int i = 0; i < m_treeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem * current = m_treeWidget->topLevelItem(i);
        if (current->data(0, Qt::UserRole).toString() != targetid)
            continue;
        for (int j = 0; j < current->childCount(); ++j) {
            QTreeWidgetItem * child = current->child(j);
            if (child->checkState(0) == Qt::Checked)
                return true;
        }
    }
    return false;
}

bool TargetSetupPage::setupProject(Qt4ProjectManager::Qt4Project *project)
{
    Q_ASSERT(project->targets().isEmpty());
213
    QtVersionManager *vm = QtVersionManager::instance();
214
215
216
217
218
219
220
221
222
223
224

    for (int i = 0; i < m_treeWidget->topLevelItemCount(); ++i) {
        QTreeWidgetItem *current = m_treeWidget->topLevelItem(i);
        QString targetId = current->data(0, Qt::UserRole).toString();

        QList<BuildConfigurationInfo> targetInfos;
        for (int j = 0; j < current->childCount(); ++j) {
            QTreeWidgetItem *child = current->child(j);
            if (child->checkState(0) != Qt::Checked)
                continue;

225
226
227
228
229
230
231
            ImportInfo &info = m_infos[child->data(0, Qt::UserRole).toInt()];

            // Register temporary Qt version
            if (info.isTemporary) {
                vm->addVersion(info.version);
                info.isTemporary = false;
            }
232

233
234
235
            if ((info.buildConfig | QtVersion::DebugBuild) != info.buildConfig)
                targetInfos.append(BuildConfigurationInfo(info.version, QtVersion::QmakeBuildConfigs(info.buildConfig | QtVersion::DebugBuild),
                                                          info.additionalArguments, info.directory));
236
237
238
239
240
241
            targetInfos.append(BuildConfigurationInfo(info.version, info.buildConfig,
                                                      info.additionalArguments, info.directory));
        }

        // create the target:
        Qt4Target *target = 0;
242
        if (!targetInfos.isEmpty())
243
244
245
246
247
248
            target = project->targetFactory()->create(project, targetId, targetInfos);

        if (target)
            project->addTarget(target);
    }

249
250
251
252
253
254
255
    // Create the default target if nothing else was set up:
    if (project->targets().isEmpty()) {
        Qt4Target *target = project->targetFactory()->create(project, Constants::DESKTOP_TARGET_ID);
        if (target)
            project->addTarget(target);
    }

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    return !project->targets().isEmpty();
}

void TargetSetupPage::itemWasChanged()
{
    emit completeChanged();
}

bool TargetSetupPage::isComplete() const
{
    return hasSelection();
}

void TargetSetupPage::setImportDirectoryBrowsingEnabled(bool browsing)
{
271
272
    m_addDirectoryButton->setEnabled(browsing);
    m_addDirectoryButton->setVisible(browsing);
273
274
275
276
}

void TargetSetupPage::setImportDirectoryBrowsingLocation(const QString &directory)
{
277
    m_defaultShadowBuildLocation = directory;
278
279
280
281
282
283
284
}

void TargetSetupPage::setShowLocationInformation(bool location)
{
    m_treeWidget->setColumnCount(location ? 3 : 1);
}

Tobias Hunger's avatar
Tobias Hunger committed
285
286
287
288
289
void TargetSetupPage::setPreferMobile(bool mobile)
{
    m_preferMobile = mobile;
}

290
291
292
void TargetSetupPage::setProFilePath(const QString &path)
{
    m_proFilePath = path;
293
294
295
296
    if (!m_proFilePath.isEmpty()) {
        m_importLabel->setText(tr("Qt Creator can set up the following targets for project <b>%1</b>:",
                                  "%1: Project name").arg(QFileInfo(m_proFilePath).baseName()));
    }
297
298
}

299
300
301
302
303
304
QList<TargetSetupPage::ImportInfo>
TargetSetupPage::importInfosForKnownQtVersions(Qt4ProjectManager::Qt4Project *project)
{
    QList<ImportInfo> results;
    QtVersionManager * vm = QtVersionManager::instance();
    QList<QtVersion *> validVersions = vm->validVersions();
305
306
307
    // Fallback in case no valid versions are found:
    if (validVersions.isEmpty())
        validVersions.append(vm->versions().at(0)); // there is always one!
308
309
    foreach (QtVersion *v, validVersions) {
        ImportInfo info;
310
311
312
313
314
315
        if (project) {
            if (v->supportsShadowBuilds())
                info.directory = project->defaultTopLevelBuildDirectory();
            else
                info.directory = project->projectDirectory();
        }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
        info.isExistingBuild = false;
        info.isTemporary = false;
        info.version = v;
        results.append(info);
    }
    return results;
}

QList<TargetSetupPage::ImportInfo> TargetSetupPage::filterImportInfos(const QSet<QString> &validTargets,
                                                                      const QList<ImportInfo> &infos)
{
    QList<ImportInfo> results;
    foreach (const ImportInfo &info, infos) {
        Q_ASSERT(info.version);
        foreach (const QString &target, validTargets) {
            if (info.version->supportsTargetId(target))
                results.append(info);
        }
    }
    return results;
}

QList<TargetSetupPage::ImportInfo>
339
TargetSetupPage::recursivelyCheckDirectoryForBuild(const QString &directory, const QString &proFile, int maxdepth)
340
341
342
343
344
345
346
{
    QList<ImportInfo> results;

    if (maxdepth <= 0)
        return results;

    // Check for in-source builds first:
347
    QString qmakeBinary = QtVersionManager::findQMakeBinaryFromMakefile(directory);
348
349

    // Recurse into subdirectories:
350
    if (qmakeBinary.isNull() || !QtVersionManager::makefileIsFor(directory, proFile)) {
351
352
        QStringList subDirs = QDir(directory).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
        foreach (QString subDir, subDirs)
353
354
            results.append(recursivelyCheckDirectoryForBuild(QDir::cleanPath(directory + QChar('/') + subDir),
                                                             proFile, maxdepth - 1));
355
356
357
358
359
360
361
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
389
390
391
392
393
        return results;
    }

    // Shiny fresh directory with a Makefile...
    QtVersionManager * vm = QtVersionManager::instance();
    TargetSetupPage::ImportInfo info;
    info.directory = directory;

    // This also means we have a build in there
    // First get the qt version
    info.version = vm->qtVersionForQMakeBinary(qmakeBinary);
    info.isExistingBuild = true;

    // Okay does not yet exist, create
    if (!info.version) {
        info.version = new QtVersion(qmakeBinary);
        info.isTemporary = true;
    }

    QPair<QtVersion::QmakeBuildConfigs, QStringList> result =
            QtVersionManager::scanMakeFile(directory, info.version->defaultBuildConfig());
    info.buildConfig = result.first;
    info.additionalArguments = Qt4BuildConfiguration::removeSpecFromArgumentList(result.second);

    QString parsedSpec = Qt4BuildConfiguration::extractSpecFromArgumentList(result.second, directory, info.version);
    QString versionSpec = info.version->mkspec();

    // Compare mkspecs and add to additional arguments
    if (parsedSpec.isEmpty() || parsedSpec == versionSpec || parsedSpec == "default") {
        // using the default spec, don't modify additional arguments
    } else {
        info.additionalArguments.prepend(parsedSpec);
        info.additionalArguments.prepend("-spec");
    }

    results.append(info);
    return results;
}

394
void TargetSetupPage::addShadowBuildLocation()
395
{
396
397
398
399
400
401
402
403
404
    QString newPath =
        QFileDialog::getExistingDirectory(this,
                                          tr("Choose a directory to scan for additional shadow builds"),
                                          m_defaultShadowBuildLocation);

    if (newPath.isEmpty())
        return;

    QFileInfo dir(QDir::fromNativeSeparators(newPath));
405
406
    if (!dir.exists() || !dir.isDir())
        return;
407

408
    QList<ImportInfo> tmp;
409
    tmp.append(recursivelyCheckDirectoryForBuild(dir.absoluteFilePath(), m_proFilePath));
410
    if (tmp.isEmpty()) {
Friedemann Kleint's avatar
Friedemann Kleint committed
411
412
        QMessageBox::warning(this, tr("No builds found"),
                             tr("No builds for project file \"%1\" were found in the folder \"%2\".",
413
414
415
416
417
                                "%1: pro-file, %2: directory that was checked.").
                             arg(m_proFilePath, dir.absoluteFilePath()));
        return;
    }
    tmp.append(m_infos);
418
419
420
421
422
423
424
425
426
427
428
429
    setImportInfos(tmp);
}

void TargetSetupPage::resetInfos()
{
    m_treeWidget->clear();
    foreach (const ImportInfo &info, m_infos) {
        if (info.isTemporary)
            delete info.version;
    }
    m_infos.clear();
}