~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/graphicsview/dragdroprobot/coloritem.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
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
 
19
**     distribution.
 
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.
 
23
**
 
24
**
 
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."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
42
 
 
43
#include "coloritem.h"
 
44
 
 
45
//! [0]
 
46
ColorItem::ColorItem()
 
47
    : color(qrand() % 256, qrand() % 256, qrand() % 256)
 
48
{
 
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);
 
54
}
 
55
//! [0]
 
56
 
 
57
//! [1]
 
58
QRectF ColorItem::boundingRect() const
 
59
{
 
60
    return QRectF(-15.5, -15.5, 34, 34);
 
61
}
 
62
//! [1]
 
63
 
 
64
//! [2]
 
65
void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 
66
{
 
67
    Q_UNUSED(option);
 
68
    Q_UNUSED(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);
 
75
}
 
76
//! [2]
 
77
 
 
78
//! [3]
 
79
void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *)
 
80
{
 
81
    setCursor(Qt::ClosedHandCursor);
 
82
}
 
83
//! [3]
 
84
 
 
85
//! [5]
 
86
void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 
87
{
 
88
    if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
 
89
        .length() < QApplication::startDragDistance()) {
 
90
        return;
 
91
    }
 
92
 
 
93
    QDrag *drag = new QDrag(event->widget());
 
94
    QMimeData *mime = new QMimeData;
 
95
    drag->setMimeData(mime);
 
96
//! [5]
 
97
 
 
98
//! [6]
 
99
    static int n = 0;
 
100
    if (n++ > 2 && (qrand() % 3) == 0) {
 
101
        QImage image(":/images/head.png");
 
102
        mime->setImageData(image);
 
103
 
 
104
        drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40));
 
105
        drag->setHotSpot(QPoint(15, 30));
 
106
//! [6]
 
107
//! [7]
 
108
    } else {
 
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')));
 
114
 
 
115
        QPixmap pixmap(34, 34);
 
116
        pixmap.fill(Qt::white);
 
117
 
 
118
        QPainter painter(&pixmap);
 
119
        painter.translate(15, 15);
 
120
        painter.setRenderHint(QPainter::Antialiasing);
 
121
        paint(&painter, 0, 0);
 
122
        painter.end();
 
123
 
 
124
        pixmap.setMask(pixmap.createHeuristicMask());
 
125
 
 
126
        drag->setPixmap(pixmap);
 
127
        drag->setHotSpot(QPoint(15, 20));
 
128
    }
 
129
//! [7]
 
130
 
 
131
//! [8]
 
132
    drag->exec();
 
133
    setCursor(Qt::OpenHandCursor);
 
134
}
 
135
//! [8]
 
136
 
 
137
//! [4]
 
138
void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
 
139
{
 
140
    setCursor(Qt::OpenHandCursor);
 
141
}
 
142
//! [4]