Load and save is OK ==> need to choose the saved file

This commit is contained in:
Edouard Dupin 2012-03-12 17:58:26 +01:00
parent 43fc91b20e
commit 018e5b7eb0
3 changed files with 169 additions and 9 deletions

View File

@ -216,7 +216,8 @@ class MainWindows :public ewol::Windows
m_drawer->SetFillY(true);
mySizer->SubWidgetAdd(m_drawer);
RegisterMultiCast(drawMsgGuiOpen);
RegisterMultiCast(drawMsgGuiSave);
};
~MainWindows(void)
@ -237,11 +238,10 @@ class MainWindows :public ewol::Windows
ewol::Windows::OnReceiveMessage(CallerObject, eventId, data);
DRAW_INFO("Receive Event from the main windows ... : widgetid=" << CallerObject << " ==> " << eventId << " ==> data=\"" << data << "\"" );
if (eventId == drawerEventRequestOpenFile) {
if (eventId == drawMsgGuiOpen) {
ewol::FileChooser* tmpWidget = new ewol::FileChooser();
tmpWidget->SetTitle("Open Files ...");
tmpWidget->SetValidateLabel("Open");
tmpWidget->SetFolder("/");
PopUpWidgetPush(tmpWidget);
tmpWidget->RegisterOnEvent(this, ewolEventFileChooserValidate, drawerEventRequestOpenFileSelected);
} else if (eventId == drawerEventRequestOpenFileSelected) {
@ -254,6 +254,17 @@ class MainWindows :public ewol::Windows
// get the filename :
etk::UString tmpData = tmpWidget->GetCompleateFileName();
DRAW_DEBUG("Request opening the file : " << tmpData);
if (NULL != m_drawer) {
m_drawer->Load(tmpData);
}
} else if (eventId == drawMsgGuiSave) {
if (NULL != m_drawer) {
if (m_drawer->HasName()) {
m_drawer->Save();
} else {
DRAW_TODO("Later ...");
}
}
} else if (eventId == drawerEventColorHasChange) {
// the button color has change ==> we really change the current color ...
if (NULL != CallerObject) {

View File

@ -402,9 +402,145 @@ void widgetDrawer::SetColorOnSelected(color_ts newColor)
}
void widgetDrawer::Load(etk::UString fileName)
/*
<e2d ratio="1.000">
<element name="hjhj">
<dot id=1 x="0.2352" y="0.435634" />
<dot id=2 x="0.6746" y="0.323467" />
<dot id=3 x="0.4657" y="0.234131" />
<dot id=3 x="0.00000" y="1.000000" />
<link id1="1" color1="#45F645FF"
id2="2" color2="#345635FF"
id3="4" color3="#867757FF"/>
</element>
</e2d>
*/
void widgetDrawer::Load(etk::UString newFileName)
{
DRAW_TODO("LATER ... ");
// Remove all local elements :
m_dotList.Clear();
m_linkList.Clear();
m_selectedList.Clear();
m_nearestDot = -1;
m_movingPoint = false;
m_fileName = newFileName;
DRAW_DEBUG("open file (DRAWER) \"" << m_fileName << "\"");
// allocate the document in the stack
TiXmlDocument XmlDocument;
// open the curent File
etk::File fileName(m_fileName, etk::FILE_TYPE_DIRECT);
if (false == fileName.Exist()) {
DRAW_ERROR("File Does not exist : " << fileName);
return;
}
int32_t fileSize = fileName.Size();
if (0==fileSize) {
DRAW_ERROR("This file is empty : " << fileName);
return;
}
if (false == fileName.fOpenRead()) {
DRAW_ERROR("Can not open the file : " << fileName);
return;
}
// allocate data
char * fileBuffer = new char[fileSize+5];
if (NULL == fileBuffer) {
DRAW_ERROR("Error Memory allocation size=" << fileSize);
return;
}
memset(fileBuffer, 0, (fileSize+5)*sizeof(char));
// load data from the file :
fileName.fRead(fileBuffer, 1, fileSize);
// close the file:
fileName.fClose();
// load the XML from the memory
XmlDocument.Parse((const char*)fileBuffer, 0, TIXML_ENCODING_UTF8);
TiXmlElement* root = XmlDocument.FirstChildElement( "e2d" );
if (NULL == root ) {
DRAW_ERROR("(l ?) main node not find: \"e2d\" in \"" << fileName << "\"");
return;
} else {
for(TiXmlNode * pNode = root->FirstChild();
NULL != pNode;
pNode = pNode->NextSibling() ) {
if (pNode->Type()==TiXmlNode::TINYXML_COMMENT) {
// nothing to do, just proceed to next step
} else if (!strcmp(pNode->Value(), "element")) {
for(TiXmlNode * pGuiNode = pNode->FirstChild();
NULL != pGuiNode;
pGuiNode = pGuiNode->NextSibling()) {
if (pGuiNode->Type()==TiXmlNode::TINYXML_COMMENT) {
// nothing to do, just proceed to next step
} else if (!strcmp(pGuiNode->Value(), "dot")) {
const char *xxx = pGuiNode->ToElement()->Attribute("x");
const char *yyy = pGuiNode->ToElement()->Attribute("y");
if( NULL != xxx
&& NULL != yyy) {
coord2D_ts pos;
double posX, posY;
sscanf(xxx, "%lf", &posX);
sscanf(yyy, "%lf", &posY);
pos.x = posX;
pos.y = posY;
DRAW_DEBUG("load dot : " << xxx << "," << yyy << " ==>" << pos);
m_dotList.PushBack(pos);
}
} else if (!strcmp(pGuiNode->Value(), "link")) {
const char *id[3];
const char *color[3];
id[0] = pGuiNode->ToElement()->Attribute("id1");
id[1] = pGuiNode->ToElement()->Attribute("id2");
id[2] = pGuiNode->ToElement()->Attribute("id3");
color[0] = pGuiNode->ToElement()->Attribute("color1");
color[1] = pGuiNode->ToElement()->Attribute("color2");
color[2] = pGuiNode->ToElement()->Attribute("color3");
if( NULL != id[0]
&& NULL != id[1]
&& NULL != id[2]
&& NULL != color[0]
&& NULL != color[1]
&& NULL != color[2]) {
link_ts localLink;
int r=0;
int v=0;
int b=0;
int a=-1;
for(int32_t kkk=0; kkk<3; kkk++) {
sscanf(id[kkk], "%d", &localLink.dot[kkk]);
sscanf(color[kkk], "#%02x%02x%02x%02x", &r, &v, &b, &a);
localLink.color[kkk].red = (float)r/255.0;
localLink.color[kkk].green = (float)v/255.0;
localLink.color[kkk].blue = (float)b/255.0;
if (-1 == a) {
localLink.color[kkk].alpha = 1;
} else {
localLink.color[kkk].alpha = (float)a/255.0;
}
}
DRAW_DEBUG("load link : [" << localLink.dot[0] << "," << localLink.dot[1] << "," << localLink.dot[2] << "] ");
DRAW_DEBUG(" col: [" << localLink.color[0] << "," << localLink.color[1] << "," << localLink.color[2] << "] ");
m_linkList.PushBack(localLink);
}
} else {
DRAW_ERROR("(l "<<pGuiNode->Row()<<") node not suported : \""<<pGuiNode->Value()<<"\" must be [dot,link]");
}
}
} else {
DRAW_ERROR("(l "<<pNode->Row()<<") node not suported : \""<<pNode->Value()<<"\" must be [element]");
}
}
}
if (NULL != fileBuffer) {
delete[] fileBuffer;
}
MarkToReedraw();
}
/*
<e2d ratio="1.000">
@ -419,8 +555,12 @@ void widgetDrawer::Load(etk::UString fileName)
</element>
</e2d>
*/
void widgetDrawer::Save(etk::UString fileName)
void widgetDrawer::Save(void)
{
if (m_fileName == "") {
DRAW_ERROR("No filename set ...");
return;
}
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );
doc.LinkEndChild( decl );
@ -433,10 +573,13 @@ void widgetDrawer::Save(etk::UString fileName)
mastertElement->LinkEndChild( element );
for(int32_t iii=0; iii<m_dotList.Size() ; iii++) {
char tmpChar[256];
TiXmlElement * elementDot = new TiXmlElement( "dot" );
elementDot->SetAttribute( "id", iii );
elementDot->SetAttribute( "x", m_dotList[iii].x );
elementDot->SetAttribute( "y", m_dotList[iii].y );
sprintf(tmpChar, "%f", m_dotList[iii].x);
elementDot->SetAttribute( "x", tmpChar );
sprintf(tmpChar, "%f", m_dotList[iii].y);
elementDot->SetAttribute( "y", tmpChar );
element->LinkEndChild( elementDot );
}
for(int32_t iii=0; iii<m_linkList.Size() ; iii++) {
@ -465,5 +608,7 @@ void widgetDrawer::Save(etk::UString fileName)
elementDot->SetAttribute( "color3", colorText );
element->LinkEndChild( elementDot );
}
//Save Document
doc.SaveFile( m_fileName.Utf8Data() );
}

View File

@ -94,12 +94,16 @@ class widgetDrawer :public ewol::Widget
etk::VectorType<int32_t> m_selectedList; //!< current selected points
int32_t m_nearestDot; //!< nearest dot from the current cursor
bool m_movingPoint;
etk::UString m_fileName;
void removeDotId(int32_t id);
int32_t GetNearestPoint(coord2D_ts pos);
etkFloat_t QuadDistance(coord2D_ts aaa, coord2D_ts bbb);
bool DotIsSelected(int32_t dotId);
public:
void Load(etk::UString fileName);
void Save(etk::UString fileName);
void Save(void);
void SetFilename(etk::UString fileName) {m_fileName = fileName; };
bool HasName(void) { if(m_fileName=="") { return false;} return true; };
};
#endif