Browse Source

added sample viewmodel stuff (unused)

pull/2/head
Skycoder42 7 years ago
parent
commit
7372d01941
  1. 7
      examples/mvvmcore/SampleCore/SampleCore.pro
  2. 19
      examples/mvvmcore/SampleCore/ieventservice.h
  3. 2
      examples/mvvmcore/SampleCore/samplecoreapp.cpp
  4. 93
      examples/mvvmcore/SampleCore/sampleviewmodel.cpp
  5. 53
      examples/mvvmcore/SampleCore/sampleviewmodel.h
  6. 5
      src/mvvmcore/viewmodel.cpp
  7. 19
      src/mvvmcore/viewmodel.h

7
examples/mvvmcore/SampleCore/SampleCore.pro

@ -8,10 +8,13 @@ DEFINES += SAMPLECORE_LIBRARY
HEADERS += \
samplecore_global.h \
samplecoreapp.h
samplecoreapp.h \
sampleviewmodel.h \
ieventservice.h
SOURCES += \
samplecoreapp.cpp
samplecoreapp.cpp \
sampleviewmodel.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
INSTALLS += target

19
examples/mvvmcore/SampleCore/ieventservice.h

@ -0,0 +1,19 @@
#ifndef IEVENTSERVICE_H
#define IEVENTSERVICE_H
#include <QtCore/QObject>
class IEventService
{
public:
virtual int addEvent(const QString &name) = 0;
virtual void removeEvent(int eventId) = 0;
Q_SIGNALS:
virtual void eventTriggered(const QString &event) = 0;
};
#define IEventServiceIid "de.skycoder42.qtmvvm.sample.IEventService"
Q_DECLARE_INTERFACE(IEventService, IEventServiceIid)
#endif // IEVENTSERVICE_H

2
examples/mvvmcore/SampleCore/samplecoreapp.cpp

@ -1,4 +1,5 @@
#include "samplecoreapp.h"
#include "sampleviewmodel.h"
#include <QtCore/QCommandLineParser>
@ -23,5 +24,6 @@ int SampleCoreApp::startApp(const QStringList &arguments)
return EXIT_SUCCESS;
//TODO implement app code
QtMvvm::ViewModel::show<SampleViewModel>();
return EXIT_FAILURE;
}

93
examples/mvvmcore/SampleCore/sampleviewmodel.cpp

@ -0,0 +1,93 @@
#include "sampleviewmodel.h"
SampleViewModel::SampleViewModel(QObject *parent) :
ViewModel(parent),
_name(),
_active(false),
_events(),
_eventService(nullptr),
_eventId(-1)
{}
QString SampleViewModel::name() const
{
return _name;
}
bool SampleViewModel::active() const
{
return _active;
}
QStringList SampleViewModel::events() const
{
return _events;
}
void SampleViewModel::setName(QString name)
{
if (_name == name)
return;
_name = name;
emit nameChanged(_name);
if(_active && _eventService) {
_eventService->removeEvent(_eventId);
_eventId = _eventService->addEvent(_name);
}
}
void SampleViewModel::setActive(bool active)
{
if (_active == active)
return;
_active = active;
if(_eventService) {
if(_active)
_eventId = _eventService->addEvent(_name);
else
_eventService->removeEvent(_eventId);
}
emit activeChanged(_active);
}
void SampleViewModel::clearEvents()
{
_events.clear();
emit eventsChanged(_events);
}
void SampleViewModel::onInit()
{
qInfo(Q_FUNC_INFO);
Q_ASSERT(_eventService);
connect(dynamic_cast<QObject*>(_eventService), SIGNAL(eventTriggered(QString)),
this, SLOT(addEvent(QString)));
}
void SampleViewModel::onDestroy()
{
qInfo(Q_FUNC_INFO);
dynamic_cast<QObject*>(_eventService)->disconnect(this);
}
void SampleViewModel::onShow()
{
qInfo(Q_FUNC_INFO);
if(_active)
_eventId = _eventService->addEvent(_name);
}
void SampleViewModel::onClose()
{
qInfo(Q_FUNC_INFO);
if(_active)
_eventService->removeEvent(_eventId);
}
void SampleViewModel::addEvent(const QString &event)
{
_events.append(event);
emit eventsChanged(_events);
}

53
examples/mvvmcore/SampleCore/sampleviewmodel.h

@ -0,0 +1,53 @@
#ifndef SAMPLEVIEWMODEL_H
#define SAMPLEVIEWMODEL_H
#include <QtMvvmCore/ViewModel>
#include "ieventservice.h"
class SampleViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
Q_PROPERTY(QStringList events READ events NOTIFY eventsChanged RESET clearEvents)
QTMVVM_INJECT_PROP(IEventService*, eventService, _eventService)
public:
explicit SampleViewModel(QObject *parent = nullptr);
QString name() const;
bool active() const;
QStringList events() const;
public Q_SLOTS:
void setName(QString name);
void setActive(bool active);
void clearEvents();
Q_SIGNALS:
void nameChanged(QString name);
void activeChanged(bool active);
void eventsChanged(QStringList events);
protected:
void onInit() override;
void onDestroy() override;
void onShow() override;
void onClose() override;
private Q_SLOTS:
void addEvent(const QString &event);
private:
QString _name;
int _active;
QStringList _events;
IEventService* _eventService;
int _eventId;
};
#endif // SAMPLEVIEWMODEL_H

5
src/mvvmcore/viewmodel.cpp

@ -36,6 +36,11 @@ void ViewModel::onShow() {}
void ViewModel::onClose() {}
void ViewModel::showImp(const QMetaObject *mo, QObject *parent)
{
Q_UNIMPLEMENTED();
}
// ------------- Private Implementation -------------
ViewModelPrivate::ViewModelPrivate() :

19
src/mvvmcore/viewmodel.h

@ -1,6 +1,8 @@
#ifndef QTMVVM_VIEWMODEL_H
#define QTMVVM_VIEWMODEL_H
#include <type_traits>
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
@ -23,6 +25,9 @@ public:
bool deleteOnClose() const;
template <typename TViewModel>
static void show(QObject *parent = nullptr);
public Q_SLOTS:
void setDeleteOnClose(bool deleteOnClose);
@ -37,8 +42,22 @@ protected:
private:
QScopedPointer<ViewModelPrivate> d;
static void showImp(const QMetaObject *mo, QObject *parent);
};
template<typename TViewModel>
void ViewModel::show(QObject *parent)
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
showImp(&TViewModel::staticMetaObject, parent);
}
}
#define QTMVVM_INJECT(x) Q_CLASSINFO("inject-" #x, "true")
#define QTMVVM_INJECT_PROP(type, name, member) \
Q_PROPERTY(type name MEMBER member) \
QTMVVM_INJECT(name)
#endif // QTMVVM_VIEWMODEL_H

Loading…
Cancel
Save