diff options
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/macunifiedtoolbar.pro | 9 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/macunifiedtoolbar.qrc | 5 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/main.cpp | 27 | ||||
-rw-r--r-- | examples/macunifiedtoolbar/qtlogo.png | bin | 0 -> 1478 bytes | |||
-rw-r--r-- | src/qtmacextras.pri | 19 | ||||
-rw-r--r-- | src/qtmactoolbardelegate.h | 80 | ||||
-rw-r--r-- | src/qtmactoolbardelegate.mm | 246 | ||||
-rw-r--r-- | src/qtmactoolbutton.h | 85 | ||||
-rw-r--r-- | src/qtmactoolbutton.mm | 80 | ||||
-rw-r--r-- | src/qtmacunifiedtoolbar.h | 88 | ||||
-rw-r--r-- | src/qtmacunifiedtoolbar.mm | 162 |
12 files changed, 802 insertions, 0 deletions
@@ -0,0 +1 @@ +Qt Mac Extras: Platform spesific Components for Mac OS X. diff --git a/examples/macunifiedtoolbar/macunifiedtoolbar.pro b/examples/macunifiedtoolbar/macunifiedtoolbar.pro new file mode 100644 index 0000000..e839421 --- /dev/null +++ b/examples/macunifiedtoolbar/macunifiedtoolbar.pro @@ -0,0 +1,9 @@ +include (../../src/qtmacextras.pri) + + +QT += widgets +SOURCES += main.cpp + +RESOURCES += \ + macunifiedtoolbar.qrc + diff --git a/examples/macunifiedtoolbar/macunifiedtoolbar.qrc b/examples/macunifiedtoolbar/macunifiedtoolbar.qrc new file mode 100644 index 0000000..04a9419 --- /dev/null +++ b/examples/macunifiedtoolbar/macunifiedtoolbar.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>qtlogo.png</file> + </qresource> +</RCC> diff --git a/examples/macunifiedtoolbar/main.cpp b/examples/macunifiedtoolbar/main.cpp new file mode 100644 index 0000000..8d58cf9 --- /dev/null +++ b/examples/macunifiedtoolbar/main.cpp @@ -0,0 +1,27 @@ +#include "qtmacunifiedtoolbar.h" +#include <QApplication> + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QWidget widget; + widget.resize(300, 300); + widget.setWindowTitle("Qt Mac Toolbar Example"); + + QtMacUnifiedToolBar toolBar; + toolBar.addAction(QIcon(":/qtlogo.png"), "Hello"); + toolBar.addAction(QIcon(":/qtlogo.png"), "World"); + toolBar.addStandardItem(QtMacToolButton::FlexibleSpace); + toolBar.addStandardItem(QtMacToolButton::ShowColors); + toolBar.addStandardItem(QtMacToolButton::ShowFonts); + + toolBar.addAllowedAction(QIcon(":/qtlogo.png"), "Extra Button 1"); + toolBar.addAllowedAction(QIcon(":/qtlogo.png"), "Extra Button 2"); + + toolBar.showInWindow(widget.windowHandle()); + widget.show(); + + return app.exec(); +} + diff --git a/examples/macunifiedtoolbar/qtlogo.png b/examples/macunifiedtoolbar/qtlogo.png Binary files differnew file mode 100644 index 0000000..d75936b --- /dev/null +++ b/examples/macunifiedtoolbar/qtlogo.png diff --git a/src/qtmacextras.pri b/src/qtmacextras.pri new file mode 100644 index 0000000..76d55af --- /dev/null +++ b/src/qtmacextras.pri @@ -0,0 +1,19 @@ + +#QT += declarative qml quick +QT += widgets gui-private +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD + +# qtmacunifiedtoolbar +HEADERS += $$PWD/qtmacunifiedtoolbar.h \ + $$PWD/qtmactoolbardelegate.h \ + $$PWD/qtmactoolbutton.h + +mac { + OBJECTIVE_SOURCES += $$PWD/qtmacunifiedtoolbar.mm \ + $$PWD/qtmactoolbardelegate.mm \ + $$PWD/qtmactoolbutton.mm + + + LIBS *= -framework AppKit +} diff --git a/src/qtmactoolbardelegate.h b/src/qtmactoolbardelegate.h new file mode 100644 index 0000000..157234a --- /dev/null +++ b/src/qtmactoolbardelegate.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#import <AppKit/AppKit.h> +#include <QtCore/QString> +#include <QtCore/QHash> +#include <QtWidgets/QAction> + +#include "qtmacunifiedtoolbar.h" + + +#ifndef QTMACTOOLBARDELEGATE_H +#define QTMACTOOLBARDELEGATE_H + +@interface QtMacToolbarDelegate : NSObject <NSToolbarDelegate> +{ +@public + NSToolbar *toolbar; + + QList<QtMacToolButton *> items; + QList<QtMacToolButton *> allowedItems; + +// QHash<QString, QAction*> actions; +// QHash<QString, QAction*> allowedActions; +} + +- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted; +- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar; +- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar; +- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar; + +- (QAction *)addActionWithText:(const QString *)text; +- (QAction *)addActionWithText:(const QString *)text icon:(const QIcon *)icon; +- (QAction *)addStandardItem:(QtMacToolButton::StandardItem)standardItem; + +- (QAction *)addAllowedActionWithText:(const QString *)text; +- (QAction *)addAllowedActionWithText:(const QString *)text icon:(const QIcon *)icon; +- (QAction *)addAllowedStandardItem:(QtMacToolButton::StandardItem)standardItem; + +- (IBAction)itemClicked:(id)sender; +@end + + +#endif // QTMACTOOLBARDELEGATE_H diff --git a/src/qtmactoolbardelegate.mm b/src/qtmactoolbardelegate.mm new file mode 100644 index 0000000..2e565fd --- /dev/null +++ b/src/qtmactoolbardelegate.mm @@ -0,0 +1,246 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qtmactoolbardelegate.h" +#include <qdebug.h> + +NSString *toNSString(const QString &string) +{ + return [NSString + stringWithCharacters : reinterpret_cast<const UniChar *>(string.unicode()) + length : string.length()]; +} + +QString toQString(NSString *string) +{ + if (!string) + return QString(); + + QString qstring; + qstring.resize([string length]); + [string getCharacters: reinterpret_cast<unichar*>(qstring.data()) range : NSMakeRange(0, [string length])]; + + return qstring; +} + +NSArray *toNSArray(const QList<QString> &stringList) +{ + NSMutableArray *array = [[NSMutableArray alloc] init]; + foreach (const QString &string, stringList) { + [array addObject : toNSString(string)]; + } + return array; +} + +static void drawImageReleaseData (void *info, const void *, size_t) +{ + delete static_cast<QImage *>(info); +} + +CGImageRef qt_mac_image_to_cgimage(const QImage &img) +{ + QImage *image; + if (img.depth() != 32) + image = new QImage(img.convertToFormat(QImage::Format_ARGB32_Premultiplied)); + else + image = new QImage(img); + + uint cgflags = kCGImageAlphaNone; + switch (image->format()) { + case QImage::Format_ARGB32_Premultiplied: + cgflags = kCGImageAlphaPremultipliedFirst; + break; + case QImage::Format_ARGB32: + cgflags = kCGImageAlphaFirst; + break; + case QImage::Format_RGB32: + cgflags = kCGImageAlphaNoneSkipFirst; + default: + break; + } + cgflags |= kCGBitmapByteOrder32Host; + CGDataProviderRef dataProvider = CGDataProviderCreateWithData(image, + static_cast<const QImage *>(image)->bits(), + image->byteCount(), + drawImageReleaseData); + CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); + + + CGImageRef cgImage = CGImageCreate(image->width(), image->height(), 8, 32, + image->bytesPerLine(), + colorspace, + cgflags, dataProvider, 0, false, kCGRenderingIntentDefault); + + CFRelease(dataProvider); + CFRelease(colorspace); + return cgImage; +} + +NSImage *toNSImage(const QPixmap &pixmap) +{ + QImage qimage = pixmap.toImage(); + NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage: qt_mac_image_to_cgimage(qimage)]; + NSImage *image = [[NSImage alloc] init]; + [image addRepresentation:bitmapRep]; + [bitmapRep release]; + return image; +} + +NSString *toNSStandardItem(QtMacToolButton::StandardItem standardItem); + +NSMutableArray *itemIdentifiers(const QList<QtMacToolButton *> &items, bool cullUnselectable) +{ + NSMutableArray *array = [[NSMutableArray alloc] init]; + foreach (const QtMacToolButton * item, items) { + if (cullUnselectable && item->selectable() == false) + continue; + if (item->standardItem() == QtMacToolButton::NoItem) { + [array addObject : toNSString(QString::number(qulonglong(item)))]; + } else { + [array addObject : toNSStandardItem(item->standardItem())]; + } + } + return array; +} + + +@implementation QtMacToolbarDelegate + +- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar +{ + return itemIdentifiers(self->items, false); +} + +- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar +{ + return itemIdentifiers(self->allowedItems, false); +} + +- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar +{ + NSMutableArray *array = itemIdentifiers(self->items, true); + [array addObjectsFromArray : itemIdentifiers(self->allowedItems, true)]; + return array; +} + +- (IBAction)itemClicked:(id)sender +{ + NSToolbarItem *item = reinterpret_cast<NSToolbarItem *>(sender); + QString identifier = toQString([item itemIdentifier]); + QtMacToolButton *toolButton = reinterpret_cast<QtMacToolButton *>(identifier.toULongLong()); + if (toolButton->m_action) { + toolButton->m_action->trigger(); + } + toolButton->emitActivated(); +} + +- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdentifier willBeInsertedIntoToolbar:(BOOL) willBeInserted +{ + Q_UNUSED(toolbar); + Q_UNUSED(willBeInserted); + const QString identifier = toQString(itemIdentifier); + + QtMacToolButton *toolButton = reinterpret_cast<QtMacToolButton *>(identifier.toULongLong()); // string -> unisgned long long -> pointer + NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease]; + [toolbarItem setLabel: toNSString(toolButton->m_action->text())]; + [toolbarItem setPaletteLabel:[toolbarItem label]]; +// [toolbarItem setToolTip: toNSString(action->toolTip())]; + + QPixmap icon = toolButton->m_action->icon().pixmap(64, 64); + if (icon.isNull() == false) { + [toolbarItem setImage : toNSImage(icon)]; + } + + [toolbarItem setTarget : self]; + [toolbarItem setAction : @selector(itemClicked:)]; + + return toolbarItem; +} + +- (QAction *)addActionWithText:(const QString *)text +{ + QIcon nullIcon; + return [self addActionWithText:text icon:&nullIcon]; +} + +- (QAction *)addActionWithText:(const QString *)text icon:(const QIcon *)icon +{ + QAction *action = new QAction(*icon, *text, 0); + QtMacToolButton *button = new QtMacToolButton(action); + button->m_action = action; + items.append(button); + return action; +} + +- (QAction *)addStandardItem:(QtMacToolButton::StandardItem) standardItem +{ + QAction *action = new QAction(0); + QtMacToolButton *button = new QtMacToolButton(action); + button->m_action = action; + button->setStandardItem(standardItem); + items.append(button); + return action; +} + +- (QAction *)addAllowedActionWithText:(const QString *)text +{ + QIcon nullIcon; + return [self addAllowedActionWithText:text icon:&nullIcon]; +} + +- (QAction *)addAllowedActionWithText:(const QString *)text icon:(const QIcon *)icon +{ + QAction *action = new QAction(*icon, *text, 0); + QtMacToolButton *button = new QtMacToolButton(action); + button->m_action = action; + allowedItems.append(button); + return action; +} + +- (QAction *)addAllowedStandardItem:(QtMacToolButton::StandardItem)standardItem +{ + QAction *action = new QAction(0); + QtMacToolButton *button = new QtMacToolButton(action); + button->m_action = action; + button->setStandardItem(standardItem); + allowedItems.append(button); + return action; +} + +@end diff --git a/src/qtmactoolbutton.h b/src/qtmactoolbutton.h new file mode 100644 index 0000000..b49e2c3 --- /dev/null +++ b/src/qtmactoolbutton.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QTMACTOOLBUTTON_H +#define QTMACTOOLBUTTON_H + +#include <QtCore/QObject> +#include <QtCore/QString> +#include <QtGui/QWindow> +#include <QtWidgets/QAction> + +class QtMacToolButton : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool selectable READ selectable WRITE setSelectable) + Q_PROPERTY(StandardItem standardItem READ standardItem WRITE setStandardItem) + Q_ENUMS(StandardItem) +public: + enum StandardItem + { + NoItem, + ShowColors, + ShowFonts, + PrintItem, + Separator, + Space, + FlexibleSpace + }; + + QtMacToolButton(); + QtMacToolButton(QObject *parent); + virtual ~QtMacToolButton(); + + bool selectable() const; + void setSelectable(bool selectable); + + StandardItem standardItem() const; + void setStandardItem(StandardItem standardItem); +signals: + void activated(); +private: + bool m_selectable; + StandardItem m_standardItem; +public: // (not really public) + QAction *m_action; + void emitActivated() { emit activated(); } +}; + +#endif diff --git a/src/qtmactoolbutton.mm b/src/qtmactoolbutton.mm new file mode 100644 index 0000000..5d7db55 --- /dev/null +++ b/src/qtmactoolbutton.mm @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qtmactoolbutton.h" + +QtMacToolButton::QtMacToolButton() +{ + m_standardItem = NoItem; + m_selectable = false; + m_action = 0; +} + +QtMacToolButton::QtMacToolButton(QObject *parent) + :QObject(parent) +{ + m_standardItem = NoItem; + m_selectable = false; + m_action = 0; +} + +QtMacToolButton::~QtMacToolButton() +{ + +} + +bool QtMacToolButton::selectable() const +{ + return m_selectable; +} + +void QtMacToolButton::setSelectable(bool selectable) +{ + m_selectable = selectable; +} + +QtMacToolButton::StandardItem QtMacToolButton::standardItem() const +{ + return m_standardItem; +} + +void QtMacToolButton::setStandardItem(StandardItem standardItem) +{ + m_standardItem = standardItem; +} diff --git a/src/qtmacunifiedtoolbar.h b/src/qtmacunifiedtoolbar.h new file mode 100644 index 0000000..16970d2 --- /dev/null +++ b/src/qtmacunifiedtoolbar.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTMACTOOLBAR_H +#define QTMACTOOLBAR_H + +#include <QtCore/QObject> +#include <QtCore/QString> +#include <QtGui/QWindow> +#include <QtWidgets/QAction> + +#include "qtmactoolbutton.h" + +class QtMacUnifiedToolBarPrivate; +class QtMacUnifiedToolBar : public QObject +{ + Q_OBJECT + Q_PROPERTY(QList<QtMacToolButton *> buttons READ buttons) + Q_PROPERTY(QList<QtMacToolButton *> allowedButtons READ allowedButtons) +public: + + QtMacUnifiedToolBar(QObject *parent = 0); + ~QtMacUnifiedToolBar(); + + QList<QtMacToolButton *> buttons(); + QList<QtMacToolButton *> allowedButtons(); + + void showInWindow(QWindow *window); + Q_INVOKABLE void showInMainWindow(); + + // Add actions to the toolbar + Q_INVOKABLE QAction *addAction(const QString &text); + Q_INVOKABLE QAction *addAction(const QIcon &icon, const QString &text); + Q_INVOKABLE QAction *addStandardItem(QtMacToolButton::StandardItem standardItem); + + // Add actions to the "Customize Toolbar" menu + Q_INVOKABLE QAction *addAllowedAction(const QString &text); + Q_INVOKABLE QAction *addAllowedAction(const QIcon &icon, const QString &text); + Q_INVOKABLE QAction *addAllowedStandardItem(QtMacToolButton::StandardItem standardItem); +private Q_SLOTS: + void showInWindow_impl(); +private: + QWindow *targetWindow; + bool m_showText; + QList<QtMacToolButton *> m_buttons; + QList<QtMacToolButton *> m_allowedButtons; + QtMacUnifiedToolBarPrivate *d; +}; + +#endif + diff --git a/src/qtmacunifiedtoolbar.mm b/src/qtmacunifiedtoolbar.mm new file mode 100644 index 0000000..32c30ec --- /dev/null +++ b/src/qtmacunifiedtoolbar.mm @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mac Extras project. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "QtMacunifiedtoolbar.h" +#include "QtMactoolbardelegate.h" +#include <QtGui/QGuiApplication> +#include <QtWidgets/QApplication> +#include <QtGui/qplatformnativeinterface_qpa.h> +#include <QtCore/QtCore> + +#import <AppKit/AppKit.h> + +NSString *toNSStandardItem(QtMacToolButton::StandardItem standardItem) +{ + if (standardItem == QtMacToolButton::ShowColors) + return NSToolbarShowColorsItemIdentifier; + else if (standardItem == QtMacToolButton::ShowFonts) + return NSToolbarShowFontsItemIdentifier; + else if (standardItem == QtMacToolButton::PrintItem) + return NSToolbarPrintItemIdentifier; + else if (standardItem == QtMacToolButton::Separator) + return NSToolbarSeparatorItemIdentifier; + else if (standardItem == QtMacToolButton::Space) + return NSToolbarSpaceItemIdentifier; + else if (standardItem == QtMacToolButton::FlexibleSpace) + return NSToolbarFlexibleSpaceItemIdentifier; + return @""; +} + + + +class QtMacUnifiedToolBarPrivate +{ +public: + NSToolbar *toolbar; + QtMacToolbarDelegate *delegate; +}; + +QtMacUnifiedToolBar::QtMacUnifiedToolBar(QObject *parent) + : QObject(parent) +{ + d = new QtMacUnifiedToolBarPrivate(); + d->toolbar = [[NSToolbar alloc] initWithIdentifier:@"QtMacUnifiedToolBar"]; + d->delegate = [[QtMacToolbarDelegate alloc] init]; + [d->toolbar setAllowsUserCustomization:YES]; + [d->toolbar setAutosavesConfiguration:NO]; + [d->toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel]; + [d->toolbar setDelegate: d->delegate]; +} + +QtMacUnifiedToolBar::~QtMacUnifiedToolBar() +{ + [d->toolbar release]; + delete d; +} + +QList<QtMacToolButton *> QtMacUnifiedToolBar::buttons() +{ + return d->delegate->items; +} + +QList<QtMacToolButton *> QtMacUnifiedToolBar::allowedButtons() +{ + return d->delegate->allowedItems; +} + +void QtMacUnifiedToolBar::showInWindow(QWindow *window) +{ + targetWindow = window; + QTimer::singleShot(100, this, SLOT(showInWindow_impl())); // ### hackety hack +} + +void QtMacUnifiedToolBar::showInMainWindow() +{ + QWidgetList widgets = QApplication::topLevelWidgets(); + if (widgets.isEmpty()) + return; + showInWindow(widgets.at(0)->windowHandle()); +} + + +void QtMacUnifiedToolBar::showInWindow_impl() +{ + NSWindow *macWindow = static_cast<NSWindow*>( + QGuiApplication::platformNativeInterface()->nativeResourceForWindow("nswindow", targetWindow)); + + if (!macWindow) { + QTimer::singleShot(100, this, SLOT(showInWindow_impl())); // ### hackety hack + return; + } + + [macWindow setToolbar: d->toolbar]; + [macWindow setShowsToolbarButton:YES]; +} + +QAction *QtMacUnifiedToolBar::addAction(const QString &text) +{ + return [d->delegate addActionWithText:&text]; +} + +QAction *QtMacUnifiedToolBar::addAction(const QIcon &icon, const QString &text) +{ + return [d->delegate addActionWithText:&text icon:&icon]; +} + +QAction *QtMacUnifiedToolBar::addStandardItem(QtMacToolButton::StandardItem standardItem) +{ + return [d->delegate addStandardItem:standardItem]; +} + +QAction *QtMacUnifiedToolBar::addAllowedAction(const QString &text) +{ + return [d->delegate addAllowedActionWithText:&text]; +} + +QAction *QtMacUnifiedToolBar::addAllowedAction(const QIcon &icon, const QString &text) +{ + return [d->delegate addAllowedActionWithText:&text icon:&icon]; +} + +QAction *QtMacUnifiedToolBar::addAllowedStandardItem(QtMacToolButton::StandardItem standardItem) +{ + return [d->delegate addAllowedStandardItem:standardItem]; +} + + |