summaryrefslogtreecommitdiff
path: root/src/gui/piePreview.vala
blob: eadaa8e89a1f1c7018ffd1cddb4cec128c5c1706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2016 by Simon Schneegans
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A custom widget displaying the preview of a Pie. It can be used to
/// configure the displayed Pie in various aspects.
/////////////////////////////////////////////////////////////////////////

class PiePreview : Gtk.DrawingArea {

    /////////////////////////////////////////////////////////////////////
    /// These get called when the last Slice is removed and when the
    /// first Slice is added respectively.
    /////////////////////////////////////////////////////////////////////

    public signal void on_last_slice_removed();
    public signal void on_first_slice_added();

    /////////////////////////////////////////////////////////////////////
    /// The internally used renderer to draw the Pie.
    /////////////////////////////////////////////////////////////////////

    private PiePreviewRenderer renderer = null;

    /////////////////////////////////////////////////////////////////////
    /// The window which pops up, when a Slice is added or edited.
    /////////////////////////////////////////////////////////////////////

    private NewSliceWindow? new_slice_window = null;

    /////////////////////////////////////////////////////////////////////
    /// A timer used for calculating the frame time.
    /////////////////////////////////////////////////////////////////////

    private GLib.Timer timer;

    /////////////////////////////////////////////////////////////////////
    /// True, when it is possible to drag a slice from this widget.
    /// False, when the user currently hovers over the add sign.
    /////////////////////////////////////////////////////////////////////

    private bool drag_enabled = false;

    /////////////////////////////////////////////////////////////////////
    /// The ID of the currently displayed Pie.
    /////////////////////////////////////////////////////////////////////

    private string current_id = "";

    /////////////////////////////////////////////////////////////////////
    /// The position from where a Slice-drag started.
    /////////////////////////////////////////////////////////////////////

    private int drag_start_index = -1;
    private string drag_start_id = "";

    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates the widget.
    /////////////////////////////////////////////////////////////////////

    public PiePreview() {
        this.renderer = new PiePreviewRenderer(this);

        this.draw.connect(this.on_draw);
        this.timer = new GLib.Timer();
        this.set_events(Gdk.EventMask.POINTER_MOTION_MASK
                      | Gdk.EventMask.LEAVE_NOTIFY_MASK
                      | Gdk.EventMask.ENTER_NOTIFY_MASK);

        // setup drag and drop
        this.enable_drag_source();

        Gtk.TargetEntry uri_dest = {"text/uri-list", 0, 0};
        Gtk.TargetEntry slice_dest = {"text/plain", Gtk.TargetFlags.SAME_WIDGET, 0};
        Gtk.TargetEntry[] destinations = { uri_dest, slice_dest };
        Gtk.drag_dest_set(this, Gtk.DestDefaults.ALL, destinations, Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK);

        this.drag_begin.connect(this.on_start_drag);
        this.drag_end.connect(this.on_end_drag);
        this.drag_data_received.connect(this.on_dnd_received);

        // connect mouse events
        this.drag_motion.connect(this.on_drag_move);
        this.leave_notify_event.connect(this.on_mouse_leave);
        this.enter_notify_event.connect(this.on_mouse_enter);
        this.motion_notify_event.connect_after(this.on_mouse_move);
        this.button_release_event.connect_after(this.on_button_release);
        this.button_press_event.connect_after(this.on_button_press);

        this.new_slice_window = new NewSliceWindow();
        this.new_slice_window.on_select.connect((new_action, as_new_slice, at_position) => {
            var pie = PieManager.all_pies[this.current_id];

            if (new_action.has_quickaction())
                renderer.disable_quickactions();

            if (as_new_slice) {
                pie.add_group(new_action, at_position+1);
                this.renderer.add_group(new_action, at_position+1);

                if (this.renderer.slice_count() == 1)
                    this.on_first_slice_added();
            } else {
                pie.update_group(new_action, at_position);
                this.renderer.update_group(new_action, at_position);
            }
        });

        this.renderer.on_edit_slice.connect((pos) => {
            this.new_slice_window.reload();

            this.new_slice_window.set_parent(this.get_toplevel() as Gtk.Window);
            this.new_slice_window.show();

            var pie = PieManager.all_pies[this.current_id];
            this.new_slice_window.set_action(pie.action_groups[pos], pos);
        });

        this.renderer.on_add_slice.connect((pos) => {
            this.new_slice_window.reload();

            this.new_slice_window.set_parent(this.get_toplevel() as Gtk.Window);
            this.new_slice_window.show();

            this.new_slice_window.set_default(this.current_id, pos);
        });

        this.renderer.on_remove_slice.connect((pos) => {

            var dialog = new Gtk.MessageDialog(this.get_toplevel() as Gtk.Window, Gtk.DialogFlags.MODAL,
                         Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
                         _("Do you really want to delete this Slice?"));

            dialog.response.connect((response) => {
                if (response == Gtk.ResponseType.YES) {
                    var pie = PieManager.all_pies[this.current_id];

                    pie.remove_group(pos);
                    this.renderer.remove_group(pos);

                    if (this.renderer.slice_count() == 0)
                        this.on_last_slice_removed();
                }
            });

            dialog.run();
            dialog.destroy();
        });
    }

