process-row.vala 7.33 KB
Newer Older
1
2
using Posix;

Petr Štětka's avatar
Petr Štětka committed
3
4
namespace Usage
{
Petr Štětka's avatar
Petr Štětka committed
5
    public class ProcessRow : Gtk.Box
Petr Štětka's avatar
Petr Štětka committed
6
7
8
9
10
11
    {
		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
12
        SubProcessListBox sub_process_list_box;
Petr Štětka's avatar
Petr Štětka committed
13
        bool in_box = false;
14
        pid_t pid;   //TODO pid should be type of pid_t no uint.
Petr Štětka's avatar
Petr Štětka committed
15
        int value;
Petr Štětka's avatar
Petr Štětka committed
16
        string name;
Petr Štětka's avatar
Petr Štětka committed
17
18
        bool alive = true;
        bool group = false;
Petr Štětka's avatar
Petr Štětka committed
19
20
21
22
23

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

24
        public ProcessRow(pid_t pid, int value, string name)
Petr Štětka's avatar
Petr Štětka committed
25
26
27
28
29
30
31
32
33
34
        {
            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
35
36
        	load_label.ellipsize = Pango.EllipsizeMode.END;
        	load_label.max_width_chars = 30;
Petr Štětka's avatar
Petr Štětka committed
37
38
39
40
41
42
        	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
43
            sub_process_list_box = new SubProcessListBox();
Petr Štětka's avatar
Petr Štětka committed
44
45
46
47
48
49
50
51

            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
52
53
                if(group)
                    switch_details();
Petr Štětka's avatar
Petr Štětka committed
54
55
                else
                {
56
                    var dialog = new ProcessDialog(pid);
Petr Štětka's avatar
Petr Štětka committed
57
58
59
                    dialog.show_all ();
                }

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

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

            show_all();
        }

Petr Štětka's avatar
Petr Štětka committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        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
101
                set_value(pid, sub_process_list_box.get_first_value());
Petr Štětka's avatar
Petr Štětka committed
102
103
104
105
106
107
108
109
                sub_process_list_box.remove_all();
                if(showing_details)
                    hide_details();
            }

            update();
        }

110
        public bool is_in_subrows(pid_t pid)
Petr Štětka's avatar
Petr Štětka committed
111
112
113
114
        {
            return sub_process_list_box.is_in_table(pid);
        }

115
        public void add_sub_row(pid_t pid, int value, string name)
Petr Štětka's avatar
Petr Štětka committed
116
117
118
119
120
121
122
123
124
125
126
        {
            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);
        }

127
        public void update_sub_row(pid_t pid, int value)
Petr Štětka's avatar
Petr Štětka committed
128
129
130
131
132
133
134
135
136
137
        {
            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
138
139
140
141
142
        public bool get_alive()
        {
            return alive;
        }

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

        public int get_value()
        {
            return value;
        }

153
        public void set_value(pid_t pid, int value)
Petr Štětka's avatar
Petr Štětka committed
154
        {
Petr Štětka's avatar
Petr Štětka committed
155
            alive = true;
Petr Štětka's avatar
Petr Štětka committed
156
157
            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
158
            else
Petr Štětka's avatar
Petr Štětka committed
159
160
                if(group)
                   sub_process_list_box.add_sub_row(pid, value, name);
Petr Štětka's avatar
Petr Štětka committed
161
                else
Petr Štětka's avatar
Petr Štětka committed
162
163
164
165
166
167
168
169
170
171
                {
                    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
172
173
174
175
        }

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

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

            style();
        }

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

Petr Štětka's avatar
Petr Štětka committed
210
211
            if(showing_details)
                event_box.get_style_context().add_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
212
213
            else
            {
Petr Štětka's avatar
Petr Štětka committed
214
                if(max_usage)
Petr Štětka's avatar
Petr Štětka committed
215
                {
Petr Štětka's avatar
Petr Štětka committed
216
217
218
219
                    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
220
221
222
223
                }
                else
                {
                    if(in_box)
Petr Štětka's avatar
Petr Štětka committed
224
                        event_box.get_style_context().add_class("processRow-hover");
Petr Štětka's avatar
Petr Štětka committed
225
                    else
Petr Štětka's avatar
Petr Štětka committed
226
                        event_box.get_style_context().add_class("processRow");
Petr Štětka's avatar
Petr Štětka committed
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
254
255
                }
            }
        }

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