Skip to content

Commit

Permalink
all entities can be added via commmands
Browse files Browse the repository at this point in the history
  • Loading branch information
vt4a2h committed Jun 24, 2015
1 parent c17f2c2 commit e71ee5e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
19 changes: 13 additions & 6 deletions commands/createentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,32 @@ class QGraphicsScene;

namespace commands {

const QMap<size_t, QString> hashName = {
{ entity::Type::staticHashType(), BaseCommand::tr("type") },
{ entity::Class::staticHashType(), BaseCommand::tr("class (struct)") },
{ entity::Enum::staticHashType(), BaseCommand::tr("enum") },
{ entity::Union::staticHashType(), BaseCommand::tr("union") },
{ entity::ExtendedType::staticHashType(), BaseCommand::tr("type alias") },
{ entity::TemplateClass::staticHashType(), BaseCommand::tr("template class") },
};

/// The class CreateEntity
template<class Type>
class CreateEntity : public BaseCommand
{
public:
CreateEntity(const models::SharedApplicationModel &model, const QString &scopeID,
QGraphicsScene &scene, const QPointF &pos, QUndoCommand *parent = nullptr)
: BaseCommand(tr("Add type"), parent)
: BaseCommand(tr("Add %1").arg(hashName[typeid(Type).hash_code()]), parent)
, m_Model(model)
, m_ProjectID(model && model->currentProject() ? model->currentProject()->id() : STUB_ID)
, m_ScopeID(scopeID)
, m_TypeItem(nullptr)
, m_Pos(pos)
, m_Item(nullptr)
, m_Scene(scene)
, m_TypeItem(entity::EntitiesFactory::get().makeEntity<Type>(m_Model, m_ScopeID, m_Scene, m_Pos))
{
Q_ASSERT(m_TypeItem);
}

void redo() override
Expand All @@ -76,10 +86,7 @@ namespace commands {
m_Scene.addItem(m_Item);
static_cast<graphics::Entity *>(m_Item)->setTypeObject(m_TypeItem);
} else {
auto&& factory = entity::EntitiesFactory::get();
m_TypeItem = factory.makeEntity<Type>(m_Model, m_ScopeID, m_Scene, m_Pos);
m_Item = m_Scene.itemAt(m_Pos, QTransform());

m_Done = true;
}
}
Expand All @@ -99,10 +106,10 @@ namespace commands {
models::SharedApplicationModel m_Model;
QString m_ProjectID;
QString m_ScopeID;
std::shared_ptr<Type> m_TypeItem;
QPointF m_Pos;
QGraphicsItem * m_Item;
QGraphicsScene & m_Scene;
std::shared_ptr<Type> m_TypeItem;
};

using MakeClass = CreateEntity<entity::Class>;
Expand Down
20 changes: 19 additions & 1 deletion gui/graphics/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#include <gui/editentitydialog.h>

#include <entity/type.h>
#include <entity/enum.h>
#include <entity/union.h>
#include <entity/class.h>
#include <entity/templateclass.h>
#include <entity/extendedtype.h>
#include <entity/scope.h>

#include <constants.h>
Expand All @@ -42,6 +47,19 @@ namespace graphics {
constexpr double tmpHeight = 100.;
constexpr double tmpWidth = 100.;
const QString stub = Entity::tr( "Stub" );

// NOTE: temporary, maybe will changed to smth else
const QMap<size_t, std::function<QColor(const entity::SharedType &)>> entitiesColor = {
{ entity::Enum::staticHashType() , [](const entity::SharedType & ){ return Qt::green; } },
{ entity::Union::staticHashType(), [](const entity::SharedType & ){ return Qt::yellow; } },
{ entity::Class::staticHashType(), [](const entity::SharedType &t){
auto c = std::static_pointer_cast<entity::Class>(t);
return c->kind() == entity::Kind::ClassType ? Qt::blue : Qt::darkBlue;
} },
{ entity::Type::staticHashType() , [](const entity::SharedType &){ return Qt::red; } },
{ entity::TemplateClass::staticHashType(), [](const entity::SharedType &){ return Qt::gray; } },
{ entity::ExtendedType::staticHashType() , [](const entity::SharedType &){ return Qt::darkCyan; } },
};
}

/**
Expand Down Expand Up @@ -88,7 +106,7 @@ namespace graphics {
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setBrush(Qt::black);
painter->setBrush(entitiesColor[m_Type->hashType()](m_Type));
painter->drawRect(QRectF(-tmpWidth / 2, -tmpHeight / 2, tmpWidth, tmpHeight));
painter->setPen(Qt::white);
painter->drawText(boundingRect(), Qt::AlignCenter, m_Type ? m_Type->name() : stub);
Expand Down
1 change: 1 addition & 0 deletions gui/main.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<file>pic/icon_component_edit.png</file>
<file>pic/icon_component_delete.png</file>
<file>pic/icon_component_plus.png</file>
<file>pic/icon_action_enum.png</file>
</qresource>
<qresource prefix="/img">
<file>pic/main.png</file>
Expand Down
1 change: 1 addition & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ namespace gui {
ui->actionAddStruct->setEnabled( state );
ui->actionAddTemplate->setEnabled( state );
ui->actionAddUnion->setEnabled( state );
ui->actionAddEnum->setEnabled( state );
ui->actionCreateScope->setEnabled( state );
ui->actionMakeRelation->setEnabled( state );
ui->actionSaveProject->setEnabled(state && !m_ApplicationModel->currentProject()->isSaved() );
Expand Down
4 changes: 4 additions & 0 deletions gui/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="main.qrc">
<normaloff>:/icons/pic/icon_action_enum.png</normaloff>:/icons/pic/icon_action_enum.png</iconset>
</property>
<property name="text">
<string>actionAddEnum</string>
</property>
Expand Down
Binary file added gui/pic/icon_action_enum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e71ee5e

Please sign in to comment.