Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to use Share API in macOS and Qt Quick

From Qt Wiki
Revision as of 11:02, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

[toc align_right="yes" depth="2"]

Hi everyone!

In this article I've tried to explain how to use Mac OS X Share API in your Qt Quick app. Share API was implemented in Mac OS 10.10 (Yosemite).

As you know all frameworks in OS X which you could use in your application created with Objective-C/C+. That's why you can't use framework in your C+ classes. You need to rename your .cpp file to .mm and then you can call Objective-C code. But sometimes you need to call methods with arguments with Objective-C. As you know in .h file you can't call Objective-C code. You must create Objective-C class which will call all needed methods to work with target framework.

Classes with Share API support.

First you need to create class with all methods in Objective-C/C++ to use Share API. To do this you need follow a few steps:
# Create a new class and rename .cpp file to .mm.
# Add .h file to .pro of your project in section "OBJECTIVE_HEADERS" and .mm to "OBJECTIVE_SOURCES".
# Change .h file content to this code:

#ifdef __cplusplus<br />extern &quot;C&amp;quot; {<br />#endif

#import &lt;Foundation/Foundation.h&amp;gt;<br />#import &lt;Social/Social.h&amp;gt;

/**

typedef QfSharePickerItemClicked
*

param sharingService Selected type of sharing.<br /> *

author Andrew Shapovelov*/
typedef void(

QfSharePickerItemClicked)(NSSharingService* sharingService);
/**
class QfSharePicker<br /> *
brief Object to show share menu.
*
author Andrew Shapovalov*/<br />
interface QfSharePicker : NSObject
/**
brief Create a new object.<br /> *
param view View on what will show share menu.
*
param frame Rect to show share window.<br /> *
param datas Datas items to share.
*
param block Block of code to select item.<br /> *
author Andrew Shapovalov*/
- (instancetype)initWithView:(NSView*)view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block;
end
<br />#ifdef __cplusplus<br />}<br />#endif<br />

# Change .mm file content to this code:
<br />#import &quot;qfsharepicker.h&amp;quot;<br />#include &lt;QtCore&amp;gt;
<br />/**
brief The private methods and properties of QfSharePicker class.
*
author Andrew Shapovalov*/<br />
interface QfSharePicker () <NSSharingServicePickerDelegate, NSSharingServiceDelegate&gt;
/** Sharing service picker./
property (nonatomic, retain) NSSharingServicePicker''' picker;
<br />/** Block of code to select item.'''/<br />
property (nonatomic, copy) QfSharePickerItemClicked onItemClicked;

end

<br />
implementation QfSharePicker


#pragma mark - Init methods
- (instancetype)initWithView:(NSView)view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block
{
self = [super init];

if(self)
{
self.onItemClicked = block;
self.picker = [[NSSharingServicePicker alloc] initWithItems: datas];
self.picker.delegate = self;
[self.picker showRelativeToRect:frame ofView:view preferredEdge:NSMinXEdge];
}
return self;
}


#pragma mark - NSSharingServicePickerDelegate methods
- (void)sharingServicePicker:(NSSharingServicePicker )sharingServicePicker didChooseSharingService:(NSSharingService)service
{
if(self.picker == sharingServicePicker)
{
if(self.onItemClicked)
{
self.onItemClicked(service);
}
}
}
- (id <NSSharingServiceDelegate&gt;)sharingServicePicker:(NSSharingServicePicker )sharingServicePicker delegateForSharingService:(NSSharingService)sharingService
{
Q_UNUSED(sharingService);
if(self.picker == sharingServicePicker)
{
}
return self;
}
#pragma mark - NSSharingServiceDelegate methods
- (void)sharingService:(NSSharingService )sharingService willShareItems:(NSArray)items
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
//Some code here
}
- (void)sharingService:(NSSharingService )sharingService didFailToShareItems:(NSArray)items error:(NSError )error
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
Q_UNUSED(error);

//Some code here
}
- (void)sharingService:(NSSharingService)sharingService didShareItems:(NSArray )items
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
//Some code here
}
#pragma mark - Clear memory
- (void)dealloc
{
[super dealloc];
[self.picker autorelease];
[self.onItemClicked release];
self.onItemClicked = nil;
}


end<br />

p. More information you can find in official Apple "documentation&quot;:https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePicker_Class/index.html. And you need to see information about class "NSSharingServicePickerDelegate&quot;:https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePickerDelegate_Protocol/index.html#//apple_ref/occ/intf/NSSharingServicePickerDelegate.


h1. Create QtQuick Item to use Share API from App.


p. Second class which you need to create is a class to call from C+. This class will call Objective-C/C. To do this you need follow a few steps:
# Create a new C
+ class.
# Rename .cpp file to .mm and added it to "OBJECTIVE_SOURCES&quot;.
# Change .h file content to code:
<br />#include &lt;QtCore&amp;gt;<br />#include &lt;QtQuick&amp;gt;
<br />/'''*
class QfShareItem
*
brief Manager to use share logic.<br /> *
author Andrew Shapovalov*/
class QfShareItem : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString shareString READ getShareString WRITE setShareString NOTIFY shareStringChanged)
Q_PROPERTY(QUrl shareUrl READ getShareUrl WRITE setShareUrl NOTIFY shareUrlChanged)


