logic: code controller communication
- Replaced CodeAccess with LocationAccess. - Implemented basic functionality for CodeController. - Added messaging to the project. - Added QtThreadedFunctor to enable our Messaging thread to invoke calls on the QtThread. - Added GraphController. fortune cookie message = Good news from someone dear is coming soon.
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
#include "component/Component.h"
|
||||
|
||||
#include "component/view/View.h"
|
||||
#include "component/controller/Controller.h"
|
||||
|
||||
Component::Component(std::shared_ptr<View> view, std::shared_ptr<Controller> controller)
|
||||
: m_controller(controller)
|
||||
, m_view(view)
|
||||
{
|
||||
m_controller->setComponent(this);
|
||||
m_view->setComponent(this);
|
||||
}
|
||||
|
||||
Component::~Component()
|
||||
{
|
||||
}
|
||||
|
||||
Controller* Component::getController() const
|
||||
{
|
||||
return m_controller.get();
|
||||
}
|
||||
|
||||
View* Component::getView() const
|
||||
{
|
||||
return m_view.get();
|
||||
m_controller->setComponent(NULL);
|
||||
m_view->setComponent(NULL);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "component/controller/Controller.h"
|
||||
#include "component/view/View.h"
|
||||
class View;
|
||||
class Controller;
|
||||
|
||||
class Component
|
||||
{
|
||||
@@ -12,8 +12,11 @@ public:
|
||||
Component(std::shared_ptr<View> view, std::shared_ptr<Controller> controller);
|
||||
~Component();
|
||||
|
||||
Controller* getController() const;
|
||||
View* getView() const;
|
||||
template <typename ControllerType>
|
||||
ControllerType* getController() const;
|
||||
|
||||
template <typename ViewType>
|
||||
ViewType* getView() const;
|
||||
|
||||
private:
|
||||
const std::shared_ptr<Controller> m_controller;
|
||||
@@ -21,4 +24,17 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template <typename ControllerType>
|
||||
ControllerType* Component::getController() const
|
||||
{
|
||||
return dynamic_cast<ControllerType*>(m_controller.get());
|
||||
}
|
||||
|
||||
template <typename ViewType>
|
||||
ViewType* Component::getView() const
|
||||
{
|
||||
return dynamic_cast<ViewType*>(m_view.get());
|
||||
}
|
||||
|
||||
|
||||
#endif // COMPONENT_H
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "component/ComponentFactory.h"
|
||||
|
||||
#include "component/Component.h"
|
||||
#include "component/controller/CodeController.h"
|
||||
#include "component/controller/GraphController.h"
|
||||
#include "component/view/CodeView.h"
|
||||
#include "component/view/GraphView.h"
|
||||
#include "component/view/ViewLayout.h"
|
||||
@@ -9,13 +11,13 @@
|
||||
std::shared_ptr<ComponentFactory> ComponentFactory::create(
|
||||
GuiFactory* guiFactory,
|
||||
ViewLayout* viewLayout,
|
||||
std::shared_ptr<CodeAccess> codeAccess,
|
||||
std::shared_ptr<LocationAccess> locationAccess,
|
||||
std::shared_ptr<GraphAccess> graphAccess)
|
||||
{
|
||||
std::shared_ptr<ComponentFactory> ptr(new ComponentFactory());
|
||||
ptr->m_guiFactory = guiFactory;
|
||||
ptr->m_viewLayout = viewLayout;
|
||||
ptr->m_codeAccess = codeAccess;
|
||||
ptr->m_locationAccess = locationAccess;
|
||||
ptr->m_graphAccess = graphAccess;
|
||||
return ptr;
|
||||
}
|
||||
@@ -26,8 +28,8 @@ ComponentFactory::~ComponentFactory()
|
||||
|
||||
std::shared_ptr<Component> ComponentFactory::createCodeComponent()
|
||||
{
|
||||
std::shared_ptr<View> view = m_guiFactory->createCodeView(m_viewLayout);
|
||||
std::shared_ptr<Controller> controller = std::make_shared<CodeController>();
|
||||
std::shared_ptr<CodeView> view = m_guiFactory->createCodeView(m_viewLayout);
|
||||
std::shared_ptr<CodeController> controller = std::make_shared<CodeController>(m_locationAccess);
|
||||
|
||||
std::shared_ptr<Component> component = std::make_shared<Component>(view, controller);
|
||||
return component;
|
||||
@@ -36,7 +38,7 @@ std::shared_ptr<Component> ComponentFactory::createCodeComponent()
|
||||
std::shared_ptr<Component> ComponentFactory::createGraphComponent()
|
||||
{
|
||||
std::shared_ptr<View> view = m_guiFactory->createGraphView(m_viewLayout);
|
||||
std::shared_ptr<Controller> controller = std::make_shared<CodeController>();
|
||||
std::shared_ptr<GraphController> controller = std::make_shared<GraphController>(m_graphAccess);
|
||||
|
||||
std::shared_ptr<Component> component = std::make_shared<Component>(view, controller);
|
||||
return component;
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "component/Component.h"
|
||||
#include "data/access/CodeAccess.h"
|
||||
#include "data/access/LocationAccess.h"
|
||||
#include "data/access/GraphAccess.h"
|
||||
|
||||
class Component;
|
||||
class GuiFactory;
|
||||
class ViewLayout;
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
static std::shared_ptr<ComponentFactory> create(
|
||||
GuiFactory* guiFactory,
|
||||
ViewLayout* viewLayout,
|
||||
std::shared_ptr<CodeAccess> codeAccess,
|
||||
std::shared_ptr<LocationAccess> locationAccess,
|
||||
std::shared_ptr<GraphAccess> graphAccess);
|
||||
|
||||
~ComponentFactory();
|
||||
@@ -31,7 +31,7 @@ private:
|
||||
GuiFactory* m_guiFactory;
|
||||
ViewLayout* m_viewLayout;
|
||||
|
||||
std::shared_ptr<CodeAccess> m_codeAccess;
|
||||
std::shared_ptr<LocationAccess> m_locationAccess;
|
||||
std::shared_ptr<GraphAccess> m_graphAccess;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,11 +7,12 @@
|
||||
std::shared_ptr<ComponentManager> ComponentManager::create(
|
||||
GuiFactory* guiFactory,
|
||||
ViewLayout* viewLayout,
|
||||
std::shared_ptr<CodeAccess> codeAccess,
|
||||
std::shared_ptr<LocationAccess> locationAccess,
|
||||
std::shared_ptr<GraphAccess> graphAccess)
|
||||
{
|
||||
std::shared_ptr<ComponentManager> ptr(new ComponentManager());
|
||||
ptr->m_componentFactory = ComponentFactory::create(guiFactory, viewLayout, codeAccess, graphAccess);
|
||||
ptr->m_componentFactory = ComponentFactory::create(guiFactory, viewLayout, locationAccess, graphAccess);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
@@ -22,7 +23,7 @@ ComponentManager::~ComponentManager()
|
||||
void ComponentManager::setup()
|
||||
{
|
||||
std::shared_ptr<Component> graphComponent = m_componentFactory->createGraphComponent();
|
||||
GraphView* graphView = dynamic_cast<GraphView*>(graphComponent->getView());
|
||||
GraphView* graphView = graphComponent->getView<GraphView>();
|
||||
graphView->addNode(Vec2i(-100, -100), "foo");
|
||||
graphView->addNode(Vec2i(50, 50), "bar");
|
||||
|
||||
@@ -31,7 +32,7 @@ void ComponentManager::setup()
|
||||
std::shared_ptr<Component> codeComponent = m_componentFactory->createCodeComponent();
|
||||
m_components.push_back(codeComponent);
|
||||
|
||||
CodeView* codeView = dynamic_cast<CodeView*>(codeComponent->getView());
|
||||
CodeView* codeView = codeComponent->getView<CodeView>();
|
||||
codeView->addCodeSnippet("class HelloWorld;");
|
||||
codeView->addCodeSnippet("static int n = 42; // the answer.");
|
||||
// codeView->clearCodeSnippets();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "component/Component.h"
|
||||
#include "component/ComponentFactory.h"
|
||||
#include "data/access/CodeAccess.h"
|
||||
#include "data/access/LocationAccess.h"
|
||||
#include "data/access/GraphAccess.h"
|
||||
|
||||
class GuiFactory;
|
||||
@@ -18,7 +18,7 @@ public:
|
||||
static std::shared_ptr<ComponentManager> create(
|
||||
GuiFactory* guiFactory,
|
||||
ViewLayout* viewLayout,
|
||||
std::shared_ptr<CodeAccess> codeAccess,
|
||||
std::shared_ptr<LocationAccess> locationAccess,
|
||||
std::shared_ptr<GraphAccess> graphAccess);
|
||||
|
||||
~ComponentManager();
|
||||
@@ -34,5 +34,4 @@ private:
|
||||
std::vector<std::shared_ptr<Component> > m_components;
|
||||
};
|
||||
|
||||
|
||||
#endif // COMPONENT_MANAGER_H
|
||||
|
||||
@@ -1 +1,39 @@
|
||||
#include "component/controller/CodeController.h"
|
||||
|
||||
#include "component/view/CodeView.h"
|
||||
#include "data/access/LocationAccess.h"
|
||||
#include "data/location/TokenLocation.h"
|
||||
#include "utility/text/TextAccess.h"
|
||||
|
||||
#include "utility/logging/logging.h"
|
||||
|
||||
|
||||
CodeController::CodeController(std::shared_ptr<LocationAccess> locationAccess)
|
||||
: m_locationAccess(locationAccess)
|
||||
{
|
||||
}
|
||||
|
||||
CodeController::~CodeController()
|
||||
{
|
||||
}
|
||||
|
||||
void CodeController::setActiveTokenLocationId(Id id)
|
||||
{
|
||||
TokenLocation* tokenLocation = m_locationAccess->getTokenLocation(id);
|
||||
if (tokenLocation)
|
||||
{
|
||||
getView()->clearCodeSnippets();
|
||||
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(tokenLocation->getFilePath());
|
||||
getView()->addCodeSnippet(textAccess->getText());
|
||||
}
|
||||
}
|
||||
|
||||
void CodeController::handleMessage(MessageActivateTokenLocation* message)
|
||||
{
|
||||
setActiveTokenLocationId(1);
|
||||
}
|
||||
|
||||
CodeView* CodeController::getView()
|
||||
{
|
||||
return Controller::getView<CodeView>();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,35 @@
|
||||
#ifndef CODE_CONTROLLER_H
|
||||
#define CODE_CONTROLLER_H
|
||||
|
||||
#include "component/controller/Controller.h"
|
||||
#include <string>
|
||||
|
||||
class CodeController: public Controller
|
||||
#include "component/controller/Controller.h"
|
||||
#include "utility/messaging/MessageListener.h"
|
||||
#include "utility/messaging/type/MessageActivateTokenLocation.h"
|
||||
#include "utility/types.h"
|
||||
|
||||
class CodeView;
|
||||
class LocationAccess;
|
||||
|
||||
struct AnnotatedText
|
||||
{
|
||||
std::string text;
|
||||
Id tokenId;
|
||||
};
|
||||
|
||||
class CodeController: public Controller, public MessageListener<MessageActivateTokenLocation>
|
||||
{
|
||||
public:
|
||||
CodeController(std::shared_ptr<LocationAccess> locationAccess);
|
||||
~CodeController();
|
||||
|
||||
void setActiveTokenLocationId(Id id);
|
||||
|
||||
private:
|
||||
virtual void handleMessage(MessageActivateTokenLocation* message);
|
||||
CodeView* getView();
|
||||
|
||||
std::shared_ptr<LocationAccess> m_locationAccess;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
#include "component/controller/Controller.h"
|
||||
|
||||
Controller::Controller()
|
||||
{
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
{
|
||||
}
|
||||
|
||||
void Controller::setComponent(Component* component)
|
||||
{
|
||||
m_component = component;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H
|
||||
|
||||
#include "component/Component.h"
|
||||
|
||||
class Controller
|
||||
{
|
||||
public:
|
||||
Controller();
|
||||
virtual ~Controller();
|
||||
|
||||
void setComponent(Component* component);
|
||||
|
||||
protected:
|
||||
template <typename ViewType>
|
||||
ViewType* getView();
|
||||
|
||||
private:
|
||||
Component* m_component;
|
||||
|
||||
};
|
||||
|
||||
|
||||
template <typename ViewType>
|
||||
ViewType* Controller::getView()
|
||||
{
|
||||
if (m_component)
|
||||
return m_component->getView<ViewType>();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // CONTROLLER_H
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "component/controller/GraphController.h"
|
||||
|
||||
#include "component/view/GraphView.h"
|
||||
#include "data/access/GraphAccess.h"
|
||||
|
||||
#include "utility/logging/logging.h"
|
||||
|
||||
|
||||
GraphController::GraphController(std::shared_ptr<GraphAccess> graphAccess)
|
||||
: m_graphAccess(graphAccess)
|
||||
{
|
||||
}
|
||||
|
||||
GraphController::~GraphController()
|
||||
{
|
||||
}
|
||||
|
||||
GraphView* GraphController::getView()
|
||||
{
|
||||
return Controller::getView<GraphView>();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef GRAPH_CONTROLLER_H
|
||||
#define GRAPH_CONTROLLER_H
|
||||
|
||||
#include "component/controller/Controller.h"
|
||||
|
||||
class GraphView;
|
||||
class GraphAccess;
|
||||
|
||||
class GraphController: public Controller
|
||||
{
|
||||
public:
|
||||
GraphController(std::shared_ptr<GraphAccess> graphAccess);
|
||||
~GraphController();
|
||||
|
||||
private:
|
||||
GraphView* getView();
|
||||
|
||||
std::shared_ptr<GraphAccess> m_graphAccess;
|
||||
};
|
||||
|
||||
|
||||
#endif // GRAPH_CONTROLLER_H
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "component/view/CodeView.h"
|
||||
|
||||
#include "component/controller/CodeController.h"
|
||||
|
||||
CodeView::CodeView(ViewLayout* viewLayout)
|
||||
: View(viewLayout, Vec2i(100, 100))
|
||||
{
|
||||
@@ -13,3 +15,8 @@ std::string CodeView::getName() const
|
||||
{
|
||||
return "CodeView";
|
||||
}
|
||||
|
||||
CodeController* CodeView::getController()
|
||||
{
|
||||
return View::getController<CodeController>();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "component/view/View.h"
|
||||
|
||||
class CodeController;
|
||||
|
||||
class CodeView: public View
|
||||
{
|
||||
public:
|
||||
@@ -13,6 +15,9 @@ public:
|
||||
|
||||
virtual void addCodeSnippet(std::string str) = 0;
|
||||
virtual void clearCodeSnippets() = 0;
|
||||
|
||||
private:
|
||||
CodeController* getController();
|
||||
};
|
||||
|
||||
#endif // CODE_VIEW_H
|
||||
|
||||
@@ -18,6 +18,11 @@ GuiWidgetWrapper* View::getWidgetWrapper()
|
||||
return m_widgetWrapper.get();
|
||||
}
|
||||
|
||||
void View::setComponent(Component* component)
|
||||
{
|
||||
m_component = component;
|
||||
}
|
||||
|
||||
int View::getMinWidth() const
|
||||
{
|
||||
return m_minSize.x;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
class GuiWidgetWrapper;
|
||||
class ViewLayout;
|
||||
class Component;
|
||||
|
||||
class View
|
||||
{
|
||||
@@ -26,14 +27,20 @@ public:
|
||||
|
||||
virtual void initGui() = 0;
|
||||
|
||||
void setComponent(Component* component);
|
||||
|
||||
int getMinWidth() const;
|
||||
int getMinHeight() const;
|
||||
Vec2i getMinSize() const;
|
||||
|
||||
protected:
|
||||
View(ViewLayout* viewLayout, const Vec2i& minSize);
|
||||
View(ViewLayout* viewLayout, const Vec2i& minSize); // make public
|
||||
|
||||
template <typename ControllerType>
|
||||
ControllerType* getController();
|
||||
|
||||
private:
|
||||
Component* m_component;
|
||||
ViewLayout* const m_viewLayout;
|
||||
std::shared_ptr<GuiWidgetWrapper> m_widgetWrapper;
|
||||
Vec2i m_minSize;
|
||||
@@ -52,4 +59,14 @@ std::shared_ptr<T> View::create(ViewLayout* viewLayout)
|
||||
return ptr;
|
||||
}
|
||||
|
||||
template <typename ControllerType>
|
||||
ControllerType* View::getController()
|
||||
{
|
||||
if (m_component)
|
||||
{
|
||||
return m_component->getController<ControllerType>();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // VIEW_H
|
||||
|
||||
Reference in New Issue
Block a user