Browse Source

fixed widgets presenter

pull/2/head
Skycoder42 7 years ago
parent
commit
1f740738ee
  1. 12
      examples/mvvmcore/SampleCore/samplecoreapp.cpp
  2. 52
      examples/mvvmcore/SampleCore/sampleviewmodel.cpp
  3. 21
      examples/mvvmcore/SampleCore/sampleviewmodel.h
  4. 1
      examples/mvvmwidgets/SampleWidgets/sampleview.cpp
  5. 14
      src/mvvmcore/viewmodel.cpp
  6. 5
      src/mvvmcore/viewmodel.h
  7. 25
      src/mvvmwidgets/widgetspresenter.cpp

12
examples/mvvmcore/SampleCore/samplecoreapp.cpp

@ -19,13 +19,19 @@ int SampleCoreApp::startApp(const QStringList &arguments)
parser.addHelpOption();
parser.addOption({
{QStringLiteral("n"), QStringLiteral("nothing")},
QStringLiteral("Pointless action")
{QStringLiteral("a"), QStringLiteral("active")},
QStringLiteral("Start with the active checkbox already checked")
});
parser.addPositionalArgument(QStringLiteral("names"),
QStringLiteral("A list of names, joined together as one string"),
QStringLiteral("[name..."));
if(!autoParse(parser, arguments))
return EXIT_SUCCESS;
show<SampleViewModel>();
QVariantHash args;
args.insert(SampleViewModel::KeyActive, parser.isSet(QStringLiteral("active")));
args.insert(SampleViewModel::KeyNames, parser.positionalArguments());
show<SampleViewModel>(args);
return EXIT_SUCCESS;
}

52
examples/mvvmcore/SampleCore/sampleviewmodel.cpp

@ -1,15 +1,24 @@
#include "sampleviewmodel.h"
#include <QDebug>
const QString SampleViewModel::KeyActive = QStringLiteral("active");
const QString SampleViewModel::KeyNames = QStringLiteral("names");
SampleViewModel::SampleViewModel(QObject *parent) :
ViewModel(parent),
_name(),
_active(false),
_events(),
_eventsModel(new QStringListModel(this)),
_eventService(nullptr),
_eventId(-1)
{}
SampleViewModel::~SampleViewModel()
{
qInfo(Q_FUNC_INFO);
setActive(false);
}
QString SampleViewModel::name() const
{
return _name;
@ -20,9 +29,9 @@ bool SampleViewModel::active() const
return _active;
}
QStringList SampleViewModel::events() const
QStringListModel *SampleViewModel::eventsModel() const
{
return _events;
return _eventsModel;
}
void SampleViewModel::setName(QString name)
@ -55,8 +64,7 @@ void SampleViewModel::setActive(bool active)
void SampleViewModel::clearEvents()
{
_events.clear();
emit eventsChanged(_events);
_eventsModel->setStringList({});
}
void SampleViewModel::onInit(const QVariantHash &params)
@ -65,35 +73,17 @@ void SampleViewModel::onInit(const QVariantHash &params)
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);
auto names = params.value(KeyNames).toStringList();
if(!names.isEmpty())
setName(names.join(QLatin1Char(' ')));
setActive(params.value(KeyActive, false).toBool());
}
void SampleViewModel::addEvent(const QString &event)
{
_events.append(event);
emit eventsChanged(_events);
}
void SampleViewModel::setEventService(IEventService *eventService)
{
_eventService = eventService;
qDebug() << event;
auto row = _eventsModel->rowCount();
if(_eventsModel->insertRow(row))
_eventsModel->setData(_eventsModel->index(row), event);
}

21
examples/mvvmcore/SampleCore/sampleviewmodel.h

@ -1,6 +1,8 @@
#ifndef SAMPLEVIEWMODEL_H
#define SAMPLEVIEWMODEL_H
#include <QtCore/QStringListModel>
#include <QtMvvmCore/ViewModel>
#include "ieventservice.h"
@ -11,17 +13,20 @@ class SampleViewModel : public QtMvvm::ViewModel
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)
Q_PROPERTY(QStringListModel* eventsModel READ eventsModel CONSTANT)
Q_PROPERTY(IEventService* eventService MEMBER _eventService WRITE setEventService)
QTMVVM_INJECT(IEventService*, eventService)
QTMVVM_INJECT_PROP(IEventService*, eventService, _eventService)
public:
static const QString KeyActive;
static const QString KeyNames;
Q_INVOKABLE explicit SampleViewModel(QObject *parent = nullptr);
~SampleViewModel();
QString name() const;
bool active() const;
QStringList events() const;
QStringListModel *eventsModel() const;
public Q_SLOTS:
void setName(QString name);
@ -31,13 +36,9 @@ public Q_SLOTS:
Q_SIGNALS:
void nameChanged(QString name);
void activeChanged(bool active);
void eventsChanged(QStringList events);
protected:
void onInit(const QVariantHash &params) override;
void onDestroy() override;
void onShow() override;
void onClose() override;
private Q_SLOTS:
void addEvent(const QString &event);
@ -45,12 +46,10 @@ private Q_SLOTS:
private:
QString _name;
bool _active;
QStringList _events;
QStringListModel *_eventsModel;
IEventService* _eventService;
int _eventId;
void setEventService(IEventService *eventService);
};
#endif // SAMPLEVIEWMODEL_H

