Move some types and constants from pan-types to specific modules
[geeqie.git] / src / pan-view / pan-timeline.cc
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-timeline.h"
23
24 #include <ctime>
25
26 #include "filedata.h"
27 #include "pan-item.h"
28 #include "pan-types.h"
29 #include "pan-util.h"
30 #include "pan-view-filter.h"
31 #include "pan-view.h"
32 #include "typedefs.h"
33
34 namespace
35 {
36
37 constexpr gint PAN_GROUP_MAX = 16;
38
39 } // namespace
40
41 void pan_timeline_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
42 {
43         GList *list;
44         GList *work;
45         gint x;
46         gint y;
47         time_t group_start_date;
48         gint total;
49         gint count;
50         PanItem *pi_month = nullptr;
51         PanItem *pi_day = nullptr;
52         gint month_start;
53         gint day_start;
54         gint x_width;
55         gint y_height;
56
57         list = pan_list_tree(dir_fd, SORT_NONE, TRUE, TRUE, pw->ignore_symlinks);
58         pan_filter_fd_list(&list, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
59
60         if (pw->cache_list && pw->exif_date_enable)
61                 {
62                 pw->cache_list = pan_cache_sort(pw->cache_list, SORT_NAME, TRUE, TRUE);
63                 list = filelist_sort(list, SORT_NAME, TRUE, TRUE);
64                 pan_cache_sync_date(pw, list);
65                 }
66
67         pw->cache_list = pan_cache_sort(pw->cache_list, SORT_TIME, TRUE, TRUE);
68         list = filelist_sort(list, SORT_TIME, TRUE, TRUE);
69
70         *width = PAN_BOX_BORDER * 2;
71         *height = PAN_BOX_BORDER * 2;
72
73         x = 0;
74         y = 0;
75         month_start = y;
76         day_start = month_start;
77         x_width = 0;
78         y_height = 0;
79         group_start_date = 0;
80         // total and count are used to enforce a stride of PAN_GROUP_MAX thumbs.
81         total = 0;
82         count = 0;
83         work = list;
84         while (work)
85                 {
86                 FileData *fd;
87                 PanItem *pi;
88
89                 fd = static_cast<FileData *>(work->data);
90                 work = work->next;
91
92                 if (!pan_date_compare(fd->date, group_start_date, PAN_DATE_LENGTH_DAY))
93                         {
94                         // FD starts a new day group.
95                         GList *needle;
96                         gchar *buf;
97
98                         if (!pan_date_compare(fd->date, group_start_date, PAN_DATE_LENGTH_MONTH))
99                                 {
100                                 // FD starts a new month group.
101                                 pi_day = nullptr;
102
103                                 if (pi_month)
104                                         {
105                                         x = pi_month->x + pi_month->width + PAN_BOX_BORDER;
106                                         }
107                                 else
108                                         {
109                                         x = PAN_BOX_BORDER;
110                                         }
111
112                                 y = PAN_BOX_BORDER;
113
114                                 buf = pan_date_value_string(fd->date, PAN_DATE_LENGTH_MONTH);
115                                 pi = pan_item_text_new(pw, x, y, buf,
116                                                        static_cast<PanTextAttrType>(PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING),
117                                                        PAN_BORDER_3,
118                                                        {PAN_TEXT_COLOR, 255});
119                                 g_free(buf);
120                                 y += pi->height;
121
122                                 pi_month = pan_item_box_new(pw, file_data_ref(fd),
123                                                             x, y, 0, 0,
124                                                             PAN_BOX_OUTLINE_THICKNESS,
125                                                             {PAN_BOX_COLOR, PAN_BOX_ALPHA},
126                                                             {PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA});
127
128                                 x += PAN_BOX_BORDER;
129                                 y += PAN_BOX_BORDER;
130                                 month_start = y;
131                                 }
132
133                         if (pi_day) x = pi_day->x + pi_day->width + PAN_BOX_BORDER;
134
135                         group_start_date = fd->date;
136                         total = 1;
137                         count = 0;
138
139                         needle = work;
140                         while (needle)
141                                 {
142                                 FileData *nfd;
143
144                                 nfd = static_cast<FileData *>(needle->data);
145                                 if (pan_date_compare(nfd->date, group_start_date, PAN_DATE_LENGTH_DAY))
146                                         {
147                                         needle = needle->next;
148                                         total++;
149                                         }
150                                 else
151                                         {
152                                         needle = nullptr;
153                                         }
154                                 }
155
156                         buf = pan_date_value_string(fd->date, PAN_DATE_LENGTH_WEEK);
157                         pi = pan_item_text_new(pw, x, y, buf, PAN_TEXT_ATTR_NONE,
158                                                PAN_BORDER_3,
159                                                {PAN_TEXT_COLOR, 255});
160                         g_free(buf);
161
162                         y += pi->height;
163
164                         pi_day = pan_item_box_new(pw, file_data_ref(fd), x, y, 0, 0,
165                                                   PAN_BOX_OUTLINE_THICKNESS,
166                                                   {PAN_BOX_COLOR, PAN_BOX_ALPHA},
167                                                   {PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA});
168
169                         x += PAN_BOX_BORDER;
170                         y += PAN_BOX_BORDER;
171                         day_start = y;
172                         }
173
174                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
175                         {
176                         pi = pan_item_image_new(pw, fd, x, y, 10, 10);
177                         if (pi->width > x_width) x_width = pi->width;
178                         y_height = pi->height;
179                         }
180                 else
181                         {
182                         pi = pan_item_thumb_new(pw, fd, x, y);
183                         x_width = PAN_THUMB_SIZE;
184                         y_height = PAN_THUMB_SIZE;
185                         }
186
187                 pan_item_size_by_item(pi_day, pi, PAN_BOX_BORDER);
188                 pan_item_size_by_item(pi_month, pi_day, PAN_BOX_BORDER);
189
190                 total--;
191                 count++;
192
193                 if (total > 0 && count < PAN_GROUP_MAX)
194                         {
195                         y += y_height + PAN_THUMB_GAP;
196                         }
197                 else
198                         {
199                         x += x_width + PAN_THUMB_GAP;
200                         x_width = 0;
201                         count = 0;
202
203                         if (total > 0)
204                                 y = day_start;
205                         else
206                                 y = month_start;
207                         }
208
209                 pan_item_size_coordinates(pi_month, PAN_BOX_BORDER, width, height);
210                 }
211
212         g_list_free(list);
213 }
214 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */