summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2016-11-24 16:09:39 +0100
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2016-12-08 15:57:18 +0000
commit7a93d98f29fca9f1610f3b61b055535916903500 (patch)
tree9dd8bee59e0a1a308228edc01b6d642186936cc5
parent6604d79a21fcdd23f7f58055299aa1ab914eb734 (diff)
Add QWindow::AncestorMode overload of QWindow::parent()
Simplifies code that traverses the parent hierarchy, including transient parents. For Qt6 we should merge the two parent() functions, adding a default value for the mode, probably ExcludeTransients. Change-Id: Ic9cdae3e31a3a8e140a5b175160f3b934d2b6e00 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r--src/gui/kernel/qwindow.cpp34
-rw-r--r--src/gui/kernel/qwindow.h14
2 files changed, 32 insertions, 16 deletions
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp
index ba7fc8533f..93edfe6331 100644
--- a/src/gui/kernel/qwindow.cpp
+++ b/src/gui/kernel/qwindow.cpp
@@ -629,6 +629,22 @@ WId QWindow::winId() const
return d->platformWindow->winId();
}
+ /*!
+ Returns the parent window, if any.
+
+ If \a mode is IncludeTransients, then the transient parent is returned
+ if there is no parent.
+
+ A window without a parent is known as a top level window.
+
+ \since 5.9
+*/
+QWindow *QWindow::parent(AncestorMode mode) const
+{
+ Q_D(const QWindow);
+ return d->parentWindow ? d->parentWindow : (mode == IncludeTransients ? transientParent() : nullptr);
+}
+
/*!
Returns the parent window, if any.
@@ -1117,11 +1133,10 @@ bool QWindow::isActive() const
if (focus == this)
return true;
- if (!parent() && !transientParent()) {
+ if (QWindow *p = parent(IncludeTransients))
+ return p->isActive();
+ else
return isAncestorOf(focus);
- } else {
- return (parent() && parent()->isActive()) || (transientParent() && transientParent()->isActive());
- }
}
/*!
@@ -1285,8 +1300,10 @@ bool QWindow::isAncestorOf(const QWindow *child, AncestorMode mode) const
if (child->parent() == this || (mode == IncludeTransients && child->transientParent() == this))
return true;
- return (child->parent() && isAncestorOf(child->parent(), mode))
- || (mode == IncludeTransients && child->transientParent() && isAncestorOf(child->transientParent(), mode));
+ if (child->parent(mode) && isAncestorOf(child->parent(mode), mode))
+ return true;
+
+ return false;
}
/*!
@@ -2497,10 +2514,7 @@ QWindow *QWindowPrivate::topLevelWindow() const
QWindow *window = const_cast<QWindow *>(q);
while (window) {
- QWindow *parent = window->parent();
- if (!parent)
- parent = window->transientParent();
-
+ QWindow *parent = window->parent(QWindow::IncludeTransients);
if (!parent)
break;
diff --git a/src/gui/kernel/qwindow.h b/src/gui/kernel/qwindow.h
index 0b84f30468..2883749d2e 100644
--- a/src/gui/kernel/qwindow.h
+++ b/src/gui/kernel/qwindow.h
@@ -132,6 +132,12 @@ public:
};
Q_ENUM(Visibility)
+ enum AncestorMode {
+ ExcludeTransients,
+ IncludeTransients
+ };
+ Q_ENUM(AncestorMode)
+
explicit QWindow(QScreen *screen = Q_NULLPTR);
explicit QWindow(QWindow *parent);
virtual ~QWindow();
@@ -148,7 +154,8 @@ public:
WId winId() const;
- QWindow *parent() const;
+ QWindow *parent(AncestorMode mode) const;
+ QWindow *parent() const; // ### Qt6: Merge with above
void setParent(QWindow *parent);
bool isTopLevel() const;
@@ -187,11 +194,6 @@ public:
void setTransientParent(QWindow *parent);
QWindow *transientParent() const;
- enum AncestorMode {
- ExcludeTransients,
- IncludeTransients
- };
-
bool isAncestorOf(const QWindow *child, AncestorMode mode = IncludeTransients) const;
bool isExposed() const;