1
examples/mvvmwidgets/SampleWidgets/sampleview.cpp

@ -6,6 +6,7 @@ SampleView::SampleView(QtMvvm::ViewModel *viewModel, QWidget *parent) :
_viewModel(static_cast<SampleViewModel*>(viewModel)),
ui(new Ui::SampleView)
{
Q_ASSERT(_viewModel);
ui->setupUi(this);
}

14
src/mvvmcore/viewmodel.cpp

@ -31,20 +31,6 @@ void ViewModel::setDeleteOnClose(bool deleteOnClose)
void ViewModel::onInit(const QVariantHash &) {}
void ViewModel::onDestroy() {}
void ViewModel::onShow() {}
void ViewModel::onClose() {}
void ViewModel::updateVisible(bool visible)
{
if(visible)
onShow();
else
onClose();
}
void ViewModel::showImp(const QMetaObject *mo, const QVariantHash &params, QPointer<ViewModel> parent)
{
CoreAppPrivate::dInstance()->showViewModel(mo, params, parent);

5
src/mvvmcore/viewmodel.h

@ -34,11 +34,6 @@ public Q_SLOTS:
void setDeleteOnClose(bool deleteOnClose);
virtual void onInit(const QVariantHash &params);
virtual void onDestroy();
virtual void onShow();
virtual void onClose();
void updateVisible(bool visible);
Q_SIGNALS:
void deleteOnCloseChanged(bool deleteOnClose, QPrivateSignal);

25
src/mvvmwidgets/widgetspresenter.cpp

@ -60,20 +60,10 @@ void WidgetsPresenter::present(ViewModel *viewModel, const QVariantHash &params,
}
// initialize viewmodel and view relationship
QMetaObject::Connection noCycleDestroyCon;
viewModel->setParent(view);
auto hasCycle = setupLifeCycle(viewModel, view);
if(hasCycle) {
QObject::connect(view, &QWidget::destroyed,
viewModel, &ViewModel::onDestroy,
Qt::DirectConnection);
} else {
if(!hasCycle)
view->setAttribute(Qt::WA_DeleteOnClose);
noCycleDestroyCon = QObject::connect(view, &QWidget::destroyed, viewModel, [viewModel](){
viewModel->onClose();
viewModel->onDestroy();
}, Qt::DirectConnection);
}
viewModel->onInit(params);
// present the view
@ -87,16 +77,10 @@ void WidgetsPresenter::present(ViewModel *viewModel, const QVariantHash &params,
//handle the present result
if(presented) {
//if no lifecycle explicit on show is needed
if(!hasCycle)
viewModel->onShow();
//TODO needed?
} else {
logCritical() << "Unable to find a method that is able to present a view of type"
<< viewMetaObject->className();
//if no lifecycle only destroy on error, not close and destroy
if(!hasCycle) {
view->disconnect(noCycleDestroyCon);
viewModel->onDestroy();
}
view->deleteLater();
}
}
@ -153,7 +137,7 @@ bool WidgetsPresenter::tryPresent(QWidget *view, QWidget *parentView)
}
// for both, the parent and the central view, depending on which is available
for(auto pView : QList<QWidget*>{} << parentView << central) {
for(auto pView : { parentView, central }) {
if(!pView)
continue;
@ -173,6 +157,9 @@ bool WidgetsPresenter::tryPresent(QWidget *view, QWidget *parentView)
bool WidgetsPresenter::setupLifeCycle(ViewModel *viewModel, QWidget *view)
{
//TODO fix
return false;
auto viewMo = view->metaObject();
if(viewMo->indexOfSignal("qtmvvm_visibleChanged(bool)") != -1) {
connect(view, SIGNAL(qtmvvm_visibleChanged(bool)),

Loading…
Cancel
Save