|
From: <dhu...@us...> - 2007-01-24 18:34:45
|
Revision: 252
http://svn.sourceforge.net/qcell/?rev=252&view=rev
Author: dhubleizh
Date: 2007-01-24 10:34:40 -0800 (Wed, 24 Jan 2007)
Log Message:
-----------
- serialization and deserialization fully work
- tested it somewhat
Modified Paths:
--------------
trunk/qcell/visgui/MainWindow.cpp
trunk/qcell/visgui/MainWindow.h
Modified: trunk/qcell/visgui/MainWindow.cpp
===================================================================
--- trunk/qcell/visgui/MainWindow.cpp 2007-01-24 17:06:29 UTC (rev 251)
+++ trunk/qcell/visgui/MainWindow.cpp 2007-01-24 18:34:40 UTC (rev 252)
@@ -447,16 +447,8 @@
return;
}
- //***************************************************
-// *sw->getNeighbourhoodEditor()->getStorage() = neighbourhood->toCalculationData();
- sw->getNeighbourhoodEditor()->setTranslation(0.0f, 0.0f, -10.0f);
- sw->getNeighbourhoodEditor()->setSymbolColor(1, QColor(128, 128, 128));
- //***************************************************
- calc.setNeighbourhood(neighbourhood);
+ setupNeighbourhood();
- // Enable saving menu for Neighbourhood
- menu_Save->setEnabled(true);
- action_Neighbourhood_save->setEnabled(true);
}
else if (type == "LocalFunction")
{
@@ -472,7 +464,7 @@
local_function = new LocalFunction();
if (!local_function->fromXmlString(&function_parsers[subtype]->parse(file_content, type, subtype)))
{
- qDebug(tr("Internal error: unable to parse %1 file")
+ qDebug(tr("Internal error: unable to parse %1 file.")
.arg(type)
.toAscii()
);
@@ -480,14 +472,8 @@
return;
}
- // Enable saving menu for LocalFunction
- menu_Save->setEnabled(true);
- action_Function_save->setEnabled(true);
+ setupLocalFunction();
- calc.setLocalFunction(local_function);
- // for test only *********************************
- sw->getFunctionTable()->setFunctionPointer(local_function);
- //************************************************
}
else if (type == "World")
{
@@ -500,76 +486,23 @@
return;
}
- /// @todo Fix that!
+ // Reset the data
data.clear();
- /// @todo Get rid of that ugly hack!
- QString XMLString = world_parsers[subtype]->parse(file_content, type, subtype);
- /// @todo Nasty - get that code out of here !
- if(XMLString.isEmpty())
+
+ // Parse the file
+ CalculationData *tempCD = new CalculationData();
+ if(!tempCD->setFromXmlString(&world_parsers[subtype]->parse(file_content, type, subtype)))
{
- qDebug(tr("The file extensions %1 isn't supported.")
+ qDebug(tr("Internal error: unable to parse %1 file.")
.arg(subtype)
.toAscii()
);
return;
}
- // Internal data settings
- //data.append((CalculationData*)world_parsers[subtype]->parse(file_content, type, subtype).toInt());
- CalculationData *tempCD = new CalculationData();
- tempCD->setFromXmlString(&XMLString);
data.append(tempCD);
- *(CalculationData*)&calc = *data.last();
- // Creating elemental rules accroding to DATA_TYPE
- elemental_rules = new ElementalRules;
- // Pass the calculation result to rules selection
- connect(&calc, SIGNAL(calculated(QVector<int>, QVector<int>, int)),
- elemental_rules, SLOT(possibleRule(QVector<int>, QVector<int>, int))
- );
-
- // Notify things connected with rules to reset the list
- // as we hold only one iteration rules
- connect(&calc, SIGNAL(calculationBegin()),
- elemental_rules, SLOT(resetList())
- );
- connect(&calc, SIGNAL(calculationBegin()),
- ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(resetList())
- );
-
- // Display the results (there is no need to refresh the table before
- // the end of one iteration)
- connect(&calc, SIGNAL(calculationEnd()),
- ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(display())
- );
-
- // Notify the GUI 'bout new items
- connect(elemental_rules, SIGNAL(newRule(int, QVector<int>, int, QVector<int>)),
- ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(addRule(int, QVector<int>, int, QVector<int>))
- );
- connect(elemental_rules, SIGNAL(newOccurance(int, QVector<int>)),
- ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(addOccurance(int, QVector<int>))
- );
-
- // Pass rules selection from GUI to engine
- connect(((ElementalRulesWidget*)elemental_dock->widget()), SIGNAL(rulesSelected(QVector<int>)),
- elemental_rules, SLOT(rulesSelected(QVector<int>))
- );
-
- // We should only work when the rules list is visible, as it slows
- // the calculation considerably
- connect(elemental_dock->toggleViewAction(), SIGNAL(toggled(bool)),
- elemental_rules, SLOT(work(bool))
- );
-
- iteration=0;
-
- // visualization update
- update();
-
- // Enable saving menu for World
- menu_Save->setEnabled(true);
- action_World_save->setEnabled(true);
+ setupWorld();
}
else
{
@@ -1095,49 +1028,143 @@
doc.appendChild(root);
+ statusBar()->showMessage(tr("Successfully saved experiment for late continuation."));
return doc.toByteArray();
}
void MainWindow::deserialize(QByteArray content)
{
/// @todo Wait for fromXml in CalculationData and user pure XML
- QDataStream ds(&content, QIODevice::ReadOnly);
- qint32 data_size;
- QByteArray data_portion;
+ QDomDocument doc;
+ QDomElement* root;
+ int iterations;
- // Neighbourhood
- ds >> data_size;
- data_portion.resize(data_size);
- ds.readRawData(data_portion.data(), (uint)data_size);
+ if(!doc.setContent(content))
+ {
+ qDebug(tr("There were errors in parsing the file's content")
+ .toAscii()
+ );
+ return;
+ }
- if (neighbourhood == NULL)
+ if(!doc.hasChildNodes())
{
- neighbourhood = new Neighbourhood();
+ qDebug(tr("The document has no nodes!")
+ .toAscii()
+ );
+ return;
}
- if (!neighbourhood->fromXmlString(&QString(data_portion)))
+
+ if(!(doc.firstChild().nodeName() == "Experiment"))
{
- qDebug(tr("Error deserializing Neighbourhood").toAscii());
+ qDebug(tr("First node isn't `Experiment'.")
+ .toAscii()
+ );
return;
}
- // LocalFunction
- ds >> data_size;
- data_portion.resize(data_size);
- ds.readRawData(data_portion.data(), (uint)data_size);
+ if (!(doc.firstChild().isElement()))
+ {
+ qDebug(tr("Error parsing Experiment node.")
+ .toAscii()
+ );
+ return;
+ }
+ root = &doc.firstChild().toElement();
- if (local_function == NULL)
+ if(!root->hasAttribute("iterations"))
{
- local_function = new LocalFunction();
+ qDebug(tr("Iteration attribute in `Experiment' node.")
+ .toAscii()
+ );
+ return;
}
- if (!local_function->fromXmlString(&QString(data_portion)))
+ iterations = root->attribute("iterations").toInt();
+ iterations++;
+
+ // Neighbourhood parsing
+ if (!(root->firstChild().nodeName() == "Neighbourhood"))
{
- qDebug(tr("Error deserializing Local Function").toAscii());
+ qDebug(tr("First leaf should be `Neighbourhood'")
+ .toAscii()
+ );
return;
}
+ neighbourhood = new Neighbourhood();
- /// @todo deserialize CalculationData
+ if (!neighbourhood->fromDomElement(&root->firstChild().toElement()))
+ {
+ qDebug(tr("Error parsing Neighbourhood element")
+ .toAscii()
+ );
+ return;
+ }
+ root->removeChild(root->firstChild());
- statusBar()->showMessage(tr("Sucessfully loaded experiment for continuation.").toAscii());
+
+ // Local Function parsing
+ if(!(root->firstChild().nodeName() == "LocalFunction"))
+ {
+ qDebug(tr("Second leaf should be `Local Function'.")
+ .toAscii()
+ );
+ return;
+ }
+ local_function = new LocalFunction();
+
+ if (!local_function->fromDomElement(&root->firstChild().toElement()))
+ {
+ qDebug(tr("Error parsing Local Function element.")
+ .toAscii()
+ );
+ return;
+ }
+ root->removeChild(root->firstChild());
+
+ // Reset data
+ data.clear();
+
+ CalculationData* tmp_data;
+ // And finally CalculationData parsing
+ for (int i = 0; i < iterations; i++)
+ {
+ if (!(root->firstChild().nodeName() == "CalculationData"))
+ {
+ qDebug(tr("The %1 leaf should be `CalculatioData")
+ .arg(i+2)
+ .toAscii()
+ );
+ return;
+ }
+ tmp_data = new CalculationData();
+
+ if (!tmp_data->parseXmlElement(&root->firstChild().toElement()))
+ {
+ qDebug(tr("Error parsing the %1th CalculationData.")
+ .arg(i)
+ .toAscii()
+ );
+ return;
+ }
+
+ data.append(tmp_data);
+ root->removeChild(root->firstChild());
+ }
+
+ if (root->hasChildNodes())
+ {
+ qDebug(tr("Hm, there are some nodes left, but I've extracted all iterations. I'll gracefully ignore that.")
+ .toAscii()
+ );
+ }
+
+ statusBar()->showMessage(tr("Successfully loaded experiment for continuation.").toAscii());
+
+ setupNeighbourhood();
+ setupLocalFunction();
+ setupWorld();
+
+ unlockExperiment();
}
void MainWindow::sliderChanged(int value)
@@ -1187,3 +1214,91 @@
}
}
+void MainWindow::setupNeighbourhood()
+{
+ // Pass Neighbourhood to Calculator
+ calc.setNeighbourhood(neighbourhood);
+
+ // Enable saving menu for Neighbourhood
+ menu_Save->setEnabled(true);
+ action_Neighbourhood_save->setEnabled(true);
+
+ // Setup Neighbourhood editor
+ //***************************************************
+ // *sw->getNeighbourhoodEditor()->getStorage() = neighbourhood->toCalculationData();
+ sw->getNeighbourhoodEditor()->setTranslation(0.0f, 0.0f, -10.0f);
+ sw->getNeighbourhoodEditor()->setSymbolColor(1, QColor(128, 128, 128));
+ //***************************************************
+}
+
+void MainWindow::setupLocalFunction()
+{
+ // Pass Local Function to Calculator
+ calc.setLocalFunction(local_function);
+
+ // Enable saving menu for LocalFunction
+ menu_Save->setEnabled(true);
+ action_Function_save->setEnabled(true);
+
+ // for test only *********************************
+ sw->getFunctionTable()->setFunctionPointer(local_function);
+ //************************************************
+}
+
+void MainWindow::setupWorld()
+{
+ // Pass data to Calculator
+ *(CalculationData*)&calc = *data.last();
+
+ // Creating elemental rules according to DATA_TYPE
+ elemental_rules = new ElementalRules;
+
+ // Pass the calculation result to rules selection
+ connect(&calc, SIGNAL(calculated(QVector<int>, QVector<int>, int)),
+ elemental_rules, SLOT(possibleRule(QVector<int>, QVector<int>, int))
+ );
+
+ // Notify things connected with rules to reset the list
+ // as we hold only one iteration rules
+ connect(&calc, SIGNAL(calculationBegin()),
+ elemental_rules, SLOT(resetList())
+ );
+ connect(&calc, SIGNAL(calculationBegin()),
+ ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(resetList())
+ );
+
+ // Display the results (there is no need to refresh the table before
+ // the end of one iteration)
+ connect(&calc, SIGNAL(calculationEnd()),
+ ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(display())
+ );
+
+ // Notify the GUI 'bout new items
+ connect(elemental_rules, SIGNAL(newRule(int, QVector<int>, int, QVector<int>)),
+ ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(addRule(int, QVector<int>, int, QVector<int>))
+ );
+ connect(elemental_rules, SIGNAL(newOccurance(int, QVector<int>)),
+ ((ElementalRulesWidget*)elemental_dock->widget()), SLOT(addOccurance(int, QVector<int>))
+ );
+
+ // Pass rules selection from GUI to engine
+ connect(((ElementalRulesWidget*)elemental_dock->widget()), SIGNAL(rulesSelected(QVector<int>)),
+ elemental_rules, SLOT(rulesSelected(QVector<int>))
+ );
+
+ // We should only work when the rules list is visible, as it slows
+ // the calculation considerably
+ connect(elemental_dock->toggleViewAction(), SIGNAL(toggled(bool)),
+ elemental_rules, SLOT(work(bool))
+ );
+
+ iteration=data.count()-1;
+
+ // visualization update
+ update();
+
+ // Enable saving menu for World
+ menu_Save->setEnabled(true);
+ action_World_save->setEnabled(true);
+}
+
Modified: trunk/qcell/visgui/MainWindow.h
===================================================================
--- trunk/qcell/visgui/MainWindow.h 2007-01-24 17:06:29 UTC (rev 251)
+++ trunk/qcell/visgui/MainWindow.h 2007-01-24 18:34:40 UTC (rev 252)
@@ -93,6 +93,9 @@
void callParser(QString filename, QString type);
void callSaver(const QString filename, const QString type);
void unlockExperiment();
+ void setupNeighbourhood();
+ void setupLocalFunction();
+ void setupWorld();
/// @todo Not in here!
enum Direction
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|