process-row.vala 7.26 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
                else
                {
                    var dialog = new ProcessDialog();
                    dialog.show_all ();
                }

Petr Štětka's avatar
Petr Štětka committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                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
77
78
            this.pack_end(separator, false, true, 0); //TODO Fix for last element

Petr Štětka's avatar
Petr Štětka committed
79
            set_value(pid, value);
Petr Štětka's avatar
Petr Štětka committed
80
81
82
83

            show_all();
        }

Petr Štětka's avatar
Petr Štětka committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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
99
                set_value(pid, sub_process_list_box.get_first_value());
Petr Štětka's avatar
Petr Štětka committed
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
130
131
132
133
134
135
                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
136
137
138
139
140
        public bool get_alive()
        {
            return alive;
        }

Petr Štětka's avatar
Petr Štětka committed
141
142
143
144
145
146
147
148
149
150
        public uint get_pid()
        {
            return pid;
        }

        public int get_value()
        {
            return value;
        }

Petr Štětka's avatar
Petr Štětka committed
151
        public void set_value(uint pid, int value)
Petr Štětka's avatar
Petr Štětka committed
152
        {
Petr Štětka's avatar
Petr Štětka committed
153
            alive = true;
Petr Štětka's avatar
Petr Štětka committed
154
155
            if(sub_process_list_box.is_in_table(pid))
                sub_process_list_box.update_sub_row(pid, value);
Petr Štětka's avatar
Petr Štětka committed
156
            else
Petr Štětka's avatar
Petr Štětka committed
157
158
                if(group)
                   sub_process_list_box.add_sub_row(pid, value, name);
Petr Štětka's avatar
Petr Štětka committed
159
                else
Petr Štětka's avatar
Petr Štětka committed
160
161
162
163
164
165
166
167
168
169
                {
                    if(pid == this.pid)
                        this.value = int.min(value, 100);
                    else
                    {
                        this.pid = pid;
                        this.value = int.min(value, 100);
                    }

                }
Petr Štětka's avatar
Petr Štětka committed
170
171
172
173
        }

        private void update()
        {
Petr Štětka's avatar
Petr Štětka committed
174
175
176
177
178
179
            if(group)
            {
                int max_value = 0;
                string values = "";
                foreach(int sub_value in sub_process_list_box.get_values())
                {
180
                    values += "   " + sub_value.to_string() + " %";
Petr Štětka's avatar
Petr Štětka committed
181
182
183
184
185
186
187
188
                    
                    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
189
            else
190
                load_label.set_label(value.to_string() + " %");
Petr Štětka's avatar
Petr Štětka committed
191

Petr Štětka's avatar
Petr Štětka committed
192
193
194
195
             if(value >= 90)
                 max_usage = true;
             else
                 max_usage = false;
Petr Štětka's avatar
Petr Štětka committed
196
197
198
199
200
201

            style();
        }

        private void style()
        {
Petr Štětka's avatar
Petr Štětka committed
202
203
204
205
206
            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
207

Petr Štětka's avatar
Petr Štětka committed
208
209
            if(showing_details)
                event_box.get_style_context().add_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
210
211
            else
            {
Petr Štětka's avatar
Petr Štětka committed
212
                if(max_usage)
Petr Štětka's avatar
Petr Štětka committed
213
                {
Petr Štětka's avatar
Petr Štětka committed
214
215
216
217
                    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
218
219
220
221
                }
                else
                {
                    if(in_box)
Petr Štětka's avatar
Petr Štětka committed
222
                        event_box.get_style_context().add_class("processRow-hover");
Petr Štětka's avatar
Petr Štětka committed
223
                    else
Petr Štětka's avatar
Petr Štětka committed
224
                        event_box.get_style_context().add_class("processRow");
Petr Štětka's avatar
Petr Štětka committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
                }
            }
        }

        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();
        }
    }
}