[chronojump] Refactorization of Cairo methods
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] Refactorization of Cairo methods
- Date: Fri, 18 Dec 2020 14:15:40 +0000 (UTC)
commit 775c21383bdbe732c4168d67dab4c43dc51d9d2a
Author: Xavier de Blas <xaviblas gmail com>
Date: Fri Dec 18 15:15:19 2020 +0100
Refactorization of Cairo methods
src/Makefile.am | 1 +
src/gui/cairo/generic.cs | 69 +++++++++++++++++++++++++++++++++++
src/gui/cairo/jumpsDjOptimalFall.cs | 4 +-
src/gui/cairo/jumpsEvolution.cs | 4 +-
src/gui/cairo/jumpsRjFatigue.cs | 4 +-
src/gui/cairo/jumpsWeightFVProfile.cs | 4 +-
src/gui/cairo/radial.cs | 42 ++-------------------
src/gui/cairo/xy.cs | 27 +-------------
8 files changed, 83 insertions(+), 72 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 896ee884..a5c473d0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -34,6 +34,7 @@ SOURCES = \
gui/app1/runEncoder.cs\
gui/app1/runEncoderAnalyze.cs\
gui/app1/shortcuts.cs\
+ gui/cairo/generic.cs\
gui/cairo/jumpsDjOptimalFall.cs\
gui/cairo/jumpsEvolution.cs\
gui/cairo/jumpsProfile.cs\
diff --git a/src/gui/cairo/generic.cs b/src/gui/cairo/generic.cs
new file mode 100644
index 00000000..460fdfd0
--- /dev/null
+++ b/src/gui/cairo/generic.cs
@@ -0,0 +1,69 @@
+
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Copyright (C) 2004-2020 Xavier de Blas <xaviblas gmail com>
+ */
+
+using System;
+using Gtk;
+using Cairo;
+
+public abstract class CairoGeneric
+{
+ /*
+ need to dispose because Cairo does not clean ok on win and mac:
+ Don’t forget to manually dispose the Context and the target Surface at the end of the expose
event. Automatic garbage collecting is not yet 100% working in Cairo.
+ https://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cairo/
+ eg. on Linux can do the writeCoordinatesOfMouseClick() without disposing, but on win and mac
does not work, so dispose always.
+ */
+
+ protected void endGraphDisposing(Cairo.Context g)
+ {
+ g.GetTarget().Dispose ();
+ g.Dispose ();
+ }
+
+ //0 - 255
+ protected Cairo.Color colorFromRGB(int red, int green, int blue)
+ {
+ return new Cairo.Color(red/256.0, green/256.0, blue/256.0);
+ }
+ protected Cairo.Color colorFromGdk(Gdk.Color color)
+ {
+ return new Cairo.Color(color.Red/65536.0, color.Green/65536.0, color.Blue/65536.0);
+ }
+
+ protected enum alignTypes { LEFT, CENTER, RIGHT }
+ protected void printText (int x, int y, int height, int textHeight, string text, Cairo.Context g,
alignTypes align)
+ {
+ int moveToLeft = 0;
+ if(align == alignTypes.CENTER || align == alignTypes.RIGHT)
+ {
+ Cairo.TextExtents te;
+ te = g.TextExtents(text);
+
+ if(align == alignTypes.CENTER)
+ moveToLeft = Convert.ToInt32(te.Width/2);
+ else
+ moveToLeft = Convert.ToInt32(te.Width);
+ }
+
+ g.MoveTo( x - moveToLeft, ((y+y+height)/2) + textHeight/2 );
+ g.ShowText(text);
+ }
+}
diff --git a/src/gui/cairo/jumpsDjOptimalFall.cs b/src/gui/cairo/jumpsDjOptimalFall.cs
index f3dfda47..d395ab22 100644
--- a/src/gui/cairo/jumpsDjOptimalFall.cs
+++ b/src/gui/cairo/jumpsDjOptimalFall.cs
@@ -39,7 +39,7 @@ public class JumpsDjOptimalFallGraph : CairoXY
printText(area.Allocation.Width /2, area.Allocation.Height /2, 24, textHeight,
needToExecuteJumpsStr + " " + jumpType + ".", g, alignTypes.CENTER);
- endGraphDisposing();
+ endGraphDisposing(g);
}
//regular constructor
@@ -103,7 +103,7 @@ public class JumpsDjOptimalFallGraph : CairoXY
}
writeTitle();
- endGraphDisposing();
+ endGraphDisposing(g);
}
protected override void writeTitle()
diff --git a/src/gui/cairo/jumpsEvolution.cs b/src/gui/cairo/jumpsEvolution.cs
index c8c78672..751fae10 100644
--- a/src/gui/cairo/jumpsEvolution.cs
+++ b/src/gui/cairo/jumpsEvolution.cs
@@ -38,7 +38,7 @@ public class JumpsEvolutionGraph : CairoXY
printText(area.Allocation.Width /2, area.Allocation.Height /2, 24, textHeight,
needToExecuteJumpsStr + " " + jumpType + ".", g, alignTypes.CENTER);
- endGraphDisposing();
+ endGraphDisposing(g);
}
//regular constructor
@@ -77,7 +77,7 @@ public class JumpsEvolutionGraph : CairoXY
writeTitle();
- endGraphDisposing();
+ endGraphDisposing(g);
}
//here X is year, add/subtract third of a year
diff --git a/src/gui/cairo/jumpsRjFatigue.cs b/src/gui/cairo/jumpsRjFatigue.cs
index 0268ee3f..4e53bc87 100644
--- a/src/gui/cairo/jumpsRjFatigue.cs
+++ b/src/gui/cairo/jumpsRjFatigue.cs
@@ -42,7 +42,7 @@ public class JumpsRjFatigueGraph : CairoXY
//printText(area.Allocation.Width /2, area.Allocation.Height /2, 24, textHeight,
// string.Format("Need to execute jumps: {0}.", jumpType), g, true);
- endGraphDisposing();
+ endGraphDisposing(g);
}
public JumpsRjFatigueGraph (
List<PointF> point_l, double slope, double intercept,
@@ -99,7 +99,7 @@ public class JumpsRjFatigueGraph : CairoXY
writeTitle();
- endGraphDisposing();
+ endGraphDisposing(g);
}
//here X is year, add/subtract third of a year
diff --git a/src/gui/cairo/jumpsWeightFVProfile.cs b/src/gui/cairo/jumpsWeightFVProfile.cs
index 878ebac3..662dd51c 100644
--- a/src/gui/cairo/jumpsWeightFVProfile.cs
+++ b/src/gui/cairo/jumpsWeightFVProfile.cs
@@ -47,7 +47,7 @@ public class JumpsWeightFVProfileGraph : CairoXY
plotError();
- endGraphDisposing();
+ endGraphDisposing(g);
}
//regular constructor
@@ -162,7 +162,7 @@ public class JumpsWeightFVProfileGraph : CairoXY
if(errorMessage != ErrorAtStart.ALLOK)
plotError();
- endGraphDisposing();
+ endGraphDisposing(g);
}
protected override void writeTitle()
diff --git a/src/gui/cairo/radial.cs b/src/gui/cairo/radial.cs
index 7c1cd196..262f3098 100644
--- a/src/gui/cairo/radial.cs
+++ b/src/gui/cairo/radial.cs
@@ -25,7 +25,7 @@ using Gtk;
using Cairo;
using Mono.Unix;
-public class CairoRadial
+public class CairoRadial : CairoGeneric
{
private Cairo.Context g;
private int textHeight = 12;
@@ -45,7 +45,7 @@ public class CairoRadial
this.font = font;
initGraph();
- endGraphDisposing();
+ endGraphDisposing(g);
}
private void initGraph()
@@ -140,7 +140,7 @@ public class CairoRadial
if(speedMax > speed)
graphLineFromCenter(speedMax, gray);
- endGraphDisposing();
+ endGraphDisposing(g);
}
private void graphLineFromCenter(double toValue, Cairo.Color color)
@@ -157,41 +157,5 @@ public class CairoRadial
g.Stroke();
}
- //TODO: all this methods have to be shared with xy.cs
-
- protected void endGraphDisposing()
- {
- g.GetTarget().Dispose ();
- g.Dispose ();
- }
-
- //0 - 255
- private Cairo.Color colorFromRGB(int red, int green, int blue)
- {
- return new Cairo.Color(red/256.0, green/256.0, blue/256.0);
- }
- protected Cairo.Color colorFromGdk(Gdk.Color color)
- {
- return new Cairo.Color(color.Red/65536.0, color.Green/65536.0, color.Blue/65536.0);
- }
-
- private enum alignTypes { LEFT, CENTER, RIGHT }
- private void printText (int x, int y, int height, int textHeight, string text, Cairo.Context g,
alignTypes align)
- {
- int moveToLeft = 0;
- if(align == alignTypes.CENTER || align == alignTypes.RIGHT)
- {
- Cairo.TextExtents te;
- te = g.TextExtents(text);
-
- if(align == alignTypes.CENTER)
- moveToLeft = Convert.ToInt32(te.Width/2);
- else
- moveToLeft = Convert.ToInt32(te.Width);
- }
-
- g.MoveTo( x - moveToLeft, ((y+y+height)/2) + textHeight/2 );
- g.ShowText(text);
- }
}
diff --git a/src/gui/cairo/xy.cs b/src/gui/cairo/xy.cs
index 036832e4..6845b76b 100644
--- a/src/gui/cairo/xy.cs
+++ b/src/gui/cairo/xy.cs
@@ -25,7 +25,7 @@ using Gtk;
using Cairo;
using Mono.Unix;
-public abstract class CairoXY
+public abstract class CairoXY : CairoGeneric
{
//used on construction
protected List<PointF> point_l;
@@ -536,7 +536,7 @@ public abstract class CairoXY
g.Color = black;
LogB.Information("writeCoordinatesOfMouseClick done! disposing");
- endGraphDisposing();
+ endGraphDisposing(g);
LogB.Information("writeCoordinatesOfMouseClick disposed!");
}
@@ -576,18 +576,6 @@ public abstract class CairoXY
writeTextAtRight(line +2, string.Format("- {0}: {1} {2}", yVariable,
Util.TrimDecimals(pClosest.Y, 2), yUnits), false);
}
- /*
- need to dispose because Cairo does not clean ok on win and mac:
- Don’t forget to manually dispose the Context and the target Surface at the end of the expose
event. Automatic garbage collecting is not yet 100% working in Cairo.
- https://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cairo/
- eg. on Linux can do the writeCoordinatesOfMouseClick() without disposing, but on win and mac
does not work, so dispose always.
- */
- protected void endGraphDisposing()
- {
- g.GetTarget().Dispose ();
- g.Dispose ();
- }
-
//TODO: fix if min == max (crashes)
protected enum gridTypes { BOTH, HORIZONTALLINES, VERTICALLINES }
protected void paintGridNiceAutoValues (double minX, double maxX, double minY, double maxY, int seps,
gridTypes gridType)
@@ -781,17 +769,6 @@ public abstract class CairoXY
}
*/
- //0 - 255
- protected Cairo.Color colorFromRGB(int red, int green, int blue)
- {
- return new Cairo.Color(red/256.0, green/256.0, blue/256.0);
- }
- //0 - 65535
- protected Cairo.Color colorFromGdk(Gdk.Color color)
- {
- return new Cairo.Color(color.Red/65536.0, color.Green/65536.0, color.Blue/65536.0);
- }
-
protected enum alignTypes { LEFT, CENTER, RIGHT }
protected void printText (int x, int y, int height, int textHeight, string text, Cairo.Context g,
alignTypes align)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]