OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-s60 / qt4symbiantarget.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 "qt4symbiantarget.h"
34 #include "qt4projectmanagerconstants.h"
35 #include "qt4project.h"
36 #include "qt-s60/s60deployconfiguration.h"
37 #include "qt-s60/s60emulatorrunconfiguration.h"
38 #include "qt-s60/s60devicerunconfiguration.h"
39
40 #include <coreplugin/coreconstants.h>
41 #include <projectexplorer/customexecutablerunconfiguration.h>
42 #include <projectexplorer/project.h>
43 #include <projectexplorer/toolchainmanager.h>
44 #include <symbianutils/symbiandevicemanager.h>
45 #include <QtGui/QPainter>
46 #include <QtGui/QApplication>
47
48 using namespace Qt4ProjectManager;
49 using namespace Qt4ProjectManager::Internal;
50
51 Qt4SymbianTarget::Qt4SymbianTarget(Qt4Project *parent, const QString &id) :
52     Qt4BaseTarget(parent, id),
53     m_connectedPixmap(QLatin1String(":/projectexplorer/images/ConnectionOn.png")),
54     m_disconnectedPixmap(QLatin1String(":/projectexplorer/images/ConnectionOff.png")),
55     m_buildConfigurationFactory(new Qt4BuildConfigurationFactory(this)),
56     m_deployConfigurationFactory(new S60DeployConfigurationFactory(this))
57 {
58     setDisplayName(defaultDisplayName(id));
59     setIcon(iconForId(id));
60     connect(this, SIGNAL(addedDeployConfiguration(ProjectExplorer::DeployConfiguration*)),
61             this, SLOT(onAddedDeployConfiguration(ProjectExplorer::DeployConfiguration*)));
62     connect(this, SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
63             this, SLOT(updateToolTipAndIcon()));
64 }
65
66 Qt4SymbianTarget::~Qt4SymbianTarget()
67 {
68
69 }
70
71 QString Qt4SymbianTarget::defaultDisplayName(const QString &id)
72 {
73     if (id == QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
74         return QApplication::translate("Qt4ProjectManager::Qt4Target", "Symbian Emulator", "Qt4 Symbian Emulator target display name");
75     if (id == QLatin1String(Constants::S60_DEVICE_TARGET_ID))
76         return QApplication::translate("Qt4ProjectManager::Qt4Target", "Symbian Device", "Qt4 Symbian Device target display name");
77     return QString();
78 }
79
80 QIcon Qt4SymbianTarget::iconForId(const QString &id)
81 {
82     if (id == QLatin1String(Constants::S60_EMULATOR_TARGET_ID))
83         return QIcon(":/projectexplorer/images/SymbianEmulator.png");
84     if (id == QLatin1String(Constants::S60_DEVICE_TARGET_ID))
85         return QIcon(":/projectexplorer/images/SymbianDevice.png");
86     return QIcon();
87 }
88
89 Qt4BuildConfigurationFactory *Qt4SymbianTarget::buildConfigurationFactory() const
90 {
91     return m_buildConfigurationFactory;
92 }
93
94 ProjectExplorer::DeployConfigurationFactory *Qt4SymbianTarget::deployConfigurationFactory() const
95 {
96     return m_deployConfigurationFactory;
97 }
98
99 QList<ProjectExplorer::ToolChain *> Qt4SymbianTarget::possibleToolChains(ProjectExplorer::BuildConfiguration *bc) const
100 {
101     QList<ProjectExplorer::ToolChain *> candidates = Qt4BaseTarget::possibleToolChains(bc);
102
103     QList<ProjectExplorer::ToolChain *> tmp;
104     if (id() == QLatin1String(Constants::S60_EMULATOR_TARGET_ID)) {
105         foreach (ProjectExplorer::ToolChain *tc, candidates) {
106             if (tc->id().startsWith(QLatin1String(Constants::WINSCW_TOOLCHAIN_ID)))
107                 tmp.append(tc);
108         }
109     } else if (id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID)) {
110         foreach (ProjectExplorer::ToolChain *tc, candidates) {
111             if (!tc->id().startsWith(Qt4ProjectManager::Constants::WINSCW_TOOLCHAIN_ID))
112                 tmp.append(tc);
113         }
114     }
115
116     return tmp;
117 }
118
119 QString Qt4SymbianTarget::defaultBuildDirectory() const
120 {
121     return project()->projectDirectory();
122 }
123
124 void Qt4SymbianTarget::createApplicationProFiles()
125 {
126     removeUnconfiguredCustomExectutableRunConfigurations();
127
128     // We use the list twice
129     QList<Qt4ProFileNode *> profiles = qt4Project()->applicationProFiles();
130     QSet<QString> paths;
131     foreach (Qt4ProFileNode *pro, profiles)
132         paths << pro->path();
133
134     if (id() == QLatin1String(Constants::S60_EMULATOR_TARGET_ID)) {
135         foreach (ProjectExplorer::RunConfiguration *rc, runConfigurations())
136             if (S60EmulatorRunConfiguration *qt4rc = qobject_cast<S60EmulatorRunConfiguration *>(rc))
137                 paths.remove(qt4rc->proFilePath());
138
139         // Only add new runconfigurations if there are none.
140         foreach (const QString &path, paths)
141             addRunConfiguration(new S60EmulatorRunConfiguration(this, path));
142     } else if (id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID)) {
143         foreach (ProjectExplorer::RunConfiguration *rc, runConfigurations())
144             if (S60DeviceRunConfiguration *qt4rc = qobject_cast<S60DeviceRunConfiguration *>(rc))
145                 paths.remove(qt4rc->proFilePath());
146
147         // Only add new runconfigurations if there are none.
148         foreach (const QString &path, paths)
149             addRunConfiguration(new S60DeviceRunConfiguration(this, path));
150     }
151
152     // Oh still none? Add a custom executable runconfiguration
153     if (runConfigurations().isEmpty()) {
154         addRunConfiguration(new ProjectExplorer::CustomExecutableRunConfiguration(this));
155     }
156 }
157
158 QList<ProjectExplorer::RunConfiguration *> Qt4SymbianTarget::runConfigurationsForNode(ProjectExplorer::Node *n)
159 {
160     QList<ProjectExplorer::RunConfiguration *> result;
161     foreach (ProjectExplorer::RunConfiguration *rc, runConfigurations()) {
162         if (id() == QLatin1String(Constants::S60_EMULATOR_TARGET_ID)) {
163             if (S60EmulatorRunConfiguration * s60rc = qobject_cast<S60EmulatorRunConfiguration *>(rc))
164                 if (s60rc->proFilePath() == n->path())
165                     result << rc;
166         } else if (id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID)) {
167             if (S60DeviceRunConfiguration *s60rc = qobject_cast<S60DeviceRunConfiguration *>(rc))
168                 if (s60rc->proFilePath() == n->path())
169                     result << rc;
170         }
171     }
172     return result;
173 }
174
175 bool Qt4SymbianTarget::isSymbianConnectionAvailable(QString &tooltipText)
176 {
177     const S60DeployConfiguration *s60DeployConf = qobject_cast<S60DeployConfiguration *>(activeDeployConfiguration());
178     if (!s60DeployConf)
179         return false;
180     switch (s60DeployConf->communicationChannel()) {
181     case S60DeployConfiguration::CommunicationTrkSerialConnection:
182     case S60DeployConfiguration::CommunicationCodaSerialConnection: {
183         const SymbianUtils::SymbianDeviceManager *sdm = SymbianUtils::SymbianDeviceManager::instance();
184         const int deviceIndex = sdm->findByPortName(s60DeployConf->serialPortName());
185         if (deviceIndex == -1) {
186             tooltipText = tr("<b>Device:</b> Not connected");
187             return false;
188         } else {
189             // device connected
190             const SymbianUtils::SymbianDevice device = sdm->devices().at(deviceIndex);
191             tooltipText = device.additionalInformation().isEmpty() ?
192                                     tr("<b>Device:</b> %1").arg(device.friendlyName()) :
193                                     tr("<b>Device:</b> %1, %2").arg(device.friendlyName(), device.additionalInformation());
194             return true;
195         }
196     }
197     break;
198     case S60DeployConfiguration::CommunicationCodaTcpConnection: {
199         if (!s60DeployConf->deviceAddress().isEmpty() && !s60DeployConf->devicePort().isEmpty()) {
200             tooltipText = tr("<b>IP address:</b> %1:%2").arg(s60DeployConf->deviceAddress(), s60DeployConf->devicePort());
201             return true;
202         }
203         return false;
204     }
205     break;
206     default:
207         break;
208     }
209     return false;
210 }
211
212 void Qt4SymbianTarget::onAddedDeployConfiguration(ProjectExplorer::DeployConfiguration *dc)
213 {
214     Q_ASSERT(dc);
215     S60DeployConfiguration *deployConf(qobject_cast<S60DeployConfiguration *>(dc));
216     if (!deployConf)
217         return;
218     connect(deployConf, SIGNAL(serialPortNameChanged()),
219             this, SLOT(slotUpdateDeviceInformation()));
220     connect(deployConf, SIGNAL(communicationChannelChanged()),
221             this, SLOT(slotUpdateDeviceInformation()));
222     connect(deployConf, SIGNAL(deviceAddressChanged()),
223             this, SLOT(slotUpdateDeviceInformation()));
224     connect(deployConf, SIGNAL(devicePortChanged()),
225             this, SLOT(slotUpdateDeviceInformation()));
226 }
227
228 void Qt4SymbianTarget::slotUpdateDeviceInformation()
229 {
230     S60DeployConfiguration *dc(qobject_cast<S60DeployConfiguration *>(sender()));
231     if (dc && dc == activeDeployConfiguration()) {
232         updateToolTipAndIcon();
233     }
234 }
235
236 void Qt4SymbianTarget::updateToolTipAndIcon()
237 {
238     static const int TARGET_OVERLAY_ORIGINAL_SIZE = 32;
239
240     if (qobject_cast<S60DeployConfiguration *>(activeDeployConfiguration()))  {
241         QPixmap overlay;
242         QString tooltip;
243         if (isSymbianConnectionAvailable(tooltip))
244             overlay = m_connectedPixmap;
245         else
246             overlay = m_disconnectedPixmap;
247         setToolTip(tooltip);
248
249         double factor = Core::Constants::TARGET_ICON_SIZE / (double)TARGET_OVERLAY_ORIGINAL_SIZE;
250         QSize overlaySize(overlay.size().width()*factor, overlay.size().height()*factor);
251         QPixmap pixmap(Core::Constants::TARGET_ICON_SIZE, Core::Constants::TARGET_ICON_SIZE);
252         pixmap.fill(Qt::transparent);
253         QPainter painter(&pixmap);
254         painter.drawPixmap(Core::Constants::TARGET_ICON_SIZE - overlaySize.width(),
255                            Core::Constants::TARGET_ICON_SIZE - overlaySize.height(),
256                            overlay.scaled(overlaySize));
257
258         setOverlayIcon(QIcon(pixmap));
259     } else {
260         setToolTip(QString());
261         setOverlayIcon(QIcon());
262     }
263 }