summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/listview_weather.c
blob: 5feb2e144e20f8240c28ea357d847a5d26258f1f (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
/* Lists/Weather
 *
 * This demo shows a few of the rarer features of GtkListView and
 * how they can be used to display weather information.
 *
 * The hourly weather info uses a horizontal listview. This is easy
 * to achieve because GtkListView implements the GtkOrientable interface.
 * To make the items in the list stand out more, the listview uses
 * separators.
 *
 * A GtkNoSelectionModel is used to make sure no item in the list can be
 * selected. All other interactions with the items is still possible.
 *
 * The dataset used here has 70 000 items.
 */

#include <gtk/gtk.h>

#define GTK_TYPE_WEATHER_INFO (gtk_weather_info_get_type ())

G_DECLARE_FINAL_TYPE (GtkWeatherInfo, gtk_weather_info, GTK, WEATHER_INFO, GObject)

typedef enum {
  GTK_WEATHER_CLEAR,
  GTK_WEATHER_FEW_CLOUDS,
  GTK_WEATHER_FOG,
  GTK_WEATHER_OVERCAST,
  GTK_WEATHER_SCATTERED_SHOWERS,
  GTK_WEATHER_SHOWERS,
  GTK_WEATHER_SNOW,
  GTK_WEATHER_STORM
} GtkWeatherType;

struct _GtkWeatherInfo
{
  GObject parent_instance;

  gint64 timestamp;
  int temperature;
  GtkWeatherType weather_type;
};

struct _GtkWeatherInfoClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (GtkWeatherInfo, gtk_weather_info, G_TYPE_OBJECT)

static void
gtk_weather_info_class_init (GtkWeatherInfoClass *klass)
{
}

static void
gtk_weather_info_init (GtkWeatherInfo *self)
{
}

static GtkWeatherInfo *
gtk_weather_info_new (GDateTime      *timestamp,
                      GtkWeatherInfo *copy_from)
{
  GtkWeatherInfo *result;

  result = g_object_new (GTK_TYPE_WEATHER_INFO, NULL);

  result->timestamp = g_date_time_to_unix (timestamp);
  if (copy_from)
    {
      result->temperature = copy_from->temperature;
      result->weather_type = copy_from->weather_type;
    }

  return result;
}

static GDateTime *
parse_timestamp (const char *string,
                 GTimeZone  *_tz)
{
  char *with_seconds;
  GDateTime *result;

  with_seconds = g_strconcat (string, ":00", NULL);
  result = g_date_time_new_from_iso8601 (with_seconds, _tz);
  g_free (with_seconds);

  return result;
}

static GtkWeatherType
parse_weather_type (const char     *clouds,
                    const char     *precip,
                    GtkWeatherType  fallback)
{
  if (strstr (precip, "SN"))
    return GTK_WEATHER_SNOW;

  if (strstr (precip, "TS"))
    return GTK_WEATHER_STORM;

  if (strstr (precip, "DZ"))
    return GTK_WEATHER_SCATTERED_SHOWERS;

  if (strstr (precip, "SH") || strstr (precip, "RA"))
    return GTK_WEATHER_SHOWERS;

  if (strstr (precip, "FG"))
    return GTK_WEATHER_FOG;

  if (g_str_equal (clouds, "M") ||
      g_str_equal (clouds, ""))
    return fallback;

  if (strstr (clouds, "OVC") ||
      strstr (clouds, "BKN"))
    return GTK_WEATHER_OVERCAST;

  if (strstr (clouds, "BKN") ||
      strstr (clouds, "SCT"))
    return GTK_WEATHER_FEW_CLOUDS;

  if (strstr (clouds, "VV"))
    return GTK_WEATHER_FOG;

  return GTK_WEATHER_CLEAR;
}

static double
parse_temperature (const char *s,
                   double      fallback)
{
  char *endptr;
  double d;

  d = g_ascii_strtod (s, &endptr);
  if (*endptr != '\0')
    return fallback;

  return d;
}

static GListModel *
create_weather_model (void)
{
  GListStore *store;
  GTimeZone *utc;
  GDateTime *timestamp;
  GtkWeatherInfo *info;
  GBytes *data;
  char **lines;
  guint i;

  store = g_list_store_new (GTK_TYPE_WEATHER_INFO);
  data = g_resources_lookup_data ("/listview_weather/listview_weather.txt", 0, NULL);
  lines = g_strsplit (g_bytes_get_data (data, NULL), "\n", 0);

  utc = g_time_zone_new_utc ();
  timestamp = g_date_time_new (utc, 2011, 1, 1, 0, 0, 0);
  info = gtk_weather_info_new (timestamp, NULL);
  g_list_store_append (store, info);
  g_object_unref (info);

  for (i = 0; lines[i] != NULL && *lines[i]; i++)
    {
      char **fields;
      GDateTime *date;

      fields = g_strsplit (lines[i], ",", 0);
      date = parse_timestamp (fields[0], utc);
      while (g_date_time_difference (date, timestamp) > 30 * G_TIME_SPAN_MINUTE)
        {
          GDateTime *new_timestamp = g_date_time_add_hours (timestamp, 1);
          g_date_time_unref (timestamp);
          timestamp = new_timestamp;
          info = gtk_weather_info_new (timestamp, info);
          g_list_store_append (store, info);
          g_object_unref (info);
        }

      info->temperature = parse_temperature (fields[1], info->temperature);
      info->weather_type = parse_weather_type (fields[2], fields[3], info->weather_type);
      g_date_time_unref (date);
      g_strfreev (fields);
    }

  g_date_time_unref (timestamp);
  g_strfreev (lines);
  g_bytes_unref (data);
  g_time_zone_unref (utc);

  return G_LIST_MODEL (store);
}

static void
setup_widget (GtkSignalListItemFactory *factory,
              GtkListItem              *list_item)
{
  GtkWidget *box, *child;

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_list_item_set_child (list_item, box);

  child = gtk_label_new (NULL);
  gtk_label_set_width_chars (GTK_LABEL (child), 5);
  gtk_box_append (GTK_BOX (box), child);

  child = gtk_image_new ();
  gtk_image_set_icon_size (GTK_IMAGE (child), GTK_ICON_SIZE_LARGE);
  gtk_box_append (GTK_BOX (box), child);

  child = gtk_label_new (NULL);
  gtk_widget_set_vexpand (child, TRUE);
  gtk_widget_set_valign (child, GTK_ALIGN_END);
  gtk_label_set_width_chars (GTK_LABEL (child), 4);
  gtk_box_append (GTK_BOX (box), child);
}

static void
bind_widget (GtkSignalListItemFactory *factory,
             GtkListItem              *list_item)
{
  GtkWidget *box, *child;
  GtkWeatherInfo *info;
  GDateTime *timestamp;
  char *s;

  box = gtk_list_item_get_child (list_item);
  info = gtk_list_item_get_item (list_item);

  child = gtk_widget_get_first_child (box);
  timestamp = g_date_time_new_from_unix_utc (info->timestamp);
  s = g_date_time_format (timestamp, "%R");
  gtk_label_set_text (GTK_LABEL (child), s);
  g_free (s);
  g_date_time_unref (timestamp);

  child = gtk_widget_get_next_sibling (child);
  switch (info->weather_type)
  {
  case GTK_WEATHER_CLEAR:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-clear-symbolic");
    break;
  case GTK_WEATHER_FEW_CLOUDS:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-few-clouds-symbolic");
    break;
  case GTK_WEATHER_FOG:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-fog-symbolic");
    break;
  case GTK_WEATHER_OVERCAST:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-overcast-symbolic");
    break;
  case GTK_WEATHER_SCATTERED_SHOWERS:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-showers-scattered-symbolic");
    break;
  case GTK_WEATHER_SHOWERS:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-showers-symbolic");
    break;
  case GTK_WEATHER_SNOW:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-snow-symbolic");
    break;
  case GTK_WEATHER_STORM:
    gtk_image_set_from_icon_name (GTK_IMAGE (child), "weather-storm-symbolic");
    break;
  default:
    gtk_image_clear (GTK_IMAGE (child));
    break;
  }


  child = gtk_widget_get_next_sibling (child);
  s = g_strdup_printf ("%d°", info->temperature);
  gtk_label_set_text (GTK_LABEL (child), s);
  g_free (s);
}

static GtkWidget *window = NULL;

GtkWidget *
create_weather_view (void)
{
  GtkWidget *listview;
  GtkSelectionModel *model;
  GtkListItemFactory *factory;

  factory = gtk_signal_list_item_factory_new ();
  g_signal_connect (factory, "setup", G_CALLBACK (setup_widget), NULL);
  g_signal_connect (factory, "bind", G_CALLBACK (bind_widget), NULL);
  model = GTK_SELECTION_MODEL (gtk_no_selection_new (create_weather_model ()));
  listview = gtk_list_view_new (model, factory);
  gtk_orientable_set_orientation (GTK_ORIENTABLE (listview), GTK_ORIENTATION_HORIZONTAL);
  gtk_list_view_set_show_separators (GTK_LIST_VIEW (listview), TRUE);

  return listview;
}

GtkWidget *
do_listview_weather (GtkWidget *do_widget)
{
  if (window == NULL)
    {
      GtkWidget *listview, *sw;

      window = gtk_window_new ();
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
      gtk_window_set_title (GTK_WINDOW (window), "Weather");
      gtk_window_set_display (GTK_WINDOW (window),
                              gtk_widget_get_display (do_widget));
      gtk_window_set_title (GTK_WINDOW (window), "Weather");
      g_object_add_weak_pointer (G_OBJECT (window), (gpointer *) &window);

      sw = gtk_scrolled_window_new ();
      gtk_window_set_child (GTK_WINDOW (window), sw);
      listview = create_weather_view ();
      gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
    }

  if (!gtk_widget_get_visible (window))
    gtk_widget_set_visible (window, TRUE);
  else
    gtk_window_destroy (GTK_WINDOW (window));

  return window;
}