Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "model.h"
#include "gliph.h"

class MenuController {
public:
MenuController(MenuModel& model):
m_model(model)
{}

void createNewDocument(std::string docName) { m_model.createDocument(docName); }
void importDocument(std::string docName) {m_model.importDocument(docName); }
void exportDocument(std::string asFile) { m_model.exportDocument(asFile); }

Glyph* createGlyph(GlyphType glyph, std::string description) { return m_model.createGlyph(glyph, description); }
private:
MenuModel& m_model;
};

class DrawController {
public:
DrawController(DrawModel& model):
m_model(model)
{}

void drawGlyph(Glyph* gliph, int x, int y) {m_model.drawGliph(gliph, x, y);}
void deleteGlyph(Glyph* gliph) {m_model.deleteGliph(gliph);}
private:
DrawModel& m_model;
};
24 changes: 24 additions & 0 deletions gliph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <string>

enum class GlyphType {
SIMPLE_GLYPH,
OTHER_GLYPH
};

class Glyph {
public:
Glyph(std::string description):
m_description(description)
{}
std::string getDescription() { return m_description;}
private:
std::string m_description;
};

class SimpleGlyph: public Glyph {
public:
SimpleGlyph(std::string description):
Glyph(description)
{}
};
40 changes: 40 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include "controller.h"
#include "model.h"
#include "view.h"
#include "gliph.h"

//1. Adding new gliphs is just creating new classes inheritable from Gliph
//2. Database interconnection is a new functions in MenuModel
//3. Modification of gliphs needs adding an access to their properties. Gliphstore in DrawModel allows to find elements and call appropriates methods for changing their properties
//4. Gliphstore in DrawModel allows to iterate through gliphs and work with them (replace, modification, etc)

int main()
{
MenuModel menuModel{};
DrawModel drawModel{};
MenuController menuController(menuModel);
DrawController drawController(drawModel);

View menuView {};
View drawView {};

menuModel.connect([&](Model* m) {
menuView.printStateOfDocument(m);
});

drawModel.connect([&](Model* m) {
drawView.printStateOfDocument(m);
});

menuController.createNewDocument("picture.jpg");
menuController.exportDocument("awesome_drawing.pdf");
menuController.importDocument("other_picture.jpg");

auto glyph = menuController.createGlyph(GlyphType::SIMPLE_GLYPH, "simple glyph");

drawController.drawGlyph(glyph, 0, 0);
drawController.deleteGlyph(glyph);

return 0;
}
112 changes: 112 additions & 0 deletions model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "model.h"

int DrawModel::m_counter = 0;

Model::~Model()
{}

void Model::notify()
{
for(const auto& view: m_listeners)
view(this);
}

void DrawModel::drawGliph(Glyph* gliph, int x, int y)
{
m_counter++;
std::cout << "New glyph " << gliph->getDescription() << " with number " << m_counter
<< " and coordinates: " << x << ", " << y << " created" << std::endl;

InternalData data = { data.x = x, data.y = y, data.number = m_counter };
m_gliphStore.emplace(gliph, data);
notify();
}

void DrawModel::deleteGliph(Glyph* gliph)
{
m_counter--;
auto result = m_gliphStore.find(gliph);
if(result == m_gliphStore.end())
return;
std::cout << "Glyph " << gliph->getDescription() << " with number " << result->second.number
<< " and coordinates: " << result->second.x << ", " << result->second.y << " deleted" << std::endl;
m_gliphStore.erase(result);
notify();
}

std::string DrawModel::printState()
{
std::string strRepresentation;
strRepresentation.append("Drawing current document: ");
strRepresentation.append(m_docName);
strRepresentation.append("\n");
for(const auto& pair: m_gliphStore)
{
strRepresentation.append("glyph name: ");
strRepresentation.append(pair.first->getDescription());
strRepresentation.append(", number: ");
strRepresentation.append(std::to_string(pair.second.number));
strRepresentation.append(" and coordinates: ");
strRepresentation.append(std::to_string(pair.second.x));
strRepresentation.append(", ");
strRepresentation.append(std::to_string(pair.second.y));
strRepresentation.append("\n");
}

return strRepresentation;
}

void MenuModel::createDocument(std::string &docName)
{
std::cout << "Manipulation for creating new document with name " << docName << std::endl;
m_docName = docName;
m_state = MenuModelState::DOC_CREATED;
notify();
}

void MenuModel::importDocument(std::string &docName)
{
std::cout << "Manipulation for importing document " << docName << std::endl;
m_docName = docName;
m_state = MenuModelState::DOC_IMPORTED;
notify();
}

void MenuModel::exportDocument(std::string& asFile)
{
std::cout << "Manipulation for exporting project as document " << asFile << std::endl;
m_state = MenuModelState::DOC_EXPORTED;
notify();
}

Glyph *MenuModel::createGlyph(GlyphType glyph, std::string description)
{
std::cout << "Choosing glyph type from menu collection" << std::endl;
switch(static_cast<int>(glyph)){
case static_cast<int>(GlyphType::SIMPLE_GLYPH):
return new SimpleGlyph(description);
default:
return nullptr;
}
}

std::string MenuModel::printState()
{
std::string strRepresentation;
strRepresentation.append("Current document: ");
strRepresentation.append(m_docName);
strRepresentation.append("\n");
switch(static_cast<int>(m_state)){
case static_cast<int>(MenuModelState::DOC_CREATED):
strRepresentation.append("It was created here");
break;
case static_cast<int>(MenuModelState::DOC_IMPORTED):
strRepresentation.append("It was imported");
break;
case static_cast<int>(MenuModelState::DOC_EXPORTED):
strRepresentation.append("It was succesfully exported");
break;
}
strRepresentation.append("\n");
return strRepresentation;
}
62 changes: 62 additions & 0 deletions model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <functional>
#include <list>
#include <iostream>
#include <map>
#include "gliph.h"

enum class MenuModelState {
DOC_CREATED,
DOC_IMPORTED,
DOC_EXPORTED
};

class Model;
using Listener = std::function<void(Model*)>;

class Model {
public:
virtual ~Model();

void connect(Listener view) { m_listeners.emplace_back(view); }
virtual std::string printState() = 0;
protected:
void notify();
private:
std::list<Listener> m_listeners;
protected:
std::string m_docName;
};

class MenuModel: public Model {
public:
MenuModel() = default;
~MenuModel() {}
void createDocument(std::string& docName);
void importDocument(std::string& docName);
void exportDocument(std::string& docName);

Glyph* createGlyph(GlyphType glyph, std::string description);
virtual std::string printState();
private:
MenuModelState m_state;
};

class DrawModel: public Model {
public:
DrawModel() = default;
~DrawModel() {}
void drawGliph(Glyph* gliphName, int x, int y);
void deleteGliph(Glyph* gliphName);

virtual std::string printState();

private:
struct InternalData {
int x, y, number;
};

private:
std::map<Glyph*, InternalData> m_gliphStore;
static int m_counter;
};
11 changes: 11 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <string>
#include "model.h"


class View {
public:
View() = default;
void printStateOfDocument(Model* model) { std::cout << model->printState() << std::endl; }
};