OSDN Git Service

Update license.
[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 (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 "maemodeploystepwidget.h"
34 #include "ui_maemodeploystepwidget.h"
35
36 #include "maemodeploystep.h"
37 #include "maemodeployablelistmodel.h"
38 #include "maemodeployables.h"
39 #include "maemodeviceconfigurations.h"
40 #include "maemosettingspages.h"
41 #include "maemoglobal.h"
42 #include "maemomanager.h"
43 #include "maemopertargetdeviceconfigurationlistmodel.h"
44 #include "maemorunconfiguration.h"
45 #include "qt4maemotarget.h"
46
47 #include <coreplugin/icore.h>
48 #include <projectexplorer/buildconfiguration.h>
49 #include <projectexplorer/target.h>
50 #include <utils/qtcassert.h>
51
52 #include <QtGui/QFileDialog>
53 #include <QtGui/QMessageBox>
54 #include <QtGui/QPixmap>
55
56 namespace Qt4ProjectManager {
57 namespace Internal {
58
59 MaemoDeployStepWidget::MaemoDeployStepWidget(MaemoDeployStep *step) :
60     ProjectExplorer::BuildStepConfigWidget(),
61     ui(new Ui::MaemoDeployStepWidget),
62     m_step(step)
63 {
64     ui->setupUi(this);
65     ui->modelComboBox->setModel(m_step->deployables().data());
66     connect(m_step->deployables().data(), SIGNAL(modelAboutToBeReset()),
67         SLOT(handleModelListToBeReset()));
68
69     // Queued connection because of race condition with combo box's reaction
70     // to modelReset().
71     connect(m_step->deployables().data(), SIGNAL(modelReset()),
72         SLOT(handleModelListReset()), Qt::QueuedConnection);
73
74     connect(ui->modelComboBox, SIGNAL(currentIndexChanged(int)),
75         SLOT(setModel(int)));
76     connect(ui->addDesktopFileButton, SIGNAL(clicked()),
77         SLOT(addDesktopFile()));
78     connect(ui->addIconButton, SIGNAL(clicked()), SLOT(addIcon()));
79     handleModelListReset();
80
81 }
82
83 MaemoDeployStepWidget::~MaemoDeployStepWidget()
84 {
85     delete ui;
86 }
87
88 void MaemoDeployStepWidget::init()
89 {
90     ui->deviceConfigComboBox->setModel(m_step->maemotarget()->deviceConfigurationsModel());
91     connect(m_step, SIGNAL(deviceConfigChanged()), SLOT(handleDeviceUpdate()));
92     handleDeviceUpdate();
93     connect(ui->deviceConfigComboBox, SIGNAL(activated(int)), this,
94         SLOT(setCurrentDeviceConfig(int)));
95     ui->deployToSysrootCheckBox->setChecked(m_step->isDeployToSysrootEnabled());
96     connect(ui->deployToSysrootCheckBox, SIGNAL(toggled(bool)), this,
97         SLOT(setDeployToSysroot(bool)));
98     connect(ui->manageDevConfsLabel, SIGNAL(linkActivated(QString)),
99         SLOT(showDeviceConfigurations()));
100 }
101
102 void MaemoDeployStepWidget::handleDeviceUpdate()
103 {
104     const MaemoDeviceConfig::ConstPtr &devConf = m_step->deviceConfig();
105     const MaemoDeviceConfig::Id internalId
106         = MaemoDeviceConfigurations::instance()->internalId(devConf);
107     const int newIndex = m_step->maemotarget()->deviceConfigurationsModel()
108         ->indexForInternalId(internalId);
109     ui->deviceConfigComboBox->setCurrentIndex(newIndex);
110     emit updateSummary();
111 }
112
113 QString MaemoDeployStepWidget::summaryText() const
114 {
115     return tr("<b>Deploy to device</b>: %1")
116         .arg(MaemoGlobal::deviceConfigurationName(m_step->deviceConfig()));
117 }
118
119 QString MaemoDeployStepWidget::displayName() const
120 {
121     return QString();
122 }
123
124 void MaemoDeployStepWidget::setCurrentDeviceConfig(int index)
125 {
126     disconnect(m_step, SIGNAL(deviceConfigChanged()), this,
127         SLOT(handleDeviceUpdate()));
128     m_step->setDeviceConfig(index);
129     connect(m_step, SIGNAL(deviceConfigChanged()), SLOT(handleDeviceUpdate()));
130     updateSummary();
131 }
132
133 void MaemoDeployStepWidget::setDeployToSysroot(bool doDeploy)
134 {
135     m_step->setDeployToSysrootEnabled(doDeploy);
136 }
137
138 void MaemoDeployStepWidget::handleModelListToBeReset()
139 {
140     ui->tableView->reset(); // Otherwise we'll crash if the user is currently editing.
141     ui->tableView->setModel(0);
142     ui->addDesktopFileButton->setEnabled(false);
143     ui->addIconButton->setEnabled(false);
144 }
145
146 void MaemoDeployStepWidget::handleModelListReset()
147 {
148     QTC_ASSERT(m_step->deployables()->modelCount() == ui->modelComboBox->count(), return);
149     if (m_step->deployables()->modelCount() > 0) {
150         if (ui->modelComboBox->currentIndex() == -1)
151             ui->modelComboBox->setCurrentIndex(0);
152         else
153             setModel(ui->modelComboBox->currentIndex());
154     }
155 }
156
157 void MaemoDeployStepWidget::setModel(int row)
158 {
159     bool canAddDesktopFile = false;
160     bool canAddIconFile = false;
161     if (row != -1) {
162         MaemoDeployableListModel *const model
163             = m_step->deployables()->modelAt(row);
164         ui->tableView->setModel(model);
165         ui->tableView->resizeRowsToContents();
166         canAddDesktopFile = model->canAddDesktopFile();
167         canAddIconFile = model->canAddIcon();
168     }
169     ui->addDesktopFileButton->setEnabled(canAddDesktopFile);
170     ui->addIconButton->setEnabled(canAddIconFile);
171 }
172
173 void MaemoDeployStepWidget::addDesktopFile()
174 {
175     const int modelRow = ui->modelComboBox->currentIndex();
176     if (modelRow == -1)
177         return;
178     MaemoDeployableListModel *const model
179         = m_step->deployables()->modelAt(modelRow);
180     QString error;
181     if (!model->addDesktopFile(error)) {
182         QMessageBox::warning(this, tr("Could not create desktop file"),
183              tr("Error creating desktop file: %1").arg(error));
184     }
185     ui->addDesktopFileButton->setEnabled(model->canAddDesktopFile());
186     ui->tableView->resizeRowsToContents();
187 }
188
189 void MaemoDeployStepWidget::addIcon()
190 {
191     const int modelRow = ui->modelComboBox->currentIndex();
192     if (modelRow == -1)
193         return;
194
195     MaemoDeployableListModel *const model
196         = m_step->deployables()->modelAt(modelRow);
197     const QString origFilePath = QFileDialog::getOpenFileName(this,
198         tr("Choose Icon (will be scaled to 64x64 pixels, if necessary)"),
199         model->projectDir(), QLatin1String("(*.png)"));
200     if (origFilePath.isEmpty())
201         return;
202     QPixmap pixmap(origFilePath);
203     if (pixmap.isNull()) {
204         QMessageBox::critical(this, tr("Invalid Icon"),
205             tr("Unable to read image"));
206         return;
207     }
208     const QSize iconSize(64, 64);
209     if (pixmap.size() != iconSize)
210         pixmap = pixmap.scaled(iconSize);
211     const QString newFileName = model->projectName() + QLatin1Char('.')
212             + QFileInfo(origFilePath).suffix();
213     const QString newFilePath = model->projectDir() + QLatin1Char('/')
214         + newFileName;
215     if (!pixmap.save(newFilePath)) {
216         QMessageBox::critical(this, tr("Failed to Save Icon"),
217             tr("Could not save icon to '%1'.").arg(newFilePath));
218         return;
219     }
220
221     QString error;
222     if (!model->addIcon(newFileName, error)) {
223         QMessageBox::critical(this, tr("Could Not Add Icon"),
224              tr("Error adding icon: %1").arg(error));
225     }
226     ui->addIconButton->setEnabled(model->canAddIcon());
227     ui->tableView->resizeRowsToContents();
228 }
229
230 void MaemoDeployStepWidget::showDeviceConfigurations()
231 {
232     MaemoDeviceConfigurationsSettingsPage *page
233         = MaemoManager::instance().deviceConfigurationsSettingsPage();
234     Core::ICore::instance()->showOptionsDialog(page->category(), page->id());
235 }
236
237 } // namespace Internal
238 } // namespace Qt4ProjectManager