/** * Performs the actual saving of project @a p. */ bool FileIO::doSave(Project* p) { Machine* m = p->machine; if (!m) return FALSE; QList<GState*> list; QList<GTransition*> tlist; GState *state; State* dest_state; GTransition* t; QFile file(act_file); if (!file.open(QIODevice::WriteOnly)) { Error::info(tr("File cannot be written.")); qDebug("file cannot be opened for writing"); return FALSE; } Q3TextStream s(&file); list = m->getSList(); QMutableListIterator<GState*> i(list); AppInfo info(main); double xpos, ypos; double c1x, c1y, c2x, c2y; double endx, endy; int initial; // int canvasw, canvash; QString transio; s << info.getVersionMajor() << " "; s << info.getVersionMinor() << endl; s << m->getName() << endl; s << m->getVersion() << endl; s << m->getAuthor() << endl; s << m->getDescription() << endl; s << m->getType() << " "; s << m->getNumMooreOutputs() << " "; s << m->getNumInputs() << " "; s << m->getNumOutputs() << endl; s << m->getMooreOutputNames() << endl; s << m->getMealyInputNames() << endl; s << m->getMealyOutputNames() << endl; s << m->countStates() << endl; state = m->getInitialState(); if (state) initial= state->getEncoding(); else initial=-1; s << initial << endl; // m->getCanvasSize(canvasw, canvash); // s << canvasw << " " << canvash << endl; s << m->getSFont().family() << endl << m->getSFont().pointSize() << endl; s << m->getTFont().family() << endl << m->getTFont().pointSize() << endl; s << m->getArrowType() << endl; s << endl; for(; i.hasNext();) { state = i.next(); state->getPos(xpos, ypos); s.setf(Q3TextStream::bin); s << state->getEncoding() << endl; s << state->getMooreOutputsStr() << endl; s.setf(Q3TextStream::dec); s << state->getStateName() << endl; s << state->getDescription() << endl; s << xpos << " " << ypos << " " << state->getRadius() << endl; s << state->getPen().color().rgb() << " " << state->getLineWidth() << endl; s << state->getBrush().color().rgb() << endl; s << state->isFinalState() << endl; s << endl; } s << endl; i.toFront(); for(; i.hasNext();) { state = i.next(); s.setf(Q3TextStream::bin); s << state->getEncoding() << endl; s.setf(Q3TextStream::dec); tlist = state->tlist; QMutableListIterator<GTransition*> j(tlist); s << state->countTransitions() << endl; for(; j.hasNext();) { t = j.next(); dest_state = t->getEnd(); s.setf(Q3TextStream::bin); if (dest_state) s << dest_state->getEncoding() << endl; else s << -1 << endl; s.setf(Q3TextStream::dec); s << t->getInfo()->getType() << endl; s << t->getDescription() << endl; // if (t->getInfo()->getType()==Binary) { s << t->getInfo()->getInputsStr(NULL) << " "; transio = t->getInfo()->getOutputsStr(NULL); if (transio.isEmpty()) transio="<noout>"; s << transio << endl; } t->getPos(xpos, ypos); t->getEndPos(endx, endy); t->getCPoint1(c1x, c1y); t->getCPoint2(c2x, c2y); s << xpos << " " << ypos << " "; s << c1x << " " << c1y << " " << c2x << " " << c2y << " "; s << endx << " " << endy << " "; s << (int)t->isStraight() << endl; s << endl; } s << endl; } // phantom state state = m->getPhantomState(); tlist = state->tlist; QMutableListIterator<GTransition*> ph(tlist); s << state->countTransitions() << endl; for(; ph.hasNext();) { t = ph.next(); dest_state = t->getEnd(); s.setf(Q3TextStream::bin); if (dest_state) s << dest_state->getEncoding() << endl; else s << -1 << endl; s.setf(Q3TextStream::dec); s << t->getInfo()->getType() << endl; s << t->getDescription() << endl; // if (t->getInfo()->getType()==Binary) { s << t->getInfo()->getInputsStr(NULL) << " "; transio = t->getInfo()->getOutputsStr(NULL); if (transio.isEmpty()) transio="<noout>"; s << transio << endl; } t->getPos(xpos, ypos); t->getEndPos(endx, endy); t->getCPoint1(c1x, c1y); t->getCPoint2(c2x, c2y); s << xpos << " " << ypos << " "; s << c1x << " " << c1y << " " << c2x << " " << c2y << " "; s << endx << " " << endy << " "; s << (int)t->isStraight() << endl; s << endl; } file.close(); p->setChanged(FALSE); return TRUE; }
/** * Creates a DOM document of this project and returns it. * * @param onlyselected If TRUE, only selected objects are used, otherwise the whole project * is used * @param obj If != NULL, only @a obj is used from the project * @returns The DOM document */ QDomDocument Project::getDomDocument(bool onlyselected/*=FALSE*/, GObject* obj/*=NULL*/) { Machine* m = machine; m->correctCodes(); QString prolog="<?xml version=\"1.0\"?>\n" "<!DOCTYPE qfsmproject SYSTEM \"qfsm.dtd\">\n\n" "<qfsmproject>\n" "</qfsmproject>\n"; QDomDocument domdoc; QDomElement root, me, one, ine, onme, itranse; QDomText ontext, intext, onmtext; QString stmp; int inits; GState* s; GITransition* initt; GTransition* t; QList<GState*> slist; QList<GTransition*> tlist; double xpos, ypos, endx, endy, c1x, c1y, c2x, c2y; AppInfo appinfo(qApp->mainWidget()); domdoc.setContent(prolog); if (!m) return domdoc; root = domdoc.documentElement(); // Machine root.setAttribute("author", "Qfsm"); root.setAttribute("version", appinfo.getVersion()); me = domdoc.createElement("machine"); me.setAttribute("name", m->getName()); me.setAttribute("version", m->getVersion()); me.setAttribute("author", m->getAuthor()); me.setAttribute("description", m->getDescription()); me.setAttribute("type", m->getType()); me.setAttribute("nummooreout", m->getNumMooreOutputs()); me.setAttribute("numbits", m->getNumEncodingBits()); me.setAttribute("numin", m->getNumInputs()); me.setAttribute("numout", m->getNumOutputs()); s = m->getInitialState(); if (s) { inits = s->getEncoding(); me.setAttribute("initialstate", inits); } me.setAttribute("statefont", m->getSFont().family()); me.setAttribute("statefontsize", m->getSFont().pointSize()); me.setAttribute("statefontweight", m->getSFont().weight()); me.setAttribute("statefontitalic", m->getSFont().italic()); me.setAttribute("transfont", m->getTFont().family()); me.setAttribute("transfontsize", m->getTFont().pointSize()); me.setAttribute("transfontweight", m->getTFont().weight()); me.setAttribute("transfontitalic", m->getTFont().italic()); me.setAttribute("arrowtype", m->getArrowType()); me.setAttribute("draw_it", m->getDrawITrans()); root.appendChild(me); // Input/Output names onme = domdoc.createElement("outputnames_moore"); ine = domdoc.createElement("inputnames"); one = domdoc.createElement("outputnames"); intext = domdoc.createTextNode(m->getMealyInputNames()); ontext = domdoc.createTextNode(m->getMealyOutputNames()); onmtext = domdoc.createTextNode(m->getMooreOutputNames()); ine.appendChild(intext); one.appendChild(ontext); onme.appendChild(onmtext); me.appendChild(onme); me.appendChild(ine); me.appendChild(one); // Initial Transition initt = m->getInitialTransition(); if (initt) { initt->getPos(xpos, ypos); initt->getEndPos(endx, endy); itranse = domdoc.createElement("itransition"); itranse.setAttribute("xpos", xpos); itranse.setAttribute("ypos", ypos); itranse.setAttribute("endx", endx); itranse.setAttribute("endy", endy); me.appendChild(itranse); } // States slist = m->getSList(); QListIterator<GState*> sit(slist); for(;sit.hasNext();) { s = sit.next(); if (!s->isDeleted() && (!onlyselected || s->isSelected() || (s==obj && s!=NULL))) { s->getPos(xpos, ypos); QDomElement se = domdoc.createElement("state"); QDomText st = domdoc.createTextNode(s->getStateName()); se.setAttribute("description", s->getDescription()); se.setAttribute("code", s->getEncoding()); se.setAttribute("moore_outputs", s->getMooreOutputsStr()); se.setAttribute("xpos", xpos); se.setAttribute("ypos", ypos); se.setAttribute("radius", s->getRadius()); se.setAttribute("pencolor", s->getColor().rgb() & 0xffffff); se.setAttribute("linewidth", s->getLineWidth()); se.setAttribute("finalstate", s->isFinalState()); se.setAttribute("entry_actions", s->getEntryActions()); se.setAttribute("exit_actions", s->getExitActions()); se.appendChild(st); me.appendChild(se); } } // Transitions sit.toFront(); for(; sit.hasNext();) { s = sit.next(); QListIterator<GTransition*> tit(s->tlist); GState *send; for(;tit.hasNext();) { t = tit.next(); if (!t->isDeleted() && (!onlyselected || t->isSelected() || (t==obj && t!=NULL))) { t->getPos(xpos, ypos); t->getEndPos(endx, endy); t->getCPoint1(c1x, c1y); t->getCPoint2(c2x, c2y); QDomElement te = domdoc.createElement("transition"); te.setAttribute("type", t->getInfo()->getType()); te.setAttribute("xpos", xpos); te.setAttribute("ypos", ypos); te.setAttribute("endx", endx); te.setAttribute("endy", endy); te.setAttribute("c1x", c1x); te.setAttribute("c1y", c1y); te.setAttribute("c2x", c2x); te.setAttribute("c2y", c2y); te.setAttribute("straight", t->isStraight()); te.setAttribute("description", t->getDescription()); send = (GState*)t->getEnd(); if (!onlyselected || s->isSelected()) { QDomElement from = domdoc.createElement("from"); QString sfrom; sfrom.sprintf("%d", s->getEncoding()); QDomText fromt = domdoc.createTextNode(sfrom); from.appendChild(fromt); te.appendChild(from); } if (send && (!onlyselected || send->isSelected())) { QDomElement to = domdoc.createElement("to"); QString sto; sto.sprintf("%d", send->getEncoding()); QDomText tot = domdoc.createTextNode(sto); to.appendChild(tot); te.appendChild(to); } QDomElement inpute, outpute; QDomText inputt, outputt; inpute = domdoc.createElement("inputs"); inpute.setAttribute("invert", t->getInfo()->getInputInfo()->isInverted()); inpute.setAttribute("any", t->getInfo()->getInputInfo()->getAnyInput()); inpute.setAttribute("default", t->getInfo()->getInputInfo()->isDefault()); outpute = domdoc.createElement("outputs"); inputt = domdoc.createTextNode(t->getInfo()->getInputsStr(NULL)); outputt = domdoc.createTextNode(t->getInfo()->getOutputsStr(NULL)); inpute.appendChild(inputt); outpute.appendChild(outputt); te.appendChild(inpute); te.appendChild(outpute); me.appendChild(te); } } } // Phantom State s = m->getPhantomState(); QListIterator<GTransition*> tit(s->tlist); GState *send; for(;tit.hasNext();) { t = tit.next(); if (!t->isDeleted() && (!onlyselected || t->isSelected() || (t==obj && t!=NULL))) { t->getPos(xpos, ypos); t->getEndPos(endx, endy); t->getCPoint1(c1x, c1y); t->getCPoint2(c2x, c2y); QDomElement te = domdoc.createElement("transition"); te.setAttribute("type", t->getInfo()->getType()); te.setAttribute("xpos", xpos); te.setAttribute("ypos", ypos); te.setAttribute("endx", endx); te.setAttribute("endy", endy); te.setAttribute("c1x", c1x); te.setAttribute("c1y", c1y); te.setAttribute("c2x", c2x); te.setAttribute("c2y", c2y); te.setAttribute("straight", t->isStraight()); send = (GState*)t->getEnd(); if (send && (!onlyselected || send->isSelected())) { QDomElement to = domdoc.createElement("to"); QString sto; sto.sprintf("%d", send->getEncoding()); QDomText tot = domdoc.createTextNode(sto); to.appendChild(tot); te.appendChild(to); } QDomElement inpute, outpute; QDomText inputt, outputt; inpute = domdoc.createElement("inputs"); outpute = domdoc.createElement("outputs"); inputt = domdoc.createTextNode(t->getInfo()->getInputsStr(NULL)); outputt = domdoc.createTextNode(t->getInfo()->getOutputsStr(NULL)); inpute.appendChild(inputt); outpute.appendChild(outputt); te.appendChild(inpute); te.appendChild(outpute); me.appendChild(te); } } return domdoc; }