examples/graphicsview/basicgraphicslayouts/layoutitem.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "layoutitem.h"
       
    43 
       
    44 //! [0]
       
    45 LayoutItem::LayoutItem(QGraphicsItem *parent/* = 0*/)
       
    46     : QGraphicsLayoutItem(), QGraphicsItem(parent)
       
    47 {
       
    48     m_pix = new QPixmap(QLatin1String(":/images/block.png"));
       
    49     setGraphicsItem(this);
       
    50 }
       
    51 //! [0]
       
    52 
       
    53 LayoutItem::~LayoutItem()
       
    54 {
       
    55     delete m_pix;
       
    56 }
       
    57 
       
    58 //! [1]
       
    59 void LayoutItem::paint(QPainter *painter,
       
    60     const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/)
       
    61 {
       
    62     Q_UNUSED(widget);
       
    63     Q_UNUSED(option);
       
    64 
       
    65     QRectF frame(QPointF(0,0), geometry().size());
       
    66     qreal w = m_pix->width();
       
    67     qreal h = m_pix->height();
       
    68     QGradientStops stops;
       
    69 //! [1]
       
    70 
       
    71 //! [2]
       
    72     // paint a background rect (with gradient)
       
    73     QLinearGradient gradient(frame.topLeft(), frame.topLeft() + QPointF(200,200));
       
    74     stops << QGradientStop(0.0, QColor(60, 60,  60));
       
    75     stops << QGradientStop(frame.height()/2/frame.height(), QColor(102, 176, 54));
       
    76 
       
    77     //stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195,  55));
       
    78     stops << QGradientStop(1.0, QColor(215, 215, 215));
       
    79     gradient.setStops(stops);
       
    80     painter->setBrush(QBrush(gradient));
       
    81     painter->drawRoundedRect(frame, 10.0, 10.0);
       
    82 
       
    83     // paint a rect around the pixmap (with gradient)
       
    84     QPointF pixpos = frame.center() - (QPointF(w, h)/2);
       
    85     QRectF innerFrame(pixpos, QSizeF(w, h));
       
    86     innerFrame.adjust(-4, -4, +4, +4);
       
    87     gradient.setStart(innerFrame.topLeft());
       
    88     gradient.setFinalStop(innerFrame.bottomRight());
       
    89     stops.clear();
       
    90     stops << QGradientStop(0.0, QColor(215, 255, 200));
       
    91     stops << QGradientStop(0.5, QColor(102, 176, 54));
       
    92     stops << QGradientStop(1.0, QColor(0, 0,  0));
       
    93     gradient.setStops(stops);
       
    94     painter->setBrush(QBrush(gradient));
       
    95     painter->drawRoundedRect(innerFrame, 10.0, 10.0);
       
    96     painter->drawPixmap(pixpos, *m_pix);
       
    97 }
       
    98 //! [2]
       
    99 
       
   100 //! [3]
       
   101 QRectF LayoutItem::boundingRect() const
       
   102 {
       
   103     return QRectF(QPointF(0,0), geometry().size());
       
   104 }
       
   105 //! [3]
       
   106 
       
   107 //! [4]
       
   108 void LayoutItem::setGeometry(const QRectF &geom)
       
   109 {
       
   110     prepareGeometryChange();
       
   111     QGraphicsLayoutItem::setGeometry(geom);
       
   112     setPos(geom.topLeft());
       
   113 }
       
   114 //! [4]
       
   115 
       
   116 //! [5]
       
   117 QSizeF LayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   118 {
       
   119     switch (which) {
       
   120     case Qt::MinimumSize:
       
   121     case Qt::PreferredSize:
       
   122         // Do not allow a size smaller than the pixmap with two frames around it.
       
   123         return m_pix->size() + QSize(12, 12);
       
   124     case Qt::MaximumSize:
       
   125         return QSizeF(1000,1000);
       
   126     default:
       
   127         break;
       
   128     }
       
   129     return constraint;
       
   130 }
       
   131 //! [5]