/*
MVC sample/demo app
Qt Based Implementation
Copyright (C) 2020, John Ryland
*/
#include <QApplication>
#include <QPushButton>
#include <QMessageBox>
#include "ModelViewController.hpp"
/*
This example shows how using the MVC classes, there is automatic
creation of the UI from a .ui file, and the signals and slots to
the created view and between the model, view and controller are
connected together.
In fact all combinations of pairs of model, view, controller are
hooked up if provided, however the example provided below only
shows the traditional MVC style where the model is only manipulated
by the controller, and the view is only updated by the model.
*/
class Model : public ModelBase
{
Q_OBJECT
public:
Model()
{
timerId1 = startTimer(5000);
timerId2 = startTimer(7000);
}
void timerEvent(QTimerEvent* te)
{
if (te->timerId() == timerId1)
{
emit dataChanged();
}
else if (te->timerId() == timerId2)
{
emit dataChanged();
}
}
private slots:
void on_controller_updateSomething(std::string s) // manipulates
{
QMessageBox::information(nullptr, "Info", ("model needs update " + s).c_str(), "Okay");
}
signals:
void dataChanged();
private:
int timerId1;
int timerId2;
};
class View : public ViewBase
{
Q_OBJECT
public:
View()
: ViewBase("MVC_sample.ui")
{
}
private slots:
void on_model_dataChanged() // updates
{
// refresh view
QMessageBox::information(nullptr, "Info", "view needs update", "Okay");
}
};
class Controller : public ControllerBase
{
Q_OBJECT
public:
Controller(ModelBase* model, ViewBase* view)
: ControllerBase(model, view)
{
}
private slots:
void on_blahButton_clicked() // uses
{
QMessageBox::information(nullptr, "Info", "user clicked button 1", "Okay");
updateSomething("1");
}
void on_fooButton_clicked() // uses
{
QMessageBox::information(nullptr, "Info", "user clicked button 2", "Okay");
updateSomething("2");
}
signals:
void updateSomething(std::string);
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Controller modelViewController(new Model(), new View());
modelViewController.start();
return app.exec();
}
#include "MVC_sample.moc"