Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 4.14 KB | None | 0 0
  1. using Gtk;
  2. public class StackClass : Gtk.Window {
  3.    public Gtk.LevelBar[] barMass;
  4.    public Gtk.Frame box3;
  5.     private CustomWidget widget;
  6.     public StackClass () {
  7.         var header = new Gtk.HeaderBar();
  8.         header.set_show_close_button(true);
  9.         barMass = new Gtk.LevelBar[10];
  10.  
  11.         box3 = new Gtk.Frame("График");  
  12.         widget = new CustomWidget ();
  13.  
  14.         var box1 = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 1);
  15. //Стак
  16.         var stack = new Gtk.Stack();
  17.         Gtk.StackSwitcher switcher = new Gtk.StackSwitcher ();
  18.         switcher.set_valign (Gtk.Align.CENTER);
  19.         header.pack_start (switcher);
  20.         stack.set_transition_duration (500);//скорость анимации
  21.         stack.set_transition_type (SLIDE_LEFT_RIGHT);
  22.         switcher.set_stack (stack);//устанавливаем переключателю стак
  23. //создаем 10 прогрессБаров и заполняем ими бокс с флагами expand
  24.         for (int i=0;i<10;i++){
  25.             barMass[i] = new Gtk.LevelBar.for_interval (0.0, 10.0);
  26.             box1.pack_start(barMass[i],true,true,2);
  27.             barMass[i].set_orientation(Gtk.Orientation.VERTICAL);
  28.             //barMass[i].set_mode (Gtk.LevelBarMode.DISCRETE);
  29.             barMass[i].set_inverted(true);
  30.             barMass[i].set_hexpand(true);
  31.         }
  32.  
  33.         box3.add(widget);
  34.         barMass[0].set_value(10.0);
  35.    
  36.         stack.add_titled(box1,"box1","box1");
  37.         stack.add_titled(box3,"box3","box3");
  38.  
  39.         this.set_titlebar(header);
  40.         this.destroy.connect(Gtk.main_quit);
  41.         this.border_width = 10;
  42.         this.set_size_request (600, 500);
  43.         this.add(stack);
  44.  
  45.         this.key_press_event.connect ((key) => {
  46.             print(key.str + " ");
  47.             addNbar(double.parse(key.str));
  48.             return false;
  49.         });
  50.  
  51.     }
  52.  
  53.  
  54.     public void addNbar(double newV){
  55.         for(int i=0;i<10-1;i++){
  56.             barMass[i].set_value(barMass[i+1].get_value());
  57.         }
  58.         barMass[9].set_value(newV);  
  59.         widget.update((int) newV);
  60.     }
  61.  
  62.  
  63.     public static int main(string[] args){
  64.         Gtk.init(ref args);
  65.         var s = new StackClass();
  66.         s.show_all();
  67.         Gtk.main();
  68.         return 0;
  69.     }
  70. }
  71.  
  72. public class CustomWidget : DrawingArea {
  73.     public string text;
  74.     public int newPoint;
  75.     public int[] points;
  76.  
  77.     public CustomWidget () {
  78.         set_size_request (get_allocated_width (), get_allocated_height ());
  79.         points = new int [10];
  80.         int height = get_allocated_height ();print (@"полученная высота в начале $height");
  81.         for(int i=0;i<10;i++)points[i]=0;
  82.         //this.newPoint = txt;
  83.     }
  84.    // public void setNewPoint(int y) {this.newPoint=y;}
  85.     /* Widget is asked to draw itself */
  86.  
  87.     public override bool draw (Cairo.Context cr) {
  88.         int width = get_allocated_width ();
  89.         int height = get_allocated_height ();
  90.         print ("ширина = %d, высота = %d \n",width,height);
  91.         //цикл в котором отрисовываем массив points[j] с шагом в 1/10 экрана
  92.         for(int i=0,j=0 ;i<width-width / 10 ;i+=width / 10, j++) {
  93.             cr.line_to(i,height-((height/10) *points[j]));
  94.             print("j = %d ",j);
  95.             print(" x %d, y %d \n",i,height/10 *points[j]);
  96.         }
  97.        cr.line_to(10* width / 10,height-newPoint*height/10);
  98.        print(" x %d, y %d \n",10* width/10,newPoint*height/10);
  99.        cr.stroke ();
  100.     return false;
  101.     }
  102.     public  bool update(int y){
  103.         this.newPoint=y;
  104.         for(int i=0;i<10-1;i++) {
  105.             points[i]=points[i+1];
  106.         }
  107.         points[9]=y;
  108.  
  109.         redraw_canvas ();
  110.             return true;
  111.     }
  112.     private void redraw_canvas () {
  113.         var window = get_window ();
  114.         if (null == window) {
  115.             return;
  116.         }
  117.         var region = window.get_clip_region ();
  118.         // redraw the cairo canvas completely by exposing it
  119.         window.invalidate_region (region, true);
  120.         window.process_updates (true);
  121.     }
  122.  
  123.  
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement