Maemo: Allow adding an application launcher icon via the GUI.

Reviewed-by: kh1
This commit is contained in:
Christian Kandeler
2010-11-25 17:58:02 +01:00
parent 7b7ce04372
commit da115606fa
5 changed files with 99 additions and 0 deletions

View File

@@ -41,9 +41,13 @@
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtGui/QBrush>
#include <QtGui/QImageReader>
namespace Qt4ProjectManager {
namespace Internal {
namespace {
const QLatin1String RemoteIconPath("/usr/share/icons/hicolor/64x64/apps");
} // anonymous namespace
MaemoDeployableListModel::MaemoDeployableListModel(const Qt4ProFileNode *proFileNode,
ProFileUpdateSetting updateSetting, QObject *parent)
@@ -232,6 +236,21 @@ bool MaemoDeployableListModel::canAddDesktopFile() const
return true;
}
bool MaemoDeployableListModel::canAddIcon() const
{
if (m_projectType == LibraryTemplate)
return false;
const QList<QByteArray> &imageTypes = QImageReader::supportedImageFormats();
foreach (const MaemoDeployable &d, m_deployables) {
const QByteArray extension
= QFileInfo(d.localFilePath).suffix().toLocal8Bit();
if (d.remoteDir.startsWith(RemoteIconPath)
&& imageTypes.contains(extension))
return false;
}
return true;
}
bool MaemoDeployableListModel::addDesktopFile(QString &error)
{
if (!canAddDesktopFile())
@@ -283,6 +302,28 @@ bool MaemoDeployableListModel::addDesktopFile(QString &error)
return true;
}
bool MaemoDeployableListModel::addIcon(const QString &fileName, QString &error)
{
if (!canAddIcon())
return true;
const QString filesLine = QLatin1String("icon.files = ") + fileName;
const QString pathLine = QLatin1String("icon.path = ") + RemoteIconPath;
const QLatin1String installsLine("INSTALLS += icon");
if (!addLinesToProFile(QStringList() << filesLine << pathLine
<< installsLine)) {
error = tr("Error writing project file.");
return false;
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
const QString filePath = QFileInfo(m_proFilePath).path()
+ QLatin1Char('/') + fileName;
m_deployables << MaemoDeployable(filePath, RemoteIconPath);
endInsertRows();
return true;
}
bool MaemoDeployableListModel::addLinesToProFile(const QStringList &lines)
{
QFile projectFile(m_proFilePath);

View File

@@ -68,7 +68,9 @@ public:
QString proFilePath() const { return m_proFilePath; }
bool hasTargetPath() const { return m_hasTargetPath; }
bool canAddDesktopFile() const;
bool canAddIcon() const;
bool addDesktopFile(QString &error);
bool addIcon(const QString &fileName, QString &error);
ProFileUpdateSetting proFileUpdateSetting() const {
return m_proFileUpdateSetting;
}

View File

@@ -11,7 +11,9 @@
#include <projectexplorer/target.h>
#include <utils/qtcassert.h>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QPixmap>
namespace Qt4ProjectManager {
namespace Internal {
@@ -35,6 +37,7 @@ MaemoDeployStepWidget::MaemoDeployStepWidget(MaemoDeployStep *step) :
SLOT(setModel(int)));
connect(ui->addDesktopFileButton, SIGNAL(clicked()),
SLOT(addDesktopFile()));
connect(ui->addIconButton, SIGNAL(clicked()), SLOT(addIcon()));
handleModelListReset();
}
@@ -104,6 +107,7 @@ void MaemoDeployStepWidget::handleModelListToBeReset()
ui->tableView->reset(); // Otherwise we'll crash if the user is currently editing.
ui->tableView->setModel(0);
ui->addDesktopFileButton->setEnabled(false);
ui->addIconButton->setEnabled(false);
}
void MaemoDeployStepWidget::handleModelListReset()
@@ -120,14 +124,17 @@ void MaemoDeployStepWidget::handleModelListReset()
void MaemoDeployStepWidget::setModel(int row)
{
bool canAddDesktopFile = false;
bool canAddIconFile = false;
if (row != -1) {
MaemoDeployableListModel *const model
= m_step->deployables()->modelAt(row);
ui->tableView->setModel(model);
ui->tableView->resizeRowsToContents();
canAddDesktopFile = model->canAddDesktopFile();
canAddIconFile = model->canAddIcon();
}
ui->addDesktopFileButton->setEnabled(canAddDesktopFile);
ui->addIconButton->setEnabled(canAddIconFile);
}
void MaemoDeployStepWidget::addDesktopFile()
@@ -146,5 +153,46 @@ void MaemoDeployStepWidget::addDesktopFile()
ui->tableView->resizeRowsToContents();
}
void MaemoDeployStepWidget::addIcon()
{
const int modelRow = ui->modelComboBox->currentIndex();
if (modelRow == -1)
return;
MaemoDeployableListModel *const model
= m_step->deployables()->modelAt(modelRow);
const QString origFilePath = QFileDialog::getOpenFileName(this,
tr("Choose Icon (will be scaled to 64x64 pixels, if necessary)"),
model->projectDir(), QLatin1String("(*.png)"));
if (origFilePath.isEmpty())
return;
QPixmap pixmap(origFilePath);
if (pixmap.isNull()) {
QMessageBox::critical(this, tr("Invalid Icon"),
tr("Unable to read image"));
return;
}
const QSize iconSize(64, 64);
if (pixmap.size() != iconSize)
pixmap = pixmap.scaled(iconSize);
const QString newFileName = model->projectName() + QLatin1Char('.')
+ QFileInfo(origFilePath).suffix();
const QString newFilePath = model->projectDir() + QLatin1Char('/')
+ newFileName;
if (!pixmap.save(newFilePath)) {
QMessageBox::critical(this, tr("Failed to Save Icon"),
tr("Could not save icon to '%1'.").arg(newFilePath));
return;
}
QString error;
if (!model->addIcon(newFileName, error)) {
QMessageBox::critical(this, tr("Could Not Add Icon"),
tr("Error adding icon: %1").arg(error));
}
ui->addIconButton->setEnabled(model->canAddIcon());
ui->tableView->resizeRowsToContents();
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -30,6 +30,7 @@ private:
Q_SLOT void handleModelListToBeReset();
Q_SLOT void handleModelListReset();
Q_SLOT void addDesktopFile();
Q_SLOT void addIcon();
virtual void init();
virtual QString summaryText() const;

View File

@@ -154,6 +154,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addIconButton">
<property name="text">
<string>Add Launcher Icon ...</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">