Browse Source

added alias support

pull/2/head
Skycoder42 7 years ago
parent
commit
8e7609951a
  1. 7
      src/mvvmquick/Switch.qml
  2. 25
      src/mvvmquick/inputviewfactory.cpp
  3. 23
      src/mvvmquick/inputviewfactory.h
  4. 1
      src/mvvmquick/inputviewfactory_p.h
  5. 1
      src/mvvmquick/qtmvvmquick_module.qrc
  6. 10
      src/mvvmwidgets/inputwidgetfactory.cpp
  7. 14
      src/mvvmwidgets/inputwidgetfactory.h
  8. 1
      src/mvvmwidgets/inputwidgetfactory_p.h

7
src/mvvmquick/Switch.qml

@ -0,0 +1,7 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
Switch {
id: _edit
property alias inputValue: _edit.checked
}

25
src/mvvmquick/inputviewfactory.cpp

@ -38,23 +38,17 @@ InputViewFactory::InputViewFactory() :
InputViewFactory::~InputViewFactory() {} InputViewFactory::~InputViewFactory() {}
//int InputViewFactory::metaTypeId(const QByteArray &type, const QVariantMap &properties)
//{
// if(type == "string")
// return QMetaType::QString;
// else if(type == "list")
// return metaTypeId(properties.value(QStringLiteral("_list_data"), QByteArray("string")).toByteArray(), properties);
// else
// return QMetaType::type(type);
//}
QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &viewProperties) QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &viewProperties)
{ {
Q_UNUSED(viewProperties) Q_UNUSED(viewProperties)
if(d->aliases.contains(type))
return getInputUrl(d->aliases.value(type), viewProperties);
if(d->simpleViews.contains(type)) if(d->simpleViews.contains(type))
return d->simpleViews.value(type); return d->simpleViews.value(type);
else if(type == QMetaType::typeName(QMetaType::Bool)) else if(type == QMetaType::typeName(QMetaType::Bool))
return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/CheckBox.qml"); return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/CheckBox.qml");
else if(type == "switch")
return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/Switch.qml");
else if(type == QMetaType::typeName(QMetaType::QString) || type == "string") else if(type == QMetaType::typeName(QMetaType::QString) || type == "string")
return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/TextField.qml"); return QStringLiteral("qrc:/de/skycoder42/qtmvvm/quick/inputs/TextField.qml");
else if(type == QMetaType::typeName(QMetaType::Int)) else if(type == QMetaType::typeName(QMetaType::Int))
@ -79,15 +73,20 @@ QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &vi
} }
} }
bool InputViewFactory::addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl) void InputViewFactory::addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl)
{ {
d->simpleViews.insert(type, qmlFileUrl); d->simpleViews.insert(type, qmlFileUrl);
return true; }
void InputViewFactory::addAlias(const QByteArray &alias, const QByteArray &targetType)
{
d->aliases.insert(alias, targetType);
} }
// ------------- Private Implementation ------------- // ------------- Private Implementation -------------
InputViewFactoryPrivate::InputViewFactoryPrivate() : InputViewFactoryPrivate::InputViewFactoryPrivate() :
simpleViews() simpleViews(),
aliases()
{} {}

23
src/mvvmquick/inputviewfactory.h

@ -19,21 +19,30 @@ public:
InputViewFactory(); InputViewFactory();
virtual ~InputViewFactory(); virtual ~InputViewFactory();
//virtual int metaTypeId(const QByteArray &type, const QVariantMap &properties);
Q_INVOKABLE virtual QUrl getInputUrl(const QByteArray &type, const QVariantMap &viewProperties); Q_INVOKABLE virtual QUrl getInputUrl(const QByteArray &type, const QVariantMap &viewProperties);
Q_INVOKABLE virtual bool addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl); Q_INVOKABLE virtual void addSimpleView(const QByteArray &type, const QUrl &qmlFileUrl);
template <typename T> template <typename TType>
bool addSimpleView(const QUrl &qmlFileUrl); inline void addSimpleView(const QUrl &qmlFileUrl);
Q_INVOKABLE virtual void addAlias(const QByteArray &alias, const QByteArray &targetType);
template <typename TAliasType, typename TTargetType>
inline void addAlias();
private: private:
QScopedPointer<InputViewFactoryPrivate> d; QScopedPointer<InputViewFactoryPrivate> d;
}; };
template<typename T> template<typename TType>
bool InputViewFactory::addSimpleView(const QUrl &qmlFileUrl) inline void InputViewFactory::addSimpleView(const QUrl &qmlFileUrl)
{
addSimpleView(QMetaType::typeName(qMetaTypeId<TType>()), qmlFileUrl);
}
template<typename TAliasType, typename TTargetType>
inline void InputViewFactory::addAlias()
{ {
return addSimpleView(QMetaType::typeName(qMetaTypeId<T>()), qmlFileUrl); addAlias(QMetaType::typeName(qMetaTypeId<TAliasType>()), QMetaType::typeName(qMetaTypeId<TTargetType>()));
} }
} }

