[chronojump] Encoder: Done load/save/update stream and curve



commit f00a6c7f06b41af2dc45a6794969ed8b7546d184
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed May 30 21:42:24 2012 +0200

    Encoder: Done load/save/update stream and curve

 glade/chronojump.glade   |    4 +-
 src/encoder.cs           |   13 +++++---
 src/gui/encoder.cs       |   79 +++++++++++++++++++++++++++++++++++++---------
 src/gui/genericWindow.cs |   35 ++++++++++++++++++++-
 src/sqlite/encoder.cs    |   40 +++++++++++++++++++++--
 src/sqlite/main.cs       |   21 ++++++++++++
 src/util.cs              |   19 ++++++++---
 7 files changed, 180 insertions(+), 31 deletions(-)
---
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 170c899..c9a038c 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -24058,7 +24058,7 @@ Evaluator can use real name or nickname.</property>
                                                           </packing>
                                                         </child>
                                                         <child>
-                                                          <widget class="GtkSpinButton" id="spin_encoder_jump_limit">
+                                                          <widget class="GtkSpinButton" id="spin_encoder_jump_weight">
                                                             <property name="visible">True</property>
                                                             <property name="sensitive">False</property>
                                                             <property name="can_focus">True</property>
@@ -24078,7 +24078,7 @@ Evaluator can use real name or nickname.</property>
                                                           </packing>
                                                         </child>
                                                         <child>
-                                                          <widget class="GtkSpinButton" id="spin_encoder_bar_limit">
+                                                          <widget class="GtkSpinButton" id="spin_encoder_bar_weight">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">True</property>
                                                             <property name="invisible_char">â</property>
diff --git a/src/encoder.cs b/src/encoder.cs
index 7ff38a0..0fc461c 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -213,21 +213,24 @@ public class EncoderSQL
 			ecconLong = Catalog.GetString("Eccentric-concentric");
 	}
 	
