Add Sir serializer and support selected type in overview

master
Marko Semet 2019-05-21 15:59:16 +02:00
parent 2f98e1f291
commit c61626d9fd
2 changed files with 249 additions and 11 deletions

View File

@ -0,0 +1,141 @@
#include <File.h>
#include <TypesOfType.h>
#include <sirEdit/main.hpp>
#include <sirEdit/data/serialize.hpp>
#include <sirEdit/utils/conver.hpp>
#include <unordered_map>
#include <tuple>
#include <functional>
using namespace sir::api;
using namespace sirEdit::data;
using namespace std;
//
// Serializer (read/write)
//
template<class SOURCE>
inline std::string _getName(SOURCE* type) {
std::string result;
for(int i = 0; i < type->getName()->getParts()->size(); i++)
result += type->getName()->getParts()->get(i).string->c_str();
return result;
}
template<class SOURCE>
inline std::unique_ptr<sirEdit::data::TypeWithFields> _loadFields(SOURCE* source) {
if(source == nullptr)
throw std::invalid_argument("Source is null pointer");
std::vector<sirEdit::data::Field> fields;
fields.resize(source->getFields()->size());
size_t counter = 0;
for(sir::FieldLike* i : *(source->getFields())) {
fields[counter] = std::move(sirEdit::data::Field(_getName(i), "", "")); // TODO: Add comments and type
counter++;
}
return std::move(std::make_unique<sirEdit::data::TypeWithFields>(_getName(source), "", fields)); // TODO: Add comments
}
inline sirEdit::data::Type* genBaseType(sir::UserdefinedType& uf) {
std::string skillType = uf.skillName();
sirEdit::data::Type* result;
if(skillType == sir::ClassType::typeName) {
std::unique_ptr<sirEdit::data::TypeWithFields> fields = std::move(_loadFields(static_cast<sir::ClassType*>(&uf)));
result = new TypeClass(*(fields.get()), std::vector<sirEdit::data::TypeInterface*>(), nullptr);
}
else if(skillType == sir::InterfaceType::typeName) {
std::unique_ptr<sirEdit::data::TypeWithFields> fields = std::move(_loadFields(static_cast<sir::InterfaceType*>(&uf)));
result = new TypeInterface(*(fields.get()), std::vector<sirEdit::data::TypeInterface*>(), nullptr);
}
else
throw std::invalid_argument(std::string("Unknown skill class type ") + skillType);
return result;
}
namespace {
class SerializerSIR : public Serializer
{
private:
SkillFile* sf;
unordered_map<Type*, sir::Type*> types;
unordered_map<sir::Type*, Type*> typesInverse;
unordered_map<Tool*, sir::Tool*> tools;
template<class TYPE, class SOURCE>
void addSuper(const SOURCE& source) {
for(auto& i : source) {
if(i.getSuper() == nullptr)
static_cast<TYPE*>(typesInverse[&i])->getSuper() = nullptr;
else {
auto tmp = this->typesInverse.find(i.getSuper());
if(tmp == this->typesInverse.end())
throw; // TODO: Exception that should never happen
static_cast<TYPE*>(typesInverse[&i])->getSuper() = tmp->second;
}
}
}
public:
SerializerSIR(std::string path) {
// Open file
this->sf = SkillFile::open(path);
if(this->sf == nullptr)
throw; // TODO: Exception
// Read types
for(auto& i : this->sf->UserdefinedType->all()) {
Type* tmpType = genBaseType(i);
this->types[tmpType] = &i;
this->typesInverse[&i] = tmpType;
}
// Gen super
this->addSuper<TypeClass>(this->sf->ClassType->all());
this->addSuper<TypeInterface>(this->sf->InterfaceType->all());
// Read tools
for(auto& i : this->sf->Tool->all()) {
Tool* tmpTool = new Tool();
// TODO: Comment
// TODO: Build command
tmpTool->getName() = std::move(std::string(i.getName()->begin(), i.getName()->end()));
//for(auto& j : i.getSelectedFields()) {
// j.getData();
// Type* selectedType = this->typesInverse[j];
//}
this->tools[tmpTool] = &i;
}
// Run general updates
this->updateRelationships();
}
~SerializerSIR() {
delete this->sf;
}
void addBaseType(Type* type) {
throw; // TODO:
}
void addBaseTool(Tool* tool) {
auto tmp = this->sf->Tool->add();
this->tools[tool] = tmp;
}
void getBaseTypes(std::function<void(Type*)> callbackFunc) {
for(auto& i : this->types)
callbackFunc(i.first);
}
void getBaseTools(std::function<void(Tool*)> callbackFunc) {
for(auto& i : this->tools)
callbackFunc(i.first);
}
void save() {
throw; // TODO:
}
};
}
namespace sirEdit::data {
std::unique_ptr<Serializer> getSir(std::string path) {
return std::move(std::make_unique<SerializerSIR>(path));
}
}

View File