1
src/mvvmquick/inputviewfactory_p.h

@ -12,6 +12,7 @@ public:
InputViewFactoryPrivate(); InputViewFactoryPrivate();
QHash<QByteArray, QUrl> simpleViews; QHash<QByteArray, QUrl> simpleViews;
QHash<QByteArray, QByteArray> aliases;
}; };
} }

1
src/mvvmquick/qtmvvmquick_module.qrc

@ -7,5 +7,6 @@
<file>DoubleSpinBox.qml</file> <file>DoubleSpinBox.qml</file>
<file>FontEdit.qml</file> <file>FontEdit.qml</file>
<file>UrlField.qml</file> <file>UrlField.qml</file>
<file>Switch.qml</file>
</qresource> </qresource>
</RCC> </RCC>

10
src/mvvmwidgets/inputwidgetfactory.cpp

@ -24,6 +24,8 @@ InputWidgetFactory::~InputWidgetFactory() {}
QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties) QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent, const QVariantMap &viewProperties)
{ {
QWidget *widget = nullptr; QWidget *widget = nullptr;
if(d->aliases.contains(type))
return createInput(d->aliases.value(type), parent, viewProperties);
if(d->simpleWidgets.contains(type)) if(d->simpleWidgets.contains(type))
widget = d->simpleWidgets.value(type)(parent); widget = d->simpleWidgets.value(type)(parent);
else if(type == QMetaType::typeName(QMetaType::Bool)) else if(type == QMetaType::typeName(QMetaType::Bool))
@ -78,8 +80,14 @@ void InputWidgetFactory::addSimpleWidget(const QByteArray &type, const std::func
d->simpleWidgets.insert(type, creator); d->simpleWidgets.insert(type, creator);
} }
void InputWidgetFactory::addAlias(const QByteArray &alias, const QByteArray &targetType)
{
d->aliases.insert(alias, targetType);
}
// ------------- Private Implementation ------------- // ------------- Private Implementation -------------
InputWidgetFactoryPrivate::InputWidgetFactoryPrivate() : InputWidgetFactoryPrivate::InputWidgetFactoryPrivate() :
simpleWidgets() simpleWidgets(),
aliases()
{} {}

14
src/mvvmwidgets/inputwidgetfactory.h

@ -22,20 +22,30 @@ public:
virtual void addSimpleWidget(const QByteArray &type, const std::function<QWidget*(QWidget*)> &creator); virtual void addSimpleWidget(const QByteArray &type, const std::function<QWidget*(QWidget*)> &creator);
template <typename TType, typename TWidget> template <typename TType, typename TWidget>
void addSimpleWidget(); inline void addSimpleWidget();
virtual void addAlias(const QByteArray &alias, const QByteArray &targetType);
template <typename TAliasType, typename TTargetType>
inline void addAlias();
private: private:
QScopedPointer<InputWidgetFactoryPrivate> d; QScopedPointer<InputWidgetFactoryPrivate> d;
}; };
template<typename TType, typename TWidget> template<typename TType, typename TWidget>
void InputWidgetFactory::addSimpleWidget() inline void InputWidgetFactory::addSimpleWidget()
{ {
addSimpleWidget(QMetaType::typeName(qMetaTypeId<TType>()), [](QWidget *parent){ addSimpleWidget(QMetaType::typeName(qMetaTypeId<TType>()), [](QWidget *parent){
return new TWidget(parent); return new TWidget(parent);
}); });
} }
template<typename TAliasType, typename TTargetType>
inline void InputWidgetFactory::addAlias()
{
addAlias(QMetaType::typeName(qMetaTypeId<TAliasType>()), QMetaType::typeName(qMetaTypeId<TTargetType>()));
}
} }
#endif // QTMVVM_INPUTWIDGETFACTORY_H #endif // QTMVVM_INPUTWIDGETFACTORY_H

1
src/mvvmwidgets/inputwidgetfactory_p.h

@ -12,6 +12,7 @@ public:
InputWidgetFactoryPrivate(); InputWidgetFactoryPrivate();
QHash<QByteArray, std::function<QWidget*(QWidget*)>> simpleWidgets; QHash<QByteArray, std::function<QWidget*(QWidget*)>> simpleWidgets;
QHash<QByteArray, QByteArray> aliases;
}; };
} }

Loading…
Cancel
Save