src/hbcore/image/hbiconsource.cpp
changeset 6 c3690ec91ef8
parent 5 627c4a0fd0e7
child 7 923ff622b8b9
equal deleted inserted replaced
5:627c4a0fd0e7 6:c3690ec91ef8
    58     if (loweredIconPath.endsWith(QLatin1String(".qpic"))) {
    58     if (loweredIconPath.endsWith(QLatin1String(".qpic"))) {
    59         return QLatin1String("PIC");
    59         return QLatin1String("PIC");
    60     }
    60     }
    61 
    61 
    62     if (loweredIconPath.endsWith(QLatin1String(".svg"))
    62     if (loweredIconPath.endsWith(QLatin1String(".svg"))
    63         || loweredIconPath.endsWith(QLatin1String(".png"))
    63             || loweredIconPath.endsWith(QLatin1String(".png"))
    64         || loweredIconPath.endsWith(QLatin1String(".mng"))
    64             || loweredIconPath.endsWith(QLatin1String(".mng"))
    65         || loweredIconPath.endsWith(QLatin1String(".gif"))
    65             || loweredIconPath.endsWith(QLatin1String(".gif"))
    66         || loweredIconPath.endsWith(QLatin1String(".xpm"))
    66             || loweredIconPath.endsWith(QLatin1String(".xpm"))
    67         || loweredIconPath.endsWith(QLatin1String(".jpg"))
    67             || loweredIconPath.endsWith(QLatin1String(".jpg"))
    68         || loweredIconPath.endsWith(QLatin1String(".nvg"))) {
    68             || loweredIconPath.endsWith(QLatin1String(".nvg"))) {
    69         return iconPath.right(3).toUpper();
    69         return iconPath.right(3).toUpper();
    70     }
    70     }
    71 
    71 
    72     if (loweredIconPath.endsWith(QLatin1String(".xml"))
    72     if (loweredIconPath.endsWith(QLatin1String(".xml"))
    73         || loweredIconPath.endsWith(QLatin1String(".axml"))
    73             || loweredIconPath.endsWith(QLatin1String(".axml"))
    74         || loweredIconPath.endsWith(QLatin1String(".fxml")))
    74             || loweredIconPath.endsWith(QLatin1String(".fxml"))) {
    75     {
       
    76         return "BLOB";
    75         return "BLOB";
    77     }
    76     }
    78 
    77 
    79     return iconPath.section('.', -1);
    78     return iconPath.section('.', -1);
    80 }
    79 }
    84 
    83 
    85 // Copied from HbNvgEnginePrivate::contentDimensions,
    84 // Copied from HbNvgEnginePrivate::contentDimensions,
    86 // to avoid NVG dependency in ThemeIndexer
    85 // to avoid NVG dependency in ThemeIndexer
    87 QSize nvgContentDimensions(const QByteArray &buffer)
    86 QSize nvgContentDimensions(const QByteArray &buffer)
    88 {
    87 {
    89     QSize ret(0,0);
    88     QSize ret(0, 0);
    90      if (buffer.length() < static_cast<qint32>(NVG_VIEWBOX_HEIGHT_OFS + sizeof (float))){
    89     if (buffer.length() < static_cast<qint32>(NVG_VIEWBOX_HEIGHT_OFS + sizeof(float))) {
    91         ret = QSize(0, 0);
    90         ret = QSize(0, 0);
    92         } 
    91     }
    93      
    92 
    94     const quint8* lBuf = (quint8*) buffer.data();
    93     const quint8 *lBuf = (quint8 *) buffer.data();
    95     if((buffer.length() > NVG_VIEWBOX_WIDTH_OFS) && (buffer.length() > NVG_VIEWBOX_HEIGHT_OFS)) {	
    94     if ((buffer.length() > NVG_VIEWBOX_WIDTH_OFS) && (buffer.length() > NVG_VIEWBOX_HEIGHT_OFS)) {
    96         // Do not change to qreal, no matter what krazy says.
    95         // Do not change to qreal, no matter what krazy says.
    97 	    float lViewboxWidth = * (float*)(lBuf + NVG_VIEWBOX_WIDTH_OFS);
    96         float lViewboxWidth = * (float *)(lBuf + NVG_VIEWBOX_WIDTH_OFS);
    98 	    float lViewboxHeight = * (float*)(lBuf + NVG_VIEWBOX_HEIGHT_OFS);
    97         float lViewboxHeight = * (float *)(lBuf + NVG_VIEWBOX_HEIGHT_OFS);
    99 
    98 
   100 	    if (lViewboxWidth > 0 && lViewboxHeight > 0) {
    99         if (lViewboxWidth > 0 && lViewboxHeight > 0) {
   101 	        ret = QSize((int)lViewboxWidth, (int)lViewboxHeight);
   100             ret = QSize((int)lViewboxWidth, (int)lViewboxHeight);
   102 	    } else {
   101         } else {
   103 	        ret = QSize(0, 0);
   102             ret = QSize(0, 0);
   104 	    }
   103         }
   105     }
   104     }
   106     return ret;
   105     return ret;
   107 }
   106 }
   108 
   107 
   109 
   108 
   110 #endif // HB_BOOTSTRAPPED
   109 #endif // HB_BOOTSTRAPPED
   111 
   110 
   112 /*!
   111 /*!
   113   \class HbIconSource
   112   \class HbIconSource
   114   
   113 
   115   \brief Encapsulates access (size and pixel data reading) to image files.
   114   \brief Encapsulates access (size and pixel data reading) to image files.
   116 
   115 
   117   \internal
   116   \internal
   118 */
   117 */
   119 
   118 
   172     if (!mDefaultSize.isValid()) {
   171     if (!mDefaultSize.isValid()) {
   173         type(); // make sure type is initialized
   172         type(); // make sure type is initialized
   174         if (mType == "NVG") {
   173         if (mType == "NVG") {
   175 #ifndef HB_BOOTSTRAPPED
   174 #ifndef HB_BOOTSTRAPPED
   176 #ifdef HB_NVG_CS_ICON
   175 #ifdef HB_NVG_CS_ICON
   177         if(!mByteArray){
   176             if (!mByteArray) {
   178             QFile file(mFilename);
   177                 QFile file(mFilename);
   179             if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   178                 if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   180                 return QSizeF();
   179                     return QSizeF();
   181             }                
   180                 }
   182             mByteArray = new QByteArray(file.readAll());
   181                 mByteArray = new QByteArray(file.readAll());
   183         }
   182             }
   184         HbNvgEngine nvgEngine; 
   183             HbNvgEngine nvgEngine;
   185         mDefaultSize = nvgEngine.contentDimensions(*mByteArray);
   184             mDefaultSize = nvgEngine.contentDimensions(*mByteArray);
   186 #endif // HB_NVG_CS_ICON
   185 #endif // HB_NVG_CS_ICON
   187 #else // HB_BOOTSTRAPPED
   186 #else // HB_BOOTSTRAPPED
   188 
   187 
   189         if(!mByteArray){
   188             if (!mByteArray) {
   190             QFile file(mFilename);
   189                 QFile file(mFilename);
   191             if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   190                 if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   192                 return QSizeF();
   191                     return QSizeF();
   193             }                
   192                 }
   194             mByteArray = new QByteArray (file.readAll());
   193                 mByteArray = new QByteArray(file.readAll());
   195         }
   194             }
   196 
   195 
   197         mDefaultSize = nvgContentDimensions(*mByteArray);
   196             mDefaultSize = nvgContentDimensions(*mByteArray);
   198 #endif
   197 #endif
   199         }
   198         } else if (mType == "SVG") {
   200         else if (mType == "SVG") {
       
   201             QSvgRenderer *renderer = svgRenderer();
   199             QSvgRenderer *renderer = svgRenderer();
   202             if (renderer) { // isValid() is already checked in svgRenderer()
   200             if (renderer) { // isValid() is already checked in svgRenderer()
   203                 mDefaultSize = renderer->defaultSize();
   201                 mDefaultSize = renderer->defaultSize();
   204             }
   202             }
   205             releaseSvgRenderer();
   203             releaseSvgRenderer();
   206         }
   204         } else if (mType == "PIC") {
   207         else if (mType == "PIC") {
       
   208             if (!mPicture) {
   205             if (!mPicture) {
   209                 mPicture = new QPicture;
   206                 mPicture = new QPicture;
   210                 mPicture->load(mFilename);
   207                 mPicture->load(mFilename);
   211             }
   208             }
   212             mDefaultSize = mPicture->boundingRect().size();
   209             mDefaultSize = mPicture->boundingRect().size();
   228                     QImage img = reader->read();
   225                     QImage img = reader->read();
   229                     mDefaultSize = img.size();
   226                     mDefaultSize = img.size();
   230                 }
   227                 }
   231             }
   228             }
   232             releaseImageReader();
   229             releaseImageReader();
   233         }
   230         } else if (mType != "BLOB") {
   234         else if (mType != "BLOB") {
       
   235             if (!mPixmap) {
   231             if (!mPixmap) {
   236                 mPixmap = new QPixmap(mFilename);
   232                 mPixmap = new QPixmap(mFilename);
   237             }
   233             }
   238             mDefaultSize = mPixmap->size();
   234             mDefaultSize = mPixmap->size();
   239         }
   235         }
   269 */
   265 */
   270 void HbIconSource::takeSvgRenderer()
   266 void HbIconSource::takeSvgRenderer()
   271 {
   267 {
   272     mSvgRenderer = 0;
   268     mSvgRenderer = 0;
   273 }
   269 }
   274     
   270 
   275 QByteArray* HbIconSource::byteArray()
   271 QByteArray *HbIconSource::byteArray()
   276 {
   272 {
   277     if (!mByteArray) {
   273     if (!mByteArray) {
   278 #ifdef HB_NVG_CS_ICON
   274 #ifdef HB_NVG_CS_ICON
   279         QFile file(mFilename);
   275         QFile file(mFilename);
   280         if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   276         if (!file.open(QIODevice::NotOpen | QIODevice::ReadOnly)) {
   281             return 0;
   277             return 0;
   282         }
   278         }
   283         mByteArray = new QByteArray(file.readAll());
   279         mByteArray = new QByteArray(file.readAll());
   284 #endif
   280 #endif
   285     }
   281     }
   286     
   282 
   287     if (mByteArray && !mByteArray->isEmpty()) {
   283     if (mByteArray && !mByteArray->isEmpty()) {
   288         return mByteArray;
   284         return mByteArray;
   289     } else {
   285     } else {
   290         return 0;
   286         return 0;
   291     }
   287     }