targetsettingspanel.cpp 8.9 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
35
36
37
38
39
40
41
42
43
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 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 "targetsettingspanel.h"

#include "addtargetdialog.h"
#include "buildsettingspropertiespage.h"
#include "project.h"
#include "projectwindow.h"
#include "runsettingspropertiespage.h"
#include "target.h"
#include "targetsettingswidget.h"

#include <extensionsystem/pluginmanager.h>

#include <QtCore/QCoreApplication>
#include <QtGui/QLabel>
Tobias Hunger's avatar
Tobias Hunger committed
44
#include <QtGui/QMessageBox>
Tobias Hunger's avatar
Tobias Hunger committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <QtGui/QVBoxLayout>

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

///
// TargetSettingsPanelFactory
///

QString TargetSettingsPanelFactory::id() const
{
    return QLatin1String(TARGETSETTINGS_PANEL_ID);
}

QString TargetSettingsPanelFactory::displayName() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
61
    return QCoreApplication::translate("TargetSettingsPanelFactory", "Targets");
Tobias Hunger's avatar
Tobias Hunger committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}

bool TargetSettingsPanelFactory::supports(Project *project)
{
    Q_UNUSED(project);
    return true;
}

bool TargetSettingsPanelFactory::supports(Target *target)
{
    Q_UNUSED(target);
    return false;
}

IPropertiesPanel *TargetSettingsPanelFactory::createPanel(Project *project)
{
    return new TargetSettingsPanel(project);
}

IPropertiesPanel *TargetSettingsPanelFactory::createPanel(Target *target)
{
    Q_UNUSED(target);
    return 0;
}

///
// TargetSettingsPanel
///

TargetSettingsPanel::TargetSettingsPanel(Project *project) :
    m_widget(new TargetSettingsPanelWidget(project))
{
}

TargetSettingsPanel::~TargetSettingsPanel()
{
    delete m_widget;
}

QString TargetSettingsPanel::displayName() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
103
    return QCoreApplication::translate("TargetSettingsPanel", "Targets");
Tobias Hunger's avatar
Tobias Hunger committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
}

QWidget *TargetSettingsPanel::widget() const
{
    return m_widget;
}

QIcon TargetSettingsPanel::icon() const
{
    return QIcon();
}

///
// TargetSettingsWidget
///

TargetSettingsPanelWidget::TargetSettingsPanelWidget(Project *project) :
    m_currentIndex(-1),
    m_project(project),
    m_selector(0),
    m_centralWidget(0)
{
    m_panelWidgets[0] = 0;
    m_panelWidgets[1] = 0;

    setupUi();

    connect(m_project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
            this, SLOT(targetAdded(ProjectExplorer::Target*)));
    connect(m_project, SIGNAL(aboutToRemoveTarget(ProjectExplorer::Target*)),
134
            this, SLOT(aboutToRemoveTarget(ProjectExplorer::Target*)));
Tobias Hunger's avatar
Tobias Hunger committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    connect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
            this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
}

TargetSettingsPanelWidget::~TargetSettingsPanelWidget()
{
}

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);

    // no projects label:
    m_noTargetLabel = new QWidget;
    QVBoxLayout *noTargetLayout = new QVBoxLayout;
    noTargetLayout->setMargin(0);
    QLabel *label = new QLabel(m_noTargetLabel);
    label->setText(tr("No target defined."));
    {
        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);
176
    m_selector->markActive(m_targets.indexOf(m_project->activeTarget()));
Tobias Hunger's avatar
Tobias Hunger committed
177
178
179
180
181

    connect(m_selector, SIGNAL(currentIndexChanged(int,int)),
            this, SLOT(currentTargetIndexChanged(int,int)));
    connect(m_selector, SIGNAL(addButtonClicked()),
            this, SLOT(addTarget()));
182
183
    connect(m_selector, SIGNAL(removeButtonClicked()),
            this, SLOT(removeTarget()));
Tobias Hunger's avatar
Tobias Hunger committed
184
185

    if (m_project->targets().count())
186
        currentTargetIndexChanged(m_targets.indexOf(m_project->activeTarget()), 0);
Tobias Hunger's avatar
Tobias Hunger committed
187
188
189
190
}

