loupiottes/DMX-2.0/MainWindow.cs
2013-10-02 16:52:47 +00:00

458 lines
No EOL
12 KiB
C#

using System;
using Gtk;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace DMX2
{
public partial class MainWindow: Gtk.Window
{
static MainWindow win;
static object circuitKey = new object();
ListStore lsMatrice = null;
FileInfo conduiteFile=null;
public static MainWindow Win {
get { return win; }
}
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
win=this;
Build ();
MajWidgets();
}
protected void MajWidgets ()
{
if (Conduite.Courante != null) {
//Activation / Désactivation des boutons en fonction de l'état de la counduite courante
showAllAction.Sensitive = universAction.Sensitive = masterScale.Sensitive = seqLinAction.Sensitive = circAction.Sensitive = saveAction.Sensitive = saveAsAction.Sensitive = closeAction.Sensitive = true;
openAction.Sensitive = newAction.Sensitive = false;
this.Title = "DMX 2.0 - " + Conduite.Courante.Name;
ConstruitMatrice();
} else {
showAllAction.Sensitive = universAction.Sensitive = masterScale.Sensitive = seqLinAction.Sensitive = circAction.Sensitive = saveAction.Sensitive = saveAsAction.Sensitive = closeAction.Sensitive = false;
openAction.Sensitive = newAction.Sensitive = true;
this.Title = "DMX 2.0";
}
}
protected void MajCircuits (bool full)
{
// Ajoute une ProgressBar par circuit, met a jour le texte si necessaire
ProgressBar pb;int i = 0;
if (full) {
foreach (var widget in vboxCircuits.Children)
vboxCircuits.Remove (widget);
foreach (var circuit in Conduite.Courante.Circuits) {
vboxCircuits.PackStart (pb=new ProgressBar (),false,false,0);
pb.Text = (++i).ToString() + " - " + circuit.Name;
pb.HeightRequest = 22;
pb.Data[circuitKey] = circuit;
}
vboxCircuits.ShowAll ();
}
foreach (var widget in vboxCircuits.Children) {
pb = (ProgressBar)widget;
pb.Fraction = (double) (pb.Data[circuitKey] as Circuit).ValeurCourante / 255;
}
}
protected void ConstruitMatrice ()
{
var idCol = new Gtk.TreeViewColumn ();
var idCell = new Gtk.CellRendererText ();
idCol.Title = "Num";
idCol.PackStart (idCell, true);
idCol.SetCellDataFunc (idCell, new Gtk.TreeCellDataFunc (RenderMatriceNum));
this.MatriceUI.AppendColumn (idCol);
var nomCol = new Gtk.TreeViewColumn ();
var nomCell = new Gtk.CellRendererText ();
nomCol.Title = "Nom";
nomCol.PackStart (nomCell, true);
nomCol.SetCellDataFunc (nomCell, new Gtk.TreeCellDataFunc (RenderMatriceNom));
nomCell.Editable = true;
nomCell.Edited += OnNomCellEdited;
this.MatriceUI.AppendColumn (nomCol);
var dureeCol = new Gtk.TreeViewColumn ();
var dureeCell = new Gtk.CellRendererText ();
dureeCol.Title = "Durée";
dureeCol.PackStart (dureeCell, true);
dureeCol.SetCellDataFunc (dureeCell, new Gtk.TreeCellDataFunc (RenderMatriceDuree));
dureeCell.Editable = true;
dureeCell.Edited += OnDureeCellEdited;
this.MatriceUI.AppendColumn (dureeCol);
ConstruitMatriceSeqColumns();
lsMatrice = new Gtk.ListStore(typeof (SequenceurMaitre.Ligne));
this.MatriceUI.Model = lsMatrice;
FillMatrice();
}
static object seqkey = new object();
void ConstruitMatriceSeqColumns ()
{
foreach(var c in MatriceUI.Columns)
if(c.CellRenderers[0].Data.ContainsKey(seqkey))
MatriceUI.RemoveColumn(c);
Gtk.TreeViewColumn seqCol;
Gtk.CellRendererText seqCell;
foreach (Sequenceur seq in Conduite.Courante.Sequenceurs) {
seqCol = new TreeViewColumn();
seqCell = new CellRendererText();
seqCol.Title = seq.Name;
seqCol.PackStart(seqCell, true);
seqCol.SetCellDataFunc ( seqCell, new Gtk.TreeCellDataFunc(RenderMatriceSeqVal));
seqCell.Editable = true;
seqCell.Edited += OnSeqValCellEdited;
seqCell.Data[seqkey] = seq;
this.MatriceUI.AppendColumn(seqCol);
}
}
void RenderMatriceNum (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
SequenceurMaitre.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMaitre.Ligne;
(cell as Gtk.CellRendererText).Text = (Conduite.Courante.SequenceurMaitre.Lignes.IndexOf(l)+1).ToString();
}
void RenderMatriceNom (TreeViewColumn tree_column, CellRenderer cell, TreeModel tree_model, TreeIter iter)
{
SequenceurMaitre.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMaitre.Ligne;
(cell as Gtk.CellRendererText).Text = l.Nom;
}
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== 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)
{
SequenceurMaitre.Ligne l = tree_model.GetValue (iter, 0) as SequenceurMaitre.Ligne;
Sequenceur seq = cell.Data[seqkey] as Sequenceur;
(cell as Gtk.CellRendererText).Text = l[seq];
}
void FillMatrice ()
{
lsMatrice.Clear ();
foreach (SequenceurMaitre.Ligne l in Conduite.Courante.SequenceurMaitre.Lignes) {
lsMatrice.AppendValues(l);
}
}
void OnNomCellEdited (object o, EditedArgs args)
{
Gtk.TreeIter iter;
lsMatrice.GetIter (out iter, new Gtk.TreePath (args.Path));
SequenceurMaitre.Ligne l = lsMatrice.GetValue(iter,0) as SequenceurMaitre.Ligne;
l.Nom = args.NewText;
}
void OnDureeCellEdited (object o, EditedArgs args)
{
Gtk.TreeIter iter;
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 = TimeSpan.Zero;
else {
int val;
if(int.TryParse(args.NewText, out val))
l.Duree = TimeSpan.FromMilliseconds(val *100);
}
}
void OnSeqValCellEdited (object o, EditedArgs args)
{
Gtk.CellRendererText cell = o as CellRendererText;
Sequenceur seq = cell.Data[seqkey] as Sequenceur;
Gtk.TreeIter iter;
lsMatrice.GetIter (out iter, new Gtk.TreePath (args.Path));
SequenceurMaitre.Ligne l = lsMatrice.GetValue(iter,0) as SequenceurMaitre.Ligne;
l[seq] = args.NewText;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnQuitActionActivated (object sender, EventArgs e)
{
Application.Quit ();
}
protected void OnCircuitsActionActivated (object sender, EventArgs e)
{
GestionCircuits gc= new GestionCircuits(this);
gc.Run();
gc.Destroy();
NextUpdateFull();
}
protected void OnNewActionActivated (object sender, EventArgs e)
{
var dlg = new Dialog ("Nom ?", this, DialogFlags.Modal); var entry = new Entry ("Conduite sans Nom");
dlg.AddButton (Stock.Ok, ResponseType.Ok).GrabDefault(); dlg.AddButton (Stock.Cancel, ResponseType.Cancel);
dlg.VBox.Add (new Label("Nom de la nouvelle Conduite :")); dlg.VBox.Add (entry); dlg.VBox.ShowAll ();
entry.ActivatesDefault=true;
if ((ResponseType)dlg.Run () == ResponseType.Ok) {
Conduite.Courante = new Conduite ();
Conduite.Courante.Name = entry.Text;
}
MajWidgets();
dlg.Destroy();
}
protected void OnCloseActionActivated (object sender, EventArgs e)
{
Conduite.Courante.Dispose();
Conduite.Courante= null;
MajWidgets();
}
// bonton ajout de sequenceur linéraire
protected void OnSeqLinActionActivated (object sender, EventArgs e)
{
Sequenceur s = new SequenceurLineaire();
Conduite.Courante.AjoutSequenceur(s);
AddSeqUI(s);
ConstruitMatriceSeqColumns();
}
void AddSeqUI (Sequenceur s)
{
VBox newvbox = new VBox();
newvbox.PackStart(s.GetUI(),false,false,0);
onglets.Add (newvbox);
onglets.ShowAll();
}
bool updScheduled=false;
bool fullUpdateFlag=false;
public void NextUpdateFull ()
{
fullUpdateFlag=true;
}
public void ScheduleUpdate ()
{
if (updScheduled) {
//Console.WriteLine("{0} Skip !",DateTime.Now);
return;
}
updScheduled=true;
Gtk.Application.Invoke(new EventHandler(Update));
}
void Update (object sender, EventArgs e)
{
foreach (var notebookvbox in onglets.Children) {
foreach (var sequi in (notebookvbox as VBox).Children) {
(sequi as SequenceurUI).Update (fullUpdateFlag);
}
}
MajCircuits(fullUpdateFlag);
fullUpdateFlag=false;
updScheduled=false;
}
protected void OnMasterScaleValueChanged (object sender, EventArgs e)
{
Conduite.Courante.Master = (int)(masterScale.Value);
}
bool isfullscreen=false;
protected void OnFullscreenAction1Activated (object sender, EventArgs e)
{
if (!isfullscreen) {
Fullscreen ();
isfullscreen = true;
} else {
Unfullscreen();
isfullscreen = false;
}
}
EditionUnivers uDlg = null;
protected void OnUniversActionActivated (object sender, EventArgs e)
{
if (uDlg != null) {
uDlg.Show();
return;
}
uDlg = new EditionUnivers(this);
uDlg.ShowAll();
uDlg.Destroyed += udlgDestroyed;
}
void udlgDestroyed (object sender, EventArgs e)
{
uDlg=null;
}
protected void OnShowAllActionActivated (object sender, EventArgs e)
{
foreach (var notebookvbox in onglets.Children)
notebookvbox.Destroy();
foreach (Sequenceur s in Conduite.Courante.Sequenceurs) {
VBox newvbox = new VBox();
newvbox.PackStart(s.GetUI(),false,false,0);
onglets.Add (newvbox);
onglets.ShowAll();
}
}
protected void OnConnectActionActivated (object sender, EventArgs e)
{
Conduite.Courante.Drivers.Add( new DriverBoitierV1());
}
protected void OnSaveActionActivated (object sender, EventArgs e)
{
if(conduiteFile==null)
SaveFileAs();
else
SaveFile();
}
protected void OnSaveAsActionActivated (object sender, EventArgs e)
{
SaveFileAs();
}
void SaveFileAs ()
{
FileChooserDialog fcd = new FileChooserDialog ("Sauver sous ...", this, FileChooserAction.Save,
"Annuler", ResponseType.Cancel,
"Enregistrer", ResponseType.Accept);
fcd.Filter = new FileFilter ();
fcd.Filter.AddPattern ("*.dmx2");
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;
}
fcd.Destroy();
SaveFile();
}
void SaveFile ()
{
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();
}
}
}