From da115606fab7cfcfdb14143f7da35bdcbca7f545 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 25 Nov 2010 17:58:02 +0100 Subject: [PATCH] Maemo: Allow adding an application launcher icon via the GUI. Reviewed-by: kh1 --- .../qt-maemo/maemodeployablelistmodel.cpp | 41 ++++++++++++++++ .../qt-maemo/maemodeployablelistmodel.h | 2 + .../qt-maemo/maemodeploystepwidget.cpp | 48 +++++++++++++++++++ .../qt-maemo/maemodeploystepwidget.h | 1 + .../qt-maemo/maemodeploystepwidget.ui | 7 +++ 5 files changed, 99 insertions(+) diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp index 9803bce27f2..a4a14b966c7 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp @@ -41,9 +41,13 @@ #include #include #include +#include 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 &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); diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h index 20262b41fe5..a90d4883fa8 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h @@ -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; } diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp index a5a286c87aa..37e1aa4117a 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp @@ -11,7 +11,9 @@ #include #include +#include #include +#include 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 diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h index 79101b46df2..516f40a15d7 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h @@ -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; diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui index 2ad404fbdbd..8c00209b6e2 100644 --- a/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui +++ b/src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui @@ -154,6 +154,13 @@ + + + + Add Launcher Icon ... + + +