Make QCocoaWindow independent of NSWindow.
QCocoaWindow now gets resize events from QNSViev and does not require a NSWindow. QWindow instances can now be inserted in NSView hierarchies. This is useful for Qt-as-a-plugin use cases and is needed to implement QMacNativeWidget for Qt 5. Change-Id: Ia95ea9c22a15a3e62d1e6543466cff07390c70a2 Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
This commit is contained in:
parent
f295ef1a4b
commit
f950a0f0c0
@ -52,8 +52,15 @@
|
||||
|
||||
void *QCocoaNativeInterface::nativeResourceForWindow(const QByteArray &resourceString, QWindow *window)
|
||||
{
|
||||
if (!window->handle()) {
|
||||
qWarning("QCocoaNativeInterface::nativeResourceForWindow: Native window has not been created.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (resourceString == "nsopenglcontext") {
|
||||
return static_cast<QCocoaWindow *>(window->handle())->currentContext()->nsOpenGLContext();
|
||||
} else if (resourceString == "nsview") {
|
||||
return static_cast<QCocoaWindow *>(window->handle())->m_contentView;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -65,6 +65,25 @@
|
||||
@end
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
// QCocoaWindow
|
||||
//
|
||||
// QCocoaWindow is an NSView (not an NSWindow!) in the sense
|
||||
// that it relies on a NSView for all event handling and
|
||||
// graphics output and does not require a NSWindow, except for
|
||||
// for the window-related functions like setWindowTitle.
|
||||
//
|
||||
// As a consequence of this it is possible to embed the QCocoaWindow
|
||||
// in an NSView hierarchy by getting a pointer to the "backing"
|
||||
// NSView and not calling QCocoaWindow::show():
|
||||
//
|
||||
// QWindow *qtWindow = new MyWindow();
|
||||
// qtWindow->create();
|
||||
// QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
|
||||
// NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
|
||||
// [parentView addSubview:qtView];
|
||||
//
|
||||
// See the qt_on_cocoa manual tests for a working example, located
|
||||
// in tests/manual/cocoa at the time of writing.
|
||||
|
||||
class QCocoaWindow : public QPlatformWindow
|
||||
{
|
||||
@ -97,12 +116,17 @@ protected:
|
||||
QRect windowGeometry() const;
|
||||
QCocoaWindow *parentCocoaWindow() const;
|
||||
|
||||
private:
|
||||
// private:
|
||||
public: // for QNSView
|
||||
friend class QCocoaBackingStore;
|
||||
NSWindow *m_nsWindow;
|
||||
friend class QCocoaNativeInterface;
|
||||
|
||||
QNSView *m_contentView;
|
||||
QNSWindow *m_nsWindow;
|
||||
|
||||
quint32 m_windowAttributes;
|
||||
quint32 m_windowClass;
|
||||
bool m_inConstructor;
|
||||
QCocoaGLContext *m_glContext;
|
||||
};
|
||||
|
||||
|
@ -81,6 +81,7 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
|
||||
, m_windowAttributes(0)
|
||||
, m_windowClass(0)
|
||||
, m_glContext(0)
|
||||
, m_inConstructor(true)
|
||||
{
|
||||
QCocoaAutoReleasePool pool;
|
||||
|
||||
@ -97,7 +98,7 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
|
||||
// QCocoaWindow is deleted by Qt.
|
||||
[m_nsWindow setReleasedWhenClosed : NO];
|
||||
|
||||
m_contentView = [[QNSView alloc] initWithQWindow:tlw];
|
||||
m_contentView = [[QNSView alloc] initWithQWindow:tlw platformWindow:this];
|
||||
|
||||
// ### Accept touch events by default.
|
||||
// Beware that enabling touch events has a negative impact on the overall performance.
|
||||
@ -107,6 +108,7 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw)
|
||||
setGeometry(tlw->geometry());
|
||||
|
||||
[m_nsWindow setContentView:m_contentView];
|
||||
m_inConstructor = false;
|
||||
}
|
||||
|
||||
QCocoaWindow::~QCocoaWindow()
|
||||
@ -124,8 +126,6 @@ void QCocoaWindow::setGeometry(const QRect &rect)
|
||||
QPlatformWindow::setGeometry(rect);
|
||||
|
||||
NSRect bounds = qt_mac_flipRect(rect, window());
|
||||
|
||||
[[m_nsWindow contentView] setFrameSize:bounds.size];
|
||||
[m_nsWindow setContentSize : bounds.size];
|
||||
[m_nsWindow setFrameOrigin : bounds.origin];
|
||||
}
|
||||
@ -232,23 +232,17 @@ void QCocoaWindow::windowDidMove()
|
||||
{
|
||||
NSRect rect = [[m_nsWindow contentView]frame];
|
||||
NSRect windowRect = [m_nsWindow frame];
|
||||
|
||||
QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
|
||||
setGeometry(geo);
|
||||
QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
|
||||
[[m_nsWindow contentView] setFrameSize:rect.size];
|
||||
}
|
||||
|
||||
void QCocoaWindow::windowDidResize()
|
||||
{
|
||||
NSRect rect = [[m_nsWindow contentView]frame];
|
||||
NSRect windowRect = [m_nsWindow frame];
|
||||
|
||||
QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
|
||||
setGeometry(geo);
|
||||
QWindowSystemInterface::handleSynchronousGeometryChange(window(), geo);
|
||||
// Call setFrameSize which will trigger a frameDidChangeNotificatio on QNSView.
|
||||
[[m_nsWindow contentView] setFrameSize:rect.size];
|
||||
}
|
||||
|
||||
|
||||
void QCocoaWindow::windowWillClose()
|
||||
{
|
||||
QWindowSystemInterface::handleSynchronousCloseEvent(window());
|
||||
|
@ -47,15 +47,17 @@
|
||||
#include <QtGui/QImage>
|
||||
#include <QtGui/QAccessible>
|
||||
|
||||
class QCocoaWindow;
|
||||
@interface QNSView : NSView {
|
||||
CGImageRef m_cgImage;
|
||||
QWindow *m_window;
|
||||
QCocoaWindow *m_platformWindow;
|
||||
Qt::MouseButtons m_buttons;
|
||||
QAccessibleInterface *m_accessibleRoot;
|
||||
}
|
||||
|
||||
- (id)init;
|
||||
- (id)initWithQWindow:(QWindow *)window;
|
||||
- (id)initWithQWindow:(QWindow *)window platformWindow:(QCocoaWindow *) platformWindow;
|
||||
|
||||
- (void)setImage:(QImage *)image;
|
||||
- (void)drawRect:(NSRect)dirtyRect;
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include <Carbon/Carbon.h>
|
||||
|
||||
#include "qnsview.h"
|
||||
#include "qcocoawindow.h"
|
||||
#include "qcocoahelpers.h"
|
||||
#include "qmultitouch_mac_p.h"
|
||||
|
||||
@ -81,12 +82,14 @@ static QTouchDevice *touchDevice = 0;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithQWindow:(QWindow *)window {
|
||||
- (id)initWithQWindow:(QWindow *)window platformWindow:(QCocoaWindow *) platformWindow
|
||||
{
|
||||
self = [self init];
|
||||
if (!self)
|
||||
return 0;
|
||||
|
||||
m_window = window;
|
||||
m_platformWindow = platformWindow;
|
||||
m_accessibleRoot = 0;
|
||||
|
||||
#ifdef QT_COCOA_ENABLE_ACCESSIBILITY_INSPECTOR
|
||||
@ -106,9 +109,31 @@ static QTouchDevice *touchDevice = 0;
|
||||
m_accessibleRoot = window->accessibleRoot();
|
||||
#endif
|
||||
|
||||
[self setPostsFrameChangedNotifications : YES];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(frameDidChangeNotification:)
|
||||
name:NSViewFrameDidChangeNotification
|
||||
object:self];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) frameDidChangeNotification: (NSNotification *) notification
|
||||
{
|
||||
NSRect rect = [self frame];
|
||||
NSRect windowRect = [[self window] frame];
|
||||
QRect geo(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
|
||||
|
||||
// Call setGeometry on QPlatformWindow. (not on QCocoaWindow,
|
||||
// doing that will initiate a geometry change it and possibly create
|
||||
// an infinite loop when this notification is triggered again.)
|
||||
m_platformWindow->QPlatformWindow::setGeometry(geo);
|
||||
|
||||
// Send a geometry change event to Qt, if it's ready to handle events
|
||||
if (!m_platformWindow->m_inConstructor)
|
||||
QWindowSystemInterface::handleSynchronousGeometryChange(m_window, geo);
|
||||
}
|
||||
|
||||
- (void) setImage:(QImage *)image
|
||||
{
|
||||
CGImageRelease(m_cgImage);
|
||||
|
213
tests/manual/cocoa/qt_on_cocoa/main.mm
Normal file
213
tests/manual/cocoa/qt_on_cocoa/main.mm
Normal file
@ -0,0 +1,213 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** GNU Lesser General Public License Usage
|
||||
** This file may be used under the terms of the GNU Lesser General Public
|
||||
** License version 2.1 as published by the Free Software Foundation and
|
||||
** appearing in the file LICENSE.LGPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU Lesser
|
||||
** General Public License version 2.1 requirements will be met:
|
||||
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Nokia gives you certain additional
|
||||
** rights. These rights are described in the Nokia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU General
|
||||
** Public License version 3.0 as published by the Free Software Foundation
|
||||
** and appearing in the file LICENSE.GPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU General
|
||||
** Public License version 3.0 requirements will be met:
|
||||
** http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
** Other Usage
|
||||
** Alternatively, this file may be used in accordance with the terms and
|
||||
** conditions contained in a signed written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
#include <QtDeclarative>
|
||||
|
||||
#include <QtWidgets/QtWidgets>
|
||||
#include <private/qwidgetwindow_qpa_p.h>
|
||||
#include <QtGui/qplatformnativeinterface_qpa.h>
|
||||
|
||||
#include <QtGui/QPixmap>
|
||||
|
||||
#include "window.h"
|
||||
|
||||
#include <Cocoa/Cocoa.h>
|
||||
|
||||
|
||||
@interface FilledView : NSView
|
||||
{
|
||||
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
@implementation FilledView
|
||||
|
||||
- (void)drawRect:(NSRect)dirtyRect {
|
||||
// set any NSColor for filling, say white:
|
||||
[[NSColor redColor] setFill];
|
||||
NSRectFill(dirtyRect);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface QtMacToolbarDelegate : NSObject <NSToolbarDelegate>
|
||||
{
|
||||
@public
|
||||
NSToolbar *toolbar;
|
||||
}
|
||||
|
||||
- (id)init;
|
||||
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted;
|
||||
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)tb;
|
||||
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
|
||||
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
|
||||
@end
|
||||
|
||||
@implementation QtMacToolbarDelegate
|
||||
|
||||
- (id)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)tb
|
||||
{
|
||||
Q_UNUSED(tb);
|
||||
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
|
||||
// [array addObject : NSToolbarPrintItemIdentifier];
|
||||
// [array addObject : NSToolbarShowColorsItemIdentifier];
|
||||
[array addObject : @"filledView"];
|
||||
return array;
|
||||
}
|
||||
|
||||
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)tb
|
||||
{
|
||||
Q_UNUSED(tb);
|
||||
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
|
||||
// [array addObject : NSToolbarPrintItemIdentifier];
|
||||
// [array addObject : NSToolbarShowColorsItemIdentifier];
|
||||
[array addObject : @"filledView"];
|
||||
return array;
|
||||
}
|
||||
|
||||
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)tb
|
||||
{
|
||||
Q_UNUSED(tb);
|
||||
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
|
||||
return array;
|
||||
}
|
||||
|
||||
- (IBAction)itemClicked:(id)sender
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
- (NSToolbarItem *) toolbar: (NSToolbar *)tb itemForItemIdentifier: (NSString *) itemIdentifier willBeInsertedIntoToolbar:(BOOL) willBeInserted
|
||||
{
|
||||
Q_UNUSED(tb);
|
||||
Q_UNUSED(willBeInserted);
|
||||
//const QString identifier = toQString(itemIdentifier);
|
||||
//NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
|
||||
//return toolbarItem;
|
||||
|
||||
//NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
|
||||
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
|
||||
FilledView *theView = [[FilledView alloc] init];
|
||||
[toolbarItem setView : theView];
|
||||
[toolbarItem setMinSize : NSMakeSize(400, 40)];
|
||||
[toolbarItem setMaxSize : NSMakeSize(4000, 40)];
|
||||
return toolbarItem;
|
||||
}
|
||||
@end
|
||||
|
||||
@interface WindowAndViewAndQtCreator : NSObject {}
|
||||
- (void)createWindowAndViewAndQt;
|
||||
@end
|
||||
|
||||
@implementation WindowAndViewAndQtCreator
|
||||
- (void)createWindowAndViewAndQt {
|
||||
|
||||
// Create the window
|
||||
NSRect frame = NSMakeRect(500, 500, 500, 500);
|
||||
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
|
||||
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
|
||||
backing:NSBackingStoreBuffered
|
||||
defer:NO];
|
||||
|
||||
NSString *title = @"This the NSWindow window";
|
||||
[window setTitle:title];
|
||||
|
||||
[window setBackgroundColor:[NSColor blueColor]];
|
||||
|
||||
// Create a tool bar, set Qt delegate
|
||||
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier : @"foobartoolbar"];
|
||||
QtMacToolbarDelegate *delegate = [[QtMacToolbarDelegate alloc] init];
|
||||
[toolbar setDelegate : delegate];
|
||||
[window setToolbar : toolbar];
|
||||
|
||||
// Create the QWindow, don't show it.
|
||||
Window *qtWindow = new Window();
|
||||
qtWindow->create();
|
||||
|
||||
//QSGView *qtWindow = new QSGView();
|
||||
//qtWindow->setSource(QUrl::fromLocalFile("/Users/msorvig/code/qt5/qtdeclarative/examples/declarative/samegame/samegame.qml"));
|
||||
// qtWindow->setWindowFlags(Qt::WindowType(13)); // 13: NativeEmbeddedWindow
|
||||
|
||||
// Get the nsview from the QWindow, set it as the content view
|
||||
// on the NSWindow created above.
|
||||
QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
|
||||
NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
|
||||
[window setContentView:qtView];
|
||||
[window makeKeyAndOrderFront:NSApp];
|
||||
}
|
||||
@end
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
// fake NSApplicationMain() implementation follows:
|
||||
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
||||
[NSApplication sharedApplication];
|
||||
|
||||
// schedule call to create the UI.
|
||||
WindowAndViewAndQtCreator *windowAndViewAndQtCreator= [WindowAndViewAndQtCreator alloc];
|
||||
[NSTimer scheduledTimerWithTimeInterval:0 target:windowAndViewAndQtCreator selector:@selector(createWindowAndViewAndQt) userInfo:nil repeats:NO];
|
||||
|
||||
[(NSApplication *)NSApp run];
|
||||
[NSApp release];
|
||||
[pool release];
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
12
tests/manual/cocoa/qt_on_cocoa/qt_on_cocoa.pro
Normal file
12
tests/manual/cocoa/qt_on_cocoa/qt_on_cocoa.pro
Normal file
@ -0,0 +1,12 @@
|
||||
TEMPLATE = app
|
||||
|
||||
OBJECTIVE_SOURCES += main.mm
|
||||
HEADERS += window.h
|
||||
SOURCES += window.cpp
|
||||
LIBS += -framework Cocoa
|
||||
|
||||
QMAKE_INFO_PLIST = Info_mac.plist
|
||||
OTHER_FILES = Info_mac.plist
|
||||
QT += gui widgets widgets-private gui-private core-private
|
||||
|
||||
QT += declarative
|
203
tests/manual/cocoa/qt_on_cocoa/window.cpp
Normal file
203
tests/manual/cocoa/qt_on_cocoa/window.cpp
Normal file
@ -0,0 +1,203 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** GNU Lesser General Public License Usage
|
||||
** This file may be used under the terms of the GNU Lesser General Public
|
||||
** License version 2.1 as published by the Free Software Foundation and
|
||||
** appearing in the file LICENSE.LGPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU Lesser
|
||||
** General Public License version 2.1 requirements will be met:
|
||||
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Nokia gives you certain additional
|
||||
** rights. These rights are described in the Nokia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU General
|
||||
** Public License version 3.0 as published by the Free Software Foundation
|
||||
** and appearing in the file LICENSE.GPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU General
|
||||
** Public License version 3.0 requirements will be met:
|
||||
** http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
** Other Usage
|
||||
** Alternatively, this file may be used in accordance with the terms and
|
||||
** conditions contained in a signed written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "window.h"
|
||||
|
||||
#include <private/qguiapplication_p.h>
|
||||
|
||||
#include <QBackingStore>
|
||||
#include <QPainter>
|
||||
#include <QtGui>
|
||||
|
||||
static int colorIndexId = 0;
|
||||
|
||||
QColor colorTable[] =
|
||||
{
|
||||
QColor("#f09f8f"),
|
||||
QColor("#a2bff2"),
|
||||
QColor("#c0ef8f")
|
||||
};
|
||||
|
||||
Window::Window(QScreen *screen)
|
||||
: QWindow(screen)
|
||||
, m_backgroundColorIndex(colorIndexId++)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
Window::Window(QWindow *parent)
|
||||
: QWindow(parent)
|
||||
, m_backgroundColorIndex(colorIndexId++)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
void Window::initialize()
|
||||
{
|
||||
if (parent())
|
||||
setGeometry(QRect(160, 120, 320, 240));
|
||||
else {
|
||||
setGeometry(QRect(10, 10, 640, 480));
|
||||
|
||||
setSizeIncrement(QSize(10, 10));
|
||||
setBaseSize(QSize(640, 480));
|
||||
setMinimumSize(QSize(240, 160));
|
||||
setMaximumSize(QSize(800, 600));
|
||||
}
|
||||
|
||||
create();
|
||||
m_backingStore = new QBackingStore(this);
|
||||
|
||||
m_image = QImage(geometry().size(), QImage::Format_RGB32);
|
||||
m_image.fill(colorTable[m_backgroundColorIndex % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba());
|
||||
|
||||
m_lastPos = QPoint(-1, -1);
|
||||
m_renderTimer = 0;
|
||||
}
|
||||
|
||||
void Window::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
m_lastPos = event->pos();
|
||||
}
|
||||
|
||||
void Window::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
if (m_lastPos != QPoint(-1, -1)) {
|
||||
QPainter p(&m_image);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
p.drawLine(m_lastPos, event->pos());
|
||||
m_lastPos = event->pos();
|
||||
}
|
||||
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
void Window::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (m_lastPos != QPoint(-1, -1)) {
|
||||
QPainter p(&m_image);
|
||||
p.setRenderHint(QPainter::Antialiasing);
|
||||
p.drawLine(m_lastPos, event->pos());
|
||||
m_lastPos = QPoint(-1, -1);
|
||||
}
|
||||
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
void Window::exposeEvent(QExposeEvent *)
|
||||
{
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
void Window::resizeEvent(QResizeEvent *)
|
||||
{
|
||||
QImage old = m_image;
|
||||
|
||||
//qDebug() << "Window::resizeEvent" << width << height;
|
||||
|
||||
int width = qMax(geometry().width(), old.width());
|
||||
int height = qMax(geometry().height(), old.height());
|
||||
|
||||
if (width > old.width() || height > old.height()) {
|
||||
m_image = QImage(width, height, QImage::Format_RGB32);
|
||||
m_image.fill(colorTable[(m_backgroundColorIndex) % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba());
|
||||
|
||||
QPainter p(&m_image);
|
||||
p.drawImage(0, 0, old);
|
||||
}
|
||||
|
||||
render();
|
||||
}
|
||||
|
||||
void Window::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Backspace:
|
||||
m_text.chop(1);
|
||||
break;
|
||||
case Qt::Key_Enter:
|
||||
case Qt::Key_Return:
|
||||
m_text.append('\n');
|
||||
break;
|
||||
default:
|
||||
m_text.append(event->text());
|
||||
break;
|
||||
}
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
void Window::scheduleRender()
|
||||
{
|
||||
if (!m_renderTimer)
|
||||
m_renderTimer = startTimer(1);
|
||||
}
|
||||
|
||||
void Window::timerEvent(QTimerEvent *)
|
||||
{
|
||||
render();
|
||||
killTimer(m_renderTimer);
|
||||
m_renderTimer = 0;
|
||||
}
|
||||
|
||||
void Window::render()
|
||||
{
|
||||
QRect rect(QPoint(), geometry().size());
|
||||
|
||||
m_backingStore->resize(rect.size());
|
||||
|
||||
m_backingStore->beginPaint(rect);
|
||||
|
||||
QPaintDevice *device = m_backingStore->paintDevice();
|
||||
|
||||
QPainter p(device);
|
||||
p.drawImage(0, 0, m_image);
|
||||
|
||||
QFont font;
|
||||
font.setPixelSize(32);
|
||||
|
||||
p.setFont(font);
|
||||
p.drawText(rect, 0, m_text);
|
||||
|
||||
m_backingStore->endPaint();
|
||||
m_backingStore->flush(rect);
|
||||
}
|
||||
|
||||
|
74
tests/manual/cocoa/qt_on_cocoa/window.h
Normal file
74
tests/manual/cocoa/qt_on_cocoa/window.h
Normal file
@ -0,0 +1,74 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
||||
** All rights reserved.
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** GNU Lesser General Public License Usage
|
||||
** This file may be used under the terms of the GNU Lesser General Public
|
||||
** License version 2.1 as published by the Free Software Foundation and
|
||||
** appearing in the file LICENSE.LGPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU Lesser
|
||||
** General Public License version 2.1 requirements will be met:
|
||||
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Nokia gives you certain additional
|
||||
** rights. These rights are described in the Nokia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU General
|
||||
** Public License version 3.0 as published by the Free Software Foundation
|
||||
** and appearing in the file LICENSE.GPL included in the packaging of this
|
||||
** file. Please review the following information to ensure the GNU General
|
||||
** Public License version 3.0 requirements will be met:
|
||||
** http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
** Other Usage
|
||||
** Alternatively, this file may be used in accordance with the terms and
|
||||
** conditions contained in a signed written agreement between you and Nokia.
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QWindow>
|
||||
#include <QImage>
|
||||
|
||||
class Window : public QWindow
|
||||
{
|
||||
public:
|
||||
Window(QWindow *parent = 0);
|
||||
Window(QScreen *screen);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *);
|
||||
void mouseMoveEvent(QMouseEvent *);
|
||||
void mouseReleaseEvent(QMouseEvent *);
|
||||
|
||||
void keyPressEvent(QKeyEvent *);
|
||||
|
||||
void exposeEvent(QExposeEvent *);
|
||||
void resizeEvent(QResizeEvent *);
|
||||
|
||||
void timerEvent(QTimerEvent *);
|
||||
|
||||
private:
|
||||
void render();
|
||||
void scheduleRender();
|
||||
void initialize();
|
||||
|
||||
QString m_text;
|
||||
QImage m_image;
|
||||
QPoint m_lastPos;
|
||||
int m_backgroundColorIndex;
|
||||
QBackingStore *m_backingStore;
|
||||
int m_renderTimer;
|
||||
};
|
Loading…
Reference in New Issue
Block a user