new battery gauge

This commit is contained in:
Pascal Brisset
2006-08-25 07:42:59 +00:00
parent a09f95df60
commit 917490e892
2 changed files with 51 additions and 27 deletions
+50 -26
View File
@@ -1,7 +1,7 @@
(************ Strip handling ***********************************************)
let bat_max = 13.
let bat_min = 8.
let bat_max = 12.5
let bat_min = 9.
(** window for the strip panel *)
let scrolled = GBin.scrolled_window ~width:300 ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC ()
@@ -9,7 +9,7 @@ let table = GPack.table ~rows: 1 ~columns: 1 ~row_spacings: 5 ~packing: (scrolle
type t = {
gauge: GRange.progress_bar ;
gauge: GMisc.drawing_area * (GDraw.pixmap option ref);
labels: (string * (GBin.event_box * GMisc.label)) list;
buttons_box : GPack.box
}
@@ -23,7 +23,8 @@ let labels_print = [|
|]
let gen_int = let i = ref (-1) in fun () -> incr i; !i
let rows = Array.length labels_name + 1
let rows = 1 + Array.length labels_name + 1
let columns = 1 + 2 * Array.length labels_name.(0)
(** add a strip to the panel *)
@@ -44,10 +45,16 @@ let add config color select center_ac commit_moves mark =
(* frame of the strip *)
let frame = GBin.frame ~shadow_type: `IN ~packing: (widget#attach ~top: (strip_number) ~left: 0) () in
let framevb = GPack.vbox ~packing:frame#add () in
let strip = GPack.table ~rows:2 ~columns: 2 ~col_spacings: 10 ~packing: framevb#add () in
ignore (GMisc.label ~text: (ac_name) ~packing: (strip#attach ~top: 0 ~left: 0) ());
let plane_color = GBin.event_box ~width:10 ~height:10 ~packing:(strip#attach ~top:0 ~left: 1) () in
(** Table (everything except the user buttons) *)
let strip = GPack.table ~rows ~columns ~col_spacings:5 ~row_spacings:3 ~packing:framevb#add () in
(* Name in top left *)
let name = (GMisc.label ~text: (ac_name) ~packing: (strip#attach ~top: 0 ~left: 0) ()) in
name#set_width_chars 5;
let plane_color = GBin.event_box ~packing:(strip#attach ~top:0 ~left:1 ~right:columns) () in
plane_color#coerce#misc#modify_bg [`NORMAL, `NAME color];
ignore (plane_color#event#connect#button_press ~callback:(fun _ -> select (); true));
let h = GPack.hbox ~packing:plane_color#add () in
@@ -57,41 +64,43 @@ let add config color select center_ac commit_moves mark =
let block_name = GMisc.label ~text: "______" ~packing:h#add () in
add_label ("block_name_value") (plane_color, block_name);
(* battery and telemetry status *)
let vb = GPack.vbox ~packing:(strip#attach ~top:1 ~left:0) () in
let pb = GRange.progress_bar ~orientation: `BOTTOM_TO_TOP ~packing:vb#add () in
pb#misc#set_size_request ~height:50 ();
pb#coerce#misc#modify_fg [`PRELIGHT, `NAME "green"];
pb#coerce#misc#modify_font_by_name "sans 12";
(* battery gauge *)
let gauge = GMisc.drawing_area ~height:60 ~show:true ~packing:(strip#attach ~top:1 ~bottom:(rows-1) ~left:0) () in
gauge#misc#realize ();
let {Gtk.x=x0; y=y0; width=width; height=height} = gauge#misc#allocation in
let eb = GBin.event_box ~packing:vb#add () in
(* Telemetry *)
let eb = GBin.event_box ~packing:(strip#attach ~top:(rows-1) ~left:0) () in
let ts = GMisc.label ~text:"N/A" ~packing:eb#add () in
add_label "telemetry_status_value" (eb, ts);
tooltips#set_tip eb#coerce ~text:"Telemetry status\nGreen if time since last bat message < 5s";
let left_box = GPack.table ~rows ~columns: 6 ~col_spacings: 5
~packing: (strip#attach ~top: 1 ~left: 1) () in
(* Labels *)
Array.iteri
(fun i a ->
Array.iteri
(fun j s ->
ignore (GMisc.label ~text: labels_print.(i).(j) ~justify:`RIGHT ~packing: (left_box#attach ~top: i ~left: (2*j)) ());
let eb = GBin.event_box ~packing: (left_box#attach ~top: i ~left: (2*j+1)) () in
ignore (GMisc.label ~text: labels_print.(i).(j) ~justify:`RIGHT ~packing: (strip#attach ~top:(1+i) ~left: (1+2*j)) ());
let eb = GBin.event_box ~packing: (strip#attach ~top:(i+1) ~left: (1+2*j+1)) () in
let lvalue = (GMisc.label ~text: "" ~justify: `RIGHT ~packing:eb#add ()) in
lvalue#set_width_chars 6;
add_label (s^"_value") (eb, lvalue);
) a
) labels_name;
let b = GButton.button ~label:"Center A/C" ~packing:(left_box#attach ~top:4 ~left:0 ~right:2) () in
(* Buttons *)
let top = rows - 1 in
let b = GButton.button ~label:"Center A/C" ~packing:(strip#attach ~top ~left:1 ~right:3) () in
ignore(b#connect#clicked ~callback:center_ac);
let b = GButton.button ~label:"Commit WPs" ~packing:(left_box#attach ~top:4 ~left:2 ~right:4) () in
let b = GButton.button ~label:"Send WPs" ~packing:(strip#attach ~top ~left:3 ~right:5) () in
ignore (b#connect#clicked ~callback:commit_moves);
let b = GButton.button ~label:"Mark" ~packing:(left_box#attach ~top:4 ~left:4 ~right:6) () in
let b = GButton.button ~label:"Mark" ~packing:(strip#attach ~top ~left:5 ~right:7) () in
ignore (b#connect#clicked ~callback:mark);
(* User buttons *)
let hbox = GPack.hbox ~packing:framevb#add () in
{gauge=pb ; labels= !strip_labels; buttons_box = hbox}
{gauge=(gauge, ref None) ; labels= !strip_labels; buttons_box = hbox}
(** set a label *)
@@ -106,11 +115,26 @@ let set_color strip name color =
eb#coerce#misc#modify_bg [`NORMAL, `NAME color]
(** set the battery *)
let set_bat strip value =
strip.gauge#set_text (string_of_float value);
let set_bat ?(color="green") strip value =
let gauge, drref = strip.gauge in
let {Gtk.x=x0; y=y0; width=width; height=height} = gauge#misc#allocation in
let dr = GDraw.pixmap ~width ~height ~window:gauge () in
dr#set_foreground (`NAME "orange");
dr#rectangle ~x:0 ~y:0 ~width ~height ~filled:true ();
let f = (value -. bat_min) /. (bat_max -. bat_min) in
let f = max 0. (min 1. f) in
strip.gauge#set_fraction f
let h = truncate (float height *. f) in
dr#set_foreground (`NAME color);
dr#rectangle ~x:0 ~y:(height-h) ~width ~height:h ~filled:true ();
let context = gauge#misc#create_pango_context in
let layout = context#create_layout in
Pango.Layout.set_text layout (string_of_float value);
let (w,h) = Pango.Layout.get_pixel_size layout in
dr#put_layout ~x:((width-w)/2) ~y:((height-h)/2) ~fore:`BLACK layout;
(new GDraw.drawable gauge#misc#window)#put_pixmap ~x:0 ~y:0 dr#pixmap
let add_widget = fun strip widget ->
+1 -1
View File
@@ -5,6 +5,6 @@ val add :
(unit -> 'a) -> (unit -> unit) -> (unit -> unit) -> (unit -> unit) -> t
val set_label : t -> string -> string -> unit
val set_color : t -> string -> string -> unit
val set_bat : t -> float -> unit
val set_bat : ?color:string -> t -> float -> unit
val scrolled : GBin.scrolled_window
val add_widget : t -> GObj.widget -> unit