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