| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qglobal.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_GRAPHICSVIEW
|
|---|
| 45 |
|
|---|
| 46 | #include "qgraphicslayout_p.h"
|
|---|
| 47 | #include "qgraphicslayout.h"
|
|---|
| 48 | #include "qgraphicswidget.h"
|
|---|
| 49 | #include "qapplication.h"
|
|---|
| 50 |
|
|---|
| 51 | QT_BEGIN_NAMESPACE
|
|---|
| 52 |
|
|---|
| 53 | /*!
|
|---|
| 54 | \internal
|
|---|
| 55 |
|
|---|
| 56 | \a mw is the new parent. all items in the layout will be a child of \a mw.
|
|---|
| 57 | */
|
|---|
| 58 | void QGraphicsLayoutPrivate::reparentChildItems(QGraphicsItem *newParent)
|
|---|
| 59 | {
|
|---|
| 60 | Q_Q(QGraphicsLayout);
|
|---|
| 61 | int n = q->count();
|
|---|
| 62 | //bool mwVisible = mw && mw->isVisible();
|
|---|
| 63 | for (int i = 0; i < n; ++i) {
|
|---|
| 64 | QGraphicsLayoutItem *layoutChild = q->itemAt(i);
|
|---|
| 65 | if (!layoutChild) {
|
|---|
| 66 | // Skip stretch items
|
|---|
| 67 | continue;
|
|---|
| 68 | }
|
|---|
| 69 | if (layoutChild->isLayout()) {
|
|---|
| 70 | QGraphicsLayout *l = static_cast<QGraphicsLayout*>(layoutChild);
|
|---|
| 71 | l->d_func()->reparentChildItems(newParent);
|
|---|
| 72 | } else if (QGraphicsItem *itemChild = layoutChild->graphicsItem()){
|
|---|
| 73 | QGraphicsItem *childParent = itemChild->parentItem();
|
|---|
| 74 | #ifdef QT_DEBUG
|
|---|
| 75 | if (childParent && childParent != newParent && itemChild->isWidget() && qt_graphicsLayoutDebug()) {
|
|---|
| 76 | QGraphicsWidget *w = static_cast<QGraphicsWidget*>(layoutChild);
|
|---|
| 77 | qWarning("QGraphicsLayout::addChildLayout: widget %s \"%s\" in wrong parent; moved to correct parent",
|
|---|
| 78 | w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
|
|---|
| 79 | }
|
|---|
| 80 | #endif
|
|---|
| 81 | if (childParent != newParent)
|
|---|
| 82 | itemChild->setParentItem(newParent);
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void QGraphicsLayoutPrivate::getMargin(qreal *result, qreal userMargin, QStyle::PixelMetric pm) const
|
|---|
| 88 | {
|
|---|
| 89 | if (!result)
|
|---|
| 90 | return;
|
|---|
| 91 | Q_Q(const QGraphicsLayout);
|
|---|
| 92 |
|
|---|
| 93 | QGraphicsLayoutItem *parent = q->parentLayoutItem();
|
|---|
| 94 | if (userMargin >= 0.0) {
|
|---|
| 95 | *result = userMargin;
|
|---|
| 96 | } else if (!parent) {
|
|---|
| 97 | *result = 0.0;
|
|---|
| 98 | } else if (parent->isLayout()) { // sublayouts have 0 margin by default
|
|---|
| 99 | *result = 0.0;
|
|---|
| 100 | } else {
|
|---|
| 101 | *result = 0.0;
|
|---|
| 102 | if (QGraphicsItem *layoutParentItem = parentItem()) {
|
|---|
| 103 | if (layoutParentItem->isWidget())
|
|---|
| 104 | *result = (qreal)static_cast<QGraphicsWidget*>(layoutParentItem)->style()->pixelMetric(pm, 0);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | Qt::LayoutDirection QGraphicsLayoutPrivate::visualDirection() const
|
|---|
| 110 | {
|
|---|
| 111 | if (QGraphicsItem *maybeWidget = parentItem()) {
|
|---|
| 112 | if (maybeWidget->isWidget())
|
|---|
| 113 | return static_cast<QGraphicsWidget*>(maybeWidget)->layoutDirection();
|
|---|
| 114 | }
|
|---|
| 115 | return QApplication::layoutDirection();
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | static bool removeLayoutItemFromLayout(QGraphicsLayout *lay, QGraphicsLayoutItem *layoutItem)
|
|---|
| 119 | {
|
|---|
| 120 | if (!lay)
|
|---|
| 121 | return false;
|
|---|
| 122 |
|
|---|
| 123 | QGraphicsLayoutItem *child;
|
|---|
| 124 | for (int i = 0; (child = lay->itemAt(i)); ++i) {
|
|---|
| 125 | if (child && child->isLayout()) {
|
|---|
| 126 | if (removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(child), layoutItem))
|
|---|
| 127 | return true;
|
|---|
| 128 | } else if (child == layoutItem) {
|
|---|
| 129 | lay->removeAt(i);
|
|---|
| 130 | return true;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | return false;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /*!
|
|---|
| 137 | \internal
|
|---|
| 138 |
|
|---|
| 139 | This function is called from subclasses to add a layout item \a layoutItem
|
|---|
| 140 | to a layout.
|
|---|
| 141 |
|
|---|
| 142 | It takes care of automatically reparenting graphics items, if needed.
|
|---|
| 143 |
|
|---|
| 144 | If \a layoutItem is a is already in a layout, it will remove it from that layout.
|
|---|
| 145 |
|
|---|
| 146 | */
|
|---|
| 147 | void QGraphicsLayoutPrivate::addChildLayoutItem(QGraphicsLayoutItem *layoutItem)
|
|---|
| 148 | {
|
|---|
| 149 | Q_Q(QGraphicsLayout);
|
|---|
| 150 | if (QGraphicsLayoutItem *maybeLayout = layoutItem->parentLayoutItem()) {
|
|---|
| 151 | if (maybeLayout->isLayout())
|
|---|
| 152 | removeLayoutItemFromLayout(static_cast<QGraphicsLayout*>(maybeLayout), layoutItem);
|
|---|
| 153 | }
|
|---|
| 154 | layoutItem->setParentLayoutItem(q);
|
|---|
| 155 | if (layoutItem->isLayout()) {
|
|---|
| 156 | if (QGraphicsItem *parItem = parentItem()) {
|
|---|
| 157 | static_cast<QGraphicsLayout*>(layoutItem)->d_func()->reparentChildItems(parItem);
|
|---|
| 158 | }
|
|---|
| 159 | } else {
|
|---|
| 160 | if (QGraphicsItem *item = layoutItem->graphicsItem()) {
|
|---|
| 161 | QGraphicsItem *newParent = parentItem();
|
|---|
| 162 | QGraphicsItem *oldParent = item->parentItem();
|
|---|
| 163 | if (oldParent == newParent || !newParent)
|
|---|
| 164 | return;
|
|---|
| 165 |
|
|---|
| 166 | #ifdef QT_DEBUG
|
|---|
| 167 | if (oldParent && item->isWidget()) {
|
|---|
| 168 | QGraphicsWidget *w = static_cast<QGraphicsWidget*>(item);
|
|---|
| 169 | qWarning("QGraphicsLayout::addChildLayoutItem: %s \"%s\" in wrong parent; moved to correct parent",
|
|---|
| 170 | w->metaObject()->className(), w->objectName().toLocal8Bit().constData());
|
|---|
| 171 | }
|
|---|
| 172 | #endif
|
|---|
| 173 |
|
|---|
| 174 | item->setParentItem(newParent);
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void QGraphicsLayoutPrivate::activateRecursive(QGraphicsLayoutItem *item)
|
|---|
| 180 | {
|
|---|
| 181 | if (item->isLayout()) {
|
|---|
| 182 | QGraphicsLayout *layout = static_cast<QGraphicsLayout *>(item);
|
|---|
| 183 | if (layout->d_func()->activated)
|
|---|
| 184 | layout->invalidate();
|
|---|
| 185 |
|
|---|
| 186 | for (int i = layout->count() - 1; i >= 0; --i) {
|
|---|
| 187 | QGraphicsLayoutItem *childItem = layout->itemAt(i);
|
|---|
| 188 | if (childItem)
|
|---|
| 189 | activateRecursive(childItem);
|
|---|
| 190 | }
|
|---|
| 191 | layout->d_func()->activated = true;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | QT_END_NAMESPACE
|
|---|
| 197 |
|
|---|
| 198 | #endif //QT_NO_GRAPHICSVIEW
|
|---|