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

#include "targetsettingspanel.h"

32
#include "buildinfo.h"
Tobias Hunger's avatar
Tobias Hunger committed
33
#include "buildsettingspropertiespage.h"
34
#include "ipotentialkit.h"
35
#include "kitoptionspage.h"
Tobias Hunger's avatar
Tobias Hunger committed
36
#include "project.h"
37
#include "projectimporter.h"
Tobias Hunger's avatar
Tobias Hunger committed
38
39
40
41
42
#include "projectwindow.h"
#include "runsettingspropertiespage.h"
#include "target.h"
#include "targetsettingswidget.h"

43
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
44
#include <extensionsystem/pluginmanager.h>
Daniel Teske's avatar
Daniel Teske committed
45
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
46
47
#include <projectexplorer/kit.h>
#include <projectexplorer/kitmanager.h>
Daniel Teske's avatar
Daniel Teske committed
48
#include <projectexplorer/buildmanager.h>
49
50
51
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/runconfiguration.h>
52
#include <utils/algorithm.h>
Tobias Hunger's avatar
Tobias Hunger committed
53
#include <utils/qtcassert.h>
Tobias Hunger's avatar
Tobias Hunger committed
54

55
#include <QFileDialog>
56
57
58
59
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
60
61
62
#include <QStackedWidget>
#include <QToolTip>
#include <QVBoxLayout>
Tobias Hunger's avatar
Tobias Hunger committed
63
64


hjk's avatar
hjk committed
65
66
67
68
using namespace Core;

