void MScenePrivate::notifySceneAboutOrientationChange(QGraphicsScene *scene, M::Orientation orientation) { if (!scene) return; MOrientationChangeEvent event(orientation); QList<QGraphicsItem *> sceneItems = scene->items(); for (int i = sceneItems.count() - 1; i >= 0; i--) { QGraphicsItem *item = sceneItems.at(i); // event handlers might remove items from the scene // so we must check if item it's still there if (scene->items().contains(item)) { scene->sendEvent(item, &event); // Sending an event to a widget means only its direct layout gets the orientation // change event. // To ensure that the event it sent to all layouts, we have to manually send it to // the sub-layouts (layout inside a layout) if (item->isWidget()) { QGraphicsLayout *layout = static_cast<QGraphicsWidget *>(item)->layout(); if (layout) MScenePrivate::sendEventToAllSubLayouts(layout, &event); } } } }
void QGraphicsWidgetPrivate::updateFont(const QFont &font) { Q_Q(QGraphicsWidget); // Update the local font setting. this->font = font; // Calculate new mask. if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) inheritedFontResolveMask = 0; int mask = font.resolve() | inheritedFontResolveMask; // Propagate to children. for (int i = 0; i < children.size(); ++i) { QGraphicsItem *item = children.at(i); if (item->isWidget()) { QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item); if (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation)) w->d_func()->resolveFont(mask); } else { item->d_ptr->resolveFont(mask); } } if (!polished) return; // Notify change. QEvent event(QEvent::FontChange); QApplication::sendEvent(q, &event); }
void QGraphicsWidgetPrivate::updatePalette(const QPalette &palette) { Q_Q(QGraphicsWidget); // Update local palette setting. this->palette = palette; // Calculate new mask. if (q->isWindow() && !q->testAttribute(Qt::WA_WindowPropagation)) inheritedPaletteResolveMask = 0; int mask = palette.resolve() | inheritedPaletteResolveMask; // Propagate to children. for (int i = 0; i < children.size(); ++i) { QGraphicsItem *item = children.at(i); if (item->isWidget()) { QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item); if (!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation)) w->d_func()->resolvePalette(mask); } else { item->d_ptr->resolvePalette(mask); } } // Notify change. QEvent event(QEvent::PaletteChange); QApplication::sendEvent(q, &event); }
QLayoutStyleInfo QGraphicsGridLayoutPrivate::styleInfo() const { static QWidget *wid = 0; if (!wid) wid = new QWidget; QGraphicsItem *item = parentItem(); QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : qApp->style(); return QLayoutStyleInfo(style, wid); }
/*! \internal */ QGraphicsLayout::QGraphicsLayout(QGraphicsLayoutPrivate &dd, QGraphicsLayoutItem *parent) : QGraphicsLayoutItem(dd) { setParentLayoutItem(parent); if (parent && !parent->isLayout()) { // If a layout has a parent that is not a layout it must be a QGraphicsWidget. QGraphicsItem *itemParent = parent->graphicsItem(); if (itemParent && itemParent->isWidget()) { static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this); } else { qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is" " neither a QGraphicsWidget nor QGraphicsLayout"); } } setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding, QSizePolicy::DefaultType); setOwnedByLayout(true); }
void UiManager::mousePressEvent(QMouseEvent *event) { QWidget *pressedWidget = NULL; // get the clicked item through the view (respects view and item transformations) QGraphicsItem* itemAt = mWidgetView->itemAt(event->pos()); if ((itemAt) && (itemAt->isWidget())) { QGraphicsProxyWidget *proxyWidget = qgraphicsitem_cast<QGraphicsProxyWidget *>(itemAt); if (proxyWidget) { QWidget *embeddedWidget = proxyWidget->widget(); // if the widget has children, use them, otherwise use the widget directly if (embeddedWidget->children().size() > 0) { QPoint widgetPoint = proxyWidget->mapFromScene(mWidgetView->mapToScene(event->pos())).toPoint(); pressedWidget = embeddedWidget->childAt(widgetPoint); } else { pressedWidget = embeddedWidget; } } } // if there was a focused widget and there is none or a different one now, defocus if (mFocusedWidget && (!pressedWidget || pressedWidget != mFocusedWidget)) { QEvent foe(QEvent::FocusOut); QApplication::sendEvent(mFocusedWidget, &foe); mFocusedWidget = NULL; mTopLevelWidget->setFocus(); } // set the new focus if (pressedWidget) { QEvent fie(QEvent::FocusIn); QApplication::sendEvent(pressedWidget, &fie); pressedWidget->setFocus(Qt::MouseFocusReason); mFocusedWidget = pressedWidget; } QApplication::sendEvent(mWidgetView->viewport(), event); }
QList<MWidget *> MScenePrivate::filterMWidgets(QList<QGraphicsItem *> itemsList) { QGraphicsItem *item; MWidget *mWidget; QList<MWidget *> widgetsList; int itemsCount = itemsList.count(); for (int i = 0; i < itemsCount; i++) { item = itemsList.at(i); if (item->isWidget()) { mWidget = qobject_cast<MWidget *>(static_cast<QGraphicsWidget *>(item)); if (mWidget) { widgetsList << mWidget; } } } return widgetsList; }
void QGraphicsWidgetPrivate::setLayoutDirection_helper(Qt::LayoutDirection direction) { Q_Q(QGraphicsWidget); if ((direction == Qt::RightToLeft) == (testAttribute(Qt::WA_RightToLeft))) return; q->setAttribute(Qt::WA_RightToLeft, (direction == Qt::RightToLeft)); // Propagate this change to all children. for (int i = 0; i < children.size(); ++i) { QGraphicsItem *item = children.at(i); if (item->isWidget()) { QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); if (widget->parentWidget() && !widget->testAttribute(Qt::WA_SetLayoutDirection)) widget->d_func()->setLayoutDirection_helper(direction); } } // Send the notification event to this widget item. QEvent e(QEvent::LayoutDirectionChange); QApplication::sendEvent(q, &e); }
QT_BEGIN_NAMESPACE /*! \class QGraphicsLayout \brief The QGraphicsLayout class provides the base class for all layouts in Graphics View. \since 4.4 \ingroup graphicsview-api QGraphicsLayout is an abstract class that defines a virtual API for arranging QGraphicsWidget children and other QGraphicsLayoutItem objects for a QGraphicsWidget. QGraphicsWidget assigns responsibility to a QGraphicsLayout through QGraphicsWidget::setLayout(). As the widget is resized, the layout will automatically arrange the widget's children. QGraphicsLayout inherits QGraphicsLayoutItem, so, it can be managed by any layout, including its own subclasses. \section1 Writing a Custom Layout You can use QGraphicsLayout as a base to write your own custom layout (e.g., a flowlayout), but it is more common to use one of its subclasses instead - QGraphicsLinearLayout or QGraphicsGridLayout. When creating a custom layout, the following functions must be reimplemented as a bare minimum: \table \header \o Function \o Description \row \o QGraphicsLayoutItem::setGeometry() \o Notifies you when the geometry of the layout is set. You can store the geometry in your own layout class in a reimplementation of this function. \row \o QGraphicsLayoutItem::sizeHint() \o Returns the layout's size hints. \row \o QGraphicsLayout::count() \o Returns the number of items in your layout. \row \o QGraphicsLayout::itemAt() \o Returns a pointer to an item in your layout. \row \o QGraphicsLayout::removeAt() \o Removes an item from your layout without destroying it. \endtable For more details on how to implement each function, refer to the individual function documentation. Each layout defines its own API for arranging widgets and layout items. For example, with a grid layout, you require a row and a column index with optional row and column spans, alignment, spacing, and more. A linear layout, however, requires a single row or column index to position its items. For a grid layout, the order of insertion does not affect the layout in any way, but for a linear layout, the order is essential. When writing your own layout subclass, you are free to choose the API that best suits your layout. \section1 Activating the Layout When the layout's geometry changes, QGraphicsLayout immediately rearranges all of its managed items by calling setGeometry() on each item. This rearrangement is called \e activating the layout. QGraphicsLayout updates its own geometry to match the contentsRect() of the QGraphicsLayoutItem it is managing. Thus, it will automatically rearrange all its items when the widget is resized. QGraphicsLayout caches the sizes of all its managed items to avoid calling setGeometry() too often. \note A QGraphicsLayout will have the same geometry as the contentsRect() of the widget (not the layout) it is assigned to. \section2 Activating the Layout Implicitly The layout can be activated implicitly using one of two ways: by calling activate() or by calling invalidate(). Calling activate() activates the layout immediately. In contrast, calling invalidate() is delayed, as it posts a \l{QEvent::LayoutRequest}{LayoutRequest} event to the managed widget. Due to event compression, the activate() will only be called once after control has returned to the event loop. This is referred to as \e invalidating the layout. Invalidating the layout also invalidates any cached information. Also, the invalidate() function is a virtual function. So, you can invalidate your own cache in a subclass of QGraphicsLayout by reimplementing this function. \section1 Event Handling QGraphicsLayout listens to events for the widget it manages through the virtual widgetEvent() event handler. When the layout is assigned to a widget, all events delivered to the widget are first processed by widgetEvent(). This allows the layout to be aware of any relevant state changes on the widget such as visibility changes or layout direction changes. \section1 Margin Handling The margins of a QGraphicsLayout can be modified by reimplementing setContentsMargins() and getContentsMargins(). */ /*! Contructs a QGraphicsLayout object. \a parent is passed to QGraphicsLayoutItem's constructor and the QGraphicsLayoutItem's isLayout argument is set to \e true. If \a parent is a QGraphicsWidget the layout will be installed on that widget. (Note that installing a layout will delete the old one installed.) */ QGraphicsLayout::QGraphicsLayout(QGraphicsLayoutItem *parent) : QGraphicsLayoutItem(*new QGraphicsLayoutPrivate) { setParentLayoutItem(parent); if (parent && !parent->isLayout()) { // If a layout has a parent that is not a layout it must be a QGraphicsWidget. QGraphicsItem *itemParent = parent->graphicsItem(); if (itemParent && itemParent->isWidget()) { static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this); } else { qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is" " neither a QGraphicsWidget nor QGraphicsLayout"); } } setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding, QSizePolicy::DefaultType); setOwnedByLayout(true); }
QLayoutStyleInfo QGraphicsGridLayoutPrivate::styleInfo() const { QGraphicsItem *item = parentItem(); QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : QApplication::style(); return QLayoutStyleInfo(style, globalStyleInfoWidget()); }
/** * is called after a reparent has taken place to fix up the focus chain(s) */ void QGraphicsWidgetPrivate::fixFocusChainBeforeReparenting(QGraphicsWidget *newParent, QGraphicsScene *oldScene, QGraphicsScene *newScene) { Q_Q(QGraphicsWidget); Q_ASSERT(focusNext && focusPrev); QGraphicsWidget *n = q; //last one in 'new' list QGraphicsWidget *o = 0; //last one in 'old' list QGraphicsWidget *w = focusNext; QGraphicsWidget *firstOld = 0; bool wasPreviousNew = true; while (w != q) { bool isCurrentNew = q->isAncestorOf(w); if (isCurrentNew) { if (!wasPreviousNew) { n->d_func()->focusNext = w; w->d_func()->focusPrev = n; } n = w; } else /*if (!isCurrentNew)*/ { if (wasPreviousNew) { if (o) { o->d_func()->focusNext = w; w->d_func()->focusPrev = o; } else { firstOld = w; } } o = w; } w = w->d_func()->focusNext; wasPreviousNew = isCurrentNew; } // repair the 'old' chain if (firstOld) { o->d_func()->focusNext = firstOld; firstOld->d_func()->focusPrev = o; } // update tabFocusFirst for oldScene if the item is going to be removed from oldScene if (newParent) newScene = newParent->scene(); if (oldScene && newScene != oldScene) oldScene->d_func()->tabFocusFirst = (firstOld && firstOld->scene() == oldScene) ? firstOld : 0; QGraphicsItem *topLevelItem = newParent ? newParent->topLevelItem() : 0; QGraphicsWidget *topLevel = 0; if (topLevelItem && topLevelItem->isWidget()) topLevel = static_cast<QGraphicsWidget *>(topLevelItem); if (topLevel && newParent) { QGraphicsWidget *last = topLevel->d_func()->focusPrev; // link last with new chain last->d_func()->focusNext = q; focusPrev = last; // link last in chain with topLevel->d_func()->focusPrev = n; n->d_func()->focusNext = topLevel; } else { // q is the start of the focus chain n->d_func()->focusNext = q; focusPrev = n; } }