shumate-scale.c 14.5 KB
Newer Older
1
2
/*
 * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com>
Marcus Lundblad's avatar
Marcus Lundblad committed
3
 * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se>
Corentin Noël's avatar
Corentin Noël committed
4
5
 * Copyright (C) 2020 Collabora Ltd. (https://www.collabora.com)
 * Copyright (C) 2020 Corentin Noël <corentin.noel@collabora.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
23
 * ShumateScale:
24
 *
Corentin Noël's avatar
Corentin Noël committed
25
26
27
28
 * A widget displaying a scale.
 *
 * # CSS nodes
 *
29
 * ```
Corentin Noël's avatar
Corentin Noël committed
30
31
 * map-scale
 * ├── label[.metric][.imperial]
32
 * ```
Corentin Noël's avatar
Corentin Noël committed
33
 *
34
 * `ShumateScale` uses a single CSS node with name map-scale, it has up to two
Corentin Noël's avatar
Corentin Noël committed
35
 * childs different labels.
36
37
 */

Marcus Lundblad's avatar
Marcus Lundblad committed
38
39
#include "shumate-scale.h"
#include "shumate-enum-types.h"
40
41

#include <glib-object.h>
42
#include <glib/gi18n.h>
43
44
45
46
47
#include <math.h>
#include <string.h>

enum
{
Corentin Noël's avatar
Corentin Noël committed
48
  PROP_UNIT = 1,
49
  PROP_MAX_SCALE_WIDTH,
50
  PROP_VIEWPORT,
Corentin Noël's avatar
Corentin Noël committed
51
  N_PROPERTIES,
52
53
};

Corentin Noël's avatar
Corentin Noël committed
54
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
55

56
struct _ShumateScale
57
{
Corentin Noël's avatar
Corentin Noël committed
58
  GtkWidget parent_instance;
59

Corentin Noël's avatar
Corentin Noël committed
60
  ShumateUnit unit;
61
62
  guint max_scale_width;

63
  ShumateViewport *viewport;
Corentin Noël's avatar
Corentin Noël committed
64
65
66

  GtkWidget *metric_label;
  GtkWidget *imperial_label;
67
68
};

Corentin Noël's avatar
Corentin Noël committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
G_DEFINE_TYPE (ShumateScale, shumate_scale, GTK_TYPE_WIDGET);

#define FEET_IN_METERS 3.280839895

#define FEET_IN_A_MILE 5280.0

/*
 * shumate_scale_compute_length:
 * @self: a #ShumateScale
 *
 * This loop will find the pretty value to display on the scale.
 * It will be run once for metric units, and twice for imperials
 * so that both feet and miles have pretty numbers.
 */
static gboolean
shumate_scale_compute_length (ShumateScale *self,
                              ShumateUnit   unit,
86
87
                              float        *out_scale_width,
                              float        *out_base,
Corentin Noël's avatar
Corentin Noël committed
88
89
90
                              gboolean     *out_is_small_unit)
{
  ShumateMapSource *map_source;
91
  double zoom_level;
92
93
94
95
  double lat, lon;
  float scale_width;
  float base;
  float factor;
Corentin Noël's avatar
Corentin Noël committed
96
  gboolean is_small_unit = TRUE;
97
  double m_per_pixel;
Corentin Noël's avatar
Corentin Noël committed
98
99
100
101
102
103
104
105
106
107
108
109

  g_assert (SHUMATE_IS_SCALE (self));

  if (out_scale_width)
    *out_scale_width = 0;

  if (out_base)
    *out_base = 1;

  if (out_is_small_unit)
    *out_is_small_unit = TRUE;

110
111
112
  if (!self->viewport)
    return FALSE;

113
114
115
116
  scale_width = self->max_scale_width;
  zoom_level = shumate_viewport_get_zoom_level (self->viewport);
  map_source = shumate_viewport_get_reference_map_source (self->viewport);
  if (!map_source)
Corentin Noël's avatar
Corentin Noël committed
117
118
    return FALSE;

119
120
  lat = shumate_location_get_latitude (SHUMATE_LOCATION (self->viewport));
  lon = shumate_location_get_longitude (SHUMATE_LOCATION (self->viewport));
Corentin Noël's avatar
Corentin Noël committed
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
  m_per_pixel = shumate_map_source_get_meters_per_pixel (map_source,
                                                         zoom_level,
                                                         lat,
                                                         lon);

  if (unit == SHUMATE_UNIT_IMPERIAL)
    m_per_pixel *= FEET_IN_METERS;  /* m_per_pixel is now in ft */

  do
    {
      /* Keep the previous power of 10 */
      base = floor (log (m_per_pixel * scale_width) / log (10));
      base = pow (10, base);

      /* How many times can it be fitted in our max scale width */
      g_assert (base > 0);
      g_assert (m_per_pixel * scale_width / base > 0);
      scale_width /= m_per_pixel * scale_width / base;
      g_assert (scale_width > 0);
      factor = floor (self->max_scale_width / scale_width);
      base *= factor;
      scale_width *= factor;

      if (unit == SHUMATE_UNIT_METRIC)
        {
          if (base / 1000.0 >= 1)
            {
              base /= 1000.0; /* base is now in km */
              is_small_unit = FALSE;
            }

          break;
        }
      else if (unit == SHUMATE_UNIT_IMPERIAL)
        {
          if (is_small_unit && base / FEET_IN_A_MILE >= 1)
            {
              m_per_pixel /= FEET_IN_A_MILE; /* m_per_pixel is now in miles */
              is_small_unit = FALSE;
              /* we need to recompute the base because 1000 ft != 1 mile */
            }
          else
            break;
        }
    } while (TRUE);


  if (out_scale_width)
    *out_scale_width = scale_width;
170

Corentin Noël's avatar
Corentin Noël committed
171
172
  if (out_base)
    *out_base = base;
173

Corentin Noël's avatar
Corentin Noël committed
174
175
176
177
178
179
180
181
182
  if (out_is_small_unit)
    *out_is_small_unit = is_small_unit;

  return TRUE;
}

static void
shumate_scale_on_scale_changed (ShumateScale *self)
{
183
184
  float metric_scale_width, metric_base;
  float imperial_scale_width, imperial_base;
Corentin Noël's avatar
Corentin Noël committed
185
  gboolean metric_is_small_unit, imperial_is_small_unit;
186
187
  g_autofree char *metric_label = NULL;
  g_autofree char *imperial_label = NULL;
Corentin Noël's avatar
Corentin Noël committed
188
189
190
191
192
193
194
195

  shumate_scale_compute_length (self, SHUMATE_UNIT_METRIC, &metric_scale_width, &metric_base, &metric_is_small_unit);
  shumate_scale_compute_length (self, SHUMATE_UNIT_IMPERIAL, &imperial_scale_width, &imperial_base, &imperial_is_small_unit);

  gtk_widget_set_size_request (self->metric_label, metric_scale_width, -1);
  gtk_widget_set_size_request (self->imperial_label, imperial_scale_width, -1);

  if (metric_is_small_unit)
196
    // m is the unit for meters
197
    metric_label = g_strdup_printf (_("%d m"), (int) metric_base);
Corentin Noël's avatar
Corentin Noël committed
198
  else
199
    // km is the unit for kilometers
200
    metric_label = g_strdup_printf (_("%d km"), (int) metric_base);
Corentin Noël's avatar
Corentin Noël committed
201
202
203
204

  gtk_label_set_label (GTK_LABEL (self->metric_label), metric_label);

  if (imperial_is_small_unit)
205
    // ft is the unit for feet
206
    imperial_label = g_strdup_printf (_("%d ft"), (int) imperial_base);
Corentin Noël's avatar
Corentin Noël committed
207
  else
208
    // mi is the unit for miles
209
    imperial_label = g_strdup_printf (_("%d mi"), (int) imperial_base);
Corentin Noël's avatar
Corentin Noël committed
210
211
212
213
214
215

  gtk_label_set_label (GTK_LABEL (self->imperial_label), imperial_label);
  gtk_widget_queue_resize (GTK_WIDGET (self));
}

static void
216
217
218
on_viewport_props_changed (ShumateScale *self,
                           G_GNUC_UNUSED GParamSpec *pspec,
                           ShumateViewport *viewport)
Corentin Noël's avatar
Corentin Noël committed
219
220
221
222
223
{
  shumate_scale_on_scale_changed (self);
}


224
static void
Marcus Lundblad's avatar
Marcus Lundblad committed
225
shumate_scale_get_property (GObject *object,
226
227
228
229
    guint prop_id,
    GValue *value,
    GParamSpec *pspec)
{
230
  ShumateScale *scale = SHUMATE_SCALE (object);
231
232
233

  switch (prop_id)
    {
Corentin Noël's avatar
Corentin Noël committed
234
235
    case PROP_UNIT:
      g_value_set_enum (value, scale->unit);
236
237
      break;

Corentin Noël's avatar
Corentin Noël committed
238
239
    case PROP_MAX_SCALE_WIDTH:
      g_value_set_uint (value, scale->max_scale_width);
240
241
      break;

242
243
244
245
    case PROP_VIEWPORT:
      g_value_set_object (value, scale->viewport);
      break;

246
247
248
249
250
251
252
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
Marcus Lundblad's avatar
Marcus Lundblad committed
253
shumate_scale_set_property (GObject *object,
254
255
256
257
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
Marcus Lundblad's avatar
Marcus Lundblad committed
258
  ShumateScale *scale = SHUMATE_SCALE (object);
259
260
261

  switch (prop_id)
    {
Corentin Noël's avatar
Corentin Noël committed
262
263
    case PROP_UNIT:
      shumate_scale_set_unit (scale, g_value_get_enum (value));
264
265
      break;

Corentin Noël's avatar
Corentin Noël committed
266
267
    case PROP_MAX_SCALE_WIDTH:
      shumate_scale_set_max_width (scale, g_value_get_uint (value));
268
269
      break;

270
    case PROP_VIEWPORT:
271
      shumate_scale_set_viewport (scale, g_value_get_object (value));
272
273
      break;

274
275
276
277
278
279
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
Marcus Lundblad's avatar
Marcus Lundblad committed
280
shumate_scale_dispose (GObject *object)
281
{
282
  ShumateScale *scale = SHUMATE_SCALE (object);
283

284
285
  g_signal_handlers_disconnect_by_data (scale->viewport, scale);
  g_clear_object (&scale->viewport);
Corentin Noël's avatar
Corentin Noël committed
286
287
  g_clear_pointer (&scale->metric_label, gtk_widget_unparent);
  g_clear_pointer (&scale->imperial_label, gtk_widget_unparent);
288

Marcus Lundblad's avatar
Marcus Lundblad committed
289
  G_OBJECT_CLASS (shumate_scale_parent_class)->dispose (object);
290
291
292
}

static void
Marcus Lundblad's avatar
Marcus Lundblad committed
293
shumate_scale_class_init (ShumateScaleClass *klass)
294
295
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Corentin Noël's avatar
Corentin Noël committed
296
297
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GdkDisplay *display;
298

Marcus Lundblad's avatar
Marcus Lundblad committed
299
300
301
  object_class->dispose = shumate_scale_dispose;
  object_class->get_property = shumate_scale_get_property;
  object_class->set_property = shumate_scale_set_property;
302
303

  /**
Marcus Lundblad's avatar
Marcus Lundblad committed
304
   * ShumateScale:max-width:
305
306
307
   *
   * The size of the map scale on screen in pixels.
   */
Corentin Noël's avatar
Corentin Noël committed
308
309
310
311
312
313
  obj_properties[PROP_MAX_SCALE_WIDTH] =
    g_param_spec_uint ("max-width",
                       "The width of the scale",
                       "The max width of the scale on screen",
                       1, G_MAXUINT, 150,
                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
314
315

  /**
Marcus Lundblad's avatar
Marcus Lundblad committed
316
   * ShumateScale:unit:
317
318
319
   *
   * The scale's units.
   */
Corentin Noël's avatar
Corentin Noël committed
320
321
322
323
324
325
326
  obj_properties[PROP_UNIT] =
    g_param_spec_enum ("unit",
                       "The scale's unit",
                       "The map scale's unit",
                       SHUMATE_TYPE_UNIT,
                       SHUMATE_UNIT_BOTH,
                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
327

328
329
330
331
332
333
334
335
336
337
  /**
   * ShumateScale:viewport:
   *
   * The viewport to use.
   */
  obj_properties[PROP_VIEWPORT] =
    g_param_spec_object ("viewport",
                         "The viewport",
                         "The viewport",
                         SHUMATE_TYPE_VIEWPORT,
338
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
339

Corentin Noël's avatar
Corentin Noël committed
340
  g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
341

342
  gtk_widget_class_set_css_name (widget_class, "map-scale");
Corentin Noël's avatar
Corentin Noël committed
343
  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
344

Corentin Noël's avatar
Corentin Noël committed
345
346
  display = gdk_display_get_default ();
  if (display)
347
    {
Corentin Noël's avatar
Corentin Noël committed
348
349
350
351
352
      g_autoptr(GtkCssProvider) provider = gtk_css_provider_new ();
      gtk_css_provider_load_from_resource (provider, "/org/gnome/shumate/scale.css");
      gtk_style_context_add_provider_for_display (display,
                                                  GTK_STYLE_PROVIDER (provider),
                                                  GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
353
354
355
356
    }
}

static void
Corentin Noël's avatar
Corentin Noël committed
357
shumate_scale_init (ShumateScale *self)
358
{
Corentin Noël's avatar
Corentin Noël committed
359
360
361
362
363
364
365
  GtkWidget *self_widget = GTK_WIDGET (self);
  GtkLayoutManager *layout = gtk_widget_get_layout_manager (self_widget);

  self->unit = SHUMATE_UNIT_BOTH;
  self->max_scale_width = 150;

  gtk_orientable_set_orientation (GTK_ORIENTABLE (layout), GTK_ORIENTATION_VERTICAL);
366
  gtk_widget_add_css_class (self_widget, "vertical");
Corentin Noël's avatar
Corentin Noël committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

  self->metric_label = gtk_label_new (NULL);
  g_object_set (G_OBJECT (self->metric_label),
                "xalign", 0.0f,
                "halign", GTK_ALIGN_START,
                "ellipsize", PANGO_ELLIPSIZE_END,
                NULL);
  gtk_widget_add_css_class (self->metric_label, "metric");
  self->imperial_label = gtk_label_new (NULL);
  g_object_set (G_OBJECT (self->imperial_label),
                "xalign", 0.0f,
                "halign", GTK_ALIGN_START,
                "ellipsize", PANGO_ELLIPSIZE_END,
                NULL);
  gtk_widget_add_css_class (self->imperial_label, "imperial");

  gtk_widget_insert_after (self->metric_label, self_widget, NULL);
  gtk_widget_insert_after (self->imperial_label, self_widget, self->metric_label);
385
386
387
388
}


/**
Marcus Lundblad's avatar
Marcus Lundblad committed
389
 * shumate_scale_new:
390
 * @viewport: (nullable): a #ShumateViewport
391
 *
Marcus Lundblad's avatar
Marcus Lundblad committed
392
 * Creates an instance of #ShumateScale.
393
 *
Marcus Lundblad's avatar
Marcus Lundblad committed
394
 * Returns: a new #ShumateScale.
395
 */
396
ShumateScale *
397
shumate_scale_new (ShumateViewport *viewport)
398
{
399
400
401
  return SHUMATE_SCALE (g_object_new (SHUMATE_TYPE_SCALE,
                                      "viewport", viewport,
                                      NULL));
402
403
404
405
}


/**
Marcus Lundblad's avatar
Marcus Lundblad committed
406
407
 * shumate_scale_set_max_width:
 * @scale: a #ShumateScale
408
409
410
411
412
 * @value: the number of pixels
 *
 * Sets the maximum width of the scale on the screen in pixels
 */
void
Marcus Lundblad's avatar
Marcus Lundblad committed
413
shumate_scale_set_max_width (ShumateScale *scale,
414
415
    guint value)
{
Marcus Lundblad's avatar
Marcus Lundblad committed
416
  g_return_if_fail (SHUMATE_IS_SCALE (scale));
417

418
419
420
  if (scale->max_scale_width == value)
    return;

421
  scale->max_scale_width = value;
422
  g_object_notify_by_pspec(G_OBJECT (scale), obj_properties[PROP_MAX_SCALE_WIDTH]);
Corentin Noël's avatar
Corentin Noël committed
423
  shumate_scale_on_scale_changed (scale);
424
425
426
427
}


/**
Marcus Lundblad's avatar
Marcus Lundblad committed
428
429
430
 * shumate_scale_set_unit:
 * @scale: a #ShumateScale
 * @unit: a #ShumateUnit
431
432
433
434
 *
 * Sets the scale unit.
 */
void
Marcus Lundblad's avatar
Marcus Lundblad committed
435
shumate_scale_set_unit (ShumateScale *scale,
Corentin Noël's avatar
Corentin Noël committed
436
                        ShumateUnit   unit)
437
{
Marcus Lundblad's avatar
Marcus Lundblad committed
438
  g_return_if_fail (SHUMATE_IS_SCALE (scale));
439

440
441
442
  if (scale->unit == unit)
    return;

Corentin Noël's avatar
Corentin Noël committed
443
444
445
446
447
  scale->unit = unit;

  gtk_widget_set_visible (scale->metric_label, unit == SHUMATE_UNIT_METRIC || unit == SHUMATE_UNIT_BOTH);
  gtk_widget_set_visible (scale->imperial_label, unit == SHUMATE_UNIT_IMPERIAL || unit == SHUMATE_UNIT_BOTH);

448
  g_object_notify_by_pspec(G_OBJECT (scale), obj_properties[PROP_UNIT]);
Corentin Noël's avatar
Corentin Noël committed
449
  shumate_scale_on_scale_changed (scale);
450
451
}

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**
 * shumate_scale_set_viewport:
 * @scale: a #ShumateScale
 * @viewport: (nullable): a #ShumateViewport
 *
 * Sets the scale viewport.
 */
void
shumate_scale_set_viewport (ShumateScale    *scale,
                            ShumateViewport *viewport)
{
  g_return_if_fail (SHUMATE_IS_SCALE (scale));

  if (scale->viewport)
    g_signal_handlers_disconnect_by_data (scale->viewport, scale);

  if (g_set_object (&scale->viewport, viewport))
    {
      g_object_notify_by_pspec(G_OBJECT (scale), obj_properties[PROP_VIEWPORT]);
      if (scale->viewport)
        {
473
474
475
          g_signal_connect_swapped (scale->viewport, "notify::latitude", G_CALLBACK (on_viewport_props_changed), scale);
          g_signal_connect_swapped (scale->viewport, "notify::zoom-level", G_CALLBACK (on_viewport_props_changed), scale);
          g_signal_connect_swapped (scale->viewport, "notify::reference-map-source", G_CALLBACK (on_viewport_props_changed), scale);
476
477
478
479
480
481
        }

      shumate_scale_on_scale_changed (scale);
    }
}

482
483

/**
Marcus Lundblad's avatar
Marcus Lundblad committed
484
485
 * shumate_scale_get_max_width:
 * @scale: a #ShumateScale
486
487
488
489
490
491
 *
 * Gets the maximum scale width.
 *
 * Returns: The maximum scale width in pixels.
 */
guint
Marcus Lundblad's avatar
Marcus Lundblad committed
492
shumate_scale_get_max_width (ShumateScale *scale)
493
{
Marcus Lundblad's avatar
Marcus Lundblad committed
494
  g_return_val_if_fail (SHUMATE_IS_SCALE (scale), FALSE);
495

496
  return scale->max_scale_width;
497
498
499
500
}


/**
Marcus Lundblad's avatar
Marcus Lundblad committed
501
502
 * shumate_scale_get_unit:
 * @scale: a #ShumateScale
503
504
505
506
507
 *
 * Gets the unit used by the scale.
 *
 * Returns: The unit used by the scale
 */
Marcus Lundblad's avatar
Marcus Lundblad committed
508
509
ShumateUnit
shumate_scale_get_unit (ShumateScale *scale)
510
{
Marcus Lundblad's avatar
Marcus Lundblad committed
511
  g_return_val_if_fail (SHUMATE_IS_SCALE (scale), FALSE);
512

Corentin Noël's avatar
Corentin Noël committed
513
  return scale->unit;
514
}
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

/**
 * shumate_scale_get_viewport:
 * @scale: a #ShumateScale
 *
 * Gets the viewport used by the scale.
 *
 * Returns: (transfer none) (nullable): The #ShumateViewport used by the scale
 */
ShumateViewport *
shumate_scale_get_viewport (ShumateScale *scale)
{
  g_return_val_if_fail (SHUMATE_IS_SCALE (scale), NULL);

  return scale->viewport;
}