1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "targetsettingspanel.h"
35 #include "buildsettingspropertiespage.h"
37 #include "projectwindow.h"
38 #include "runsettingspropertiespage.h"
40 #include "targetsettingswidget.h"
42 #include <extensionsystem/pluginmanager.h>
44 #include <QtCore/QCoreApplication>
45 #include <QtGui/QLabel>
46 #include <QtGui/QMenu>
47 #include <QtGui/QMessageBox>
48 #include <QtGui/QVBoxLayout>
49 #include <QtGui/QStackedWidget>
51 using namespace ProjectExplorer;
52 using namespace ProjectExplorer::Internal;
56 // TargetSettingsWidget
59 TargetSettingsPanelWidget::TargetSettingsPanelWidget(Project *project) :
67 m_panelWidgets[0] = 0;
68 m_panelWidgets[1] = 0;
70 m_addMenu = new QMenu(this);
74 connect(m_project, SIGNAL(addedTarget(ProjectExplorer::Target*)),
75 this, SLOT(targetAdded(ProjectExplorer::Target*)));
76 connect(m_project, SIGNAL(removedTarget(ProjectExplorer::Target*)),
77 this, SLOT(removedTarget(ProjectExplorer::Target*)));
79 connect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
80 this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
82 QList<ITargetFactory *> factories =
83 ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
85 foreach (ITargetFactory *fac, factories) {
86 connect(fac, SIGNAL(supportedTargetIdsChanged()),
87 this, SLOT(updateTargetAddAndRemoveButtons()));
91 TargetSettingsPanelWidget::~TargetSettingsPanelWidget()
95 void TargetSettingsPanelWidget::setupUi()
97 QVBoxLayout *viewLayout = new QVBoxLayout(this);
98 viewLayout->setMargin(0);
99 viewLayout->setSpacing(0);
101 m_selector = new TargetSettingsWidget(this);
102 viewLayout->addWidget(m_selector);
104 // Setup our container for the contents:
105 m_centralWidget = new QStackedWidget(this);
106 m_selector->setCentralWidget(m_centralWidget);
109 m_noTargetLabel = new QWidget;
110 QVBoxLayout *noTargetLayout = new QVBoxLayout(m_noTargetLabel);
111 noTargetLayout->setMargin(0);
112 QLabel *label = new QLabel(m_noTargetLabel);
113 label->setText(tr("No target defined."));
115 QFont f = label->font();
116 f.setPointSizeF(f.pointSizeF() * 1.4);
120 label->setMargin(10);
121 label->setAlignment(Qt::AlignTop);
122 noTargetLayout->addWidget(label);
123 noTargetLayout->addStretch(10);
124 m_centralWidget->addWidget(m_noTargetLabel);
126 foreach (Target *t, m_project->targets())
129 // Now set the correct target
130 int index = m_targets.indexOf(m_project->activeTarget());
131 m_selector->setCurrentIndex(index);
132 m_selector->setCurrentSubIndex(0);
134 currentTargetChanged(index, 0);
136 connect(m_selector, SIGNAL(currentChanged(int,int)),
137 this, SLOT(currentTargetChanged(int,int)));
139 connect(m_selector, SIGNAL(removeButtonClicked()),
140 this, SLOT(removeTarget()));
142 m_selector->setAddButtonMenu(m_addMenu);
143 connect(m_addMenu, SIGNAL(triggered(QAction*)),
144 this, SLOT(addTarget(QAction*)));
146 updateTargetAddAndRemoveButtons();
149 void TargetSettingsPanelWidget::currentTargetChanged(int targetIndex, int subIndex)
151 if (targetIndex < -1 || targetIndex >= m_targets.count())
153 if (subIndex < -1 || subIndex >= 2)
156 if (targetIndex == -1 || subIndex == -1) { // no more targets!
157 delete m_panelWidgets[0];
158 m_panelWidgets[0] = 0;
159 delete m_panelWidgets[1];
160 m_panelWidgets[1] = 0;
162 m_centralWidget->setCurrentWidget(m_noTargetLabel);
166 Target *target = m_targets.at(targetIndex);
168 // Target was not actually changed:
169 if (m_currentTarget == target) {
170 if (m_panelWidgets[subIndex])
171 m_centralWidget->setCurrentWidget(m_panelWidgets[subIndex]);
173 m_centralWidget->setCurrentWidget(m_noTargetLabel);
177 // Target has changed:
178 m_currentTarget = target;
180 PanelsWidget *buildPanel = new PanelsWidget(m_centralWidget);
181 PanelsWidget *runPanel = new PanelsWidget(m_centralWidget);
183 foreach (ITargetPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<ITargetPanelFactory>()) {
184 if (panelFactory->id() == QLatin1String(BUILDSETTINGS_PANEL_ID)) {
185 IPropertiesPanel *panel = panelFactory->createPanel(target);
186 buildPanel->addPropertiesPanel(panel);
189 if (panelFactory->id() == QLatin1String(RUNSETTINGS_PANEL_ID)) {
190 IPropertiesPanel *panel = panelFactory->createPanel(target);
191 runPanel->addPropertiesPanel(panel);
195 m_centralWidget->addWidget(buildPanel);
196 m_centralWidget->addWidget(runPanel);
198 m_centralWidget->setCurrentWidget(subIndex == 0 ? buildPanel : runPanel);
200 delete m_panelWidgets[0];
201 m_panelWidgets[0] = buildPanel;
202 delete m_panelWidgets[1];
203 m_panelWidgets[1] = runPanel;
206 m_project->setActiveTarget(target);
209 void TargetSettingsPanelWidget::addTarget(QAction *action)
211 QString id = action->data().toString();
212 Q_ASSERT(!m_project->target(id));
213 QList<ITargetFactory *> factories =
214 ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
217 foreach (ITargetFactory *fac, factories) {
218 if (fac->canCreate(m_project, id)) {
219 target = fac->create(m_project, id);
226 m_project->addTarget(target);
229 void TargetSettingsPanelWidget::removeTarget()
231 int index = m_selector->currentIndex();
232 Target *t = m_targets.at(index);
233 int ret = QMessageBox::warning(this, tr("Qt Creator"),
234 tr("Do you really want to remove the\n"
235 "\"%1\" target?").arg(t->displayName()),
236 QMessageBox::Yes | QMessageBox::No,
238 if (ret == QMessageBox::Yes)
239 m_project->removeTarget(t);
242 void TargetSettingsPanelWidget::targetAdded(ProjectExplorer::Target *target)
244 Q_ASSERT(m_project == target->project());
245 Q_ASSERT(m_selector);
247 for (int pos = 0; pos <= m_targets.count(); ++pos) {
248 if (m_targets.count() == pos ||
249 m_targets.at(pos)->displayName() > target->displayName()) {
250 m_targets.insert(pos, target);
251 m_selector->insertTarget(pos, target->displayName());
256 updateTargetAddAndRemoveButtons();
259 void TargetSettingsPanelWidget::removedTarget(ProjectExplorer::Target *target)
261 Q_ASSERT(m_project == target->project());
262 Q_ASSERT(m_selector);
264 int index(m_targets.indexOf(target));
267 m_targets.removeAt(index);
269 m_selector->removeTarget(index);
271 updateTargetAddAndRemoveButtons();
274 void TargetSettingsPanelWidget::activeTargetChanged(ProjectExplorer::Target *target)
276 Q_ASSERT(m_project == target->project());
277 Q_ASSERT(m_selector);
279 int index = m_targets.indexOf(target);
280 m_selector->setCurrentIndex(index);
283 void TargetSettingsPanelWidget::updateTargetAddAndRemoveButtons()
290 QList<ITargetFactory *> factories =
291 ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
293 foreach (ITargetFactory *fac, factories) {
294 foreach (const QString &id, fac->supportedTargetIds(m_project)) {
295 if (m_project->target(id))
297 QString displayName = fac->displayNameForId(id);
298 QAction *action = new QAction(displayName, m_addMenu);
299 action->setData(QVariant(id));
301 foreach (QAction *existing, m_addMenu->actions()) {
302 if (existing->text() > action->text()) {
303 m_addMenu->insertAction(existing, action);
309 m_addMenu->addAction(action);
313 m_selector->setAddButtonEnabled(!m_addMenu->actions().isEmpty());
314 m_selector->setRemoveButtonEnabled(m_project->targets().count() > 1);
317 int TargetSettingsPanelWidget::currentSubIndex() const
319 return m_selector->currentSubIndex();
322 void TargetSettingsPanelWidget::setCurrentSubIndex(int subIndex)
324 m_selector->setCurrentSubIndex(subIndex);