OSDN Git Service

b258d9f331c7454c036f78fcfa65d874532d92be
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-maemo / maemodeploystepwidget.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "maemodeploystepwidget.h"
35 #include "ui_maemodeploystepwidget.h"
36
37 #include "maemodeploystep.h"
38 #include "maemodeployablelistmodel.h"
39 #include "maemodeployables.h"
40 #include "maemodeviceconfigurations.h"
41 #include "maemosettingspages.h"
42 #include "maemoglobal.h"
43 #include "maemomanager.h"
44 #include "maemopertargetdeviceconfigurationlistmodel.h"
45 #include "maemorunconfiguration.h"
46 #include "qt4maemotarget.h"
47
48 #include <coreplugin/icore.h>
49 #include <projectexplorer/buildconfiguration.h>
50 #include <projectexplorer/target.h>
51 #include <utils/qtcassert.h>
52
53 #include <QtGui/QFileDialog>
54 #include <QtGui/QMessageBox>
55 #include <QtGui/QPixmap>
56
57 namespace Qt4ProjectManager {
58 namespace Internal {
59
60 MaemoDeployStepWidget::MaemoDeployStepWidget(MaemoDeployStep *step) :
61     ProjectExplorer::BuildStepConfigWidget(),
62     ui(new Ui::MaemoDeployStepWidget),
63     m_step(step)
64 {
65     ui->setupUi(this);
66     ui->modelComboBox->setModel(m_step->deployables().data());
67     connect(m_step->deployables().data(), SIGNAL(modelAboutToBeReset()),
68         SLOT(handleModelListToBeReset()));
69
70     // Queued connection because of race condition with combo box's reaction
71     // to modelReset().
72     connect(m_step->deployables().data(), SIGNAL(modelReset()),
73         SLOT(handleModelListReset()), Qt::QueuedConnection);
74
75     connect(ui->modelComboBox, SIGNAL(currentIndexChanged(int)),
76         SLOT(setModel(int)));
77     connect(ui->addDesktopFileButton, SIGNAL(clicked()),
78         SLOT(addDesktopFile()));
79     connect(ui->addIconButton, SIGNAL(clicked()), SLOT(addIcon()));
80     handleModelListReset();
81
82 }
83
84 MaemoDeployStepWidget::~MaemoDeployStepWidget()
85 {
86     delete ui;
87 }
88
89 void MaemoDeployStepWidget::init()
90 {
91     ui->deviceConfigComboBox->setModel(m_step->maemotarget()->deviceConfigurationsModel());
92     connect(m_step, SIGNAL(deviceConfigChanged()), SLOT(handleDeviceUpdate()));
93     handleDeviceUpdate();
94     connect(ui->deviceConfigComboBox, SIGNAL(activated(int)), this,
95         SLOT(setCurrentDeviceConfig(int)));
96     ui->deployToSysrootCheckBox->setChecked(m_step->isDeployToSysrootEnabled());
97     connect(ui->deployToSysrootCheckBox, SIGNAL(toggled(bool)), this,
98         SLOT(setDeployToSysroot(bool)));
99     connect(ui->manageDevConfsLabel, SIGNAL(linkActivated(QString)),
100         SLOT(showDeviceConfigurations()));
101 }
102
103 void MaemoDeployStepWidget::handleDeviceUpdate()
104 {
105     const MaemoDeviceConfig::ConstPtr &devConf = m_step->deviceConfig();
106     const MaemoDeviceConfig::Id internalId
107         = MaemoDeviceConfigurations::instance()->internalId(devConf);
108     const int newIndex = m_step->maemotarget()->deviceConfigurationsModel()
109         ->indexForInternalId(internalId);
110     ui->deviceConfigComboBox->setCurrentIndex(newIndex);
111     emit updateSummary();
112 }
113
114 QString MaemoDeployStepWidget::summaryText() const
115 {
116     return tr("<b>Deploy to device</b>: %1")
117         .arg(MaemoGlobal::deviceConfigurationName(m_step->deviceConfig()));
118 }
119
120 QString MaemoDeployStepWidget::displayName() const
121 {
122     return QString();
123 }
124
125 void MaemoDeployStepWidget::setCurrentDeviceConfig(int index)
126 {
127     disconnect(m_step, SIGNAL(deviceConfigChanged()), this,
128         SLOT(handleDeviceUpdate()));
129     m_step->setDeviceConfig(index);
130     connect(m_step, SIGNAL(deviceConfigChanged()), SLOT(handleDeviceUpdate()));
131     updateSummary();
132 }
133
134 void MaemoDeployStepWidget::setDeployToSysroot(bool doDeploy)
135 {
136     m_step->setDeployToSysrootEnabled(doDeploy);
137 }
138
139 void MaemoDeployStepWidget::handleModelListToBeReset()
140 {
141     ui->tableView->reset(); // Otherwise we'll crash if the user is currently editing.
142     ui->tableView->setModel(0);
143     ui->addDesktopFileButton->setEnabled(false);
144     ui->addIconButton->setEnabled(false);
145 }
146
147 void MaemoDeployStepWidget::handleModelListReset()
148 {
149     QTC_ASSERT(m_step->deployables()->modelCount() == ui->modelComboBox->count(), return);
150     if (m_step->deployables()->modelCount() > 0) {
151         if (ui->modelComboBox->currentIndex() == -1)
152             ui->modelComboBox->setCurrentIndex(0);
153         else
154             setModel(ui->modelComboBox->currentIndex());
155     }
156 }
157
158 void MaemoDeployStepWidget::setModel(int row)
159 {
160     bool canAddDesktopFile = false;
161     bool canAddIconFile = false;
162     if (row != -1) {
163         MaemoDeployableListModel *const model
164             = m_step->deployables()->modelAt(row);
165         ui->tableView->setModel(model);
166         ui->tableView->resizeRowsToContents();
167         canAddDesktopFile = model->canAddDesktopFile();
168         canAddIconFile = model->canAddIcon();
169     }
170     ui->addDesktopFileButton->setEnabled(canAddDesktopFile);
171     ui->addIconButton->setEnabled(canAddIconFile);
172 }
173
174 void MaemoDeployStepWidget::addDesktopFile()
175 {
176     const int modelRow = ui->modelComboBox->currentIndex();
177     if (modelRow == -1)
178         return;
179     MaemoDeployableListModel *const model
180         = m_step->deployables()->modelAt(modelRow);
181     QString error;
182     if (!model->addDesktopFile(error)) {
183         QMessageBox::warning(this, tr("Could not create desktop file"),
184              tr("Error creating desktop file: %1").arg(error));
185     }
186     ui->addDesktopFileButton->setEnabled(model->canAddDesktopFile());
187     ui->tableView->resizeRowsToContents();
188 }
189
190 void MaemoDeployStepWidget::addIcon()
191 {
192     const int modelRow = ui->modelComboBox->currentIndex();
193     if (modelRow == -1)
194         return;
195
196     MaemoDeployableListModel *const model
197         = m_step->deployables()->modelAt(modelRow);
198     const QString origFilePath = QFileDialog::getOpenFileName(this,
199         tr("Choose Icon (will be scaled to 64x64 pixels, if necessary)"),
200         model->projectDir(), QLatin1String("(*.png)"));
201     if (origFilePath.isEmpty())
202         return;
203     QPixmap pixmap(origFilePath);
204     if (pixmap.isNull()) {
205         QMessageBox::critical(this, tr("Invalid Icon"),
206             tr("Unable to read image"));
207         return;
208     }
209     const QSize iconSize(64, 64);
210     if (pixmap.size() != iconSize)
211         pixmap = pixmap.scaled(iconSize);
212     const QString newFileName = model->projectName() + QLatin1Char('.')
213             + QFileInfo(origFilePath).suffix();
214     const QString newFilePath = model->projectDir() + QLatin1Char('/')
215         + newFileName;
216     if (!pixmap.save(newFilePath)) {
217         QMessageBox::critical(this, tr("Failed to Save Icon"),
218             tr("Could not save icon to '%1'.").arg(newFilePath));
219         return;
220     }
221
222     QString error;
223     if (!model->addIcon(newFileName, error)) {
224         QMessageBox::critical(this, tr("Could Not Add Icon"),
225              tr("Error adding icon: %1").arg(error));
226     }
227     ui->addIconButton->setEnabled(model->canAddIcon());
228     ui->tableView->resizeRowsToContents();
229 }
230
231 void MaemoDeployStepWidget::showDeviceConfigurations()
232 {
233     MaemoDeviceConfigurationsSettingsPage *page
234         = MaemoManager::instance().deviceConfigurationsSettingsPage();
235     Core::ICore::instance()->showOptionsDialog(page->category(), page->id());
236 }
237
238 } // namespace Internal
239 } // namespace Qt4ProjectManager