OSDN Git Service

Maemo: Allow adding an application launcher icon via the GUI.
authorChristian Kandeler <christian.kandeler@nokia.com>
Thu, 25 Nov 2010 16:58:02 +0000 (17:58 +0100)
committerChristian Kandeler <christian.kandeler@nokia.com>
Thu, 25 Nov 2010 16:59:15 +0000 (17:59 +0100)
Reviewed-by: kh1
src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.cpp
src/plugins/qt4projectmanager/qt-maemo/maemodeployablelistmodel.h
src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.cpp
src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.h
src/plugins/qt4projectmanager/qt-maemo/maemodeploystepwidget.ui

index 9803bce..a4a14b9 100644 (file)
 #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);
index 20262b4..a90d488 100644 (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;
     }
index a5a286c..37e1aa4 100644 (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
index 79101b4..516f40a 100644 (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;
index 2ad404f..8c00209 100644 (file)
         </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">
           <enum>Qt::Vertical</enum>