From a25cf04d7e265c3e0faeb738eea005c61d715d6e Mon Sep 17 00:00:00 2001 From: tzim Date: Wed, 2 Oct 2013 15:12:38 +0000 Subject: [PATCH] Ajout Sauvegarde et Ouverture de fichier --- DMX-2.0/Conduite.cs | 86 ++++++++++++++++++++- DMX-2.0/DMX-2.0.csproj | 1 + DMX-2.0/EditionUnivers.cs | 4 - DMX-2.0/MainWindow.cs | 118 +++++++++++++++++++++++------ DMX-2.0/SeqLinUI.cs | 1 + DMX-2.0/Sequenceur.cs | 12 ++- DMX-2.0/SequenceurLineaire.cs | 83 +++++++++++++++++--- DMX-2.0/SequenceurMaitre.cs | 39 ++++++++-- DMX-2.0/UniversDMX.cs | 21 ++++- DMX-2.0/gtk-gui/DMX2.MainWindow.cs | 1 + DMX-2.0/gtk-gui/gui.stetic | 1 + 11 files changed, 313 insertions(+), 54 deletions(-) diff --git a/DMX-2.0/Conduite.cs b/DMX-2.0/Conduite.cs index 1bc5932..625ca6a 100644 --- a/DMX-2.0/Conduite.cs +++ b/DMX-2.0/Conduite.cs @@ -64,6 +64,12 @@ namespace DMX2 } } + public Circuit GetCircuitByID (int i) + { + foreach(Circuit c in circuits) + if(c.ID == i) return c; + return null; + } public string Name { get { @@ -184,19 +190,69 @@ namespace DMX2 public XmlDocument Save () { - XmlDocument doc = new XmlDocument(); + XmlDocument xmlDoc = new XmlDocument (); + XmlElement xmlRoot = xmlDoc.CreateElement ("Conduite"); + xmlDoc.AppendChild (xmlRoot); + xmlRoot.SetAttribute ("nom", this.Name); + XmlElement xmlCircuits = xmlDoc.CreateElement ("Circuits"); + xmlRoot.AppendChild (xmlCircuits); - return doc; + foreach (Circuit c in circuits) { + c.Save(xmlCircuits); + } + + XmlElement xmlSequenceurs = xmlDoc.CreateElement("Sequenceurs"); + xmlRoot.AppendChild(xmlSequenceurs); + + foreach(Sequenceur seq in sequenceurs) + { + seq.Save(xmlSequenceurs); + } + + XmlElement xmlUniversList = xmlDoc.CreateElement("ListeUnivers"); + xmlRoot.AppendChild(xmlUniversList); + + foreach(UniversDMX univ in univers) + { + univ.Save(xmlUniversList); + } + + seqmaitre.Save(xmlRoot); + + return xmlDoc; } public static Conduite Load (XmlDocument doc) { - return null; + Conduite cond = new Conduite (); + cond.LoadDoc (doc); + return cond; +// cond.Dispose(); +// return null; + } + + private void LoadDoc (XmlDocument doc) + { + XmlElement root = doc.DocumentElement; + + _name = root.Attributes ["nom"].Value; + + foreach (var xc in root["Circuits"].ChildNodes) { + Circuit c = Circuit.Load (xc as XmlElement); + if (c != null) + circuits.Add (c); + } + + foreach (var xs in root["Sequenceurs"].ChildNodes) { + Sequenceur s = Sequenceur.Load (this,xs as XmlElement); + if(s!=null)sequenceurs.Add (s); + } + + } - #endregion @@ -256,8 +312,30 @@ namespace DMX2 set { _curval = value; } + } + + public void Save (XmlElement parent) + { + XmlElement el= parent.OwnerDocument.CreateElement ("Circuit"); + parent.AppendChild(el); + el.SetAttribute("ID",id.ToString()); + el.SetAttribute("name",name); + el.SetAttribute("shortName",shortName); + } + + public static Circuit Load (XmlElement xc) + { + if(xc.Name!="Circuit") throw new ErreurLectureFichier(" attendu."); + return new Circuit(xc); } + private Circuit(XmlElement xc) + { + id = int.Parse(xc.GetAttribute("ID")); + maxid = Math.Max (maxid,id+1); + name = xc.GetAttribute("name"); + shortName = xc.GetAttribute("shortName"); + } } diff --git a/DMX-2.0/DMX-2.0.csproj b/DMX-2.0/DMX-2.0.csproj index a81495c..96b831a 100644 --- a/DMX-2.0/DMX-2.0.csproj +++ b/DMX-2.0/DMX-2.0.csproj @@ -107,6 +107,7 @@ + diff --git a/DMX-2.0/EditionUnivers.cs b/DMX-2.0/EditionUnivers.cs index 7902b8f..2afa698 100644 --- a/DMX-2.0/EditionUnivers.cs +++ b/DMX-2.0/EditionUnivers.cs @@ -304,9 +304,5 @@ namespace DMX2 MajListeDimmer (); } } - - - - } } diff --git a/DMX-2.0/MainWindow.cs b/DMX-2.0/MainWindow.cs index 6bc4352..5d7cd59 100644 --- a/DMX-2.0/MainWindow.cs +++ b/DMX-2.0/MainWindow.cs @@ -139,8 +139,8 @@ namespace DMX2 void RenderMatriceDuree (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) { SequenceurMaitre.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMaitre.Ligne; - if (l.Duree==-1) (cell as Gtk.CellRendererText).Text = string.Empty; - else (cell as Gtk.CellRendererText).Text = l.Duree.ToString(); + if (l.Duree== TimeSpan.Zero) (cell as Gtk.CellRendererText).Text = string.Empty; + else (cell as Gtk.CellRendererText).Text = (l.Duree.TotalMilliseconds /100).ToString(); } void RenderMatriceSeqVal (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter) @@ -172,11 +172,11 @@ namespace DMX2 lsMatrice.GetIter (out iter, new Gtk.TreePath (args.Path)); SequenceurMaitre.Ligne l = lsMatrice.GetValue (iter, 0) as SequenceurMaitre.Ligne; if (args.NewText.Length == 0) - l.Duree = -1; + l.Duree = TimeSpan.Zero; else { int val; if(int.TryParse(args.NewText, out val)) - l.Duree=val; + l.Duree = TimeSpan.FromMilliseconds(val *100); } } @@ -355,36 +355,104 @@ namespace DMX2 void SaveFileAs () { - FileChooserDialog fcd = new FileChooserDialog("Sauver sous ...",this,FileChooserAction.Save, - "Annuler",ResponseType.Cancel, - "Ouvrir",ResponseType.Accept); + FileChooserDialog fcd = new FileChooserDialog ("Sauver sous ...", this, FileChooserAction.Save, + "Annuler", ResponseType.Cancel, + "Enregistrer", ResponseType.Accept); - fcd.Filter = new FileFilter(); + fcd.Filter = new FileFilter (); - fcd.Filter.AddPattern("*.dmx2"); + fcd.Filter.AddPattern ("*.dmx2"); - if ((ResponseType)fcd.Run() == ResponseType.Cancel) - { - fcd.Destroy(); - return; + bool ok = false; + + while (!ok) { + + if ((ResponseType)fcd.Run () == ResponseType.Cancel) { + fcd.Destroy (); + return; + } + string file = fcd.Filename; + + if (!file.EndsWith (".dmx2")) + file += ".dmx2"; + + conduiteFile = new FileInfo(file); + + if(conduiteFile.Exists) + { + MessageDialog msg = new MessageDialog(fcd,DialogFlags.Modal, + MessageType.Warning, + ButtonsType.YesNo, + "Le fichier existe déja. \nVoulez vous écraser le fichier existant ?"); + if ((ResponseType)msg.Run () == ResponseType.Yes)ok=true; + msg.Destroy(); + + } + else ok=true; } - string file = fcd.Filename; - - if(!file.EndsWith(".dmx2")) - file+=".dmx2"; - - conduiteFile = new FileInfo(fcd.Filename); - if (conduiteFile.Exists); - - fcd.Destroy(); - + SaveFile(); } - void SaveFile() + void SaveFile () { - if(conduiteFile==null)return; + if (conduiteFile == null) + return; + + using (FileStream stream = conduiteFile.Open(FileMode.Create,FileAccess.Write)) { + System.Xml.XmlDocument doc = Conduite.Courante.Save (); + doc.Save (stream); + stream.Close (); + } + + } + + protected void OnOpenActionActivated (object sender, EventArgs e) + { + FileChooserDialog fcd = new FileChooserDialog ("Sauver sous ...", this, FileChooserAction.Open, + "Annuler", ResponseType.Cancel, + "Ouvrir", ResponseType.Accept); + + fcd.Filter = new FileFilter (); + fcd.Filter.AddPattern ("*.dmx2"); + if ((ResponseType)fcd.Run () == ResponseType.Cancel) { + fcd.Destroy (); + return; + } + + FileInfo openFile = new FileInfo (fcd.Filename); + fcd.Destroy (); + if (!openFile.Exists) + return; + + System.Xml.XmlDocument doc; + + try { + using(FileStream stream = openFile.OpenRead()) + { + doc = new System.Xml.XmlDocument(); + doc.Load(stream); + stream.Close(); + } + + Conduite cond = Conduite.Load(doc); + if (cond==null) + { + // TODO Message erreur au chargement + return; + } + + Conduite.Courante = cond; + + conduiteFile = openFile; + + } catch (IOException) { + } + + MajWidgets(); + NextUpdateFull(); } + } } \ No newline at end of file diff --git a/DMX-2.0/SeqLinUI.cs b/DMX-2.0/SeqLinUI.cs index 03acffc..27217fb 100644 --- a/DMX-2.0/SeqLinUI.cs +++ b/DMX-2.0/SeqLinUI.cs @@ -74,6 +74,7 @@ namespace DMX2 dureeCell.Edited += EditDuree; transCell.Edited += EditTrans; + UpdListeEffets(); #endregion } diff --git a/DMX-2.0/Sequenceur.cs b/DMX-2.0/Sequenceur.cs index a31654c..ff766dd 100644 --- a/DMX-2.0/Sequenceur.cs +++ b/DMX-2.0/Sequenceur.cs @@ -28,6 +28,10 @@ namespace DMX2 get { return id; } + protected set { + id=value; + idmax = Math.Max(id+1,idmax); + } } string name; @@ -45,8 +49,12 @@ namespace DMX2 public abstract int ValeurCircuit(Circuit c); public abstract void Tick(TimeSpan time); - public static Sequenceur Load(XmlElement el) + public static Sequenceur Load (Conduite conduite, XmlElement el) { + switch (el.Name) { + case "SequenceurLineaire": + return SequenceurLineaire.Load(conduite, el); + } return null; } @@ -54,7 +62,7 @@ namespace DMX2 { } - public abstract XmlElement Save (); + public abstract void Save (XmlElement parent); diff --git a/DMX-2.0/SequenceurLineaire.cs b/DMX-2.0/SequenceurLineaire.cs index f9bd676..b925d9c 100644 --- a/DMX-2.0/SequenceurLineaire.cs +++ b/DMX-2.0/SequenceurLineaire.cs @@ -11,6 +11,7 @@ namespace DMX2 string _nom; + public Effet (string nom, Dictionary valeurs, TimeSpan duree, TimeSpan transition) { _nom = nom; @@ -63,6 +64,37 @@ namespace DMX2 _transition = value; } } + public void Save (System.Xml.XmlElement parent) + { + System.Xml.XmlElement el = parent.OwnerDocument.CreateElement ("Effet"); + System.Xml.XmlElement xmlVal; + parent.AppendChild (el); + el.SetAttribute("nom",_nom); + el.SetAttribute ("duree", _duree.TotalMilliseconds.ToString ()); + el.SetAttribute ("transition", _transition.TotalMilliseconds.ToString ()); + foreach (var valeur in _valeurs) { + xmlVal = parent.OwnerDocument.CreateElement("Valeur"); + el.AppendChild(xmlVal); + xmlVal.SetAttribute("circuit",valeur.Key.ID.ToString()); + xmlVal.SetAttribute("valeur",valeur.Value.ToString()); + } + } + public static Effet Load (Conduite conduite, System.Xml.XmlElement el) + { + Dictionary valeurs = new Dictionary (); + foreach (var xv in el.GetElementsByTagName("Valeur")) { + System.Xml.XmlElement xval = xv as System.Xml.XmlElement; + valeurs.Add( + conduite.GetCircuitByID(int.Parse(xval.GetAttribute("circuit"))), + int.Parse(xval.GetAttribute("valeur")) + ); + } + return new Effet( el.GetAttribute("nom"),valeurs, + TimeSpan.FromMilliseconds(Double.Parse(el.GetAttribute("duree"))), + TimeSpan.FromMilliseconds(Double.Parse(el.GetAttribute("transition"))) + ); + } + } TimeSpan timeStamp = TimeSpan.Zero; @@ -91,13 +123,6 @@ namespace DMX2 effetcourrant = new Effet ("",valeurscourantes , TimeSpan.Zero, TimeSpan.Zero); } - public SequenceurLineaire (int id) :base (id) - { - - } - - - public TimeSpan TimeStamp { get { return timeStamp; @@ -305,9 +330,25 @@ namespace DMX2 return index +1; } - public override System.Xml.XmlElement Save () + public override void Save (System.Xml.XmlElement parent) { - throw new System.NotImplementedException (); + System.Xml.XmlElement el = parent.OwnerDocument.CreateElement ("SequenceurLineaire"); + System.Xml.XmlElement xmlC; + + parent.AppendChild (el); + el.SetAttribute ("id", ID.ToString ()); + el.SetAttribute ("name", Name); + el.SetAttribute ("master", master.ToString ()); + + foreach (Circuit c in circuitsSeq) { + el.AppendChild(xmlC = parent.OwnerDocument.CreateElement ("CircuitSeq")); + xmlC.SetAttribute("id",c.ID.ToString()); + } + + foreach (Effet ef in effets) { + ef.Save(el); + } + } @@ -326,5 +367,29 @@ namespace DMX2 { ui = null; } + + public static SequenceurLineaire Load (Conduite conduite, System.Xml.XmlElement el) + { + SequenceurLineaire seq = new SequenceurLineaire(); + seq.LoadSeq(conduite,el); + return seq; + } + + private void LoadSeq (Conduite conduite, System.Xml.XmlElement el) + { + ID = int.Parse (el.GetAttribute ("id")); + Name = el.GetAttribute ("name"); + master = int.Parse (el.GetAttribute ("master")); + + foreach (var xc in el.GetElementsByTagName("CircuitSeq")) { + System.Xml.XmlElement xcir = xc as System.Xml.XmlElement; + Circuit c = conduite.GetCircuitByID (int.Parse (xcir.GetAttribute ("id"))); + circuitsSeq.Add (c); + AjouteCircuit (c); + } + + foreach (var xe in el.GetElementsByTagName("Effet")) + effets.Add(Effet.Load(conduite,xe as System.Xml.XmlElement)); + } } } diff --git a/DMX-2.0/SequenceurMaitre.cs b/DMX-2.0/SequenceurMaitre.cs index 4514d79..6730f19 100644 --- a/DMX-2.0/SequenceurMaitre.cs +++ b/DMX-2.0/SequenceurMaitre.cs @@ -9,7 +9,7 @@ namespace DMX2 public class Ligne { public Ligne(){} string nom; - int duree = -1; + TimeSpan duree = TimeSpan.Zero; Dictionary data = new Dictionary(); public string Nom { @@ -21,7 +21,7 @@ namespace DMX2 } } - public int Duree { + public TimeSpan Duree { get { return duree; } @@ -37,9 +37,28 @@ namespace DMX2 return value; } set { - data[index] = value; + if(value.Length==0) if(data.ContainsKey(index)) + data.Remove(index); + else + data[index] = value; } } + public void Save (XmlElement parent) + { + XmlElement el = parent.OwnerDocument.CreateElement ("Ligne"); + parent.AppendChild (el); + + el.SetAttribute ("nom", nom); + el.SetAttribute ("duree", duree.TotalMilliseconds.ToString ()); + + XmlElement xmlSeq; + foreach (var val in data) { + el.AppendChild(xmlSeq=parent.OwnerDocument.CreateElement ("data")); + xmlSeq.SetAttribute("seq",val.Key.ID.ToString()); + xmlSeq.SetAttribute("val",val.Value); + } + } + } List lignes = new List(); @@ -57,16 +76,20 @@ namespace DMX2 lignes.Add(new Ligne()); } - public XmlElement Save () + public void Save (XmlElement parent) { - return null; + XmlElement el = parent.OwnerDocument.CreateElement("SequenceurMaitre"); + parent.AppendChild(el); + + foreach(Ligne l in lignes) + l.Save(el); + } + public static SequenceurMaitre Load (XmlElement doc) { return null; } - - - } + } } diff --git a/DMX-2.0/UniversDMX.cs b/DMX-2.0/UniversDMX.cs index a871a0f..abb9ce4 100644 --- a/DMX-2.0/UniversDMX.cs +++ b/DMX-2.0/UniversDMX.cs @@ -84,9 +84,26 @@ namespace DMX2 } } - public XmlElement Save () + public void Save (XmlElement parent) { - return null; + XmlElement el = parent.OwnerDocument.CreateElement ("UniversDMX"); + parent.AppendChild (el); + + el.SetAttribute ("nom", Nom); + XmlElement xmlDim; int dim; + for(dim=0; dim < _dimmers.Length; dim++) + { + if(_dimmers[dim].circuitAssocié!=null) + { + el.AppendChild(xmlDim = el.OwnerDocument.CreateElement("Dimmer")); + xmlDim.SetAttribute("num",dim.ToString()); + xmlDim.SetAttribute("circuit",_dimmers[dim].circuitAssocié.ID.ToString()); + xmlDim.SetAttribute("ft",_dimmers[dim].fonctionTransfert.ToString()); + xmlDim.SetAttribute("param1",_dimmers[dim].param1.ToString() ); + xmlDim.SetAttribute("param2",_dimmers[dim].param2.ToString() ); + } + + } } public static UniversDMX Load (XmlElement doc) diff --git a/DMX-2.0/gtk-gui/DMX2.MainWindow.cs b/DMX-2.0/gtk-gui/DMX2.MainWindow.cs index b45e99f..ca97e7b 100644 --- a/DMX-2.0/gtk-gui/DMX2.MainWindow.cs +++ b/DMX-2.0/gtk-gui/DMX2.MainWindow.cs @@ -375,6 +375,7 @@ namespace DMX2 this.DefaultHeight = 709; this.Show (); this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent); + this.openAction.Activated += new global::System.EventHandler (this.OnOpenActionActivated); this.saveAction.Activated += new global::System.EventHandler (this.OnSaveActionActivated); this.saveAsAction.Activated += new global::System.EventHandler (this.OnSaveAsActionActivated); this.quitAction.Activated += new global::System.EventHandler (this.OnQuitActionActivated); diff --git a/DMX-2.0/gtk-gui/gui.stetic b/DMX-2.0/gtk-gui/gui.stetic index 1c4b678..91aba44 100644 --- a/DMX-2.0/gtk-gui/gui.stetic +++ b/DMX-2.0/gtk-gui/gui.stetic @@ -69,6 +69,7 @@ _Ouvrir _Ouvrir gtk-open + Action