private:
/** The share string info./
QString m_shareString;

/* The share link./
QUrl m_shareUrl;


/*
brief Strip all HTML tags from string.<br /> *
param body The source string.
*
return Plain text.<br /> *
author Andrew Shapovalov*/
QString stripHTMLTags(QString body);

/**
brief Works with all applications events.<br /> *
param obj The object which call event.
*
param event The event of call.<br /> *
author Andrew Shapovalov*/
bool eventFilter(QObject* obj, QEvent* event);
/**
brief Share current content.<br /> *
author Andrew Shapovalov*/
void shareCurrentContent();
public:
/**
brief Create a new object.<br /> *
param parent Parent object.
*
author Andrew Shapovalov*/<br /> explicit QfShareItem(QQuickPaintedItem '''parent = 0);
<br /> //Others<br /> /'''*
brief Called when current object will redrawing.
*
param painter The object for draw data.<br /> *
author Andrew Shapovalov*/
void paint(QPainter painter);
//Getters
/*
brief Get share content string.<br /> *
return Share string content.
*
author Andrew Shapovalov*/<br /> inline QString getShareString(){return m_shareString;}
<br /> /**
brief Get share url.
*
return Share url.<br /> *
author Andrew Shapovalov*/
inline QUrl getShareUrl(){return m_shareUrl;}

//Setters
/**
brief Set share content string.<br /> *
param value Share string content.
*
author Andrew Shapovalov*/<br /> inline void setShareString(QString value){m_shareString = value; emit shareStringChanged(m_shareString);}
<br /> /**
brief Set share url.
*
param value Share url.<br /> *
author Andrew Shapovalov*/
inline void setShareUrl(QUrl value){m_shareUrl = value; emit shareUrlChanged(m_shareUrl);}
/**
brief Share content and url.<br /> *
param text The text of share.
*
param url The url to share.<br /> *
author Andrew Shapovalov*/
Q_INVOKABLE void shareContent(QString text = QString(), QUrl url = QUrl());
signals:
/**
brief Called when share content string was changed.<br /> *
param value Share string content.
*
author Andrew Shapovalov*/<br /> void shareStringChanged(QString value);
<br /> /**
brief Called when share url was changed.
*
param value Share url.<br /> *
author Andrew Shapovalov*/
void shareUrlChanged(QUrl value);
/**
brief Called when user select share service.<br /> *
param serviceName The name of selected service.
*
author Andrew Shapovalov*/<br /> void selectedService(QString serviceName);
<br /> public slots:<br />};<br />

