summaryrefslogtreecommitdiffstats
path: root/src/gui/graphicsview/qgraphicsitem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/graphicsview/qgraphicsitem.cpp')
-rw-r--r--src/gui/graphicsview/qgraphicsitem.cpp118
1 files changed, 95 insertions, 23 deletions
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp
index 620f6f4..8860677 100644
--- a/src/gui/graphicsview/qgraphicsitem.cpp
+++ b/src/gui/graphicsview/qgraphicsitem.cpp
@@ -335,6 +335,13 @@
\value ItemAutoDetectsFocusProxy The item will assign any child that
gains input focus as its focus proxy. See also focusProxy().
This flag was introduced in Qt 4.6.
+
+ \value ItemIsPanel. The item is a panel. A panel provides activation and
+ contained focus handling. Only one panel can be active at a time (see
+ QGraphicsItem::isActive()). When no panel is active, QGraphicsScene
+ activates all non-panel items. Window items (i.e.,
+ QGraphicsItem::isWindow() returns true) are panels. This flag was
+ introduced in Qt 4.6.
*/
/*!
@@ -1363,14 +1370,29 @@ QGraphicsWidget *QGraphicsItem::topLevelWidget() const
*/
QGraphicsWidget *QGraphicsItem::window() const
{
- if (isWidget() && static_cast<const QGraphicsWidget *>(this)->isWindow())
- return static_cast<QGraphicsWidget *>(const_cast<QGraphicsItem *>(this));
- if (QGraphicsWidget *parent = parentWidget())
- return parent->window();
+ QGraphicsItem *p = panel();
+ if (p && p->isWindow())
+ return static_cast<QGraphicsWidget *>(p);
return 0;
}
/*!
+ \since 4.6
+
+ Returns the item's panel, or 0 if this item does not have a panel. If the
+ item is a panel, it will return itself. Otherwise it will return the
+ closest ancestor that is a panel.
+
+ \sa isPanel(), ItemIsPanel
+*/
+QGraphicsItem *QGraphicsItem::panel() const
+{
+ if (d_ptr->flags & ItemIsPanel)
+ return const_cast<QGraphicsItem *>(this);
+ return d_ptr->parent ? d_ptr->parent->panel() : 0;
+}
+
+/*!
\since 4.6
Return the graphics item cast to a QGraphicsObject, if the class is actually a
@@ -1456,6 +1478,17 @@ bool QGraphicsItem::isWindow() const
}
/*!
+ \since 4.6
+ Returns true if the item is a panel; otherwise returns false.
+
+ \sa QGraphicsItem::panel(), ItemIsPanel
+*/
+bool QGraphicsItem::isPanel() const
+{
+ return d_ptr->flags & ItemIsPanel;
+}
+
+/*!
Returns this item's flags. The flags describe what configurable features
of the item are enabled and not. For example, if the flags include
ItemIsFocusable, the item can accept input focus.
@@ -1518,6 +1551,9 @@ static void _q_qgraphicsItemSetFlag(QGraphicsItem *item, QGraphicsItem::Graphics
*/
void QGraphicsItem::setFlags(GraphicsItemFlags flags)
{
+ if (isWindow())
+ flags |= ItemIsPanel;
+
// Notify change and check for adjustment.
if (quint32(d_ptr->flags) == quint32(flags))
return;
@@ -1872,16 +1908,16 @@ void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, bo
q->ungrabKeyboard();
}
if (q_ptr->hasFocus() && scene) {
- // Hiding the closest non-window ancestor of the focus item
+ // Hiding the closest non-panel ancestor of the focus item
QGraphicsItem *focusItem = scene->focusItem();
bool clear = true;
- if (isWidget && !focusItem->isWindow()) {
+ if (isWidget && !focusItem->isPanel()) {
do {
if (focusItem == q_ptr) {
clear = !static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(true);
break;
}
- } while ((focusItem = focusItem->parentWidget()) && !focusItem->isWindow());
+ } while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel());
}
if (clear)
q_ptr->clearFocus();
@@ -2003,17 +2039,17 @@ void QGraphicsItemPrivate::setEnabledHelper(bool newEnabled, bool explicitly, bo
if (scene && scene->mouseGrabberItem() == q_ptr)
q_ptr->ungrabMouse();
if (q_ptr->hasFocus()) {
- // Disabling the closest non-window ancestor of the focus item
+ // Disabling the closest non-panel ancestor of the focus item
// causes focus to pop to the next item, otherwise it's cleared.
QGraphicsItem *focusItem = scene->focusItem();
bool clear = true;
- if (isWidget && !focusItem->isWindow() && q_ptr->isAncestorOf(focusItem)) {
+ if (isWidget && !focusItem->isPanel() && q_ptr->isAncestorOf(focusItem)) {
do {
if (focusItem == q_ptr) {
clear = !static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(true);
break;
}
- } while ((focusItem = focusItem->parentWidget()) && !focusItem->isWindow());
+ } while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel());
}
if (clear)
q_ptr->clearFocus();
@@ -2610,12 +2646,33 @@ void QGraphicsItem::setHandlesChildEvents(bool enabled)
d_ptr->handlesChildEvents = enabled;
d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1));
}
+/*!
+ \since 4.6
+ Returns true if this item is active; otherwise returns false.
+
+ An item can only be active if the scene is active. An item is active
+ if it is, or is a descendent of, an active panel. Items in non-active
+ panels are not active.
+
+ Items that are not part of a panel follow scene activation when the
+ scene has no active panel.
+
+ Only active items can gain input focus.
+
+ \sa QGraphicsScene::isActive(), QGraphicsScene::activePanel(), panel(), isPanel()
+*/
+bool QGraphicsItem::isActive() const
+{
+ if (!d_ptr->scene || !d_ptr->scene->isActive())
+ return false;
+ return panel() == d_ptr->scene->activePanel();
+}
/*!
- Returns true if this item or its \l{focusProxy()}{focus proxy} has keyboard
- input focus; otherwise, returns false.
+ Returns true if this item is active, and it or its \l{focusProxy()}{focus
+ proxy} has keyboard input focus; otherwise, returns false.
- \sa focusItem(), setFocus(), QGraphicsScene::setFocusItem()
+ \sa focusItem(), setFocus(), QGraphicsScene::setFocusItem(), isActive()
*/
bool QGraphicsItem::hasFocus() const
{
@@ -2632,9 +2689,10 @@ bool QGraphicsItem::hasFocus() const
Only enabled items that set the ItemIsFocusable flag can accept keyboard
focus.
- If this item is not visible, or not associated with a scene, it will not
- gain immediate input focus. However, it will be registered as the preferred
- focus item for its subtree of items, should it later become visible.
+ If this item is not visible, not active, or not associated with a scene,
+ it will not gain immediate input focus. However, it will be registered as
+ the preferred focus item for its subtree of items, should it later become
+ visible.
As a result of calling this function, this item will receive a
\l{focusInEvent()}{focus in event} with \a focusReason. If another item
@@ -2663,8 +2721,8 @@ void QGraphicsItem::setFocus(Qt::FocusReason focusReason)
// Update the scene's focus item.
if (d_ptr->scene) {
- QGraphicsWidget *w = window();
- if (!w || w->isActiveWindow()) {
+ QGraphicsItem *p = panel();
+ if (!p || p->isActive()) {
// Visible items immediately gain focus from scene.
d_ptr->scene->d_func()->setFocusItemHelper(f, focusReason);
}
@@ -2674,8 +2732,8 @@ void QGraphicsItem::setFocus(Qt::FocusReason focusReason)
/*!
Takes keyboard input focus from the item.
- If it has focus, a \l{focusOutEvent()}{focus out event} is sent to this item
- to tell it that it is about to lose the focus.
+ If it has focus, a \l{focusOutEvent()}{focus out event} is sent to this
+ item to tell it that it is about to lose the focus.
Only items that set the ItemIsFocusable flag, or widgets that set an
appropriate focus policy, can accept keyboard focus.
@@ -4770,7 +4828,7 @@ void QGraphicsItemPrivate::setSubFocus()
bool hidden = !visible;
do {
parent->d_func()->subFocusItem = item;
- } while (!parent->isWindow() && (parent = parent->d_ptr->parent) && (!hidden || !parent->d_func()->visible));
+ } while (!parent->isPanel() && (parent = parent->d_ptr->parent) && (!hidden || !parent->d_func()->visible));
}
/*!
@@ -4784,7 +4842,7 @@ void QGraphicsItemPrivate::clearSubFocus()
if (parent->d_ptr->subFocusItem != q_ptr)
break;
parent->d_ptr->subFocusItem = 0;
- } while (!parent->isWindow() && (parent = parent->d_ptr->parent));
+ } while (!parent->isPanel() && (parent = parent->d_ptr->parent));
}
/*!
@@ -6035,6 +6093,17 @@ bool QGraphicsItem::sceneEvent(QEvent *event)
case QEvent::InputMethod:
inputMethodEvent(static_cast<QInputMethodEvent *>(event));
break;
+ case QEvent::WindowActivate:
+ case QEvent::WindowDeactivate:
+ // Propagate panel activation.
+ if (d_ptr->scene) {
+ for (int i = 0; i < d_ptr->children.size(); ++i) {
+ QGraphicsItem *child = d_ptr->children.at(i);
+ if (!(child->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents))
+ d_ptr->scene->sendEvent(child, event);
+ }
+ }
+ break;
default:
return false;
}
@@ -10440,6 +10509,9 @@ QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlag flag)
case QGraphicsItem::ItemAutoDetectsFocusProxy:
str = "ItemAutoDetectsFocusProxy";
break;
+ case QGraphicsItem::ItemIsPanel:
+ str = "ItemIsPanel";
+ break;
}
debug << str;
return debug;
@@ -10449,7 +10521,7 @@ QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlags flags)
{
debug << '(';
bool f = false;
- for (int i = 0; i < 9; ++i) {
+ for (int i = 0; i < 16; ++i) {
if (flags & (1 << i)) {
if (f)
debug << '|';