[chronojump] Encoder process can be cancelled



commit 24ceb3680a91c94075fa0d4cf0df20b2a04048a3
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Jun 5 11:18:09 2012 +0200

    Encoder process can be cancelled

 glade/chronojump.glade |  168 +++++++++++++++++++++++++++++++++---------------
 src/gui/encoder.cs     |   78 ++++++++++++++++------
 src/util.cs            |   10 ++-
 3 files changed, 180 insertions(+), 76 deletions(-)
---
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 37d9fd9..283d5d9 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -24517,34 +24517,98 @@ Evaluator can use real name or nickname.</property>
                                           </packing>
                                         </child>
                                         <child>
-                                          <widget class="GtkHButtonBox" id="hbuttonbox9">
+                                          <widget class="GtkHBox" id="hbox87">
                                             <property name="visible">True</property>
-                                            <property name="layout_style">spread</property>
+                                            <property name="spacing">12</property>
                                             <child>
-                                              <widget class="GtkButton" id="button_encoder_capture">
-                                                <property name="label" translatable="yes">Capture from encoder</property>
+                                              <widget class="GtkHButtonBox" id="hbuttonbox9">
                                                 <property name="visible">True</property>
-                                                <property name="can_focus">True</property>
-                                                <property name="can_default">True</property>
-                                                <property name="receives_default">True</property>
-                                                <property name="use_underline">True</property>
-                                                <signal name="clicked" handler="on_button_encoder_capture_clicked"/>
+                                                <property name="layout_style">spread</property>
+                                                <child>
+                                                  <widget class="GtkButton" id="button_encoder_capture">
+                                                    <property name="label" translatable="yes">Capture from encoder</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">True</property>
+                                                    <property name="can_default">True</property>
+                                                    <property name="receives_default">True</property>
+                                                    <property name="use_underline">True</property>
+                                                    <signal name="clicked" handler="on_button_encoder_capture_clicked"/>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">0</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
+                                                  <widget class="GtkButton" id="button_encoder_recalculate">
+                                                    <property name="label" translatable="yes">Recalculate</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">True</property>
+                                                    <property name="can_default">True</property>
+                                                    <property name="receives_default">True</property>
+                                                    <property name="use_underline">True</property>
+                                                    <signal name="clicked" handler="on_button_encoder_recalculate_clicked"/>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">1</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
+                                                  <widget class="GtkButton" id="button_encoder_load_signal">
+                                                    <property name="label">Load signal</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="can_focus">True</property>
+                                                    <property name="receives_default">True</property>
+                                                    <signal name="clicked" handler="on_button_encoder_load_signal_clicked"/>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">2</property>
+                                                  </packing>
+                                                </child>
                                               </widget>
                                               <packing>
-                                                <property name="expand">False</property>
-                                                <property name="fill">False</property>
                                                 <property name="position">0</property>
                                               </packing>
                                             </child>
                                             <child>
-                                              <widget class="GtkButton" id="button_encoder_recalculate">
-                                                <property name="label" translatable="yes">Recalculate</property>
+                                              <widget class="GtkHBox" id="hbox93">
                                                 <property name="visible">True</property>
-                                                <property name="can_focus">True</property>
-                                                <property name="can_default">True</property>
-                                                <property name="receives_default">True</property>
-                                                <property name="use_underline">True</property>
-                                                <signal name="clicked" handler="on_button_encoder_recalculate_clicked"/>
+                                                <property name="spacing">4</property>
+                                                <child>
+                                                  <widget class="GtkProgressBar" id="encoder_pulsebar_capture">
+                                                    <property name="visible">True</property>
+                                                    <property name="activity_mode">True</property>
+                                                    <property name="show_text">True</property>
+                                                    <property name="pulse_step">0.10000000149</property>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">0</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
+                                                  <widget class="GtkButton" id="button_encoder_capture_cancel">
+                                                    <property name="label" translatable="yes">Cancel</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="sensitive">False</property>
+                                                    <property name="can_focus">True</property>
+                                                    <property name="can_default">True</property>
+                                                    <property name="receives_default">True</property>
+                                                    <property name="use_underline">True</property>
+                                                    <signal name="clicked" handler="on_button_encoder_cancel_clicked"/>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">1</property>
+                                                  </packing>
+                                                </child>
                                               </widget>
                                               <packing>
                                                 <property name="expand">False</property>
