ganeswidgets/src/hgqtquadrenderer.cpp
changeset 2 49c70dcc3f17
child 5 4fa04caf0f43
equal deleted inserted replaced
1:e48454f237ca 2:49c70dcc3f17
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    
       
    15 *
       
    16 */
       
    17 
       
    18 #include "hgqtquadrenderer.h"
       
    19 #include "hgquad.h"
       
    20 #include "hgtransformedquad.h"
       
    21 #include "trace.h"
       
    22 #include "hgimage.h"
       
    23 
       
    24 #include <qvector2d>
       
    25 #include <qpolygon>
       
    26 #include <qmatrix4x4>
       
    27 #include <qpainter>
       
    28 #include <qpixmapcache>
       
    29 
       
    30 
       
    31 class HgQtImage : public HgImage
       
    32 {
       
    33 public:    
       
    34     HgQtImage(HgQtQuadRenderer* renderer)
       
    35     {
       
    36         
       
    37     }
       
    38 
       
    39     ~HgQtImage()
       
    40     {
       
    41     }
       
    42 
       
    43     int width() const
       
    44     {
       
    45         return mPixmap.width();
       
    46     }
       
    47 
       
    48     int height() const
       
    49     {
       
    50         return mPixmap.height();
       
    51     }
       
    52 
       
    53     int mirrorImageWidth() const
       
    54     {
       
    55         return width();
       
    56     }
       
    57 
       
    58     int mirrorImageHeight() const
       
    59     {
       
    60         return height();
       
    61     }
       
    62 
       
    63     void setImage(const QImage& image)
       
    64     {
       
    65         mPixmap = QPixmap::fromImage(image);
       
    66         //mMirrorPixmap = QPixmap();
       
    67     }
       
    68     
       
    69     void setPixmap(const QPixmap& pixmap)
       
    70     {
       
    71         mPixmap = pixmap;
       
    72         //mMirrorPixmap = QPixmap();
       
    73     }
       
    74     
       
    75     void releaseImage()
       
    76     {
       
    77         //mPixmap = QPixmap();
       
    78         //mMirrorPixmap = QPixmap();
       
    79     }
       
    80     
       
    81     QImage getQImage() const
       
    82     {
       
    83         return mPixmap.toImage();
       
    84     }
       
    85         
       
    86     const QPixmap& pixmap() const
       
    87     {
       
    88         return mPixmap;
       
    89     }
       
    90     
       
    91     const QPixmap& mirrorPixmap(QPainter* painter)
       
    92     {
       
    93 
       
    94         return mPixmap;
       
    95 /*        
       
    96         if (mPixmap.isNull())
       
    97             return mPixmap;
       
    98 
       
    99         if (mMirrorPixmap.isNull())
       
   100         {
       
   101             QImage img = mPixmap.toImage();
       
   102             QImage mirrorImage = img.scaled(QSize(img.width()/3,img.height()/3)).convertToFormat(QImage::Format_ARGB32);
       
   103             
       
   104             // apply gradient to alpha channel so that mirror image looks like
       
   105             // it fades under the floor
       
   106             for (int i = 0; i < mirrorImage.height(); i++)
       
   107             {
       
   108                 qreal t = qreal(i) / qreal(mirrorImage.height());
       
   109                 int a = (int)(t * 255.0);
       
   110                 uchar* scanline = mirrorImage.scanLine(i);
       
   111                 for (int j = 0; j < mirrorImage.width(); j++)
       
   112                 {
       
   113                     scanline[j*4+0] /= 3;
       
   114                     scanline[j*4+1] /= 3;
       
   115                     scanline[j*4+2] /= 3;
       
   116                     scanline[j*4+3] = 255;
       
   117                 }        
       
   118             }
       
   119             
       
   120             mMirrorPixmap = QPixmap::fromImage(mirrorImage);
       
   121             
       
   122             QPaintDevice* device = painter->device();
       
   123             painter->end();
       
   124 
       
   125             mMirrorPixmap = mPixmap.scaled(100,100);
       
   126             int w = mMirrorPixmap.width();
       
   127             int h = mMirrorPixmap.height();
       
   128             //QPainter p;
       
   129             painter->begin(&mMirrorPixmap);
       
   130             painter->fillRect(0,0,w, h, QColor::fromRgbF(0, 0, 0, 0.5f));
       
   131             painter->end();
       
   132 
       
   133             painter->begin(device);
       
   134         
       
   135         }
       
   136         
       
   137         
       
   138         return mMirrorPixmap;*/
       
   139     }
       
   140 
       
   141     QPixmap mPixmap;
       
   142 };
       
   143 
       
   144 class HgQtQuad : public HgTransformedQuad
       
   145 {
       
   146 public:
       
   147     
       
   148     HgQtQuad(HgQtQuadRenderer* renderer) : HgTransformedQuad(-1), mRenderer(renderer)
       
   149     {
       
   150         
       
   151     }
       
   152 
       
   153     ~HgQtQuad()
       
   154     {
       
   155         
       
   156     }
       
   157 
       
   158     void draw(QPainter* painter, const QRectF& rect)    
       
   159     {
       
   160         if (!quad()->visible())
       
   161             return;
       
   162                 
       
   163         HgQtImage* image = (HgQtImage*)quad()->image();        
       
   164         if (image == NULL)
       
   165             image = mRenderer->defaultImage();
       
   166         
       
   167         if (image == NULL)
       
   168             return;
       
   169         
       
   170         if (image->pixmap().isNull())
       
   171             image = mRenderer->defaultImage();
       
   172         
       
   173         drawImage(painter, image, rect);                      
       
   174     }
       
   175 
       
   176     
       
   177 private:
       
   178 
       
   179     void computeWarpMatrix(QTransform& tm, int pxWidth, int pxHeight, const QVector2D* points)
       
   180     {
       
   181         QPolygonF poly;
       
   182 
       
   183         poly << points[0].toPointF();
       
   184         poly << points[1].toPointF();
       
   185         poly << points[2].toPointF();
       
   186         poly << points[3].toPointF();
       
   187 
       
   188         QPolygonF img;
       
   189 
       
   190         img.append(QPointF(0,pxHeight));
       
   191         img.append(QPointF(pxWidth,pxHeight));
       
   192         img.append(QPointF(pxWidth,0));
       
   193         img.append(QPointF(0,0));
       
   194 
       
   195         QTransform::quadToQuad(img, poly, tm);
       
   196     }
       
   197     
       
   198     void drawImage(QPainter* painter, HgQtImage* image, const QRectF& rect)
       
   199     {
       
   200         const QPixmap& pixmap = image->pixmap();
       
   201         
       
   202         if (pixmap.isNull())            
       
   203             return;
       
   204         
       
   205         const QVector2D* points = mTransformedPoints;
       
   206         if (mRenderer->isReflection() && quad()->mirrorImageEnabled())
       
   207             points = mMirroredPoints;
       
   208         
       
   209         QPolygonF poly;
       
   210         poly << points[0].toPointF();
       
   211         poly << points[1].toPointF();
       
   212         poly << points[2].toPointF();
       
   213         poly << points[3].toPointF();        
       
   214         QRectF bounds = poly.boundingRect();
       
   215         if (!(bounds.intersects(rect) || rect.contains(bounds))) {
       
   216             return;
       
   217         }
       
   218         
       
   219         computeWarpMatrix(mTransform, image->width(), image->height(), points);
       
   220         
       
   221         QTransform base = painter->transform(); 
       
   222         painter->setTransform(mTransform * base);    
       
   223         painter->drawPixmap(QPointF(0,0), pixmap);
       
   224         painter->setTransform(base);
       
   225     }
       
   226 
       
   227     HgQtQuadRenderer* mRenderer;
       
   228     QTransform mTransform;
       
   229     QTransform mMirrorTransform;
       
   230 };
       
   231 
       
   232 
       
   233 HgQtQuadRenderer::HgQtQuadRenderer(int maxQuads) : 
       
   234     HgTransformedQuadRenderer(maxQuads),
       
   235     mDefaultQtImage(NULL)
       
   236 {
       
   237     // initialize base class to the end.
       
   238     init(maxQuads);
       
   239     QImage image(QSize(200,200), QImage::Format_RGB16);
       
   240     image.fill(0xFFFFFFFF);
       
   241     setDefaultImage(image);
       
   242     
       
   243     QPixmapCache::setCacheLimit(2048);
       
   244 }
       
   245 
       
   246 HgQtQuadRenderer::~HgQtQuadRenderer()
       
   247 {
       
   248     delete mDefaultQtImage;
       
   249 }
       
   250 
       
   251 void HgQtQuadRenderer::drawQuads(QPainter* painter, const QRectF& rect, 
       
   252     const QMatrix4x4& viewMatrix, const QMatrix4x4& projectionMatrix,
       
   253     Qt::Orientation orientation, 
       
   254     const QTransform& sceneTransform)
       
   255 {
       
   256 
       
   257     transformQuads(viewMatrix, projectionMatrix, 
       
   258         QPointF(rect.width()/2, rect.height()/2), QSizeF(rect.width(), rect.height()));
       
   259     
       
   260     // save old transform
       
   261     QTransform temp = painter->transform();
       
   262 
       
   263     if (mReflectionsEnabled) 
       
   264     {
       
   265         mIsReflection = true;
       
   266 
       
   267         drawTransformedQuads(painter, rect);
       
   268     
       
   269         drawFloor(painter, rect);
       
   270     }
       
   271     
       
   272     mIsReflection = false;
       
   273     
       
   274     drawTransformedQuads(painter, rect);
       
   275         
       
   276     painter->setTransform(temp);
       
   277     
       
   278 }
       
   279 
       
   280 HgImage* HgQtQuadRenderer::createNativeImage()
       
   281 {    
       
   282     return new HgQtImage(this);   
       
   283 }
       
   284 
       
   285 HgQtImage* HgQtQuadRenderer::defaultImage()
       
   286 {
       
   287     return mDefaultQtImage;
       
   288 }
       
   289 
       
   290 void HgQtQuadRenderer::setDefaultImage(QImage defaultImage)
       
   291 {
       
   292     HgQuadRenderer::setDefaultImage(defaultImage);
       
   293     
       
   294     delete mDefaultQtImage;
       
   295     mDefaultQtImage = 0;
       
   296     
       
   297     mDefaultQtImage = static_cast<HgQtImage*>(createNativeImage());
       
   298     mDefaultQtImage->setImage(mDefaultImage);
       
   299 
       
   300 }
       
   301 
       
   302 HgTransformedQuad* HgQtQuadRenderer::createNativeQuad()
       
   303 {
       
   304     return new HgQtQuad(this);
       
   305 }
       
   306 
       
   307 bool HgQtQuadRenderer::isReflection() const
       
   308 {
       
   309     return mIsReflection;
       
   310 }
       
   311 
       
   312 void HgQtQuadRenderer::drawFloor(QPainter* painter, const QRectF& rect)
       
   313 {
       
   314     QRectF floorRect(0, rect.height()/2, rect.width(), rect.height()/2);
       
   315     QBrush brush(QColor::fromRgbF(0,0,0,0.5f));
       
   316     painter->setBrush(brush);
       
   317     painter->drawRect(floorRect);
       
   318 }
       
   319