GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 62.2% 107 / 0 / 172
Functions: 82.1% 23 / 0 / 28
Branches: 44.2% 38 / 0 / 86

shumate/shumate-tile.c
Line Branch Exec Source
1 /*
2 * Copyright (C) 2008-2009 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
3 * Copyright (C) 2010-2013 Jiri Techet <techet@gmail.com>
4 * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se>
5 * Copyright (C) 2020 Collabora, Ltd. (https://www.collabora.com)
6 * Copyright (C) 2020 Corentin Noël <corentin.noel@collabora.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * ShumateTile:
25 *
26 * An object that represents map tiles. Tiles are loaded by a [class@MapSource].
27 */
28
29 #include "shumate-tile.h"
30
31 #include "shumate-enum-types.h"
32 #include "shumate-marshal.h"
33
34 #include <math.h>
35 #include <gdk/gdk.h>
36 #include <gio/gio.h>
37
38 struct _ShumateTile
39 {
40 GObject parent_instance;
41
42 guint x; /* The x position on the map (in pixels) */
43 guint y; /* The y position on the map (in pixels) */
44 guint size; /* The tile's width and height (only support square tiles */
45 guint zoom_level; /* The tile's zoom level */
46
47 ShumateState state; /* The tile state: loading, validation, done */
48 gboolean fade_in;
49
50 double scale_factor;
51
52 GdkPaintable *paintable;
53 GPtrArray *symbols;
54 };
55
56
4/6
shumate_tile_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5 times.
shumate_tile_get_type:
✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 7 taken 137 times.
✓ Branch 4 → 5 taken 5 times.
✗ Branch 4 → 7 not taken.
152 G_DEFINE_TYPE (ShumateTile, shumate_tile, G_TYPE_OBJECT);
57
58 enum
59 {
60 PROP_X = 1,
61 PROP_Y,
62 PROP_ZOOM_LEVEL,
63 PROP_SIZE,
64 PROP_STATE,
65 PROP_FADE_IN,
66 PROP_PAINTABLE,
67 PROP_SCALE_FACTOR,
68 N_PROPERTIES
69 };
70
71 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
72
73 static void
74 ✗ shumate_tile_get_property (GObject *object,
75 guint property_id,
76 GValue *value,
77 GParamSpec *pspec)
78 {
79 ✗ ShumateTile *self = SHUMATE_TILE (object);
80
81 ✗ switch (property_id)
82 {
83 ✗ case PROP_X:
84 ✗ g_value_set_uint (value, shumate_tile_get_x (self));
85 ✗ break;
86
87 ✗ case PROP_Y:
88 ✗ g_value_set_uint (value, shumate_tile_get_y (self));
89 ✗ break;
90
91 ✗ case PROP_ZOOM_LEVEL:
92 ✗ g_value_set_uint (value, shumate_tile_get_zoom_level (self));
93 ✗ break;
94
95 ✗ case PROP_SIZE:
96 ✗ g_value_set_uint (value, shumate_tile_get_size (self));
97 ✗ break;
98
99 ✗ case PROP_STATE:
100 ✗ g_value_set_enum (value, shumate_tile_get_state (self));
101 ✗ break;
102
103 ✗ case PROP_FADE_IN:
104 ✗ g_value_set_boolean (value, shumate_tile_get_fade_in (self));
105 ✗ break;
106
107 ✗ case PROP_PAINTABLE:
108 ✗ g_value_set_object (value, shumate_tile_get_paintable (self));
109 ✗ break;
110
111 ✗ case PROP_SCALE_FACTOR:
112 ✗ g_value_set_double (value, self->scale_factor);
113 ✗ break;
114
115 ✗ default:
116 ✗ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
117 }
118 ✗ }
119
120
121 static void
122 64 shumate_tile_set_property (GObject *object,
123 guint property_id,
124 const GValue *value,
125 GParamSpec *pspec)
126 {
127 64 ShumateTile *self = SHUMATE_TILE (object);
128
129
4/9
✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 6 taken 16 times.
✓ Branch 2 → 9 taken 16 times.
✓ Branch 2 → 12 taken 16 times.
✗ Branch 2 → 15 not taken.
✗ Branch 2 → 18 not taken.
✗ Branch 2 → 21 not taken.
✗ Branch 2 → 24 not taken.
✗ Branch 2 → 27 not taken.
64 switch (property_id)
130 {
131 16 case PROP_X:
132 16 shumate_tile_set_x (self, g_value_get_uint (value));
133 16 break;
134
135 16 case PROP_Y:
136 16 shumate_tile_set_y (self, g_value_get_uint (value));
137 16 break;
138
139 16 case PROP_ZOOM_LEVEL:
140 16 shumate_tile_set_zoom_level (self, g_value_get_uint (value));
141 16 break;
142
143 16 case PROP_SIZE:
144 16 shumate_tile_set_size (self, g_value_get_uint (value));
145 16 break;
146
147 ✗ case PROP_STATE:
148 ✗ shumate_tile_set_state (self, g_value_get_enum (value));
149 ✗ break;
150
151 ✗ case PROP_FADE_IN:
152 ✗ shumate_tile_set_fade_in (self, g_value_get_boolean (value));
153 ✗ break;
154
155 ✗ case PROP_PAINTABLE:
156 ✗ shumate_tile_set_paintable (self, g_value_get_object (value));
157 ✗ break;
158
159 ✗ case PROP_SCALE_FACTOR:
160 ✗ shumate_tile_set_scale_factor (self, g_value_get_double (value));
161 ✗ break;
162
163 ✗ default:
164 ✗ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
165 }
166 64 }
167
168
169 static void
170 16 shumate_tile_dispose (GObject *object)
171 {
172 16 ShumateTile *self = SHUMATE_TILE (object);
173
174
2/2
✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 4 taken 8 times.
16 g_clear_object (&self->paintable);
175
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 16 times.
16 g_clear_pointer (&self->symbols, g_ptr_array_unref);
176
177 16 G_OBJECT_CLASS (shumate_tile_parent_class)->dispose (object);
178 16 }
179
180
181 static void
182 5 shumate_tile_class_init (ShumateTileClass *klass)
183 {
184 5 GObjectClass *object_class = G_OBJECT_CLASS (klass);
185
186 5 object_class->get_property = shumate_tile_get_property;
187 5 object_class->set_property = shumate_tile_set_property;
188 5 object_class->dispose = shumate_tile_dispose;
189
190 /**
191 * ShumateTile:x:
192 *
193 * The x position of the tile
194 */
195 10 obj_properties[PROP_X] =
196 5 g_param_spec_uint ("x", NULL, NULL,
197 0,
198 G_MAXUINT,
199 0,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
201
202 /**
203 * ShumateTile:y:
204 *
205 * The y position of the tile
206 */
207 10 obj_properties[PROP_Y] =
208 5 g_param_spec_uint ("y", NULL, NULL,
209 0,
210 G_MAXUINT,
211 0,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
213
214 /**
215 * ShumateTile:zoom-level:
216 *
217 * The zoom level of the tile
218 */
219 10 obj_properties[PROP_ZOOM_LEVEL] =
220 5 g_param_spec_uint ("zoom-level", NULL, NULL,
221 0,
222 G_MAXUINT,
223 0,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
225
226 /**
227 * ShumateTile:size:
228 *
229 * The size of the tile in pixels
230 */
231 10 obj_properties[PROP_SIZE] =
232 5 g_param_spec_uint ("size", NULL, NULL,
233 0,
234 G_MAXUINT,
235 256,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
237
238 /**
239 * ShumateTile:state:
240 *
241 * The state of the tile
242 */
243 10 obj_properties[PROP_STATE] =
244 5 g_param_spec_enum ("state", NULL, NULL,
245 SHUMATE_TYPE_STATE,
246 SHUMATE_STATE_NONE,
247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
248
249 /**
250 * ShumateTile:fade-in:
251 *
252 * Specifies whether the tile should fade in when loading
253 */
254 10 obj_properties[PROP_FADE_IN] =
255 5 g_param_spec_boolean ("fade-in", NULL, NULL,
256 FALSE,
257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
258
259 /**
260 * ShumateTile:paintable:
261 *
262 * The [iface@Gdk.Paintable] backing the tile
263 */
264 10 obj_properties[PROP_PAINTABLE] =
265 5 g_param_spec_object ("paintable", NULL, NULL,
266 GDK_TYPE_PAINTABLE,
267 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
268
269 /**
270 * ShumateTile:scale-factor:
271 *
272 * The scale factor of the widget the tile will be displayed in.
273 *
274 * Since: 1.1
275 */
276 10 obj_properties[PROP_SCALE_FACTOR] =
277 5 g_param_spec_double ("scale-factor", NULL, NULL,
278 G_MINDOUBLE, G_MAXDOUBLE, 1.0,
279 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
280
281 5 g_object_class_install_properties (object_class,
282 N_PROPERTIES,
283 obj_properties);
284 5 }
285
286
287 static void
288 16 shumate_tile_init (ShumateTile *self)
289 {
290 16 self->state = SHUMATE_STATE_NONE;
291 16 self->scale_factor = 1.0;
292 16 }
293
294 /**
295 * shumate_tile_new:
296 *
297 * Creates an instance of #ShumateTile.
298 *
299 * Returns: a new #ShumateTile
300 */
301 ShumateTile *
302 ✗ shumate_tile_new (void)
303 {
304 ✗ return g_object_new (SHUMATE_TYPE_TILE, NULL);
305 }
306
307
308 /**
309 * shumate_tile_new_full:
310 * @x: the x position
311 * @y: the y position
312 * @size: the size in pixels
313 * @zoom_level: the zoom level
314 *
315 * Creates an instance of #ShumateTile.
316 *
317 * Returns: a #ShumateTile
318 */
319 ShumateTile *
320 16 shumate_tile_new_full (guint x,
321 guint y,
322 guint size,
323 guint zoom_level)
324 {
325 16 return g_object_new (SHUMATE_TYPE_TILE,
326 "x", x,
327 "y", y,
328 "zoom-level", zoom_level,
329 "size", size,
330 NULL);
331 }
332
333
334 /**
335 * shumate_tile_get_x:
336 * @self: the #ShumateTile
337 *
338 * Gets the tile's x position.
339 *
340 * Returns: the tile's x position
341 */
342 guint
343 44 shumate_tile_get_x (ShumateTile *self)
344 {
345
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 44 times.
44 g_return_val_if_fail (SHUMATE_TILE (self), 0);
346
347 44 return self->x;
348 }
349
350
351 /**
352 * shumate_tile_set_x:
353 * @self: the #ShumateTile
354 * @x: the position
355 *
356 * Sets the tile's x position
357 */
358 void
359 16 shumate_tile_set_x (ShumateTile *self,
360 guint x)
361 {
362
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
16 g_return_if_fail (SHUMATE_TILE (self));
363
364
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 16 times.
16 if (self->x == x)
365 return;
366
367 ✗ self->x = x;
368 ✗ g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_X]);
369 }
370
371
372 /**
373 * shumate_tile_get_y:
374 * @self: the #ShumateTile
375 *
376 * Gets the tile's y position.
377 *
378 * Returns: the tile's y position
379 */
380 guint
381 44 shumate_tile_get_y (ShumateTile *self)
382 {
383
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 44 times.
44 g_return_val_if_fail (SHUMATE_TILE (self), 0);
384
385 44 return self->y;
386 }
387
388
389 /**
390 * shumate_tile_set_y:
391 * @self: the #ShumateTile
392 * @y: the position
393 *
394 * Sets the tile's y position
395 */
396 void
397 16 shumate_tile_set_y (ShumateTile *self,
398 guint y)
399 {
400
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
16 g_return_if_fail (SHUMATE_TILE (self));
401
402
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 16 times.
16 if (self->y == y)
403 return;
404
405 ✗ self->y = y;
406 ✗ g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_Y]);
407 }
408
409
410 /**
411 * shumate_tile_get_zoom_level:
412 * @self: the #ShumateTile
413 *
414 * Gets the tile's zoom level.
415 *
416 * Returns: the tile's zoom level
417 */
418 guint
419 44 shumate_tile_get_zoom_level (ShumateTile *self)
420 {
421
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 44 times.
44 g_return_val_if_fail (SHUMATE_TILE (self), 0);
422
423 44 return self->zoom_level;
424 }
425
426
427 /**
428 * shumate_tile_set_zoom_level:
429 * @self: the #ShumateTile
430 * @zoom_level: the zoom level
431 *
432 * Sets the tile's zoom level
433 */
434 void
435 16 shumate_tile_set_zoom_level (ShumateTile *self,
436 guint zoom_level)
437 {
438
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
16 g_return_if_fail (SHUMATE_TILE (self));
439
440
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 14 times.
16 if (self->zoom_level == zoom_level)
441 return;
442
443 2 self->zoom_level = zoom_level;
444 2 g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_ZOOM_LEVEL]);
445 }
446
447
448 /**
449 * shumate_tile_get_size:
450 * @self: the #ShumateTile
451 *
452 * Gets the tile's size.
453 *
454 * Returns: the tile's size in pixels
455 */
456 guint
457 2 shumate_tile_get_size (ShumateTile *self)
458 {
459
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 g_return_val_if_fail (SHUMATE_TILE (self), 0);
460
461 2 return self->size;
462 }
463
464
465 /**
466 * shumate_tile_set_size:
467 * @self: the #ShumateTile
468 * @size: the size in pixels
469 *
470 * Sets the tile's size
471 */
472 void
473 16 shumate_tile_set_size (ShumateTile *self,
474 guint size)
475 {
476
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 16 times.
16 g_return_if_fail (SHUMATE_TILE (self));
477
478
1/2
✓ Branch 4 → 5 taken 16 times.
✗ Branch 4 → 6 not taken.
16 if (self->size == size)
479 return;
480
481 16 self->size = size;
482 16 g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_SIZE]);
483 }
484
485
486 /**
487 * shumate_tile_get_state:
488 * @self: the #ShumateTile
489 *
490 * Gets the current state of tile loading.
491 *
492 * Returns: the tile's #ShumateState
493 */
494 ShumateState
495 ✗ shumate_tile_get_state (ShumateTile *self)
496 {
497 ✗ g_return_val_if_fail (SHUMATE_TILE (self), SHUMATE_STATE_NONE);
498
499 ✗ return self->state;
500 }
501
502
503 /**
504 * shumate_tile_set_state:
505 * @self: the #ShumateTile
506 * @state: a #ShumateState
507 *
508 * Sets the tile's #ShumateState
509 */
510 void
511 18 shumate_tile_set_state (ShumateTile *self,
512 ShumateState state)
513 {
514
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 18 times.
18 g_return_if_fail (SHUMATE_TILE (self));
515
516
2/2
✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 6 taken 10 times.
18 if (state == self->state)
517 return;
518
519 8 self->state = state;
520 8 g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_STATE]);
521 }
522
523
524 /**
525 * shumate_tile_get_fade_in:
526 * @self: the #ShumateTile
527 *
528 * Checks whether the tile should fade in.
529 *
530 * Returns: the return value determines whether the tile should fade in when loading.
531 */
532 gboolean
533 ✗ shumate_tile_get_fade_in (ShumateTile *self)
534 {
535 ✗ g_return_val_if_fail (SHUMATE_TILE (self), FALSE);
536
537 ✗ return self->fade_in;
538 }
539
540
541 /**
542 * shumate_tile_set_fade_in:
543 * @self: the #ShumateTile
544 * @fade_in: determines whether the tile should fade in when loading
545 *
546 * Sets the flag determining whether the tile should fade in when loading
547 */
548 void
549 18 shumate_tile_set_fade_in (ShumateTile *self,
550 gboolean fade_in)
551 {
552
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 18 times.
18 g_return_if_fail (SHUMATE_TILE (self));
553
554
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 18 times.
18 if (self->fade_in == fade_in)
555 return;
556
557 ✗ self->fade_in = fade_in;
558 ✗ g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FADE_IN]);
559 }
560
561 /**
562 * shumate_tile_get_paintable:
563 * @self: the [class@Tile]
564 *
565 * Get the [iface@Gdk.Paintable] representing this tile.
566 *
567 * Returns: (transfer none) (nullable): A [iface@Gdk.Paintable]
568 */
569 GdkPaintable *
570 26 shumate_tile_get_paintable (ShumateTile *self)
571 {
572
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 26 times.
26 g_return_val_if_fail (SHUMATE_TILE (self), NULL);
573
574 26 return self->paintable;
575 }
576
577 /**
578 * shumate_tile_set_paintable:
579 * @self: the [class@Tile]
580 * @paintable: a [iface@Gdk.Paintable]
581 *
582 * Sets the [iface@Gdk.Paintable] representing this tile.
583 */
584 void
585 26 shumate_tile_set_paintable (ShumateTile *self,
586 GdkPaintable *paintable)
587 {
588
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 26 times.
26 g_return_if_fail (SHUMATE_TILE (self));
589
590
2/2
✓ Branch 5 → 6 taken 8 times.
✓ Branch 5 → 7 taken 18 times.
26 if (g_set_object (&self->paintable, paintable))
591 8 g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_PAINTABLE]);
592 }
593
594
595 /**
596 * shumate_tile_get_scale_factor:
597 * @self: a [class@Tile]
598 *
599 * Gets the scale factor of the tile.
600 *
601 * Returns: the scale factor
602 *
603 * Since: 1.1
604 */
605 double
606 2 shumate_tile_get_scale_factor (ShumateTile *self)
607 {
608
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
2 g_return_val_if_fail (SHUMATE_IS_TILE (self), 1.0);
609 2 return self->scale_factor;
610 }
611
612
613 /**
614 * shumate_tile_set_scale_factor:
615 * @self: the [class@Tile]
616 * @scale_factor: the scale factor
617 *
618 * Sets the scale factor of the tile.
619 *
620 * Since: 1.1
621 */
622 void
623 ✗ shumate_tile_set_scale_factor (ShumateTile *self,
624 double scale_factor)
625 {
626 ✗ g_return_if_fail (SHUMATE_IS_TILE (self));
627 ✗ g_return_if_fail (scale_factor >= G_MINDOUBLE);
628
629 ✗ if (self->scale_factor == scale_factor)
630 return;
631
632 ✗ self->scale_factor = scale_factor;
633 ✗ g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_SCALE_FACTOR]);
634 }
635
636
637
638 void
639 18 shumate_tile_set_symbols (ShumateTile *self, GPtrArray *symbols)
640 {
641
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 18 times.
18 g_return_if_fail (SHUMATE_IS_TILE (self));
642
643
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 18 times.
18 g_clear_pointer (&self->symbols, g_ptr_array_unref);
644
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 18 times.
18 if (symbols != NULL)
645 ✗ self->symbols = g_ptr_array_ref (symbols);
646 }
647
648
649 GPtrArray *
650 18 shumate_tile_get_symbols (ShumateTile *self)
651 {
652
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 18 times.
18 g_return_val_if_fail (SHUMATE_IS_TILE (self), NULL);
653
654 18 return self->symbols;
655 }
656