-	private string getDate() {
+	public string GetDate(bool pretty) {
 		int pointPos = name.LastIndexOf('.');
 		int dateLength = 19; //YYYY-MM-DD_hh-mm-ss
 		string date = name.Substring(pointPos - dateLength, dateLength);
-		string [] dateParts = date.Split(new char[] {'_'});
-		return dateParts[0] + " " + dateParts[1].Replace('-',':');
+		if(pretty) {
+			string [] dateParts = date.Split(new char[] {'_'});
+			date = dateParts[0] + " " + dateParts[1].Replace('-',':');
+		}
+		return date;
 	}
 
 	public string [] ToStringArray () {
-		string [] str = new String [5];
+		string [] str = new String [6];
 		str[0] = uniqueID;
 		str[1] = type;
 		str[2] = ecconLong;
 		str[3] = extraWeight;
-		str[4] = getDate();
+		str[4] = GetDate(true);
 		str[5] = description;
 		return str;
 	}
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 102cf7c..4b136d1 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -30,8 +30,8 @@ using Mono.Unix;
 
 public partial class ChronoJumpWindow 
 {
-	[Widget] Gtk.SpinButton spin_encoder_bar_limit;
-	[Widget] Gtk.SpinButton spin_encoder_jump_limit;
+	[Widget] Gtk.SpinButton spin_encoder_bar_weight;
+	[Widget] Gtk.SpinButton spin_encoder_jump_weight;
 	[Widget] Gtk.SpinButton spin_encoder_smooth;
 
 	[Widget] Gtk.Button button_encoder_capture;
@@ -80,6 +80,7 @@ public partial class ChronoJumpWindow
 	private string encoderAnalysis="powerBars";
 	private string ecconLast;
 	private string encoderTimeStamp;
+	private string encoderStreamUniqueID;
 	enum encoderModes { CAPTURE, ANALYZE }
 	
 	//TODO: store encoder data: auto save, and show on a treeview.
@@ -102,12 +103,12 @@ public partial class ChronoJumpWindow
 	}
 
 	private void on_radiobutton_encoder_capture_bar_toggled (object obj, EventArgs args) {
-		spin_encoder_bar_limit.Sensitive = true;
-		spin_encoder_jump_limit.Sensitive = false;
+		spin_encoder_bar_weight.Sensitive = true;
+		spin_encoder_jump_weight.Sensitive = false;
 	}
 	private void on_radiobutton_encoder_capture_jump_toggled (object obj, EventArgs args) {
-		spin_encoder_bar_limit.Sensitive = false;
-		spin_encoder_jump_limit.Sensitive = true;
+		spin_encoder_bar_weight.Sensitive = false;
+		spin_encoder_jump_weight.Sensitive = true;
 	}
 
 	private void on_radiobutton_encoder_eccon_toggled (object obj, EventArgs args) {
@@ -181,6 +182,7 @@ public partial class ChronoJumpWindow
 		Util.RunPythonEncoder(Constants.EncoderScriptCapture, es, true);
 
 		encoderTimeStamp = UtilDate.ToFile(DateTime.Now);
+		encoderStreamUniqueID = "-1"; //mark to know that there's no ID for this until it's saved on database
 
 		encoderThreadStart(encoderModes.CAPTURE);
 	}
@@ -244,7 +246,7 @@ public partial class ChronoJumpWindow
 		
 	void on_button_encoder_load_stream_clicked (object o, EventArgs args) 
 	{
-		ArrayList data = SqliteEncoder.SelectStreams(false, currentPerson.UniqueID, currentSession.UniqueID);
+		ArrayList data = SqliteEncoder.SelectStreams(false, -1, currentPerson.UniqueID, currentSession.UniqueID);
 
 		ArrayList dataPrint = new ArrayList();
 		foreach(EncoderSQL es in data) {
@@ -265,6 +267,36 @@ public partial class ChronoJumpWindow
 					currentPerson.Name), Constants.GenericWindowShow.TREEVIEW);
 
 		genericWin.SetTreeview(columnsString, dataPrint);
+		genericWin.SetButtonAcceptLabel(Catalog.GetString("Load"));
+		genericWin.Button_accept.Clicked += new EventHandler(on_encoder_load_stream_accepted);
+	}
+	
+	protected void on_encoder_load_stream_accepted (object o, EventArgs args)
+	{
+		genericWin.Button_accept.Clicked -= new EventHandler(on_encoder_load_stream_accepted);
+		int uniqueID = genericWin.TreeviewSelectedRowID();
+
+		ArrayList data = SqliteEncoder.SelectStreams(false, uniqueID, 
+				currentPerson.UniqueID, currentSession.UniqueID);
+
+		foreach(EncoderSQL es in data) {	//it will run only one time
+			Util.CopyEncoderDataToTemp(es.url, es.name);
+			if(es.type.EndsWith("BAR")) { //BAR or JUMP
+				radiobutton_encoder_capture_bar.Active = true;
+				spin_encoder_bar_weight.Value = Convert.ToInt32(es.extraWeight);
+			} else {
+				radiobutton_encoder_capture_bar.Active = false;
+				spin_encoder_jump_weight.Value = Convert.ToInt32(es.extraWeight);
+			}
+			radiobutton_encoder_concentric.Active = es.eccon == "c";
+			spin_encoder_capture_min_height.Value = es.minHeight;
+			spin_encoder_smooth.Value = es.smooth;
+			encoderTimeStamp = es.GetDate(false); 
+			encoderStreamUniqueID = es.uniqueID;
+		}
+	
+		//force a recalculate
+		on_button_encoder_recalculate_clicked (o, args); 
 	}
 
 	private EncoderCurve getCurve(int selectedID) 
@@ -322,7 +354,6 @@ public partial class ChronoJumpWindow
 			feedback = string.Format(Catalog.GetString("Curve {0} saved"), curveNum);
 		} else {	//(button == button_encoder_save_stream) {
 			type = "stream";
-			feedback = Catalog.GetString("Stream saved");
 		}
 		
 		string desc = Util.RemoveTildeAndColonAndDot(entry_encoder_capture_comment.Text.ToString());
@@ -342,12 +373,13 @@ public partial class ChronoJumpWindow
 			}
 
 			//Log.WriteLine(curveStart + "->" + duration);
+			int curveIDMax = Sqlite.Max(Constants.EncoderTable, "uniqueID", false);
 			fileSaved = Util.EncoderSaveCurve(Util.GetEncoderDataTempFileName(), curveStart, duration,
 					currentSession.UniqueID, currentPerson.UniqueID, 
-					currentPerson.Name, encoderTimeStamp);
+					currentPerson.Name, encoderTimeStamp, curveIDMax);
 			path = Util.GetEncoderSessionDataCurveDir(currentSession.UniqueID);
 		} else { //stream
-			fileSaved = Util.CopyTempEncoderData (currentSession.UniqueID, currentPerson.UniqueID, 
+			fileSaved = Util.CopyTempToEncoderData (currentSession.UniqueID, currentPerson.UniqueID, 
 					currentPerson.Name, encoderTimeStamp);
 			path = Util.GetEncoderSessionDataStreamDir(currentSession.UniqueID);
 		}
@@ -356,9 +388,13 @@ public partial class ChronoJumpWindow
 			type += "BAR";
 		else
 			type += "JUMP";
+		
+		string myID = "-1";	
+		if(button == button_encoder_save_stream)
+			myID = encoderStreamUniqueID;
 
 		EncoderSQL eSQL = new EncoderSQL(
-				"-1", 
+				myID, 
 				currentPerson.UniqueID, currentSession.UniqueID, 
 				fileSaved,		//to know date do: select substr(name,-23,19) from encoder;
 				path,			//url
@@ -370,8 +406,21 @@ public partial class ChronoJumpWindow
 				(double) spin_encoder_smooth.Value,
 				desc);
 		
-				//Adding on SQL
-		SqliteEncoder.Insert(false, eSQL);
+		//if is a stream that we just loaded, then don't insert, do an update
+		//we know it because encoderUniqueID is != than "-1" if we loaded something from database
+		//on curves, always insert, because it can be done with different smoothing, different params
+		if(myID == "-1") {
+			myID = SqliteEncoder.Insert(false, eSQL).ToString(); //Adding on SQL
+			if(button == button_encoder_save_stream) {
+				encoderStreamUniqueID = myID;
+				feedback = Catalog.GetString("Stream saved");
+			}
+		}
+		else {
+			//only stream is updated
+			SqliteEncoder.Update(false, eSQL); //Adding on SQL
+			feedback = Catalog.GetString("Stream updated");
+		}
 		
 		encoder_pulsebar_capture.Text = feedback;
 	}
@@ -447,9 +496,9 @@ public partial class ChronoJumpWindow
 	private string findMass(bool includePerson) {
 		double mass = 0;
 		if(radiobutton_encoder_capture_bar.Active)
-			mass = spin_encoder_bar_limit.Value;
+			mass = spin_encoder_bar_weight.Value;
 		else {
-			mass = spin_encoder_jump_limit.Value;
+			mass = spin_encoder_jump_weight.Value;
 			if(includePerson)
 				mass += Convert.ToDouble(label_encoder_person_weight.Text);
 		}
diff --git a/src/gui/genericWindow.cs b/src/gui/genericWindow.cs
index b3c1933..b5b4e29 100644
--- a/src/gui/genericWindow.cs
+++ b/src/gui/genericWindow.cs
@@ -117,6 +117,9 @@ public class GenericWindow
 		
 		foreach (string [] line in data) 
 			store.AppendValues (line);
+		
+		setButtonAcceptSensitive(false);
+		treeview.CursorChanged += on_treeview_cursor_changed; 
 	}
 	
 	private TreeStore getStore (int columns)
