dotherside/lib/src/DOtherSide.cpp

564 lines
16 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-01-31 12:50:14 +01:00
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlApplicationEngine>
#include <QtQuick/QQuickView>
#include <QtWidgets/QApplication>
#include <QResource>
2014-07-19 18:26:08 +02:00
#include "DOtherSide/DynamicQObject.h"
#include "DOtherSide/BaseQAbstractListModel.h"
#include "DOtherSide/BaseQObject.h"
#include "DOtherSide/OnSlotExecutedHandler.h"
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_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)
{
QQmlApplicationEngine* engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
engine->load(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QDir::separator() + QString(filename)));
}
void dos_qqmlapplicationengine_load_url(void* vptr, void* url)
{
QQmlApplicationEngine* engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
QUrl* qurl = reinterpret_cast<QUrl*>(url);
engine -> load(*qurl);
}
void dos_qqmlapplicationengine_add_import_path(void* vptr, const char* path)
{
QQmlApplicationEngine* engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
engine -> addImportPath(QString(path));
}
2014-12-08 12:55:09 +01:00
void dos_qqmlapplicationengine_context(void* vptr, void** context)
{
QQmlApplicationEngine* engine = reinterpret_cast<QQmlApplicationEngine*>(vptr);
engine->rootContext();
*context = engine->rootContext();
}
void dos_qqmlapplicationengine_delete(void* vptr)
{
QQmlApplicationEngine* 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
{
QQuickView* view = reinterpret_cast<QQuickView*>(vptr);
view->show();
}
void dos_qquickview_delete(void* vptr)
2014-07-19 18:26:08 +02:00
{
QQuickView* view = reinterpret_cast<QQuickView*>(vptr);
delete view;
}
void dos_qquickview_source(void* vptr, char** result)
2014-07-19 18:26:08 +02:00
{
QQuickView* view = reinterpret_cast<QQuickView*>(vptr);
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
{
QQuickView* view = reinterpret_cast<QQuickView*>(vptr);
view->setSource(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QDir::separator() + QString(filename)));
}
void dos_qquickview_rootContext(void* vptr, void** context)
2014-07-19 18:26:08 +02:00
{
QQuickView* view = reinterpret_cast<QQuickView*>(vptr);
*context = view->rootContext();
}
void dos_chararray_delete(char* ptr)
2014-07-19 18:26:08 +02:00
{
if (ptr) delete[] ptr;
}
void dos_qqmlcontext_baseUrl(void* vptr, char** result)
2014-07-19 18:26:08 +02:00
{
QQmlContext* context = reinterpret_cast<QQmlContext*>(vptr);
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
{
QQmlContext* 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
}
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);
}
2014-08-30 18:46:34 +02:00
void dos_qobject_create(void** vptr, void* dObjectPointer, DObjectCallback dObjectCallback)
2014-07-19 18:26:08 +02:00
{
auto dynamicQObject = new BaseQObject();
2014-12-08 12:55:09 +01:00
QQmlEngine::setObjectOwnership(dynamicQObject, QQmlEngine::CppOwnership);
dynamicQObject->setOnSlotExecutedHandler(OnSlotExecutedHandler(dObjectPointer, dObjectCallback));
2014-07-19 18:26:08 +02:00
*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 dynamicQObject = reinterpret_cast<BaseQObject*>(vptr);
2014-12-08 12:55:09 +01:00
dynamicQObject->disconnect();
2014-07-19 18:26:08 +02:00
delete dynamicQObject;
}
void dos_qobject_slot_create(void* vptr, const char* name, int parametersCount, int* parametersMetaTypes, int* slotIndex)
{
if (parametersCount <= 0)
return;
auto qobject = reinterpret_cast<QObject*>(vptr);
auto dynamicQObject = dynamic_cast<IDynamicQObject*>(qobject);
2014-07-19 18:26:08 +02:00
QMetaType::Type returnType = static_cast<QMetaType::Type>(parametersMetaTypes[0]);
QList<QMetaType::Type> argumentsTypes;
for (int i = 1; i < parametersCount; ++i)
argumentsTypes << static_cast<QMetaType::Type>(parametersMetaTypes[i]);
dynamicQObject->registerSlot(QString::fromStdString(name), returnType, argumentsTypes, *slotIndex);
}
2014-08-30 18:46:34 +02:00
void dos_qobject_signal_create(void* vptr, const char* name, int parametersCount, int* parametersMetaTypes, int* signalIndex)
{
if (parametersCount <= 0)
return;
auto qobject = reinterpret_cast<QObject*>(vptr);
auto dynamicQObject = dynamic_cast<IDynamicQObject*>(qobject);
2014-08-30 18:46:34 +02:00
QList<QMetaType::Type> argumentsTypes;
for (int i = 0; i < parametersCount; ++i)
argumentsTypes << static_cast<QMetaType::Type>(parametersMetaTypes[i]);
dynamicQObject->registerSignal(QString::fromStdString(name), argumentsTypes, *signalIndex);
}
void dos_qobject_signal_emit(void* vptr, const char* name, int parametersCount, void** parameters)
{
auto qobject = reinterpret_cast<QObject*>(vptr);
auto dynamicQObject = dynamic_cast<IDynamicQObject*>(qobject);
2014-08-30 18:46:34 +02:00
QVariantList arguments;
for (int i = 0; i < parametersCount; ++i)
arguments << *(reinterpret_cast<QVariant*>(parameters[i]));
2014-08-30 18:46:34 +02:00
dynamicQObject->emitSignal(QString::fromStdString(name), arguments);
}
void dos_qobject_property_create(void* vptr,
const char* name,
int type,
const char* readSlot,
const char* writeSlot,
const char* notifySignal)
{
auto qobject = reinterpret_cast<QObject*>(vptr);
auto dynamicQObject = dynamic_cast<IDynamicQObject*>(qobject);
dynamicQObject->registerProperty(QString(name),
QMetaType::Type(type),
QString(readSlot),
QString(writeSlot),
QString(notifySignal));
}
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-20 21:18:58 +01:00
void dos_qabstractlistmodel_create(void** vptr,
void* dObjectPointer,
DObjectCallback dObjectCallback,
2015-01-26 21:26:21 +01:00
RowCountCallback rowCountCallback,
ColumnCountCallback columnCountCallback,
DataCallback dataCallback,
SetDataCallback setDataCallback,
RoleNamesCallback roleNamesCallaback,
FlagsCallback flagsCallback,
HeaderDataCallback headerDataCallback)
{
auto model = new BaseQAbstractListModel(dObjectPointer,
2015-01-31 17:01:03 +01:00
rowCountCallback,
columnCountCallback,
2015-01-31 17:01:03 +01:00
dataCallback,
setDataCallback,
roleNamesCallaback,
flagsCallback,
headerDataCallback);
model->setOnSlotExecutedHandler(OnSlotExecutedHandler(dObjectPointer, dObjectCallback));
*vptr = model;
}
void dos_qabstractlistmodel_delete(void* vptr)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
delete model;
}
2015-01-31 12:50:14 +01:00
void dos_qabstractlistmodel_beginInsertRows(void* vptr, QModelIndexVoidPtr parentIndex, int first, int last)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
auto index = reinterpret_cast<QModelIndex*>(parentIndex);
model->publicBeginInsertRows(*index, first, last);
}
void dos_qabstractlistmodel_endInsertRows(void* vptr)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
model->publicEndInsertRows();
}
void dos_qabstractlistmodel_beginRemoveRows(void* vptr, QModelIndexVoidPtr parentIndex, int first, int last)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
auto index = reinterpret_cast<QModelIndex*>(parentIndex);
model->publicBeginRemoveRows(*index, first, last);
}
void dos_qabstractlistmodel_endRemoveRows(void* vptr)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
model->publicEndRemoveRows();
}
void dos_qabstractlistmodel_beginResetModel(void* vptr)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
model->publicBeginResetModel();
}
void dos_qabstractlistmodel_endResetModel(void* vptr)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
model->publicEndResetModel();
}
2015-02-04 23:02:26 +01:00
void dos_qabstractlistmodel_dataChanged(void* vptr,
QModelIndexVoidPtr topLeftIndex,
QModelIndexVoidPtr bottomRightIndex,
int* rolesArrayPtr,
int rolesArrayLength)
{
auto model = reinterpret_cast<BaseQAbstractListModel*>(vptr);
auto topLeft = reinterpret_cast<QModelIndex*>(topLeftIndex);
auto bottomRight = reinterpret_cast<QModelIndex*>(bottomRightIndex);
auto roles = QVector<int>::fromStdVector(std::vector<int>(rolesArrayPtr, rolesArrayPtr + rolesArrayLength));
model->publicDataChanged(*topLeft, *bottomRight, roles);
}
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)
{
QResource::registerResource(QString(filename));
}
void dos_qurl_create(void** vptr, const char* url, int parsingMode)
{
*vptr = new QUrl(QString(url), (QUrl::ParsingMode) parsingMode);
}
void dos_qurl_delete(void* vptr)
{
QUrl* url = reinterpret_cast<QUrl*>(vptr);
delete url;
}