demos/chip/chip.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "chip.h"
00025 
00026 #include <QtGui>
00027 
00028 Chip::Chip(const QColor &color, int x, int y)
00029 {
00030     this->x = x;
00031     this->y = y;
00032     this->color = color;
00033     setZValue((x + y) % 2);
00034 
00035     setFlags(ItemIsSelectable | ItemIsMovable);
00036     setAcceptsHoverEvents(true);
00037 }
00038 
00039 QRectF Chip::boundingRect() const
00040 {
00041     return QRectF(0, 0, 110, 70);
00042 }
00043 
00044 QPainterPath Chip::shape() const
00045 {
00046     QPainterPath path;
00047     path.addRect(14, 14, 82, 42);
00048     return path;
00049 }
00050 
00051 void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00052 {
00053     Q_UNUSED(widget);
00054 
00055     QColor fillColor = (option->state & QStyle::State_Selected) ? Qt::black : color;
00056     if (option->state & QStyle::State_MouseOver)
00057         fillColor = fillColor.light(125);
00058     
00059     if (option->levelOfDetail < 0.2) {
00060         if (option->levelOfDetail < 0.125) {
00061             painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
00062             return;
00063         }
00064 
00065         painter->setPen(QPen(Qt::black, 0));
00066         painter->setBrush(fillColor);
00067         painter->drawRect(13, 13, 97, 57);
00068         return;
00069     }
00070 
00071     QPen oldPen = painter->pen();
00072     QPen pen = oldPen;
00073     int width = 0;
00074     if (option->state & QStyle::State_Selected)
00075         width += 2;
00076 
00077     pen.setWidth(width);
00078     painter->setPen(pen);
00079     painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
00080 
00081     painter->drawRect(QRect(14, 14, 79, 39));
00082     if (option->levelOfDetail >= 1) {
00083         painter->setPen(QPen(Qt::gray, 1));
00084         painter->drawLine(15, 54, 94, 54);
00085         painter->drawLine(94, 53, 94, 15);
00086     }
00087     painter->setPen(oldPen);
00088     
00089     if (option->levelOfDetail >= 2) {
00090         // Draw text
00091         QFont font("Times", 10);
00092         font.setStyleStrategy(QFont::ForceOutline);
00093         painter->setFont(font);
00094         painter->setRenderHint(QPainter::TextAntialiasing, false);
00095         painter->save();
00096         painter->scale(0.1, 0.1);
00097         painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
00098         painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
00099         painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
00100         painter->restore();
00101     }
00102     if (option->levelOfDetail >= 0.5) {
00103         // Draw lines
00104         for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
00105             painter->drawLine(18 + 7 * i, 13, 18 + 7 * i, 5);
00106             painter->drawLine(18 + 7 * i, 54, 18 + 7 * i, 62);
00107         }
00108         for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
00109             painter->drawLine(5, 18 + i * 5, 13, 18 + i * 5);
00110             painter->drawLine(94, 18 + i * 5, 102, 18 + i * 5);
00111         }
00112     }
00113     if (option->levelOfDetail >= 0.4) {
00114         painter->setPen(oldPen);
00115         painter->drawLine(25, 35, 35, 35);
00116         painter->drawLine(35, 30, 35, 40);
00117         painter->drawLine(35, 30, 45, 35);
00118         painter->drawLine(35, 40, 45, 35);
00119         painter->drawLine(45, 30, 45, 40);
00120         painter->drawLine(45, 35, 55, 35);
00121     }
00122 
00123     painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
00124     painter->setBrush(Qt::NoBrush);
00125     QPainterPath path;
00126     if (stuff.size() > 1) {
00127         path.moveTo(stuff.first());
00128         for (int i = 1; i < stuff.size(); ++i)
00129             path.lineTo(stuff.at(i));
00130         painter->drawPath(path);
00131     }
00132 }
00133 
00134 void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
00135 {
00136     QGraphicsItem::mousePressEvent(event);
00137     update();
00138 }
00139 
00140 void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
00141 {
00142     if (event->modifiers() & Qt::ShiftModifier) {
00143         stuff << event->pos();
00144         update();
00145         return;
00146     }
00147     QGraphicsItem::mouseMoveEvent(event);
00148 }
00149 
00150 void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
00151 {
00152     QGraphicsItem::mouseReleaseEvent(event);
00153     update();
00154 }

Generated on Thu Mar 15 11:52:28 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1