ObjQt  QtObject.mm at [d43a881019]

File QtCore/QtObject.mm artifact d103de14ea part of check-in d43a881019


/*
 * Copyright (c) 2017, Jonathan Schleifer <js@heap.zone>
 *
 * https://heap.zone/git/objqt.git
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * 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.
 */

#import "QtObject.h"
#import "QtEvent.h"
#import "QtThread.h"

#import "helpers.h"

#include <QVariant>

@implementation QtObject
@synthesize qObject = _qObject;

- init
{
	OF_INVALID_INIT_METHOD
}

- initWithQObject: (QObject*)qObject
{
	self = [super init];

	_qObject = qObject;

	return self;
}

- (void)dealloc
{
	if (_ownsObject)
		delete _qObject;

	[super dealloc];
}

- (void)takeOwnership
{
	OF_ENSURE(!_ownsObject);
	_ownsObject = true;
}

- (void)giveUpOwnership
{
	OF_ENSURE(_ownsObject);
	_ownsObject = false;
}

- (OFString*)objectName
{
	return toOF(_qObject->objectName());
}

- (void)setObjectName: (OFString*)objectName
{
	_qObject->setObjectName(toQt(objectName));
}

- (bool)setBlockSignals: (bool)block
{
	return _qObject->blockSignals(block);
}

- (OFArray OF_GENERIC(QtObject*)*)children
{
	const QObjectList &qChildren = _qObject->children();
	OFMutableArray *children = [OFMutableArray arrayWithCapacity:
	    qChildren.count()];
	void *pool = objc_autoreleasePoolPush();

	for (QObject *qChild: qChildren)
		[children addObject:
		    [[[QtObject alloc] initWithQObject: qChild] autorelease]];

	[children makeImmutable];

	objc_autoreleasePoolPop(pool);

	return children;
}

- (QMetaObject::Connection)connectSignal: (OFString*)signal
				  sender: (QtObject*)sender
				  method: (OFString*)method
				    type: (Qt::ConnectionType)type
{
	return _qObject->connect([sender qObject],
	    [signal UTF8String], [method UTF8String], type);
}

- (bool)disconnectSignal: (OFString*)signal
		receiver: (QtObject*)receiver
		  method: (OFString*)method
{
	return _qObject->disconnect([signal UTF8String], [receiver qObject],
	    [method UTF8String]);
}

- (bool)disconnectAllSignalsForReceiver: (QtObject*)receiver
				 method: (OFString*)method
{
	return _qObject->disconnect([receiver qObject], [method UTF8String]);
}

- (void)dumpObjectInfo
{
	_qObject->dumpObjectInfo();
}

- (void)dumpObjectTree
{
	_qObject->dumpObjectTree();
}

- (OFArray OF_GENERIC(OFDataArray*)*)dynamicPropertyNames
{
	const QList<QByteArray> &qDynamicPropertyNames =
	    _qObject->dynamicPropertyNames();
	OFMutableArray *dynamicPropertyNames =
	    [OFMutableArray arrayWithCapacity: qDynamicPropertyNames.count()];
	void *pool = objc_autoreleasePoolPush();

	for (const QByteArray &qDynamicPropertyName: qDynamicPropertyNames) {
		OFDataArray *dynamicPropertyName = [OFDataArray dataArray];
		[dynamicPropertyName addItems: qDynamicPropertyName.data()
					count: qDynamicPropertyName.count()];
		[dynamicPropertyNames addObject: dynamicPropertyName];
	}

	[dynamicPropertyNames makeImmutable];

	objc_autoreleasePoolPop(pool);

	return dynamicPropertyNames;
}

- (bool)handleEvent: (QtEvent*)event
{
	return _qObject->event([event qEvent]);
}

- (bool)filterEvent: (QtEvent*)event
	  forObject: (QtObject*)watched
{
	return _qObject->eventFilter([watched qObject], [event qEvent]);
}

- (bool)inheritsClassWithName: (OFString*)className
{
	return _qObject->inherits([className UTF8String]);
}

- (void)installEventFilter: (QtObject*)filterObj
{
	_qObject->installEventFilter([filterObj qObject]);
}

- (bool)isWidgetType
{
	return _qObject->isWidgetType();
}

- (bool)isWindowType
{
	return _qObject->isWindowType();
}

- (void)killTimerWithID: (int)ID
{
	_qObject->killTimer(ID);
}

- (const QMetaObject*)metaObject
{
	return _qObject->metaObject();
}

- (void)moveToThread: (QtThread*)targetThread
{
	_qObject->moveToThread([targetThread qThread]);
}

- (QtObject*)parent
{
	return [[[QtObject alloc]
	    initWithQObject: _qObject->parent()] autorelease];
}

- (void)setParent: (QtObject*)parent
{
	_qObject->setParent([parent qObject]);
}

- (QVariant)propertyForName: (OFString*)name
{
	return _qObject->property([name UTF8String]);
}

- (void)removeEventFilter: (QtObject*)obj
{
	_qObject->removeEventFilter([obj qObject]);
}

- (bool)setProperty: (QVariant&)value
	    forName: (OFString*)name
{
	return _qObject->setProperty([name UTF8String], value);
}

- (bool)signalsBlocked
{
	return _qObject->signalsBlocked();
}

- (int)startTimerWithInterval: (int)interval
			 type: (Qt::TimerType)type
{
	return _qObject->startTimer(interval, type);
}

- (QtThread*)thread
{
	return [[[QtThread alloc]
	    initWithQThread: _qObject->thread()] autorelease];
}

- (void)deleteLater
{
	OF_ENSURE(!_ownsObject);
	_qObject->deleteLater();
}
@end