summaryrefslogtreecommitdiff
path: root/src/gui/piePreviewRenderer.vala
blob: dbd929ab96a1eb02bb2f01cbe2961cebd6d00017 (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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/////////////////////////////////////////////////////////////////////////
// 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/>.
/////////////////////////////////////////////////////////////////////////

using GLib.Math;

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A complex class which is able to draw the preview of a Pie. It can
/// manipulate the displayed Pie as well.
/////////////////////////////////////////////////////////////////////////

public class PiePreviewRenderer : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// These signals get emitted when a slice is added, removed or
    /// manipulated.
    /////////////////////////////////////////////////////////////////////

    public signal void on_add_slice(int position);
    public signal void on_remove_slice(int position);
    public signal void on_edit_slice(int position);

    /////////////////////////////////////////////////////////////////////
    /// True, when there is currently a drag going on.
    /////////////////////////////////////////////////////////////////////

    public bool drag_n_drop_mode { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// A list containing all SliceRenderers of this Pie.
    /////////////////////////////////////////////////////////////////////

    public Gee.ArrayList<PiePreviewSliceRenderer?> slices;

    /////////////////////////////////////////////////////////////////////
    /// When a Slice is moved within a Pie it is temporarily removed.
    /// If so, it is stored in this member.
    /////////////////////////////////////////////////////////////////////

    public PiePreviewSliceRenderer hidden_group { get; private set; default=null; }

    /////////////////////////////////////////////////////////////////////
    /// The add sign which indicates that a new Slice could be added.
    /////////////////////////////////////////////////////////////////////

    private PiePreviewAddSign add_sign = null;

    /////////////////////////////////////////////////////////////////////
    /// The object which renders the name of the currently selected Slice
    /// in the middle.
    /////////////////////////////////////////////////////////////////////

    private PiePreviewCenter center_renderer = null;
    private enum CenterDisplay { NONE, ACTIVE_SLICE, DROP, ADD, DELETE }

    /////////////////////////////////////////////////////////////////////
    /// Some members storing some inter-frame-information.
    /////////////////////////////////////////////////////////////////////

    private int active_slice = -1;
    private double angle = 0.0;
    private double mouse_x = 0.0;
    private double mouse_y = 0.0;

    /////////////////////////////////////////////////////////////////////
    /// The parent DrawingArea.
    /////////////////////////////////////////////////////////////////////

    public unowned Gtk.DrawingArea parent;

    /////////////////////////////////////////////////////////////////////
    /// C'tor, initializes members.
    /////////////////////////////////////////////////////////////////////

    public PiePreviewRenderer(Gtk.DrawingArea parent) {
        this.parent = parent;
        this.slices = new Gee.ArrayList<PiePreviewSliceRenderer?>();
        this.center_renderer = new PiePreviewCenter(this);
        this.add_sign = new PiePreviewAddSign(this);
        this.add_sign.load();

        this.add_sign.on_clicked.connect((pos) => {
            this.on_add_slice(pos);
        });
    }

    /////////////////////////////////////////////////////////////////////
    /// Loads an Pie. All members are initialized accordingly.
    /////////////////////////////////////////////////////////////////////

    public void load_pie(Pie pie) {
        this.slices.clear();

        foreach (var group in pie.action_groups) {
            var renderer = new PiePreviewSliceRenderer(this);
            renderer.load(group);

            this.add_slice_renderer(renderer);
            this.connect_siganls(renderer);
        }

        this.active_slice = -1;
        this.update_sizes();
        this.update_positions(false);
    }

    /////////////////////////////////////////////////////////////////////
    /// Enables or disables the drag n dropn mode.
    /////////////////////////////////////////////////////////////////////

    public void set_dnd_mode(bool dnd) {
        if (this.drag_n_drop_mode != dnd) {
            this.drag_n_drop_mode = dnd;
            this.update_positions();
            this.update_sizes();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the number of Slices.
    /////////////////////////////////////////////////////////////////////

    public int slice_count() {
        if (this.drag_n_drop_mode && !(this.slices.size == 0))
            return slices.size+1;

        return slices.size;
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the index of the currently hovered Slice.
    /////////////////////////////////////////////////////////////////////

    public int get_active_slice() {
        if (this.slices.size == 0)
            return 0;

        if (this.drag_n_drop_mode)
            return (int)(this.angle/(2*PI)*this.slice_count() + 0.5) % this.slice_count();

        return this.active_slice;
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the Icon of the currently hovered Slice.
    /////////////////////////////////////////////////////////////////////

    public Icon get_active_icon() {
        if (this.active_slice >= 0 && this.active_slice < this.slices.size)
            return this.slices[this.active_slice].icon;
        else
            return new Icon("", 24);
    }

    /////////////////////////////////////////////////////////////////////
    /// Draws the entire Pie to the given context.
    /////////////////////////////////////////////////////////////////////

    public void draw(double frame_time, Cairo.Context ctx) {
        this.add_sign.draw(frame_time, ctx);
        this.center_renderer.draw(frame_time, ctx);

        foreach (var slice in this.slices)
            slice.draw(frame_time, ctx);
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse leaves the drawing area of this renderer.
    /////////////////////////////////////////////////////////////////////

    public void on_mouse_leave() {
        this.add_sign.hide();
        this.update_positions();
        this.update_center(CenterDisplay.NONE);

        foreach (var slice in this.slices)
            slice.on_mouse_leave();
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse enters the drawing area of this renderer.
    /////////////////////////////////////////////////////////////////////

    public void on_mouse_enter() {
        this.add_sign.show();
        this.update_positions();
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when the mouse moves in the drawing area of this renderer.
    /////////////////////////////////////////////////////////////////////

    public void on_mouse_move(double x, double y) {
        this.mouse_x = x;
        this.mouse_y = y;

        this.angle = acos(x/sqrt(x*x + y*y));
        if (y < 0) this.angle = 2*PI - this.angle;

        if (!this.drag_n_drop_mode)
            this.active_slice = -1;

        bool delete_hovered = false;

        for (int i=0; i<this.slices.size; ++i)
            if (slices[i].on_mouse_move(this.angle, x, y) && !this.drag_n_drop_mode) {
                this.active_slice = i;
                delete_hovered = slices[i].delete_hovered;
            }

        if (this.drag_n_drop_mode)      this.update_center(CenterDisplay.DROP);
        else if (this.active_slice < 0) this.update_center(CenterDisplay.ADD);
        else if (delete_hovered)        this.update_center(CenterDisplay.DELETE);
        else                            this.update_center(CenterDisplay.ACTIVE_SLICE);

        this.add_sign.on_mouse_move(this.angle);

        this.update_positions();
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a mouse button is pressed over this renderer.
    /////////////////////////////////////////////////////////////////////

    public void on_button_press() {
        for (int i=0; i<this.slices.size; ++i)
            this.slices[i].on_button_press(this.mouse_x, this.mouse_y);
        this.add_sign.on_button_press(this.mouse_x, this.mouse_y);
    }

    /////////////////////////////////////////////////////////////////////
    /// Called when a mouse button is released over this renderer.
    /////////////////////////////////////////////////////////////////////

    public void on_button_release() {
        for (int i=0; i<this.slices.size; ++i)
            this.slices[i].on_button_release(this.mouse_x, this.mouse_y);
        this.add_sign.on_button_release(this.mouse_x, this.mouse_y);
    }

    /////////////////////////////////////////////////////////////////////
    /// Adds a new Slice to the renderer.
    /////////////////////////////////////////////////////////////////////

    public void add_group(ActionGroup group, int at_position = -1) {
        var renderer = new PiePreviewSliceRenderer(this);
        renderer.load(group);
        this.add_slice_renderer(renderer, at_position);
        this.connect_siganls(renderer);
    }

    /////////////////////////////////////////////////////////////////////
    /// Removes a Slice from the renderer.
    /////////////////////////////////////////////////////////////////////

    public void remove_group(int index) {
        if (this.slices.size > index) {
            this.slices.remove_at(index);
            this.update_positions();
            this.update_sizes();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Hides the Slice at the given position temporarily.
    /////////////////////////////////////////////////////////////////////

    public void hide_group(int index) {
        if (this.slices.size > index) {
            this.hidden_group = this.slices[index];
            this.remove_group(index);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Re-shows a Slice which has been hidden before.
    /////////////////////////////////////////////////////////////////////

    public void show_hidden_group_at(int index) {
        if (this.slices.size >= index && this.hidden_group != null) {
            this.hidden_group.set_position(index, false);
            this.add_slice_renderer(this.hidden_group, index);
            this.hidden_group = null;
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Updates a Slice at the given position.
    /////////////////////////////////////////////////////////////////////

    public void update_group(ActionGroup group, int index) {
        if (this.slices.size > index) {
            var renderer = new PiePreviewSliceRenderer(this);
            this.slices.set(index, renderer);
            renderer.load(group);

            this.connect_siganls(renderer);

            this.update_positions(false);
            this.update_sizes();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Disables all quickactions of this pie preview.
    /////////////////////////////////////////////////////////////////////

    public void disable_quickactions() {
        foreach (var slice in this.slices)
            slice.disable_quickactions();
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method which adds a new Slice to the given position.
    /////////////////////////////////////////////////////////////////////

    private void add_slice_renderer(PiePreviewSliceRenderer renderer, int at_position = -1) {
        if (at_position < 0 || at_position >= this.slices.size)
            this.slices.add(renderer);
        else
            this.slices.insert(at_position, renderer);

        this.update_positions(false);
        this.update_sizes();
    }

    /////////////////////////////////////////////////////////////////////
    /// Helper method which connects all neccessary signals of a newly
    /// added Slice.
    /////////////////////////////////////////////////////////////////////

    private void connect_siganls(PiePreviewSliceRenderer renderer) {
        renderer.on_clicked.connect((pos) => {
            this.on_edit_slice(pos);
        });

        renderer.on_remove.connect((pos) => {
            this.on_remove_slice(pos);
        });
    }

    /////////////////////////////////////////////////////////////////////
    /// Moves all slices to their positions. This may happen smoothly if
    /// desired.
    /////////////////////////////////////////////////////////////////////

    private void update_positions(bool smoothly = true) {
        if (this.slices.size > 0) {
            if (this.add_sign.visible) {
                int add_position = 0;
                add_position = (int)(this.angle/(2*PI)*this.slice_count()) % this.slice_count();
                this.add_sign.set_position(add_position);

                for (int i=0; i<this.slices.size; ++i) {
                    this.slices[i].set_position(i, smoothly);
                }

            } else if (this.drag_n_drop_mode) {
                int add_position = 0;
                add_position = (int)(this.angle/(2*PI)*this.slice_count() + 0.5) % this.slice_count();

                for (int i=0; i<this.slices.size; ++i) {
                    this.slices[i].set_position(i >= add_position ? i+1 : i, smoothly);
                }

                this.update_center(CenterDisplay.DROP);

            } else {
                for (int i=0; i<this.slices.size; ++i) {
                    this.slices[i].set_position(i, smoothly);
                }

                if (this.active_slice < 0)  this.update_center(CenterDisplay.NONE);
                else                        this.update_center(CenterDisplay.ACTIVE_SLICE);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Resizes all slices to their new sizes. This may happen smoothly
    /// if desired.
    /////////////////////////////////////////////////////////////////////

    private void update_sizes() {
        double size = 1.0;
        if (this.slice_count() > 20)     size = 0.5;
        else if (this.slice_count() > 8) size = 1.0 - (double)(this.slice_count() - 8)/24.0;

        this.add_sign.set_size(size);

        for (int i=0; i<this.slices.size; ++i)
            this.slices[i].set_size(size);
    }

    /////////////////////////////////////////////////////////////////////
    /// Displays a new text in the middle of the preview.
    /////////////////////////////////////////////////////////////////////

    private void update_center(CenterDisplay display) {
        switch (display) {
            case CenterDisplay.ACTIVE_SLICE:
                if (this.active_slice >= 0 && this.active_slice < this.slices.size)
                    this.center_renderer.set_text("<b>" + GLib.Markup.escape_text(slices[this.active_slice].name) + "</b>\n<small>"
                                            + _("Click to edit") + "\n" + _("Drag to move") + "</small>");
                break;
            case CenterDisplay.ADD:
                this.center_renderer.set_text("<small>" + _("Click to add a new Slice") + "</small>");
                break;
            case CenterDisplay.DROP:
                if (hidden_group == null)
                    this.center_renderer.set_text("<small>" + _("Drop to add as new Slice") + "</small>");
                else
                    this.center_renderer.set_text("<b>" + GLib.Markup.escape_text(this.hidden_group.name) + "</b>\n<small>"
                                            + _("Drop to move Slice") + "</small>");
                break;
            case CenterDisplay.DELETE:
                if (this.active_slice >= 0 && this.active_slice < this.slices.size)
                    this.center_renderer.set_text("<b>" + GLib.Markup.escape_text(slices[this.active_slice].name) + "</b>\n<small>"
                                            + _("Click to delete") + "\n" + _("Drag to move") + "</small>");
                break;
            default:
                this.center_renderer.set_text("");
                break;
        }
    }
}

}