@@ -132,21 +135,51 @@ public class GenericWindow
 	
 	private void prepareHeaders(string [] columnsString) 
 	{
+		treeviewRemoveColumns();
 		treeview.HeadersVisible=true;
 		int i=0;
 		bool visible = false;
 		foreach(string myCol in columnsString) {
 			UtilGtk.CreateCols(treeview, store, myCol, i++, visible);
-			if(i == 1)
+			if(i == 1)	//first columns: ID, is hidden
 				store.SetSortFunc (0, UtilGtk.IdColumnCompare);
 			visible = true;
 		}
 	}
 	
+	private void treeviewRemoveColumns() {
+		Gtk.TreeViewColumn [] myColumns = treeview.Columns;
+		foreach (Gtk.TreeViewColumn column in myColumns) {
+			treeview.RemoveColumn (column);
+		}
+	}
+	
+	private void on_treeview_cursor_changed (object o, EventArgs args) 
+	{
+		TreeIter iter = new TreeIter();
+		TreeModel myModel = treeview.Model;
+		if (treeview.Selection.GetSelected (out myModel, out iter)) 
+			setButtonAcceptSensitive(true);
+		else
+			setButtonAcceptSensitive(false);
+	}
+	
+	public int TreeviewSelectedRowID() {
+		TreeIter iter = new TreeIter();
+		TreeModel myModel = treeview.Model;
+		if (treeview.Selection.GetSelected (out myModel, out iter)) 
+			return Convert.ToInt32(treeview.Model.GetValue (iter, 0));
+		else
+			return 0;
+	}
 	
 	public void SetButtonAcceptLabel(string str) {
 		button_accept.Label=str;
 	}
+	
+	private void setButtonAcceptSensitive(bool show) {
+		button_accept.Sensitive=show;
+	}
 
 
 	protected void on_button_cancel_clicked (object o, EventArgs args)
diff --git a/src/sqlite/encoder.cs b/src/sqlite/encoder.cs
index 8d073aa..022ab39 100644
--- a/src/sqlite/encoder.cs
+++ b/src/sqlite/encoder.cs
@@ -61,7 +61,6 @@ class SqliteEncoder : Sqlite
 	 */
 	
 	public static int Insert(bool dbconOpened, EncoderSQL es)
-//			string uniqueID, int personID, int sessionID, string name, string url, string type, string extraWeight, string eccon, int time, int minHeight, double smooth, string description)
 	{
 		if(! dbconOpened)
 			dbcon.Open();
@@ -90,14 +89,49 @@ class SqliteEncoder : Sqlite
 		return myLast;
 	}
 	
-	public static ArrayList SelectStreams (bool dbconOpened, int personID, int sessionID)
+	public static void Update(bool dbconOpened, EncoderSQL es)
 	{
 		if(! dbconOpened)
 			dbcon.Open();
 
+		if(es.uniqueID == "-1")
+			es.uniqueID = "NULL";
+
+		dbcmd.CommandText = "UPDATE " + Constants.EncoderTable + " SET " +
+				" personID = " + es.personID +
+				", sessionID = " + es.sessionID +
+				", name = '" + es.name +
+				"', url = '" + es.url +
+				"', type = '" + es.type +
+				"', extraWeight = '" + es.extraWeight +
+				"', eccon = '" + es.eccon +
+				"', time = " + es.time +
+				", minHeight = " + es.minHeight +
+				", smooth = " + Util.ConvertToPoint(es.smooth) +
+				", description = '" + es.description + "'" +
+				" WHERE uniqueID == " + es.uniqueID ;
+
+		Log.WriteLine(dbcmd.CommandText.ToString());
+		dbcmd.ExecuteNonQuery();
+
+		if(! dbconOpened)
+			dbcon.Close();
+	}
+	
+	
+	public static ArrayList SelectStreams (bool dbconOpened, int uniqueID, int personID, int sessionID)
+	{
+		if(! dbconOpened)
+			dbcon.Open();
+
+		string uniqueIDStr = "";
+		if(uniqueID != -1)
+			uniqueIDStr = " AND uniqueID = " + uniqueID;
+
+
 		dbcmd.CommandText = "SELECT * FROM " + Constants.EncoderTable + 
 			" WHERE personID = " + personID + " AND sessionID = " + sessionID +
-			" AND SUBSTR(type,1,6)='stream'";
+			" AND SUBSTR(type,1,6)='stream'" + uniqueIDStr;
 		
 		SqliteDataReader reader;
 		reader = dbcmd.ExecuteReader();
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 000da0e..6f5e4ed 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -1975,6 +1975,27 @@ Console.WriteLine("5" + tableName);
 		return myReaderStr;
 	}
 
