examples/painting/basicdrawing/renderarea.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 examples 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 <QtGui>
       
    43 
       
    44 #include "renderarea.h"
       
    45 
       
    46 //! [0]
       
    47 RenderArea::RenderArea(QWidget *parent)
       
    48     : QWidget(parent)
       
    49 {
       
    50     shape = Polygon;
       
    51     antialiased = false;
       
    52     transformed = false;
       
    53     pixmap.load(":/images/qt-logo.png");
       
    54 
       
    55     setBackgroundRole(QPalette::Base);
       
    56     setAutoFillBackground(true);
       
    57 }
       
    58 //! [0]
       
    59 
       
    60 //! [1]
       
    61 QSize RenderArea::minimumSizeHint() const
       
    62 {
       
    63     return QSize(100, 100);
       
    64 }
       
    65 //! [1]
       
    66 
       
    67 //! [2]
       
    68 QSize RenderArea::sizeHint() const
       
    69 {
       
    70     return QSize(400, 200);
       
    71 }
       
    72 //! [2]
       
    73 
       
    74 //! [3]
       
    75 void RenderArea::setShape(Shape shape)
       
    76 {
       
    77     this->shape = shape;
       
    78     update();
       
    79 }
       
    80 //! [3]
       
    81 
       
    82 //! [4]
       
    83 void RenderArea::setPen(const QPen &pen)
       
    84 {
       
    85     this->pen = pen;
       
    86     update();
       
    87 }
       
    88 //! [4]
       
    89 
       
    90 //! [5]
       
    91 void RenderArea::setBrush(const QBrush &brush)
       
    92 {
       
    93     this->brush = brush;
       
    94     update();
       
    95 }
       
    96 //! [5]
       
    97 
       
    98 //! [6]
       
    99 void RenderArea::setAntialiased(bool antialiased)
       
   100 {
       
   101     this->antialiased = antialiased;
       
   102     update();
       
   103 }
       
   104 //! [6]
       
   105 
       
   106 //! [7]
       
   107 void RenderArea::setTransformed(bool transformed)
       
   108 {
       
   109     this->transformed = transformed;
       
   110     update();
       
   111 }
       
   112 //! [7]
       
   113 
       
   114 //! [8]
       
   115 void RenderArea::paintEvent(QPaintEvent * /* event */)
       
   116 {
       
   117     static const QPoint points[4] = {
       
   118         QPoint(10, 80),
       
   119         QPoint(20, 10),
       
   120         QPoint(80, 30),
       
   121         QPoint(90, 70)
       
   122     };
       
   123 
       
   124     QRect rect(10, 20, 80, 60);
       
   125 
       
   126     QPainterPath path;
       
   127     path.moveTo(20, 80);
       
   128     path.lineTo(20, 30);
       
   129     path.cubicTo(80, 0, 50, 50, 80, 80);
       
   130 
       
   131     int startAngle = 20 * 16;
       
   132     int arcLength = 120 * 16;
       
   133 //! [8]
       
   134 
       
   135 //! [9]
       
   136     QPainter painter(this);
       
   137     painter.setPen(pen);
       
   138     painter.setBrush(brush);
       
   139     if (antialiased)
       
   140         painter.setRenderHint(QPainter::Antialiasing, true);
       
   141 //! [9]
       
   142 
       
   143 //! [10]
       
   144     for (int x = 0; x < width(); x += 100) {
       
   145         for (int y = 0; y < height(); y += 100) {
       
   146             painter.save();
       
   147             painter.translate(x, y);
       
   148 //! [10] //! [11]
       
   149             if (transformed) {
       
   150                 painter.translate(50, 50);
       
   151                 painter.rotate(60.0);
       
   152                 painter.scale(0.6, 0.9);
       
   153                 painter.translate(-50, -50);
       
   154             }
       
   155 //! [11]
       
   156 
       
   157 //! [12]
       
   158             switch (shape) {
       
   159             case Line:
       
   160                 painter.drawLine(rect.bottomLeft(), rect.topRight());
       
   161                 break;
       
   162             case Points:
       
   163                 painter.drawPoints(points, 4);
       
   164                 break;
       
   165             case Polyline:
       
   166                 painter.drawPolyline(points, 4);
       
   167                 break;
       
   168             case Polygon:
       
   169                 painter.drawPolygon(points, 4);
       
   170                 break;
       
   171             case Rect:
       
   172                 painter.drawRect(rect);
       
   173                 break;
       
   174             case RoundedRect:
       
   175                 painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
       
   176                 break;
       
   177             case Ellipse:
       
   178                 painter.drawEllipse(rect);
       
   179                 break;
       
   180             case Arc:
       
   181                 painter.drawArc(rect, startAngle, arcLength);
       
   182                 break;
       
   183             case Chord:
       
   184                 painter.drawChord(rect, startAngle, arcLength);
       
   185                 break;
       
   186             case Pie:
       
   187                 painter.drawPie(rect, startAngle, arcLength);
       
   188                 break;
       
   189             case Path:
       
   190                 painter.drawPath(path);
       
   191                 break;
       
   192             case Text:
       
   193                 painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nNokia"));
       
   194                 break;
       
   195             case Pixmap:
       
   196                 painter.drawPixmap(10, 10, pixmap);
       
   197             }
       
   198 //! [12] //! [13]
       
   199             painter.restore();
       
   200         }
       
   201     }
       
   202 
       
   203     painter.setRenderHint(QPainter::Antialiasing, false);
       
   204     painter.setPen(palette().dark().color());
       
   205     painter.setBrush(Qt::NoBrush);
       
   206     painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
       
   207 }
       
   208 //! [13]