91aa003e996e2324208c97aa32ad754e862f8f57
[geeqie.git] / src / pan-view / pan-calendar.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "pan-calendar.h"
23
24 #include <glib/gprintf.h>
25 #include <math.h>
26
27 #include "pan-util.h"
28 #include "pan-view.h"
29 #include "pixbuf_util.h"
30
31 #define PAN_CAL_POPUP_COLOR 220, 220, 220
32 #define PAN_CAL_POPUP_ALPHA 255
33 #define PAN_CAL_POPUP_BORDER 1
34 #define PAN_CAL_POPUP_BORDER_COLOR 0, 0, 0
35 #define PAN_CAL_POPUP_TEXT_COLOR 0, 0, 0
36
37 #define PAN_CAL_DAY_WIDTH 100
38 #define PAN_CAL_DAY_HEIGHT 80
39
40 #define PAN_CAL_DAY_COLOR 255, 255, 255
41 #define PAN_CAL_DAY_ALPHA 220
42 #define PAN_CAL_DAY_BORDER 2
43 #define PAN_CAL_DAY_BORDER_COLOR 0, 0, 0
44 #define PAN_CAL_DAY_TEXT_COLOR 0, 0, 0
45
46 #define PAN_CAL_MONTH_COLOR 255, 255, 255
47 #define PAN_CAL_MONTH_ALPHA 200
48 #define PAN_CAL_MONTH_BORDER 4
49 #define PAN_CAL_MONTH_BORDER_COLOR 0, 0, 0
50 #define PAN_CAL_MONTH_TEXT_COLOR 0, 0, 0
51
52 #define PAN_CAL_DOT_SIZE 3
53 #define PAN_CAL_DOT_GAP 2
54 #define PAN_CAL_DOT_COLOR 128, 128, 128
55 #define PAN_CAL_DOT_ALPHA 128
56
57
58 /*
59  *-----------------------------------------------------------------------------
60  * calendar
61  *-----------------------------------------------------------------------------
62  */
63
64 void pan_calendar_update(PanWindow *pw, PanItem *pi_day)
65 {
66         PanItem *pbox;
67         PanItem *pi;
68         GList *list;
69         GList *work;
70         gint x1, y1, x2, y2, x3, y3;
71         gint x, y, w, h;
72         gint grid;
73         gint column;
74
75         while ((pi = pan_item_find_by_key(pw, PAN_ITEM_NONE, "day_bubble"))) pan_item_remove(pw, pi);
76
77         if (!pi_day || pi_day->type != PAN_ITEM_BOX ||
78             !pi_day->key || strcmp(pi_day->key, "day") != 0) return;
79
80         list = pan_layout_intersect(pw, pi_day->x, pi_day->y, pi_day->width, pi_day->height);
81
82         work = list;
83         while (work)
84                 {
85                 PanItem *dot;
86                 GList *node;
87
88                 dot = work->data;
89                 node = work;
90                 work = work->next;
91
92                 if (dot->type != PAN_ITEM_BOX || !dot->fd ||
93                     !dot->key || strcmp(dot->key, "dot") != 0)
94                         {
95                         list = g_list_delete_link(list, node);
96                         }
97                 }
98
99         grid = (gint)(sqrt(g_list_length(list)) + 0.5);
100
101         x = pi_day->x + pi_day->width + 4;
102         y = pi_day->y;
103
104         pbox = pan_item_box_new(pw, NULL, x, y, PAN_BOX_BORDER, PAN_BOX_BORDER,
105                                 PAN_CAL_POPUP_BORDER,
106                                 PAN_CAL_POPUP_COLOR, PAN_CAL_POPUP_ALPHA,
107                                 PAN_CAL_POPUP_BORDER_COLOR, PAN_CAL_POPUP_ALPHA);
108         pan_item_set_key(pbox, "day_bubble");
109
110         if (pi_day->fd)
111                 {
112                 PanItem *plabel;
113                 gchar *buf;
114
115                 buf = pan_date_value_string(pi_day->fd->date, PAN_DATE_LENGTH_WEEK);
116                 plabel = pan_item_text_new(pw, x, y, buf, PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING,
117                                            PAN_TEXT_BORDER_SIZE,
118                                            PAN_CAL_POPUP_TEXT_COLOR, 255);
119                 pan_item_set_key(plabel, "day_bubble");
120                 g_free(buf);
121
122                 pan_item_size_by_item(pbox, plabel, 0);
123
124                 y += plabel->height;
125                 }
126
127         if (list)
128                 {
129                 column = 0;
130
131                 x += PAN_BOX_BORDER;
132                 y += PAN_BOX_BORDER;
133
134                 work = list;
135                 while (work)
136                         {
137                         PanItem *dot;
138
139                         dot = work->data;
140                         work = work->next;
141
142                         if (dot->fd)
143                                 {
144                                 PanItem *pimg;
145
146                                 pimg = pan_item_thumb_new(pw, file_data_ref(dot->fd), x, y);
147                                 pan_item_set_key(pimg, "day_bubble");
148
149                                 pan_item_size_by_item(pbox, pimg, PAN_BOX_BORDER);
150
151                                 column++;
152                                 if (column < grid)
153                                         {
154                                         x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
155                                         }
156                                 else
157                                         {
158                                         column = 0;
159                                         x = pbox->x + PAN_BOX_BORDER;
160                                         y += PAN_THUMB_SIZE + PAN_THUMB_GAP;
161                                         }
162                                 }
163                         }
164                 }
165
166         x1 = pi_day->x + pi_day->width - 8;
167         y1 = pi_day->y + 8;
168         x2 = pbox->x + 1;
169         y2 = pbox->y + MIN(42, pbox->height);
170         x3 = pbox->x + 1;
171         y3 = MAX(pbox->y, y2 - 30);
172         util_clip_triangle(x1, y1, x2, y2, x3, y3,
173                            &x, &y, &w, &h);
174
175         pi = pan_item_tri_new(pw, NULL, x, y, w, h,
176                               x1, y1, x2, y2, x3, y3,
177                               PAN_CAL_POPUP_COLOR, PAN_CAL_POPUP_ALPHA);
178         pan_item_tri_border(pi, PAN_BORDER_1 | PAN_BORDER_3, PAN_CAL_POPUP_BORDER_COLOR, PAN_CAL_POPUP_ALPHA);
179         pan_item_set_key(pi, "day_bubble");
180         pan_item_added(pw, pi);
181
182         pan_item_box_shadow(pbox, PAN_SHADOW_OFFSET * 2, PAN_SHADOW_FADE * 2);
183         pan_item_added(pw, pbox);
184
185         pan_layout_resize(pw);
186 }
187
188 void pan_calendar_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
189 {
190         GList *list;
191         GList *work;
192         gint x, y;
193         time_t tc;
194         gint count;
195         gint day_max;
196         gint grid;
197         gint year = 0;
198         gint month = 0;
199         gint end_year = 0;
200         gint end_month = 0;
201
202         list = pan_list_tree(dir_fd, SORT_NONE, TRUE, pw->ignore_symlinks);
203
204         if (pw->cache_list && pw->exif_date_enable)
205                 {
206                 pw->cache_list = pan_cache_sort(pw->cache_list, SORT_NAME, TRUE);
207                 list = filelist_sort(list, SORT_NAME, TRUE);
208                 pan_cache_sync_date(pw, list);
209                 }
210
211         pw->cache_list = pan_cache_sort(pw->cache_list, SORT_TIME, TRUE);
212         list = filelist_sort(list, SORT_TIME, TRUE);
213
214         day_max = 0;
215         count = 0;
216         tc = 0;
217         work = list;
218         while (work)
219                 {
220                 FileData *fd;
221
222                 fd = work->data;
223                 work = work->next;
224
225                 if (!pan_date_compare(fd->date, tc, PAN_DATE_LENGTH_DAY))
226                         {
227                         count = 0;
228                         tc = fd->date;
229                         }
230                 else
231                         {
232                         count++;
233                         if (day_max < count) day_max = count;
234                         }
235                 }
236
237         DEBUG_1("biggest day contains %d images", day_max);
238
239         grid = (gint)(sqrt((gdouble)day_max) + 0.5) * (PAN_THUMB_SIZE + PAN_SHADOW_OFFSET * 2 + PAN_THUMB_GAP);
240
241         if (list)
242                 {
243                 FileData *fd = list->data;
244
245                 year = pan_date_value(fd->date, PAN_DATE_LENGTH_YEAR);
246                 month = pan_date_value(fd->date, PAN_DATE_LENGTH_MONTH);
247                 }
248
249         work = g_list_last(list);
250         if (work)
251                 {
252                 FileData *fd = work->data;
253                 end_year = pan_date_value(fd->date, PAN_DATE_LENGTH_YEAR);
254                 end_month = pan_date_value(fd->date, PAN_DATE_LENGTH_MONTH);
255                 }
256
257         *width = PAN_BOX_BORDER * 2;
258         *height = PAN_BOX_BORDER * 2;
259
260         x = PAN_BOX_BORDER;
261         y = PAN_BOX_BORDER;
262
263         work = list;
264         while (work && (year < end_year || (year == end_year && month <= end_month)))
265                 {
266                 PanItem *pi_month;
267                 PanItem *pi_text;
268                 gint day;
269                 gint days;
270                 gint col;
271                 gint row;
272                 time_t dt;
273                 gchar *buf;
274
275                 /* figure last second of this month */
276                 dt = pan_date_to_time((month == 12) ? year + 1 : year, (month == 12) ? 1 : month + 1, 1);
277                 dt -= 60 * 60 * 24;
278
279                 /* anything to show this month? */
280                 if (!pan_date_compare(((FileData *)(work->data))->date, dt, PAN_DATE_LENGTH_MONTH))
281                         {
282                         month ++;
283                         if (month > 12)
284                                 {
285                                 year++;
286                                 month = 1;
287                                 }
288                         continue;
289                         }
290
291                 days = pan_date_value(dt, PAN_DATE_LENGTH_DAY);
292                 dt = pan_date_to_time(year, month, 1);
293                 col = pan_date_value(dt, PAN_DATE_LENGTH_WEEK);
294                 row = 1;
295
296                 x = PAN_BOX_BORDER;
297
298                 pi_month = pan_item_box_new(pw, NULL, x, y, PAN_CAL_DAY_WIDTH * 7, PAN_CAL_DAY_HEIGHT / 4,
299                                             PAN_CAL_MONTH_BORDER,
300                                             PAN_CAL_MONTH_COLOR, PAN_CAL_MONTH_ALPHA,
301                                             PAN_CAL_MONTH_BORDER_COLOR, PAN_CAL_MONTH_ALPHA);
302                 buf = pan_date_value_string(dt, PAN_DATE_LENGTH_MONTH);
303                 pi_text = pan_item_text_new(pw, x, y, buf,
304                                             PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING,
305                                             PAN_TEXT_BORDER_SIZE,
306                                             PAN_CAL_MONTH_TEXT_COLOR, 255);
307                 g_free(buf);
308                 pi_text->x = pi_month->x + (pi_month->width - pi_text->width) / 2;
309
310                 pi_month->height = pi_text->y + pi_text->height - pi_month->y;
311
312                 x = PAN_BOX_BORDER + col * PAN_CAL_DAY_WIDTH;
313                 y = pi_month->y + pi_month->height + PAN_BOX_BORDER;
314
315                 for (day = 1; day <= days; day++)
316                         {
317                         FileData *fd;
318                         PanItem *pi_day;
319                         gint dx, dy;
320                         gint n = 0;
321                         gchar fake_path[20];
322
323                         dt = pan_date_to_time(year, month, day);
324
325                         /*
326                          * Create a FileData entry that represents the given day.
327                          * It does not correspond to any real file
328                          */
329
330                         g_snprintf(fake_path, sizeof(fake_path), "//%04d-%02d-%02d", year, month, day);
331                         fd = file_data_new_no_grouping(fake_path);
332                         fd->date = dt;
333                         pi_day = pan_item_box_new(pw, fd, x, y, PAN_CAL_DAY_WIDTH, PAN_CAL_DAY_HEIGHT,
334                                                   PAN_CAL_DAY_BORDER,
335                                                   PAN_CAL_DAY_COLOR, PAN_CAL_DAY_ALPHA,
336                                                   PAN_CAL_DAY_BORDER_COLOR, PAN_CAL_DAY_ALPHA);
337                         pan_item_set_key(pi_day, "day");
338
339                         dx = x + PAN_CAL_DOT_GAP * 2;
340                         dy = y + PAN_CAL_DOT_GAP * 2;
341
342                         fd = (work) ? work->data : NULL;
343                         while (fd && pan_date_compare(fd->date, dt, PAN_DATE_LENGTH_DAY))
344                                 {
345                                 PanItem *pi;
346
347                                 pi = pan_item_box_new(pw, fd, dx, dy, PAN_CAL_DOT_SIZE, PAN_CAL_DOT_SIZE,
348                                                       0,
349                                                       PAN_CAL_DOT_COLOR, PAN_CAL_DOT_ALPHA,
350                                                       0, 0, 0, 0);
351                                 pan_item_set_key(pi, "dot");
352
353                                 dx += PAN_CAL_DOT_SIZE + PAN_CAL_DOT_GAP;
354                                 if (dx + PAN_CAL_DOT_SIZE > pi_day->x + pi_day->width - PAN_CAL_DOT_GAP * 2)
355                                         {
356                                         dx = x + PAN_CAL_DOT_GAP * 2;
357                                         dy += PAN_CAL_DOT_SIZE + PAN_CAL_DOT_GAP;
358                                         }
359                                 if (dy + PAN_CAL_DOT_SIZE > pi_day->y + pi_day->height - PAN_CAL_DOT_GAP * 2)
360                                         {
361                                         /* must keep all dots within respective day even if it gets ugly */
362                                         dy = y + PAN_CAL_DOT_GAP * 2;
363                                         }
364
365                                 n++;
366
367                                 work = work->next;
368                                 fd = (work) ? work->data : NULL;
369                                 }
370
371                         if (n > 0)
372                                 {
373                                 PanItem *pi;
374
375                                 pi_day->color_r = MAX(pi_day->color_r - 61 - n * 3, 80);
376                                 pi_day->color_g = pi_day->color_r;
377
378                                 buf = g_strdup_printf("( %d )", n);
379                                 pi = pan_item_text_new(pw, x, y, buf, PAN_TEXT_ATTR_NONE,
380                                                        PAN_TEXT_BORDER_SIZE,
381                                                        PAN_CAL_DAY_TEXT_COLOR, 255);
382                                 g_free(buf);
383
384                                 pi->x = pi_day->x + (pi_day->width - pi->width) / 2;
385                                 pi->y = pi_day->y + (pi_day->height - pi->height) / 2;
386                                 }
387
388                         buf = g_strdup_printf("%d", day);
389                         pan_item_text_new(pw, x + 4, y + 4, buf, PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING,
390                                           PAN_TEXT_BORDER_SIZE,
391                                           PAN_CAL_DAY_TEXT_COLOR, 255);
392                         g_free(buf);
393
394
395                         pan_item_size_coordinates(pi_day, PAN_BOX_BORDER, width, height);
396
397                         col++;
398                         if (col > 6)
399                                 {
400                                 col = 0;
401                                 row++;
402                                 x = PAN_BOX_BORDER;
403                                 y += PAN_CAL_DAY_HEIGHT;
404                                 }
405                         else
406                                 {
407                                 x += PAN_CAL_DAY_WIDTH;
408                                 }
409                         }
410
411                 if (col > 0) y += PAN_CAL_DAY_HEIGHT;
412                 y += PAN_BOX_BORDER * 2;
413
414                 month ++;
415                 if (month > 12)
416                         {
417                         year++;
418                         month = 1;
419                         }
420                 }
421
422         *width += grid;
423         *height = MAX(*height, grid + PAN_BOX_BORDER * 2 * 2);
424
425         g_list_free(list);
426 }
427 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */