7 changed files with 196 additions and 2 deletions
			
			
		@ -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
 | 
				
			||||
@ -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); | 
				
			||||
 | 
					} | 
				
			||||
@ -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
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue