champlain-coordinate.c 6.08 KB
Newer Older
1
/*
Jiří Techet's avatar
Jiří Techet committed
2
 * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
 */

/**
 * SECTION:champlain-coordinate
21
 * @short_description: The simplest implementation of #ChamplainLocation
Jiří Techet's avatar
Jiří Techet committed
22
 *
Jiří Techet's avatar
Jiří Techet committed
23
 * #ChamplainCoordinate is a simple object implementing #ChamplainLocation.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 */

#include "champlain-coordinate.h"

#include "config.h"
#include "champlain-marshal.h"
#include "champlain-private.h"
#include "champlain-location.h"

enum
{
  PROP_0,
  PROP_LONGITUDE,
  PROP_LATITUDE,
};


41
static void set_location (ChamplainLocation *location,
42 43 44 45 46 47 48 49
    gdouble latitude,
    gdouble longitude);
static gdouble get_latitude (ChamplainLocation *location);
static gdouble get_longitude (ChamplainLocation *location);

static void location_interface_init (ChamplainLocationIface *iface);

G_DEFINE_TYPE_WITH_CODE (ChamplainCoordinate, champlain_coordinate, G_TYPE_INITIALLY_UNOWNED,
50
    G_IMPLEMENT_INTERFACE (CHAMPLAIN_TYPE_LOCATION, location_interface_init));
51

52
#define GET_PRIVATE(obj) \
53 54 55 56
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAMPLAIN_TYPE_COORDINATE, ChamplainCoordinatePrivate))

struct _ChamplainCoordinatePrivate
{
57 58
  gdouble longitude;
  gdouble latitude;
59 60 61 62 63 64 65 66 67 68 69 70 71 72
};

