process-row.vala 6.93 KB
Newer Older
Petr Štětka's avatar
Petr Štětka committed
1
2
namespace Usage
{
Petr Štětka's avatar
Petr Štětka committed
3
    public class ProcessRow : Gtk.Box
Petr Štětka's avatar
Petr Štětka committed
4
5
6
7
8
9
    {
		Gtk.Image icon;
        Gtk.Label title_label;
        Gtk.Label load_label;
        Gtk.Revealer revealer;
        Gtk.EventBox event_box;
Petr Štětka's avatar
Petr Štětka committed
10
        SubProcessListBox sub_process_list_box;
Petr Štětka's avatar
Petr Štětka committed
11
        bool in_box = false;
Petr Štětka's avatar
Petr Štětka committed
12
13
        uint pid;
        int value;
Petr Štětka's avatar
Petr Štětka committed
14
        string name;
Petr Štětka's avatar
Petr Štětka committed
15
16
        bool alive = true;
        bool group = false;
Petr Štětka's avatar
Petr Štětka committed
17
18
19
20
21

        //public bool is_headline { get; private set; }
        public bool showing_details { get; private set; }
        public bool max_usage { get; private set; }

Petr Štětka's avatar
Petr Štětka committed
22
        public ProcessRow(uint pid, int value, string name)
Petr Štětka's avatar
Petr Štětka committed
23
24
25
26
27
28
29
30
31
32
        {
            this.pid = pid;
            this.name = name;

            this.margin = 0;
            this.orientation = Gtk.Orientation.VERTICAL;
			var main_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
			main_box.margin = 12;
        	title_label = new Gtk.Label(name);  //TODO implement give name
        	load_label = new Gtk.Label(null);
Petr Štětka's avatar
Petr Štětka committed
33
34
        	load_label.ellipsize = Pango.EllipsizeMode.END;
        	load_label.max_width_chars = 30;
Petr Štětka's avatar
Petr Štětka committed
35
36
37
38
39
40
        	icon = new Gtk.Image.from_icon_name("dialog-error", Gtk.IconSize.BUTTON); //TODO implement give icon

            main_box.pack_start(icon, false, false, 10);
            main_box.pack_start(title_label, false, true, 5);
            main_box.pack_end(load_label, false, true, 10);

Petr Štětka's avatar
Petr Štětka committed
41
            sub_process_list_box = new SubProcessListBox();
Petr Štětka's avatar
Petr Štětka committed
42
43
44
45
46
47
48
49

            revealer = new Gtk.Revealer();
            revealer.add(sub_process_list_box);

            event_box = new Gtk.EventBox();
            event_box.add(main_box);

            event_box.button_press_event.connect ((event) => {
Petr Štětka's avatar
Petr Štětka committed
50
51
                if(group)
                    switch_details();
Petr Štětka's avatar
Petr Štětka committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
                return false;
            });

            event_box.enter_notify_event.connect ((event) => {
                in_box = true;
                style();
                return false;
            });

            event_box.leave_notify_event.connect ((event) => {
                in_box = false;
                style();
                return false;
            });

            var separator = new Gtk.Separator(Gtk.Orientation.VERTICAL);

            this.pack_start(event_box, false, true, 0);
            this.pack_end(revealer, false, true, 0);
Petr Štětka's avatar
Petr Štětka committed
71
72
            this.pack_end(separator, false, true, 0); //TODO Fix for last element

Petr Štětka's avatar
Petr Štětka committed
73
            set_value(pid, value);
Petr Štětka's avatar
Petr Štětka committed
74
75
76
77

            show_all();
        }

Petr Štětka's avatar
Petr Štětka committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        public void pre_update()
        {
            alive = false;
            sub_process_list_box.pre_update();
        }

        public void post_update()
        {
            sub_process_list_box.post_update();

            if(sub_process_list_box.get_sub_rows_count() == 1)
            {
                group = false;
                name = sub_process_list_box.get_first_name();
                pid = sub_process_list_box.get_first_pid();
Petr Štětka's avatar
Petr Štětka committed
93
                set_value(pid, sub_process_list_box.get_first_value());
Petr Štětka's avatar
Petr Štětka committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                sub_process_list_box.remove_all();
                if(showing_details)
                    hide_details();
            }

            update();
        }

        public bool is_in_subrows(uint pid)
        {
            return sub_process_list_box.is_in_table(pid);
        }

        public void add_sub_row(uint pid, int value, string name)
        {
            alive = true;
            if(sub_process_list_box.get_sub_rows_count() == 0)
            {
                sub_process_list_box.add_sub_row(this.pid, this.value, this.name);
                group = true;
            }

            sub_process_list_box.add_sub_row(pid, value, name);
        }

        public void update_sub_row(uint pid, int value)
        {
            alive = true;
            sub_process_list_box.update_sub_row(pid, value);
        }

        public string get_name()
        {
            return name;
        }

Petr Štětka's avatar
Petr Štětka committed
130
131
132
133
134
        public bool get_alive()
        {
            return alive;
        }

Petr Štětka's avatar
Petr Štětka committed
135
136
137
138
139
140
141
142
143
144
        public uint get_pid()
        {
            return pid;
        }

        public int get_value()
        {
            return value;
        }

Petr Štětka's avatar
Petr Štětka committed
145
        public void set_value(uint pid, int value)//TODO rename to set_value?
Petr Štětka's avatar
Petr Štětka committed
146
        {
Petr Štětka's avatar
Petr Štětka committed
147
148
149
150
151
152
153
154
155
156
            alive = true;
            if(!group)
                this.value = int.min(value, 100);
            else
            {
                if(sub_process_list_box.is_in_table(pid))
                    sub_process_list_box.update_sub_row(pid, value);
                else
                    sub_process_list_box.add_sub_row(pid, value, this.name);
            }
Petr Štětka's avatar
Petr Štětka committed
157
158
159
160
        }

        private void update()
        {
Petr Štětka's avatar
Petr Štětka committed
161
162
163
164
165
166
            if(group)
            {
                int max_value = 0;
                string values = "";
                foreach(int sub_value in sub_process_list_box.get_values())
                {
167
                    values += "   " + sub_value.to_string() + " %";
Petr Štětka's avatar
Petr Štětka committed
168
169
170
171
172
173
174
175
                    
                    if(sub_value > max_value)
                        max_value = sub_value;
                }
                this.value = max_value;

                load_label.set_label(values);
            }
Petr Štětka's avatar
Petr Štětka committed
176
            else
177
                load_label.set_label(value.to_string() + " %");
Petr Štětka's avatar
Petr Štětka committed
178

Petr Štětka's avatar
Petr Štětka committed
179
180
181
182
             if(value >= 90)
                 max_usage = true;
             else
                 max_usage = false;
Petr Štětka's avatar
Petr Štětka committed
183
184
185
186
187
188

            style();
        }

        private void style()
        {
Petr Štětka's avatar
Petr Štětka committed
189
190
191
192
193
            event_box.get_style_context().remove_class("processRow-max");
            event_box.get_style_context().remove_class("processRow-max-hover");
            event_box.get_style_context().remove_class("processRow");
            event_box.get_style_context().remove_class("processRow-hover");
            event_box.get_style_context().remove_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
194

Petr Štětka's avatar
Petr Štětka committed
195
196
            if(showing_details)
                event_box.get_style_context().add_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
197
198
            else
            {
Petr Štětka's avatar
Petr Štětka committed
199
                if(max_usage)
Petr Štětka's avatar
Petr Štětka committed
200
                {
Petr Štětka's avatar
Petr Štětka committed
201
202
203
204
                    if(in_box)
                        event_box.get_style_context().add_class("processRow-max-hover");
                    else
                        event_box.get_style_context().add_class("processRow-max");
Petr Štětka's avatar
Petr Štětka committed
205
206
207
208
                }
                else
                {
                    if(in_box)
Petr Štětka's avatar
Petr Štětka committed
209
                        event_box.get_style_context().add_class("processRow-hover");
Petr Štětka's avatar
Petr Štětka committed
210
                    else
Petr Štětka's avatar
Petr Štětka committed
211
                        event_box.get_style_context().add_class("processRow");
Petr Štětka's avatar
Petr Štětka committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                }
            }
        }

        private void hide_details()
        {
            showing_details = false;
            revealer.set_reveal_child(false);
            load_label.visible = true;
            style();
        }

        private void show_details()
        {
            showing_details = true;
            revealer.set_reveal_child(true);
            load_label.visible = false;
            style();
        }

        private void switch_details()
        {
            if(showing_details)
                hide_details();
            else
                show_details();
        }
    }
}