refactored classes to only use std or boost, not Qt, for easier

modularization.
This commit is contained in:
Marcel M. Otte 2016-11-08 21:52:46 +01:00
parent a47213104c
commit 160501b7ca
5 changed files with 95 additions and 51 deletions

View File

@ -20,9 +20,10 @@ EditorNode::~EditorNode()
const std::shared_ptr<EditorNode> EditorNode::nextSibling() const std::shared_ptr<EditorNode> EditorNode::nextSibling()
{ {
if (parent != nullptr) { if (parent != nullptr) {
int i = parent->getChildren().indexOf(std::shared_ptr<EditorNode>(this)); auto l = parent->getChildren();
if (i + 1 < parent->getChildren().size()) auto it = std::find(l.begin(), l.end(), std::shared_ptr<EditorNode>(this));
return parent->getChildren().at(i + 1); //next sibling... if (it != l.end() && ++it != l.end())
return *(++it); //next sibling...
else else
return nullptr; return nullptr;
} }
@ -41,7 +42,8 @@ const std::shared_ptr<EditorNode> EditorNode::next(TreeWalkStrategy walkStrategy
{ {
for (auto n : parent->getChildren()) for (auto n : parent->getChildren())
if (n->getChildren().size() > 0) if (n->getChildren().size() > 0)
return n->getChildren().first(); return n->getChildren().front();
//return n->getChildren().first();
auto siblingOfParent = parent->nextSibling(); auto siblingOfParent = parent->nextSibling();
while (siblingOfParent != nullptr && siblingOfParent->getChildren().size() == 0) { while (siblingOfParent != nullptr && siblingOfParent->getChildren().size() == 0) {
if (siblingOfParent->nextSibling() == nullptr) if (siblingOfParent->nextSibling() == nullptr)
@ -50,19 +52,22 @@ const std::shared_ptr<EditorNode> EditorNode::next(TreeWalkStrategy walkStrategy
siblingOfParent = siblingOfParent->nextSibling(); siblingOfParent = siblingOfParent->nextSibling();
} }
if (siblingOfParent != nullptr) if (siblingOfParent != nullptr)
return siblingOfParent->getChildren().first(); return siblingOfParent->getChildren().front();
//return siblingOfParent->getChildren().first();
else else
return nullptr; return nullptr;
} }
} else { } else {
if (getChildren().size() > 0) if (getChildren().size() > 0)
return getChildren().first(); return getChildren().front();
//return getChildren().first();
else else
return nullptr; return nullptr;
} }
else if (walkStrategy == TreeWalkStrategy::depthFirst) else if (walkStrategy == TreeWalkStrategy::depthFirst)
if (getChildren().size() > 0) if (getChildren().size() > 0)
return getChildren().first(); return getChildren().front();
//return getChildren().first();
else { else {
auto node = std::shared_ptr<EditorNode>(this); auto node = std::shared_ptr<EditorNode>(this);
while (node->nextSibling() == nullptr && node->getParent() != nullptr) while (node->nextSibling() == nullptr && node->getParent() != nullptr)
@ -75,7 +80,8 @@ const std::shared_ptr<EditorNode> EditorNode::next(TreeWalkStrategy walkStrategy
if (nextOnLvl == nullptr){ if (nextOnLvl == nullptr){
for (auto n : this->getChildren()) for (auto n : this->getChildren())
if (n->getChildren().size() > 0) if (n->getChildren().size() > 0)
return n->getChildren().first(); return n->getChildren().front();
//return n->getChildren().first();
} }
else return nextOnLvl; else return nextOnLvl;
} }
@ -92,8 +98,10 @@ const std::shared_ptr<EditorNode> EditorNode::getRoot()
void EditorNode::move(std::shared_ptr<EditorNode> to) void EditorNode::move(std::shared_ptr<EditorNode> to)
{ {
this->getParent()->getChildren().removeOne(std::shared_ptr<EditorNode>(this)); this->getParent()->getChildren().remove(std::shared_ptr<EditorNode>(this));
to->getChildren().append(std::shared_ptr<EditorNode>(this)); //this->getParent()->getChildren().removeOne(std::shared_ptr<EditorNode>(this));
to->getChildren().push_back(std::shared_ptr<EditorNode>(this));
//to->getChildren().append(std::shared_ptr<EditorNode>(this));
fireTreeChanged(); fireTreeChanged();
} }
@ -106,14 +114,16 @@ void EditorNode::moveChildren(std::shared_ptr<EditorNode> to)
void EditorNode::addChild(std::shared_ptr<EditorNode> child) void EditorNode::addChild(std::shared_ptr<EditorNode> child)
{ {
this->getChildren().append(child); this->getChildren().push_back(child);
//this->getChildren().append(child);
child->setParent(std::shared_ptr<EditorNode>(this)); child->setParent(std::shared_ptr<EditorNode>(this));
fireTreeChanged(); fireTreeChanged();
} }
bool EditorNode::removeChild(std::shared_ptr<EditorNode> child) bool EditorNode::removeChild(std::shared_ptr<EditorNode> child)
{ {
bool ret = this->getChildren().removeOne(child); this->getChildren().remove(child);
bool ret = true; //this->getChildren().removeOne(child);
if (ret) fireTreeChanged(); if (ret) fireTreeChanged();
return ret; return ret;
} }
@ -127,7 +137,8 @@ bool EditorNode::remove()
} }
} }
if (worked && parent != nullptr) { if (worked && parent != nullptr) {
worked &= parent->getChildren().removeOne(std::shared_ptr<EditorNode>(this)); parent->getChildren().remove(std::shared_ptr<EditorNode>(this));
//worked &= parent->getChildren().removeOne(std::shared_ptr<EditorNode>(this));
} }
if (worked) fireTreeChanged(); if (worked) fireTreeChanged();
return worked; return worked;
@ -165,7 +176,7 @@ void EditorNode::setValue(const std::shared_ptr<EditorElementValue> &value)
this->value = value; this->value = value;
} }
QList<std::shared_ptr<EditorNode> > EditorNode::getChildren() const std::list<std::shared_ptr<EditorNode> > EditorNode::getChildren() const
{ {
return children; return children;
} }
@ -191,7 +202,8 @@ std::shared_ptr<EditorNode> EditorNode::nextOnSameLevel()
auto pnode = std::shared_ptr<EditorNode>(this); auto pnode = std::shared_ptr<EditorNode>(this);
while (lvl > 0 && node != nullptr) { while (lvl > 0 && node != nullptr) {
if (node != pnode->getParent() && node->getChildren().size() > 0) { if (node != pnode->getParent() && node->getChildren().size() > 0) {
node = node->getChildren().first(); node = node->getChildren().front();
//node = node->getChildren().first();
--lvl; --lvl;
} else { } else {
pnode = node; pnode = node;

View File

@ -2,8 +2,8 @@
#define EDITORNODE_H #define EDITORNODE_H
#include <memory> #include <memory>
#include <QList>
#include <functional> #include <functional>
#include <list>
#include "editorelementdata.h" #include "editorelementdata.h"
#include "EditorElementValue.h" #include "EditorElementValue.h"
@ -43,7 +43,7 @@ public:
std::shared_ptr<EditorElementValue> getValue() const; std::shared_ptr<EditorElementValue> getValue() const;
void setValue(const std::shared_ptr<EditorElementValue> &value); void setValue(const std::shared_ptr<EditorElementValue> &value);
QList<std::shared_ptr<EditorNode> > getChildren() const; std::list<std::shared_ptr<EditorNode> > getChildren() const;
void setDataChangedSignal(std::function<void(std::shared_ptr<EditorNode>)> f) void setDataChangedSignal(std::function<void(std::shared_ptr<EditorNode>)> f)
{ {
@ -68,7 +68,7 @@ private:
std::shared_ptr<EditorNode> parent; std::shared_ptr<EditorNode> parent;
std::shared_ptr<EditorElementData> data; std::shared_ptr<EditorElementData> data;
std::shared_ptr<EditorElementValue> value; std::shared_ptr<EditorElementValue> value;
QList<std::shared_ptr<EditorNode>> children; std::list<std::shared_ptr<EditorNode>> children;
}; };
#endif // EDITORNODE_H #endif // EDITORNODE_H

View File

@ -3,6 +3,8 @@
#include <QString> #include <QString>
#include <QMap> #include <QMap>
#include <QList> #include <QList>
#include <list>
#include <string>
enum class ValueType { enum class ValueType {
BIN,OCT,DEC,HEX,TXT BIN,OCT,DEC,HEX,TXT
@ -19,12 +21,24 @@ public:
QString getTitle() const; QString getTitle() const;
void setTitle(const QString &value); void setTitle(const QString &value);
void setTitle(const std::string &value) {
this->setTitle(QString::fromStdString(value));
}
QString getId() const; QString getId() const;
void setId(const QString &value); void setId(const QString &value);
void setId(const std::string &value){
this->setId(QString::fromStdString(value));
}
QList<QString> getSyntaxes() const; QList<QString> getSyntaxes() const;
void setSyntaxes(const QList<QString> &value); void setSyntaxes(const QList<QString> &value);
void setSyntaxes(const std::list<std::string> &value) {
QList<QString> newlist;
for(std::string str : value)
newlist.append(QString::fromStdString(str));
this->setSyntaxes(newlist);
}
bool isMultiLine() const; bool isMultiLine() const;
void setIsMultiLine(bool value); void setIsMultiLine(bool value);
@ -34,9 +48,15 @@ public:
QString getRegex() const; QString getRegex() const;
void setRegex(const QString &value); void setRegex(const QString &value);
void setRegex(const std::string &value){
this->setRegex(QString::fromStdString(value));
}
QMap<QString, QString> getHints() const; QMap<QString, QString> getHints() const;
void setHint(const QString &key, const QString &value); void setHint(const QString &key, const QString &value);
void setHint(const std::string &key, const std::string &value){
this->setHint(QString::fromStdString(key), QString::fromStdString(value));
}
ulong getBytes() const; ulong getBytes() const;
void setBytes(const ulong &value); void setBytes(const ulong &value);

View File

@ -1,37 +1,47 @@
#include "DataLoader.h" #include "DataLoader.h"
#include <QDir> #include <boost/filesystem.hpp>
#include <string>
DataLoader::DataLoader() DataLoader::DataLoader()
{ {
} }
DataLoader::DataLoader(QString directory) DataLoader::DataLoader(std::string directory) : DataLoader()
{ {
this->loadProtocolDirectory(directory);
} }
void DataLoader::loadProtocolDirectory(QString directory) void DataLoader::loadProtocolDirectory(std::__cxx11::string directory)
{ {
using namespace boost::filesystem;
// awaiting // awaiting
QDir dir(directory); path p(directory);
QStringList filters; if (exists(p)) {
filters << "*osi*"; // load all files in osi* directories
for(QString entry : dir.entryList(filters)){ for(directory_entry& d : directory_iterator(p))
if(QDir(entry).exists()) { {
QDir cdir(entry); if(is_directory(d) && d.path().filename().string().find("osi") != std::string::npos)
QStringList ymlfilter; {
ymlfilter << "*.yml"; path osi = d.path();
for(QString file : cdir.entryList(ymlfilter)){ for(directory_entry& od: directory_iterator(p))
YAML::Node node = YAML::LoadFile( {
QString(directory + QDir::separator() + entry + QDir::separator() + file).toStdString()); if(od.path().extension().string().compare(std::string(".yml")) == 0 ) {
protocolList.append(node); this->loadSingleProtocol(od.path().string());
}
}
} }
} }
} }
} }
void DataLoader::loadSingleProtocol(std::__cxx11::string filename)
{
YAML::Node node = YAML::LoadFile(filename);
protocolList.push_back(node);
}
std::shared_ptr<EditorNode> DataLoader::generateTree(YAML::Node &node) std::shared_ptr<EditorNode> DataLoader::generateTree(YAML::Node &node)
{ {
if(!node["protocol"]) if(!node["protocol"])
@ -41,9 +51,9 @@ std::shared_ptr<EditorNode> DataLoader::generateTree(YAML::Node &node)
std::shared_ptr<EditorNode> editorNode = std::shared_ptr<EditorNode>(new EditorNode()); std::shared_ptr<EditorNode> editorNode = std::shared_ptr<EditorNode>(new EditorNode());
std::shared_ptr<EditorElementData> data = std::shared_ptr<EditorElementData>(new EditorElementData()); std::shared_ptr<EditorElementData> data = std::shared_ptr<EditorElementData>(new EditorElementData());
data->setTitle(QString::fromStdString(node["protocol"]["name"].as<std::string>())); data->setTitle(node["protocol"]["name"].as<std::string>());
if(node["protocol"]["longname"]) if(node["protocol"]["longname"])
data->setHint("subtitle", QString::fromStdString(node["protocol"]["longname"].as<std::string>())); data->setHint("subtitle", node["protocol"]["longname"].as<std::string>());
editorNode->setData(data); editorNode->setData(data);
// add children containing the fields // add children containing the fields
auto fields = node["protocol"]["fields"]; auto fields = node["protocol"]["fields"];
@ -53,11 +63,11 @@ std::shared_ptr<EditorNode> DataLoader::generateTree(YAML::Node &node)
return editorNode; return editorNode;
} }
bool DataLoader::getProtocolByName(QString name, YAML::Node &node) bool DataLoader::getProtocolByName(std::string name, YAML::Node &node)
{ {
for(YAML::Node n : protocolList) { for(YAML::Node n : protocolList) {
if(n["protocol"]) { if(n["protocol"]) {
if(name.toStdString().compare(n["protocol"]["name"].as<std::string>())) { if(name.compare(n["protocol"]["name"].as<std::string>()) == 0) {
node = n; node = n;
return true; return true;
} }
@ -66,12 +76,12 @@ bool DataLoader::getProtocolByName(QString name, YAML::Node &node)
return false; return false;
} }
QStringList DataLoader::getProtocolNames() std::list<std::string> DataLoader::getProtocolNames()
{ {
QStringList names; std::list<std::string> names;
for(YAML::Node node : protocolList) { for(YAML::Node node : protocolList) {
if(node["protocol"]) { if(node["protocol"]) {
names << QString::fromStdString(node["protocol"]["name"].as<std::string>()); names.push_back(node["protocol"]["name"].as<std::string>());
} }
} }
return names; return names;
@ -83,9 +93,9 @@ std::shared_ptr<EditorNode> DataLoader::genNode(YAML::Node &n, std::shared_ptr<E
std::shared_ptr<EditorNode> newNode = std::shared_ptr<EditorNode>(new EditorNode()); std::shared_ptr<EditorNode> newNode = std::shared_ptr<EditorNode>(new EditorNode());
newNode->setParent(parent); newNode->setParent(parent);
std::shared_ptr<EditorElementData> newData = std::shared_ptr<EditorElementData>(new EditorElementData()); std::shared_ptr<EditorElementData> newData = std::shared_ptr<EditorElementData>(new EditorElementData());
newData->setTitle(QString::fromStdString(n["field"].as<std::string>())); newData->setTitle(n["field"].as<std::string>());
if(n["desc"]) if(n["desc"])
newData->setHint("desc", QString::fromStdString(n["desc"].as<std::string>())); newData->setHint("desc",n["desc"].as<std::string>());
if(n["bytes"]) if(n["bytes"])
newData->setBytes(n["bytes"].as<ulong>()); newData->setBytes(n["bytes"].as<ulong>());
else if (n["bits"]) else if (n["bits"])

View File

@ -3,8 +3,8 @@
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <QString> #include <string>
#include <QList> #include <list>
#include "../editor/EditorNode.h" #include "../editor/EditorNode.h"
@ -12,19 +12,21 @@
class DataLoader { class DataLoader {
public: public:
explicit DataLoader(); explicit DataLoader();
explicit DataLoader(QString directory); explicit DataLoader(std::string directory);
void loadProtocolDirectory(QString directory); void loadProtocolDirectory(std::string directory);
void loadSingleProtocol(std::string filename);
std::shared_ptr<EditorNode> generateTree(YAML::Node &node); std::shared_ptr<EditorNode> generateTree(YAML::Node &node);
bool getProtocolByName(QString name, YAML::Node &node); bool getProtocolByName(std::string name, YAML::Node &node);
QStringList getProtocolNames(); std::list<std::string> getProtocolNames();
private: private:
std::shared_ptr<EditorNode> genNode(YAML::Node& n, std::shared_ptr<EditorNode> parent = nullptr); std::shared_ptr<EditorNode> genNode(YAML::Node& n, std::shared_ptr<EditorNode> parent = nullptr);
QString directory; std::string directory;
QList<YAML::Node> protocolList; std::list<YAML::Node> protocolList;
}; };