@@ -24552,33 +24616,6 @@ Evaluator can use real name or nickname.</property>
                                                 <property name="position">1</property>
                                               </packing>
                                             </child>
-                                            <child>
-                                              <widget class="GtkProgressBar" id="encoder_pulsebar_capture">
-                                                <property name="visible">True</property>
-                                                <property name="activity_mode">True</property>
-                                                <property name="show_text">True</property>
-                                                <property name="pulse_step">0.10000000149</property>
-                                              </widget>
-                                              <packing>
-                                                <property name="expand">False</property>
-                                                <property name="fill">False</property>
-                                                <property name="position">2</property>
-                                              </packing>
-                                            </child>
-                                            <child>
-                                              <widget class="GtkButton" id="button_encoder_load_signal">
-                                                <property name="label">Load signal</property>
-                                                <property name="visible">True</property>
-                                                <property name="can_focus">True</property>
-                                                <property name="receives_default">True</property>
-                                                <signal name="clicked" handler="on_button_encoder_load_signal_clicked"/>
-                                              </widget>
-                                              <packing>
-                                                <property name="expand">False</property>
-                                                <property name="fill">False</property>
-                                                <property name="position">3</property>
-                                              </packing>
-                                            </child>
                                           </widget>
                                           <packing>
                                             <property name="position">1</property>
@@ -25210,8 +25247,7 @@ Evaluator can use real name or nickname.</property>
                                         <child>
                                           <widget class="GtkHBox" id="hbox74">
                                             <property name="visible">True</property>
-                                            <property name="spacing">20</property>
-                                            <property name="homogeneous">True</property>
+                                            <property name="spacing">12</property>
                                             <child>
                                               <widget class="GtkButton" id="button_encoder_analyze">
                                                 <property name="label" translatable="yes">Analyze</property>
@@ -25232,12 +25268,40 @@ Evaluator can use real name or nickname.</property>
                                               </packing>
                                             </child>
                                             <child>
-                                              <widget class="GtkProgressBar" id="encoder_pulsebar_analyze">
-                                                <property name="width_request">150</property>
+                                              <widget class="GtkHBox" id="hbox79">
                                                 <property name="visible">True</property>
-                                                <property name="activity_mode">True</property>
-                                                <property name="show_text">True</property>
-                                                <property name="pulse_step">0.10000000149</property>
+                                                <property name="spacing">6</property>
+                                                <child>
+                                                  <widget class="GtkProgressBar" id="encoder_pulsebar_analyze">
+                                                    <property name="width_request">150</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="activity_mode">True</property>
+                                                    <property name="show_text">True</property>
+                                                    <property name="pulse_step">0.10000000149</property>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">0</property>
+                                                  </packing>
+                                                </child>
+                                                <child>
+                                                  <widget class="GtkButton" id="button_encoder_analyze_cancel">
+                                                    <property name="label" translatable="yes">Cancel</property>
+                                                    <property name="visible">True</property>
+                                                    <property name="sensitive">False</property>
+                                                    <property name="can_focus">True</property>
+                                                    <property name="can_default">True</property>
+                                                    <property name="receives_default">True</property>
+                                                    <property name="use_underline">True</property>
+                                                    <signal name="clicked" handler="on_button_encoder_cancel_clicked"/>
+                                                  </widget>
+                                                  <packing>
+                                                    <property name="expand">False</property>
+                                                    <property name="fill">False</property>
+                                                    <property name="position">1</property>
+                                                  </packing>
+                                                </child>
                                               </widget>
                                               <packing>
                                                 <property name="expand">False</property>
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 91b8ea5..4e16cef 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -34,6 +34,7 @@ public partial class ChronoJumpWindow
 	[Widget] Gtk.SpinButton spin_encoder_smooth;
 
 	[Widget] Gtk.Button button_encoder_capture;
+	[Widget] Gtk.Button button_encoder_capture_cancel;
 	[Widget] Gtk.Button button_encoder_recalculate;
 	[Widget] Gtk.Button button_encoder_load_signal;
 	[Widget] Gtk.Viewport viewport_image_encoder_capture;
@@ -59,6 +60,7 @@ public partial class ChronoJumpWindow
 
 	
 	[Widget] Gtk.Button button_encoder_analyze;
+	[Widget] Gtk.Button button_encoder_analyze_cancel;
 	[Widget] Gtk.RadioButton radiobutton_encoder_analyze_data_current_signal;
 	[Widget] Gtk.RadioButton radiobutton_encoder_analyze_data_user_curves;
 	[Widget] Gtk.Box hbox_encoder_user_curves_num;