namespace ProjectExplorer {
namespace Internal {
Tobias Hunger's avatar
Tobias Hunger committed
69

70
71
int TargetSettingsPanelWidget::s_targetSubIndex = -1;

Tobias Hunger's avatar
Tobias Hunger committed
72
73
74
75
76
///
// TargetSettingsWidget
///

TargetSettingsPanelWidget::TargetSettingsPanelWidget(Project *project) :
77
    m_currentTarget(0),
Tobias Hunger's avatar
Tobias Hunger committed
78
    m_project(project),
79
    m_importer(project->createProjectImporter()),
Tobias Hunger's avatar
Tobias Hunger committed
80
    m_selector(0),
81
    m_centralWidget(0),
82
83
    m_changeMenu(0),
    m_duplicateMenu(0),
84
85
    m_lastAction(0),
    m_importAction(0)
Tobias Hunger's avatar
Tobias Hunger committed
86
{
87
88
    Q_ASSERT(m_project);

Tobias Hunger's avatar
Tobias Hunger committed
89
90
91
    m_panelWidgets[0] = 0;
    m_panelWidgets[1] = 0;

92
    m_addMenu = new QMenu(this);
93
    m_targetMenu = new QMenu(this);
94

95
96
97
98
99
    if (m_importer) {
        m_importAction = new QAction(tr("Import existing build..."), this);
        connect(m_importAction, SIGNAL(triggered()), this, SLOT(importTarget()));
    }

100
101
    setFocusPolicy(Qt::NoFocus);

Tobias Hunger's avatar
Tobias Hunger committed
102
103
104
105
    setupUi();

    connect(m_project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
            this, SLOT(targetAdded(ProjectExplorer::Target*)));
106
107
108
    connect(m_project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
            this, SLOT(removedTarget(ProjectExplorer::Target*)));

Tobias Hunger's avatar
Tobias Hunger committed
109
110
    connect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
            this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
111

Tobias Hunger's avatar
Tobias Hunger committed
112
    connect(KitManager::instance(), SIGNAL(kitsChanged()),
113
            this, SLOT(updateTargetButtons()));
Tobias Hunger's avatar
Tobias Hunger committed
114
115
116
117
}

TargetSettingsPanelWidget::~TargetSettingsPanelWidget()
{
118
    delete m_importer;
Tobias Hunger's avatar
Tobias Hunger committed
119
120
}

121
122
123
bool TargetSettingsPanelWidget::event(QEvent *event)
{
    if (event->type() == QEvent::StatusTip) {
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        QAction *act = 0;
        QMenu *menu = 0;
        if (m_addMenu->activeAction()) {
            menu = m_addMenu;
            act = m_addMenu->activeAction();
        } else if (m_changeMenu && m_changeMenu->activeAction()) {
            menu = m_changeMenu;
            act = m_changeMenu->activeAction();
        } else if (m_duplicateMenu && m_duplicateMenu->activeAction()) {
            menu = m_duplicateMenu;
            act = m_duplicateMenu->activeAction();
        } else {
            return QWidget::event(event);
        }

139
140
141
142
143
144
145
        QStatusTipEvent *ev = static_cast<QStatusTipEvent *>(event);
        ev->accept();

        if (act != m_lastAction)
            QToolTip::showText(QPoint(), QString());
        m_lastAction = act;
        if (act) {
146
147
            QRect actionRect = menu->actionGeometry(act);
            actionRect.translate(menu->pos());
148
149
150
151
            QPoint p = QCursor::pos();
            if (!actionRect.contains(p))
                p = actionRect.center();
            p.setY(actionRect.center().y());
152
            QToolTip::showText(p, ev->tip(), menu, menu->actionGeometry(act));
153
154
155
156
157
158
159
160
161
        } else {
            QToolTip::showText(QPoint(), QString());
        }

        return true;
    }
    return QWidget::event(event);
}

Tobias Hunger's avatar
Tobias Hunger committed
162
163
164
165
166
167
168
169
170
171
172
173
174
void TargetSettingsPanelWidget::setupUi()
{
    QVBoxLayout *viewLayout = new QVBoxLayout(this);
    viewLayout->setMargin(0);
    viewLayout->setSpacing(0);

    m_selector = new TargetSettingsWidget(this);
    viewLayout->addWidget(m_selector);

    // Setup our container for the contents:
    m_centralWidget = new QStackedWidget(this);
    m_selector->setCentralWidget(m_centralWidget);

175
    // no target label:
Tobias Hunger's avatar
Tobias Hunger committed
176
    m_noTargetLabel = new QWidget;
177
    QVBoxLayout *noTargetLayout = new QVBoxLayout(m_noTargetLabel);
Tobias Hunger's avatar
Tobias Hunger committed
178
179
    noTargetLayout->setMargin(0);
    QLabel *label = new QLabel(m_noTargetLabel);
180
    label->setText(tr("No kit defined in this project."));
Tobias Hunger's avatar
Tobias Hunger committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    {
        QFont f = label->font();
        f.setPointSizeF(f.pointSizeF() * 1.4);
        f.setBold(true);
        label->setFont(f);
    }
    label->setMargin(10);
    label->setAlignment(Qt::AlignTop);
    noTargetLayout->addWidget(label);
    noTargetLayout->addStretch(10);
    m_centralWidget->addWidget(m_noTargetLabel);

    foreach (Target *t, m_project->targets())
        targetAdded(t);

dt's avatar
dt committed
196
197
198
    // Now set the correct target
    int index = m_targets.indexOf(m_project->activeTarget());
    m_selector->setCurrentIndex(index);
199
    m_selector->setCurrentSubIndex(s_targetSubIndex);
200
    currentTargetChanged(index, m_selector->currentSubIndex());
dt's avatar
dt committed
201
202
203

    connect(m_selector, SIGNAL(currentChanged(int,int)),
            this, SLOT(currentTargetChanged(int,int)));
204
205
    connect(m_selector, SIGNAL(manageButtonClicked()),
            this, SLOT(openTargetPreferences()));
206
207
    connect(m_selector, SIGNAL(toolTipRequested(QPoint,int)),
            this, SLOT(showTargetToolTip(QPoint,int)));
208
209
    connect(m_selector, SIGNAL(menuShown(int)),
            this, SLOT(menuShown(int)));
Tobias Hunger's avatar
Tobias Hunger committed
210

211
    connect(m_addMenu, SIGNAL(triggered(QAction*)),
212
            this, SLOT(addActionTriggered(QAction*)));
213

214
215
216
217
    m_selector->setAddButtonMenu(m_addMenu);
    m_selector->setTargetMenu(m_targetMenu);

    updateTargetButtons();
Tobias Hunger's avatar
Tobias Hunger committed
218
219
}

220
void TargetSettingsPanelWidget::currentTargetChanged(int targetIndex, int subIndex)
Tobias Hunger's avatar
Tobias Hunger committed
221
{
222
    if (targetIndex < -1 || targetIndex >= m_targets.count())
Tobias Hunger's avatar
Tobias Hunger committed
223
224
225
226
        return;
    if (subIndex < -1 || subIndex >= 2)
        return;

Tobias Hunger's avatar
Tobias Hunger committed
227
    if (targetIndex == -1 || subIndex == -1) { // no more kits!
228
229
230
231
232
233
234
235
236
        delete m_panelWidgets[0];
        m_panelWidgets[0] = 0;
        delete m_panelWidgets[1];
        m_panelWidgets[1] = 0;

        m_centralWidget->setCurrentWidget(m_noTargetLabel);
        return;
    }

237
238
    s_targetSubIndex = subIndex;

239
    Target *target = m_targets.at(targetIndex);
Tobias Hunger's avatar
Tobias Hunger committed
240
241

    // Target was not actually changed:
242
    if (m_currentTarget == target) {
243
244
245
246
        if (m_panelWidgets[subIndex])
            m_centralWidget->setCurrentWidget(m_panelWidgets[subIndex]);
        else
            m_centralWidget->setCurrentWidget(m_noTargetLabel);
Tobias Hunger's avatar
Tobias Hunger committed
247
248
249
        return;
    }

250
    // Target has changed:
251
    m_currentTarget = target;
Tobias Hunger's avatar
Tobias Hunger committed
252

253
254
    PanelsWidget *buildPanel = new PanelsWidget(m_centralWidget);
    PanelsWidget *runPanel = new PanelsWidget(m_centralWidget);
Tobias Hunger's avatar
Tobias Hunger committed
255

256
    foreach (ITargetPanelFactory *panelFactory, ExtensionSystem::PluginManager::getObjects<ITargetPanelFactory>()) {
Tobias Hunger's avatar
Tobias Hunger committed
257
        if (panelFactory->id() == QLatin1String(BUILDSETTINGS_PANEL_ID)) {
258
            PropertiesPanel *panel = panelFactory->createPanel(target);
Tobias Hunger's avatar
Tobias Hunger committed
259
260
261
262
            buildPanel->addPropertiesPanel(panel);
            continue;
        }
        if (panelFactory->id() == QLatin1String(RUNSETTINGS_PANEL_ID)) {
263
            PropertiesPanel *panel = panelFactory->createPanel(target);
Tobias Hunger's avatar
Tobias Hunger committed
264
265
266
267
268
269
270
            runPanel->addPropertiesPanel(panel);
            continue;
        }
    }
    m_centralWidget->addWidget(buildPanel);
    m_centralWidget->addWidget(runPanel);

271
272
273
    m_centralWidget->setCurrentWidget(subIndex == 0 ? buildPanel : runPanel);

    delete m_panelWidgets[0];
Tobias Hunger's avatar
Tobias Hunger committed
274
    m_panelWidgets[0] = buildPanel;
275
    delete m_panelWidgets[1];
Tobias Hunger's avatar
Tobias Hunger committed
276
    m_panelWidgets[1] = runPanel;
277
278
279


    m_project->setActiveTarget(target);
Tobias Hunger's avatar
Tobias Hunger committed
280
281
}

282
283
284
285
286
287
288
void TargetSettingsPanelWidget::menuShown(int targetIndex)
{
    m_menuTargetIndex = targetIndex;
}

void TargetSettingsPanelWidget::changeActionTriggered(QAction *action)
{
289
    Kit *k = KitManager::find(action->data().value<Core::Id>());
290
291
292
293
294
295
296
297
298
299
300
301
    Target *sourceTarget = m_targets.at(m_menuTargetIndex);
    Target *newTarget = cloneTarget(sourceTarget, k);

    if (newTarget) {
        m_project->addTarget(newTarget);
        m_project->setActiveTarget(newTarget);
        m_project->removeTarget(sourceTarget);
    }
}

void TargetSettingsPanelWidget::duplicateActionTriggered(QAction *action)
{
302
    Kit *k = KitManager::find(action->data().value<Core::Id>());
303
304
305
306
307
308
309
310
311
    Target *newTarget = cloneTarget(m_targets.at(m_menuTargetIndex), k);

    if (newTarget) {
        m_project->addTarget(newTarget);
        m_project->setActiveTarget(newTarget);
    }
}

void TargetSettingsPanelWidget::addActionTriggered(QAction *action)
Tobias Hunger's avatar
Tobias Hunger committed
312
{
313
314
315
316
    const QVariant data = action->data();
    if (data.canConvert<Core::Id>()) { // id of kit
        Kit *k = KitManager::find(action->data().value<Core::Id>());
        QTC_ASSERT(!m_project->target(k), return);
317

318
319
320
321
322
323
324
325
326
        Target *target = m_project->createTarget(k);
        if (!target)
            return;
        m_project->addTarget(target);
    } else {
        QTC_ASSERT(data.canConvert<IPotentialKit *>(), return);
        IPotentialKit *potentialKit = data.value<IPotentialKit *>();
        potentialKit->executeFromMenu();
    }
Tobias Hunger's avatar
Tobias Hunger committed
327
328
}

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
Target *TargetSettingsPanelWidget::cloneTarget(Target *sourceTarget, Kit *k)
{
    Target *newTarget = new Target(m_project, k);

    QStringList buildconfigurationError;
    QStringList deployconfigurationError;
    QStringList runconfigurationError;

    foreach (BuildConfiguration *sourceBc, sourceTarget->buildConfigurations()) {
        IBuildConfigurationFactory *factory = IBuildConfigurationFactory::find(newTarget, sourceBc);
        if (!factory) {
            buildconfigurationError << sourceBc->displayName();
            continue;
        }
        BuildConfiguration *newBc = factory->clone(newTarget, sourceBc);
        if (!newBc) {
            buildconfigurationError << sourceBc->displayName();
            continue;
        }
        newBc->setDisplayName(sourceBc->displayName());
        newTarget->addBuildConfiguration(newBc);
        if (sourceTarget->activeBuildConfiguration() == sourceBc)
            newTarget->setActiveBuildConfiguration(newBc);
    }
    if (!newTarget->activeBuildConfiguration()) {
        QList<BuildConfiguration *> bcs = newTarget->buildConfigurations();
        if (!bcs.isEmpty())
            newTarget->setActiveBuildConfiguration(bcs.first());
    }

    foreach (DeployConfiguration *sourceDc, sourceTarget->deployConfigurations()) {
        DeployConfigurationFactory *factory = DeployConfigurationFactory::find(newTarget, sourceDc);
        if (!factory) {
            deployconfigurationError << sourceDc->displayName();
            continue;
        }
        DeployConfiguration *newDc = factory->clone(newTarget, sourceDc);
        if (!newDc) {
            deployconfigurationError << sourceDc->displayName();
            continue;
        }
        newDc->setDisplayName(sourceDc->displayName());
        newTarget->addDeployConfiguration(newDc);
        if (sourceTarget->activeDeployConfiguration() == sourceDc)
            newTarget->setActiveDeployConfiguration(newDc);
    }
    if (!newTarget->activeBuildConfiguration()) {
        QList<DeployConfiguration *> dcs = newTarget->deployConfigurations();
        if (!dcs.isEmpty())
            newTarget->setActiveDeployConfiguration(dcs.first());
    }

    foreach (RunConfiguration *sourceRc, sourceTarget->runConfigurations()) {
        IRunConfigurationFactory *factory = IRunConfigurationFactory::find(newTarget, sourceRc);
        if (!factory) {
            runconfigurationError << sourceRc->displayName();
            continue;
        }
        RunConfiguration *newRc = factory->clone(newTarget, sourceRc);
        if (!newRc) {
            runconfigurationError << sourceRc->displayName();
            continue;
        }
        newRc->setDisplayName(sourceRc->displayName());
        newTarget->addRunConfiguration(newRc);
        if (sourceTarget->activeRunConfiguration() == sourceRc)
            newTarget->setActiveRunConfiguration(newRc);
    }
    if (!newTarget->activeRunConfiguration()) {
        QList<RunConfiguration *> rcs = newTarget->runConfigurations();
        if (!rcs.isEmpty())
            newTarget->setActiveRunConfiguration(rcs.first());
    }

    bool fatalError = false;
    if (buildconfigurationError.count() == sourceTarget->buildConfigurations().count())
        fatalError = true;

    if (deployconfigurationError.count() == sourceTarget->deployConfigurations().count())
        fatalError = true;

    if (runconfigurationError.count() == sourceTarget->runConfigurations().count())
        fatalError = true;

    if (fatalError) {
        // That could be a more granular error message
        QMessageBox::critical(Core::ICore::mainWindow(),
                              tr("Incompatible Kit"),
417
                              tr("Kit %1 is incompatible with kit %2.")
418
419
420
421
422
423
424
425
426
427
428
                              .arg(sourceTarget->kit()->displayName())
                              .arg(k->displayName()));

        delete newTarget;
        newTarget = 0;
    } else if (!buildconfigurationError.isEmpty()
               || !deployconfigurationError.isEmpty()
               || ! runconfigurationError.isEmpty()) {

        QString error;
        if (!buildconfigurationError.isEmpty())
429
430
            error += tr("Build configurations:")
                    + QLatin1Char('\n')
431
432
433
434
435
                    + buildconfigurationError.join(QLatin1String("\n"));

        if (!deployconfigurationError.isEmpty()) {
            if (!error.isEmpty())
                error.append(QLatin1Char('\n'));
436
437
            error += tr("Deploy configurations:")
                    + QLatin1Char('\n')
438
439
440
441
442
443
                    + deployconfigurationError.join(QLatin1String("\n"));
        }

        if (!runconfigurationError.isEmpty()) {
            if (!error.isEmpty())
                error.append(QLatin1Char('\n'));
444
            error += tr("Run configurations") + QLatin1Char(' ')
445
446
447
448
449
                    + runconfigurationError.join(QLatin1String("\n"));
        }

        QMessageBox msgBox(Core::ICore::mainWindow());
        msgBox.setIcon(QMessageBox::Warning);
450
        msgBox.setWindowTitle(tr("Partially Incompatible Kit"));
451
452
453
454
455
456
457
458
459
460
461
462
463
        msgBox.setText(tr("Some configurations could not be copied."));
        msgBox.setDetailedText(error);
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        if (msgBox.exec() != QDialog::Accepted) {
            delete newTarget;
            newTarget = 0;
        }
    }

    return newTarget;
}

void TargetSettingsPanelWidget::removeTarget()
464
{
465
466
467
    Target *t = m_targets.at(m_menuTargetIndex);
    removeTarget(t);
}
Daniel Teske's avatar
Daniel Teske committed
468

469
470
void TargetSettingsPanelWidget::removeTarget(Target *t)
{
hjk's avatar
hjk committed
471
    if (BuildManager::isBuilding(t)) {
Daniel Teske's avatar
Daniel Teske committed
472
        QMessageBox box;
Tobias Hunger's avatar
Tobias Hunger committed
473
        QPushButton *closeAnyway = box.addButton(tr("Cancel Build && Remove Kit"), QMessageBox::AcceptRole);
Daniel Teske's avatar
Daniel Teske committed
474
475
        QPushButton *cancelClose = box.addButton(tr("Do Not Remove"), QMessageBox::RejectRole);
        box.setDefaultButton(cancelClose);
Tobias Hunger's avatar
Tobias Hunger committed
476
477
        box.setWindowTitle(tr("Remove Kit %1?").arg(t->displayName()));
        box.setText(tr("The kit <b>%1</b> is currently being built.").arg(t->displayName()));
478
        box.setInformativeText(tr("Do you want to cancel the build process and remove the kit anyway?"));
Daniel Teske's avatar
Daniel Teske committed
479
480
481
        box.exec();
        if (box.clickedButton() != closeAnyway)
            return;
hjk's avatar
hjk committed
482
        BuildManager::cancel();
Daniel Teske's avatar
Daniel Teske committed
483
484
485
486
    } else {
        // We don't show the generic message box on removing the target, if we showed the still building one
        int ret = QMessageBox::warning(this, tr("Qt Creator"),
                                       tr("Do you really want to remove the\n"
Tobias Hunger's avatar
Tobias Hunger committed
487
                                          "\"%1\" kit?").arg(t->displayName()),
Daniel Teske's avatar
Daniel Teske committed
488
489
490
491
492
493
494
495
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::No);
        if (ret != QMessageBox::Yes)
            return;
    }

    m_project->removeTarget(t);

496
497
}

498
499
500
501
502
503
504
void TargetSettingsPanelWidget::showTargetToolTip(const QPoint &globalPos, int targetIndex)
{
    QTC_ASSERT(targetIndex >= 0 && targetIndex < m_targets.count(), return);
    Target *target = m_targets.at(targetIndex);
    QToolTip::showText(globalPos, target->kit()->toHtml());
}

Tobias Hunger's avatar
Tobias Hunger committed
505
506
507
508
509
void TargetSettingsPanelWidget::targetAdded(ProjectExplorer::Target *target)
{
    Q_ASSERT(m_project == target->project());
    Q_ASSERT(m_selector);

510
511
512
513
    for (int pos = 0; pos <= m_targets.count(); ++pos) {
        if (m_targets.count() == pos ||
            m_targets.at(pos)->displayName() > target->displayName()) {
            m_targets.insert(pos, target);
514
515
516
            m_selector->insertTarget(pos, m_project->hasActiveBuildSettings() ? 0 : 1,
                                     target->displayName());

517
518
519
520
            break;
        }
    }

Tobias Hunger's avatar
Tobias Hunger committed
521
    connect(target, SIGNAL(displayNameChanged()), this, SLOT(renameTarget()));
522
    updateTargetButtons();
Tobias Hunger's avatar
Tobias Hunger committed
523
524
}

525
void TargetSettingsPanelWidget::removedTarget(ProjectExplorer::Target *target)
Tobias Hunger's avatar
Tobias Hunger committed
526
527
528
529
{
    Q_ASSERT(m_project == target->project());
    Q_ASSERT(m_selector);

530
    int index(m_targets.indexOf(target));
Tobias Hunger's avatar
Tobias Hunger committed
531
532
    if (index < 0)
        return;
533
534
    m_targets.removeAt(index);

Tobias Hunger's avatar
Tobias Hunger committed
535
    m_selector->removeTarget(index);
536

537
    updateTargetButtons();
Tobias Hunger's avatar
Tobias Hunger committed
538
539
540
541
542
543
}

void TargetSettingsPanelWidget::activeTargetChanged(ProjectExplorer::Target *target)
{
    Q_ASSERT(m_selector);

544
545
    int index = m_targets.indexOf(target);
    m_selector->setCurrentIndex(index);
Tobias Hunger's avatar
Tobias Hunger committed
546
}
547

548
549
550
551
void TargetSettingsPanelWidget::createAction(Kit *k, QMenu *menu)
{
    QAction *action = new QAction(k->displayName(), menu);
    action->setData(QVariant::fromValue(k->id()));
552
    QString statusTip = QLatin1String("<html><body>");
553
554
555
    QString errorMessage;
    if (!m_project->supportsKit(k, &errorMessage)) {
        action->setEnabled(false);
556
        statusTip += errorMessage;
557
    }
558
559
560
    statusTip += k->toHtml();
    action->setStatusTip(statusTip);

561
562
563
564
    menu->addAction(action);
}

void TargetSettingsPanelWidget::updateTargetButtons()
565
566
567
568
{
    if (!m_selector)
        return;

569
    m_addMenu->clear();
570
    m_targetMenu->clear();
571

572
    if (m_importAction)
573
        m_addMenu->addAction(m_importAction);
574
575
576
577
578
579
580
581
    const QList<IPotentialKit *> potentialKits
            = ExtensionSystem::PluginManager::getObjects<IPotentialKit>();
    foreach (IPotentialKit *potentialKit, potentialKits) {
        if (!potentialKit->isEnabled())
            continue;
        QAction *action = new QAction(potentialKit->displayName(), m_addMenu);
        action->setData(QVariant::fromValue(potentialKit));
        m_addMenu->addAction(action);
582
    }
583
584
    if (!m_addMenu->actions().isEmpty())
        m_addMenu->addSeparator();
585

586
587
588
    m_changeMenu = m_targetMenu->addMenu(tr("Change Kit"));
    m_duplicateMenu = m_targetMenu->addMenu(tr("Copy to Kit"));
    QAction *removeAction = m_targetMenu->addAction(tr("Remove Kit"));
589

590
591
    if (m_project->targets().size() < 2)
        removeAction->setEnabled(false);
Tobias Hunger's avatar
Tobias Hunger committed
592

593
594
595
596
597
598
    connect(m_changeMenu, SIGNAL(triggered(QAction*)),
            this, SLOT(changeActionTriggered(QAction*)));
    connect(m_duplicateMenu, SIGNAL(triggered(QAction*)),
            this, SLOT(duplicateActionTriggered(QAction*)));
    connect(removeAction, SIGNAL(triggered()), this, SLOT(removeTarget()));

599
    QList<Kit *> kits = KitManager::kits();
600
601
602
    Utils::sort(kits, [](const Kit *a, const Kit *b) {
        return a->displayName() < b->displayName();
    });
603
604
605
606
607
608
    foreach (Kit *k, kits) {
        if (m_project->target(k))
            continue;
        createAction(k, m_addMenu);
        createAction(k, m_changeMenu);
        createAction(k, m_duplicateMenu);
609
    }
610
611
612
613
614
    if (m_changeMenu->actions().isEmpty())
        m_changeMenu->setEnabled(false);
    if (m_duplicateMenu->actions().isEmpty())
        m_duplicateMenu->setEnabled(false);

615
    m_selector->setAddButtonEnabled(!m_addMenu->actions().isEmpty());
616
}
617

Tobias Hunger's avatar
Tobias Hunger committed
618
619
620
621
622
623
624
625
626
627
628
void TargetSettingsPanelWidget::renameTarget()
{
    Target *t = qobject_cast<Target *>(sender());
    if (!t)
        return;
    const int pos = m_targets.indexOf(t);
    if (pos < 0)
        return;
    m_selector->renameTarget(pos, t->displayName());
}

629
630
void TargetSettingsPanelWidget::openTargetPreferences()
{
631
632
    int targetIndex = m_selector->currentIndex();
    if (targetIndex >= 0 && targetIndex < m_targets.size()) {
hjk's avatar
hjk committed
633
        KitOptionsPage *page = ExtensionSystem::PluginManager::getObject<ProjectExplorer::KitOptionsPage>();
634
635
636
        if (page)
            page->showKit(m_targets.at(targetIndex)->kit());
    }
hjk's avatar
hjk committed
637
    ICore::showOptionsDialog(Constants::PROJECTEXPLORER_SETTINGS_CATEGORY,
638
639
                             Constants::KITS_SETTINGS_PAGE_ID,
                             this);
640
641
}

642
643
void TargetSettingsPanelWidget::importTarget()
{
644
    QString toImport = QFileDialog::getExistingDirectory(this, tr("Import directory"), m_project->projectDirectory().toString());
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
    importTarget(Utils::FileName::fromString(toImport));
}

void TargetSettingsPanelWidget::importTarget(const Utils::FileName &path)
{
    if (!m_importer)
        return;

    Target *target = 0;
    BuildConfiguration *bc = 0;
    QList<BuildInfo *> toImport = m_importer->import(path, false);
    foreach (BuildInfo *info, toImport) {
        target = m_project->target(info->kitId);
        if (!target) {
            target = new Target(m_project, KitManager::find(info->kitId));
            m_project->addTarget(target);
        }
        bc = info->factory()->create(target, info);
        QTC_ASSERT(bc, continue);
        target->addBuildConfiguration(bc);
    }
    m_project->setActiveTarget(target);
    if (target && bc)
        target->setActiveBuildConfiguration(bc);

    qDeleteAll(toImport);
}

673
674
675
676
677
678
679
680
681
int TargetSettingsPanelWidget::currentSubIndex() const
{
    return m_selector->currentSubIndex();
}

void TargetSettingsPanelWidget::setCurrentSubIndex(int subIndex)
{
    m_selector->setCurrentSubIndex(subIndex);
}
hjk's avatar
hjk committed
682
683
684

} // namespace Internal
} // namespace ProjectExplorer