dotherside/lib/src/DOtherSide.cpp

564 lines
15 KiB
C++
Raw Normal View History

#include "DOtherSide/DOtherSide.h"
2014-07-19 18:26:08 +02:00
2015-02-04 23:02:26 +01:00
#include <iostream>
2014-07-19 18:26:08 +02:00
#include <QtCore/QDir>
#include <QtCore/QDebug>
#include <QtCore/QModelIndex>
#include <QtCore/QHash>
2015-12-06 00:23:25 +01:00
#include <QtCore/QResource>
2015-01-31 12:50:14 +01:00
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlApplicationEngine>
#include <QtQuick/QQuickView>
#include <QtWidgets/QApplication>
2014-07-19 18:26:08 +02:00
#include "DOtherSide/DOtherSideTypesCpp.h"
#include "DOtherSide/OnSlotExecutedHandler.h"
#include "DOtherSide/DosQMetaObject.h"
#include "DOtherSide/DosQObject.h"
#include "DOtherSide/DosQObjectImpl.h"
using namespace DOS;
2014-07-19 18:26:08 +02:00
void convert_to_cstring(const QString& source, char** destination)
2014-07-19 18:26:08 +02:00
{
QByteArray array = source.toUtf8();
*destination = qstrdup(array.data());
2014-07-19 18:26:08 +02:00
}
void dos_qcoreapplication_application_dir_path(char** result)
{
convert_to_cstring(QCoreApplication::applicationDirPath(), result);
}
void dos_qguiapplication_create()
2014-07-19 18:26:08 +02:00
{
static int argc = 1;
static char empty[1] = {0};
static char* argv[] = {empty};
new QGuiApplication(argc, argv);
}
void dos_qguiapplication_delete()
2014-07-19 18:26:08 +02:00
{
delete qApp;
}
void dos_qguiapplication_exec()
2014-07-19 18:26:08 +02:00
{
qApp->exec();
}
void dos_qguiapplication_quit()
{
qApp->quit();
}
2015-01-11 13:29:44 +01:00
void dos_qapplication_create()
{
static int argc = 1;
static char empty[1] = {0};
static char* argv[] = {empty};
new QApplication(argc, argv);
}
void dos_qapplication_delete()
{
delete qApp;
}
void dos_qapplication_exec()
{
qApp->exec();
}
void dos_qapplication_quit()
{
qApp->quit();
}
2015-01-31 17:01:03 +01:00
void dos_qqmlapplicationengine_create(void** vptr)
{
*vptr = new QQmlApplicationEngine();
}
2015-01-31 17:01:03 +01:00
void dos_qqmlapplicationengine_load(void* vptr, const char* filename)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
engine->load(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QDir::separator() + QString(filename)));
}
void dos_qqmlapplicationengine_load_url(void* vptr, void* url)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
auto qurl = reinterpret_cast<QUrl*>(url);
2015-12-06 00:23:25 +01:00
engine->load(*qurl);
}
void dos_qqmlapplicationengine_load_data(void *vptr, const char *data)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
engine->loadData(data);
}
void dos_qqmlapplicationengine_add_import_path(void* vptr, const char* path)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
2015-12-06 00:23:25 +01:00
engine->addImportPath(QString(path));
}
2014-12-08 12:55:09 +01:00
void dos_qqmlapplicationengine_context(void* vptr, void** context)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
2014-12-08 12:55:09 +01:00
engine->rootContext();
*context = engine->rootContext();
}
void dos_qqmlapplicationengine_rootObjects(void* vptr, void*** array, int* array_length)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
2015-12-24 12:04:31 +01:00
QList<QObject*> list = engine->rootObjects();
2015-12-24 12:16:37 +01:00
auto objects = new QObject*[list.size()];
2015-12-24 12:04:31 +01:00
if (objects == nullptr) return;
for (int i = 0; i < list.length(); i += 1) objects[i] = list.at(i);
*array = reinterpret_cast<void**>(objects);
*array_length = list.length();
}
void dos_qqmlapplicationengine_delete(void* vptr)
{
2015-12-29 15:50:43 +01:00
auto engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
delete engine;
}
void dos_qquickview_create(void** vptr)
2014-07-19 18:26:08 +02:00
{
*vptr = new QQuickView();
}
void dos_qquickview_show(void* vptr)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
2014-07-19 18:26:08 +02:00
view->show();
}
void dos_qquickview_delete(void* vptr)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
2014-07-19 18:26:08 +02:00
delete view;
}
void dos_qquickview_source(void* vptr, char** result)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
2014-07-19 18:26:08 +02:00
QUrl url = view->source();
convert_to_cstring(url.toString(), result);
2014-07-19 18:26:08 +02:00
}
void dos_qquickview_set_source(void* vptr, const char* filename)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
2014-07-19 18:26:08 +02:00
view->setSource(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QDir::separator() + QString(filename)));
}
void dos_qquickview_set_source_url(void* vptr, void* url)
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
auto _url = reinterpret_cast<QUrl*>(url);
view->setSource(*_url);
}
void dos_qquickview_set_resize_mode(void* vptr, int resizeMode)
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
view->setResizeMode((QQuickView::ResizeMode) resizeMode);
}
void dos_qquickview_rootContext(void* vptr, void** context)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto view = reinterpret_cast<QQuickView*>(vptr);
2014-07-19 18:26:08 +02:00
*context = view->rootContext();
}
void dos_chararray_delete(char* ptr)
2014-07-19 18:26:08 +02:00
{
if (ptr) delete[] ptr;
}
2015-12-24 12:16:37 +01:00
void dos_qobjectptr_array_delete(void** ptr)
{
if (ptr) delete[] ptr;
}
void dos_qqmlcontext_baseUrl(void* vptr, char** result)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto context = reinterpret_cast<QQmlContext*>(vptr);
2014-07-19 18:26:08 +02:00
QUrl url = context->baseUrl();
convert_to_cstring(url.toString(), result);
2014-07-19 18:26:08 +02:00
}
void dos_qqmlcontext_setcontextproperty(void* vptr, const char* name, void* value)
2014-07-19 18:26:08 +02:00
{
2015-12-29 15:50:43 +01:00
auto context = reinterpret_cast<QQmlContext*>(vptr);
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(value);
2014-07-19 18:26:08 +02:00
context->setContextProperty(QString::fromUtf8(name), *variant);
}
2015-01-31 17:01:03 +01:00
void dos_qvariant_create(void** vptr)
2014-07-19 18:26:08 +02:00
{
*vptr = new QVariant();
}
2015-01-31 17:01:03 +01:00
void dos_qvariant_create_int(void** vptr, int value)
2014-07-19 18:26:08 +02:00
{
*vptr = new QVariant(value);
}
2015-01-31 17:01:03 +01:00
void dos_qvariant_create_bool(void** vptr, bool value)
2014-07-19 18:26:08 +02:00
{
*vptr = new QVariant(value);
}
void dos_qvariant_create_string(void** vptr, const char* value)
{
*vptr = new QVariant(value);
}
void dos_qvariant_create_qvariant(void** vptr, void* other)
{
auto newQVariant = new QVariant();
auto otherQVariant = reinterpret_cast<QVariant*>(other);
*newQVariant = *otherQVariant;
*vptr = newQVariant;
}
2015-01-31 17:01:03 +01:00
void dos_qvariant_create_qobject(void** vptr, void* value)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto qobject = reinterpret_cast<QObject*>(value);
auto variant = new QVariant();
2014-07-19 18:26:08 +02:00
variant->setValue<QObject*>(qobject);
*vptr = variant;
}
void dos_qvariant_create_float(void** vptr, float value)
{
*vptr = new QVariant(value);
}
void dos_qvariant_create_double(void** vptr, double value)
{
*vptr = new QVariant(value);
}
2015-01-26 19:11:50 +01:00
void dos_qvariant_create_qabstractlistmodel(void** vptr, void* value)
{
auto qobject = reinterpret_cast<QObject*>(value);
auto variant = new QVariant();
variant->setValue<QObject*>(qobject);
*vptr = variant;
}
void dos_qvariant_isnull(void* vptr, bool* isNull)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(vptr);
*isNull = variant->isNull();
2014-07-19 18:26:08 +02:00
}
2015-01-31 17:01:03 +01:00
void dos_qvariant_delete(void* vptr)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(vptr);
2014-07-19 18:26:08 +02:00
delete variant;
}
void dos_qvariant_assign(void* vptr, void* other)
{
auto leftQVariant = reinterpret_cast<QVariant*>(vptr);
auto rightQVariant = reinterpret_cast<QVariant*>(other);
*leftQVariant = *rightQVariant;
}
void dos_qvariant_toInt(void* vptr, int* value)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(vptr);
*value = variant->toInt();
2014-07-19 18:26:08 +02:00
}
void dos_qvariant_toBool(void* vptr, bool* value)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(vptr);
*value = variant->toBool();
2014-07-19 18:26:08 +02:00
}
void dos_qvariant_toFloat(void* vptr, float* value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*value = variant->toFloat();
}
void dos_qvariant_toDouble(void* vptr, double* value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*value = variant->toDouble();
}
void dos_qvariant_toString(void* vptr, char** ptr)
2014-07-19 18:26:08 +02:00
{
2014-08-30 18:46:34 +02:00
auto variant = reinterpret_cast<QVariant*>(vptr);
convert_to_cstring(variant->toString(), ptr);
2014-07-19 18:26:08 +02:00
}
void dos_qvariant_toQObject(void* vptr, void** value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*value = variant->value<QObject*>();
}
2014-08-30 18:46:34 +02:00
void dos_qvariant_setInt(void* vptr, int value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*variant = value;
}
void dos_qvariant_setBool(void* vptr, bool value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*variant = value;
}
void dos_qvariant_setFloat(void* vptr, float value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*variant = value;
}
void dos_qvariant_setDouble(void* vptr, double value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*variant = value;
}
2014-08-30 18:46:34 +02:00
void dos_qvariant_setString(void* vptr, const char* value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
*variant = value;
}
void dos_qvariant_setQObject(void* vptr, void* value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
auto qobject = reinterpret_cast<QObject*>(value);
variant->setValue<QObject*>(qobject);
}
2015-01-26 19:11:50 +01:00
void dos_qvariant_setQAbstractListModel(void* vptr, void* value)
{
auto variant = reinterpret_cast<QVariant*>(vptr);
auto qobject = reinterpret_cast<QObject*>(value);
variant->setValue<QObject*>(qobject);
}
void dos_qobject_create(void** vptr, void* dObjectPointer,
MetaObjectCallback dMetaObjectCallback,
DObjectCallback dObjectCallback)
{
auto dynamicQObject = new DosQObject();
auto impl = std::make_unique<DynamicQObjectImpl>(dynamicQObject,
OnMetaObjectHandler(dObjectPointer, dMetaObjectCallback),
OnSlotExecutedHandler(dObjectPointer, dObjectCallback));
dynamicQObject->setImpl(std::move(impl));
QQmlEngine::setObjectOwnership(dynamicQObject, QQmlEngine::CppOwnership);
*vptr = dynamicQObject;
}
2015-01-31 17:01:03 +01:00
void dos_qobject_delete(void* vptr)
2014-07-19 18:26:08 +02:00
{
auto qobject = reinterpret_cast<QObject*>(vptr);
qobject->disconnect();
delete qobject;
2014-08-30 18:46:34 +02:00
}
void dos_qobject_signal_emit(void* vptr, const char* name, int parametersCount, void** parameters)
{
auto qobject = reinterpret_cast<QObject*>(vptr);
auto dynamicQObject = dynamic_cast<IDosQObject*>(qobject);
auto transformation = [](void* vptr)->QVariant{return *(reinterpret_cast<QVariant*>(vptr));};
const std::vector<QVariant> variants = toVector(parameters, parametersCount, transformation);
dynamicQObject->emitSignal(QString::fromStdString(name), variants);
2014-08-30 18:46:34 +02:00
}
void dos_qobject_signal_connect(void* senderVPtr,
const char* signal,
void* receiverVPtr,
const char* method,
int type,
bool* result)
{
auto sender = reinterpret_cast<QObject*>(senderVPtr);
auto receiver = reinterpret_cast<QObject*>(receiverVPtr);
*result = QObject::connect(sender, signal, receiver, method, (Qt::ConnectionType) type);
}
void dos_qobject_signal_disconnect(void* senderVPtr,
const char* signal,
void* receiverVPtr,
const char* method,
bool* result)
{
auto sender = reinterpret_cast<QObject*>(senderVPtr);
auto receiver = reinterpret_cast<QObject*>(receiverVPtr);
*result = QObject::disconnect(sender, signal, receiver, method);
}
void dos_qobject_objectName(void* vptr, char** result)
{
2015-12-29 15:50:43 +01:00
auto object = reinterpret_cast<QObject*>(vptr);
convert_to_cstring(object->objectName(), result);
}
void dos_qobject_findChild(void* vptr, const char* name, int options, void** child)
{
2015-12-29 15:50:43 +01:00
auto object = reinterpret_cast<QObject*>(vptr);
*child = object->findChild<QObject*>(QString::fromUtf8(name), (Qt::FindChildOptions) options);
}
void dos_qmodelindex_create(void** vptr)
{
auto index = new QModelIndex();
*vptr = index;
}
void dos_qmodelindex_delete(void* vptr)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
delete index;
}
void dos_qmodelindex_row(void* vptr, int* row)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
*row = index->row();
}
void dos_qmodelindex_column(void* vptr, int* column)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
*column = index->column();
}
void dos_qmodelindex_isValid(void* vptr, bool* isValid)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
*isValid = index->isValid();
}
void dos_qmodelindex_data(void* vptr, int role, void* data)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
auto result = reinterpret_cast<QVariant*>(data);
*result = index->data(role);
}
void dos_qmodelindex_parent(void* vptr, void* parent)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
auto parentIndex = reinterpret_cast<QModelIndex*>(parent);
*parentIndex = index->parent();
}
void dos_qmodelindex_child(void* vptr, int row, int column, void* child)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
auto childIndex = reinterpret_cast<QModelIndex*>(child);
*childIndex = index->child(row, column);
}
void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling)
{
auto index = reinterpret_cast<QModelIndex*>(vptr);
auto siblingIndex = reinterpret_cast<QModelIndex*>(sibling);
*siblingIndex = index->sibling(row, column);
}
2015-01-31 12:50:14 +01:00
void dos_qhash_int_qbytearray_create(QHashIntQByteArrayVoidPtr* vptr)
{
*vptr = new QHash<int, QByteArray>();
}
void dos_qhash_int_qbytearray_delete(QHashIntQByteArrayVoidPtr vptr)
{
2015-01-31 17:01:03 +01:00
auto qHash = reinterpret_cast<QHash<int, QByteArray>*>(vptr);
2015-01-31 12:50:14 +01:00
delete qHash;
}
void dos_qhash_int_qbytearray_insert(QHashIntQByteArrayVoidPtr vptr, int key, const char* value)
2015-01-31 12:50:14 +01:00
{
auto qHash = reinterpret_cast<QHash<int, QByteArray>*>(vptr);
qHash->insert(key, QByteArray(value));
}
void dos_qhash_int_qbytearray_value(QHashIntQByteArrayVoidPtr vptr, int key, char** result)
{
auto qHash = reinterpret_cast<QHash<int, QByteArray>*>(vptr);
QByteArray value = qHash->value(key);
*result = qstrdup(value.data());
}
void dos_qresource_register(const char* filename)
{
2015-12-06 00:23:25 +01:00
QResource::registerResource(QString::fromUtf8(filename));
}
void dos_qurl_create(void** vptr, const char* url, int parsingMode)
{
2015-12-06 00:23:25 +01:00
*vptr = new QUrl(QString::fromUtf8(url), (QUrl::ParsingMode) parsingMode);
}
void dos_qurl_delete(void* vptr)
{
2015-12-29 15:50:43 +01:00
auto url = reinterpret_cast<QUrl*>(vptr);
delete url;
}
2015-12-22 17:53:29 +01:00
void dos_qurl_to_string(void* vptr, char** result)
{
2015-12-29 15:50:43 +01:00
auto url = reinterpret_cast<QUrl*>(vptr);
2015-12-22 17:53:29 +01:00
convert_to_cstring(url->toString(), result);
}
2016-01-06 15:42:21 +01:00
void dos_qmetaobject_create(void **vptr,
void* superClassVPtr,
const char* className,
::SignalDefinitions signalDefinitions,
::SlotDefinitions slotDefinitions,
::PropertyDefinitions propertyDefinitions)
{
auto superClassHolder = static_cast<DosIQMetaObjectHolder*>(superClassVPtr);
auto metaObject = std::make_shared<DosQMetaObject>(*superClassHolder->data(),
QString::fromUtf8(className),
toVector(signalDefinitions),
toVector(slotDefinitions),
toVector(propertyDefinitions));
*vptr = new DosIQMetaObjectHolder(std::move(metaObject));
}
2016-01-06 15:42:21 +01:00
void dos_qmetaobject_delete(void *vptr)
{
auto factory = reinterpret_cast<DosIQMetaObjectHolder*>(vptr);
delete factory;
}
void dos_qobject_qmetaobject(void **vptr)
{
*vptr = new DosIQMetaObjectHolder(std::make_shared<DosQObjectMetaObject>());
}
void dos_qabstractlistmodel_qmetaobject(void **vptr)
{
*vptr = new DosIQMetaObjectHolder(std::make_shared<DosQAbstractListModelMetaObject>());
}