| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/graphicsview/qgraphicslinearlayout.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | /*! | - | ||||||||||||
| 41 | \class QGraphicsLinearLayout | - | ||||||||||||
| 42 | \brief The QGraphicsLinearLayout class provides a horizontal or vertical | - | ||||||||||||
| 43 | layout for managing widgets in Graphics View. | - | ||||||||||||
| 44 | \since 4.4 | - | ||||||||||||
| 45 | \ingroup graphicsview-api | - | ||||||||||||
| 46 | \inmodule QtWidgets | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | The default orientation for a linear layout is Qt::Horizontal. You can | - | ||||||||||||
| 49 | choose a vertical orientation either by calling setOrientation(), or by | - | ||||||||||||
| 50 | passing Qt::Vertical to QGraphicsLinearLayout's constructor. | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | The most common way to use QGraphicsLinearLayout is to construct an object | - | ||||||||||||
| 53 | on the heap with no parent, add widgets and layouts by calling addItem(), | - | ||||||||||||
| 54 | and finally assign the layout to a widget by calling | - | ||||||||||||
| 55 | QGraphicsWidget::setLayout(). | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | \snippet code/src_gui_graphicsview_qgraphicslinearlayout.cpp 0 | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | You can add widgets, layouts, stretches (addStretch(), insertStretch() or | - | ||||||||||||
| 60 | setStretchFactor()), and spacings (setItemSpacing()) to a linear | - | ||||||||||||
| 61 | layout. The layout takes ownership of the items. In some cases when the layout | - | ||||||||||||
| 62 | item also inherits from QGraphicsItem (such as QGraphicsWidget) there will be a | - | ||||||||||||
| 63 | ambiguity in ownership because the layout item belongs to two ownership hierarchies. | - | ||||||||||||
| 64 | See the documentation of QGraphicsLayoutItem::setOwnedByLayout() how to handle | - | ||||||||||||
| 65 | this. | - | ||||||||||||
| 66 | You can access each item in the layout by calling count() and itemAt(). Calling | - | ||||||||||||
| 67 | removeAt() or removeItem() will remove an item from the layout, without | - | ||||||||||||
| 68 | destroying it. | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | \section1 Size Hints and Size Policies in QGraphicsLinearLayout | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | QGraphicsLinearLayout respects each item's size hints and size policies, | - | ||||||||||||
| 73 | and when the layout contains more space than the items can fill, each item | - | ||||||||||||
| 74 | is arranged according to the layout's alignment for that item. You can set | - | ||||||||||||
| 75 | an alignment for each item by calling setAlignment(), and check the | - | ||||||||||||
| 76 | alignment for any item by calling alignment(). By default, items are | - | ||||||||||||
| 77 | aligned to the top left. | - | ||||||||||||
| 78 | - | |||||||||||||
| 79 | \section1 Spacing within QGraphicsLinearLayout | - | ||||||||||||
| 80 | - | |||||||||||||
| 81 | Between the items, the layout distributes some space. The actual amount of | - | ||||||||||||
| 82 | space depends on the managed widget's current style, but the common | - | ||||||||||||
| 83 | spacing is 4. You can also set your own spacing by calling setSpacing(), | - | ||||||||||||
| 84 | and get the current spacing value by calling spacing(). If you want to | - | ||||||||||||
| 85 | configure individual spacing for your items, you can call setItemSpacing(). | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | \section1 Stretch Factor in QGraphicsLinearLayout | - | ||||||||||||
| 88 | - | |||||||||||||
| 89 | You can assign a stretch factor to each item to control how much space it | - | ||||||||||||
| 90 | will get compared to the other items. By default, two identical widgets | - | ||||||||||||
| 91 | arranged in a linear layout will have the same size, but if the first | - | ||||||||||||
| 92 | widget has a stretch factor of 1 and the second widget has a stretch | - | ||||||||||||
| 93 | factor of 2, the first widget will get 1/3 of the available space, and the | - | ||||||||||||
| 94 | second will get 2/3. | - | ||||||||||||
| 95 | - | |||||||||||||
| 96 | QGraphicsLinearLayout calculates the distribution of sizes by adding up | - | ||||||||||||
| 97 | the stretch factors of all items, and then dividing the available space | - | ||||||||||||
| 98 | accordingly. The default stretch factor is 0 for all items; a factor of 0 | - | ||||||||||||
| 99 | means the item does not have any defined stretch factor; effectively this | - | ||||||||||||
| 100 | is the same as setting the stretch factor to 1. The stretch factor only | - | ||||||||||||
| 101 | applies to the available space in the lengthwise direction of the layout | - | ||||||||||||
| 102 | (following its orientation). If you want to control both the item's | - | ||||||||||||
| 103 | horizontal and vertical stretch, you can use QGraphicsGridLayout instead. | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | \section1 QGraphicsLinearLayout Compared to Other Layouts | - | ||||||||||||
| 106 | - | |||||||||||||
| 107 | QGraphicsLinearLayout is very similar to QVBoxLayout and QHBoxLayout, but | - | ||||||||||||
| 108 | in contrast to these classes, it is used to manage QGraphicsWidget and | - | ||||||||||||
| 109 | QGraphicsLayout instead of QWidget and QLayout. | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | \sa QGraphicsGridLayout, QGraphicsWidget | - | ||||||||||||
| 112 | */ | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | #include "qapplication.h" | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 117 | - | |||||||||||||
| 118 | #include "qwidget.h" | - | ||||||||||||
| 119 | #include "qgraphicslayout_p.h" | - | ||||||||||||
| 120 | #include "qgraphicslayoutitem.h" | - | ||||||||||||
| 121 | #include "qgraphicslinearlayout.h" | - | ||||||||||||
| 122 | #include "qgraphicswidget.h" | - | ||||||||||||
| 123 | #include "qgraphicsgridlayoutengine_p.h" | - | ||||||||||||
| 124 | #include "qgraphicslayoutstyleinfo_p.h" | - | ||||||||||||
| 125 | #include "qscopedpointer.h" | - | ||||||||||||
| 126 | #ifdef QT_DEBUG | - | ||||||||||||
| 127 | #include <QtCore/qdebug.h> | - | ||||||||||||
| 128 | #endif | - | ||||||||||||
| 129 | - | |||||||||||||
| 130 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 131 | - | |||||||||||||
| 132 | class QGraphicsLinearLayoutPrivate : public QGraphicsLayoutPrivate | - | ||||||||||||
| 133 | { | - | ||||||||||||
| 134 | public: | - | ||||||||||||
| 135 | QGraphicsLinearLayoutPrivate(Qt::Orientation orientation) | - | ||||||||||||
| 136 | : orientation(orientation) | - | ||||||||||||
| 137 | { } never executed: end of block | 0 | ||||||||||||
| 138 | - | |||||||||||||
| 139 | void removeGridItem(QGridLayoutItem *gridItem); | - | ||||||||||||
| 140 | QGraphicsLayoutStyleInfo *styleInfo() const; | - | ||||||||||||
| 141 | void fixIndex(int *index) const; | - | ||||||||||||
| 142 | int gridRow(int index) const; | - | ||||||||||||
| 143 | int gridColumn(int index) const; | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | Qt::Orientation orientation; | - | ||||||||||||
| 146 | mutable QScopedPointer<QGraphicsLayoutStyleInfo> m_styleInfo; | - | ||||||||||||
| 147 | QGraphicsGridLayoutEngine engine; | - | ||||||||||||
| 148 | }; | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | void QGraphicsLinearLayoutPrivate::removeGridItem(QGridLayoutItem *gridItem) | - | ||||||||||||
| 151 | { | - | ||||||||||||
| 152 | int index = gridItem->firstRow(orientation); | - | ||||||||||||
| 153 | engine.removeItem(gridItem); | - | ||||||||||||
| 154 | engine.removeRows(index, 1, orientation); | - | ||||||||||||
| 155 | } never executed: end of block | 0 | ||||||||||||
| 156 | - | |||||||||||||
| 157 | void QGraphicsLinearLayoutPrivate::fixIndex(int *index) const | - | ||||||||||||
| 158 | { | - | ||||||||||||
| 159 | int count = engine.rowCount(orientation); | - | ||||||||||||
| 160 | if (uint(*index) > uint(count))
| 0 | ||||||||||||
| 161 | *index = count; never executed: *index = count; | 0 | ||||||||||||
| 162 | } never executed: end of block | 0 | ||||||||||||
| 163 | - | |||||||||||||
| 164 | int QGraphicsLinearLayoutPrivate::gridRow(int index) const | - | ||||||||||||
| 165 | { | - | ||||||||||||
| 166 | if (orientation == Qt::Horizontal)
| 0 | ||||||||||||
| 167 | return 0; never executed: return 0; | 0 | ||||||||||||
| 168 | return int(qMin(uint(index), uint(engine.rowCount()))); never executed: return int(qMin(uint(index), uint(engine.rowCount()))); | 0 | ||||||||||||
| 169 | } | - | ||||||||||||
| 170 | - | |||||||||||||
| 171 | int QGraphicsLinearLayoutPrivate::gridColumn(int index) const | - | ||||||||||||
| 172 | { | - | ||||||||||||
| 173 | if (orientation == Qt::Vertical)
| 0 | ||||||||||||
| 174 | return 0; never executed: return 0; | 0 | ||||||||||||
| 175 | return int(qMin(uint(index), uint(engine.columnCount()))); never executed: return int(qMin(uint(index), uint(engine.columnCount()))); | 0 | ||||||||||||
| 176 | } | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | QGraphicsLayoutStyleInfo *QGraphicsLinearLayoutPrivate::styleInfo() const | - | ||||||||||||
| 179 | { | - | ||||||||||||
| 180 | if (!m_styleInfo)
| 0 | ||||||||||||
| 181 | m_styleInfo.reset(new QGraphicsLayoutStyleInfo(this)); never executed: m_styleInfo.reset(new QGraphicsLayoutStyleInfo(this)); | 0 | ||||||||||||
| 182 | return m_styleInfo.data(); never executed: return m_styleInfo.data(); | 0 | ||||||||||||
| 183 | } | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | /*! | - | ||||||||||||
| 186 | Constructs a QGraphicsLinearLayout instance. You can pass the | - | ||||||||||||
| 187 | \a orientation for the layout, either horizontal or vertical, and | - | ||||||||||||
| 188 | \a parent is passed to QGraphicsLayout's constructor. | - | ||||||||||||
| 189 | */ | - | ||||||||||||
| 190 | QGraphicsLinearLayout::QGraphicsLinearLayout(Qt::Orientation orientation, QGraphicsLayoutItem *parent) | - | ||||||||||||
| 191 | : QGraphicsLayout(*new QGraphicsLinearLayoutPrivate(orientation), parent) | - | ||||||||||||
| 192 | { | - | ||||||||||||
| 193 | } never executed: end of block | 0 | ||||||||||||
| 194 | - | |||||||||||||
| 195 | /*! | - | ||||||||||||
| 196 | Constructs a QGraphicsLinearLayout instance using Qt::Horizontal | - | ||||||||||||
| 197 | orientation. \a parent is passed to QGraphicsLayout's constructor. | - | ||||||||||||
| 198 | */ | - | ||||||||||||
| 199 | QGraphicsLinearLayout::QGraphicsLinearLayout(QGraphicsLayoutItem *parent) | - | ||||||||||||
| 200 | : QGraphicsLayout(*new QGraphicsLinearLayoutPrivate(Qt::Horizontal), parent) | - | ||||||||||||
| 201 | { | - | ||||||||||||
| 202 | } never executed: end of block | 0 | ||||||||||||
| 203 | - | |||||||||||||
| 204 | /*! | - | ||||||||||||
| 205 | Destroys the QGraphicsLinearLayout object. | - | ||||||||||||
| 206 | */ | - | ||||||||||||
| 207 | QGraphicsLinearLayout::~QGraphicsLinearLayout() | - | ||||||||||||
| 208 | { | - | ||||||||||||
| 209 | for (int i = count() - 1; i >= 0; --i) {
| 0 | ||||||||||||
| 210 | QGraphicsLayoutItem *item = itemAt(i); | - | ||||||||||||
| 211 | // The following lines can be removed, but this removes the item | - | ||||||||||||
| 212 | // from the layout more efficiently than the implementation of | - | ||||||||||||
| 213 | // ~QGraphicsLayoutItem. | - | ||||||||||||
| 214 | removeAt(i); | - | ||||||||||||
| 215 | if (item) {
| 0 | ||||||||||||
| 216 | item->setParentLayoutItem(0); | - | ||||||||||||
| 217 | if (item->ownedByLayout())
| 0 | ||||||||||||
| 218 | delete item; never executed: delete item; | 0 | ||||||||||||
| 219 | } never executed: end of block | 0 | ||||||||||||
| 220 | } never executed: end of block | 0 | ||||||||||||
| 221 | } never executed: end of block | 0 | ||||||||||||
| 222 | - | |||||||||||||
| 223 | /*! | - | ||||||||||||
| 224 | Change the layout orientation to \a orientation. Changing the layout | - | ||||||||||||
| 225 | orientation will automatically invalidate the layout. | - | ||||||||||||
| 226 | - | |||||||||||||
| 227 | \sa orientation() | - | ||||||||||||
| 228 | */ | - | ||||||||||||
| 229 | void QGraphicsLinearLayout::setOrientation(Qt::Orientation orientation) | - | ||||||||||||
| 230 | { | - | ||||||||||||
| 231 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 232 | if (orientation != d->orientation) {
| 0 | ||||||||||||
| 233 | d->engine.transpose(); | - | ||||||||||||
| 234 | d->orientation = orientation; | - | ||||||||||||
| 235 | invalidate(); | - | ||||||||||||
| 236 | } never executed: end of block | 0 | ||||||||||||
| 237 | } never executed: end of block | 0 | ||||||||||||
| 238 | - | |||||||||||||
| 239 | /*! | - | ||||||||||||
| 240 | Returns the layout orientation. | - | ||||||||||||
| 241 | \sa setOrientation() | - | ||||||||||||
| 242 | */ | - | ||||||||||||
| 243 | Qt::Orientation QGraphicsLinearLayout::orientation() const | - | ||||||||||||
| 244 | { | - | ||||||||||||
| 245 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 246 | return d->orientation; never executed: return d->orientation; | 0 | ||||||||||||
| 247 | } | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | /*! | - | ||||||||||||
| 250 | \fn void QGraphicsLinearLayout::addItem(QGraphicsLayoutItem *item) | - | ||||||||||||
| 251 | - | |||||||||||||
| 252 | This convenience function is equivalent to calling | - | ||||||||||||
| 253 | insertItem(-1, \a item). | - | ||||||||||||
| 254 | */ | - | ||||||||||||
| 255 | - | |||||||||||||
| 256 | /*! | - | ||||||||||||
| 257 | \fn void QGraphicsLinearLayout::addStretch(int stretch) | - | ||||||||||||
| 258 | - | |||||||||||||
| 259 | This convenience function is equivalent to calling | - | ||||||||||||
| 260 | insertStretch(-1, \a stretch). | - | ||||||||||||
| 261 | */ | - | ||||||||||||
| 262 | - | |||||||||||||
| 263 | /*! | - | ||||||||||||
| 264 | Inserts \a item into the layout at \a index, or before any item that is | - | ||||||||||||
| 265 | currently at \a index. | - | ||||||||||||
| 266 | - | |||||||||||||
| 267 | \sa addItem(), itemAt(), insertStretch(), setItemSpacing() | - | ||||||||||||
| 268 | */ | - | ||||||||||||
| 269 | void QGraphicsLinearLayout::insertItem(int index, QGraphicsLayoutItem *item) | - | ||||||||||||
| 270 | { | - | ||||||||||||
| 271 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 272 | if (!item) {
| 0 | ||||||||||||
| 273 | qWarning("QGraphicsLinearLayout::insertItem: cannot insert null item"); | - | ||||||||||||
| 274 | return; never executed: return; | 0 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | if (item == this) {
| 0 | ||||||||||||
| 277 | qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself"); | - | ||||||||||||
| 278 | return; never executed: return; | 0 | ||||||||||||
| 279 | } | - | ||||||||||||
| 280 | d->addChildLayoutItem(item); | - | ||||||||||||
| 281 | - | |||||||||||||
| 282 | Q_ASSERT(item); | - | ||||||||||||
| 283 | d->fixIndex(&index); | - | ||||||||||||
| 284 | d->engine.insertRow(index, d->orientation); | - | ||||||||||||
| 285 | QGraphicsGridLayoutEngineItem *gridEngineItem = new QGraphicsGridLayoutEngineItem(item, d->gridRow(index), d->gridColumn(index), 1, 1, 0); | - | ||||||||||||
| 286 | d->engine.insertItem(gridEngineItem, index); | - | ||||||||||||
| 287 | invalidate(); | - | ||||||||||||
| 288 | } never executed: end of block | 0 | ||||||||||||
| 289 | - | |||||||||||||
| 290 | /*! | - | ||||||||||||
| 291 | Inserts a stretch of \a stretch at \a index, or before any item that is | - | ||||||||||||
| 292 | currently at \a index. | - | ||||||||||||
| 293 | - | |||||||||||||
| 294 | \sa addStretch(), setStretchFactor(), setItemSpacing(), insertItem() | - | ||||||||||||
| 295 | */ | - | ||||||||||||
| 296 | void QGraphicsLinearLayout::insertStretch(int index, int stretch) | - | ||||||||||||
| 297 | { | - | ||||||||||||
| 298 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 299 | d->fixIndex(&index); | - | ||||||||||||
| 300 | d->engine.insertRow(index, d->orientation); | - | ||||||||||||
| 301 | d->engine.setRowStretchFactor(index, stretch, d->orientation); | - | ||||||||||||
| 302 | invalidate(); | - | ||||||||||||
| 303 | } never executed: end of block | 0 | ||||||||||||
| 304 | - | |||||||||||||
| 305 | /*! | - | ||||||||||||
| 306 | Removes \a item from the layout without destroying it. Ownership of | - | ||||||||||||
| 307 | \a item is transferred to the caller. | - | ||||||||||||
| 308 | - | |||||||||||||
| 309 | \sa removeAt(), insertItem() | - | ||||||||||||
| 310 | */ | - | ||||||||||||
| 311 | void QGraphicsLinearLayout::removeItem(QGraphicsLayoutItem *item) | - | ||||||||||||
| 312 | { | - | ||||||||||||
| 313 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 314 | if (QGraphicsGridLayoutEngineItem *gridItem = d->engine.findLayoutItem(item)) {
| 0 | ||||||||||||
| 315 | item->setParentLayoutItem(0); | - | ||||||||||||
| 316 | d->removeGridItem(gridItem); | - | ||||||||||||
| 317 | delete gridItem; | - | ||||||||||||
| 318 | invalidate(); | - | ||||||||||||
| 319 | } never executed: end of block | 0 | ||||||||||||
| 320 | } never executed: end of block | 0 | ||||||||||||
| 321 | - | |||||||||||||
| 322 | /*! | - | ||||||||||||
| 323 | Removes the item at \a index without destroying it. Ownership of the item | - | ||||||||||||
| 324 | is transferred to the caller. | - | ||||||||||||
| 325 | - | |||||||||||||
| 326 | \sa removeItem(), insertItem() | - | ||||||||||||
| 327 | */ | - | ||||||||||||
| 328 | void QGraphicsLinearLayout::removeAt(int index) | - | ||||||||||||
| 329 | { | - | ||||||||||||
| 330 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 331 | if (index < 0 || index >= d->engine.itemCount()) {
| 0 | ||||||||||||
| 332 | qWarning("QGraphicsLinearLayout::removeAt: invalid index %d", index); | - | ||||||||||||
| 333 | return; never executed: return; | 0 | ||||||||||||
| 334 | } | - | ||||||||||||
| 335 | - | |||||||||||||
| 336 | if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(index))) {
| 0 | ||||||||||||
| 337 | if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
| 0 | ||||||||||||
| 338 | layoutItem->setParentLayoutItem(0); never executed: layoutItem->setParentLayoutItem(0); | 0 | ||||||||||||
| 339 | d->removeGridItem(gridItem); | - | ||||||||||||
| 340 | delete gridItem; | - | ||||||||||||
| 341 | invalidate(); | - | ||||||||||||
| 342 | } never executed: end of block | 0 | ||||||||||||
| 343 | } never executed: end of block | 0 | ||||||||||||
| 344 | - | |||||||||||||
| 345 | /*! | - | ||||||||||||
| 346 | Sets the layout's spacing to \a spacing. Spacing refers to the | - | ||||||||||||
| 347 | vertical and horizontal distances between items. | - | ||||||||||||
| 348 | - | |||||||||||||
| 349 | \sa setItemSpacing(), setStretchFactor(), QGraphicsGridLayout::setSpacing() | - | ||||||||||||
| 350 | */ | - | ||||||||||||
| 351 | void QGraphicsLinearLayout::setSpacing(qreal spacing) | - | ||||||||||||
| 352 | { | - | ||||||||||||
| 353 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 354 | if (spacing < 0) {
| 0 | ||||||||||||
| 355 | qWarning("QGraphicsLinearLayout::setSpacing: invalid spacing %g", spacing); | - | ||||||||||||
| 356 | return; never executed: return; | 0 | ||||||||||||
| 357 | } | - | ||||||||||||
| 358 | d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical); | - | ||||||||||||
| 359 | invalidate(); | - | ||||||||||||
| 360 | } never executed: end of block | 0 | ||||||||||||
| 361 | - | |||||||||||||
| 362 | /*! | - | ||||||||||||
| 363 | Returns the layout's spacing. Spacing refers to the | - | ||||||||||||
| 364 | vertical and horizontal distances between items. | - | ||||||||||||
| 365 | - | |||||||||||||
| 366 | \sa setSpacing() | - | ||||||||||||
| 367 | */ | - | ||||||||||||
| 368 | qreal QGraphicsLinearLayout::spacing() const | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 371 | return d->engine.spacing(d->orientation, d->styleInfo()); never executed: return d->engine.spacing(d->orientation, d->styleInfo()); | 0 | ||||||||||||
| 372 | } | - | ||||||||||||
| 373 | - | |||||||||||||
| 374 | /*! | - | ||||||||||||
| 375 | Sets the spacing after item at \a index to \a spacing. | - | ||||||||||||
| 376 | */ | - | ||||||||||||
| 377 | void QGraphicsLinearLayout::setItemSpacing(int index, qreal spacing) | - | ||||||||||||
| 378 | { | - | ||||||||||||
| 379 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 380 | d->engine.setRowSpacing(index, spacing, d->orientation); | - | ||||||||||||
| 381 | invalidate(); | - | ||||||||||||
| 382 | } never executed: end of block | 0 | ||||||||||||
| 383 | /*! | - | ||||||||||||
| 384 | Returns the spacing after item at \a index. | - | ||||||||||||
| 385 | */ | - | ||||||||||||
| 386 | qreal QGraphicsLinearLayout::itemSpacing(int index) const | - | ||||||||||||
| 387 | { | - | ||||||||||||
| 388 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 389 | return d->engine.rowSpacing(index, d->orientation); never executed: return d->engine.rowSpacing(index, d->orientation); | 0 | ||||||||||||
| 390 | } | - | ||||||||||||
| 391 | - | |||||||||||||
| 392 | /*! | - | ||||||||||||
| 393 | Sets the stretch factor for \a item to \a stretch. If an item's stretch | - | ||||||||||||
| 394 | factor changes, this function will invalidate the layout. | - | ||||||||||||
| 395 | - | |||||||||||||
| 396 | Setting \a stretch to 0 removes the stretch factor from the item, and is | - | ||||||||||||
| 397 | effectively equivalent to setting \a stretch to 1. | - | ||||||||||||
| 398 | - | |||||||||||||
| 399 | \sa stretchFactor() | - | ||||||||||||
| 400 | */ | - | ||||||||||||
| 401 | void QGraphicsLinearLayout::setStretchFactor(QGraphicsLayoutItem *item, int stretch) | - | ||||||||||||
| 402 | { | - | ||||||||||||
| 403 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 404 | if (!item) {
| 0 | ||||||||||||
| 405 | qWarning("QGraphicsLinearLayout::setStretchFactor: cannot assign" | - | ||||||||||||
| 406 | " a stretch factor to a null item"); | - | ||||||||||||
| 407 | return; never executed: return; | 0 | ||||||||||||
| 408 | } | - | ||||||||||||
| 409 | if (stretchFactor(item) == stretch)
| 0 | ||||||||||||
| 410 | return; never executed: return; | 0 | ||||||||||||
| 411 | d->engine.setStretchFactor(item, stretch, d->orientation); | - | ||||||||||||
| 412 | invalidate(); | - | ||||||||||||
| 413 | } never executed: end of block | 0 | ||||||||||||
| 414 | - | |||||||||||||
| 415 | /*! | - | ||||||||||||
| 416 | Returns the stretch factor for \a item. The default stretch factor is 0, | - | ||||||||||||
| 417 | meaning that the item has no assigned stretch factor. | - | ||||||||||||
| 418 | - | |||||||||||||
| 419 | \sa setStretchFactor() | - | ||||||||||||
| 420 | */ | - | ||||||||||||
| 421 | int QGraphicsLinearLayout::stretchFactor(QGraphicsLayoutItem *item) const | - | ||||||||||||
| 422 | { | - | ||||||||||||
| 423 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 424 | if (!item) {
| 0 | ||||||||||||
| 425 | qWarning("QGraphicsLinearLayout::setStretchFactor: cannot return" | - | ||||||||||||
| 426 | " a stretch factor for a null item"); | - | ||||||||||||
| 427 | return 0; never executed: return 0; | 0 | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | return d->engine.stretchFactor(item, d->orientation); never executed: return d->engine.stretchFactor(item, d->orientation); | 0 | ||||||||||||
| 430 | } | - | ||||||||||||
| 431 | - | |||||||||||||
| 432 | /*! | - | ||||||||||||
| 433 | Sets the alignment of \a item to \a alignment. If \a item's alignment | - | ||||||||||||
| 434 | changes, the layout is automatically invalidated. | - | ||||||||||||
| 435 | - | |||||||||||||
| 436 | \sa alignment(), invalidate() | - | ||||||||||||
| 437 | */ | - | ||||||||||||
| 438 | void QGraphicsLinearLayout::setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment) | - | ||||||||||||
| 439 | { | - | ||||||||||||
| 440 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 441 | if (this->alignment(item) == alignment)
| 0 | ||||||||||||
| 442 | return; never executed: return; | 0 | ||||||||||||
| 443 | d->engine.setAlignment(item, alignment); | - | ||||||||||||
| 444 | invalidate(); | - | ||||||||||||
| 445 | } never executed: end of block | 0 | ||||||||||||
| 446 | - | |||||||||||||
| 447 | /*! | - | ||||||||||||
| 448 | Returns the alignment for \a item. The default alignment is | - | ||||||||||||
| 449 | Qt::AlignTop | Qt::AlignLeft. | - | ||||||||||||
| 450 | - | |||||||||||||
| 451 | The alignment decides how the item is positioned within its assigned space | - | ||||||||||||
| 452 | in the case where there's more space available in the layout than the | - | ||||||||||||
| 453 | widgets can occupy. | - | ||||||||||||
| 454 | - | |||||||||||||
| 455 | \sa setAlignment() | - | ||||||||||||
| 456 | */ | - | ||||||||||||
| 457 | Qt::Alignment QGraphicsLinearLayout::alignment(QGraphicsLayoutItem *item) const | - | ||||||||||||
| 458 | { | - | ||||||||||||
| 459 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 460 | return d->engine.alignment(item); never executed: return d->engine.alignment(item); | 0 | ||||||||||||
| 461 | } | - | ||||||||||||
| 462 | - | |||||||||||||
| 463 | #if 0 // ### | - | ||||||||||||
| 464 | QSizePolicy::ControlTypes QGraphicsLinearLayout::controlTypes(LayoutSide side) const | - | ||||||||||||
| 465 | { | - | ||||||||||||
| 466 | return d->engine.controlTypes(side); | - | ||||||||||||
| 467 | } | - | ||||||||||||
| 468 | #endif | - | ||||||||||||
| 469 | - | |||||||||||||
| 470 | /*! | - | ||||||||||||
| 471 | \reimp | - | ||||||||||||
| 472 | */ | - | ||||||||||||
| 473 | int QGraphicsLinearLayout::count() const | - | ||||||||||||
| 474 | { | - | ||||||||||||
| 475 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 476 | return d->engine.itemCount(); never executed: return d->engine.itemCount(); | 0 | ||||||||||||
| 477 | } | - | ||||||||||||
| 478 | - | |||||||||||||
| 479 | /*! | - | ||||||||||||
| 480 | \reimp | - | ||||||||||||
| 481 | When iterating from 0 and up, it will return the items in the visual arranged order. | - | ||||||||||||
| 482 | */ | - | ||||||||||||
| 483 | QGraphicsLayoutItem *QGraphicsLinearLayout::itemAt(int index) const | - | ||||||||||||
| 484 | { | - | ||||||||||||
| 485 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 486 | if (index < 0 || index >= d->engine.itemCount()) {
| 0 | ||||||||||||
| 487 | qWarning("QGraphicsLinearLayout::itemAt: invalid index %d", index); | - | ||||||||||||
| 488 | return 0; never executed: return 0; | 0 | ||||||||||||
| 489 | } | - | ||||||||||||
| 490 | QGraphicsLayoutItem *item = 0; | - | ||||||||||||
| 491 | if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem *>(d->engine.itemAt(index)))
| 0 | ||||||||||||
| 492 | item = gridItem->layoutItem(); never executed: item = gridItem->layoutItem(); | 0 | ||||||||||||
| 493 | return item; never executed: return item; | 0 | ||||||||||||
| 494 | } | - | ||||||||||||
| 495 | - | |||||||||||||
| 496 | /*! | - | ||||||||||||
| 497 | \reimp | - | ||||||||||||
| 498 | */ | - | ||||||||||||
| 499 | void QGraphicsLinearLayout::setGeometry(const QRectF &rect) | - | ||||||||||||
| 500 | { | - | ||||||||||||
| 501 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 502 | QGraphicsLayout::setGeometry(rect); | - | ||||||||||||
| 503 | QRectF effectiveRect = geometry(); | - | ||||||||||||
| 504 | qreal left, top, right, bottom; | - | ||||||||||||
| 505 | getContentsMargins(&left, &top, &right, &bottom); | - | ||||||||||||
| 506 | Qt::LayoutDirection visualDir = d->visualDirection(); | - | ||||||||||||
| 507 | d->engine.setVisualDirection(visualDir); | - | ||||||||||||
| 508 | if (visualDir == Qt::RightToLeft)
| 0 | ||||||||||||
| 509 | qSwap(left, right); never executed: qSwap(left, right); | 0 | ||||||||||||
| 510 | effectiveRect.adjust(+left, +top, -right, -bottom); | - | ||||||||||||
| 511 | #ifdef QGRIDLAYOUTENGINE_DEBUG | - | ||||||||||||
| 512 | if (qt_graphicsLayoutDebug()) { | - | ||||||||||||
| 513 | static int counter = 0; | - | ||||||||||||
| 514 | qDebug() << counter++ << "QGraphicsLinearLayout::setGeometry - " << rect; | - | ||||||||||||
| 515 | dump(1); | - | ||||||||||||
| 516 | } | - | ||||||||||||
| 517 | #endif | - | ||||||||||||
| 518 | d->engine.setGeometries(effectiveRect, d->styleInfo()); | - | ||||||||||||
| 519 | #ifdef QGRIDLAYOUTENGINE_DEBUG | - | ||||||||||||
| 520 | if (qt_graphicsLayoutDebug()) { | - | ||||||||||||
| 521 | qDebug("post dump"); | - | ||||||||||||
| 522 | dump(1); | - | ||||||||||||
| 523 | } | - | ||||||||||||
| 524 | #endif | - | ||||||||||||
| 525 | } never executed: end of block | 0 | ||||||||||||
| 526 | - | |||||||||||||
| 527 | /*! | - | ||||||||||||
| 528 | \reimp | - | ||||||||||||
| 529 | */ | - | ||||||||||||
| 530 | QSizeF QGraphicsLinearLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | - | ||||||||||||
| 531 | { | - | ||||||||||||
| 532 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 533 | qreal left, top, right, bottom; | - | ||||||||||||
| 534 | getContentsMargins(&left, &top, &right, &bottom); | - | ||||||||||||
| 535 | const QSizeF extraMargins(left + right, top + bottom); | - | ||||||||||||
| 536 | return d->engine.sizeHint(which , constraint - extraMargins, d->styleInfo()) + extraMargins; never executed: return d->engine.sizeHint(which , constraint - extraMargins, d->styleInfo()) + extraMargins; | 0 | ||||||||||||
| 537 | } | - | ||||||||||||
| 538 | - | |||||||||||||
| 539 | /*! | - | ||||||||||||
| 540 | \reimp | - | ||||||||||||
| 541 | */ | - | ||||||||||||
| 542 | void QGraphicsLinearLayout::invalidate() | - | ||||||||||||
| 543 | { | - | ||||||||||||
| 544 | Q_D(QGraphicsLinearLayout); | - | ||||||||||||
| 545 | d->engine.invalidate(); | - | ||||||||||||
| 546 | if (d->m_styleInfo)
| 0 | ||||||||||||
| 547 | d->m_styleInfo->invalidate(); never executed: d->m_styleInfo->invalidate(); | 0 | ||||||||||||
| 548 | QGraphicsLayout::invalidate(); | - | ||||||||||||
| 549 | } never executed: end of block | 0 | ||||||||||||
| 550 | - | |||||||||||||
| 551 | /*! | - | ||||||||||||
| 552 | \internal | - | ||||||||||||
| 553 | */ | - | ||||||||||||
| 554 | void QGraphicsLinearLayout::dump(int indent) const | - | ||||||||||||
| 555 | { | - | ||||||||||||
| 556 | #ifdef QGRIDLAYOUTENGINE_DEBUG | - | ||||||||||||
| 557 | if (qt_graphicsLayoutDebug()) { | - | ||||||||||||
| 558 | Q_D(const QGraphicsLinearLayout); | - | ||||||||||||
| 559 | qDebug("%*s%s layout", indent, "", | - | ||||||||||||
| 560 | d->orientation == Qt::Horizontal ? "Horizontal" : "Vertical"); | - | ||||||||||||
| 561 | d->engine.dump(indent + 1); | - | ||||||||||||
| 562 | } | - | ||||||||||||
| 563 | #else | - | ||||||||||||
| 564 | Q_UNUSED(indent); | - | ||||||||||||
| 565 | #endif | - | ||||||||||||
| 566 | } never executed: end of block | 0 | ||||||||||||
| 567 | - | |||||||||||||
| 568 | QT_END_NAMESPACE | - | ||||||||||||
| 569 | - | |||||||||||||
| 570 | #endif //QT_NO_GRAPHICSVIEW | - | ||||||||||||
| Source code | Switch to Preprocessed file |