Implemented QWidget's move() vs setGeometry() distinction in QWindow.

QWidget's move() sets the position of the window including the window
frame, unlike setGeometry(). There was no equivalent for this in
QWindow, so several QWidget auto-tests were failing. Now we add
setFramePos() to achieve the same purpose in QWindow.

This fixes tst_QWidget::windowState(), which uses move().

Change-Id: I9a3e558bd615a8f0234cc3dd94fbb2bf5ecbc148
Reviewed-on: http://codereview.qt-project.org/5405
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Samuel Rødal 2011-09-22 14:37:58 +02:00 committed by Qt by Nokia
parent 294df24621
commit 0bc19b4997
6 changed files with 185 additions and 9 deletions

View File

@ -412,9 +412,13 @@ void QWindow::setSizeIncrement(const QSize &size)
d->platformWindow->propagateSizeHints(); d->platformWindow->propagateSizeHints();
} }
/*!
Sets the geometry of the window excluding its window frame.
*/
void QWindow::setGeometry(const QRect &rect) void QWindow::setGeometry(const QRect &rect)
{ {
Q_D(QWindow); Q_D(QWindow);
d->positionPolicy = QWindowPrivate::WindowFrameExclusive;
if (d->platformWindow) { if (d->platformWindow) {
d->platformWindow->setGeometry(rect); d->platformWindow->setGeometry(rect);
} else { } else {
@ -422,6 +426,9 @@ void QWindow::setGeometry(const QRect &rect)
} }
} }
/*!
Returns the geometry of the window excluding its window frame.
*/
QRect QWindow::geometry() const QRect QWindow::geometry() const
{ {
Q_D(const QWindow); Q_D(const QWindow);
@ -430,6 +437,9 @@ QRect QWindow::geometry() const
return d->geometry; return d->geometry;
} }
/*!
Returns the window frame margins surrounding the window.
*/
QMargins QWindow::frameMargins() const QMargins QWindow::frameMargins() const
{ {
Q_D(const QWindow); Q_D(const QWindow);
@ -438,6 +448,56 @@ QMargins QWindow::frameMargins() const
return QMargins(); return QMargins();
} }
/*!
Returns the geometry of the window including its window frame.
*/
QRect QWindow::frameGeometry() const
{
Q_D(const QWindow);
if (d->platformWindow) {
QMargins m = frameMargins();
return d->platformWindow->geometry().adjusted(-m.left(), -m.top(), m.right(), m.bottom());
}
return d->geometry;
}
/*!
Returns the top left position of the window including its window frame.
*/
QPoint QWindow::framePos() const
{
Q_D(const QWindow);
if (d->platformWindow) {
QMargins margins = frameMargins();
return d->platformWindow->geometry().topLeft() - QPoint(margins.left(), margins.top());
}
return d->geometry.topLeft();
}
/*!
Sets the upper left position of the window including its window frame.
*/
void QWindow::setFramePos(const QPoint &point)
{
Q_D(QWindow);
d->positionPolicy = QWindowPrivate::WindowFrameInclusive;
if (d->platformWindow) {
d->platformWindow->setGeometry(QRect(point, size()));
} else {
d->geometry.setTopLeft(point);
}
}
void QWindow::resize(const QSize &newSize)
{
Q_D(QWindow);
if (d->platformWindow) {
d->platformWindow->setGeometry(QRect(pos(), newSize));
} else {
d->geometry.setSize(newSize);
}
}
void QWindow::setWindowIcon(const QImage &icon) const void QWindow::setWindowIcon(const QImage &icon) const
{ {
Q_UNUSED(icon); Q_UNUSED(icon);

View File

@ -142,6 +142,12 @@ public:
void setGeometry(const QRect &rect); void setGeometry(const QRect &rect);
QRect geometry() const; QRect geometry() const;
QMargins frameMargins() const;
QRect frameGeometry() const;
QPoint framePos() const;
void setFramePos(const QPoint &point);
inline int width() const { return geometry().width(); } inline int width() const { return geometry().width(); }
inline int height() const { return geometry().height(); } inline int height() const { return geometry().height(); }
inline int x() const { return geometry().x(); } inline int x() const { return geometry().x(); }
@ -150,14 +156,15 @@ public:
inline QSize size() const { return geometry().size(); } inline QSize size() const { return geometry().size(); }
inline QPoint pos() const { return geometry().topLeft(); } inline QPoint pos() const { return geometry().topLeft(); }
inline void move(const QPoint &pt) { setGeometry(QRect(pt, size())); } QT_DEPRECATED inline void move(const QPoint &pt) { setPos(pt); }
inline void move(int posx, int posy) { move(QPoint(posx, posy)); } QT_DEPRECATED inline void move(int posx, int posy) { setPos(posx, posy); }
inline void resize(const QSize &newsize) { setGeometry(QRect(pos(), newsize)); } inline void setPos(const QPoint &pt) { setGeometry(QRect(pt, size())); }
inline void setPos(int posx, int posy) { setPos(QPoint(posx, posy)); }
void resize(const QSize &newSize);
inline void resize(int w, int h) { setGeometry(QRect(x(), y(), w, h)); } inline void resize(int w, int h) { setGeometry(QRect(x(), y(), w, h)); }
QMargins frameMargins() const;
void setWindowIcon(const QImage &icon) const; void setWindowIcon(const QImage &icon) const;
void destroy(); void destroy();

View File

@ -43,6 +43,7 @@
#define QWINDOW_QPA_P_H #define QWINDOW_QPA_P_H
#include <QtGui/qwindow.h> #include <QtGui/qwindow.h>
#include <QtGui/qplatformwindow_qpa.h>
#include <QtCore/private/qobject_p.h> #include <QtCore/private/qobject_p.h>
@ -59,6 +60,12 @@ class Q_GUI_EXPORT QWindowPrivate : public QObjectPrivate
Q_DECLARE_PUBLIC(QWindow) Q_DECLARE_PUBLIC(QWindow)
public: public:
enum PositionPolicy
{
WindowFrameInclusive,
WindowFrameExclusive
};
QWindowPrivate() QWindowPrivate()
: QObjectPrivate() : QObjectPrivate()
, surfaceType(QWindow::RasterSurface) , surfaceType(QWindow::RasterSurface)
@ -68,6 +75,7 @@ public:
, visible(false) , visible(false)
, windowState(Qt::WindowNoState) , windowState(Qt::WindowNoState)
, resizeEventPending(true) , resizeEventPending(true)
, positionPolicy(WindowFrameExclusive)
, maximumSize(QWINDOWSIZE_MAX, QWINDOWSIZE_MAX) , maximumSize(QWINDOWSIZE_MAX, QWINDOWSIZE_MAX)
, modality(Qt::NonModal) , modality(Qt::NonModal)
, transientParent(0) , transientParent(0)
@ -101,6 +109,7 @@ public:
QRect geometry; QRect geometry;
Qt::WindowState windowState; Qt::WindowState windowState;
bool resizeEventPending; bool resizeEventPending;
PositionPolicy positionPolicy;
QSize minimumSize; QSize minimumSize;
QSize maximumSize; QSize maximumSize;

View File

@ -246,6 +246,8 @@ void QXcbWindow::create()
Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values)); Q_XCB_CALL(xcb_change_window_attributes(xcb_connection(), m_window, mask, values));
propagateSizeHints();
xcb_atom_t properties[4]; xcb_atom_t properties[4];
int propertyCount = 0; int propertyCount = 0;
properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW); properties[propertyCount++] = atom(QXcbAtom::WM_DELETE_WINDOW);
@ -335,6 +337,8 @@ void QXcbWindow::setGeometry(const QRect &rect)
{ {
QPlatformWindow::setGeometry(rect); QPlatformWindow::setGeometry(rect);
propagateSizeHints();
const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; const quint32 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
const quint32 values[] = { rect.x(), const quint32 values[] = { rect.x(),
rect.y(), rect.y(),
@ -1062,11 +1066,12 @@ void QXcbWindow::propagateSizeHints()
QRect rect = geometry(); QRect rect = geometry();
QWindow *win = window();
xcb_size_hints_set_position(&hints, true, rect.x(), rect.y()); xcb_size_hints_set_position(&hints, true, rect.x(), rect.y());
xcb_size_hints_set_size(&hints, true, rect.width(), rect.height()); xcb_size_hints_set_size(&hints, true, rect.width(), rect.height());
xcb_size_hints_set_win_gravity(&hints, XCB_GRAVITY_STATIC); xcb_size_hints_set_win_gravity(&hints, qt_window_private(win)->positionPolicy == QWindowPrivate::WindowFrameInclusive
? XCB_GRAVITY_NORTH_WEST : XCB_GRAVITY_STATIC);
QWindow *win = window();
QSize minimumSize = win->minimumSize(); QSize minimumSize = win->minimumSize();
QSize maximumSize = win->maximumSize(); QSize maximumSize = win->maximumSize();

View File

@ -126,6 +126,7 @@ void QWidgetPrivate::create_sys(WId window, bool initializeWindow, bool destroyO
} }
} }
qt_window_private(win)->positionPolicy = topData()->posFromMove ? QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
win->create(); win->create();
data.window_flags = win->windowFlags(); data.window_flags = win->windowFlags();
@ -661,6 +662,7 @@ void QWidgetPrivate::setGeometry_sys(int x, int y, int w, int h, bool isMove)
if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) { if (!q->testAttribute(Qt::WA_DontShowOnScreen) && !q->testAttribute(Qt::WA_OutsideWSRange)) {
if (q->windowHandle()) { if (q->windowHandle()) {
if (q->isWindow()) { if (q->isWindow()) {
qt_window_private(q->windowHandle())->positionPolicy = topData()->posFromMove ? QWindowPrivate::WindowFrameInclusive : QWindowPrivate::WindowFrameExclusive;
q->windowHandle()->setGeometry(q->geometry()); q->windowHandle()->setGeometry(q->geometry());
} else { } else {
QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint()); QPoint posInNativeParent = q->mapTo(q->nativeParentWidget(),QPoint());

View File

@ -40,7 +40,10 @@
****************************************************************************/ ****************************************************************************/
#include <qwindow.h> #include <qwindow.h>
#include <qtest.h>
#include <QtTest/QtTest>
#include "../../shared/util.h"
class tst_QWindow: public QObject class tst_QWindow: public QObject
{ {
@ -48,6 +51,7 @@ class tst_QWindow: public QObject
private slots: private slots:
void mapGlobal(); void mapGlobal();
void positioning();
}; };
@ -70,5 +74,94 @@ void tst_QWindow::mapGlobal()
QCOMPARE(c.mapFromGlobal(QPoint(100, 100)), QPoint(30, 30)); QCOMPARE(c.mapFromGlobal(QPoint(100, 100)), QPoint(30, 30));
} }
class Window : public QWindow
{
public:
Window()
: gotResizeEvent(false)
, gotMapEvent(false)
, gotMoveEvent(false)
{
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
}
bool event(QEvent *event)
{
switch (event->type()) {
case QEvent::Map:
gotMapEvent = true;
break;
case QEvent::Resize:
gotResizeEvent = true;
break;
case QEvent::Move:
gotMoveEvent = true;
break;
default:
break;
}
return QWindow::event(event);
}
bool gotResizeEvent;
bool gotMapEvent;
bool gotMoveEvent;
};
void tst_QWindow::positioning()
{
QRect geometry(80, 80, 40, 40);
Window window;
window.setGeometry(geometry);
QCOMPARE(window.geometry(), geometry);
window.show();
QTRY_VERIFY(window.gotResizeEvent && window.gotMapEvent);
QMargins originalMargins = window.frameMargins();
QCOMPARE(window.pos(), window.framePos() + QPoint(originalMargins.left(), originalMargins.top()));
QVERIFY(window.frameGeometry().contains(window.geometry()));
QPoint originalPos = window.pos();
QPoint originalFramePos = window.framePos();
window.gotResizeEvent = false;
window.setWindowState(Qt::WindowFullScreen);
QTRY_VERIFY(window.gotResizeEvent);
window.gotResizeEvent = false;
window.setWindowState(Qt::WindowNoState);
QTRY_VERIFY(window.gotResizeEvent);
QTRY_COMPARE(originalPos, window.pos());
QTRY_COMPARE(originalFramePos, window.framePos());
QTRY_COMPARE(originalMargins, window.frameMargins());
// if our positioning is actually fully respected by the window manager
// test whether it correctly handles frame positioning as well
if (originalPos == geometry.topLeft() && (originalMargins.top() != 0 || originalMargins.left() != 0)) {
QPoint framePos(40, 40);
window.gotMoveEvent = false;
window.setFramePos(framePos);
QTRY_VERIFY(window.gotMoveEvent);
QTRY_COMPARE(framePos, window.framePos());
QTRY_COMPARE(originalMargins, window.frameMargins());
QCOMPARE(window.pos(), window.framePos() + QPoint(originalMargins.left(), originalMargins.top()));
// and back to regular positioning
window.gotMoveEvent = false;
window.setPos(originalPos);
QTRY_VERIFY(window.gotMoveEvent);
QTRY_COMPARE(originalPos, window.pos());
}
}
#include <tst_qwindow.moc> #include <tst_qwindow.moc>
QTEST_MAIN(tst_QWindow); QTEST_MAIN(tst_QWindow);