[chronojump] Encoder: save row working



commit 333e89b3e3f0cc0cc1cf00887621094a1319c467
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue May 29 04:00:39 2012 +0200

    Encoder: save row working

 encoder/graph.R    |   10 ++++++++++
 src/gui/encoder.cs |   35 ++++++++++++++++++++++++++---------
 src/util.cs        |   36 ++++++++++++++++++++++++++++++------
 3 files changed, 66 insertions(+), 15 deletions(-)
---
diff --git a/encoder/graph.R b/encoder/graph.R
index c051779..0b27368 100644
--- a/encoder/graph.R
+++ b/encoder/graph.R
@@ -24,6 +24,7 @@ library("EMD")
 findCurves <- function(rawdata, eccon, min_height, draw) {
 	a=cumsum(rawdata)
 	b=extrema(a)
+print(b)
 	start=0; end=0; startH=0
 	tempStart=0; tempEnd=0;
 	#TODO: fer algo per si no es detecta el minindex previ al salt
@@ -49,6 +50,15 @@ findCurves <- function(rawdata, eccon, min_height, draw) {
 		}
 	} else { #ec, and ec-rep
 		row=1; i=1; j=2
+		#when saved a row with ec-con, and there's only this curve, extrema doesn't find maxindex
+		if(length(b$maxindex) == 0) {
+			start[1] =1
+			end[1]   = mean(which(a == min(a)))
+			startH[1]=a[1]
+			start[2] =end[1]+1
+			end[2]   =length(a)
+			startH[2]=a[start[2]]
+		}
 		while(j <= length(b$maxindex[,1])) {
 			tempStart = mean(c(b$maxindex[i,1],b$maxindex[i,2]))
 			tempEnd   = mean(c(b$maxindex[j,1],b$maxindex[j,2]))
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index f1a05c6..b576833 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -241,23 +241,25 @@ public partial class ChronoJumpWindow
 		Log.WriteLine("TODO: Load stream");
 	}
 
-	void on_button_encoder_delete_selected_clicked (object o, EventArgs args) 
+	private EncoderCurve getCurve(int selectedID) 
 	{
-		//concentric stuff
-		int duration;
-		int selectedID = treeviewEncoderCurvesEventSelectedID();
-
 		if(ecconLast != "c") {
 			bool isEven = (selectedID % 2 == 0); //check if it's even (in spanish "par")
 			if(isEven)
 				selectedID --;
 		}
+		return treeviewEncoderCurvesGetCurve(selectedID);
+	}
 
-		EncoderCurve curve = treeviewEncoderCurvesGetCurve(selectedID);
+	void on_button_encoder_delete_selected_clicked (object o, EventArgs args) 
+	{
+		int selectedID = treeviewEncoderCurvesEventSelectedID();
+		EncoderCurve curve = getCurve(selectedID);
 
 		//some start at ,5 because of the spline filtering
 		int curveStart = Convert.ToInt32(decimal.Truncate(Convert.ToDecimal(curve.Start)));
 
+		int duration;
 		if( (ecconLast == "c" && selectedID == encoderCurves.Count) ||
 				(ecconLast != "c" && selectedID+1 == encoderCurves.Count) )
 			duration = -1; //until the end
@@ -267,12 +269,11 @@ public partial class ChronoJumpWindow
 				curveNext = treeviewEncoderCurvesGetCurve(selectedID+2);
 
 			int curveNextStart = Convert.ToInt32(decimal.Truncate(Convert.ToDecimal(curveNext.Start)));
-
 			duration = curveNextStart - curveStart;
 		}
 
 		if(curve.Start != null) {
-			Log.WriteLine(curveStart + "->" + duration);
+			//Log.WriteLine(curveStart + "->" + duration);
 			Util.EncoderDeleteRow(Util.GetEncoderDataTempFileName(), curveStart, duration);
 		}
 		//force a recalculate
@@ -281,7 +282,23 @@ public partial class ChronoJumpWindow
 
 	void on_button_encoder_save_selected_clicked (object o, EventArgs args) 
 	{
-		Log.WriteLine("TODO: Save selected");
+		int selectedID = treeviewEncoderCurvesEventSelectedID();
+		EncoderCurve curve = getCurve(selectedID);
+
+		//some start at ,5 because of the spline filtering
+		int curveStart = Convert.ToInt32(decimal.Truncate(Convert.ToDecimal(curve.Start)));
+
+		int duration = Convert.ToInt32(decimal.Truncate(Convert.ToDecimal(curve.Duration)));
+		if(ecconLast != "c") {
+			EncoderCurve curveNext = treeviewEncoderCurvesGetCurve(selectedID+1);
+			duration += Convert.ToInt32(decimal.Truncate(Convert.ToDecimal(curveNext.Duration)));
+		}
+			
+		//Log.WriteLine(curveStart + "->" + duration);
+		Util.EncoderSaveRow(Util.GetEncoderDataTempFileName(), curveStart, duration);
+		
+		//force a recalculate
+		//on_button_encoder_recalculate_clicked (o, args); 
 	}
 
 	void on_button_encoder_save_stream_clicked (object o, EventArgs args) 
diff --git a/src/util.cs b/src/util.cs
index 2671204..877ab2d 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -1005,8 +1005,7 @@ public class Util
 		}
 	}
 
-	public static void EncoderDeleteRow(string fileName, int start, int duration) {
-		string contents = ReadFile(fileName);
+	private static string [] encoderFindPos(string contents, int start, int duration) {
 		int startPos = 0;
 		int durationPos = 0;
 		int i,digits;
@@ -1036,12 +1035,24 @@ public class Util
 					durationPos = i-startPos;
 			}
 		}
-		Log.WriteLine("s "+ startPos.ToString());
-		Log.WriteLine("d "+ durationPos.ToString());
-		Log.WriteLine("i " + i.ToString());
+		//Log.WriteLine("s "+ startPos.ToString());
+		//Log.WriteLine("d "+ durationPos.ToString());
+		//Log.WriteLine("i " + i.ToString());
+
+		string [] returnStr = new string[2];
+		returnStr [0] = startPos.ToString();
+		returnStr [1] = durationPos.ToString();
+		return returnStr;
+	}
+
+	public static void EncoderDeleteRow(string fileName, int start, int duration) {
+		string contents = ReadFile(fileName);
+		string [] startAndDuration = encoderFindPos(contents, start, duration);
 
 		StringBuilder myStringBuilder = new StringBuilder(contents);
-		myStringBuilder.Remove(startPos, durationPos);
+		myStringBuilder.Remove(
+				Convert.ToInt32(startAndDuration[0]),
+				Convert.ToInt32(startAndDuration[1]));
 		contents = myStringBuilder.ToString();
 		
 		TextWriter writer = File.CreateText(fileName);
@@ -1050,6 +1061,19 @@ public class Util
 		((IDisposable)writer).Dispose();
 	}
 
+	public static void EncoderSaveRow(string fileName, int start, int duration) {
+		string contents = ReadFile(fileName);
+		string [] startAndDuration = encoderFindPos(contents, start, duration);
+
+		contents = contents.Substring(
+				Convert.ToInt32(startAndDuration[0]), 
+				Convert.ToInt32(startAndDuration[1])-1); //-1 is for not ending file with a comma
+		
+		TextWriter writer = File.CreateText(fileName + "-testrow");
+		writer.Write(contents);
+		writer.Flush();
+		((IDisposable)writer).Dispose();
+	}
 
 
 /*



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