| 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 <QtWidgets> |
| 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.darker(150) : color; |
| 61 |
if (option->state & QStyle::State_MouseOver) |
| 62 |
fillColor = fillColor.lighter(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 |
QBrush b = painter->brush(); |
| 71 |
painter->setBrush(fillColor); |
| 72 |
painter->drawRect(13, 13, 97, 57); |
| 73 |
painter->setBrush(b); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
QPen oldPen = painter->pen(); |
| 78 |
QPen pen = oldPen; |
| 79 |
int width = 0; |
| 80 |
if (option->state & QStyle::State_Selected) |
| 81 |
width += 2; |
| 82 |
|
| 83 |
pen.setWidth(width); |
| 84 |
QBrush b = painter->brush(); |
| 85 |
painter->setBrush(QBrush(fillColor.darker(option->state & QStyle::State_Sunken ? 120 : 100))); |
| 86 |
|
| 87 |
painter->drawRect(QRect(14, 14, 79, 39)); |
| 88 |
painter->setBrush(b); |
| 89 |
|
| 90 |
if (option->levelOfDetail >= 1) { |
| 91 |
painter->setPen(QPen(Qt::gray, 1)); |
| 92 |
painter->drawLine(15, 54, 94, 54); |
| 93 |
painter->drawLine(94, 53, 94, 15); |
| 94 |
painter->setPen(QPen(Qt::black, 0)); |
| 95 |
} |
| 96 |
|
| 97 |
// Draw text |
| 98 |
if (option->levelOfDetail >= 2) { |
| 99 |
QFont font("Times", 10); |
| 100 |
font.setStyleStrategy(QFont::ForceOutline); |
| 101 |
painter->setFont(font); |
| 102 |
painter->save(); |
| 103 |
painter->scale(0.1, 0.1); |
| 104 |
painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y)); |
| 105 |
painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ")); |
| 106 |
painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer")); |
| 107 |
painter->restore(); |
| 108 |
} |
| 109 |
|
| 110 |
// Draw lines |
| 111 |
QVarLengthArray<QLineF, 36> lines; |
| 112 |
if (option->levelOfDetail >= 0.5) { |
| 113 |
for (int i = 0; i <= 10; i += (option->levelOfDetail > 0.5 ? 1 : 2)) { |
| 114 |
lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5)); |
| 115 |
lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62)); |
| 116 |
} |
| 117 |
for (int i = 0; i <= 6; i += (option->levelOfDetail > 0.5 ? 1 : 2)) { |
| 118 |
lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5)); |
| 119 |
lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5)); |
| 120 |
} |
| 121 |
} |
| 122 |
if (option->levelOfDetail >= 0.4) { |
| 123 |
const QLineF lineData[] = { |
| 124 |
QLineF(25, 35, 35, 35), |
| 125 |
QLineF(35, 30, 35, 40), |
| 126 |
QLineF(35, 30, 45, 35), |
| 127 |
QLineF(35, 40, 45, 35), |
| 128 |
QLineF(45, 30, 45, 40), |
| 129 |
QLineF(45, 35, 55, 35) |
| 130 |
}; |
| 131 |
lines.append(lineData, 6); |
| 132 |
} |
| 133 |
painter->drawLines(lines.data(), lines.size()); |
| 134 |
|
| 135 |
// Draw red ink |
| 136 |
if (stuff.size() > 1) { |
| 137 |
QPen p = painter->pen(); |
| 138 |
painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
| 139 |
painter->setBrush(Qt::NoBrush); |
| 140 |
QPainterPath path; |
| 141 |
path.moveTo(stuff.first()); |
| 142 |
for (int i = 1; i < stuff.size(); ++i) |
| 143 |
path.lineTo(stuff.at(i)); |
| 144 |
painter->drawPath(path); |
| 145 |
painter->setPen(p); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event) |
| 150 |
{ |
| 151 |
QGraphicsItem::mousePressEvent(event); |
| 152 |
update(); |
| 153 |
} |
| 154 |
|
| 155 |
void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
| 156 |
{ |
| 157 |
if (event->modifiers() & Qt::ShiftModifier) { |
| 158 |
stuff << event->pos(); |
| 159 |
update(); |
| 160 |
return; |
| 161 |
} |
| 162 |
QGraphicsItem::mouseMoveEvent(event); |
| 163 |
} |
| 164 |
|
| 165 |
void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
| 166 |
{ |
| 167 |
QGraphicsItem::mouseReleaseEvent(event); |
| 168 |
update(); |
| 169 |
} |