ganeswidgets/src/hgtransformedquadrenderer.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 "hgtransformedquadrenderer.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 
       
    29 
       
    30 HgTransformedQuadRenderer::HgTransformedQuadRenderer(int maxQuads) : 
       
    31     HgQuadRenderer(maxQuads)
       
    32 {
       
    33 }
       
    34 
       
    35 HgTransformedQuadRenderer::~HgTransformedQuadRenderer()
       
    36 {
       
    37     qDeleteAll(mTransformedQuads.begin(), mTransformedQuads.end());
       
    38 }
       
    39 
       
    40 void HgTransformedQuadRenderer::init(int maxQuads)
       
    41 {
       
    42     for (int i = 0; i < maxQuads; i++)
       
    43     {
       
    44         mTransformedQuads.append(createNativeQuad());
       
    45     }    
       
    46 }
       
    47 
       
    48 HgQuad* HgTransformedQuadRenderer::getQuadAt(const QPointF& point) const
       
    49 {
       
    50     QList<HgTransformedQuad*>::const_iterator i = mSortedQuads.begin();
       
    51     while(i != mSortedQuads.end())
       
    52     {
       
    53         HgTransformedQuad* q = (*i);
       
    54         if (q->isPointInside(point))
       
    55         {
       
    56             return q->quad();
       
    57         }
       
    58         i++;
       
    59     }
       
    60     
       
    61     return NULL;
       
    62 }
       
    63 
       
    64 
       
    65 void HgTransformedQuadRenderer::transformQuads(const QMatrix4x4& view, const QMatrix4x4& proj, 
       
    66     const QPointF& center, const QSizeF& windowSize)
       
    67 {
       
    68     QMatrix4x4 pv = proj * view;
       
    69     
       
    70     mSortedQuads.clear();
       
    71     
       
    72     for (int i = 0; i < mQuads.size(); i++)
       
    73     {
       
    74         HgQuad* q = mQuads[i];
       
    75             
       
    76         HgTransformedQuad* tq = mTransformedQuads[i];
       
    77         
       
    78         if (q->visible())
       
    79         {
       
    80             tq->transformQuad(i, pv, q, mMirroringPlaneY, mTranslation, center, windowSize);   
       
    81             mSortedQuads.append(tq);
       
    82         }
       
    83     }
       
    84         
       
    85     qSort(mSortedQuads.begin(), mSortedQuads.end(), HgTransformedQuad::quadSorter);
       
    86 }
       
    87 
       
    88 bool HgTransformedQuadRenderer::getQuadTranformedPointsByUserData(QPolygonF& points, const QVariant& userData) const
       
    89 {    
       
    90     for (int i = 0; i < mSortedQuads.count(); i++)
       
    91     {
       
    92         HgTransformedQuad* quad = mSortedQuads[i];
       
    93         if (quad->quad())
       
    94         {
       
    95             if (quad->quad()->userData() == userData)
       
    96             {
       
    97                 quad->getTransformedPoints(points);
       
    98                 return true;                
       
    99             }                
       
   100         }
       
   101     }
       
   102     
       
   103     return false;
       
   104 }
       
   105 
       
   106 QList<HgQuad*> HgTransformedQuadRenderer::getVisibleQuads(const QRectF& rect) const
       
   107 {
       
   108     // this implementation isn't 100% precise
       
   109     QList<HgQuad*> result;
       
   110     for (int i = 0; i < mSortedQuads.count(); i++) {
       
   111        QPolygonF poly;
       
   112        mSortedQuads[i]->getTransformedPoints(poly);
       
   113        QRectF bounds = poly.boundingRect();       
       
   114        if (bounds.intersects(rect) || rect.contains(bounds)) {
       
   115            result.append(mSortedQuads[i]->quad());
       
   116        }           
       
   117     }
       
   118     
       
   119     return result;    
       
   120 }
       
   121 
       
   122 void HgTransformedQuadRenderer::drawTransformedQuads(QPainter* painter, const QRectF& rect)
       
   123 {
       
   124     // draw quads
       
   125     for (int i = 0; i < mSortedQuads.size(); i++)
       
   126     {
       
   127         mSortedQuads[i]->draw(painter, rect);
       
   128     }    
       
   129 }