Debian Codegraph @

tests/benchmarks/widgets/graphicsview/qgraphicsview/benchapps/chipTest/chip.cpp

raw
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 test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "chip.h"
30
31 #include <QtGui>
32
33 Chip::Chip(const QColor &color, int x, int y)
34 {
35 this->x = x;
36 this->y = y;
37 this->color = color;
38 setZValue((x + y) % 2);
39
40 setFlags(ItemIsSelectable | ItemIsMovable);
41 setAcceptHoverEvents(true);
42 }
43
44 QRectF Chip::boundingRect() const
45 {
46 return QRectF(0, 0, 110, 70);
47 }
48
49 QPainterPath Chip::shape() const
50 {
51 QPainterPath path;
52 path.addRect(14, 14, 82, 42);
53 return path;
54 }
55
56 void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
57 {
58 Q_UNUSED(widget);
59
60 QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
61 if (option->state & QStyle::State_MouseOver)
62 fillColor = fillColor.light(125);
63
64 if (option->levelOfDetail < 0.2) {
65 if (option->levelOfDetail < 0.125) {
66 painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
67 return;
68 }
69
70 painter->setPen(QPen(Qt::black, 0));
71 painter->setBrush(fillColor);
72 painter->drawRect(13, 13, 97, 57);
73 return;
74 }
75
76 QPen oldPen = painter->pen();
77 QPen pen = oldPen;
78 int width = 0;
79 if (option->state & QStyle::State_Selected)
80 width += 2;
81
82 pen.setWidth(width);
83 painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
84
85 painter->drawRect(QRect(14, 14, 79, 39));
86 if (option->levelOfDetail >= 1) {
87 painter->setPen(QPen(Qt::gray, 1));
88 painter->drawLine(15, 54, 94, 54);
89 painter->drawLine(94, 53, 94, 15);
90 painter->setPen(QPen(Qt::black, 0));
91 }
92
93 // Draw text
94 if (option->levelOfDetail >= 2) {
95 QFont font("Times", 10);
96 font.setStyleStrategy(QFont::ForceOutline);
97 painter->setFont(font);
98 painter->save();
99 painter->scale(0.1, 0.1);
100 painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
101 painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
102 painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
103 painter->restore();
104 }
105
106 // Draw lines
107 QVarLengthArray<QLineF, 36> lines;
108 if (option->levelOfDetail >= 0.5) {
109 for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
110 lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
111 lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
112 }
113 for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) {
114 lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5));
115 lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5));
116 }
117 }
118 if (option->levelOfDetail >= 0.4) {
119 const QLineF lineData[] = {
120 QLineF(25, 35, 35, 35),
121 QLineF(35, 30, 35, 40),
122 QLineF(35, 30, 45, 35),
123 QLineF(35, 40, 45, 35),
124 QLineF(45, 30, 45, 40),
125 QLineF(45, 35, 55, 35)
126 };
127 lines.append(lineData, 6);
128 }
129 painter->drawLines(lines.data(), lines.size());
130
131 // Draw red ink
132 if (stuff.size() > 1) {
133 painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
134 painter->setBrush(Qt::NoBrush);
135 QPainterPath path;
136 path.moveTo(stuff.first());
137 for (int i = 1; i < stuff.size(); ++i)
138 path.lineTo(stuff.at(i));
139 painter->drawPath(path);
140 }
141 }
142
143 void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
144 {
145 QGraphicsItem::mousePressEvent(event);
146 update();
147 }
148
149 void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
150 {
151 if (event->modifiers() & Qt::ShiftModifier) {
152 stuff << event->pos();
153 update();
154 return;
155 }
156 QGraphicsItem::mouseMoveEvent(event);
157 }
158
159 void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
160 {
161 QGraphicsItem::mouseReleaseEvent(event);
162 update();
163 }