Clean up qt_on_cocoa manual test.

Focus the test on embedding QWindow: Remove the
NSToolBar code. Use standard NSApplication startup
code. Use winId() to access the NSView instead of
QPlatformNativeInterface (which is gui-private).
Rename Window -> RasterWindow.

Change-Id: Ie98cb73020d2721743bb665b89b17f07f2bf984b
Reviewed-by: Morten Johan Sørvig <morten.sorvig@theqtcompany.com>
This commit is contained in:
Morten Johan Sørvig 2015-01-27 14:13:39 +01:00 committed by Morten Johan Sørvig
parent 737eccf1ef
commit 795400d1e5
4 changed files with 53 additions and 169 deletions

View File

@ -31,123 +31,35 @@
** **
****************************************************************************/ ****************************************************************************/
#include "rasterwindow.h"
#include <QtGui> #include <QtGui>
#include <QtDeclarative>
#include <QtWidgets/QtWidgets> #include <QtWidgets/QtWidgets>
#include <private/qwidgetwindow_p.h>
#include <QtGui/qpa/qplatformnativeinterface.h>
#include <QtGui/QPixmap>
#include "window.h"
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
@interface FilledView : NSView QGuiApplication *m_app;
{ QWindow *m_window;
} }
- (AppDelegate *) initWithArgc:(int)argc argv:(const char **)argv;
- (void) applicationWillFinishLaunching: (NSNotification *)notification;
- (void)applicationWillTerminate:(NSNotification *)notification;
@end @end
@implementation FilledView @implementation AppDelegate
- (AppDelegate *) initWithArgc:(int)argc argv:(const char **)argv
- (void)drawRect:(NSRect)dirtyRect {
// set any NSColor for filling, say white:
[[NSColor redColor] setFill];
NSRectFill(dirtyRect);
}
@end
@interface QtMacToolbarDelegate : NSObject <NSToolbarDelegate>
{ {
@public m_app = new QGuiApplication(argc, const_cast<char **>(argv));
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; return self;
} }
- (void)dealloc - (void) applicationWillFinishLaunching: (NSNotification *)notification
{ {
[super dealloc]; Q_UNUSED(notification);
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)tb // Create the NSWindow
{
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); NSRect frame = NSMakeRect(500, 500, 500, 500);
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
@ -156,49 +68,31 @@
NSString *title = @"This the NSWindow window"; NSString *title = @"This the NSWindow window";
[window setTitle:title]; [window setTitle:title];
[window setBackgroundColor:[NSColor blueColor]]; [window setBackgroundColor:[NSColor blueColor]];
// Create a tool bar, set Qt delegate // Create the QWindow, use its NSView as the content view
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier : @"foobartoolbar"]; m_window = new RasterWindow();
QtMacToolbarDelegate *delegate = [[QtMacToolbarDelegate alloc] init]; [window setContentView:reinterpret_cast<NSView *>(m_window->winId())];
[toolbar setDelegate : delegate];
[window setToolbar : toolbar];
// Create the QWindow, don't show it. // Show the NSWindow
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]; [window makeKeyAndOrderFront:NSApp];
} }
- (void)applicationWillTerminate:(NSNotification *)notification
{
Q_UNUSED(notification);
delete m_window;
delete m_app;
}
@end @end
int main(int argc, char *argv[]) int main(int argc, const char *argv[])
{ {
QGuiApplication app(argc, argv); // Create NSApplicaiton with delgate
NSApplication *app =[NSApplication sharedApplication];
// fake NSApplicationMain() implementation follows: app.delegate = [[AppDelegate alloc] initWithArgc:argc argv:argv];
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; return NSApplicationMain (argc, argv);
[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;
} }

View File

@ -1,13 +1,11 @@
TEMPLATE = app TEMPLATE = app
OBJECTIVE_SOURCES += main.mm OBJECTIVE_SOURCES += main.mm
HEADERS += window.h HEADERS += rasterwindow.h
SOURCES += window.cpp SOURCES += rasterwindow.cpp
LIBS += -framework Cocoa LIBS += -framework Cocoa
QMAKE_INFO_PLIST = Info_mac.plist QT += gui widgets quick
OTHER_FILES = Info_mac.plist
QT += gui widgets widgets-private gui-private core-private
QT += declarative QT += quick
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

View File

@ -31,9 +31,9 @@
** **
****************************************************************************/ ****************************************************************************/
#include "window.h" #include "rasterwindow.h"
#include <private/qguiapplication_p.h> //#include <private/qguiapplication_p.h>
#include <QBackingStore> #include <QBackingStore>
#include <QPainter> #include <QPainter>
@ -48,21 +48,14 @@ QColor colorTable[] =
QColor("#c0ef8f") QColor("#c0ef8f")
}; };
Window::Window(QScreen *screen) RasterWindow::RasterWindow(QRasterWindow *parent)
: QWindow(screen) : QRasterWindow(parent)
, m_backgroundColorIndex(colorIndexId++) , m_backgroundColorIndex(colorIndexId++)
{ {
initialize(); initialize();
} }
Window::Window(QWindow *parent) void RasterWindow::initialize()
: QWindow(parent)
, m_backgroundColorIndex(colorIndexId++)
{
initialize();
}
void Window::initialize()
{ {
if (parent()) if (parent())
setGeometry(QRect(160, 120, 320, 240)); setGeometry(QRect(160, 120, 320, 240));
@ -85,12 +78,12 @@ void Window::initialize()
m_renderTimer = 0; m_renderTimer = 0;
} }
void Window::mousePressEvent(QMouseEvent *event) void RasterWindow::mousePressEvent(QMouseEvent *event)
{ {
m_lastPos = event->pos(); m_lastPos = event->pos();
} }
void Window::mouseMoveEvent(QMouseEvent *event) void RasterWindow::mouseMoveEvent(QMouseEvent *event)
{ {
if (m_lastPos != QPoint(-1, -1)) { if (m_lastPos != QPoint(-1, -1)) {
QPainter p(&m_image); QPainter p(&m_image);
@ -102,7 +95,7 @@ void Window::mouseMoveEvent(QMouseEvent *event)
scheduleRender(); scheduleRender();
} }
void Window::mouseReleaseEvent(QMouseEvent *event) void RasterWindow::mouseReleaseEvent(QMouseEvent *event)
{ {
if (m_lastPos != QPoint(-1, -1)) { if (m_lastPos != QPoint(-1, -1)) {
QPainter p(&m_image); QPainter p(&m_image);
@ -114,16 +107,16 @@ void Window::mouseReleaseEvent(QMouseEvent *event)
scheduleRender(); scheduleRender();
} }
void Window::exposeEvent(QExposeEvent *) void RasterWindow::exposeEvent(QExposeEvent *)
{ {
scheduleRender(); scheduleRender();
} }
void Window::resizeEvent(QResizeEvent *) void RasterWindow::resizeEvent(QResizeEvent *)
{ {
QImage old = m_image; QImage old = m_image;
//qDebug() << "Window::resizeEvent" << width << height; //qDebug() << "RasterWindow::resizeEvent" << width << height;
int width = qMax(geometry().width(), old.width()); int width = qMax(geometry().width(), old.width());
int height = qMax(geometry().height(), old.height()); int height = qMax(geometry().height(), old.height());
@ -139,7 +132,7 @@ void Window::resizeEvent(QResizeEvent *)
render(); render();
} }
void Window::keyPressEvent(QKeyEvent *event) void RasterWindow::keyPressEvent(QKeyEvent *event)
{ {
switch (event->key()) { switch (event->key()) {
case Qt::Key_Backspace: case Qt::Key_Backspace:
@ -156,20 +149,20 @@ void Window::keyPressEvent(QKeyEvent *event)
scheduleRender(); scheduleRender();
} }
void Window::scheduleRender() void RasterWindow::scheduleRender()
{ {
if (!m_renderTimer) if (!m_renderTimer)
m_renderTimer = startTimer(1); m_renderTimer = startTimer(1);
} }
void Window::timerEvent(QTimerEvent *) void RasterWindow::timerEvent(QTimerEvent *)
{ {
render(); render();
killTimer(m_renderTimer); killTimer(m_renderTimer);
m_renderTimer = 0; m_renderTimer = 0;
} }
void Window::render() void RasterWindow::render()
{ {
QRect rect(QPoint(), geometry().size()); QRect rect(QPoint(), geometry().size());

View File

@ -31,14 +31,13 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QWindow> #include <QRasterWindow>
#include <QImage> #include <QImage>
class Window : public QWindow class RasterWindow : public QRasterWindow
{ {
public: public:
Window(QWindow *parent = 0); RasterWindow(QRasterWindow *parent = 0);
Window(QScreen *screen);
protected: protected:
void mousePressEvent(QMouseEvent *); void mousePressEvent(QMouseEvent *);