# Change .mm file content to code:
<br />#include &quot;qfshareitem.h&amp;quot;<br />#include &quot;qfsharepicker.h&amp;quot;
<br />QString QfShareItem::stripHTMLTags(QString body)<br />{<br /> body.replace(&quot;&lt;br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/p&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/td&amp;gt;&quot;,&quot;&quot;);<br /> body.remove(QRegExp(&quot;&amp;lt;head&amp;amp;gt;(.''')&amp;lt;/head&amp;amp;gt;&quot;));<br /> body.remove(QRegExp(&quot;&amp;lt;form(.''')&amp;lt;/form&amp;gt;&quot;));<br /> body.remove(QRegExp( &quot;&lt;(.)[</sup>&gt;]'''&gt;&quot;));
<br /> return body.trimmed();<br />}
<br />bool QfShareItem::eventFilter(QObject''' obj, QEvent* event)<br />{<br /> if(obj == this)<br /> {<br /> if(parentItem())<br /> {<br /> return QObject::eventFilter(parentItem(), event);<br /> }<br /> }

return QObject::eventFilter(obj, event);<br />}

void QfShareItem::shareCurrentContent()<br />{<br /> QQuickItem* parentItem = this-&gt;parentItem();<br /> if(!m_shareString.isEmpty() &amp;&amp; parentItem)<br /> {<br /> QRectF rect = parentItem-&gt;mapRectToItem(NULL, parentItem-&gt;boundingRect());<br /> NSView* view = reinterpret_cast&amp;lt;NSView *&gt;(parentItem-&gt;window()<s>&gt;winId());<br /> NSRect frame = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
<br /> m_shareString = m_shareString.replace(&quot;&amp;lt;style type=quot;text/cssquot;&amp;gt;a {color:#44a51c;text-decoration:none;}&amp;lt;/style&amp;amp;gt;&quot;, &quot;&quot;);<br /> QString content = stripHTMLTags(m_shareString).trimmed();<br /> NSMutableArray* datas = [NSMutableArray arrayWithObject: content.toNSString()];<br /> if(!m_shareUrl.isEmpty())<br /> {<br /> NSURL* url = [NSURL URLWithString: m_shareUrl.toString().toNSString()];<br /> if(url)<br /> {<br /> [datas addObject: url];<br /> }<br /> }<br /> QfSharePicker* sharePicker = [[QfSharePicker alloc] initWithView:view frame:frame datasArray:datas onItemClicked:nil];<br /> [sharePicker autorelease];<br /> }<br />}
<br />QfShareItem::QfShareItem(QQuickPaintedItem '''parent) :<br /> QQuickPaintedItem(parent)<br />{<br /> m_shareString.clear();<br /> m_shareUrl.clear();
<br /> connect(this, &amp;QQuickPaintedItem::parentChanged, [this](QQuickItem''' newParent){
<br /> if(newParent)<br /> {<br /> newParent</s>&gt;setFiltersChildMouseEvents(true);<br /> }<br /> });<br /> this-&gt;installEventFilter(this);

setFlag(QQuickPaintedItem::ItemHasContents, true);<br /> setFlag(QQuickPaintedItem::ItemClipsChildrenToShape, true);<br /> setFlag(QQuickPaintedItem::ItemAcceptsDrops, true);<br /> setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);<br />}

void QfShareItem::paint(QPainter *painter)<br />{<br /> try<br /> {<br /> Q_UNUSED(painter);<br /> }<br /> catch(std::exception&amp;amp; exception)<br /> {<br /> qDebug()&lt;&lt;&quot;exception: &quot;&lt;&lt;exception.what();<br /> }<br />}

void QfShareItem::shareContent(QString text, QUrl url)<br />{<br /> if(!text.isEmpty())<br /> {<br /> m_shareString = text;<br /> }

if(!url.isEmpty())<br /> {<br /> m_shareUrl = url;<br /> }

shareCurrentContent();<br />}<br />

# Register new type to use in QML.
<br />qmlRegisterType&amp;lt;YourShareItem&amp;gt;(uri, 1, 1, &quot;YourShareItem&amp;quot;);<br />

How to use in QML.

Import the new type to your QML file where you need to use share logic and add this code:
<br />Button {<br /> id: btnLink<br /> width: 22<br /> height: 22

onClicked: {<br /> share.shareContent();<br /> }

QfShareItem {<br /> id: share<br /> anchors.fill: btnLink<br /> shareUrl: userPanelView.shareUrl<br /> }<br />}<br />

Summary