tools/designer/src/lib/shared/qtresourceeditordialog.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Designer of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    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 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    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 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "abstractsettings_p.h"
       
    43 #include "abstractformeditor.h"
       
    44 #include "qtresourceeditordialog_p.h"
       
    45 #include "ui_qtresourceeditordialog.h"
       
    46 #include "qtresourcemodel_p.h"
       
    47 #include "iconloader_p.h"
       
    48 
       
    49 #include <abstractdialoggui_p.h>
       
    50 
       
    51 #include <QtCore/QFileInfo>
       
    52 #include <QtCore/QDir>
       
    53 #include <QtCore/QCoreApplication>
       
    54 #include <QtXml/QDomDocument>
       
    55 #include <QtGui/QMenu>
       
    56 #include <QtGui/QHeaderView>
       
    57 #include <QtGui/QInputDialog>
       
    58 #include <QtGui/QMessageBox>
       
    59 #include <QtGui/QPushButton>
       
    60 #include <QtGui/QStandardItemModel>
       
    61 
       
    62 QT_BEGIN_NAMESPACE
       
    63 
       
    64 static const char *rccRootTag = "RCC";
       
    65 static const char *rccTag = "qresource";
       
    66 static const char *rccFileTag = "file";
       
    67 static const char *rccAliasAttribute = "alias";
       
    68 static const char *rccPrefixAttribute = "prefix";
       
    69 static const char *rccLangAttribute = "lang";
       
    70 static const char *SplitterPosition = "SplitterPosition";
       
    71 static const char *Geometry = "Geometry";
       
    72 static const char *QrcDialogC = "QrcDialog";
       
    73 
       
    74 static QString msgOverwrite(const QString &fname)
       
    75 {
       
    76     return QApplication::translate("QtResourceEditorDialog", "%1 already exists.\nDo you want to replace it?", 0, QApplication::UnicodeUTF8).arg(fname);
       
    77 }
       
    78 
       
    79 static QString msgTagMismatch(const QString &got, const QString &expected)
       
    80 {
       
    81     return QApplication::translate("QtResourceEditorDialog", "The file does not appear to be a resource file; element '%1' was found where '%2' was expected.").arg(got).arg(expected);
       
    82 }
       
    83 
       
    84 namespace {
       
    85 
       
    86 // below 3 data classes should be derived from QSharedData and made implicit shared class
       
    87 struct QtResourceFileData {
       
    88     QString path;
       
    89     QString alias;
       
    90     bool operator==(const QtResourceFileData &other) const {
       
    91         if (path == other.path && alias == other.alias)
       
    92             return true;
       
    93         return false;
       
    94     }
       
    95 };
       
    96 
       
    97 struct QtResourcePrefixData {
       
    98     QString prefix;
       
    99     QString language;
       
   100     QList<QtResourceFileData> resourceFileList;
       
   101     bool operator==(const QtResourcePrefixData &other) const {
       
   102         if (prefix == other.prefix && language == other.language && resourceFileList == other.resourceFileList)
       
   103             return true;
       
   104         return false;
       
   105     }
       
   106 };
       
   107 
       
   108 struct QtQrcFileData {
       
   109     QString qrcPath;
       
   110     QList<QtResourcePrefixData> resourceList;
       
   111     bool operator==(const QtQrcFileData &other) const {
       
   112         if (qrcPath == other.qrcPath && resourceList == other.resourceList)
       
   113             return true;
       
   114         return false;
       
   115     }
       
   116 };
       
   117 
       
   118 bool loadResourceFileData(const QDomElement &fileElem, QtResourceFileData *fileData, QString *errorMessage)
       
   119 {
       
   120     if (!fileData)
       
   121         return false;
       
   122 
       
   123     if (fileElem.tagName() != QLatin1String(rccFileTag)) {
       
   124         *errorMessage = msgTagMismatch(fileElem.tagName(), QLatin1String(rccFileTag));
       
   125         return false;
       
   126     }
       
   127 
       
   128     QtResourceFileData &data = *fileData;
       
   129 
       
   130     data.path = fileElem.text();
       
   131     data.alias = fileElem.attribute(QLatin1String(rccAliasAttribute));
       
   132 
       
   133     return true;
       
   134 }
       
   135 
       
   136 static bool loadResourcePrefixData(const QDomElement &prefixElem, QtResourcePrefixData *prefixData, QString *errorMessage)
       
   137 {
       
   138     if (!prefixData)
       
   139         return false;
       
   140 
       
   141     if (prefixElem.tagName() != QLatin1String(rccTag)) {
       
   142         *errorMessage = msgTagMismatch(prefixElem.tagName(), QLatin1String(rccTag));
       
   143         return false;
       
   144     }
       
   145 
       
   146     QtResourcePrefixData &data = *prefixData;
       
   147 
       
   148     data.prefix = prefixElem.attribute(QLatin1String(rccPrefixAttribute));
       
   149     data.language = prefixElem.attribute(QLatin1String(rccLangAttribute));
       
   150     QDomElement fileElem = prefixElem.firstChildElement();
       
   151     while (!fileElem.isNull()) {
       
   152         QtResourceFileData fileData;
       
   153         if (!loadResourceFileData(fileElem, &fileData, errorMessage))
       
   154             return false;
       
   155         data.resourceFileList.append(fileData);
       
   156         fileElem = fileElem.nextSiblingElement();
       
   157     }
       
   158     return true;
       
   159 }
       
   160 
       
   161 static bool loadQrcFileData(const QDomDocument &doc, const QString &path, QtQrcFileData *qrcFileData, QString *errorMessage)
       
   162 {
       
   163     if (!qrcFileData)
       
   164         return false;
       
   165 
       
   166     QtQrcFileData &data = *qrcFileData;
       
   167 
       
   168     QDomElement docElem = doc.documentElement();
       
   169     if (docElem.tagName() != QLatin1String(rccRootTag)) {
       
   170         *errorMessage = msgTagMismatch(docElem.tagName(), QLatin1String(rccRootTag));
       
   171         return false;
       
   172     }
       
   173 
       
   174     QDomElement prefixElem = docElem.firstChildElement();
       
   175     while (!prefixElem.isNull()) {
       
   176         QtResourcePrefixData prefixData;
       
   177         if (!loadResourcePrefixData(prefixElem, &prefixData, errorMessage))
       
   178             return false;
       
   179         data.resourceList.append(prefixData);
       
   180         prefixElem = prefixElem.nextSiblingElement();
       
   181     }
       
   182 
       
   183     data.qrcPath = path;
       
   184 
       
   185     return true;
       
   186 }
       
   187 
       
   188 QDomElement saveResourceFileData(QDomDocument &doc, const QtResourceFileData &fileData)
       
   189 {
       
   190     QDomElement fileElem = doc.createElement(QLatin1String(rccFileTag));
       
   191     if (!fileData.alias.isEmpty())
       
   192         fileElem.setAttribute(QLatin1String(rccAliasAttribute), fileData.alias);
       
   193 
       
   194     QDomText textElem = doc.createTextNode(fileData.path);
       
   195     fileElem.appendChild(textElem);
       
   196 
       
   197     return fileElem;
       
   198 }
       
   199 
       
   200 QDomElement saveResourcePrefixData(QDomDocument &doc, const QtResourcePrefixData &prefixData)
       
   201 {
       
   202     QDomElement prefixElem = doc.createElement(QLatin1String(rccTag));
       
   203     if (!prefixData.prefix.isEmpty())
       
   204         prefixElem.setAttribute(QLatin1String(rccPrefixAttribute), prefixData.prefix);
       
   205     if (!prefixData.language.isEmpty())
       
   206         prefixElem.setAttribute(QLatin1String(rccLangAttribute), prefixData.language);
       
   207 
       
   208     QListIterator<QtResourceFileData> itFile(prefixData.resourceFileList);
       
   209     while (itFile.hasNext()) {
       
   210         QDomElement fileElem = saveResourceFileData(doc, itFile.next());
       
   211         prefixElem.appendChild(fileElem);
       
   212     }
       
   213 
       
   214     return prefixElem;
       
   215 }
       
   216 
       
   217 QDomDocument saveQrcFileData(const QtQrcFileData &qrcFileData)
       
   218 {
       
   219     QDomDocument doc;
       
   220     QDomElement docElem = doc.createElement(QLatin1String(rccRootTag));
       
   221     QListIterator<QtResourcePrefixData> itPrefix(qrcFileData.resourceList);
       
   222     while (itPrefix.hasNext()) {
       
   223         QDomElement prefixElem = saveResourcePrefixData(doc, itPrefix.next());
       
   224 
       
   225         docElem.appendChild(prefixElem);
       
   226     }
       
   227     doc.appendChild(docElem);
       
   228 
       
   229     return doc;
       
   230 }
       
   231 // --------------- QtResourceFile
       
   232 class QtResourceFile {
       
   233 public:
       
   234     friend class QtQrcManager;
       
   235 
       
   236     QString path() const { return m_path; }
       
   237     QString alias() const { return m_alias; }
       
   238     QString fullPath() const { return m_fullPath; }
       
   239 private:
       
   240     QtResourceFile() {}
       
   241 
       
   242     QString m_path;
       
   243     QString m_alias;
       
   244     QString m_fullPath;
       
   245 };
       
   246 
       
   247 class QtResourcePrefix {
       
   248 public:
       
   249     friend class QtQrcManager;
       
   250 
       
   251     QString prefix() const { return m_prefix; }
       
   252     QString language() const { return m_language; }
       
   253     QList<QtResourceFile *> resourceFiles() const { return m_resourceFiles; }
       
   254 private:
       
   255     QtResourcePrefix() {}
       
   256 
       
   257     QString m_prefix;
       
   258     QString m_language;
       
   259     QList<QtResourceFile *> m_resourceFiles;
       
   260 
       
   261 };
       
   262 // ------------------- QtQrcFile
       
   263 class QtQrcFile {
       
   264 public:
       
   265     friend class QtQrcManager;
       
   266 
       
   267     QString path() const { return m_path; }
       
   268     QString fileName() const { return m_fileName; }
       
   269     QList<QtResourcePrefix *> resourcePrefixList() const { return m_resourcePrefixes; }
       
   270     QtQrcFileData initialState() const { return m_initialState; }
       
   271 
       
   272 private:
       
   273     QtQrcFile() { }
       
   274 
       
   275     void setPath(const QString &path) {
       
   276         m_path = path;
       
   277         QFileInfo fi(path);
       
   278         m_fileName = fi.fileName();
       
   279     }
       
   280 
       
   281     QString m_path;
       
   282     QString m_fileName;
       
   283     QList<QtResourcePrefix *> m_resourcePrefixes;
       
   284     QtQrcFileData m_initialState;
       
   285 };
       
   286 
       
   287 // ------------------ QtQrcManager
       
   288 class QtQrcManager : public QObject
       
   289 {
       
   290     Q_OBJECT
       
   291 public:
       
   292     QtQrcManager(QObject *parent = 0);
       
   293     ~QtQrcManager();
       
   294 
       
   295     QList<QtQrcFile *> qrcFiles() const;
       
   296 
       
   297     // helpers
       
   298     QtQrcFile *qrcFileOf(const QString &path) const;
       
   299     QtQrcFile *qrcFileOf(QtResourcePrefix *resourcePrefix) const;
       
   300     QtQrcFile *qrcFileOf(QtResourceFile *resourceFile) const;
       
   301     QtResourcePrefix *resourcePrefixOf(QtResourceFile *resourceFile) const;
       
   302 
       
   303     QtQrcFile *importQrcFile(const QtQrcFileData &qrcFileData, QtQrcFile *beforeQrcFile = 0);
       
   304     void exportQrcFile(QtQrcFile *qrcFile, QtQrcFileData *qrcFileData) const;
       
   305 
       
   306     QList<QtResourceFile *> resourceFilesOf(const QString &resourceFullPath) const;
       
   307     QIcon icon(const QString &resourceFullPath) const;
       
   308     bool exists(const QString &resourceFullPath) const;
       
   309     bool exists(QtQrcFile *qrcFile) const;
       
   310 
       
   311     QtQrcFile *prevQrcFile(QtQrcFile *qrcFile) const;
       
   312     QtQrcFile *nextQrcFile(QtQrcFile *qrcFile) const;
       
   313     QtResourcePrefix *prevResourcePrefix(QtResourcePrefix *resourcePrefix) const;
       
   314     QtResourcePrefix *nextResourcePrefix(QtResourcePrefix *resourcePrefix) const;
       
   315     QtResourceFile *prevResourceFile(QtResourceFile *resourceFile) const;
       
   316     QtResourceFile *nextResourceFile(QtResourceFile *resourceFile) const;
       
   317 
       
   318     void clear();
       
   319 
       
   320 public slots:
       
   321 
       
   322     QtQrcFile *insertQrcFile(const QString &path, QtQrcFile *beforeQrcFile = 0, bool newFile = false);
       
   323     void moveQrcFile(QtQrcFile *qrcFile, QtQrcFile *beforeQrcFile);
       
   324     void setInitialState(QtQrcFile *qrcFile, const QtQrcFileData &initialState);
       
   325     void removeQrcFile(QtQrcFile *qrcFile);
       
   326 
       
   327     QtResourcePrefix *insertResourcePrefix(QtQrcFile *qrcFile, const QString &prefix,
       
   328                     const QString &language, QtResourcePrefix *beforeResourcePrefix = 0);
       
   329     void moveResourcePrefix(QtResourcePrefix *resourcePrefix, QtResourcePrefix *beforeResourcePrefix); // the same qrc file???
       
   330     void changeResourcePrefix(QtResourcePrefix *resourcePrefix, const QString &newPrefix);
       
   331     void changeResourceLanguage(QtResourcePrefix *resourcePrefix, const QString &newLanguage);
       
   332     void removeResourcePrefix(QtResourcePrefix *resourcePrefix);
       
   333 
       
   334     QtResourceFile *insertResourceFile(QtResourcePrefix *resourcePrefix, const QString &path,
       
   335                     const QString &alias, QtResourceFile *beforeResourceFile = 0);
       
   336     void moveResourceFile(QtResourceFile *resourceFile, QtResourceFile *beforeResourceFile); // the same prefix???
       
   337     void changeResourceAlias(QtResourceFile *resourceFile, const QString &newAlias);
       
   338     void removeResourceFile(QtResourceFile *resourceFile);
       
   339 
       
   340 signals:
       
   341     void qrcFileInserted(QtQrcFile *qrcFile);
       
   342     void qrcFileMoved(QtQrcFile *qrcFile, QtQrcFile *oldBeforeQrcFile);
       
   343     void qrcFileRemoved(QtQrcFile *qrcFile);
       
   344 
       
   345     void resourcePrefixInserted(QtResourcePrefix *resourcePrefix);
       
   346     void resourcePrefixMoved(QtResourcePrefix *resourcePrefix, QtResourcePrefix *oldBeforeResourcePrefix);
       
   347     void resourcePrefixChanged(QtResourcePrefix *resourcePrefix, const QString &oldPrefix);
       
   348     void resourceLanguageChanged(QtResourcePrefix *resourcePrefix, const QString &oldLanguage);
       
   349     void resourcePrefixRemoved(QtResourcePrefix *resourcePrefix);
       
   350 
       
   351     void resourceFileInserted(QtResourceFile *resourceFile);
       
   352     void resourceFileMoved(QtResourceFile *resourceFile, QtResourceFile *oldBeforeResourceFile);
       
   353     void resourceAliasChanged(QtResourceFile *resourceFile, const QString &oldAlias);
       
   354     void resourceFileRemoved(QtResourceFile *resourceFile);
       
   355 private:
       
   356 
       
   357     QList<QtQrcFile *> m_qrcFiles;
       
   358     QMap<QString, QtQrcFile *> m_pathToQrc;
       
   359     QMap<QtQrcFile *, bool> m_qrcFileToExists;
       
   360     QMap<QtResourcePrefix *, QtQrcFile *> m_prefixToQrc;
       
   361     QMap<QtResourceFile *, QtResourcePrefix *> m_fileToPrefix;
       
   362     QMap<QString, QList<QtResourceFile *> > m_fullPathToResourceFiles;
       
   363     QMap<QString, QIcon> m_fullPathToIcon;
       
   364     QMap<QString, bool> m_fullPathToExists;
       
   365 };
       
   366 
       
   367 QtQrcManager::QtQrcManager(QObject *parent)
       
   368     : QObject(parent)
       
   369 {
       
   370 
       
   371 }
       
   372 
       
   373 QtQrcManager::~QtQrcManager()
       
   374 {
       
   375     clear();
       
   376 }
       
   377 
       
   378 QList<QtQrcFile *> QtQrcManager::qrcFiles() const
       
   379 {
       
   380     return m_qrcFiles;
       
   381 }
       
   382 
       
   383 QtQrcFile *QtQrcManager::qrcFileOf(const QString &path) const
       
   384 {
       
   385     return m_pathToQrc.value(path);
       
   386 }
       
   387 
       
   388 QtQrcFile *QtQrcManager::qrcFileOf(QtResourcePrefix *resourcePrefix) const
       
   389 {
       
   390     return m_prefixToQrc.value(resourcePrefix);
       
   391 }
       
   392 
       
   393 QtQrcFile *QtQrcManager::qrcFileOf(QtResourceFile *resourceFile) const
       
   394 {
       
   395     return qrcFileOf(resourcePrefixOf(resourceFile));
       
   396 }
       
   397 
       
   398 QtResourcePrefix *QtQrcManager::resourcePrefixOf(QtResourceFile *resourceFile) const
       
   399 {
       
   400     return m_fileToPrefix.value(resourceFile);
       
   401 }
       
   402 
       
   403 QtQrcFile *QtQrcManager::importQrcFile(const QtQrcFileData &qrcFileData, QtQrcFile *beforeQrcFile)
       
   404 {
       
   405     QtQrcFile *qrcFile = insertQrcFile(qrcFileData.qrcPath, beforeQrcFile);
       
   406     if (!qrcFile)
       
   407         return 0;
       
   408     QListIterator<QtResourcePrefixData> itPrefix(qrcFileData.resourceList);
       
   409     while (itPrefix.hasNext()) {
       
   410         const QtResourcePrefixData &prefixData = itPrefix.next();
       
   411         QtResourcePrefix *resourcePrefix = insertResourcePrefix(qrcFile, prefixData.prefix, prefixData.language, 0);
       
   412         QListIterator<QtResourceFileData> itFile(prefixData.resourceFileList);
       
   413         while (itFile.hasNext()) {
       
   414             const QtResourceFileData &fileData = itFile.next();
       
   415             insertResourceFile(resourcePrefix, fileData.path, fileData.alias, 0);
       
   416         }
       
   417     }
       
   418     setInitialState(qrcFile, qrcFileData);
       
   419     return qrcFile;
       
   420 }
       
   421 
       
   422 void QtQrcManager::exportQrcFile(QtQrcFile *qrcFile, QtQrcFileData *qrcFileData) const
       
   423 {
       
   424     if (!qrcFileData)
       
   425         return;
       
   426 
       
   427     if (!qrcFile)
       
   428         return;
       
   429 
       
   430     QtQrcFileData &data = *qrcFileData;
       
   431 
       
   432     QList<QtResourcePrefixData> resourceList;
       
   433 
       
   434     QList<QtResourcePrefix *> resourcePrefixes = qrcFile->resourcePrefixList();
       
   435     QListIterator<QtResourcePrefix *> itPrefix(resourcePrefixes);
       
   436     while (itPrefix.hasNext()) {
       
   437         QList<QtResourceFileData> resourceFileList;
       
   438 
       
   439         QtResourcePrefix *prefix = itPrefix.next();
       
   440 
       
   441         QList<QtResourceFile *> resourceFiles = prefix->resourceFiles();
       
   442         QListIterator<QtResourceFile *> itFile(resourceFiles);
       
   443         while (itFile.hasNext()) {
       
   444             QtResourceFile *file = itFile.next();
       
   445             QtResourceFileData fileData;
       
   446             fileData.path = file->path();
       
   447             fileData.alias = file->alias();
       
   448             resourceFileList << fileData;
       
   449         }
       
   450         QtResourcePrefixData prefixData;
       
   451         prefixData.prefix = prefix->prefix();
       
   452         prefixData.language = prefix->language();
       
   453         prefixData.resourceFileList = resourceFileList;
       
   454 
       
   455         resourceList << prefixData;
       
   456     }
       
   457     data = QtQrcFileData();
       
   458     data.qrcPath = qrcFile->path();
       
   459     data.resourceList = resourceList;
       
   460 }
       
   461 
       
   462 QList<QtResourceFile *> QtQrcManager::resourceFilesOf(const QString &resourcePath) const
       
   463 {
       
   464     return m_fullPathToResourceFiles.value(resourcePath);
       
   465 }
       
   466 
       
   467 QIcon QtQrcManager::icon(const QString &resourceFullPath) const
       
   468 {
       
   469     return m_fullPathToIcon.value(resourceFullPath);
       
   470 }
       
   471 
       
   472 bool QtQrcManager::exists(const QString &resourceFullPath) const
       
   473 {
       
   474     return m_fullPathToExists.value(resourceFullPath, false);
       
   475 }
       
   476 
       
   477 bool QtQrcManager::exists(QtQrcFile *qrcFile) const
       
   478 {
       
   479     return m_qrcFileToExists.value(qrcFile, false);
       
   480 }
       
   481 
       
   482 QtQrcFile *QtQrcManager::prevQrcFile(QtQrcFile *qrcFile) const
       
   483 {
       
   484     if (!qrcFile)
       
   485         return 0;
       
   486     const int idx = m_qrcFiles.indexOf(qrcFile);
       
   487     if (idx <= 0)
       
   488         return 0;
       
   489     return m_qrcFiles.at(idx - 1);
       
   490 }
       
   491 
       
   492 QtQrcFile *QtQrcManager::nextQrcFile(QtQrcFile *qrcFile) const
       
   493 {
       
   494     if (!qrcFile)
       
   495         return 0;
       
   496     const int idx = m_qrcFiles.indexOf(qrcFile);
       
   497     if (idx < 0 || idx == m_qrcFiles.size() - 1)
       
   498         return 0;
       
   499     return m_qrcFiles.at(idx + 1);
       
   500 }
       
   501 
       
   502 QtResourcePrefix *QtQrcManager::prevResourcePrefix(QtResourcePrefix *resourcePrefix) const
       
   503 {
       
   504     if (!resourcePrefix)
       
   505         return 0;
       
   506     QList<QtResourcePrefix *> prefixes = qrcFileOf(resourcePrefix)->resourcePrefixList();
       
   507     const int idx = prefixes.indexOf(resourcePrefix);
       
   508     if (idx <= 0)
       
   509         return 0;
       
   510     return prefixes.at(idx - 1);
       
   511 }
       
   512 
       
   513 QtResourcePrefix *QtQrcManager::nextResourcePrefix(QtResourcePrefix *resourcePrefix) const
       
   514 {
       
   515     if (!resourcePrefix)
       
   516         return 0;
       
   517     QList<QtResourcePrefix *> prefixes = qrcFileOf(resourcePrefix)->resourcePrefixList();
       
   518     const int idx = prefixes.indexOf(resourcePrefix);
       
   519     if (idx < 0 || idx == prefixes.size() - 1)
       
   520         return 0;
       
   521     return prefixes.at(idx + 1);
       
   522 }
       
   523 
       
   524 QtResourceFile *QtQrcManager::prevResourceFile(QtResourceFile *resourceFile) const
       
   525 {
       
   526     if (!resourceFile)
       
   527         return 0;
       
   528     QList<QtResourceFile *> files = resourcePrefixOf(resourceFile)->resourceFiles();
       
   529     const int idx = files.indexOf(resourceFile);
       
   530     if (idx <= 0)
       
   531         return 0;
       
   532     return files.at(idx - 1);
       
   533 }
       
   534 
       
   535 QtResourceFile *QtQrcManager::nextResourceFile(QtResourceFile *resourceFile) const
       
   536 {
       
   537     if (!resourceFile)
       
   538         return 0;
       
   539     QList<QtResourceFile *> files = resourcePrefixOf(resourceFile)->resourceFiles();
       
   540     const int idx = files.indexOf(resourceFile);
       
   541     if (idx < 0 || idx == files.size() - 1)
       
   542         return 0;
       
   543     return files.at(idx + 1);
       
   544 }
       
   545 
       
   546 void QtQrcManager::clear()
       
   547 {
       
   548     QList<QtQrcFile *> oldQrcFiles = qrcFiles();
       
   549     QListIterator<QtQrcFile *> it(oldQrcFiles);
       
   550     while (it.hasNext())
       
   551         removeQrcFile(it.next());
       
   552 }
       
   553 
       
   554 QtQrcFile *QtQrcManager::insertQrcFile(const QString &path, QtQrcFile *beforeQrcFile, bool newFile)
       
   555 {
       
   556     if (m_pathToQrc.contains(path))
       
   557         return 0;
       
   558 
       
   559     int idx = m_qrcFiles.indexOf(beforeQrcFile);
       
   560     if (idx < 0)
       
   561         idx = m_qrcFiles.size();
       
   562 
       
   563     QtQrcFile *qrcFile = new QtQrcFile();
       
   564     qrcFile->setPath(path);
       
   565 
       
   566     m_qrcFiles.insert(idx, qrcFile);
       
   567     m_pathToQrc[path] = qrcFile;
       
   568 
       
   569     const QFileInfo fi(path);
       
   570     m_qrcFileToExists[qrcFile] = fi.exists() || newFile;
       
   571 
       
   572     emit qrcFileInserted(qrcFile);
       
   573     return qrcFile;
       
   574 }
       
   575 
       
   576 void QtQrcManager::moveQrcFile(QtQrcFile *qrcFile, QtQrcFile *beforeQrcFile)
       
   577 {
       
   578     if (qrcFile == beforeQrcFile)
       
   579         return;
       
   580 
       
   581     const int idx = m_qrcFiles.indexOf(qrcFile);
       
   582     if (idx < 0)
       
   583         return;
       
   584 
       
   585     int beforeIdx = m_qrcFiles.indexOf(beforeQrcFile);
       
   586     if (beforeIdx < 0)
       
   587         beforeIdx = m_qrcFiles.size();
       
   588 
       
   589     if (idx == beforeIdx - 1) // the same position, nothing changes
       
   590         return;
       
   591 
       
   592     QtQrcFile *oldBefore = 0;
       
   593     if (idx < m_qrcFiles.size() - 1)
       
   594         oldBefore = m_qrcFiles.at(idx + 1);
       
   595 
       
   596     m_qrcFiles.removeAt(idx);
       
   597     if (idx < beforeIdx)
       
   598         beforeIdx -= 1;
       
   599 
       
   600     m_qrcFiles.insert(beforeIdx, qrcFile);
       
   601 
       
   602     emit qrcFileMoved(qrcFile, oldBefore);
       
   603 }
       
   604 
       
   605 void QtQrcManager::setInitialState(QtQrcFile *qrcFile, const QtQrcFileData &initialState)
       
   606 {
       
   607     qrcFile->m_initialState = initialState;
       
   608 }
       
   609 
       
   610 void QtQrcManager::removeQrcFile(QtQrcFile *qrcFile)
       
   611 {
       
   612     const int idx = m_qrcFiles.indexOf(qrcFile);
       
   613     if (idx < 0)
       
   614         return;
       
   615 
       
   616     QList<QtResourcePrefix *> resourcePrefixes = qrcFile->resourcePrefixList();
       
   617     QListIterator<QtResourcePrefix *> it(resourcePrefixes);
       
   618     while (it.hasNext())
       
   619         removeResourcePrefix(it.next());
       
   620 
       
   621     emit qrcFileRemoved(qrcFile);
       
   622 
       
   623     m_qrcFiles.removeAt(idx);
       
   624     m_pathToQrc.remove(qrcFile->path());
       
   625     m_qrcFileToExists.remove(qrcFile);
       
   626     delete qrcFile;
       
   627 }
       
   628 
       
   629 QtResourcePrefix *QtQrcManager::insertResourcePrefix(QtQrcFile *qrcFile, const QString &prefix,
       
   630         const QString &language, QtResourcePrefix *beforeResourcePrefix)
       
   631 {
       
   632     if (!qrcFile)
       
   633         return 0;
       
   634 
       
   635     int idx = qrcFile->m_resourcePrefixes.indexOf(beforeResourcePrefix);
       
   636     if (idx < 0)
       
   637         idx = qrcFile->m_resourcePrefixes.size();
       
   638 
       
   639     QtResourcePrefix *resourcePrefix = new QtResourcePrefix();
       
   640     resourcePrefix->m_prefix = prefix;
       
   641     resourcePrefix->m_language = language;
       
   642 
       
   643     qrcFile->m_resourcePrefixes.insert(idx, resourcePrefix);
       
   644     m_prefixToQrc[resourcePrefix] = qrcFile;
       
   645 
       
   646     emit resourcePrefixInserted(resourcePrefix);
       
   647     return resourcePrefix;
       
   648 }
       
   649 
       
   650 void QtQrcManager::moveResourcePrefix(QtResourcePrefix *resourcePrefix, QtResourcePrefix *beforeResourcePrefix)
       
   651 {
       
   652     if (resourcePrefix == beforeResourcePrefix)
       
   653         return;
       
   654 
       
   655     QtQrcFile *qrcFile = qrcFileOf(resourcePrefix);
       
   656     if (!qrcFile)
       
   657         return;
       
   658 
       
   659     if (beforeResourcePrefix && qrcFileOf(beforeResourcePrefix) != qrcFile)
       
   660         return;
       
   661 
       
   662     const int idx = qrcFile->m_resourcePrefixes.indexOf(resourcePrefix);
       
   663 
       
   664     int beforeIdx = qrcFile->m_resourcePrefixes.indexOf(beforeResourcePrefix);
       
   665     if (beforeIdx < 0)
       
   666         beforeIdx = qrcFile->m_resourcePrefixes.size();
       
   667 
       
   668     if (idx == beforeIdx - 1) // the same position, nothing changes
       
   669         return;
       
   670 
       
   671     QtResourcePrefix *oldBefore = 0;
       
   672     if (idx < qrcFile->m_resourcePrefixes.size() - 1)
       
   673         oldBefore = qrcFile->m_resourcePrefixes.at(idx + 1);
       
   674 
       
   675     qrcFile->m_resourcePrefixes.removeAt(idx);
       
   676     if (idx < beforeIdx)
       
   677         beforeIdx -= 1;
       
   678 
       
   679     qrcFile->m_resourcePrefixes.insert(beforeIdx, resourcePrefix);
       
   680 
       
   681     emit resourcePrefixMoved(resourcePrefix, oldBefore);
       
   682 }
       
   683 
       
   684 void QtQrcManager::changeResourcePrefix(QtResourcePrefix *resourcePrefix, const QString &newPrefix)
       
   685 {
       
   686     if (!resourcePrefix)
       
   687         return;
       
   688 
       
   689     const QString oldPrefix = resourcePrefix->m_prefix;
       
   690     if (oldPrefix == newPrefix)
       
   691         return;
       
   692 
       
   693     resourcePrefix->m_prefix = newPrefix;
       
   694 
       
   695     emit resourcePrefixChanged(resourcePrefix, oldPrefix);
       
   696 }
       
   697 
       
   698 void QtQrcManager::changeResourceLanguage(QtResourcePrefix *resourcePrefix, const QString &newLanguage)
       
   699 {
       
   700     if (!resourcePrefix)
       
   701         return;
       
   702 
       
   703     const QString oldLanguage = resourcePrefix->m_language;
       
   704     if (oldLanguage == newLanguage)
       
   705         return;
       
   706 
       
   707     resourcePrefix->m_language = newLanguage;
       
   708 
       
   709     emit resourceLanguageChanged(resourcePrefix, oldLanguage);
       
   710 }
       
   711 
       
   712 void QtQrcManager::removeResourcePrefix(QtResourcePrefix *resourcePrefix)
       
   713 {
       
   714     QtQrcFile *qrcFile = qrcFileOf(resourcePrefix);
       
   715     if (!qrcFile)
       
   716         return;
       
   717 
       
   718     const int idx = qrcFile->m_resourcePrefixes.indexOf(resourcePrefix);
       
   719 
       
   720     QList<QtResourceFile *> resourceFiles = resourcePrefix->resourceFiles();
       
   721     QListIterator<QtResourceFile *> it(resourceFiles);
       
   722     while (it.hasNext())
       
   723         removeResourceFile(it.next());
       
   724 
       
   725     emit resourcePrefixRemoved(resourcePrefix);
       
   726 
       
   727     qrcFile->m_resourcePrefixes.removeAt(idx);
       
   728     m_prefixToQrc.remove(resourcePrefix);
       
   729     delete resourcePrefix;
       
   730 }
       
   731 
       
   732 QtResourceFile *QtQrcManager::insertResourceFile(QtResourcePrefix *resourcePrefix, const QString &path,
       
   733         const QString &alias, QtResourceFile *beforeResourceFile)
       
   734 {
       
   735     if (!resourcePrefix)
       
   736         return 0;
       
   737 
       
   738     int idx = resourcePrefix->m_resourceFiles.indexOf(beforeResourceFile);
       
   739     if (idx < 0)
       
   740         idx = resourcePrefix->m_resourceFiles.size();
       
   741 
       
   742     QtResourceFile *resourceFile = new QtResourceFile();
       
   743     resourceFile->m_path = path;
       
   744     resourceFile->m_alias = alias;
       
   745     const QFileInfo fi(qrcFileOf(resourcePrefix)->path());
       
   746     const QDir dir(fi.absolutePath());
       
   747     const QString fullPath = dir.absoluteFilePath(path);
       
   748     resourceFile->m_fullPath = fullPath;
       
   749 
       
   750     resourcePrefix->m_resourceFiles.insert(idx, resourceFile);
       
   751     m_fileToPrefix[resourceFile] = resourcePrefix;
       
   752     m_fullPathToResourceFiles[fullPath].append(resourceFile);
       
   753     if (!m_fullPathToIcon.contains(fullPath)) {
       
   754         m_fullPathToIcon[fullPath] = QIcon(fullPath);
       
   755         const QFileInfo fullInfo(fullPath);
       
   756         m_fullPathToExists[fullPath] = fullInfo.exists();
       
   757     }
       
   758 
       
   759     emit resourceFileInserted(resourceFile);
       
   760     return resourceFile;
       
   761 }
       
   762 
       
   763 void QtQrcManager::moveResourceFile(QtResourceFile *resourceFile, QtResourceFile *beforeResourceFile)
       
   764 {
       
   765     if (resourceFile == beforeResourceFile)
       
   766         return;
       
   767 
       
   768     QtResourcePrefix *resourcePrefix = resourcePrefixOf(resourceFile);
       
   769     if (!resourcePrefix)
       
   770         return;
       
   771 
       
   772     if (beforeResourceFile && resourcePrefixOf(beforeResourceFile) != resourcePrefix)
       
   773         return;
       
   774 
       
   775     const int idx = resourcePrefix->m_resourceFiles.indexOf(resourceFile);
       
   776 
       
   777     int beforeIdx = resourcePrefix->m_resourceFiles.indexOf(beforeResourceFile);
       
   778     if (beforeIdx < 0)
       
   779         beforeIdx = resourcePrefix->m_resourceFiles.size();
       
   780 
       
   781     if (idx == beforeIdx - 1) // the same position, nothing changes
       
   782         return;
       
   783 
       
   784     QtResourceFile *oldBefore = 0;
       
   785     if (idx < resourcePrefix->m_resourceFiles.size() - 1)
       
   786         oldBefore = resourcePrefix->m_resourceFiles.at(idx + 1);
       
   787 
       
   788     resourcePrefix->m_resourceFiles.removeAt(idx);
       
   789     if (idx < beforeIdx)
       
   790         beforeIdx -= 1;
       
   791 
       
   792     resourcePrefix->m_resourceFiles.insert(beforeIdx, resourceFile);
       
   793 
       
   794     emit resourceFileMoved(resourceFile, oldBefore);
       
   795 }
       
   796 
       
   797 void QtQrcManager::changeResourceAlias(QtResourceFile *resourceFile, const QString &newAlias)
       
   798 {
       
   799     if (!resourceFile)
       
   800         return;
       
   801 
       
   802     const QString oldAlias = resourceFile->m_alias;
       
   803     if (oldAlias == newAlias)
       
   804         return;
       
   805 
       
   806     resourceFile->m_alias = newAlias;
       
   807 
       
   808     emit resourceAliasChanged(resourceFile, oldAlias);
       
   809 }
       
   810 
       
   811 void QtQrcManager::removeResourceFile(QtResourceFile *resourceFile)
       
   812 {
       
   813     QtResourcePrefix *resourcePrefix = resourcePrefixOf(resourceFile);
       
   814     if (!resourcePrefix)
       
   815         return;
       
   816 
       
   817     const int idx = resourcePrefix->m_resourceFiles.indexOf(resourceFile);
       
   818 
       
   819     emit resourceFileRemoved(resourceFile);
       
   820 
       
   821     resourcePrefix->m_resourceFiles.removeAt(idx);
       
   822     m_fileToPrefix.remove(resourceFile);
       
   823     const QString fullPath = resourceFile->fullPath();
       
   824     m_fullPathToResourceFiles[fullPath].removeAll(resourceFile); // optimize me
       
   825     if (m_fullPathToResourceFiles[fullPath].isEmpty()) {
       
   826         m_fullPathToResourceFiles.remove(fullPath);
       
   827         m_fullPathToIcon.remove(fullPath);
       
   828         m_fullPathToExists.remove(fullPath);
       
   829     }
       
   830     delete resourceFile;
       
   831 }
       
   832 
       
   833 
       
   834 
       
   835 }
       
   836 
       
   837 // ----------------- QtResourceEditorDialogPrivate
       
   838 class QtResourceEditorDialogPrivate
       
   839 {
       
   840     QtResourceEditorDialog *q_ptr;
       
   841     Q_DECLARE_PUBLIC(QtResourceEditorDialog)
       
   842 public:
       
   843     QtResourceEditorDialogPrivate();
       
   844 
       
   845     void slotQrcFileInserted(QtQrcFile *qrcFile);
       
   846     void slotQrcFileMoved(QtQrcFile *qrcFile);
       
   847     void slotQrcFileRemoved(QtQrcFile *qrcFile);
       
   848 
       
   849     QStandardItem *insertResourcePrefix(QtResourcePrefix *resourcePrefix);
       
   850 
       
   851     void slotResourcePrefixInserted(QtResourcePrefix *resourcePrefix) { insertResourcePrefix(resourcePrefix); }
       
   852     void slotResourcePrefixMoved(QtResourcePrefix *resourcePrefix);
       
   853     void slotResourcePrefixChanged(QtResourcePrefix *resourcePrefix);
       
   854     void slotResourceLanguageChanged(QtResourcePrefix *resourcePrefix);
       
   855     void slotResourcePrefixRemoved(QtResourcePrefix *resourcePrefix);
       
   856     void slotResourceFileInserted(QtResourceFile *resourceFile);
       
   857     void slotResourceFileMoved(QtResourceFile *resourceFile);
       
   858     void slotResourceAliasChanged(QtResourceFile *resourceFile);
       
   859     void slotResourceFileRemoved(QtResourceFile *resourceFile);
       
   860 
       
   861     void slotCurrentQrcFileChanged(QListWidgetItem *item);
       
   862     void slotCurrentTreeViewItemChanged(const QModelIndex &index);
       
   863     void slotListWidgetContextMenuRequested(const QPoint &pos);
       
   864     void slotTreeViewContextMenuRequested(const QPoint &pos);
       
   865     void slotTreeViewItemChanged(QStandardItem *item);
       
   866 
       
   867     void slotNewQrcFile();
       
   868     void slotImportQrcFile();
       
   869     void slotRemoveQrcFile();
       
   870     void slotMoveUpQrcFile();
       
   871     void slotMoveDownQrcFile();
       
   872 
       
   873     void slotNewPrefix();
       
   874     void slotAddFiles();
       
   875     void slotChangePrefix();
       
   876     void slotChangeLanguage();
       
   877     void slotChangeAlias();
       
   878     void slotClonePrefix();
       
   879     void slotRemove();
       
   880     void slotMoveUp();
       
   881     void slotMoveDown();
       
   882 
       
   883     bool loadQrcFile(const QString &path, QtQrcFileData *qrcFileData, QString *errorMessage);
       
   884     bool loadQrcFile(const QString &path, QtQrcFileData *qrcFileData);
       
   885     bool saveQrcFile(const QtQrcFileData &qrcFileData);
       
   886 
       
   887     QString qrcFileText(QtQrcFile *qrcFile) const;
       
   888 
       
   889     QMessageBox::StandardButton warning(const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok,
       
   890                                         QMessageBox::StandardButton defaultButton = QMessageBox::NoButton) const;
       
   891 
       
   892     QString browseForNewLocation(const QString &resourceFile, const QDir &rootDir) const;
       
   893     QString copyResourceFile(const QString &resourceFile, const QString &destPath) const;
       
   894     QtResourceFile *getCurrentResourceFile() const;
       
   895     QtResourcePrefix *getCurrentResourcePrefix() const;
       
   896     void selectTreeRow(QStandardItem *item);
       
   897     QString getSaveFileNameWithExtension(QWidget *parent,
       
   898             const QString &title, QString dir, const QString &filter, const QString &extension) const;
       
   899     QString qrcStartDirectory() const;
       
   900 
       
   901     Ui::QtResourceEditorDialog m_ui;
       
   902     QDesignerFormEditorInterface *m_core;
       
   903     QtResourceModel *m_resourceModel;
       
   904     QDesignerDialogGuiInterface *m_dlgGui;
       
   905     QtQrcManager *m_qrcManager;
       
   906     QList<QtQrcFileData> m_initialState;
       
   907 
       
   908     QMap<QtQrcFile *, QListWidgetItem *> m_qrcFileToItem;
       
   909     QMap<QListWidgetItem *, QtQrcFile *> m_itemToQrcFile;
       
   910     QMap<QtResourcePrefix *, QStandardItem *> m_resourcePrefixToPrefixItem;
       
   911     QMap<QtResourcePrefix *, QStandardItem *> m_resourcePrefixToLanguageItem;
       
   912     QMap<QStandardItem *, QtResourcePrefix *> m_prefixItemToResourcePrefix;
       
   913     QMap<QStandardItem *, QtResourcePrefix *> m_languageItemToResourcePrefix;
       
   914     QMap<QtResourceFile *, QStandardItem *> m_resourceFileToPathItem;
       
   915     QMap<QtResourceFile *, QStandardItem *> m_resourceFileToAliasItem;
       
   916     QMap<QStandardItem *, QtResourceFile *> m_pathItemToResourceFile;
       
   917     QMap<QStandardItem *, QtResourceFile *> m_aliasItemToResourceFile;
       
   918 
       
   919     bool m_ignoreCurrentChanged;
       
   920     bool m_firstQrcFileDialog;
       
   921     QtQrcFile *m_currentQrcFile;
       
   922 
       
   923     QAction *m_newQrcFileAction;
       
   924     QAction *m_importQrcFileAction;
       
   925     QAction *m_removeQrcFileAction;
       
   926     QAction *m_moveUpQrcFileAction;
       
   927     QAction *m_moveDownQrcFileAction;
       
   928 
       
   929     QAction *m_newPrefixAction;
       
   930     QAction *m_addResourceFileAction;
       
   931     QAction *m_changePrefixAction;
       
   932     QAction *m_changeLanguageAction;
       
   933     QAction *m_changeAliasAction;
       
   934     QAction *m_clonePrefixAction;
       
   935     QAction *m_moveUpAction;
       
   936     QAction *m_moveDownAction;
       
   937     QAction *m_removeAction;
       
   938 
       
   939     QStandardItemModel *m_treeModel;
       
   940     QItemSelectionModel *m_treeSelection;
       
   941 };
       
   942 
       
   943 QtResourceEditorDialogPrivate::QtResourceEditorDialogPrivate() :
       
   944     q_ptr(0),
       
   945     m_core(0),
       
   946     m_resourceModel(0),
       
   947     m_dlgGui(0),
       
   948     m_qrcManager(0),
       
   949     m_ignoreCurrentChanged(false),
       
   950     m_firstQrcFileDialog(true),
       
   951     m_currentQrcFile(0),
       
   952     m_newQrcFileAction(0),
       
   953     m_importQrcFileAction(0),
       
   954     m_removeQrcFileAction(0),
       
   955     m_moveUpQrcFileAction(0),
       
   956     m_moveDownQrcFileAction(0),
       
   957     m_newPrefixAction(0),
       
   958     m_addResourceFileAction(0),
       
   959     m_changePrefixAction(0),
       
   960     m_changeLanguageAction(0),
       
   961     m_changeAliasAction(0),
       
   962     m_clonePrefixAction(0),
       
   963     m_moveUpAction(0),
       
   964     m_moveDownAction(0),
       
   965     m_removeAction(0),
       
   966     m_treeModel(0),
       
   967     m_treeSelection(0)
       
   968 {
       
   969 }
       
   970 
       
   971 QMessageBox::StandardButton QtResourceEditorDialogPrivate::warning(const QString &title, const QString &text, QMessageBox::StandardButtons buttons,
       
   972                                                                    QMessageBox::StandardButton defaultButton) const
       
   973 {
       
   974     return m_dlgGui->message(q_ptr, QDesignerDialogGuiInterface::ResourceEditorMessage, QMessageBox::Warning, title, text, buttons, defaultButton);
       
   975 }
       
   976 
       
   977 QString QtResourceEditorDialogPrivate::qrcFileText(QtQrcFile *qrcFile) const
       
   978 {
       
   979     const QString path = qrcFile->path();
       
   980     const QString fileName = qrcFile->fileName();
       
   981     const QFileInfo fi(path);
       
   982     if (fi.exists() && !fi.isWritable())
       
   983         return QApplication::translate("QtResourceEditorDialog", "%1 [read-only]").arg(fileName);
       
   984     if (!m_qrcManager->exists(qrcFile))
       
   985         return QApplication::translate("QtResourceEditorDialog", "%1 [missing]").arg(fileName);
       
   986     return fileName;
       
   987 }
       
   988 
       
   989 void QtResourceEditorDialogPrivate::slotQrcFileInserted(QtQrcFile *qrcFile)
       
   990 {
       
   991     QListWidgetItem *currentItem = m_ui.qrcFileList->currentItem();
       
   992     int idx = m_ui.qrcFileList->count();
       
   993     QtQrcFile *nextQrcFile = m_qrcManager->nextQrcFile(qrcFile);
       
   994     QListWidgetItem *nextItem = m_qrcFileToItem.value(nextQrcFile);
       
   995     if (nextItem) {
       
   996         const int row = m_ui.qrcFileList->row(nextItem);
       
   997         if (row >= 0)
       
   998             idx = row;
       
   999     }
       
  1000     const QString path = qrcFile->path();
       
  1001     QListWidgetItem *item = new QListWidgetItem(qrcFileText(qrcFile));
       
  1002     item->setToolTip(path);
       
  1003     m_ignoreCurrentChanged = true;
       
  1004     m_ui.qrcFileList->insertItem(idx, item);
       
  1005     m_ui.qrcFileList->setCurrentItem(currentItem);
       
  1006     m_ignoreCurrentChanged = false;
       
  1007     m_qrcFileToItem[qrcFile] = item;
       
  1008     m_itemToQrcFile[item] = qrcFile;
       
  1009     if (!m_qrcManager->exists(qrcFile))
       
  1010         item->setForeground(QBrush(Qt::red));
       
  1011 }
       
  1012 
       
  1013 void QtResourceEditorDialogPrivate::slotQrcFileMoved(QtQrcFile *qrcFile)
       
  1014 {
       
  1015     QListWidgetItem *currentItem = m_ui.qrcFileList->currentItem();
       
  1016     QListWidgetItem *item = m_qrcFileToItem.value(qrcFile);
       
  1017     m_ignoreCurrentChanged = true;
       
  1018     m_ui.qrcFileList->takeItem(m_ui.qrcFileList->row(item));
       
  1019 
       
  1020     int idx = m_ui.qrcFileList->count();
       
  1021     QtQrcFile *nextQrcFile = m_qrcManager->nextQrcFile(qrcFile);
       
  1022     QListWidgetItem *nextItem = m_qrcFileToItem.value(nextQrcFile);
       
  1023     if (nextItem) {
       
  1024         int row = m_ui.qrcFileList->row(nextItem);
       
  1025         if (row >= 0)
       
  1026             idx = row;
       
  1027     }
       
  1028     m_ui.qrcFileList->insertItem(idx, item);
       
  1029     if (currentItem == item)
       
  1030         m_ui.qrcFileList->setCurrentItem(item);
       
  1031     m_ignoreCurrentChanged = false;
       
  1032 }
       
  1033 
       
  1034 void QtResourceEditorDialogPrivate::slotQrcFileRemoved(QtQrcFile *qrcFile)
       
  1035 {
       
  1036     QListWidgetItem *item = m_qrcFileToItem.value(qrcFile);
       
  1037     if (item == m_ui.qrcFileList->currentItem())
       
  1038         m_ui.qrcFileList->setCurrentItem(0); // this should trigger list view signal currentItemChanged(0), and slot should set m_currentQrcFile to 0
       
  1039     m_ignoreCurrentChanged = true;
       
  1040     delete item;
       
  1041     m_ignoreCurrentChanged = false;
       
  1042     m_itemToQrcFile.remove(item);
       
  1043     m_qrcFileToItem.remove(qrcFile);
       
  1044 }
       
  1045 
       
  1046 QStandardItem *QtResourceEditorDialogPrivate::insertResourcePrefix(QtResourcePrefix *resourcePrefix)
       
  1047 {
       
  1048     if (m_qrcManager->qrcFileOf(resourcePrefix) != m_currentQrcFile)
       
  1049         return 0;
       
  1050 
       
  1051     QtResourcePrefix *prevResourcePrefix = m_qrcManager->prevResourcePrefix(resourcePrefix);
       
  1052     QStandardItem *prevItem = m_resourcePrefixToPrefixItem.value(prevResourcePrefix);
       
  1053 
       
  1054     int row = 0;
       
  1055     if (prevItem)
       
  1056         row = m_treeModel->indexFromItem(prevItem).row() + 1;
       
  1057 
       
  1058     QStandardItem *prefixItem = new QStandardItem();
       
  1059     QStandardItem *languageItem = new QStandardItem();
       
  1060     QList<QStandardItem *> items;
       
  1061     items << prefixItem;
       
  1062     items << languageItem;
       
  1063     m_treeModel->insertRow(row, items);
       
  1064     const QModelIndex newIndex = m_treeModel->indexFromItem(prefixItem);
       
  1065     m_ui.resourceTreeView->setExpanded(newIndex, true);
       
  1066     prefixItem->setFlags(prefixItem->flags() | Qt::ItemIsEditable);
       
  1067     languageItem->setFlags(languageItem->flags() | Qt::ItemIsEditable);
       
  1068     m_resourcePrefixToPrefixItem[resourcePrefix] = prefixItem;
       
  1069     m_resourcePrefixToLanguageItem[resourcePrefix] = languageItem;
       
  1070     m_prefixItemToResourcePrefix[prefixItem] = resourcePrefix;
       
  1071     m_languageItemToResourcePrefix[languageItem] = resourcePrefix;
       
  1072     slotResourcePrefixChanged(resourcePrefix);
       
  1073     slotResourceLanguageChanged(resourcePrefix);
       
  1074     return prefixItem;
       
  1075 }
       
  1076 
       
  1077 void QtResourceEditorDialogPrivate::slotResourcePrefixMoved(QtResourcePrefix *resourcePrefix)
       
  1078 {
       
  1079     QStandardItem *prefixItem = m_resourcePrefixToPrefixItem.value(resourcePrefix);
       
  1080     if (!prefixItem)
       
  1081         return;
       
  1082 
       
  1083     QStandardItem *languageItem = m_resourcePrefixToLanguageItem.value(resourcePrefix);
       
  1084     if (!languageItem)
       
  1085         return;
       
  1086 
       
  1087     const QModelIndex index = m_treeModel->indexFromItem(prefixItem);
       
  1088     const bool expanded = m_ui.resourceTreeView->isExpanded(index);
       
  1089     m_ignoreCurrentChanged = true;
       
  1090     const QList<QStandardItem *> items = m_treeModel->takeRow(index.row());
       
  1091 
       
  1092     int row = m_treeModel->rowCount();
       
  1093     QtResourcePrefix *nextResourcePrefix = m_qrcManager->nextResourcePrefix(resourcePrefix);
       
  1094     QStandardItem *nextItem = m_resourcePrefixToPrefixItem.value(nextResourcePrefix);
       
  1095     if (nextItem)
       
  1096         row = m_treeModel->indexFromItem(nextItem).row();
       
  1097     m_treeModel->insertRow(row, items);
       
  1098     m_ignoreCurrentChanged = false;
       
  1099     m_ui.resourceTreeView->setExpanded(m_treeModel->indexFromItem(items.at(0)), expanded);
       
  1100 }
       
  1101 
       
  1102 void QtResourceEditorDialogPrivate::slotResourcePrefixChanged(QtResourcePrefix *resourcePrefix)
       
  1103 {
       
  1104     QStandardItem *item = m_resourcePrefixToPrefixItem.value(resourcePrefix);
       
  1105     if (!item)
       
  1106         return;
       
  1107 
       
  1108     m_ignoreCurrentChanged = true;
       
  1109     QString prefix = resourcePrefix->prefix();
       
  1110     if (prefix.isEmpty())
       
  1111         prefix = QApplication::translate("QtResourceEditorDialog", "<no prefix>", 0, QApplication::UnicodeUTF8);
       
  1112     item->setText(prefix);
       
  1113     item->setToolTip(prefix);
       
  1114     m_ignoreCurrentChanged = false;
       
  1115 }
       
  1116 
       
  1117 void QtResourceEditorDialogPrivate::slotResourceLanguageChanged(QtResourcePrefix *resourcePrefix)
       
  1118 {
       
  1119     QStandardItem *item = m_resourcePrefixToLanguageItem.value(resourcePrefix);
       
  1120     if (!item)
       
  1121         return;
       
  1122 
       
  1123     m_ignoreCurrentChanged = true;
       
  1124     const QString language = resourcePrefix->language();
       
  1125     item->setText(language);
       
  1126     item->setToolTip(language);
       
  1127 
       
  1128     m_ignoreCurrentChanged = false;
       
  1129 }
       
  1130 
       
  1131 void QtResourceEditorDialogPrivate::slotResourcePrefixRemoved(QtResourcePrefix *resourcePrefix)
       
  1132 {
       
  1133     QStandardItem *prefixItem = m_resourcePrefixToPrefixItem.value(resourcePrefix);
       
  1134     if (!prefixItem)
       
  1135         return;
       
  1136 
       
  1137     QStandardItem *languageItem = m_resourcePrefixToLanguageItem.value(resourcePrefix);
       
  1138     if (!languageItem)
       
  1139         return;
       
  1140 
       
  1141     m_ignoreCurrentChanged = true;
       
  1142     m_treeModel->takeRow(m_treeModel->indexFromItem(prefixItem).row());
       
  1143     delete prefixItem;
       
  1144     delete languageItem;
       
  1145     m_ignoreCurrentChanged = false;
       
  1146     m_prefixItemToResourcePrefix.remove(prefixItem);
       
  1147     m_languageItemToResourcePrefix.remove(languageItem);
       
  1148     m_resourcePrefixToPrefixItem.remove(resourcePrefix);
       
  1149     m_resourcePrefixToLanguageItem.remove(resourcePrefix);
       
  1150 }
       
  1151 
       
  1152 void QtResourceEditorDialogPrivate::slotResourceFileInserted(QtResourceFile *resourceFile)
       
  1153 {
       
  1154     QtResourcePrefix *resourcePrefix = m_qrcManager->resourcePrefixOf(resourceFile);
       
  1155     if (m_qrcManager->qrcFileOf(resourcePrefix) != m_currentQrcFile)
       
  1156         return;
       
  1157 
       
  1158     QtResourceFile *prevResourceFile = m_qrcManager->prevResourceFile(resourceFile);
       
  1159     QStandardItem *prevItem = m_resourceFileToPathItem.value(prevResourceFile);
       
  1160 
       
  1161     QStandardItem *pathItem = new QStandardItem(resourceFile->path());
       
  1162     QStandardItem *aliasItem = new QStandardItem();
       
  1163     QStandardItem *parentItem = m_resourcePrefixToPrefixItem.value(resourcePrefix);
       
  1164     QList<QStandardItem *> items;
       
  1165     items << pathItem;
       
  1166     items << aliasItem;
       
  1167 
       
  1168     int row = 0;
       
  1169     if (prevItem)
       
  1170         row = m_treeModel->indexFromItem(prevItem).row() + 1;
       
  1171 
       
  1172     parentItem->insertRow(row, items);
       
  1173 
       
  1174     pathItem->setFlags(pathItem->flags() & ~Qt::ItemIsEditable);
       
  1175     aliasItem->setFlags(aliasItem->flags() | Qt::ItemIsEditable);
       
  1176     m_resourceFileToPathItem[resourceFile] = pathItem;
       
  1177     m_resourceFileToAliasItem[resourceFile] = aliasItem;
       
  1178     m_pathItemToResourceFile[pathItem] = resourceFile;
       
  1179     m_aliasItemToResourceFile[aliasItem] = resourceFile;
       
  1180     pathItem->setToolTip(resourceFile->path());
       
  1181     pathItem->setIcon(m_qrcManager->icon(resourceFile->fullPath()));
       
  1182     if (!m_qrcManager->exists(resourceFile->fullPath())) {
       
  1183         pathItem->setText(QApplication::translate("QtResourceEditorDialog", "%1 [missing]").arg(resourceFile->path()));
       
  1184         QBrush redBrush(Qt::red);
       
  1185         pathItem->setForeground(redBrush);
       
  1186         aliasItem->setForeground(redBrush);
       
  1187     }
       
  1188     slotResourceAliasChanged(resourceFile);
       
  1189 }
       
  1190 
       
  1191 void QtResourceEditorDialogPrivate::slotResourceFileMoved(QtResourceFile *resourceFile)
       
  1192 {
       
  1193     QStandardItem *pathItem = m_resourceFileToPathItem.value(resourceFile);
       
  1194     if (!pathItem)
       
  1195         return;
       
  1196 
       
  1197     QStandardItem *aliasItem = m_resourceFileToAliasItem.value(resourceFile);
       
  1198     if (!aliasItem)
       
  1199         return;
       
  1200 
       
  1201     QStandardItem *parentItem = pathItem->parent();
       
  1202     m_ignoreCurrentChanged = true;
       
  1203     const QList<QStandardItem *> items = parentItem->takeRow(m_treeModel->indexFromItem(pathItem).row());
       
  1204 
       
  1205     int row = parentItem->rowCount();
       
  1206     QtResourceFile *nextResourceFile = m_qrcManager->nextResourceFile(resourceFile);
       
  1207     QStandardItem *nextItem = m_resourceFileToPathItem.value(nextResourceFile);
       
  1208     if (nextItem)
       
  1209         row = m_treeModel->indexFromItem(nextItem).row();
       
  1210     parentItem->insertRow(row, items);
       
  1211     m_ignoreCurrentChanged = false;
       
  1212 }
       
  1213 
       
  1214 void QtResourceEditorDialogPrivate::slotResourceAliasChanged(QtResourceFile *resourceFile)
       
  1215 {
       
  1216     QStandardItem *item = m_resourceFileToAliasItem.value(resourceFile);
       
  1217     if (!item)
       
  1218         return;
       
  1219 
       
  1220     m_ignoreCurrentChanged = true;
       
  1221     const QString alias = resourceFile->alias();
       
  1222     item->setText(alias);
       
  1223     item->setToolTip(alias);
       
  1224 
       
  1225     m_ignoreCurrentChanged = false;
       
  1226 }
       
  1227 
       
  1228 void QtResourceEditorDialogPrivate::slotResourceFileRemoved(QtResourceFile *resourceFile)
       
  1229 {
       
  1230     QStandardItem *pathItem = m_resourceFileToPathItem.value(resourceFile);
       
  1231     if (!pathItem)
       
  1232         return;
       
  1233 
       
  1234     QStandardItem *aliasItem = m_resourceFileToAliasItem.value(resourceFile);
       
  1235     if (!aliasItem)
       
  1236         return;
       
  1237 
       
  1238     QStandardItem *parentItem = pathItem->parent();
       
  1239 
       
  1240     m_ignoreCurrentChanged = true;
       
  1241     parentItem->takeRow(m_treeModel->indexFromItem(pathItem).row());
       
  1242     delete pathItem;
       
  1243     delete aliasItem;
       
  1244     m_ignoreCurrentChanged = false;
       
  1245     m_pathItemToResourceFile.remove(pathItem);
       
  1246     m_aliasItemToResourceFile.remove(aliasItem);
       
  1247     m_resourceFileToPathItem.remove(resourceFile);
       
  1248     m_resourceFileToAliasItem.remove(resourceFile);
       
  1249 }
       
  1250 
       
  1251 
       
  1252 void QtResourceEditorDialogPrivate::slotCurrentQrcFileChanged(QListWidgetItem *item)
       
  1253 {
       
  1254     if (m_ignoreCurrentChanged)
       
  1255         return;
       
  1256 
       
  1257     QtQrcFile *newCurrentQrcFile = m_itemToQrcFile.value(item);
       
  1258 
       
  1259     if (newCurrentQrcFile == m_currentQrcFile)
       
  1260         return;
       
  1261 
       
  1262     if (m_currentQrcFile) {
       
  1263         QMap<QtResourcePrefix *, QStandardItem *> currentPrefixList = m_resourcePrefixToPrefixItem;
       
  1264         QMapIterator<QtResourcePrefix *, QStandardItem *> itPrefix(currentPrefixList);
       
  1265         while (itPrefix.hasNext()) {
       
  1266             QtResourcePrefix *resourcePrefix = itPrefix.next().key();
       
  1267             QList<QtResourceFile *> currentResourceFiles = resourcePrefix->resourceFiles();
       
  1268             QListIterator<QtResourceFile *> itFile(currentResourceFiles);
       
  1269             while (itFile.hasNext())
       
  1270                 slotResourceFileRemoved(itFile.next());
       
  1271             slotResourcePrefixRemoved(resourcePrefix);
       
  1272         }
       
  1273     }
       
  1274 
       
  1275     m_currentQrcFile = newCurrentQrcFile;
       
  1276     slotCurrentTreeViewItemChanged(QModelIndex());
       
  1277     QStandardItem *firstPrefix = 0; // select first prefix
       
  1278     if (m_currentQrcFile) {
       
  1279         QList<QtResourcePrefix *> newPrefixList = m_currentQrcFile->resourcePrefixList();
       
  1280         QListIterator<QtResourcePrefix *> itPrefix(newPrefixList);
       
  1281         while (itPrefix.hasNext()) {
       
  1282             QtResourcePrefix *resourcePrefix = itPrefix.next();
       
  1283             if (QStandardItem *newPrefixItem = insertResourcePrefix(resourcePrefix))
       
  1284                 if (!firstPrefix)
       
  1285                     firstPrefix = newPrefixItem;
       
  1286             QList<QtResourceFile *> newResourceFiles = resourcePrefix->resourceFiles();
       
  1287             QListIterator<QtResourceFile *> itFile(newResourceFiles);
       
  1288             while (itFile.hasNext())
       
  1289                 slotResourceFileInserted(itFile.next());
       
  1290         }
       
  1291     }
       
  1292     m_ui.resourceTreeView->setCurrentIndex(firstPrefix ? m_treeModel->indexFromItem(firstPrefix) : QModelIndex());
       
  1293 
       
  1294     m_removeQrcFileAction->setEnabled(m_currentQrcFile);
       
  1295     m_moveUpQrcFileAction->setEnabled(m_currentQrcFile && m_qrcManager->prevQrcFile(m_currentQrcFile));
       
  1296     m_moveDownQrcFileAction->setEnabled(m_currentQrcFile && m_qrcManager->nextQrcFile(m_currentQrcFile));
       
  1297 }
       
  1298 
       
  1299 void QtResourceEditorDialogPrivate::slotCurrentTreeViewItemChanged(const QModelIndex &index)
       
  1300 {
       
  1301     QStandardItem *item = m_treeModel->itemFromIndex(index);
       
  1302     QtResourceFile *resourceFile = m_pathItemToResourceFile.value(item);
       
  1303     if (!resourceFile)
       
  1304         resourceFile = m_aliasItemToResourceFile.value(item);
       
  1305     QtResourcePrefix *resourcePrefix = m_prefixItemToResourcePrefix.value(item);
       
  1306     if (!resourcePrefix)
       
  1307         resourcePrefix = m_languageItemToResourcePrefix.value(item);
       
  1308 
       
  1309     bool moveUpEnabled = false;
       
  1310     bool moveDownEnabled = false;
       
  1311     bool currentItem = resourceFile || resourcePrefix;
       
  1312 
       
  1313     if (resourceFile) {
       
  1314         if (m_qrcManager->prevResourceFile(resourceFile))
       
  1315             moveUpEnabled = true;
       
  1316         if (m_qrcManager->nextResourceFile(resourceFile))
       
  1317             moveDownEnabled = true;
       
  1318     } else if (resourcePrefix) {
       
  1319         if (m_qrcManager->prevResourcePrefix(resourcePrefix))
       
  1320             moveUpEnabled = true;
       
  1321         if (m_qrcManager->nextResourcePrefix(resourcePrefix))
       
  1322             moveDownEnabled = true;
       
  1323     }
       
  1324 
       
  1325     m_newPrefixAction->setEnabled(m_currentQrcFile);
       
  1326     m_addResourceFileAction->setEnabled(currentItem);
       
  1327     m_changePrefixAction->setEnabled(currentItem);
       
  1328     m_changeLanguageAction->setEnabled(currentItem);
       
  1329     m_changeAliasAction->setEnabled(resourceFile);
       
  1330     m_removeAction->setEnabled(currentItem);
       
  1331     m_moveUpAction->setEnabled(moveUpEnabled);
       
  1332     m_moveDownAction->setEnabled(moveDownEnabled);
       
  1333     m_clonePrefixAction->setEnabled(currentItem);
       
  1334 }
       
  1335 
       
  1336 void QtResourceEditorDialogPrivate::slotListWidgetContextMenuRequested(const QPoint &pos)
       
  1337 {
       
  1338     QMenu menu(q_ptr);
       
  1339     menu.addAction(m_newQrcFileAction);
       
  1340     menu.addAction(m_importQrcFileAction);
       
  1341     menu.addAction(m_removeQrcFileAction);
       
  1342     menu.addSeparator();
       
  1343     menu.addAction(m_moveUpQrcFileAction);
       
  1344     menu.addAction(m_moveDownQrcFileAction);
       
  1345     menu.exec(m_ui.qrcFileList->mapToGlobal(pos));
       
  1346 }
       
  1347 
       
  1348 void QtResourceEditorDialogPrivate::slotTreeViewContextMenuRequested(const QPoint &pos)
       
  1349 {
       
  1350     QMenu menu(q_ptr);
       
  1351     menu.addAction(m_newPrefixAction);
       
  1352     menu.addAction(m_addResourceFileAction);
       
  1353     menu.addAction(m_removeAction);
       
  1354     menu.addSeparator();
       
  1355     menu.addAction(m_changePrefixAction);
       
  1356     menu.addAction(m_changeLanguageAction);
       
  1357     menu.addAction(m_changeAliasAction);
       
  1358     menu.addSeparator();
       
  1359     menu.addAction(m_clonePrefixAction);
       
  1360     menu.addSeparator();
       
  1361     menu.addAction(m_moveUpAction);
       
  1362     menu.addAction(m_moveDownAction);
       
  1363     menu.exec(m_ui.resourceTreeView->mapToGlobal(pos));
       
  1364 }
       
  1365 
       
  1366 void QtResourceEditorDialogPrivate::slotTreeViewItemChanged(QStandardItem *item)
       
  1367 {
       
  1368     if (m_ignoreCurrentChanged)
       
  1369         return;
       
  1370 
       
  1371     const QString newValue = item->text();
       
  1372     QtResourceFile *resourceFile = m_aliasItemToResourceFile.value(item);
       
  1373     if (resourceFile) {
       
  1374         m_qrcManager->changeResourceAlias(resourceFile, newValue);
       
  1375         return;
       
  1376     }
       
  1377 
       
  1378     QtResourcePrefix *resourcePrefix = m_prefixItemToResourcePrefix.value(item);
       
  1379     if (resourcePrefix) {
       
  1380         m_qrcManager->changeResourcePrefix(resourcePrefix, newValue);
       
  1381         return;
       
  1382     }
       
  1383 
       
  1384     resourcePrefix = m_languageItemToResourcePrefix.value(item);
       
  1385     if (resourcePrefix) {
       
  1386         m_qrcManager->changeResourceLanguage(resourcePrefix, newValue);
       
  1387         return;
       
  1388     }
       
  1389 }
       
  1390 
       
  1391 QString QtResourceEditorDialogPrivate::getSaveFileNameWithExtension(QWidget *parent,
       
  1392             const QString &title, QString dir, const QString &filter, const QString &extension) const
       
  1393 {
       
  1394     const QChar dot = QLatin1Char('.');
       
  1395 
       
  1396     QString saveFile;
       
  1397     while (true) {
       
  1398         saveFile = m_dlgGui->getSaveFileName(parent, title, dir, filter, 0, QFileDialog::DontConfirmOverwrite);
       
  1399         if (saveFile.isEmpty())
       
  1400             return saveFile;
       
  1401 
       
  1402         const QFileInfo fInfo(saveFile);
       
  1403         if (fInfo.suffix().isEmpty() && !fInfo.fileName().endsWith(dot)) {
       
  1404             saveFile += dot;
       
  1405             saveFile += extension;
       
  1406         }
       
  1407 
       
  1408         const QFileInfo fi(saveFile);
       
  1409         if (!fi.exists())
       
  1410             break;
       
  1411 
       
  1412         if (warning(title, msgOverwrite(fi.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
       
  1413             break;
       
  1414 
       
  1415         dir = saveFile;
       
  1416     }
       
  1417     return saveFile;
       
  1418 }
       
  1419 
       
  1420 QString QtResourceEditorDialogPrivate::qrcStartDirectory() const
       
  1421 {
       
  1422     if (!m_currentQrcFile)
       
  1423         return QString();
       
  1424     const QDir dir = QFileInfo(m_currentQrcFile->path()).dir();
       
  1425     return dir.exists() ? dir.absolutePath() : QString();
       
  1426 }
       
  1427 
       
  1428 void QtResourceEditorDialogPrivate::slotNewQrcFile()
       
  1429 {
       
  1430     const QString qrcPath = getSaveFileNameWithExtension(q_ptr,
       
  1431                 QApplication::translate("QtResourceEditorDialog", "New Resource File", 0, QApplication::UnicodeUTF8),
       
  1432                 m_firstQrcFileDialog ? qrcStartDirectory() : QString(),
       
  1433                 QApplication::translate("QtResourceEditorDialog", "Resource files (*.qrc)", 0, QApplication::UnicodeUTF8),
       
  1434                 QLatin1String("qrc"));
       
  1435     if (qrcPath.isEmpty())
       
  1436         return;
       
  1437 
       
  1438     m_firstQrcFileDialog = false;
       
  1439     if (QtQrcFile *sameQrcFile = m_qrcManager->qrcFileOf(qrcPath)) {
       
  1440         // QMessageBox ???
       
  1441         QListWidgetItem *item = m_qrcFileToItem.value(sameQrcFile);
       
  1442         m_ui.qrcFileList->setCurrentItem(item);
       
  1443         item->setSelected(true);
       
  1444         return;
       
  1445     }
       
  1446 
       
  1447     QtQrcFile *nextQrcFile = m_qrcManager->nextQrcFile(m_currentQrcFile);
       
  1448 
       
  1449     QtQrcFile *qrcFile = m_qrcManager->insertQrcFile(qrcPath, nextQrcFile, true);
       
  1450     m_ui.qrcFileList->setCurrentItem(m_qrcFileToItem.value(qrcFile));
       
  1451 }
       
  1452 
       
  1453 void QtResourceEditorDialogPrivate::slotImportQrcFile()
       
  1454 {
       
  1455     const QString qrcPath = m_dlgGui->getOpenFileName(q_ptr,
       
  1456                 QApplication::translate("QtResourceEditorDialog", "Import Resource File", 0, QApplication::UnicodeUTF8),
       
  1457                 m_firstQrcFileDialog ? qrcStartDirectory() : QString(),
       
  1458                 QApplication::translate("QtResourceEditorDialog", "Resource files (*.qrc)", 0, QApplication::UnicodeUTF8));
       
  1459     if (qrcPath.isEmpty())
       
  1460         return;
       
  1461     m_firstQrcFileDialog = false;
       
  1462     if (QtQrcFile *sameQrcFile = m_qrcManager->qrcFileOf(qrcPath)) {
       
  1463         // QMessageBox ???
       
  1464         QListWidgetItem *item = m_qrcFileToItem.value(sameQrcFile);
       
  1465         m_ui.qrcFileList->setCurrentItem(item);
       
  1466         item->setSelected(true);
       
  1467         return;
       
  1468     }
       
  1469 
       
  1470     QtQrcFile *nextQrcFile = m_qrcManager->nextQrcFile(m_currentQrcFile);
       
  1471 
       
  1472     QtQrcFileData qrcFileData;
       
  1473     loadQrcFile(qrcPath, &qrcFileData);
       
  1474     QtQrcFile *qrcFile = m_qrcManager->importQrcFile(qrcFileData, nextQrcFile);
       
  1475     m_ui.qrcFileList->setCurrentItem(m_qrcFileToItem.value(qrcFile));
       
  1476 }
       
  1477 
       
  1478 void QtResourceEditorDialogPrivate::slotRemoveQrcFile()
       
  1479 {
       
  1480     if (!m_currentQrcFile)
       
  1481         return;
       
  1482 
       
  1483     QtQrcFile *currentQrcFile = m_qrcManager->nextQrcFile(m_currentQrcFile);
       
  1484     if (!currentQrcFile)
       
  1485         currentQrcFile = m_qrcManager->prevQrcFile(m_currentQrcFile);
       
  1486 
       
  1487     m_qrcManager->removeQrcFile(m_currentQrcFile);
       
  1488     QListWidgetItem *item = m_qrcFileToItem.value(currentQrcFile);
       
  1489     if (item) {
       
  1490         m_ui.qrcFileList->setCurrentItem(item);
       
  1491         item->setSelected(true);
       
  1492     }
       
  1493 }
       
  1494 
       
  1495 void QtResourceEditorDialogPrivate::slotMoveUpQrcFile()
       
  1496 {
       
  1497     if (!m_currentQrcFile)
       
  1498         return;
       
  1499 
       
  1500     QtQrcFile *prevQrcFile = m_qrcManager->prevQrcFile(m_currentQrcFile);
       
  1501     if (!prevQrcFile)
       
  1502         return;
       
  1503 
       
  1504     m_qrcManager->moveQrcFile(m_currentQrcFile, prevQrcFile);
       
  1505 }
       
  1506 
       
  1507 void QtResourceEditorDialogPrivate::slotMoveDownQrcFile()
       
  1508 {
       
  1509     if (!m_currentQrcFile)
       
  1510         return;
       
  1511 
       
  1512     QtQrcFile *nextQrcFile = m_qrcManager->nextQrcFile(m_currentQrcFile);
       
  1513     if (!nextQrcFile)
       
  1514         return;
       
  1515     nextQrcFile = m_qrcManager->nextQrcFile(nextQrcFile);
       
  1516 
       
  1517     m_qrcManager->moveQrcFile(m_currentQrcFile, nextQrcFile);
       
  1518 }
       
  1519 
       
  1520 QtResourceFile *QtResourceEditorDialogPrivate::getCurrentResourceFile() const
       
  1521 {
       
  1522     QStandardItem *currentItem = m_treeModel->itemFromIndex(m_treeSelection->currentIndex());
       
  1523 
       
  1524 
       
  1525     QtResourceFile *currentResourceFile = 0;
       
  1526     if (currentItem) {
       
  1527         currentResourceFile = m_pathItemToResourceFile.value(currentItem);
       
  1528         if (!currentResourceFile)
       
  1529             currentResourceFile = m_aliasItemToResourceFile.value(currentItem);
       
  1530     }
       
  1531     return currentResourceFile;
       
  1532 }
       
  1533 
       
  1534 QtResourcePrefix *QtResourceEditorDialogPrivate::getCurrentResourcePrefix() const
       
  1535 {
       
  1536     QStandardItem *currentItem = m_treeModel->itemFromIndex(m_treeSelection->currentIndex());
       
  1537 
       
  1538     QtResourcePrefix *currentResourcePrefix = 0;
       
  1539     if (currentItem) {
       
  1540         currentResourcePrefix = m_prefixItemToResourcePrefix.value(currentItem);
       
  1541         if (!currentResourcePrefix) {
       
  1542             currentResourcePrefix = m_languageItemToResourcePrefix.value(currentItem);
       
  1543             if (!currentResourcePrefix) {
       
  1544                 QtResourceFile *currentResourceFile = getCurrentResourceFile();
       
  1545                 if (currentResourceFile)
       
  1546                     currentResourcePrefix = m_qrcManager->resourcePrefixOf(currentResourceFile);
       
  1547             }
       
  1548         }
       
  1549     }
       
  1550     return currentResourcePrefix;
       
  1551 }
       
  1552 
       
  1553 void QtResourceEditorDialogPrivate::selectTreeRow(QStandardItem *item)
       
  1554 {
       
  1555     const QModelIndex index = m_treeModel->indexFromItem(item);
       
  1556     m_treeSelection->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
       
  1557     m_treeSelection->setCurrentIndex(index, QItemSelectionModel::Select);
       
  1558 }
       
  1559 
       
  1560 void QtResourceEditorDialogPrivate::slotNewPrefix()
       
  1561 {
       
  1562     if (!m_currentQrcFile)
       
  1563         return;
       
  1564 
       
  1565     QtResourcePrefix *currentResourcePrefix = getCurrentResourcePrefix();
       
  1566     QtResourcePrefix *nextResourcePrefix = m_qrcManager->nextResourcePrefix(currentResourcePrefix);
       
  1567     QtResourcePrefix *newResourcePrefix = m_qrcManager->insertResourcePrefix(m_currentQrcFile,
       
  1568                 QApplication::translate("QtResourceEditorDialog", "newPrefix", 0, QApplication::UnicodeUTF8),
       
  1569                 QString(), nextResourcePrefix);
       
  1570     if (!newResourcePrefix)
       
  1571         return;
       
  1572 
       
  1573     QStandardItem *newItem = m_resourcePrefixToPrefixItem.value(newResourcePrefix);
       
  1574     if (!newItem)
       
  1575         return;
       
  1576 
       
  1577     const QModelIndex index = m_treeModel->indexFromItem(newItem);
       
  1578     selectTreeRow(newItem);
       
  1579     m_ui.resourceTreeView->edit(index);
       
  1580 }
       
  1581 
       
  1582 static inline QString outOfPathWarning(const QString &fname)
       
  1583 {
       
  1584     return QApplication::translate("QtResourceEditorDialog",
       
  1585     "<p><b>Warning:</b> The file</p>"
       
  1586     "<p>%1</p>"
       
  1587     "<p>is outside of the current resource file's parent directory.</p>").arg(fname);
       
  1588 }
       
  1589 
       
  1590 static inline QString outOfPathWarningInfo()
       
  1591 {
       
  1592     return QApplication::translate("QtResourceEditorDialog",
       
  1593     "<p>To resolve the issue, press:</p>"
       
  1594     "<table>"
       
  1595     "<tr><th align=\"left\">Copy</th><td>to copy the file to the resource file's parent directory.</td></tr>"
       
  1596     "<tr><th align=\"left\">Copy As...</th><td>to copy the file into a subdirectory of the resource file's parent directory.</td></tr>"
       
  1597     "<tr><th align=\"left\">Keep</th><td>to use its current location.</td></tr></table>");
       
  1598 }
       
  1599 
       
  1600 void QtResourceEditorDialogPrivate::slotAddFiles()
       
  1601 {
       
  1602     if (!m_currentQrcFile)
       
  1603         return;
       
  1604 
       
  1605     QtResourcePrefix *currentResourcePrefix = getCurrentResourcePrefix();
       
  1606     QtResourceFile *currentResourceFile = getCurrentResourceFile();
       
  1607     if (!currentResourcePrefix)
       
  1608         return;
       
  1609 
       
  1610     QString initialPath = m_currentQrcFile->path();
       
  1611     if (currentResourceFile) {
       
  1612         QFileInfo fi(currentResourceFile->fullPath());
       
  1613         initialPath = fi.absolutePath();
       
  1614     }
       
  1615 
       
  1616     const QStringList resourcePaths = m_dlgGui->getOpenImageFileNames(q_ptr,
       
  1617                 QApplication::translate("QtResourceEditorDialog", "Add Files", 0, QApplication::UnicodeUTF8),
       
  1618                 initialPath);
       
  1619     if (resourcePaths.isEmpty())
       
  1620         return;
       
  1621 
       
  1622     QtResourceFile *nextResourceFile = m_qrcManager->nextResourceFile(currentResourceFile);
       
  1623     if (!currentResourceFile) {
       
  1624         QList<QtResourceFile *> resourceFiles = currentResourcePrefix->resourceFiles();
       
  1625         if (resourceFiles.count() > 0)
       
  1626             nextResourceFile = resourceFiles.first();
       
  1627     }
       
  1628 
       
  1629     const QFileInfo fi(m_currentQrcFile->path());
       
  1630     const QString destDir = fi.absolutePath();
       
  1631     const QDir dir(fi.absolutePath());
       
  1632     QStringListIterator itResourcePath(resourcePaths);
       
  1633     while (itResourcePath.hasNext()) {
       
  1634         QString resourcePath = itResourcePath.next();
       
  1635         QString relativePath = dir.relativeFilePath(resourcePath);
       
  1636         if (relativePath.startsWith(QLatin1String(".."))) {
       
  1637             QMessageBox msgBox(QMessageBox::Warning,
       
  1638                     QApplication::translate("QtResourceEditorDialog", "Incorrect Path", 0, QApplication::UnicodeUTF8),
       
  1639                     outOfPathWarning(relativePath), QMessageBox::Cancel);
       
  1640             msgBox.setInformativeText(outOfPathWarningInfo());
       
  1641             QPushButton *copyButton = msgBox.addButton(QApplication::translate("QtResourceEditorDialog",
       
  1642                         "Copy", 0, QApplication::UnicodeUTF8), QMessageBox::ActionRole);
       
  1643             QPushButton *copyAsButton = msgBox.addButton(QApplication::translate("QtResourceEditorDialog",
       
  1644                         "Copy As...", 0, QApplication::UnicodeUTF8), QMessageBox::ActionRole);
       
  1645             QPushButton *keepButton = msgBox.addButton(QApplication::translate("QtResourceEditorDialog",
       
  1646                         "Keep", 0, QApplication::UnicodeUTF8), QMessageBox::ActionRole);
       
  1647             QPushButton *skipButton = msgBox.addButton(QApplication::translate("QtResourceEditorDialog",
       
  1648                         "Skip", 0, QApplication::UnicodeUTF8), QMessageBox::ActionRole);
       
  1649             msgBox.setEscapeButton(QMessageBox::Cancel);
       
  1650             msgBox.setDefaultButton(copyButton);
       
  1651             msgBox.exec();
       
  1652             QString destPath;
       
  1653             if (msgBox.clickedButton() == keepButton) {
       
  1654                 // nothing
       
  1655             } else if (msgBox.clickedButton() == copyButton) {
       
  1656                 QFileInfo resInfo(resourcePath);
       
  1657                 QDir dd(destDir);
       
  1658                 destPath = dd.absoluteFilePath(resInfo.fileName());
       
  1659                 if (dd.exists(resInfo.fileName())) {
       
  1660                     if (warning(QApplication::translate("QtResourceEditorDialog", "Copy", 0, QApplication::UnicodeUTF8),
       
  1661                                 msgOverwrite(resInfo.fileName()),
       
  1662                                 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel) != QMessageBox::Yes)
       
  1663                         continue;
       
  1664                 }
       
  1665                 resourcePath = copyResourceFile(resourcePath, destPath); // returns empty string in case copy failed or was canceled
       
  1666             } else if (msgBox.clickedButton() == copyAsButton) {
       
  1667                 destPath = browseForNewLocation(resourcePath, dir); // returns empty string in case browsing was canceled
       
  1668                 if (destPath.isEmpty())
       
  1669                     continue;
       
  1670                 resourcePath = copyResourceFile(resourcePath, destPath); // returns empty string in case copy failed or was canceled
       
  1671             } else if (msgBox.clickedButton() == skipButton) { // skipped
       
  1672                 continue;
       
  1673             } else { // canceled
       
  1674                 return;
       
  1675             }
       
  1676             if (resourcePath.isEmpty())
       
  1677                 continue;
       
  1678         }
       
  1679         relativePath = dir.relativeFilePath(resourcePath);
       
  1680         QtResourceFile *newResourceFile = m_qrcManager->insertResourceFile(currentResourcePrefix, relativePath, QString(), nextResourceFile);
       
  1681 
       
  1682         QStandardItem *newItem = m_resourceFileToPathItem.value(newResourceFile);
       
  1683         if (newItem)
       
  1684             selectTreeRow(newItem);
       
  1685     }
       
  1686 }
       
  1687 
       
  1688 void QtResourceEditorDialogPrivate::slotChangePrefix()
       
  1689 {
       
  1690     QtResourcePrefix *currentResourcePrefix = getCurrentResourcePrefix();
       
  1691     if (!currentResourcePrefix)
       
  1692         return;
       
  1693 
       
  1694     QStandardItem *item = m_resourcePrefixToPrefixItem.value(currentResourcePrefix);
       
  1695     QModelIndex index = m_treeModel->indexFromItem(item);
       
  1696     selectTreeRow(item);
       
  1697     m_ui.resourceTreeView->scrollTo(index);
       
  1698     m_ui.resourceTreeView->edit(index);
       
  1699 }
       
  1700 
       
  1701 void QtResourceEditorDialogPrivate::slotChangeLanguage()
       
  1702 {
       
  1703     QtResourcePrefix *currentResourcePrefix = getCurrentResourcePrefix();
       
  1704     if (!currentResourcePrefix)
       
  1705         return;
       
  1706 
       
  1707     QStandardItem *item = m_resourcePrefixToLanguageItem.value(currentResourcePrefix);
       
  1708     QModelIndex index = m_treeModel->indexFromItem(item);
       
  1709     selectTreeRow(item);
       
  1710     m_ui.resourceTreeView->scrollTo(index);
       
  1711     m_ui.resourceTreeView->edit(index);
       
  1712 }
       
  1713 
       
  1714 void QtResourceEditorDialogPrivate::slotChangeAlias()
       
  1715 {
       
  1716     QtResourceFile *currentResourceFile = getCurrentResourceFile();
       
  1717     if (!currentResourceFile)
       
  1718         return;
       
  1719 
       
  1720     QStandardItem *item = m_resourceFileToAliasItem.value(currentResourceFile);
       
  1721     QModelIndex index = m_treeModel->indexFromItem(item);
       
  1722     selectTreeRow(item);
       
  1723     m_ui.resourceTreeView->scrollTo(index);
       
  1724     m_ui.resourceTreeView->edit(index);
       
  1725 }
       
  1726 
       
  1727 void QtResourceEditorDialogPrivate::slotClonePrefix()
       
  1728 {
       
  1729     QtResourcePrefix *currentResourcePrefix = getCurrentResourcePrefix();
       
  1730     if (!currentResourcePrefix)
       
  1731         return;
       
  1732 
       
  1733     bool ok;
       
  1734     QString suffix = QInputDialog::getText(q_ptr, QApplication::translate("QtResourceEditorDialog", "Clone Prefix", 0, QApplication::UnicodeUTF8),
       
  1735             QApplication::translate("QtResourceEditorDialog", "Enter the suffix which you want to add to the names of the cloned files.\n"
       
  1736                 "This could for example be a language extension like \"_de\".", 0, QApplication::UnicodeUTF8),
       
  1737             QLineEdit::Normal, QString(), &ok);
       
  1738     if (!ok)
       
  1739         return;
       
  1740 
       
  1741     QtResourcePrefix *newResourcePrefix = m_qrcManager->insertResourcePrefix(m_currentQrcFile, currentResourcePrefix->prefix(),
       
  1742                                     currentResourcePrefix->language(), m_qrcManager->nextResourcePrefix(currentResourcePrefix));
       
  1743     if (newResourcePrefix) {
       
  1744         QList<QtResourceFile *> files = currentResourcePrefix->resourceFiles();
       
  1745         QListIterator<QtResourceFile *> itFile(files);
       
  1746         while (itFile.hasNext()) {
       
  1747             QtResourceFile *resourceFile = itFile.next();
       
  1748             QString path = resourceFile->path();
       
  1749             QFileInfo fi(path);
       
  1750             QDir dir(fi.dir());
       
  1751             QString oldSuffix = fi.completeSuffix();
       
  1752             if (!oldSuffix.isEmpty())
       
  1753                 oldSuffix = QLatin1Char('.') + oldSuffix;
       
  1754             const QString newBaseName = fi.baseName() + suffix + oldSuffix;
       
  1755             const QString newPath = QDir::cleanPath(dir.filePath(newBaseName));
       
  1756             m_qrcManager->insertResourceFile(newResourcePrefix, newPath,
       
  1757                     resourceFile->alias());
       
  1758         }
       
  1759     }
       
  1760 }
       
  1761 
       
  1762 void QtResourceEditorDialogPrivate::slotRemove()
       
  1763 {
       
  1764     QStandardItem *item = m_treeModel->itemFromIndex(m_treeSelection->currentIndex());
       
  1765     if (!item)
       
  1766         return;
       
  1767 
       
  1768     QtResourceFile *resourceFile = m_pathItemToResourceFile.value(item);
       
  1769     if (!resourceFile)
       
  1770         resourceFile = m_aliasItemToResourceFile.value(item);
       
  1771     QtResourcePrefix *resourcePrefix = m_prefixItemToResourcePrefix.value(item);
       
  1772     if (!resourcePrefix)
       
  1773         resourcePrefix = m_languageItemToResourcePrefix.value(item);
       
  1774 
       
  1775     QStandardItem *newCurrentItem = 0;
       
  1776 
       
  1777     if (resourceFile) {
       
  1778         QtResourceFile *nextFile = m_qrcManager->nextResourceFile(resourceFile);
       
  1779         if (!nextFile)
       
  1780             nextFile = m_qrcManager->prevResourceFile(resourceFile);
       
  1781         newCurrentItem = m_resourceFileToPathItem.value(nextFile);
       
  1782         if (!newCurrentItem)
       
  1783             newCurrentItem = m_resourcePrefixToPrefixItem.value(m_qrcManager->resourcePrefixOf(resourceFile));
       
  1784     }
       
  1785     if (!newCurrentItem) {
       
  1786         QtResourcePrefix *nextPrefix = m_qrcManager->nextResourcePrefix(resourcePrefix);
       
  1787         if (!nextPrefix)
       
  1788             nextPrefix = m_qrcManager->prevResourcePrefix(resourcePrefix);
       
  1789         newCurrentItem = m_resourcePrefixToPrefixItem.value(nextPrefix);
       
  1790     }
       
  1791 
       
  1792     selectTreeRow(newCurrentItem);
       
  1793 
       
  1794     if (resourcePrefix)
       
  1795         m_qrcManager->removeResourcePrefix(resourcePrefix);
       
  1796     else if (resourceFile)
       
  1797         m_qrcManager->removeResourceFile(resourceFile);
       
  1798 }
       
  1799 
       
  1800 void QtResourceEditorDialogPrivate::slotMoveUp()
       
  1801 {
       
  1802     if (QtResourceFile *resourceFile = getCurrentResourceFile()) {
       
  1803         QtResourceFile *prevFile = m_qrcManager->prevResourceFile(resourceFile);
       
  1804 
       
  1805         if (!prevFile)
       
  1806             return;
       
  1807 
       
  1808         m_qrcManager->moveResourceFile(resourceFile, prevFile);
       
  1809         selectTreeRow(m_resourceFileToPathItem.value(resourceFile));
       
  1810     } else if (QtResourcePrefix *resourcePrefix = getCurrentResourcePrefix()) {
       
  1811         QtResourcePrefix *prevPrefix = m_qrcManager->prevResourcePrefix(resourcePrefix);
       
  1812 
       
  1813         if (!prevPrefix)
       
  1814             return;
       
  1815 
       
  1816         m_qrcManager->moveResourcePrefix(resourcePrefix, prevPrefix);
       
  1817         selectTreeRow(m_resourcePrefixToPrefixItem.value(resourcePrefix));
       
  1818     }
       
  1819 }
       
  1820 
       
  1821 void QtResourceEditorDialogPrivate::slotMoveDown()
       
  1822 {
       
  1823     if (QtResourceFile *resourceFile = getCurrentResourceFile()) {
       
  1824         QtResourceFile *nextFile = m_qrcManager->nextResourceFile(resourceFile);
       
  1825 
       
  1826         if (!nextFile)
       
  1827             return;
       
  1828 
       
  1829         m_qrcManager->moveResourceFile(resourceFile, m_qrcManager->nextResourceFile(nextFile));
       
  1830         selectTreeRow(m_resourceFileToPathItem.value(resourceFile));
       
  1831     } else if (QtResourcePrefix *resourcePrefix = getCurrentResourcePrefix()) {
       
  1832         QtResourcePrefix *nextPrefix = m_qrcManager->nextResourcePrefix(resourcePrefix);
       
  1833 
       
  1834         if (!nextPrefix)
       
  1835             return;
       
  1836 
       
  1837         m_qrcManager->moveResourcePrefix(resourcePrefix, m_qrcManager->nextResourcePrefix(nextPrefix));
       
  1838         selectTreeRow(m_resourcePrefixToPrefixItem.value(resourcePrefix));
       
  1839     }
       
  1840 }
       
  1841 
       
  1842 QString QtResourceEditorDialogPrivate::browseForNewLocation(const QString &resourceFile, const QDir &rootDir) const
       
  1843 {
       
  1844     QFileInfo fi(resourceFile);
       
  1845     const QString initialPath = rootDir.absoluteFilePath(fi.fileName());
       
  1846     while (1) {
       
  1847         QString newPath = m_dlgGui->getSaveFileName(q_ptr,
       
  1848                     QApplication::translate("QtResourceEditorDialog", "Copy As", 0, QApplication::UnicodeUTF8),
       
  1849                     initialPath);
       
  1850         QString relativePath = rootDir.relativeFilePath(newPath);
       
  1851         if (relativePath.startsWith(QLatin1String(".."))) {
       
  1852             if (warning(QApplication::translate("QtResourceEditorDialog", "Copy As", 0, QApplication::UnicodeUTF8),
       
  1853                         QApplication::translate("QtResourceEditorDialog", "<p>The selected file:</p>"
       
  1854                                         "<p>%1</p><p>is outside of the current resource file's directory:</p><p>%2</p>"
       
  1855                                         "<p>Please select another path within this directory.<p>", 0,
       
  1856                                         QApplication::UnicodeUTF8).arg(relativePath).arg(rootDir.absolutePath()),
       
  1857                         QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok) != QMessageBox::Ok)
       
  1858                 return QString();
       
  1859         } else {
       
  1860             return newPath;
       
  1861         }
       
  1862     }
       
  1863 
       
  1864     return QString();
       
  1865 }
       
  1866 
       
  1867 QString QtResourceEditorDialogPrivate::copyResourceFile(const QString &resourceFile, const QString &destPath) const
       
  1868 {
       
  1869     QFileInfo fi(destPath);
       
  1870     if (fi.exists()) {
       
  1871         while (fi.exists() && !QFile::remove(destPath)) {
       
  1872             if (warning(QApplication::translate("QtResourceEditorDialog", "Copy", 0, QApplication::UnicodeUTF8),
       
  1873                         QApplication::translate("QtResourceEditorDialog", "Could not overwrite %1.", 0, QApplication::UnicodeUTF8).arg(fi.fileName()),
       
  1874                         QMessageBox::Retry | QMessageBox::Cancel, QMessageBox::Cancel) != QMessageBox::Retry)
       
  1875                 return QString();
       
  1876         }
       
  1877     }
       
  1878     while (!QFile::copy(resourceFile, destPath)) {
       
  1879         if (warning(QApplication::translate("QtResourceEditorDialog", "Copy", 0, QApplication::UnicodeUTF8),
       
  1880                     QApplication::translate("QtResourceEditorDialog", "Could not copy\n%1\nto\n%2",
       
  1881                         0, QApplication::UnicodeUTF8).arg(resourceFile).arg(destPath),
       
  1882                     QMessageBox::Retry | QMessageBox::Cancel, QMessageBox::Cancel) != QMessageBox::Retry)
       
  1883             return QString();
       
  1884     }
       
  1885     return destPath;
       
  1886 }
       
  1887 bool QtResourceEditorDialogPrivate::loadQrcFile(const QString &path, QtQrcFileData *qrcFileData)
       
  1888 {
       
  1889     QString errorMessage;
       
  1890     const bool rc = loadQrcFile(path, qrcFileData, &errorMessage);
       
  1891 //    if (!rc)
       
  1892 //        warning(QApplication::translate("QtResourceEditorDialog", "Resource File Load Error"), errorMessage);
       
  1893     return rc;
       
  1894 }
       
  1895 bool QtResourceEditorDialogPrivate::loadQrcFile(const QString &path, QtQrcFileData *qrcFileData, QString *errorMessage)
       
  1896 {
       
  1897     if (!qrcFileData)
       
  1898         return false;
       
  1899 
       
  1900     qrcFileData->qrcPath = path;
       
  1901 
       
  1902     QFile file(path);
       
  1903     if (!file.open(QIODevice::ReadOnly)) {
       
  1904         // there is sufficient hint while loading a form and after opening the editor (qrc marked marked with red and with [missing] text)
       
  1905         //*errorMessage =  QApplication::translate("QtResourceEditorDialog", "Unable to open %1 for reading: %2").arg(path).arg(file.errorString());
       
  1906         return false;
       
  1907     }
       
  1908 
       
  1909     QByteArray dataArray = file.readAll();
       
  1910     file.close();
       
  1911 
       
  1912     QDomDocument doc;
       
  1913     int errLine, errCol;
       
  1914     if (!doc.setContent(dataArray, errorMessage, &errLine, &errCol))  {
       
  1915         *errorMessage = QCoreApplication::translate("QtResourceEditorDialog", "A parse error occurred at line %1, column %2 of %3:\n%4").arg(errLine).arg(errCol).arg(path).arg(*errorMessage);
       
  1916         return false;
       
  1917     }
       
  1918 
       
  1919     return loadQrcFileData(doc, path, qrcFileData, errorMessage);
       
  1920 }
       
  1921 
       
  1922 bool QtResourceEditorDialogPrivate::saveQrcFile(const QtQrcFileData &qrcFileData)
       
  1923 {
       
  1924     QFile file(qrcFileData.qrcPath);
       
  1925     while (!file.open(QIODevice::WriteOnly)) {
       
  1926         QMessageBox msgBox(QMessageBox::Warning,
       
  1927                 QApplication::translate("QtResourceEditorDialog", "Save Resource File", 0, QApplication::UnicodeUTF8),
       
  1928                 QApplication::translate("QtResourceEditorDialog", "Could not write %1: %2", 0, QApplication::UnicodeUTF8).arg(qrcFileData.qrcPath).arg(file.errorString()),
       
  1929                 QMessageBox::Cancel|QMessageBox::Ignore|QMessageBox::Retry);
       
  1930         msgBox.setEscapeButton(QMessageBox::Cancel);
       
  1931         msgBox.setDefaultButton(QMessageBox::Ignore);
       
  1932         switch (msgBox.exec()) {
       
  1933         case QMessageBox::Retry:
       
  1934             break; // nothing
       
  1935         case QMessageBox::Ignore:
       
  1936             return true;
       
  1937         default:
       
  1938             return false;
       
  1939         }
       
  1940     }
       
  1941 
       
  1942     QDomDocument doc = saveQrcFileData(qrcFileData);
       
  1943 
       
  1944     QByteArray dataArray = doc.toByteArray(2);
       
  1945     file.write(dataArray);
       
  1946 
       
  1947     file.close();
       
  1948     return true;
       
  1949 }
       
  1950 
       
  1951 QtResourceEditorDialog::QtResourceEditorDialog(QDesignerFormEditorInterface *core, QDesignerDialogGuiInterface *dlgGui, QWidget *parent)
       
  1952     : QDialog(parent), d_ptr(new QtResourceEditorDialogPrivate())
       
  1953 {
       
  1954     d_ptr->q_ptr = this;
       
  1955     d_ptr->m_ui.setupUi(this);
       
  1956     d_ptr->m_qrcManager = new QtQrcManager(this);
       
  1957     d_ptr->m_dlgGui = dlgGui;
       
  1958     d_ptr->m_core = core;
       
  1959 
       
  1960     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
       
  1961     setWindowTitle(tr("Edit Resources"));
       
  1962 
       
  1963     connect(d_ptr->m_qrcManager, SIGNAL(qrcFileInserted(QtQrcFile *)),
       
  1964                 this, SLOT(slotQrcFileInserted(QtQrcFile *)));
       
  1965     connect(d_ptr->m_qrcManager, SIGNAL(qrcFileMoved(QtQrcFile *, QtQrcFile *)),
       
  1966                 this, SLOT(slotQrcFileMoved(QtQrcFile *)));
       
  1967     connect(d_ptr->m_qrcManager, SIGNAL(qrcFileRemoved(QtQrcFile *)),
       
  1968                 this, SLOT(slotQrcFileRemoved(QtQrcFile *)));
       
  1969     connect(d_ptr->m_qrcManager, SIGNAL(resourcePrefixInserted(QtResourcePrefix *)),
       
  1970                 this, SLOT(slotResourcePrefixInserted(QtResourcePrefix *)));
       
  1971     connect(d_ptr->m_qrcManager, SIGNAL(resourcePrefixMoved(QtResourcePrefix *, QtResourcePrefix *)),
       
  1972                 this, SLOT(slotResourcePrefixMoved(QtResourcePrefix *)));
       
  1973     connect(d_ptr->m_qrcManager, SIGNAL(resourcePrefixChanged(QtResourcePrefix *, const QString &)),
       
  1974                 this, SLOT(slotResourcePrefixChanged(QtResourcePrefix *)));
       
  1975     connect(d_ptr->m_qrcManager, SIGNAL(resourceLanguageChanged(QtResourcePrefix *, const QString &)),
       
  1976                 this, SLOT(slotResourceLanguageChanged(QtResourcePrefix *)));
       
  1977     connect(d_ptr->m_qrcManager, SIGNAL(resourcePrefixRemoved(QtResourcePrefix *)),
       
  1978                 this, SLOT(slotResourcePrefixRemoved(QtResourcePrefix *)));
       
  1979     connect(d_ptr->m_qrcManager, SIGNAL(resourceFileInserted(QtResourceFile *)),
       
  1980                 this, SLOT(slotResourceFileInserted(QtResourceFile *)));
       
  1981     connect(d_ptr->m_qrcManager, SIGNAL(resourceFileMoved(QtResourceFile *, QtResourceFile *)),
       
  1982                 this, SLOT(slotResourceFileMoved(QtResourceFile *)));
       
  1983     connect(d_ptr->m_qrcManager, SIGNAL(resourceAliasChanged(QtResourceFile *, const QString &)),
       
  1984                 this, SLOT(slotResourceAliasChanged(QtResourceFile *)));
       
  1985     connect(d_ptr->m_qrcManager, SIGNAL(resourceFileRemoved(QtResourceFile *)),
       
  1986                 this, SLOT(slotResourceFileRemoved(QtResourceFile *)));
       
  1987 
       
  1988     QIcon upIcon = qdesigner_internal::createIconSet(QString::fromUtf8("up.png"));
       
  1989     QIcon downIcon = qdesigner_internal::createIconSet(QString::fromUtf8("down.png"));
       
  1990     QIcon minusIcon = qdesigner_internal::createIconSet(QString::fromUtf8("minus-16.png"));
       
  1991     QIcon newIcon = qdesigner_internal::createIconSet(QString::fromUtf8("filenew-16.png"));
       
  1992     QIcon openIcon = qdesigner_internal::createIconSet(QString::fromUtf8("fileopen-16.png"));
       
  1993     QIcon removeIcon = qdesigner_internal::createIconSet(QString::fromUtf8("editdelete-16.png"));
       
  1994     QIcon addPrefixIcon = qdesigner_internal::createIconSet(QString::fromUtf8("prefix-add.png"));
       
  1995 
       
  1996     d_ptr->m_newQrcFileAction = new QAction(newIcon, tr("New..."), this);
       
  1997     d_ptr->m_newQrcFileAction->setToolTip(tr("New Resource File"));
       
  1998     d_ptr->m_importQrcFileAction = new QAction(openIcon, tr("Open..."), this);
       
  1999     d_ptr->m_importQrcFileAction->setToolTip(tr("Open Resource File"));
       
  2000     d_ptr->m_removeQrcFileAction = new QAction(removeIcon, tr("Remove"), this);
       
  2001     d_ptr->m_moveUpQrcFileAction = new QAction(upIcon, tr("Move Up"), this);
       
  2002     d_ptr->m_moveDownQrcFileAction = new QAction(downIcon, tr("Move Down"), this);
       
  2003 
       
  2004     d_ptr->m_newPrefixAction = new QAction(addPrefixIcon, tr("Add Prefix"), this);
       
  2005     d_ptr->m_newPrefixAction->setToolTip(tr("Add Prefix"));
       
  2006     d_ptr->m_addResourceFileAction = new QAction(openIcon, tr("Add Files..."), this);
       
  2007     d_ptr->m_changePrefixAction = new QAction(tr("Change Prefix"), this);
       
  2008     d_ptr->m_changeLanguageAction = new QAction(tr("Change Language"), this);
       
  2009     d_ptr->m_changeAliasAction = new QAction(tr("Change Alias"), this);
       
  2010     d_ptr->m_clonePrefixAction = new QAction(tr("Clone Prefix..."), this);
       
  2011     d_ptr->m_removeAction = new QAction(minusIcon, tr("Remove"), this);
       
  2012     d_ptr->m_moveUpAction = new QAction(upIcon, tr("Move Up"), this);
       
  2013     d_ptr->m_moveDownAction = new QAction(downIcon, tr("Move Down"), this);
       
  2014 
       
  2015     d_ptr->m_ui.newQrcButton->setDefaultAction(d_ptr->m_newQrcFileAction);
       
  2016     d_ptr->m_ui.importQrcButton->setDefaultAction(d_ptr->m_importQrcFileAction);
       
  2017     d_ptr->m_ui.removeQrcButton->setDefaultAction(d_ptr->m_removeQrcFileAction);
       
  2018 
       
  2019     d_ptr->m_ui.newResourceButton->setDefaultAction(d_ptr->m_newPrefixAction);
       
  2020     d_ptr->m_ui.addResourceButton->setDefaultAction(d_ptr->m_addResourceFileAction);
       
  2021     d_ptr->m_ui.removeResourceButton->setDefaultAction(d_ptr->m_removeAction);
       
  2022 
       
  2023     connect(d_ptr->m_newQrcFileAction, SIGNAL(triggered()), this, SLOT(slotNewQrcFile()));
       
  2024     connect(d_ptr->m_importQrcFileAction, SIGNAL(triggered()), this, SLOT(slotImportQrcFile()));
       
  2025     connect(d_ptr->m_removeQrcFileAction, SIGNAL(triggered()), this, SLOT(slotRemoveQrcFile()));
       
  2026     connect(d_ptr->m_moveUpQrcFileAction, SIGNAL(triggered()), this, SLOT(slotMoveUpQrcFile()));
       
  2027     connect(d_ptr->m_moveDownQrcFileAction, SIGNAL(triggered()), this, SLOT(slotMoveDownQrcFile()));
       
  2028 
       
  2029     connect(d_ptr->m_newPrefixAction, SIGNAL(triggered()), this, SLOT(slotNewPrefix()));
       
  2030     connect(d_ptr->m_addResourceFileAction, SIGNAL(triggered()), this, SLOT(slotAddFiles()));
       
  2031     connect(d_ptr->m_changePrefixAction, SIGNAL(triggered()), this, SLOT(slotChangePrefix()));
       
  2032     connect(d_ptr->m_changeLanguageAction, SIGNAL(triggered()), this, SLOT(slotChangeLanguage()));
       
  2033     connect(d_ptr->m_changeAliasAction, SIGNAL(triggered()), this, SLOT(slotChangeAlias()));
       
  2034     connect(d_ptr->m_clonePrefixAction, SIGNAL(triggered()), this, SLOT(slotClonePrefix()));
       
  2035     connect(d_ptr->m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemove()));
       
  2036     connect(d_ptr->m_moveUpAction, SIGNAL(triggered()), this, SLOT(slotMoveUp()));
       
  2037     connect(d_ptr->m_moveDownAction, SIGNAL(triggered()), this, SLOT(slotMoveDown()));
       
  2038 
       
  2039     d_ptr->m_ui.qrcFileList->setContextMenuPolicy(Qt::CustomContextMenu);
       
  2040     connect(d_ptr->m_ui.qrcFileList, SIGNAL(customContextMenuRequested(const QPoint &)),
       
  2041                 this, SLOT(slotListWidgetContextMenuRequested(const QPoint &)));
       
  2042     connect(d_ptr->m_ui.qrcFileList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
       
  2043                     this, SLOT(slotCurrentQrcFileChanged(QListWidgetItem *)));
       
  2044 
       
  2045     d_ptr->m_treeModel = new QStandardItemModel(this);
       
  2046     d_ptr->m_treeModel->setColumnCount(2);
       
  2047     d_ptr->m_treeModel->setHorizontalHeaderItem(0, new QStandardItem(tr("Prefix / Path")));
       
  2048     d_ptr->m_treeModel->setHorizontalHeaderItem(1, new QStandardItem(tr("Language / Alias")));
       
  2049     d_ptr->m_ui.resourceTreeView->setModel(d_ptr->m_treeModel);
       
  2050     d_ptr->m_ui.resourceTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
       
  2051     d_ptr->m_treeSelection = d_ptr->m_ui.resourceTreeView->selectionModel();
       
  2052     connect(d_ptr->m_ui.resourceTreeView->header(), SIGNAL(sectionDoubleClicked(int)), d_ptr->m_ui.resourceTreeView, SLOT(resizeColumnToContents(int)));
       
  2053     d_ptr->m_ui.resourceTreeView->setTextElideMode(Qt::ElideLeft);
       
  2054 
       
  2055     connect(d_ptr->m_ui.resourceTreeView, SIGNAL(customContextMenuRequested(const QPoint &)),
       
  2056                 this, SLOT(slotTreeViewContextMenuRequested(const QPoint &)));
       
  2057     connect(d_ptr->m_treeModel, SIGNAL(itemChanged(QStandardItem *)),
       
  2058                 this, SLOT(slotTreeViewItemChanged(QStandardItem *)));
       
  2059     connect(d_ptr->m_treeSelection, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
       
  2060                     this, SLOT(slotCurrentTreeViewItemChanged(const QModelIndex &)));
       
  2061 
       
  2062     d_ptr->m_ui.resourceTreeView->setColumnWidth(0, 200);
       
  2063 
       
  2064     d_ptr->slotCurrentTreeViewItemChanged(QModelIndex());
       
  2065     d_ptr->m_removeQrcFileAction->setEnabled(false);
       
  2066     d_ptr->m_moveUpQrcFileAction->setEnabled(false);
       
  2067     d_ptr->m_moveDownQrcFileAction->setEnabled(false);
       
  2068 
       
  2069     QDesignerSettingsInterface *settings = core->settingsManager();
       
  2070     settings->beginGroup(QLatin1String(QrcDialogC));
       
  2071 
       
  2072     d_ptr->m_ui.splitter->restoreState(settings->value(QLatin1String(SplitterPosition)).toByteArray());
       
  2073     if (settings->contains(QLatin1String(Geometry)))
       
  2074         setGeometry(settings->value(QLatin1String(Geometry)).toRect());
       
  2075 
       
  2076     settings->endGroup();
       
  2077 }
       
  2078 
       
  2079 QtResourceEditorDialog::~QtResourceEditorDialog()
       
  2080 {
       
  2081     QDesignerSettingsInterface *settings = d_ptr->m_core->settingsManager();
       
  2082     settings->beginGroup(QLatin1String(QrcDialogC));
       
  2083 
       
  2084     settings->setValue(QLatin1String(SplitterPosition), d_ptr->m_ui.splitter->saveState());
       
  2085     settings->setValue(QLatin1String(Geometry), geometry());
       
  2086     settings->endGroup();
       
  2087 }
       
  2088 
       
  2089 QtResourceModel *QtResourceEditorDialog::model() const
       
  2090 {
       
  2091     return d_ptr->m_resourceModel;
       
  2092 }
       
  2093 
       
  2094 void QtResourceEditorDialog::setResourceModel(QtResourceModel *model)
       
  2095 {
       
  2096     d_ptr->m_resourceModel = model;
       
  2097 
       
  2098     QtResourceSet *resourceSet = d_ptr->m_resourceModel->currentResourceSet();
       
  2099     if (!resourceSet) {
       
  2100         // disable everything but cancel button
       
  2101         return;
       
  2102     }
       
  2103 
       
  2104     d_ptr->m_initialState.clear();
       
  2105 
       
  2106     // enable qrcBox
       
  2107 
       
  2108     QStringList paths = resourceSet->activeQrcPaths();
       
  2109     QStringListIterator it(paths);
       
  2110     while (it.hasNext()) {
       
  2111         const QString path = it.next();
       
  2112         QtQrcFileData qrcFileData;
       
  2113         d_ptr->loadQrcFile(path, &qrcFileData);
       
  2114         d_ptr->m_initialState << qrcFileData;
       
  2115         d_ptr->m_qrcManager->importQrcFile(qrcFileData);
       
  2116     }
       
  2117     if (d_ptr->m_ui.qrcFileList->count() > 0) {
       
  2118         d_ptr->m_ui.qrcFileList->item(0)->setSelected(true);
       
  2119     }
       
  2120 }
       
  2121 
       
  2122 QString QtResourceEditorDialog::selectedResource() const
       
  2123 {
       
  2124     //QtResourcePrefix *currentResourcePrefix = d_ptr->m_qrcManager->resourcePrefixOf(currentResourceFile);
       
  2125     QtResourcePrefix *currentResourcePrefix = d_ptr->getCurrentResourcePrefix();
       
  2126     if (!currentResourcePrefix)
       
  2127         return QString();
       
  2128 
       
  2129     const QChar slash(QLatin1Char('/'));
       
  2130     QString resource = currentResourcePrefix->prefix();
       
  2131     if (!resource.startsWith(slash))
       
  2132         resource.prepend(slash);
       
  2133     if (!resource.endsWith(slash))
       
  2134         resource.append(slash);
       
  2135     resource.prepend(QLatin1Char(':'));
       
  2136 
       
  2137     QtResourceFile *currentResourceFile = d_ptr->getCurrentResourceFile();
       
  2138     if (!currentResourceFile)
       
  2139         return resource;
       
  2140 
       
  2141     QString resourceEnding = currentResourceFile->path();
       
  2142     if (!currentResourceFile->alias().isEmpty())
       
  2143         resourceEnding = currentResourceFile->alias();
       
  2144 
       
  2145     const QString dotSlash(QLatin1String("./"));
       
  2146     const QString dotDotSlash(QLatin1String("../"));
       
  2147     while (1) {
       
  2148         if (resourceEnding.startsWith(slash))
       
  2149             resourceEnding = resourceEnding.mid(1);
       
  2150         else if (resourceEnding.startsWith(dotSlash))
       
  2151             resourceEnding = resourceEnding.mid(dotSlash.count());
       
  2152         else if (resourceEnding.startsWith(dotDotSlash))
       
  2153             resourceEnding = resourceEnding.mid(dotDotSlash.count());
       
  2154         else
       
  2155             break;
       
  2156     }
       
  2157 
       
  2158     resource.append(resourceEnding);
       
  2159 
       
  2160     return resource;
       
  2161 }
       
  2162 
       
  2163 void QtResourceEditorDialog::displayResourceFailures(const QString &logOutput, QDesignerDialogGuiInterface *dlgGui, QWidget *parent)
       
  2164 {
       
  2165     const QString msg = tr("<html><p><b>Warning:</b> There have been problems while reloading the resources:</p><pre>%1</pre></html>").arg(logOutput);
       
  2166     dlgGui->message(parent, QDesignerDialogGuiInterface::ResourceEditorMessage, QMessageBox::Warning,
       
  2167                     tr("Resource Warning"), msg);
       
  2168 }
       
  2169 
       
  2170 void QtResourceEditorDialog::accept()
       
  2171 {
       
  2172     QStringList newQrcPaths;
       
  2173     QList<QtQrcFileData> currentState;
       
  2174 
       
  2175     QList<QtQrcFile *> qrcFiles = d_ptr->m_qrcManager->qrcFiles();
       
  2176     QListIterator<QtQrcFile *> itQrc(qrcFiles);
       
  2177     while (itQrc.hasNext()) {
       
  2178         QtQrcFile *qrcFile = itQrc.next();
       
  2179         QtQrcFileData qrcFileData;
       
  2180         d_ptr->m_qrcManager->exportQrcFile(qrcFile, &qrcFileData);
       
  2181         currentState << qrcFileData;
       
  2182         if (qrcFileData == qrcFile->initialState()) {
       
  2183             // nothing
       
  2184         } else {
       
  2185             d_ptr->m_resourceModel->setWatcherEnabled(qrcFileData.qrcPath, false);
       
  2186             bool ok = d_ptr->saveQrcFile(qrcFileData);
       
  2187             d_ptr->m_resourceModel->setWatcherEnabled(qrcFileData.qrcPath, true);
       
  2188             if (!ok)
       
  2189                 return;
       
  2190 
       
  2191             d_ptr->m_resourceModel->setModified(qrcFileData.qrcPath);
       
  2192         }
       
  2193         newQrcPaths << qrcFileData.qrcPath;
       
  2194     }
       
  2195 
       
  2196     if (currentState == d_ptr->m_initialState) {
       
  2197         // nothing
       
  2198     } else {
       
  2199         int errorCount;
       
  2200         QString errorMessages;
       
  2201         d_ptr->m_resourceModel->currentResourceSet()->activateQrcPaths(newQrcPaths, &errorCount, &errorMessages);
       
  2202         if (errorCount)
       
  2203             displayResourceFailures(errorMessages, d_ptr->m_dlgGui, this);
       
  2204     }
       
  2205     QDialog::accept();
       
  2206 }
       
  2207 
       
  2208 QString QtResourceEditorDialog::editResources(QDesignerFormEditorInterface *core,
       
  2209                                               QtResourceModel *model,
       
  2210                                               QDesignerDialogGuiInterface *dlgGui,
       
  2211                                               QWidget *parent)
       
  2212 {
       
  2213     QtResourceEditorDialog dialog(core, dlgGui, parent);
       
  2214     dialog.setResourceModel(model);
       
  2215     if (dialog.exec() == QDialog::Accepted)
       
  2216         return dialog.selectedResource();
       
  2217     return QString();
       
  2218 }
       
  2219 
       
  2220 QT_END_NAMESPACE
       
  2221 
       
  2222 #include "moc_qtresourceeditordialog_p.cpp"
       
  2223 #include "qtresourceeditordialog.moc"