| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include "layoutitem.h"
|
|---|
| 42 |
|
|---|
| 43 | //! [0]
|
|---|
| 44 | LayoutItem::LayoutItem(QGraphicsItem *parent/* = 0*/)
|
|---|
| 45 | : QGraphicsLayoutItem(), QGraphicsItem(parent)
|
|---|
| 46 | {
|
|---|
| 47 | m_pix = new QPixmap(QLatin1String(":/images/block.png"));
|
|---|
| 48 | setGraphicsItem(this);
|
|---|
| 49 | }
|
|---|
| 50 | //! [0]
|
|---|
| 51 |
|
|---|
| 52 | LayoutItem::~LayoutItem()
|
|---|
| 53 | {
|
|---|
| 54 | delete m_pix;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | //! [1]
|
|---|
| 58 | void LayoutItem::paint(QPainter *painter,
|
|---|
| 59 | const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/)
|
|---|
| 60 | {
|
|---|
| 61 | Q_UNUSED(widget);
|
|---|
| 62 | Q_UNUSED(option);
|
|---|
| 63 |
|
|---|
| 64 | QRectF frame(QPointF(0,0), geometry().size());
|
|---|
| 65 | qreal w = m_pix->width();
|
|---|
| 66 | qreal h = m_pix->height();
|
|---|
| 67 | QGradientStops stops;
|
|---|
| 68 | //! [1]
|
|---|
| 69 |
|
|---|
| 70 | //! [2]
|
|---|
| 71 | // paint a background rect (with gradient)
|
|---|
| 72 | QLinearGradient gradient(frame.topLeft(), frame.topLeft() + QPointF(200,200));
|
|---|
| 73 | stops << QGradientStop(0.0, QColor(60, 60, 60));
|
|---|
| 74 | stops << QGradientStop(frame.height()/2/frame.height(), QColor(102, 176, 54));
|
|---|
| 75 |
|
|---|
| 76 | //stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195, 55));
|
|---|
| 77 | stops << QGradientStop(1.0, QColor(215, 215, 215));
|
|---|
| 78 | gradient.setStops(stops);
|
|---|
| 79 | painter->setBrush(QBrush(gradient));
|
|---|
| 80 | painter->drawRoundedRect(frame, 10.0, 10.0);
|
|---|
| 81 |
|
|---|
| 82 | // paint a rect around the pixmap (with gradient)
|
|---|
| 83 | QPointF pixpos = frame.center() - (QPointF(w, h)/2);
|
|---|
| 84 | QRectF innerFrame(pixpos, QSizeF(w, h));
|
|---|
| 85 | innerFrame.adjust(-4, -4, +4, +4);
|
|---|
| 86 | gradient.setStart(innerFrame.topLeft());
|
|---|
| 87 | gradient.setFinalStop(innerFrame.bottomRight());
|
|---|
| 88 | stops.clear();
|
|---|
| 89 | stops << QGradientStop(0.0, QColor(215, 255, 200));
|
|---|
| 90 | stops << QGradientStop(0.5, QColor(102, 176, 54));
|
|---|
| 91 | stops << QGradientStop(1.0, QColor(0, 0, 0));
|
|---|
| 92 | gradient.setStops(stops);
|
|---|
| 93 | painter->setBrush(QBrush(gradient));
|
|---|
| 94 | painter->drawRoundedRect(innerFrame, 10.0, 10.0);
|
|---|
| 95 | painter->drawPixmap(pixpos, *m_pix);
|
|---|
| 96 | }
|
|---|
| 97 | //! [2]
|
|---|
| 98 |
|
|---|
| 99 | //! [3]
|
|---|
| 100 | QRectF LayoutItem::boundingRect() const
|
|---|
| 101 | {
|
|---|
| 102 | return QRectF(QPointF(0,0), geometry().size());
|
|---|
| 103 | }
|
|---|
| 104 | //! [3]
|
|---|
| 105 |
|
|---|
| 106 | //! [4]
|
|---|
| 107 | void LayoutItem::setGeometry(const QRectF &geom)
|
|---|
| 108 | {
|
|---|
| 109 | prepareGeometryChange();
|
|---|
| 110 | QGraphicsLayoutItem::setGeometry(geom);
|
|---|
| 111 | setPos(geom.topLeft());
|
|---|
| 112 | }
|
|---|
| 113 | //! [4]
|
|---|
| 114 |
|
|---|
| 115 | //! [5]
|
|---|
| 116 | QSizeF LayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
|
|---|
| 117 | {
|
|---|
| 118 | switch (which) {
|
|---|
| 119 | case Qt::MinimumSize:
|
|---|
| 120 | case Qt::PreferredSize:
|
|---|
| 121 | // Do not allow a size smaller than the pixmap with two frames around it.
|
|---|
| 122 | return m_pix->size() + QSize(12, 12);
|
|---|
| 123 | case Qt::MaximumSize:
|
|---|
| 124 | return QSizeF(1000,1000);
|
|---|
| 125 | default:
|
|---|
| 126 | break;
|
|---|
| 127 | }
|
|---|
| 128 | return constraint;
|
|---|
| 129 | }
|
|---|
| 130 | //! [5]
|
|---|