ui: Abstracting the UI on view level instead of element level

This change switches the view creation architecture from element based creation in the lib to view based creation in the
app. This avoids wrapping of the Qt elements and instead defines interfaces for the different View implementations.

* GuiElementFactory was renamed to GuiFactory and is now responsible for View creation instead of Element creation.
* WidgetWrappers are now used on Views to access their root widget.
* Removed DummyView and DummyController.
* Removed GuiElement and corresponding QtElement files.
* Removed ViewManager, functionality is now handled via MainView.
* Added MainView, where Views are registered via the ViewLayout interface.
* Added QtMainWindow, derived from QMainWindow and instantiated by QtMainView, which displays the Views as QDockWidgets.

fortune cookie message = All your hard work will soon pay off.
This commit is contained in:
Eberhard Graether
2014-07-02 12:25:51 +02:00
parent 3dabe009e0
commit fdba1b2a84
67 changed files with 424 additions and 888 deletions
+8 -13
View File
@@ -1,28 +1,23 @@
add_files(
APP_FILES
qt/element/QtArea.cpp
qt/element/QtArea.h
qt/element/QtCanvas.cpp
qt/element/QtCanvas.h
qt/element/QtLayout.cpp
qt/element/QtLayout.h
qt/element/QtMainWindow.cpp
qt/element/QtMainWindow.h
qt/utility/QtHighLighter.cpp
qt/utility/QtHighLighter.h
qt/utility/utilityQt.cpp
qt/utility/utilityQt.h
qt/view/QtCodeView.cpp
qt/view/QtCodeView.h
qt/view/QtMainView.cpp
qt/view/QtMainView.h
qt/QtGuiFactory.cpp
qt/QtGuiFactory.h
qt/QtWidgetWrapper.cpp
qt/QtWidgetWrapper.h
qt/QtElementFactory.cpp
qt/QtElementFactory.h
qt/QtWindow.cpp
qt/QtWindow.h
utility/utilityQt.cpp
utility/utilityQt.h
includesWindows.h
main.cpp
+3 -6
View File
@@ -4,17 +4,14 @@
#include "Application.h"
#include "includes.h"
#include "qt/QtElementFactory.h"
#include "qt/QtWindow.h"
#include "qt/QtGuiFactory.h"
int main(int argc, char *argv[])
{
QApplication qtApp(argc, argv);
std::shared_ptr<GuiElementFactory> elementFactory = std::make_shared<QtElementFactory>();
QtGuiFactory guiFactory;
std::shared_ptr<QtWindow> window = std::make_shared<QtWindow>();
std::shared_ptr<Application> app = Application::create(elementFactory, window);
std::shared_ptr<Application> app = Application::create(&guiFactory);
app->loadProject();
return qtApp.exec();
-38
View File
@@ -1,38 +0,0 @@
#include "qt/QtElementFactory.h"
#include "component/view/CodeView.h"
#include "qt/element/QtArea.h"
#include "qt/element/QtCanvas.h"
#include "qt/element/QtLayout.h"
#include "qt/view/QtCodeView.h"
#include "qt/QtWindow.h"
QtElementFactory::QtElementFactory()
{
}
QtElementFactory::~QtElementFactory()
{
}
std::shared_ptr<GuiArea> QtElementFactory::createArea() const
{
return std::make_shared<QtArea>();
}
std::shared_ptr<GuiCanvas> QtElementFactory::createCanvas(std::shared_ptr<GuiWindow> window) const
{
std::shared_ptr<GuiCanvas> canvas = std::make_shared<QtCanvas>();
window->setCanvas(canvas);
return canvas;
}
std::shared_ptr<GuiLayout> QtElementFactory::createLayout() const
{
return std::make_shared<QtLayout>();
}
std::shared_ptr<CodeView> QtElementFactory::createCodeView(std::shared_ptr<ViewManager> viewManager) const
{
return std::make_shared<QtCodeView>(viewManager);
}
-22
View File
@@ -1,22 +0,0 @@
#ifndef QT_ELEMENT_FACTORY_H
#define QT_ELEMENT_FACTORY_H
#include "gui/GuiElementFactory.h"
class CodeView;
class ViewManager;
class QtElementFactory: public GuiElementFactory
{
public:
QtElementFactory();
virtual ~QtElementFactory();
virtual std::shared_ptr<GuiArea> createArea() const;
virtual std::shared_ptr<GuiCanvas> createCanvas(std::shared_ptr<GuiWindow> window) const;
virtual std::shared_ptr<GuiLayout> createLayout() const;
virtual std::shared_ptr<CodeView> createCodeView(std::shared_ptr<ViewManager> viewManager) const;
};
# endif // QT_ELEMENT_FACTORY_H
+22
View File
@@ -0,0 +1,22 @@
#include "qt/QtGuiFactory.h"
#include "qt/view/QtCodeView.h"
#include "qt/view/QtMainView.h"
QtGuiFactory::QtGuiFactory()
{
}
QtGuiFactory::~QtGuiFactory()
{
}
std::shared_ptr<MainView> QtGuiFactory::createMainView() const
{
return std::make_shared<QtMainView>();
}
std::shared_ptr<CodeView> QtGuiFactory::createCodeView(ViewLayout* viewLayout) const
{
return View::create<QtCodeView>(viewLayout);
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef QT_GUI_FACTORY_H
#define QT_GUI_FACTORY_H
#include "gui/GuiFactory.h"
class QtGuiFactory: public GuiFactory
{
public:
QtGuiFactory();
virtual ~QtGuiFactory();
virtual std::shared_ptr<MainView> createMainView() const;
virtual std::shared_ptr<CodeView> createCodeView(ViewLayout* viewLayout) const;
};
#endif // QT_GUI_FACTORY_H
+14 -15
View File
@@ -1,26 +1,25 @@
#include "qt/QtWidgetWrapper.h"
#include "gui/GuiElement.h"
#include "component/view/View.h"
#include "utility/logging/logging.h"
std::shared_ptr<QWidget> QtWidgetWrapper::getWidgetOfElement(std::shared_ptr<GuiElement> element)
QWidget* QtWidgetWrapper::getWidgetOfView(View* view)
{
return getWidgetOfElement(element.get());
}
QtWidgetWrapper* widgetWrapper = dynamic_cast<QtWidgetWrapper*>(view->getWidgetWrapper());
std::shared_ptr<QWidget> QtWidgetWrapper::getWidgetOfElement(GuiElement* element)
{
std::shared_ptr<QWidget> widget;
std::shared_ptr<QtWidgetWrapper> qtWidgetWrapper = std::dynamic_pointer_cast<QtWidgetWrapper>(element->getWidgetWrapper());
if (!qtWidgetWrapper || qtWidgetWrapper.use_count() == 0)
if (!widgetWrapper)
{
LOG_ERROR("Trying to get the qt widget of non qt gui element.");
LOG_ERROR("Trying to get the qt widget of non qt view.");
return nullptr;
}
else
if (!widgetWrapper->getWidget())
{
widget = qtWidgetWrapper->getWidget();
LOG_ERROR("The QtWidgetWrapper is not holdling a QWidget.");
return nullptr;
}
return widget;
return widgetWrapper->getWidget();
}
QtWidgetWrapper::QtWidgetWrapper(std::shared_ptr<QWidget> widget)
@@ -32,7 +31,7 @@ QtWidgetWrapper::~QtWidgetWrapper()
{
}
std::shared_ptr<QWidget> QtWidgetWrapper::getWidget()
QWidget* QtWidgetWrapper::getWidget()
{
return m_widget;
return m_widget.get();
}
+5 -6
View File
@@ -5,20 +5,19 @@
#include <QtWidgets/qwidget.h>
#include "gui/WidgetWrapper.h"
#include "gui/GuiWidgetWrapper.h"
class GuiElement;
class View;
class QtWidgetWrapper: public WidgetWrapper
class QtWidgetWrapper: public GuiWidgetWrapper
{
public:
static std::shared_ptr<QWidget> getWidgetOfElement(std::shared_ptr<GuiElement> element);
static std::shared_ptr<QWidget> getWidgetOfElement(GuiElement* element);
static QWidget* getWidgetOfView(View* view);
QtWidgetWrapper(std::shared_ptr<QWidget> widget);
virtual ~QtWidgetWrapper();
std::shared_ptr<QWidget> getWidget(); // not const because returned widget can be changed.
QWidget* getWidget();
private:
std::shared_ptr<QWidget> m_widget;
-19
View File
@@ -1,19 +0,0 @@
#include "qt/QtWindow.h"
#include "gui/GuiCanvas.h"
#include "QtWidgetWrapper.h"
QtWindow::QtWindow()
{
m_window.setObjectName("QtWindow");
m_window.show();
}
QtWindow::~QtWindow()
{
}
void QtWindow::setCanvas(std::shared_ptr<GuiCanvas> canvas)
{
m_window.setCentralWidget(QtWidgetWrapper::getWidgetOfElement(canvas).get());
}
-24
View File
@@ -1,24 +0,0 @@
#ifndef QT_WINDOW_H
#define QT_WINDOW_H
#include <memory>
#include <QtWidgets/QMainWindow>
#include "gui/GuiWindow.h"
class GuiCanvas;
class QtWindow: public GuiWindow
{
public:
QtWindow();
~QtWindow();
void setCanvas(std::shared_ptr<GuiCanvas> canvas);
private:
QMainWindow m_window;
};
# endif // QT_WINDOW_H
-43
View File
@@ -1,43 +0,0 @@
#include "qt/element/QtArea.h"
#include <qframe.h>
#include "qt/QtWidgetWrapper.h"
#include "utility/utilityQt.h"
QtArea::QtArea()
: GuiArea(std::make_shared<QtWidgetWrapper>(std::make_shared<QFrame>()))
{
}
QtArea::~QtArea()
{
}
void QtArea::setPosition(Vec2i position)
{
}
Vec2i QtArea::getPosition() const
{
return Vec2i();
}
void QtArea::setSize(Vec2i size)
{
}
Vec2i QtArea::getSize() const
{
return Vec2i();
}
void QtArea::setBackgroundColor(Colori color)
{
utility::setWidgetBackgroundColor(getWidget(), color);
}
std::shared_ptr<QWidget> QtArea::getWidget()
{
return QtWidgetWrapper::getWidgetOfElement(this);
}
-25
View File
@@ -1,25 +0,0 @@
#ifndef QT_AREA_H
#define QT_AREA_H
#include <qwidget.h>
#include "gui/GuiArea.h"
class QtArea: public GuiArea
{
public:
QtArea();
virtual ~QtArea();
virtual void setPosition(Vec2i position);
virtual Vec2i getPosition() const;
virtual void setSize(Vec2i size);
virtual Vec2i getSize() const;
virtual void setBackgroundColor(Colori color);
private:
std::shared_ptr<QWidget> getWidget();
};
# endif // QT_AREA_H
-59
View File
@@ -1,59 +0,0 @@
#include "qt/element/QtCanvas.h"
#include <qgroupbox.h>
#include <qlayout.h>
#include "qt/QtWidgetWrapper.h"
#include "utility/utilityQt.h"
QtCanvas::QtCanvas()
: GuiCanvas(std::make_shared<QtWidgetWrapper>(std::make_shared<QGroupBox>()))
{
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom); // gets deleted by qt
getWidget()->setLayout(layout);
}
QtCanvas::~QtCanvas()
{
}
void QtCanvas::addChild(std::shared_ptr<GuiElement> element)
{
getWidget()->layout()->addWidget(QtWidgetWrapper::getWidgetOfElement(element).get());
}
void QtCanvas::removeChild(std::shared_ptr<GuiElement> element)
{
std::shared_ptr<QWidget> widgetToRemove = QtWidgetWrapper::getWidgetOfElement(element);
getWidget()->layout()->removeWidget(widgetToRemove.get());
widgetToRemove->setGeometry(0, 0, 0, 0);
}
void QtCanvas::setPosition(Vec2i position)
{
}
Vec2i QtCanvas::getPosition() const
{
return Vec2i();
}
void QtCanvas::setSize(Vec2i size)
{
}
Vec2i QtCanvas::getSize() const
{
return Vec2i();
}
void QtCanvas::setBackgroundColor(Colori color)
{
utility::setWidgetBackgroundColor(getWidget(), color);
}
std::shared_ptr<QWidget> QtCanvas::getWidget()
{
return QtWidgetWrapper::getWidgetOfElement(this);
}
-29
View File
@@ -1,29 +0,0 @@
#ifndef QT_CANVAS_H
#define QT_CANVAS_H
#include <qwidget.h>
#include <QtWidgets/QMainWindow>
#include "gui/GuiCanvas.h"
class QtCanvas: public GuiCanvas
{
public:
QtCanvas();
virtual ~QtCanvas();
virtual void addChild(std::shared_ptr<GuiElement> element);
virtual void removeChild(std::shared_ptr<GuiElement> element);
virtual void setPosition(Vec2i position);
virtual Vec2i getPosition() const;
virtual void setSize(Vec2i size);
virtual Vec2i getSize() const;
virtual void setBackgroundColor(Colori color);
private:
std::shared_ptr<QWidget> getWidget();
};
# endif // QT_CANVAS_H
-70
View File
@@ -1,70 +0,0 @@
#include "qt/element/QtLayout.h"
#include <qgroupbox.h>
#include <qlayout.h>
#include "qt/QtWidgetWrapper.h"
#include "utility/utilityQt.h"
QtLayout::QtLayout()
: GuiLayout(std::make_shared<QtWidgetWrapper>(std::make_shared<QGroupBox>()))
{
m_layout = new QBoxLayout(QBoxLayout::TopToBottom); // gets deleted by qt
getWidget()->setLayout(m_layout);
}
QtLayout::~QtLayout()
{
}
void QtLayout::setDirection(LayoutDirectionType direction)
{
switch (direction)
{
case LAYOUT_DIRECTION_HORIZONTAL:
m_layout->setDirection(QBoxLayout::LeftToRight);
case LAYOUT_DIRECTION_VERTICAL:
m_layout->setDirection(QBoxLayout::TopToBottom);
}
getWidget()->setLayout(m_layout);
}
void QtLayout::addChild(std::shared_ptr<GuiElement> element)
{
m_layout->addWidget(QtWidgetWrapper::getWidgetOfElement(element).get());
}
void QtLayout::removeChild(std::shared_ptr<GuiElement> element)
{
std::shared_ptr<QWidget> widgetToRemove = QtWidgetWrapper::getWidgetOfElement(element);
m_layout->removeWidget(widgetToRemove.get());
widgetToRemove->setGeometry(0, 0, 0, 0);
}
void QtLayout::setPosition(Vec2i position)
{
}
Vec2i QtLayout::getPosition() const
{
return Vec2i();
}
void QtLayout::setSize(Vec2i size)
{
}
Vec2i QtLayout::getSize() const
{
return Vec2i();
}
void QtLayout::setBackgroundColor(Colori color)
{
utility::setWidgetBackgroundColor(getWidget(), color);
}
std::shared_ptr<QWidget> QtLayout::getWidget()
{
return QtWidgetWrapper::getWidgetOfElement(this);
}
-33
View File
@@ -1,33 +0,0 @@
#ifndef QT_LAYOUT_H
#define QT_LAYOUT_H
#include <qwidget.h>
#include <qlayout.h>
#include "gui/GuiLayout.h"
class QtLayout: public GuiLayout
{
public:
QtLayout();
virtual ~QtLayout();
virtual void setDirection(LayoutDirectionType direction);
virtual void addChild(std::shared_ptr<GuiElement> element);
virtual void removeChild(std::shared_ptr<GuiElement> element);
virtual void setPosition(Vec2i position);
virtual Vec2i getPosition() const;
virtual void setSize(Vec2i size);
virtual Vec2i getSize() const;
virtual void setBackgroundColor(Colori color);
private:
std::shared_ptr<QWidget> getWidget();
QBoxLayout* m_layout;
};
# endif // QT_LAYOUT_H
+37
View File
@@ -0,0 +1,37 @@
#include "qt/element/QtMainWindow.h"
#include <QDockWidget>
#include "component/view/View.h"
#include "qt/QtWidgetWrapper.h"
QtMainWindow::QtMainWindow()
{
setObjectName("QtMainWindow");
setCentralWidget(nullptr);
}
QtMainWindow::~QtMainWindow()
{
}
void QtMainWindow::addView(View* view)
{
QDockWidget* dock = new QDockWidget(tr(view->getName().c_str()), this);
dock->setWidget(QtWidgetWrapper::getWidgetOfView(view));
addDockWidget(Qt::TopDockWidgetArea, dock);
m_dockWidgets.push_back(std::make_pair(view, dock));
}
void QtMainWindow::removeView(View* view)
{
for (size_t i = 0; i < m_dockWidgets.size(); i++)
{
if (m_dockWidgets[i].first == view)
{
removeDockWidget(m_dockWidgets[i].second);
m_dockWidgets.erase(m_dockWidgets.begin() + i);
return;
}
}
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef QT_MAIN_WINDOW_H
#define QT_MAIN_WINDOW_H
#include <utility>
#include <vector>
#include <QtWidgets/QMainWindow>
class QDockWidget;
class View;
class QtMainWindow: public QMainWindow
{
Q_OBJECT
public:
QtMainWindow();
~QtMainWindow();
void addView(View* view);
void removeView(View* view);
private:
std::vector<std::pair<View*, QDockWidget*> > m_dockWidgets;
};
#endif // QT_MAIN_WINDOW_H
@@ -2,7 +2,7 @@
namespace utility
{
void setWidgetBackgroundColor(std::shared_ptr<QWidget> widget, const Colori& color)
void setWidgetBackgroundColor(QWidget* widget, const Colori& color)
{
QPalette palette = widget->palette();
palette.setColor(widget->backgroundRole(), QColor(color.r, color.g, color.b, color.a));
@@ -10,4 +10,3 @@ namespace utility
widget->setAutoFillBackground(true);
}
}
@@ -8,7 +8,7 @@
namespace utility
{
void setWidgetBackgroundColor(std::shared_ptr<QWidget> widget, const Colori& color);
void setWidgetBackgroundColor(QWidget* widget, const Colori& color);
}
# endif // UTILITY_QT_H
+19 -10
View File
@@ -2,16 +2,29 @@
#include <QtWidgets>
#include "qt/element/QtArea.h"
#include "qt/QtWidgetWrapper.h"
#include "qt/utility/QtHighLighter.h"
#include "qt/utility/utilityQt.h"
QtCodeView::QtCodeView(std::shared_ptr<ViewManager> viewManager)
: CodeView(viewManager, std::make_shared<QtArea>())
QtCodeView::QtCodeView(ViewLayout* viewLayout)
: CodeView(viewLayout)
{
getRootElement()->setBackgroundColor(255, 125, 0, 255);
}
QtCodeView::~QtCodeView()
{
}
void QtCodeView::createWidgetWrapper()
{
setWidgetWrapper(std::make_shared<QtWidgetWrapper>(std::make_shared<QFrame>()));
}
void QtCodeView::initGui()
{
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
utility::setWidgetBackgroundColor(widget, Colori(255, 125, 0, 255));
std::shared_ptr<QWidget> widget = QtWidgetWrapper::getWidgetOfElement(getRootElement());
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom);
layout->setSpacing(3);
layout->setContentsMargins(3, 3, 3, 3);
@@ -22,10 +35,6 @@ QtCodeView::QtCodeView(std::shared_ptr<ViewManager> viewManager)
m_font.setPointSize(10);
}
QtCodeView::~QtCodeView()
{
}
void QtCodeView::addCodeSnippet(std::string str)
{
std::shared_ptr<Snippet> snippet = std::make_shared<Snippet>();
@@ -38,7 +47,7 @@ void QtCodeView::addCodeSnippet(std::string str)
snippet->textField->setPlainText(QString::fromUtf8(str.c_str()));
std::shared_ptr<QWidget> widget = QtWidgetWrapper::getWidgetOfElement(getRootElement());
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
widget->layout()->addWidget(snippet->textField.get());
m_snippets.push_back(snippet);
+5 -3
View File
@@ -3,7 +3,6 @@
#include <memory>
#include <vector>
#include <iostream>
#include <QFont>
@@ -11,14 +10,17 @@
class QtHighlighter;
class QTextEdit;
class ViewManager;
class QtCodeView: public CodeView
{
public:
QtCodeView(std::shared_ptr<ViewManager> viewManager);
QtCodeView(ViewLayout* viewLayout);
~QtCodeView();
// View implementation
virtual void createWidgetWrapper();
virtual void initGui();
// CodeView implementation
virtual void addCodeSnippet(std::string str);
virtual void clearCodeSnippets();
+33
View File
@@ -0,0 +1,33 @@
#include "qt/view/QtMainView.h"
#include "qt/element/QtMainWindow.h"
#include "utility/logging/logging.h"
QtMainView::QtMainView()
{
m_window = new QtMainWindow();
m_window->show();
}
QtMainView::~QtMainView()
{
}
void QtMainView::addView(View* view)
{
m_views.push_back(view);
m_window->addView(view);
}
void QtMainView::removeView(View* view)
{
std::vector<View*>::iterator it = std::find(m_views.begin(), m_views.end(), view);
if (it == m_views.end())
{
LOG_ERROR("View was not found.");
return;
}
m_window->removeView(view);
m_views.erase(it);
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef QT_MAIN_VIEW_H
#define QT_MAIN_VIEW_H
#include <memory>
#include <vector>
#include "component/view/MainView.h"
class QtMainWindow;
class View;
class QtMainView: public MainView
{
public:
QtMainView();
virtual ~QtMainView();
// ViewLayout implementation
void addView(View* view);
void removeView(View* view);
private:
QtMainWindow* m_window;
std::vector<View*> m_views;
};
#endif // QT_MAIN_VIEW_H