OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / targetsettingspanel.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "targetsettingspanel.h"
34
35 #include "buildsettingspropertiespage.h"
36 #include "project.h"
37 #include "projectwindow.h"
38 #include "runsettingspropertiespage.h"
39 #include "target.h"
40 #include "targetsettingswidget.h"
41
42 #include <extensionsystem/pluginmanager.h>
43
44 #include <QtCore/QCoreApplication>
45 #include <QtGui/QLabel>
46 #include <QtGui/QMenu>
47 #include <QtGui/QMessageBox>
48 #include <QtGui/QVBoxLayout>
49 #include <QtGui/QStackedWidget>
50
51 using namespace ProjectExplorer;
52 using namespace ProjectExplorer::Internal;
53
54
55 ///
56 // TargetSettingsWidget
57 ///
58
59 TargetSettingsPanelWidget::TargetSettingsPanelWidget(Project *project) :
60     m_currentTarget(0),
61     m_project(project),
62     m_selector(0),
63     m_centralWidget(0)
64 {
65     Q_ASSERT(m_project);
66
67     m_panelWidgets[0] = 0;
68     m_panelWidgets[1] = 0;
69
70     m_addMenu = new QMenu(this);
71
72     setupUi();
73
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*)));
78
79     connect(m_project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
80             this, SLOT(activeTargetChanged(ProjectExplorer::Target*)));
81
82     QList<ITargetFactory *> factories =
83             ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
84
85     foreach (ITargetFactory *fac, factories) {
86         connect(fac, SIGNAL(supportedTargetIdsChanged()),
87                 this, SLOT(updateTargetAddAndRemoveButtons()));
88     }
89 }
90
91 TargetSettingsPanelWidget::~TargetSettingsPanelWidget()
92 {
93 }
94
95 void TargetSettingsPanelWidget::setupUi()
96 {
97     QVBoxLayout *viewLayout = new QVBoxLayout(this);
98     viewLayout->setMargin(0);
99     viewLayout->setSpacing(0);
100
101     m_selector = new TargetSettingsWidget(this);
102     viewLayout->addWidget(m_selector);
103
104     // Setup our container for the contents:
105     m_centralWidget = new QStackedWidget(this);
106     m_selector->setCentralWidget(m_centralWidget);
107
108     // no target label:
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."));
114     {
115         QFont f = label->font();
116         f.setPointSizeF(f.pointSizeF() * 1.4);
117         f.setBold(true);
118         label->setFont(f);
119     }
120     label->setMargin(10);
121     label->setAlignment(Qt::AlignTop);
122     noTargetLayout->addWidget(label);
123     noTargetLayout->addStretch(10);
124     m_centralWidget->addWidget(m_noTargetLabel);
125
126     foreach (Target *t, m_project->targets())
127         targetAdded(t);
128
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);
133
134     currentTargetChanged(index, 0);
135
136     connect(m_selector, SIGNAL(currentChanged(int,int)),
137             this, SLOT(currentTargetChanged(int,int)));
138
139     connect(m_selector, SIGNAL(removeButtonClicked()),
140             this, SLOT(removeTarget()));
141
142     m_selector->setAddButtonMenu(m_addMenu);
143     connect(m_addMenu, SIGNAL(triggered(QAction*)),
144             this, SLOT(addTarget(QAction*)));
145
146     updateTargetAddAndRemoveButtons();
147 }
148
149 void TargetSettingsPanelWidget::currentTargetChanged(int targetIndex, int subIndex)
150 {
151     if (targetIndex < -1 || targetIndex >= m_targets.count())
152         return;
153     if (subIndex < -1 || subIndex >= 2)
154         return;
155
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;
161
162         m_centralWidget->setCurrentWidget(m_noTargetLabel);
163         return;
164     }
165
166     Target *target = m_targets.at(targetIndex);
167
168     // Target was not actually changed:
169     if (m_currentTarget == target) {
170         if (m_panelWidgets[subIndex])
171             m_centralWidget->setCurrentWidget(m_panelWidgets[subIndex]);
172         else
173             m_centralWidget->setCurrentWidget(m_noTargetLabel);
174         return;
175     }
176
177     // Target has changed:
178     m_currentTarget = target;
179
180     PanelsWidget *buildPanel = new PanelsWidget(m_centralWidget);
181     PanelsWidget *runPanel = new PanelsWidget(m_centralWidget);
182
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);
187             continue;
188         }
189         if (panelFactory->id() == QLatin1String(RUNSETTINGS_PANEL_ID)) {
190             IPropertiesPanel *panel = panelFactory->createPanel(target);
191             runPanel->addPropertiesPanel(panel);
192             continue;
193         }
194     }
195     m_centralWidget->addWidget(buildPanel);
196     m_centralWidget->addWidget(runPanel);
197
198     m_centralWidget->setCurrentWidget(subIndex == 0 ? buildPanel : runPanel);
199
200     delete m_panelWidgets[0];
201     m_panelWidgets[0] = buildPanel;
202     delete m_panelWidgets[1];
203     m_panelWidgets[1] = runPanel;
204
205
206     m_project->setActiveTarget(target);
207 }
208
209 void TargetSettingsPanelWidget::addTarget(QAction *action)
210 {
211     QString id = action->data().toString();
212     Q_ASSERT(!m_project->target(id));
213     QList<ITargetFactory *> factories =
214             ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
215
216     Target *target = 0;
217     foreach (ITargetFactory *fac, factories) {
218         if (fac->canCreate(m_project, id)) {
219             target = fac->create(m_project, id);
220             break;
221         }
222     }
223
224     if (!target)
225         return;
226     m_project->addTarget(target);
227 }
228
229 void TargetSettingsPanelWidget::removeTarget()
230 {
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,
237                                     QMessageBox::No);
238     if (ret == QMessageBox::Yes)
239         m_project->removeTarget(t);
240 }
241
242 void TargetSettingsPanelWidget::targetAdded(ProjectExplorer::Target *target)
243 {
244     Q_ASSERT(m_project == target->project());
245     Q_ASSERT(m_selector);
246
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());
252             break;
253         }
254     }
255
256     updateTargetAddAndRemoveButtons();
257 }
258
259 void TargetSettingsPanelWidget::removedTarget(ProjectExplorer::Target *target)
260 {
261     Q_ASSERT(m_project == target->project());
262     Q_ASSERT(m_selector);
263
264     int index(m_targets.indexOf(target));
265     if (index < 0)
266         return;
267     m_targets.removeAt(index);
268
269     m_selector->removeTarget(index);
270
271     updateTargetAddAndRemoveButtons();
272 }
273
274 void TargetSettingsPanelWidget::activeTargetChanged(ProjectExplorer::Target *target)
275 {
276     Q_ASSERT(m_project == target->project());
277     Q_ASSERT(m_selector);
278
279     int index = m_targets.indexOf(target);
280     m_selector->setCurrentIndex(index);
281 }
282
283 void TargetSettingsPanelWidget::updateTargetAddAndRemoveButtons()
284 {
285     if (!m_selector)
286         return;
287
288     m_addMenu->clear();
289
290     QList<ITargetFactory *> factories =
291             ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>();
292
293     foreach (ITargetFactory *fac, factories) {
294         foreach (const QString &id, fac->supportedTargetIds(m_project)) {
295             if (m_project->target(id))
296                 continue;
297             QString displayName = fac->displayNameForId(id);
298             QAction *action = new QAction(displayName, m_addMenu);
299             action->setData(QVariant(id));
300             bool added = false;
301             foreach (QAction *existing, m_addMenu->actions()) {
302                 if (existing->text() > action->text()) {
303                     m_addMenu->insertAction(existing, action);
304                     added = true;
305                 }
306             }
307
308             if (!added)
309                 m_addMenu->addAction(action);
310         }
311     }
312
313     m_selector->setAddButtonEnabled(!m_addMenu->actions().isEmpty());
314     m_selector->setRemoveButtonEnabled(m_project->targets().count() > 1);
315 }
316
317 int TargetSettingsPanelWidget::currentSubIndex() const
318 {
319     return m_selector->currentSubIndex();
320 }
321
322 void TargetSettingsPanelWidget::setCurrentSubIndex(int subIndex)
323 {
324     m_selector->setCurrentSubIndex(subIndex);
325 }