static void
champlain_coordinate_get_property (GObject *object,
    guint prop_id,
    GValue *value,
    GParamSpec *pspec)
{
  ChamplainCoordinate *coordinate = CHAMPLAIN_COORDINATE (object);
  ChamplainCoordinatePrivate *priv = coordinate->priv;

  switch (prop_id)
    {
    case PROP_LONGITUDE:
73
      g_value_set_double (value, priv->longitude);
74 75 76
      break;

    case PROP_LATITUDE:
77
      g_value_set_double (value, priv->latitude);
78
      break;
Jiří Techet's avatar
Jiří Techet committed
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
champlain_coordinate_set_property (GObject *object,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
  ChamplainCoordinate *coordinate = CHAMPLAIN_COORDINATE (object);
  ChamplainCoordinatePrivate *priv = coordinate->priv;

  switch (prop_id)
    {
    case PROP_LONGITUDE:
Jiří Techet's avatar
Jiří Techet committed
98
      {
99 100
        gdouble longitude = g_value_get_double (value);
        set_location (CHAMPLAIN_LOCATION (coordinate), priv->latitude, longitude);
Jiří Techet's avatar
Jiří Techet committed
101 102
        break;
      }
103 104

    case PROP_LATITUDE:
Jiří Techet's avatar
Jiří Techet committed
105
      {
106 107
        gdouble latitude = g_value_get_double (value);
        set_location (CHAMPLAIN_LOCATION (coordinate), latitude, priv->longitude);
Jiří Techet's avatar
Jiří Techet committed
108 109
        break;
      }
110 111 112 113 114 115 116 117

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
118
set_location (ChamplainLocation *location,
119 120 121 122 123 124 125
    gdouble latitude,
    gdouble longitude)
{
  g_return_if_fail (CHAMPLAIN_IS_COORDINATE (location));

  ChamplainCoordinatePrivate *priv = CHAMPLAIN_COORDINATE (location)->priv;

126 127
  priv->longitude = longitude;
  priv->latitude = latitude;
128 129 130 131 132 133 134 135 136 137 138 139 140

  g_object_notify (G_OBJECT (location), "latitude");
  g_object_notify (G_OBJECT (location), "longitude");
}


static gdouble
get_latitude (ChamplainLocation *location)
{
  g_return_val_if_fail (CHAMPLAIN_IS_COORDINATE (location), 0.0);

  ChamplainCoordinatePrivate *priv = CHAMPLAIN_COORDINATE (location)->priv;

141
  return priv->latitude;
142 143 144 145 146 147 148 149 150 151
}


static gdouble
get_longitude (ChamplainLocation *location)
{
  g_return_val_if_fail (CHAMPLAIN_IS_COORDINATE (location), 0.0);

  ChamplainCoordinatePrivate *priv = CHAMPLAIN_COORDINATE (location)->priv;

152
  return priv->longitude;
153 154 155 156 157 158 159 160
}


static void
location_interface_init (ChamplainLocationIface *iface)
{
  iface->get_latitude = get_latitude;
  iface->get_longitude = get_longitude;
161
  iface->set_location = set_location;
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
}


static void
champlain_coordinate_dispose (GObject *object)
{
  G_OBJECT_CLASS (champlain_coordinate_parent_class)->dispose (object);
}


static void
champlain_coordinate_finalize (GObject *object)
{
  G_OBJECT_CLASS (champlain_coordinate_parent_class)->finalize (object);
}


static void
champlain_coordinate_class_init (ChamplainCoordinateClass *coordinate_class)
{
  g_type_class_add_private (coordinate_class, sizeof (ChamplainCoordinatePrivate));

  GObjectClass *object_class = G_OBJECT_CLASS (coordinate_class);
  object_class->finalize = champlain_coordinate_finalize;
  object_class->dispose = champlain_coordinate_dispose;
  object_class->get_property = champlain_coordinate_get_property;
  object_class->set_property = champlain_coordinate_set_property;

  g_object_class_override_property (object_class,
Jiří Techet's avatar
Jiří Techet committed
191 192
      PROP_LONGITUDE,
      "longitude");
193 194

  g_object_class_override_property (object_class,
Jiří Techet's avatar
Jiří Techet committed
195 196
      PROP_LATITUDE,
      "latitude");
197 198 199 200 201 202
}


static void
champlain_coordinate_init (ChamplainCoordinate *coordinate)
{
203
  ChamplainCoordinatePrivate *priv = GET_PRIVATE (coordinate);
204 205 206

  coordinate->priv = priv;

207 208
  priv->latitude = 0.0;
  priv->longitude = 0.0;
209 210
}

Jiří Techet's avatar
Jiří Techet committed
211

Jiří Techet's avatar
Jiří Techet committed
212 213 214 215 216 217 218 219 220
/**
 * champlain_coordinate_new:
 *
 * Creates a new instance of #ChamplainCoordinate.
 *
 * Returns: the created instance.
 *
 * Since: 0.10
 */
221 222 223 224 225 226 227
ChamplainCoordinate *
champlain_coordinate_new ()
{
  return CHAMPLAIN_COORDINATE (g_object_new (CHAMPLAIN_TYPE_COORDINATE, NULL));
}


Jiří Techet's avatar
Jiří Techet committed
228 229 230 231
/**
 * champlain_coordinate_new_full:
 * @latitude: the latitude coordinate
 * @longitude: the longitude coordinate
Jiří Techet's avatar
Jiří Techet committed
232 233
 *
 * Creates a new instance of #ChamplainCoordinate initialized with the given
Jiří Techet's avatar
Jiří Techet committed
234 235 236 237 238 239
 * coordinates.
 *
 * Returns: the created instance.
 *
 * Since: 0.10
 */
240
ChamplainCoordinate *
Jiří Techet's avatar
Jiří Techet committed
241
champlain_coordinate_new_full (gdouble latitude,
242 243
    gdouble longitude)
{
Jiří Techet's avatar
Jiří Techet committed
244 245 246 247
  return CHAMPLAIN_COORDINATE (g_object_new (CHAMPLAIN_TYPE_COORDINATE,
          "latitude", latitude,
          "longitude", longitude,
          NULL));
248
}