1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "chip.h"
52
53#include <QGraphicsSceneMouseEvent>
54#include <QPainter>
55#include <QStyleOptionGraphicsItem>
56
57Chip::Chip(const QColor &color, int x, int y)
58{
59 this->x = x;
60 this->y = y;
61 this->color = color;
62 setZValue((x + y) % 2);
63
64 setFlags(ItemIsSelectable | ItemIsMovable);
65 setAcceptHoverEvents(true);
66}
67
68QRectF Chip::boundingRect() const
69{
70 return QRectF(0, 0, 110, 70);
71}
72
73QPainterPath Chip::shape() const
74{
75 QPainterPath path;
76 path.addRect(x: 14, y: 14, w: 82, h: 42);
77 return path;
78}
79
80void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
81{
82 Q_UNUSED(widget);
83
84 QColor fillColor = (option->state & QStyle::State_Selected) ? color.darker(f: 150) : color;
85 if (option->state & QStyle::State_MouseOver)
86 fillColor = fillColor.lighter(f: 125);
87
88 const qreal lod = option->levelOfDetailFromTransform(worldTransform: painter->worldTransform());
89 if (lod < 0.2) {
90 if (lod < 0.125) {
91 painter->fillRect(QRectF(0, 0, 110, 70), color: fillColor);
92 return;
93 }
94
95 QBrush b = painter->brush();
96 painter->setBrush(fillColor);
97 painter->drawRect(x: 13, y: 13, w: 97, h: 57);
98 painter->setBrush(b);
99 return;
100 }
101
102 QPen oldPen = painter->pen();
103 QPen pen = oldPen;
104 int width = 0;
105 if (option->state & QStyle::State_Selected)
106 width += 2;
107
108 pen.setWidth(width);
109 QBrush b = painter->brush();
110 painter->setBrush(QBrush(fillColor.darker(f: option->state & QStyle::State_Sunken ? 120 : 100)));
111
112 painter->drawRect(r: QRect(14, 14, 79, 39));
113 painter->setBrush(b);
114
115 if (lod >= 1) {
116 painter->setPen(QPen(Qt::gray, 1));
117 painter->drawLine(x1: 15, y1: 54, x2: 94, y2: 54);
118 painter->drawLine(x1: 94, y1: 53, x2: 94, y2: 15);
119 painter->setPen(QPen(Qt::black, 0));
120 }
121
122 // Draw text
123 if (lod >= 2) {
124 QFont font("Times", 10);
125 font.setStyleStrategy(QFont::ForceOutline);
126 painter->setFont(font);
127 painter->save();
128 painter->scale(sx: 0.1, sy: 0.1);
129 painter->drawText(x: 170, y: 180, s: QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(a: x).arg(a: y));
130 painter->drawText(x: 170, y: 200, s: QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
131 painter->drawText(x: 170, y: 220, s: QString("Manufacturer: Chip Manufacturer"));
132 painter->restore();
133 }
134
135 // Draw lines
136 QVarLengthArray<QLineF, 36> lines;
137 if (lod >= 0.5) {
138 for (int i = 0; i <= 10; i += (lod > 0.5 ? 1 : 2)) {
139 lines.append(t: QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
140 lines.append(t: QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
141 }
142 for (int i = 0; i <= 6; i += (lod > 0.5 ? 1 : 2)) {
143 lines.append(t: QLineF(5, 18 + i * 5, 13, 18 + i * 5));
144 lines.append(t: QLineF(94, 18 + i * 5, 102, 18 + i * 5));
145 }
146 }
147 if (lod >= 0.4) {
148 const QLineF lineData[] = {
149 QLineF(25, 35, 35, 35),
150 QLineF(35, 30, 35, 40),
151 QLineF(35, 30, 45, 35),
152 QLineF(35, 40, 45, 35),
153 QLineF(45, 30, 45, 40),
154 QLineF(45, 35, 55, 35)
155 };
156 lines.append(abuf: lineData, increment: 6);
157 }
158 painter->drawLines(lines: lines.data(), lineCount: lines.size());
159
160 // Draw red ink
161 if (stuff.size() > 1) {
162 QPen p = painter->pen();
163 painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
164 painter->setBrush(Qt::NoBrush);
165 QPainterPath path;
166 path.moveTo(p: stuff.first());
167 for (int i = 1; i < stuff.size(); ++i)
168 path.lineTo(p: stuff.at(i));
169 painter->drawPath(path);
170 painter->setPen(p);
171 }
172}
173
174void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
175{
176 QGraphicsItem::mousePressEvent(event);
177 update();
178}
179
180void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
181{
182 if (event->modifiers() & Qt::ShiftModifier) {
183 stuff << event->pos();
184 update();
185 return;
186 }
187 QGraphicsItem::mouseMoveEvent(event);
188}
189
190void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
191{
192 QGraphicsItem::mouseReleaseEvent(event);
193 update();
194}
195

source code of qtbase/examples/widgets/graphicsview/chip/chip.cpp