@ -47,13 +47,13 @@ class FieldModel : public Gtk::TreeModel::ColumnRecord
Gtk::TreeModelColumn<Glib::ustring> data_name;
Gtk::TreeModelColumn<bool> data_used;
Gtk::TreeModelColumn<bool> data_active;
Gtk::TreeModelColumn<Field*> data_type;
Gtk::TreeModelColumn<Field*> data_field;
FieldModel() {
this->add(data_used);
this->add(data_active);
this->add(data_name);
this->add(data_type);
this->add(data_field);
}
};
static FieldModel fieldModel;
@ -96,8 +96,26 @@ class Overview : public Gtk::VBox {
std::unordered_set<const Type*> __cache_selected_type;
std::unordered_set<const Tool*> __cache_selected_tools;
std::unordered_set<const Tool*> __cache_used_tools;
std::unordered_set<const Field*> __cache_used_field;
std::unordered_set<const Field*> __cache_selected_fields;
const Type* __current_type;
std::unordered_set<const Type*> getActiveTypes() {
std::unordered_set<const Type*> result;
for(auto& i : this->type_view.get_selection()->get_selected_rows()) {
const Type* tmp = (*(this->type_store->get_iter(i)))[typeModel.data_type];
result.insert(tmp);
}
return std::move(result);
}
std::unordered_set<const Field*> getActiveFields() {
std::unordered_set<const Field*> result;
for(auto& i : this->field_view.get_selection()->get_selected_rows()) {
const Field* tmp = (*(this->field_store->get_iter(i)))[fieldModel.data_field];
result.insert(tmp);
}
return std::move(result);
}
void genCacheTools() {
this->__cache_selected_tools = {};
for(auto& i : this->tool_view.get_selection()->get_selected_rows()) {
@ -106,11 +124,10 @@ class Overview : public Gtk::VBox {
}
}
void genCacheTypes() {
this->__cache_selected_type = {};
for(auto& i : this->type_view.get_selection()->get_selected_rows()) {
const Type* tmp = (*(this->type_store->get_iter(i)))[typeModel.data_type];
this->__cache_selected_type.insert(tmp);
}
this->__cache_selected_type = std::move(this->getActiveTypes());
}
void genCacheFields() {
this->__cache_selected_fields = std::move(this->getActiveFields());
}
//
@ -144,7 +161,25 @@ class Overview : public Gtk::VBox {
if(!found)
return false;
}
// TODO: Check field marked
// Check field
if(this->__cache_selected_fields.size() != 0) {
bool found = false;
for(auto& i : this->__cache_selected_fields) {
auto tmp = tool->getStatesFields().find(i);
if(tmp != tool->getStatesFields().end()) {
for(auto& j : tmp->second)
if(j.second >= FIELD_STATE::READ) {
found = true;
break;
}
if(found)
break;
}
}
if(!found)
return false;
}
return true;
}
bool isTypeActive(const Type* type) {
@ -173,7 +208,22 @@ class Overview : public Gtk::VBox {
if(!found)
return false;
}
// TODO: Check fields
// Check field
if(this->__cache_selected_fields.size() != 0) {
bool found = false;
for(auto& i : this->__cache_selected_fields) {
for(auto& j : this->transactions.getData().getTools())
if(j->getFieldSetState(*i, *type) >= FIELD_STATE::READ) {
found = true;
break;
}
if(found)
break;
}
if(!found)
return false;
}
return true;
}
@ -313,7 +363,17 @@ class Overview : public Gtk::VBox {
this->update_all();
}
void toggle_field_used(const Glib::ustring& index) {
// TODO: Toogle used
auto tmp = *(this->field_store->get_iter(index));
auto tmpIter = this->__cache_used_field.find(static_cast<const Field*>(tmp[fieldModel.data_field]));
if(tmpIter == this->__cache_used_field.end()) {
this->__cache_used_field.insert(static_cast<const Field*>(tmp[fieldModel.data_field]));
tmp[fieldModel.data_used] = true;
}
else {
this->__cache_used_field.erase(tmpIter);
tmp[fieldModel.data_used] = false;
}
this->update_all();
}
void update_all() {
@ -321,8 +381,33 @@ class Overview : public Gtk::VBox {
return;
blockChange = true;
// Get last selected
{
std::unordered_set<const Type*> tmp = std::move(this->getActiveTypes());
bool found = false;
const Type* foundType = nullptr;
for(auto& i : tmp) {
auto tmp2 = this->__cache_selected_type.find(i);
if(tmp2 == this->__cache_selected_type.end()) {
if(found) {
found = false;
break;
}
else {
found = true;
foundType = *i;
}
}
}
if(found)
this->__current_type = foundType;
this->__cache_selected_type = std::move(tmp);
}
// Update views
this->genCacheTools();
this->genCacheTypes();
//this->genCacheTypes();
this->genCacheFields();
this->updateToolData();
this->updateTypeData();
this->updateFieldData();
@ -398,14 +483,26 @@ class Overview : public Gtk::VBox {
this->toggle_type_used(index);
});
this->type_view.get_selection()->signal_changed().connect([this]() -> void {
//if(!this->blockChange) {
// auto tmp = this->type_view.get_selection()->get_selected_rows();
// if(tmp.size() > 0)
// this->changeCorrentType((*(this->type_store->get_iter(tmp[tmp.size() - 1])))[typeModel.data_type]);
//}
this->update_all();
});
//this->type_view.set_activate_on_single_click(true);
//this->type_view.signal_row_activated().connect([this](const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) -> void {
// this->changeCorrentType((*(this->type_store->get_iter(path)))[typeModel.data_type]);
//});
this->field_store = Gtk::TreeStore::create(fieldModel);
this->field_view.set_model(this->field_store);
genTreeView(this->field_view, fieldModel, [this](Glib::ustring index) -> void {
this->toggle_field_used(index);
});
this->field_view.get_selection()->signal_changed().connect([this]() -> void {
this->update_all();
});
// Content
this->update_all();