| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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 documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example graphicsview/basicgraphicslayouts
|
|---|
| 44 | \title Basic Graphics Layouts Example
|
|---|
| 45 |
|
|---|
| 46 | The Basic Graphics Layouts example shows how to use the layout classes
|
|---|
| 47 | in QGraphicsView: QGraphicsLinearLayout and QGraphicsGridLayout.
|
|---|
| 48 | In addition to that it shows how to write your own custom layout item.
|
|---|
| 49 |
|
|---|
| 50 | \image basicgraphicslayouts-example.png Screenshot of the Basic Layouts Example
|
|---|
| 51 |
|
|---|
| 52 | \section1 Window Class Definition
|
|---|
| 53 |
|
|---|
| 54 | The \c Window class is a subclass of QGraphicsWidget. It has a
|
|---|
| 55 | constructor with a QGraphicsWidget \a parent as its parameter.
|
|---|
| 56 |
|
|---|
| 57 | \snippet examples/graphicsview/basicgraphicslayouts/window.h 0
|
|---|
| 58 |
|
|---|
| 59 | \section1 Window Class Implementation
|
|---|
| 60 |
|
|---|
| 61 | The constructor of \c Window instantiates a QGraphicsLinearLayout object,
|
|---|
| 62 | \c windowLayout, with vertical orientation. We instantiate another
|
|---|
| 63 | QGraphicsLinearLayout object, \c linear, whose parent is \c windowLayout.
|
|---|
| 64 | Next, we create a \c LayoutItem object, \c item and add it to \c linear
|
|---|
| 65 | with the \l{QGraphicsLinearLayout::}{addItem()} function. We also provide
|
|---|
| 66 | \c item with a \l{QGraphicsLinearLayout::setStretchFactor()}
|
|---|
| 67 | {stretchFactor}.
|
|---|
| 68 |
|
|---|
| 69 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 0
|
|---|
| 70 |
|
|---|
| 71 | We repeat the process:
|
|---|
| 72 |
|
|---|
| 73 | \list
|
|---|
| 74 | \o create a new \c LayoutItem,
|
|---|
| 75 | \o add the item \c linear, and
|
|---|
| 76 | \o provide a stretch factor.
|
|---|
| 77 | \endlist
|
|---|
| 78 |
|
|---|
| 79 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 1
|
|---|
| 80 |
|
|---|
| 81 | We then add \c linear to \c windowLayout, nesting two
|
|---|
| 82 | QGraphicsLinearLayout objects. Apart from the QGraphicsLinearLayout, we
|
|---|
| 83 | also use a QGraphicsGridLayout object, \c grid, which is a 4x3 grid with
|
|---|
| 84 | some cells spanning to other rows.
|
|---|
| 85 |
|
|---|
| 86 | We create seven \c LayoutItem objects and place them into \c grid with
|
|---|
| 87 | the \l{QGraphicsGridLayout::}{addItem()} function as shown in the code
|
|---|
| 88 | snippet below:
|
|---|
| 89 |
|
|---|
| 90 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 2
|
|---|
| 91 |
|
|---|
| 92 | The first item we add to \c grid is placed in the top left cell,
|
|---|
| 93 | spanning four rows. The next two items are placed in the second column,
|
|---|
| 94 | and they span two rows. Each item's \l{QGraphicsWidget::}{maximumHeight()}
|
|---|
| 95 | and \l{QGraphicsWidget::}{minimumHeight()} are set to be equal so that
|
|---|
| 96 | they do not expand vertically. As a result, these items will not
|
|---|
| 97 | fit vertically in their cells. So, we specify that they should be
|
|---|
| 98 | vertically aligned in the center of the cell using Qt::AlignVCenter.
|
|---|
| 99 |
|
|---|
| 100 | Finally, \c grid itself is added to \c windowLayout. Unlike
|
|---|
| 101 | QGridLayout::addItem(), QGraphicsGridLayout::addItem() requires a row
|
|---|
| 102 | and a column for its argument, specifying which cell the item should be
|
|---|
| 103 | positioned in. Also, if the \c rowSpan and \c columnSpan arguments
|
|---|
| 104 | are omitted, they will default to 1.
|
|---|
| 105 |
|
|---|
| 106 | Note that we do not specify a parent for each \c LayoutItem that we
|
|---|
| 107 | construct, as all these items will be added to \c windowLayout. When we
|
|---|
| 108 | add an item to a layout, it will be automatically reparented to the widget
|
|---|
| 109 | on which the layout is installed.
|
|---|
| 110 |
|
|---|
| 111 | \snippet examples/graphicsview/basicgraphicslayouts/window.cpp 3
|
|---|
| 112 |
|
|---|
| 113 | Now that we have set up \c grid and added it to \c windowLayout, we
|
|---|
| 114 | install \c windowLayout onto the window object using
|
|---|
| 115 | QGraphicsWidget::setLayout() and we set the window title.
|
|---|
| 116 |
|
|---|
| 117 | \section1 LayoutItem Class Definition
|
|---|
| 118 |
|
|---|
| 119 | The \c LayoutItem class is a subclass of QGraphicsLayoutItem and
|
|---|
| 120 | QGraphicsItem. It has a constructor, a destructor, and some required
|
|---|
| 121 | reimplementations.
|
|---|
| 122 | Since it inherits QGraphicsLayoutItem it must reimplement
|
|---|
| 123 | {QGraphicsLayoutItem::setGeometry()}{setGeometry()} and
|
|---|
| 124 | {QGraphicsLayoutItem::sizeHint()}{sizeHint()}.
|
|---|
| 125 | In addition to that it inherits QGraphicsItem, so it must reimplement
|
|---|
| 126 | {QGraphicsItem::boundingRect()}{boundingRect()} and
|
|---|
| 127 | {QGraphicsItem::paint()}{paint()}.
|
|---|
| 128 |
|
|---|
| 129 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.h 0
|
|---|
| 130 |
|
|---|
| 131 | The \c LayoutItem class also has a private instance of QPixmap, \c m_pix.
|
|---|
| 132 |
|
|---|
| 133 | \section1 LayoutItem Class Implementation
|
|---|
| 134 |
|
|---|
| 135 | In \c{LayoutItem}'s constructor, \c m_pix is instantiated and the
|
|---|
| 136 | \c{block.png} image is loaded into it.
|
|---|
| 137 |
|
|---|
| 138 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 0
|
|---|
| 139 |
|
|---|
| 140 | We use the Q_UNUSED() macro to prevent the compiler from generating
|
|---|
| 141 | warnings regarding unused parameters.
|
|---|
| 142 |
|
|---|
| 143 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 1
|
|---|
| 144 |
|
|---|
| 145 | The idea behind the \c paint() function is to paint the
|
|---|
| 146 | background rect then paint a rect around the pixmap.
|
|---|
| 147 |
|
|---|
| 148 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 2
|
|---|
| 149 |
|
|---|
| 150 | The reimplementation of {QGraphicsItem::boundingRect()}{boundingRect()}
|
|---|
| 151 | will set the top left corner at (0,0), and the size of it will be
|
|---|
| 152 | the size of the layout items
|
|---|
| 153 | {QGraphicsLayoutItem::geometry()}{geometry()}. This is the area that
|
|---|
| 154 | we paint within.
|
|---|
| 155 |
|
|---|
| 156 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 3
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | The reimplementation of {QGraphicsLayoutItem::setGeometry()}{setGeometry()}
|
|---|
| 160 | simply calls its baseclass implementation. However, since this will change
|
|---|
| 161 | the boundingRect we must also call
|
|---|
| 162 | {QGraphicsItem::prepareGeometryChange()}{prepareGeometryChange()}.
|
|---|
| 163 | Finally, we move the item according to \c geom.topLeft().
|
|---|
| 164 |
|
|---|
| 165 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 4
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | Since we don't want the size of the item to be smaller than the pixmap, we
|
|---|
| 169 | must make sure that we return a size hint that is larger than \c m_pix.
|
|---|
| 170 | We also add some extra space around for borders that we will paint later.
|
|---|
| 171 | Alternatively, you could scale the pixmap to prevent the item from
|
|---|
| 172 | becoming smaller than the pixmap.
|
|---|
| 173 | The preferred size is the same as the minimum size hint, while we set
|
|---|
| 174 | maximum to be a large value
|
|---|
| 175 |
|
|---|
| 176 | \snippet examples/graphicsview/basicgraphicslayouts/layoutitem.cpp 5
|
|---|
| 177 |
|
|---|
| 178 | */
|
|---|