    /////////////////////////////////////////////////////////////////////
    /// Sets the currently displayed Pie to the Pie with the given ID.
    /////////////////////////////////////////////////////////////////////

    public void set_pie(string id) {
        var style = this.get_style_context();

        this.current_id = id;
        this.override_background_color(Gtk.StateFlags.NORMAL, style.get_background_color(Gtk.StateFlags.NORMAL));
        this.renderer.load_pie(PieManager.all_pies[id]);

        if (id == this.drag_start_id) {
            this.renderer.hide_group(this.drag_start_index);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Begins the draw loop. It automatically ends, when the containing
    /// window becomes invisible.
    /////////////////////////////////////////////////////////////////////

    public void draw_loop() {
        this.timer.start();
        this.queue_draw();

        GLib.Timeout.add((uint)(1000.0/Config.global.refresh_rate), () => {
            this.queue_draw();
            return this.get_toplevel().visible;
        });
    }

    /////////////////////////////////////////////////////////////////////
    /// Called every frame.
    /////////////////////////////////////////////////////////////////////

    private bool on_draw(Cairo.Context ctx) {
        // store the frame time
        double frame_time = this.timer.elapsed();
        this.timer.reset();

        Gtk.Allocation allocation;
        this.get_allocation(out allocation);

        ctx.translate((int)(allocation.width*0.5), (int)(allocation.height*0.5));

        this.renderer.draw(frame_time, ctx);

        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse leaves the area of this widget.
    /////////////////////////////////////////////////////////////////////

    public bool on_mouse_leave(Gdk.EventCrossing event) {
        this.renderer.on_mouse_leave();
        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse enters the area of this widget.
    /////////////////////////////////////////////////////////////////////

    public bool on_mouse_enter(Gdk.EventCrossing event) {
        this.renderer.on_mouse_enter();
        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse moves in the area of this widget.
    /////////////////////////////////////////////////////////////////////

    private bool on_mouse_move(Gdk.EventMotion event) {
        this.renderer.set_dnd_mode(false);
        Gtk.Allocation allocation;
        this.get_allocation(out allocation);
        this.renderer.on_mouse_move(event.x-allocation.width*0.5, event.y-allocation.height*0.5);

        if (this.renderer.get_active_slice() < 0) this.disable_drag_source();
        else                                      this.enable_drag_source();

        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a mouse button is pressed.
    /////////////////////////////////////////////////////////////////////

    private bool on_button_press() {
        this.renderer.on_button_press();
        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a mouse button is released.
    /////////////////////////////////////////////////////////////////////

    private bool on_button_release() {
        if (!this.renderer.drag_n_drop_mode)
            this.renderer.on_button_release();
        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse is moved over this widget.
    /////////////////////////////////////////////////////////////////////

    private bool on_drag_move(Gdk.DragContext ctx, int x, int y, uint time) {
        this.renderer.set_dnd_mode(true);
        Gtk.Allocation allocation;
        this.get_allocation(out allocation);
        this.renderer.on_mouse_move(x-allocation.width*0.5, y-allocation.height*0.5);

        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the user tries to drag something from this widget.
    /////////////////////////////////////////////////////////////////////

    private void on_start_drag(Gdk.DragContext ctx) {
        this.drag_start_index = this.renderer.get_active_slice();
        this.drag_start_id = this.current_id;
        var icon = this.renderer.get_active_icon();
        var pixbuf = icon.to_pixbuf();

        this.renderer.hide_group(this.drag_start_index);
        Gtk.drag_set_icon_pixbuf(ctx, pixbuf, icon.size()/2, icon.size()/2);

        this.renderer.set_dnd_mode(true);
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the user finishes a drag operation on this widget.
    /// Only used for Slice-movement.
    /////////////////////////////////////////////////////////////////////

    private void on_end_drag(Gdk.DragContext context) {

        if (context.list_targets() != null) {

            int target_index = this.renderer.get_active_slice();
            this.renderer.set_dnd_mode(false);

            context.list_targets().foreach((target) => {
                Gdk.Atom target_type = (Gdk.Atom)target;
                if (target_type.name() == "text/plain") {
                    if (this.current_id == this.drag_start_id) {
                        var pie = PieManager.all_pies[this.current_id];
                        pie.move_group(this.drag_start_index, target_index);
                        this.renderer.show_hidden_group_at(target_index);
                    } else {
                        var src_pie = PieManager.all_pies[this.drag_start_id];
                        var dst_pie = PieManager.all_pies[this.current_id];
                        dst_pie.add_group(src_pie.action_groups[this.drag_start_index], target_index);
                        this.renderer.add_group(dst_pie.action_groups[target_index], target_index);

                        if (this.renderer.slices.size == 1)
                            this.on_first_slice_added();

                        if ((context.get_actions() & Gdk.DragAction.COPY) == 0)
                            src_pie.remove_group(this.drag_start_index);
                    }


                }
            });

            this.drag_start_index = -1;
            this.drag_start_id = "";
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the user finishes a drag operation on this widget.
    /// Only used for external drags.
    /////////////////////////////////////////////////////////////////////

    private void on_dnd_received(Gdk.DragContext context, int x, int y,
                                 Gtk.SelectionData selection_data, uint info, uint time_) {

        var pie = PieManager.all_pies[this.current_id];
        int position = this.renderer.get_active_slice();
        this.renderer.set_dnd_mode(false);

        foreach (var uri in selection_data.get_uris()) {
            pie.add_action(ActionRegistry.new_for_uri(uri), position);
            this.renderer.add_group(pie.action_groups[position], position);

            if (this.renderer.slices.size == 1)
                this.on_first_slice_added();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Enables this widget to be a source for drag operations.
    /////////////////////////////////////////////////////////////////////

    private void enable_drag_source() {
        if (!this.drag_enabled) {
            this.drag_enabled = true;
            Gtk.TargetEntry slice_source = {"text/plain", Gtk.TargetFlags.SAME_WIDGET | Gtk.TargetFlags.SAME_APP, 0};
            Gtk.TargetEntry[] sources = { slice_source };
            Gtk.drag_source_set(this, Gdk.ModifierType.BUTTON1_MASK, sources, Gdk.DragAction.MOVE | Gdk.DragAction.COPY);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Disables this widget to be a source for drag operations.
    /////////////////////////////////////////////////////////////////////

    private void disable_drag_source() {
        if (this.drag_enabled) {
            this.drag_enabled = false;
            Gtk.drag_source_unset(this);
        }
    }

}

}