tests/benchmarks/qgraphicsview/benchapps/chipTest/chip.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 test suite 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 "chip.h"
       
    43 
       
    44 #include <QtGui>
       
    45 
       
    46 Chip::Chip(const QColor &color, int x, int y)
       
    47 {
       
    48     this->x = x;
       
    49     this->y = y;
       
    50     this->color = color;
       
    51     setZValue((x + y) % 2);
       
    52 
       
    53     setFlags(ItemIsSelectable | ItemIsMovable);
       
    54     setAcceptsHoverEvents(true);
       
    55 }
       
    56 
       
    57 QRectF Chip::boundingRect() const
       
    58 {
       
    59     return QRectF(0, 0, 110, 70);
       
    60 }
       
    61 
       
    62 QPainterPath Chip::shape() const
       
    63 {
       
    64     QPainterPath path;
       
    65     path.addRect(14, 14, 82, 42);
       
    66     return path;
       
    67 }
       
    68 
       
    69 void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    70 {
       
    71     Q_UNUSED(widget);
       
    72 
       
    73     QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
       
    74     if (option->state & QStyle::State_MouseOver)
       
    75         fillColor = fillColor.light(125);
       
    76 
       
    77     if (option->levelOfDetail < 0.2) {
       
    78         if (option->levelOfDetail < 0.125) {
       
    79             painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
       
    80             return;
       
    81         }
       
    82 
       
    83         painter->setPen(QPen(Qt::black, 0));
       
    84         painter->setBrush(fillColor);
       
    85         painter->drawRect(13, 13, 97, 57);
       
    86         return;
       
    87     }
       
    88 
       
    89     QPen oldPen = painter->pen();
       
    90     QPen pen = oldPen;
       
    91     int width = 0;
       
    92     if (option->state & QStyle::State_Selected)
       
    93         width += 2;
       
    94 
       
    95     pen.setWidth(width);
       
    96     painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
       
    97 
       
    98     painter->drawRect(QRect(14, 14, 79, 39));
       
    99     if (option->levelOfDetail >= 1) {
       
   100         painter->setPen(QPen(Qt::gray, 1));
       
   101         painter->drawLine(15, 54, 94, 54);
       
   102         painter->drawLine(94, 53, 94, 15);
       
   103         painter->setPen(QPen(Qt::black, 0));
       
   104     }
       
   105 
       
   106     // Draw text
       
   107     if (option->levelOfDetail >= 2) {
       
   108         QFont font("Times", 10);
       
   109         font.setStyleStrategy(QFont::ForceOutline);
       
   110         painter->setFont(font);
       
   111         painter->save();
       
   112         painter->scale(0.1, 0.1);
       
   113         painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
       
   114         painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
       
   115         painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
       
   116         painter->restore();
       
   117     }
       
   118 
       
   119     // Draw lines
       
   120     QVarLengthArray<QLineF, 36> lines;
       
   121     if (option->levelOfDetail >= 0.5) {
       
   122         for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
       
   123             lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
       
   124             lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
       
   125         }
       
   126         for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
       
   127             lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5));
       
   128             lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5));
       
   129         }
       
   130     }
       
   131     if (option->levelOfDetail >= 0.4) {
       
   132         const QLineF lineData[] = {
       
   133             QLineF(25, 35, 35, 35),
       
   134             QLineF(35, 30, 35, 40),
       
   135             QLineF(35, 30, 45, 35),
       
   136             QLineF(35, 40, 45, 35),
       
   137             QLineF(45, 30, 45, 40),
       
   138             QLineF(45, 35, 55, 35)
       
   139         };
       
   140         lines.append(lineData, 6);
       
   141     }
       
   142     painter->drawLines(lines.data(), lines.size());
       
   143 
       
   144     // Draw red ink
       
   145     if (stuff.size() > 1) {
       
   146         painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
       
   147         painter->setBrush(Qt::NoBrush);
       
   148         QPainterPath path;
       
   149         path.moveTo(stuff.first());
       
   150         for (int i = 1; i < stuff.size(); ++i)
       
   151             path.lineTo(stuff.at(i));
       
   152         painter->drawPath(path);
       
   153     }
       
   154 }
       
   155 
       
   156 void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   157 {
       
   158     QGraphicsItem::mousePressEvent(event);
       
   159     update();
       
   160 }
       
   161 
       
   162 void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   163 {
       
   164     if (event->modifiers() & Qt::ShiftModifier) {
       
   165         stuff << event->pos();
       
   166         update();
       
   167         return;
       
   168     }
       
   169     QGraphicsItem::mouseMoveEvent(event);
       
   170 }
       
   171 
       
   172 void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   173 {
       
   174     QGraphicsItem::mouseReleaseEvent(event);
       
   175     update();
       
   176 }