@@ -94,13 +96,14 @@ public partial class ChronoJumpWindow
 	int image_encoder_width;
 	int image_encoder_height;
 
-	private static string encoderAnalysis="powerBars";
+	private string encoderAnalysis="powerBars";
 	private string ecconLast;
 	private string encoderTimeStamp;
 	private string encoderSignalUniqueID;
 	enum encoderModes { CAPTURE, ANALYZE }
 	enum encoderSensEnum { 
 		NOSESSION, NOPERSON, YESPERSON, PROCESSING, DONENOSIGNAL, DONEYESSIGNAL, SELECTEDCURVE }
+	private static bool encoderProcessCancel;
 
 	
 	//TODO: auto close capturing window
@@ -128,6 +131,12 @@ public partial class ChronoJumpWindow
 	//TODO: if a signal is loaded, exercise has to be updated on combo. (use exerciseID in database)
 	//
 	//TODO: do the graphical capturing with pygame
+	//
+	//TODO: allow gui/generic.cs to select rows on treeview. return an array of selected uniqueIDs or curveID
+	//
+	//TODO: calling to R should give feedback during the process
+	//
+	//TODO: fix problem that on saving maybe dirs are not created
 
 	
 	private void encoderInitializeStuff() {
@@ -212,6 +221,11 @@ public partial class ChronoJumpWindow
 		encoderThreadStart(encoderModes.CAPTURE);
 	}
 		
+	void on_button_encoder_cancel_clicked (object o, EventArgs args) 
+	{
+		encoderProcessCancel = true;
+	}
+
 	void on_button_encoder_recalculate_clicked (object o, EventArgs args) 
 	{
 		if (File.Exists(Util.GetEncoderDataTempFileName()))
@@ -520,7 +534,7 @@ public partial class ChronoJumpWindow
 			//TODO: in the future plot a "no curves" message,
 			//or beter done allow to analyze if there's no curves
 		}
-		
+	
 		encoderThreadStart(encoderModes.ANALYZE);
 	}
 	
@@ -1314,20 +1328,21 @@ public partial class ChronoJumpWindow
 		//c4 button_encoder_delete_curve , button_encoder_save_curve
 		//c5 button_encoder_analyze
 		//c6 button_encoder_analyze_data_show_user_curves
+		//c7 button_cancel (on capture and analyze)
 
 		//other dependencies
 		//c5 True needs 
 		//	(signal || (! radiobutton_encoder_analyze_data_current_signal.Active && user has curves))
 		//c6 True needs ! radiobutton_encoder_analyze_data_current_signal.Active
 		
-		//columns		 0  1  2  3  4  5  6
-		int [] noSession = 	{0, 0, 0, 0, 0, 0, 0};
-		int [] noPerson = 	{0, 0, 0, 0, 0, 0, 0};
-		int [] yesPerson = 	{1, 0, 1, 0, 0, 1, 1};
-		int [] processing = 	{0, 0, 0, 0, 0, 0, 0};
-		int [] doneNoSignal = 	{1, 1, 1, 0, 0, 1, 1};
-		int [] doneYesSignal = 	{1, 1, 1, 1, 0, 1, 1};
-		int [] selectedCurve = 	{1, 1, 1, 1, 1, 1, 1};
+		//columns		 0  1  2  3  4  5  6  7
+		int [] noSession = 	{0, 0, 0, 0, 0, 0, 0, 0};
+		int [] noPerson = 	{0, 0, 0, 0, 0, 0, 0, 0};
+		int [] yesPerson = 	{1, 0, 1, 0, 0, 1, 1, 0};
+		int [] processing = 	{0, 0, 0, 0, 0, 0, 0, 1};
+		int [] doneNoSignal = 	{1, 1, 1, 0, 0, 1, 1, 0};
+		int [] doneYesSignal = 	{1, 1, 1, 1, 0, 1, 1, 0};
+		int [] selectedCurve = 	{1, 1, 1, 1, 1, 1, 1, 0};
 		int [] table = new int[7];
 
 		switch(option) {
@@ -1376,6 +1391,9 @@ public partial class ChronoJumpWindow
 
 		button_encoder_analyze_data_show_user_curves.Sensitive = 
 			(Util.IntToBool(table[6]) && ! radiobutton_encoder_analyze_data_current_signal.Active);
+		
+		button_encoder_capture_cancel.Sensitive = Util.IntToBool(table[7]);
+		button_encoder_analyze_cancel.Sensitive = Util.IntToBool(table[7]);
 	}
 
 	/* end of sensitivity stuff */	
