diff options
author | Morten Johan Sørvig <morten.sorvig@digia.com> | 2013-01-04 13:04:51 +0100 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@digia.com> | 2013-01-31 08:29:56 +0100 |
commit | 77ea1322d192cd3b9a807370cc90df5a598a2550 (patch) | |
tree | 2d26b4cd234ea32acd2b70815e155d30dde41289 /examples | |
parent | 2f604147e4cc1ab5926368e80a0e5586b918bbab (diff) |
Add Qt::SubView example.
Change-Id: I9718de6db6672b539f0d5dd36c94dbbdd4604ed7
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/embeddedqwindow/embeddedqwindow.pro | 11 | ||||
-rw-r--r-- | examples/embeddedqwindow/main.mm | 116 | ||||
-rw-r--r-- | examples/embeddedqwindow/window.cpp | 205 | ||||
-rw-r--r-- | examples/embeddedqwindow/window.h | 74 |
4 files changed, 406 insertions, 0 deletions
diff --git a/examples/embeddedqwindow/embeddedqwindow.pro b/examples/embeddedqwindow/embeddedqwindow.pro new file mode 100644 index 0000000..73a0ae4 --- /dev/null +++ b/examples/embeddedqwindow/embeddedqwindow.pro @@ -0,0 +1,11 @@ +TEMPLATE = app + +OBJECTIVE_SOURCES += main.mm +HEADERS += window.h +SOURCES += window.cpp +LIBS += -framework Cocoa + +QT += gui widgets +QT += widgets-private gui-private core-private + +DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/examples/embeddedqwindow/main.mm b/examples/embeddedqwindow/main.mm new file mode 100644 index 0000000..89ae8b8 --- /dev/null +++ b/examples/embeddedqwindow/main.mm @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "window.h" + +#include <QtGui> +#include <qpa/qplatformnativeinterface.h> + + +#include <Cocoa/Cocoa.h> + + +NSView *getEmbeddableView(QWindow *qtWindow) +{ + // Set Qt::SubWindow flag. Should be done before crate() is + // called - if you call create() or caused it ot be called + // before calling this function you need to set SubWindow + // yourself + qtWindow->setFlags(qtWindow->flags() | Qt::SubWindow); + + // Make sure the platform window is created + qtWindow->create(); + + // Get the Qt content NSView for the QWindow forom the Qt platform plugin + QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface(); + NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow); + return qtView; // qtView is ready for use. +} + +@interface WindowCreator : NSObject {} +- (void)createWindow; +@end + +@implementation WindowCreator +- (void)createWindow { + + // Create the NSWindow + NSRect frame = NSMakeRect(500, 500, 500, 500); + NSWindow* window = [[NSWindow alloc] initWithContentRect:frame + styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask + backing:NSBackingStoreBuffered + defer:NO]; + + [window setTitle:@"NSWindow"]; + [window setBackgroundColor:[NSColor blueColor]]; // if you see blue something is wrong + + // Create the QWindow and embed its view. + Window *qtWindow = new Window(); // ### who owns this window? + NSView *qtView = getEmbeddableView(qtWindow); + [window setContentView:qtView]; + + // Show the NSWindow + [window makeKeyAndOrderFront:NSApp]; +} +@end + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + // Start Cocoa. Create NSApplicaiton. + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + [NSApplication sharedApplication]; + + // Schedule call to create the UI using a timer. + WindowCreator *windowCreator= [WindowCreator alloc]; + [NSTimer scheduledTimerWithTimeInterval:0 target:windowCreator selector:@selector(createWindow) userInfo:nil repeats:NO]; + + // Starte the Cocoa event loop. + [(NSApplication *)NSApp run]; + [NSApp release]; + [pool release]; + exit(0); + return 0; +} + + + diff --git a/examples/embeddedqwindow/window.cpp b/examples/embeddedqwindow/window.cpp new file mode 100644 index 0000000..8ae918c --- /dev/null +++ b/examples/embeddedqwindow/window.cpp @@ -0,0 +1,205 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "window.h" + +#include <private/qguiapplication_p.h> + +#include <QBackingStore> +#include <QPainter> +#include <QtWidgets> + +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)); + } + + setFlags(flags() | Qt::SubWindow); // Don't create a toplevel window. + + 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); +} + + diff --git a/examples/embeddedqwindow/window.h b/examples/embeddedqwindow/window.h new file mode 100644 index 0000000..cdd6ebd --- /dev/null +++ b/examples/embeddedqwindow/window.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $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; +}; |