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 examples 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 "coloritem.h"
52
53#include <QApplication>
54#include <QBitmap>
55#include <QCursor>
56#include <QDrag>
57#include <QGraphicsSceneMouseEvent>
58#include <QMimeData>
59#include <QPainter>
60#include <QRandomGenerator>
61#include <QWidget>
62
63//! [0]
64ColorItem::ColorItem()
65 : color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256))
66{
67 setToolTip(QString("QColor(%1, %2, %3)\n%4")
68 .arg(color.red()).arg(color.green()).arg(color.blue())
69 .arg("Click and drag this color onto the robot!"));
70 setCursor(Qt::OpenHandCursor);
71 setAcceptedMouseButtons(Qt::LeftButton);
72}
73//! [0]
74
75//! [1]
76QRectF ColorItem::boundingRect() const
77{
78 return QRectF(-15.5, -15.5, 34, 34);
79}
80//! [1]
81
82//! [2]
83void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
84{
85 Q_UNUSED(option);
86 Q_UNUSED(widget);
87 painter->setPen(Qt::NoPen);
88 painter->setBrush(Qt::darkGray);
89 painter->drawEllipse(-12, -12, 30, 30);
90 painter->setPen(QPen(Qt::black, 1));
91 painter->setBrush(QBrush(color));
92 painter->drawEllipse(-15, -15, 30, 30);
93}
94//! [2]
95
96//! [3]
97void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *)
98{
99 setCursor(Qt::ClosedHandCursor);
100}
101//! [3]
102
103//! [5]
104void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
105{
106 if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
107 .length() < QApplication::startDragDistance()) {
108 return;
109 }
110
111 QDrag *drag = new QDrag(event->widget());
112 QMimeData *mime = new QMimeData;
113 drag->setMimeData(mime);
114//! [5]
115
116//! [6]
117 static int n = 0;
118 if (n++ > 2 && QRandomGenerator::global()->bounded(3) == 0) {
119 QImage image(":/images/head.png");
120 mime->setImageData(image);
121
122 drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40));
123 drag->setHotSpot(QPoint(15, 30));
124//! [6]
125//! [7]
126 } else {
127 mime->setColorData(color);
128 mime->setText(QString("#%1%2%3")
129 .arg(color.red(), 2, 16, QLatin1Char('0'))
130 .arg(color.green(), 2, 16, QLatin1Char('0'))
131 .arg(color.blue(), 2, 16, QLatin1Char('0')));
132
133 QPixmap pixmap(34, 34);
134 pixmap.fill(Qt::white);
135
136 QPainter painter(&pixmap);
137 painter.translate(15, 15);
138 painter.setRenderHint(QPainter::Antialiasing);
139 paint(&painter, nullptr, nullptr);
140 painter.end();
141
142 pixmap.setMask(pixmap.createHeuristicMask());
143
144 drag->setPixmap(pixmap);
145 drag->setHotSpot(QPoint(15, 20));
146 }
147//! [7]
148
149//! [8]
150 drag->exec();
151 setCursor(Qt::OpenHandCursor);
152}
153//! [8]
154
155//! [4]
156void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
157{
158 setCursor(Qt::OpenHandCursor);
159}
160//! [4]
161