source: branches/4.5.1/demos/chip/chip.cpp@ 921

Last change on this file since 921 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "chip.h"
43
44#include <QtGui>
45
46Chip::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
57QRectF Chip::boundingRect() const
58{
59 return QRectF(0, 0, 110, 70);
60}
61
62QPainterPath Chip::shape() const
63{
64 QPainterPath path;
65 path.addRect(14, 14, 82, 42);
66 return path;
67}
68
69void 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 QBrush b = painter->brush();
84 painter->setBrush(fillColor);
85 painter->drawRect(13, 13, 97, 57);
86 painter->setBrush(b);
87 return;
88 }
89
90 QPen oldPen = painter->pen();
91 QPen pen = oldPen;
92 int width = 0;
93 if (option->state & QStyle::State_Selected)
94 width += 2;
95
96 pen.setWidth(width);
97 QBrush b = painter->brush();
98 painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
99
100 painter->drawRect(QRect(14, 14, 79, 39));
101 painter->setBrush(b);
102
103 if (option->levelOfDetail >= 1) {
104 painter->setPen(QPen(Qt::gray, 1));
105 painter->drawLine(15, 54, 94, 54);
106 painter->drawLine(94, 53, 94, 15);
107 painter->setPen(QPen(Qt::black, 0));
108 }
109
110 // Draw text
111 if (option->levelOfDetail >= 2) {
112 QFont font("Times", 10);
113 font.setStyleStrategy(QFont::ForceOutline);
114 painter->setFont(font);
115 painter->save();
116 painter->scale(0.1, 0.1);
117 painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
118 painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
119 painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
120 painter->restore();
121 }
122
123 // Draw lines
124 QVarLengthArray<QLineF, 36> lines;
125 if (option->levelOfDetail >= 0.5) {
126 for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
127 lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
128 lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
129 }
130 for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
131 lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5));
132 lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5));
133 }
134 }
135 if (option->levelOfDetail >= 0.4) {
136 const QLineF lineData[] = {
137 QLineF(25, 35, 35, 35),
138 QLineF(35, 30, 35, 40),
139 QLineF(35, 30, 45, 35),
140 QLineF(35, 40, 45, 35),
141 QLineF(45, 30, 45, 40),
142 QLineF(45, 35, 55, 35)
143 };
144 lines.append(lineData, 6);
145 }
146 painter->drawLines(lines.data(), lines.size());
147
148 // Draw red ink
149 if (stuff.size() > 1) {
150 QPen p = painter->pen();
151 painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
152 painter->setBrush(Qt::NoBrush);
153 QPainterPath path;
154 path.moveTo(stuff.first());
155 for (int i = 1; i < stuff.size(); ++i)
156 path.lineTo(stuff.at(i));
157 painter->drawPath(path);
158 painter->setPen(p);
159 }
160}
161
162void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
163{
164 QGraphicsItem::mousePressEvent(event);
165 update();
166}
167
168void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
169{
170 if (event->modifiers() & Qt::ShiftModifier) {
171 stuff << event->pos();
172 update();
173 return;
174 }
175 QGraphicsItem::mouseMoveEvent(event);
176}
177
178void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
179{
180 QGraphicsItem::mouseReleaseEvent(event);
181 update();
182}
Note: See TracBrowser for help on using the repository browser.