40000 Chips▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"chip.h"
#include <QtWidgets>
Chip::
Chip(const
QColor &
amp;color, int
x, int
y)
{
this
-&
gt;x =
x;
this
-&
gt;y =
y;
this
-&
gt;color =
color;
setZValue((x +
y) %
2
);
setFlags(ItemIsSelectable |
ItemIsMovable);
setAcceptHoverEvents(true
);
}
QRectF Chip::
boundingRect() const
{
return
QRectF(0
, 0
, 110
, 70
);
}
QPainterPath Chip::
shape() const
{
QPainterPath path;
path.addRect(14
, 14
, 82
, 42
);
return
path;
}
void
Chip::
paint(QPainter *
painter, const
QStyleOptionGraphicsItem *
option, QWidget *
widget)
{
Q_UNUSED(widget);
QColor fillColor =
(option-&
gt;state &
amp; QStyle::
State_Selected) ? color.dark(150
) : color;
if
(option-&
gt;state &
amp; QStyle::
State_MouseOver)
fillColor =
fillColor.light(125
);
const
qreal lod =
option-&
gt;levelOfDetailFromTransform(painter-&
gt;worldTransform());
if
(lod &
lt; 0.2
) {
if
(lod &
lt; 0.125
) {
painter-&
gt;fillRect(QRectF(0
, 0
, 110
, 70
), fillColor);
return
;
}
QBrush b =
painter-&
gt;brush();
painter-&
gt;setBrush(fillColor);
painter-&
gt;drawRect(13
, 13
, 97
, 57
);
painter-&
gt;setBrush(b);
return
;
}
QPen oldPen =
painter-&
gt;pen();
QPen pen =
oldPen;
int
width =
0
;
if
(option-&
gt;state &
amp; QStyle::
State_Selected)
width +=
2
;
pen.setWidth(width);
QBrush b =
painter-&
gt;brush();
painter-&
gt;setBrush(QBrush(fillColor.dark(option-&
gt;state &
amp; QStyle::
State_Sunken ? 120
: 100
)));
painter-&
gt;drawRect(QRect(14
, 14
, 79
, 39
));
painter-&
gt;setBrush(b);
if
(lod &
gt;=
1
) {
painter-&
gt;setPen(QPen(Qt::
gray, 1
));
painter-&
gt;drawLine(15
, 54
, 94
, 54
);
painter-&
gt;drawLine(94
, 53
, 94
, 15
);
painter-&
gt;setPen(QPen(Qt::
black, 0
));
}
// Draw text
if
(lod &
gt;=
2
) {
QFont font("Times"
, 10
);
font.setStyleStrategy(QFont::
ForceOutline);
painter-&
gt;setFont(font);
painter-&
gt;save();
painter-&
gt;scale(0.1
, 0.1
);
painter-&
gt;drawText(170
, 180
, QString("Model: VSC-2000 (Very Small Chip) at %1x%2"
).arg(x).arg(y));
painter-&
gt;drawText(170
, 200
, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"
));
painter-&
gt;drawText(170
, 220
, QString("Manufacturer: Chip Manufacturer"
));
painter-&
gt;restore();
}
// Draw lines
QVarLengthArray&
lt;QLineF, 36
&
gt; lines;
if
(lod &
gt;=
0.5
) {
for
(int
i =
0
; i &
lt;=
10
; i +=
(lod &
gt; 0.5
? 1
: 2
)) {
lines.append(QLineF(18
+
7
*
i, 13
, 18
+
7
*
i, 5
));
lines.append(QLineF(18
+
7
*
i, 54
, 18
+
7
*
i, 62
));
}
for
(int
i =
0
; i &
lt;=
6
; i +=
(lod &
gt; 0.5
? 1
: 2
)) {
lines.append(QLineF(5
, 18
+
i *
5
, 13
, 18
+
i *
5
));
lines.append(QLineF(94
, 18
+
i *
5
, 102
, 18
+
i *
5
));
}
}
if
(lod &
gt;=
0.4
) {
const
QLineF lineData[] =
{
QLineF(25
, 35
, 35
, 35
),
QLineF(35
, 30
, 35
, 40
),
QLineF(35
, 30
, 45
, 35
),
QLineF(35
, 40
, 45
, 35
),
QLineF(45
, 30
, 45
, 40
),
QLineF(45
, 35
, 55
, 35
)
}
;
lines.append(lineData, 6
);
}
painter-&
gt;drawLines(lines.data(), lines.size());
// Draw red ink
if
(stuff.size() &
gt; 1
) {
QPen p =
painter-&
gt;pen();
painter-&
gt;setPen(QPen(Qt::
red, 1
, Qt::
SolidLine, Qt::
RoundCap, Qt::
RoundJoin));
painter-&
gt;setBrush(Qt::
NoBrush);
QPainterPath path;
path.moveTo(stuff.first());
for
(int
i =
1
; i &
lt; stuff.size(); ++
i)
path.lineTo(stuff.at(i));
painter-&
gt;drawPath(path);
painter-&
gt;setPen(p);
}
}
void
Chip::
mousePressEvent(QGraphicsSceneMouseEvent *
event)
{
QGraphicsItem::
mousePressEvent(event);
update();
}
void
Chip::
mouseMoveEvent(QGraphicsSceneMouseEvent *
event)
{
if
(event-&
gt;modifiers() &
amp; Qt::
ShiftModifier) {
stuff &
lt;&
lt; event-&
gt;pos();
update();
return
;
}
QGraphicsItem::
mouseMoveEvent(event);
}
void
Chip::
mouseReleaseEvent(QGraphicsSceneMouseEvent *
event)
{
QGraphicsItem::
mouseReleaseEvent(event);
update();
}