Created
November 25, 2018 21:59
-
-
Save naaando/efbbeaab86ccc3a5aa8a0c18cf2d1778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//author gavr https://www.youtube.com/channel/UChHL_HIocsBbeLGhi50bSvw?view_as=subscriber | |
//compile with valac --pkg gtk+-3.0 TreeViewExample.vala | |
using Gtk; | |
public class StackClass : Gtk.Window { | |
public Gtk.LevelBar[] barMass; | |
public Gtk.Frame box3; | |
private CustomWidget widget; | |
public StackClass () { | |
var header = new Gtk.HeaderBar(); | |
header.set_show_close_button(true); | |
barMass = new Gtk.LevelBar[10]; | |
box3 = new Gtk.Frame("График"); | |
widget = new CustomWidget (); | |
var box1 = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 1); | |
//Стак | |
var stack = new Gtk.Stack(); | |
Gtk.StackSwitcher switcher = new Gtk.StackSwitcher (); | |
switcher.set_valign (Gtk.Align.CENTER); | |
header.pack_start (switcher); | |
stack.set_transition_duration (500);//скорость анимации | |
stack.set_transition_type (SLIDE_LEFT_RIGHT); | |
switcher.set_stack (stack);//устанавливаем переключателю стак | |
//создаем 10 прогрессБаров и заполняем ими бокс с флагами expand | |
for (int i=0;i<10;i++){ | |
barMass[i] = new Gtk.LevelBar.for_interval (0.0, 10.0); | |
box1.pack_start(barMass[i],true,true,2); | |
barMass[i].set_orientation(Gtk.Orientation.VERTICAL); | |
//barMass[i].set_mode (Gtk.LevelBarMode.DISCRETE); | |
barMass[i].set_inverted(true); | |
barMass[i].set_hexpand(true); | |
} | |
box3.add(widget); | |
barMass[0].set_value(10.0); | |
stack.add_titled(box1,"box1","box1"); | |
stack.add_titled(box3,"box3","box3"); | |
this.set_titlebar(header); | |
this.destroy.connect(Gtk.main_quit); | |
this.border_width = 10; | |
this.set_size_request (600, 500); | |
this.add(stack); | |
this.key_press_event.connect ((key) => { | |
print(key.str + " "); | |
addNbar(double.parse(key.str)); | |
return false; | |
}); | |
} | |
public void addNbar(double newV){ | |
for(int i=0;i<10-1;i++){ | |
barMass[i].set_value(barMass[i+1].get_value()); | |
} | |
barMass[9].set_value(newV); | |
widget.update((int) newV); | |
} | |
public static int main(string[] args){ | |
Gtk.init(ref args); | |
var s = new StackClass(); | |
s.show_all(); | |
Gtk.main(); | |
return 0; | |
} | |
} | |
public class CustomWidget : DrawingArea { | |
public string text; | |
public int newPoint; | |
public int[] points; | |
public CustomWidget () { | |
set_size_request (get_allocated_width (), get_allocated_height ()); | |
points = new int [10]; | |
int height = get_allocated_height ();print (@"полученная высота в начале $height"); | |
for(int i=0;i<10;i++)points[i]=0; | |
//this.newPoint = txt; | |
} | |
// public void setNewPoint(int y) {this.newPoint=y;} | |
/* Widget is asked to draw itself */ | |
public override bool draw (Cairo.Context cr) { | |
int width = get_allocated_width (); | |
int height = get_allocated_height (); | |
print ("ширина = %d, высота = %d \n",width,height); | |
//цикл в котором отрисовываем массив points[j] с шагом в 1/10 экрана | |
for(int i=0,j=0 ;i<width-width / 10 ;i+=width / 10, j++) { | |
cr.line_to(i,height-((height/10) *points[j])); | |
print("j = %d ",j); | |
print(" x %d, y %d \n",i,height/10 *points[j]); | |
} | |
cr.line_to(10* width / 10,height-newPoint*height/10); | |
print(" x %d, y %d \n",10* width/10,newPoint*height/10); | |
cr.stroke (); | |
return false; | |
} | |
public bool update(int y){ | |
this.newPoint=y; | |
for(int i=0;i<10-1;i++) { | |
points[i]=points[i+1]; | |
} | |
points[9]=y; | |
redraw_canvas (); | |
return true; | |
} | |
private void redraw_canvas () { | |
var window = get_window (); | |
if (null == window) { | |
return; | |
} | |
var region = window.get_clip_region (); | |
// redraw the cairo canvas completely by exposing it | |
window.invalidate_region (region, true); | |
window.process_updates (true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment