Created
January 12, 2017 14:33
-
-
Save keithel/6bef6f4bab270f8b6a2b9d0fd379eea7 to your computer and use it in GitHub Desktop.
Unqlite Key Value Store QML wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qmlRegisterType<UnqliteStore>("com.l3.demo", 1, 0, "KeyValueStore"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KeyValueStore { | |
id: keyValueStore | |
source: "demo.db" | |
property alias fpsVisible: fpsLabel.visible | |
property alias rollIndicatorVisible: rollIndicator.visible | |
onFpsVisibleChanged: console.log("fpsVisible: " + fpsVisible.toLocaleString()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "unqlitestore.h" | |
#include <QEvent> | |
#include <QVariant> | |
#include <QTimer> | |
#include <QMetaProperty> | |
#include <QDebug> | |
UnqliteStore::UnqliteStore(QObject *parent) | |
: QObject(parent) | |
{ | |
m_timer = new QTimer(this); | |
connect(m_timer, &QTimer::timeout, [this]{ | |
const QMetaObject* metaObject = this->metaObject(); | |
QStringList properties; | |
for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) | |
properties << QString::fromLatin1(metaObject->property(i).name()); | |
qDebug() << properties; | |
}); | |
m_timer->setSingleShot(true); | |
m_timer->start(2000); | |
} | |
UnqliteStore::~UnqliteStore() | |
{ | |
unqlite_close(m_db); | |
} | |
void UnqliteStore::setSource(const QString &source) | |
{ | |
int ret; | |
if (!m_dbSource.isEmpty()) | |
{ | |
ret = unqlite_close(m_db); | |
Q_ASSERT(ret == UNQLITE_OK); | |
} | |
m_dbSource = source; | |
ret = unqlite_open(&m_db, m_dbSource.toLocal8Bit().constData(), UNQLITE_OPEN_CREATE | UNQLITE_OPEN_MMAP); | |
Q_ASSERT(ret == UNQLITE_OK); | |
emit sourceChanged(); | |
} | |
bool UnqliteStore::event(QEvent *event) | |
{ | |
if (event->type() != QEvent::DynamicPropertyChange) | |
return QObject::event(event); | |
QDynamicPropertyChangeEvent* dpcEvent = (QDynamicPropertyChangeEvent*)event; | |
QByteArray propName = dpcEvent->propertyName(); | |
if (propName == "source") | |
return QObject::event(event); | |
QVariant propValue = this->property(dpcEvent->propertyName()); | |
store(propName, this->property(propName)); | |
return QObject::event(event); | |
} | |
bool UnqliteStore::store(const QString &key, const QVariant &value) | |
{ | |
QByteArray valueBA(value.toByteArray()); | |
int ret = unqlite_kv_store(m_db, qPrintable(key), key.toLocal8Bit().length(), valueBA.constData(), valueBA.length()); | |
Q_ASSERT(ret == UNQLITE_OK); | |
return ret == UNQLITE_OK; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef UNQLITESTORE_H | |
#define UNQLITESTORE_H | |
#include <QObject> | |
extern "C" { | |
#include <unqlite.h> | |
} | |
class QTimer; | |
class UnqliteStore : public QObject | |
{ | |
Q_OBJECT | |
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged) | |
public: | |
explicit UnqliteStore(QObject *parent = 0); | |
~UnqliteStore(); | |
inline QString source() { return m_dbSource; } | |
void setSource(const QString& source); | |
virtual bool event(QEvent *event) override; | |
Q_INVOKABLE bool store(const QString& key, const QVariant& value); | |
signals: | |
void sourceChanged(); | |
private: | |
unqlite* m_db = nullptr; | |
QString m_dbSource; | |
QTimer* m_timer; | |
}; | |
#endif // UNQLITESTORE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment