1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
43
#include "coloritem.h"
46
ColorItem::ColorItem()
47
: color(qrand() % 256, qrand() % 256, qrand() % 256)
49
setToolTip(QString("QColor(%1, %2, %3)\n%4")
50
.arg(color.red()).arg(color.green()).arg(color.blue())
51
.arg("Click and drag this color onto the robot!"));
52
setCursor(Qt::OpenHandCursor);
53
setAcceptedMouseButtons(Qt::LeftButton);
58
QRectF ColorItem::boundingRect() const
60
return QRectF(-15.5, -15.5, 34, 34);
65
void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
69
painter->setPen(Qt::NoPen);
70
painter->setBrush(Qt::darkGray);
71
painter->drawEllipse(-12, -12, 30, 30);
72
painter->setPen(QPen(Qt::black, 1));
73
painter->setBrush(QBrush(color));
74
painter->drawEllipse(-15, -15, 30, 30);
79
void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *)
81
setCursor(Qt::ClosedHandCursor);
86
void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
88
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
89
.length() < QApplication::startDragDistance()) {
93
QDrag *drag = new QDrag(event->widget());
94
QMimeData *mime = new QMimeData;
95
drag->setMimeData(mime);
100
if (n++ > 2 && (qrand() % 3) == 0) {
101
QImage image(":/images/head.png");
102
mime->setImageData(image);
104
drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40));
105
drag->setHotSpot(QPoint(15, 30));
109
mime->setColorData(color);
110
mime->setText(QString("#%1%2%3")
111
.arg(color.red(), 2, 16, QLatin1Char('0'))
112
.arg(color.green(), 2, 16, QLatin1Char('0'))
113
.arg(color.blue(), 2, 16, QLatin1Char('0')));
115
QPixmap pixmap(34, 34);
116
pixmap.fill(Qt::white);
118
QPainter painter(&pixmap);
119
painter.translate(15, 15);
120
painter.setRenderHint(QPainter::Antialiasing);
121
paint(&painter, 0, 0);
124
pixmap.setMask(pixmap.createHeuristicMask());
126
drag->setPixmap(pixmap);
127
drag->setHotSpot(QPoint(15, 20));
133
setCursor(Qt::OpenHandCursor);
138
void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
140
setCursor(Qt::OpenHandCursor);