+	public static int Max (string tableName, string column, bool dbconOpened)
+	{
+		if(!dbconOpened)
+			dbcon.Open();
+
+		dbcmd.CommandText = "SELECT MAX(" + column + ") FROM " + tableName ;
+		Log.WriteLine(dbcmd.CommandText.ToString());
+		dbcmd.ExecuteNonQuery();
+		SqliteDataReader reader;
+		reader = dbcmd.ExecuteReader();
+		
+		int myReturn = 0;
+		if(reader.Read()) 
+			myReturn = Convert.ToInt32(reader[0].ToString());
+		reader.Close();
+
+		if(!dbconOpened)
+			dbcon.Close();
+		return myReturn;
+	}
+
 	public static int Count (string tableName, bool dbconOpened)
 	{
 		if(!dbconOpened)
diff --git a/src/util.cs b/src/util.cs
index 90f1e77..c444f58 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -854,8 +854,8 @@ public class Util
 		return Path.Combine(Path.GetTempPath(), Constants.EncoderGraphTemp);
 	}
 
-//	public static void MoveTempEncoderData(int sessionID, int uniqueID) {
-	public static string CopyTempEncoderData(int sessionID, int uniqueID, string personName, string timeStamp) 
+//	public static void MoveTempToEncoderData(int sessionID, int uniqueID) {
+	public static string CopyTempToEncoderData(int sessionID, int uniqueID, string personName, string timeStamp) 
 	{
 		string fileName="";
 		if(File.Exists(GetEncoderDataTempFileName())) {
@@ -868,12 +868,20 @@ public class Util
 				
 				File.Copy(GetEncoderDataTempFileName(), 
 						GetEncoderSessionDataStreamDir(sessionID) + 
-						Path.DirectorySeparatorChar + fileName);
+						Path.DirectorySeparatorChar + fileName, true);
 //			}
 		}
 		return fileName;
 	}
 	
+	public static void CopyEncoderDataToTemp(string url, string fileName)
+	{
+		string origin = url + Path.DirectorySeparatorChar + fileName;
+		string dest = GetEncoderDataTempFileName();
+		if(File.Exists(origin)) 
+			File.Copy(origin, dest, true);
+	}
+	
 	
 	/********** end of encoder paths ************/
 
@@ -1074,7 +1082,7 @@ public class Util
 	}
 
 	public static string EncoderSaveCurve(string fileNameStream, int start, int duration, 
-			int sessionID, int uniqueID, string personName, string timeStamp) 
+			int sessionID, int uniqueID, string personName, string timeStamp, int curveIDMax) 
 	{
 		string contents = ReadFile(fileNameStream);
 		string [] startAndDuration = encoderFindPos(contents, start, duration);
@@ -1083,7 +1091,8 @@ public class Util
 				Convert.ToInt32(startAndDuration[0]), 
 				Convert.ToInt32(startAndDuration[1])-1); //-1 is for not ending file with a comma
 			
-		string fileCurve = uniqueID.ToString() + "-" + personName + "-" + timeStamp + ".txt";
+		string fileCurve = uniqueID.ToString() + "-" + personName + "-" + 
+			(++ curveIDMax).ToString() + "-" + timeStamp + ".txt";
 		string fileCurveFull = GetEncoderSessionDataCurveDir(sessionID) + Path.DirectorySeparatorChar + fileCurve;
 		
 		TextWriter writer = File.CreateText(fileCurveFull);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]