Skip to content

Commit

Permalink
Support for arbitrary NSViews
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviotordini committed Apr 5, 2016
1 parent d897bcc commit 9e15f2e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/macextras/qmacfunctions.h
Expand Up @@ -79,6 +79,7 @@ Q_MACEXTRAS_EXPORT void setBadgeLabelText(const QString &text);
Q_MACEXTRAS_EXPORT QString badgeLabelText();

Q_MACEXTRAS_EXPORT NSImage *toNSImage(const QPixmap &pixmap);
Q_MACEXTRAS_EXPORT NSImage *iconToNSImage(const QIcon &icon);

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
Q_MACEXTRAS_EXPORT bool isMainWindow(QWindow *window);
Expand Down
23 changes: 23 additions & 0 deletions src/macextras/qmacfunctions_mac.mm
Expand Up @@ -78,6 +78,29 @@
return image;
}

/*!
\fn NSImage* QtMac::iconToNSImage(const QIcon &icon)
Creates an \c NSImage equivalent to the QIcon \a icon. Returns the \c NSImage handle.
It is the caller's responsibility to release the \c NSImage data
after use.
*/
NSImage* iconToNSImage(const QIcon &icon)
{
NSImage *image = [[NSImage alloc] init];
foreach (const QSize &size, icon.availableSizes()) {
QPixmap pixmap = icon.pixmap(size);
CGImageRef cgimage = QtMac::toCGImageRef(pixmap);
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
[bitmapRep setSize: NSMakeSize(size.width(), size.height())];
[image addRepresentation:bitmapRep];
[bitmapRep release];
CFRelease(cgimage);
}
return image;
}

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
/*!
\fn bool QtMac::isMainWindow(QWindow *window)
Expand Down
1 change: 1 addition & 0 deletions src/macextras/qmactoolbar.h
Expand Up @@ -67,6 +67,7 @@ class Q_MACEXTRAS_EXPORT QMacToolBar : public QObject
QMacToolBarItem *addAllowedItem(const QIcon &icon, const QString &text);
QMacToolBarItem *addStandardItem(QMacToolBarItem::StandardItem standardItem);
QMacToolBarItem *addAllowedStandardItem(QMacToolBarItem::StandardItem standardItem);
QMacToolBarItem *addView(NSView *view);
void addSeparator();

void setItems(QList<QMacToolBarItem *> &items);
Expand Down
10 changes: 10 additions & 0 deletions src/macextras/qmactoolbar.mm
Expand Up @@ -183,6 +183,16 @@ Add items by calling addItem(). The toolbar has a customization menu which
return item;
}

QMacToolBarItem *QMacToolBar::addView(NSView *view)
{
Q_D(QMacToolBar);
QMacToolBarItem *item = new QMacToolBarItem(this);
item->setView(view);
d->items.append(item);
d->allowedItems.append(item);
return item;
}

/*!
Sets the list of the default toolbar \a items.
*/
Expand Down
17 changes: 13 additions & 4 deletions src/macextras/qmactoolbardelegate.mm
Expand Up @@ -98,8 +98,10 @@ - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar

- (IBAction)itemClicked:(id)sender
{
NSToolbarItem *item = reinterpret_cast<NSToolbarItem *>(sender);
toolbarPrivate->itemClicked(item);
if ([sender isKindOfClass:[NSToolbarItem class]]) {
NSToolbarItem *item = reinterpret_cast<NSToolbarItem *>(sender);
toolbarPrivate->itemClicked(item);
}
}

- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdentifier willBeInsertedIntoToolbar:(BOOL) willBeInserted
Expand All @@ -110,10 +112,17 @@ - (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSStri
QMacToolBarItem *toolButton = reinterpret_cast<QMacToolBarItem *>(identifier.toULongLong()); // string -> unisgned long long -> pointer
NSToolbarItem *toolbarItem = toolButton->nativeToolBarItem();

[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(itemClicked:)];
if (![toolbarItem view]) {
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(itemClicked:)];
}

return toolbarItem;
}

- (BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem
{
return [toolbarItem isEnabled];
}

@end
7 changes: 7 additions & 0 deletions src/macextras/qmactoolbaritem.h
Expand Up @@ -47,6 +47,7 @@
#include <QtMacExtras/qmacextrasglobal.h>

Q_FORWARD_DECLARE_OBJC_CLASS(NSToolbarItem);
Q_FORWARD_DECLARE_OBJC_CLASS(NSView);

QT_BEGIN_NAMESPACE

Expand All @@ -71,6 +72,9 @@ class Q_MACEXTRAS_EXPORT QMacToolBarItem : public QObject
QMacToolBarItem(QObject *parent = 0);
virtual ~QMacToolBarItem();

bool enabled() const;
void setEnabled(bool enabled);

bool selectable() const;
void setSelectable(bool selectable);

Expand All @@ -83,6 +87,9 @@ class Q_MACEXTRAS_EXPORT QMacToolBarItem : public QObject
QIcon icon() const;
void setIcon(const QIcon &icon);

NSView *view() const;
void setView(NSView *view);

NSToolbarItem *nativeToolBarItem() const;

Q_SIGNALS:
Expand Down
43 changes: 39 additions & 4 deletions src/macextras/qmactoolbaritem.mm
Expand Up @@ -83,6 +83,24 @@

}

/*!
\property QMacToolBarItem::enabled
\brief Whether the item is enabled
This property's default is true.
*/
bool QMacToolBarItem::enabled() const
{
Q_D(const QMacToolBarItem);
return [d->toolbarItem enabled];
}

void QMacToolBarItem::setEnabled(bool enabled)
{
Q_D(QMacToolBarItem);
[d->toolbarItem setEnabled:enabled];
}

/*!
\property QMacToolBarItem::selectable
\brief Whether the item is selecatble
Expand All @@ -101,6 +119,26 @@
d->selectable = selectable;
}

/*!
\property QMacToolBarItem::view
\brief The item's view, if any.
*/
NSView *QMacToolBarItem::view() const
{
Q_D(const QMacToolBarItem);
return [d->toolbarItem view];
}

void QMacToolBarItem::setView(NSView *view)
{
Q_D(QMacToolBarItem);

if (d->standardItem != QMacToolBarItem::NoStandardItem)
return;

[d->toolbarItem setView:view];
}

/*!
\property QMacToolBarItem::standardItem
\brief Whether the item is a standard item.
Expand Down Expand Up @@ -163,14 +201,11 @@
{
Q_D(QMacToolBarItem);
d->icon = icon;
QPixmap pixmap = icon.pixmap(64, 64);

if (d->standardItem != QMacToolBarItem::NoStandardItem)
return;

if (pixmap.isNull() == false) {
[d->toolbarItem setImage: QtMac::toNSImage(pixmap)];
}
[d->toolbarItem setImage: QtMac::iconToNSImage(icon)];
}

/*!
Expand Down

0 comments on commit 9e15f2e

Please sign in to comment.