void TargetSettingsPanelWidget::currentTargetIndexChanged(int targetIndex, int subIndex)
{
191
    if (targetIndex < -1 || targetIndex >= m_targets.count())
Tobias Hunger's avatar
Tobias Hunger committed
192
193
194
195
196
197
        return;
    if (subIndex < -1 || subIndex >= 2)
        return;
    m_selector->setCurrentIndex(targetIndex);
    m_selector->setCurrentSubIndex(subIndex);

198
    Target *target(m_targets.at(targetIndex));
Tobias Hunger's avatar
Tobias Hunger committed
199
200
201

    // Target was not actually changed:
    if (m_currentIndex == targetIndex) {
202
203
204
205
        if (m_panelWidgets[subIndex])
            m_centralWidget->setCurrentWidget(m_panelWidgets[subIndex]);
        else
            m_centralWidget->setCurrentWidget(m_noTargetLabel);
Tobias Hunger's avatar
Tobias Hunger committed
206
207
208
        return;
    }

209
    m_currentIndex = targetIndex;
Tobias Hunger's avatar
Tobias Hunger committed
210

211
    // Target has changed:
Tobias Hunger's avatar
Tobias Hunger committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    if (targetIndex == -1) { // no more targets!
        m_centralWidget->setCurrentWidget(m_noTargetLabel);
        return;
    }

    PanelsWidget *buildPanel(new PanelsWidget(m_centralWidget));
    PanelsWidget *runPanel(new PanelsWidget(m_centralWidget));

    foreach (IPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<IPanelFactory>()) {
        if (panelFactory->id() == QLatin1String(BUILDSETTINGS_PANEL_ID)) {
            IPropertiesPanel *panel = panelFactory->createPanel(target);
            buildPanel->addPropertiesPanel(panel);
            continue;
        }
        if (panelFactory->id() == QLatin1String(RUNSETTINGS_PANEL_ID)) {
            IPropertiesPanel *panel = panelFactory->createPanel(target);
            runPanel->addPropertiesPanel(panel);
            continue;
        }
    }
    m_centralWidget->addWidget(buildPanel);
    m_centralWidget->addWidget(runPanel);

235
236
237
    m_centralWidget->setCurrentWidget(subIndex == 0 ? buildPanel : runPanel);

    delete m_panelWidgets[0];
Tobias Hunger's avatar
Tobias Hunger committed
238
    m_panelWidgets[0] = buildPanel;
239
    delete m_panelWidgets[1];
Tobias Hunger's avatar
Tobias Hunger committed
240
241
242
243
244
245
246
247
248
    m_panelWidgets[1] = runPanel;
}

void TargetSettingsPanelWidget::addTarget()
{
    AddTargetDialog dialog(m_project);
    dialog.exec();
}

249
250
251
void TargetSettingsPanelWidget::removeTarget()
{
    int index = m_selector->currentIndex();
252
    Target *t = m_targets.at(index);
Tobias Hunger's avatar
Tobias Hunger committed
253
254
255
256
257
258
259
    int ret = QMessageBox::warning(this, tr("Qt Creator"),
                                   tr("Do you really want to remove the\n"
                                      "\"%1\" target?").arg(t->displayName()),
                                    QMessageBox::Yes | QMessageBox::No,
                                    QMessageBox::No);
    if (ret == QMessageBox::Yes)
        m_project->removeTarget(t);
260
261
}

Tobias Hunger's avatar
Tobias Hunger committed
262
263
264
265
266
void TargetSettingsPanelWidget::targetAdded(ProjectExplorer::Target *target)
{
    Q_ASSERT(m_project == target->project());
    Q_ASSERT(m_selector);

267
268
269
270
271
272
273
274
275
    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);
            m_selector->insertTarget(pos, target->displayName());
            break;
        }
    }

276
277
    m_selector->setAddButtonEnabled(m_project->possibleTargetIds().count() > 0);
    m_selector->setRemoveButtonEnabled(m_project->targets().count() > 1);
Tobias Hunger's avatar
Tobias Hunger committed
278
279
}

280
void TargetSettingsPanelWidget::aboutToRemoveTarget(ProjectExplorer::Target *target)
Tobias Hunger's avatar
Tobias Hunger committed
281
282
283
284
{
    Q_ASSERT(m_project == target->project());
    Q_ASSERT(m_selector);

285
    int index(m_targets.indexOf(target));
Tobias Hunger's avatar
Tobias Hunger committed
286
287
    if (index < 0)
        return;
288
289
    m_targets.removeAt(index);

Tobias Hunger's avatar
Tobias Hunger committed
290
    m_selector->removeTarget(index);
291
292
    m_selector->setAddButtonEnabled(m_project->possibleTargetIds().count() > 0);
    m_selector->setRemoveButtonEnabled(m_project->targets().count() > 2); // target is not yet removed!
Tobias Hunger's avatar
Tobias Hunger committed
293
294
295
296
297
298
299
}

void TargetSettingsPanelWidget::activeTargetChanged(ProjectExplorer::Target *target)
{
    Q_ASSERT(m_project == target->project());
    Q_ASSERT(m_selector);

300
    int index(m_targets.indexOf(target));
Tobias Hunger's avatar
Tobias Hunger committed
301
302
    m_selector->markActive(index);
}