@@ -1409,7 +1427,11 @@ public partial class ChronoJumpWindow
 	
 	private bool pulseGTKEncoderCapture ()
 	{
-		if(! encoderThread.IsAlive) {
+		if(! encoderThread.IsAlive || encoderProcessCancel) {
+			if(encoderProcessCancel){
+				Util.CancelRScript = true;
+			}
+
 			finishPulsebar(encoderModes.CAPTURE);
 			Log.Write("dying");
 			return false;
@@ -1422,7 +1444,11 @@ public partial class ChronoJumpWindow
 	
 	private bool pulseGTKEncoderAnalyze ()
 	{
-		if(! encoderThread.IsAlive) {
+		if(! encoderThread.IsAlive || encoderProcessCancel) {
+			if(encoderProcessCancel){
+				Util.CancelRScript = true;
+			}
+
 			finishPulsebar(encoderModes.ANALYZE);
 			Log.Write("dying");
 			return false;
@@ -1442,22 +1468,32 @@ public partial class ChronoJumpWindow
 	
 	private void finishPulsebar(encoderModes mode) {
 		if(mode == encoderModes.CAPTURE) {
+			if(encoderProcessCancel) {
+				encoderProcessCancel = false;
+			
+				encoderButtonsSensitive(encoderSensEnum.DONEYESSIGNAL);
+			} else {
+				Pixbuf pixbuf = new Pixbuf (Util.GetEncoderGraphTempFileName()); //from a file
+				image_encoder_capture.Pixbuf = pixbuf;
+				encoderUpdateTreeView();
+			}
+
 			encoder_pulsebar_capture.Fraction = 1;
 			encoder_pulsebar_capture.Text = "";
-			treeview_encoder_curves.Sensitive = true;
-			
-			Pixbuf pixbuf = new Pixbuf (Util.GetEncoderGraphTempFileName()); //from a file
-			image_encoder_capture.Pixbuf = pixbuf;
 
-			encoderUpdateTreeView();
 		} else {
+			if(encoderProcessCancel) 
+				encoderProcessCancel = false;
+			else {
+				//TODO pensar en si s'ha de fer 1er amb mida petita i despres amb gran (en el zoom),
+				//o si es una sola i fa alguna edicio
+				Pixbuf pixbuf = new Pixbuf (Util.GetEncoderGraphTempFileName()); //from a file
+				image_encoder_analyze.Pixbuf = pixbuf;
+			}
+
 			encoder_pulsebar_analyze.Fraction = 1;
 			encoder_pulsebar_analyze.Text = "";
 			
-			//TODO pensar en si s'ha de fer 1er amb mida petita i despres amb gran (en el zoom), o si es una sola i fa alguna edicio
-			Pixbuf pixbuf = new Pixbuf (Util.GetEncoderGraphTempFileName()); //from a file
-			image_encoder_analyze.Pixbuf = pixbuf;
-			
 			encoderButtonsSensitive(encoderSensEnum.DONEYESSIGNAL);
 		}
 
diff --git a/src/util.cs b/src/util.cs
index cc414ce..108656c 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -939,8 +939,10 @@ public class Util
 		}
 	}
 	
-
+	public static bool CancelRScript;
 	public static void RunRScript(string rScript){
+		CancelRScript = false;
+
 		ProcessStartInfo pinfo;
 	        Process r;
 		string rBin="R";
@@ -964,11 +966,13 @@ public class Util
 		r.StartInfo = pinfo;
 		r.Start();
 		r.WaitForExit();
-		while (!File.Exists(outputFile));				
+		while ( ! ( File.Exists(outputFile) || CancelRScript) );
 	}
 	
 	//python program
 	public static void RunPythonEncoder(string pythonScript, EncoderStruct es, bool useTerminal) {
+		CancelRScript = false;
+
 		ProcessStartInfo pinfo;
 	        Process p;
 		//If output file is not given, R will try to write in the running folder
@@ -1014,7 +1018,7 @@ public class Util
 		p.StartInfo = pinfo;
 		p.Start();
 		p.WaitForExit();
-		while (!File.Exists(outputFileCheck));
+		while ( ! ( File.Exists(outputFileCheck) || CancelRScript) );
 	}
 
 	public static string ReadFile(string fileName)



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