Skip to content

Commit

Permalink
Serialization and Deserialization of models
Browse files Browse the repository at this point in the history
  • Loading branch information
amelhaoui committed Mar 17, 2016
1 parent 5030358 commit 9d4116f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 22 deletions.
Binary file modified FlowTransformer.app/Contents/MacOS/FlowTransformer
Binary file not shown.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -719,15 +719,19 @@ main.o: main.cpp mainwindow.h \
../../../../Qt/5.5/clang_64/lib/QtWidgets.framework/Headers/qtreeview.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp

mainwindow.o: mainwindow.cpp mainwindow.h \
ui_mainwindow.h \
mainwindow.o: mainwindow.cpp inc/tinyxml2.h \
emc_parser.cpp \
emc_parse_utility.h \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QHash \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qhash.h \
treemodel.h \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QAbstractItemModel \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qabstractitemmodel.h \
emc_parse_utility.h \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QModelIndex \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QVariant \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qvariant.h \
mainwindow.h \
ui_mainwindow.h \
treemodelcompleter.h \
../../../../Qt/5.5/clang_64/lib/QtWidgets.framework/Headers/QCompleter \
../../../../Qt/5.5/clang_64/lib/QtWidgets.framework/Headers/qcompleter.h \
Expand All @@ -736,10 +740,6 @@ mainwindow.o: mainwindow.cpp mainwindow.h \
../../../../Qt/5.5/clang_64/lib/QtWidgets.framework/Headers/qmainwindow.h \
../../../../Qt/5.5/clang_64/lib/QtGui.framework/Headers/QStandardItemModel \
../../../../Qt/5.5/clang_64/lib/QtGui.framework/Headers/qstandarditemmodel.h \
inc/tinyxml2.h \
emc_parser.cpp \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QHash \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qhash.h \
treeitem.h \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/QList \
../../../../Qt/5.5/clang_64/lib/QtCore.framework/Headers/qlist.h \
Expand Down
125 changes: 112 additions & 13 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "mainwindow.h"

#include "expat.h"
#include "inc/tinyxml2.h"
#include "emc_parser.cpp"
#include "treemodel.h"
#include "mainwindow.h"

#include "treeitem.h"
#include "emc_parse_utility.h"
#include "itemtabledelegate.h"
Expand Down Expand Up @@ -511,7 +513,7 @@ print_to_xml (XMLDocument& xmlDoc, TreeModel* m_target, QHash<QString, QString>&

QString path = root_data; // path used in mapping
// print using a helper function
for (int i = 0; i < root->childCount() - 1; ++i) { // -1 because the last one is "Attributes"
for (int i = root->childCount() - 1; i > 0; --i) { // -1 because the last one is "Attributes"
QString child_tag = root->child(i)->data(0).toString();
QString child_path = path + "." + child_tag;
print_to_xml_Helper(root->child(i), pRoot->ToElement(), xmlDoc, values_target, occurence, mapping, child_path);
Expand Down Expand Up @@ -601,19 +603,116 @@ void MainWindow::on_btn_upload_model_target_clicked()
{
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Load your input"), "/", tr("File (*.xml *.xls)"));
char buffer[BUFFER_SIZE];
tr("Load your mapping"), "/", tr("File (*.txt)"));

// char buffer[BUFFER_SIZE];
// ModelXML* model_a = new ModelXML(NULL, "american");
// parse_xml(buffer, BUFFER_SIZE, fileName.toStdString().c_str(), model_a, mapping);
// // use target model if available
// if (model_cible != NULL)
// model_target = qobject_cast<TreeModel*> (model_source);
// else

if (fileName.isEmpty())
return;
ModelXML* model_a = new ModelXML(NULL, "american");
parse_xml(buffer, BUFFER_SIZE, fileName.toStdString().c_str(), model_a, mapping);
QStringList headers;
headers << tr("Tag");
// // use target model if available
// if (model_cible != NULL)
// model_target = qobject_cast<TreeModel*> (model_source);
// else
model_target = new TreeModel(headers, model_a->root);

QFile file(fileName);

if(!file.open(QIODevice::ReadOnly))
return;
QDataStream in(&file);

// todo : check if file data is valid
QStack<pair<TreeItem*, int>*> s;
QVector<QVariant> data;
int num;
QVariant var;
in >> var >> num;
data << "Tag";
TreeItem* root = new TreeItem(data);
root->insertChildren(0, 1, var);
s.push(new pair<TreeItem*, int>(root->child(0), num));
// now we have created our root and know how many childs it has

deserialize(in, s);

model_target = new TreeModel(root);
treeViewTarget->setModel(model_target);
}
void MainWindow::deserialize(QDataStream &in, QStack<pair<TreeItem*, int>*>& s) {
if (s.empty())
return;

TreeItem* current = s.top()->first;
int num_childs = s.top()->second;
s.pop();

QVariant var;
int num;

for (int i = 0; i < num_childs; ++i) { // until - 1 because the last element is Attributes
in >> var >> num;
current->insertChildren(i, 1, var);
s.push(new pair<TreeItem*, int>(current->child(i), num));
// before we continue
deserialize(in, s);
}
}

void MainWindow::on_pushButton_clicked()
{
TreeModel* source_model = qobject_cast<TreeModel*> (sourceView->model());
serializeModel(source_model, this);
}

void MainWindow::serializeModel(TreeModel* model, MainWindow* mainWindow)
{
// serialize model into a file
if (model == NULL)
return;
// fake root is the header, child(0) represent the tree root
TreeItem* root = model->getRoot();
if (root == NULL)
return;
root = root->child(0);

// we suppose it's not an empty tree, checked that at beggining
QString fileName = QFileDialog::getSaveFileName(mainWindow, "Save Mapping",
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
"Text files (*.txt)");
if (fileName.isEmpty())
return;

QFile file(fileName);

if(!file.open(QIODevice::WriteOnly))
return;

QDataStream out(&file);

// we use a stack to traverse the structure and serialize it's content
QStack<TreeItem*> s;
// initialise it with root element
s.push(root);

while (!s.empty()) {
// process each element
TreeItem* current = s.top(); s.pop();
// we need data but also how many childs it has to deserialize it later
out << current->data(0) << current->childCount();
// add childrens to stack
for (int i = 0; i < current->childCount(); ++i) {
s.push(current->child(i));
}
}

// push to the file
file.flush();
file.close();
}

void MainWindow::on_pushButton_2_clicked()
{
TreeModel* target_model = qobject_cast<TreeModel*> (targetView->model());
serializeModel(target_model, this);
}
7 changes: 7 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class MainWindow : public QMainWindow, private Ui::MainWindow

public:
MainWindow(QWidget *parent = 0);
void deserialize(QDataStream &in, QStack<pair<TreeItem*, int>*>& s);
void serializeModel(TreeModel* model, MainWindow* mainWindow);


public slots:
void updateActions();
Expand Down Expand Up @@ -49,6 +52,10 @@ private slots:

void on_btn_upload_model_target_clicked();

void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
DragDropModel* model_cible;
DragDropModel* model_source;
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<item>
<widget class="QTabWidget" name="tabWidget_2">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<property name="movable">
<bool>true</bool>
Expand Down
6 changes: 6 additions & 0 deletions treemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ TreeModel::TreeModel(const QStringList &headers, Node *root, QObject *parent)
setupModelData(root);
}

TreeModel::TreeModel(TreeItem *root, QObject *parent)
: QAbstractItemModel(parent)
{
rootItem = root;
}

TreeModel::~TreeModel()
{
delete rootItem;
Expand Down
2 changes: 2 additions & 0 deletions treemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TreeModel : public QAbstractItemModel
TreeModel(const QStringList &headers, const QString &data,
QObject *parent = 0);
TreeModel(const QStringList &header, Node* root, QObject *parent = 0);
TreeModel(TreeItem* root, QObject *parent = 0);

~TreeModel();

QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
Expand Down
2 changes: 1 addition & 1 deletion ui_mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class Ui_MainWindow

retranslateUi(MainWindow);

tabWidget_2->setCurrentIndex(1);
tabWidget_2->setCurrentIndex(0);


QMetaObject::connectSlotsByName(MainWindow);
Expand Down

0 comments on commit 9d4116f

Please sign in to comment.