c61adcf20994aecabbacc3c70a7daab9bc20fd13
[geeqie.git] / src / pan-view / pan-folder.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-folder.h"
23
24 #include <cmath>
25
26 #include "pan-item.h"
27 #include "pan-util.h"
28 #include "pan-view-filter.h"
29
30 static void pan_flower_size(PanWindow *pw, gint *width, gint *height)
31 {
32         GList *work;
33         gint x1, y1, x2, y2;
34
35         x1 = 0;
36         y1 = 0;
37         x2 = 0;
38         y2 = 0;
39
40         work = pw->list;
41         while (work)
42                 {
43                 PanItem *pi;
44
45                 pi = static_cast<PanItem *>(work->data);
46                 work = work->next;
47
48                 if (x1 > pi->x) x1 = pi->x;
49                 if (y1 > pi->y) y1 = pi->y;
50                 if (x2 < pi->x + pi->width) x2 = pi->x + pi->width;
51                 if (y2 < pi->y + pi->height) y2 = pi->y + pi->height;
52                 }
53
54         x1 -= PAN_BOX_BORDER;
55         y1 -= PAN_BOX_BORDER;
56         x2 += PAN_BOX_BORDER;
57         y2 += PAN_BOX_BORDER;
58
59         work = pw->list;
60         while (work)
61                 {
62                 PanItem *pi;
63
64                 pi = static_cast<PanItem *>(work->data);
65                 work = work->next;
66
67                 pi->x -= x1;
68                 pi->y -= y1;
69
70                 if (pi->type == PAN_ITEM_TRIANGLE && pi->data)
71                         {
72                         gint *coord;
73
74                         coord = static_cast<gint *>(pi->data);
75                         coord[0] -= x1;
76                         coord[1] -= y1;
77                         coord[2] -= x1;
78                         coord[3] -= y1;
79                         coord[4] -= x1;
80                         coord[5] -= y1;
81                         }
82                 }
83
84         if (width) *width = x2 - x1;
85         if (height) *height = y2 - y1;
86 }
87
88 struct FlowerGroup {
89         GList *items;
90         GList *children;
91         gint x;
92         gint y;
93         gint width;
94         gint height;
95
96         gdouble angle;
97         gint circumference;
98         gint diameter;
99 };
100
101 static void pan_flower_move(FlowerGroup *group, gint x, gint y)
102 {
103         GList *work;
104
105         work = group->items;
106         while (work)
107                 {
108                 PanItem *pi;
109
110                 pi = static_cast<PanItem *>(work->data);
111                 work = work->next;
112
113                 pi->x += x;
114                 pi->y += y;
115                 }
116
117         group->x += x;
118         group->y += y;
119 }
120
121 #define PI 3.14159265
122
123 static void pan_flower_position(FlowerGroup *group, FlowerGroup *parent,
124                                                              gint *result_x, gint *result_y)
125 {
126         gint x, y;
127         gint radius;
128         gdouble a;
129
130         radius = parent->circumference / (2*PI);
131         radius = MAX(radius, parent->diameter / 2 + group->diameter / 2);
132
133         a = 2*PI * group->diameter / parent->circumference;
134
135         x = static_cast<gint>(static_cast<gdouble>(radius) * cos(parent->angle + a / 2));
136         y = static_cast<gint>(static_cast<gdouble>(radius) * sin(parent->angle + a / 2));
137
138         parent->angle += a;
139
140         x += parent->x;
141         y += parent->y;
142
143         x += parent->width / 2;
144         y += parent->height / 2;
145
146         x -= group->width / 2;
147         y -= group->height / 2;
148
149         *result_x = x;
150         *result_y = y;
151 }
152
153 static void pan_flower_build(PanWindow *pw, FlowerGroup *group, FlowerGroup *parent)
154 {
155         GList *work;
156         gint x, y;
157
158         if (!group) return;
159
160         if (parent && parent->children)
161                 {
162                 pan_flower_position(group, parent, &x, &y);
163                 }
164         else
165                 {
166                 x = 0;
167                 y = 0;
168                 }
169
170         pan_flower_move(group, x, y);
171
172         if (parent)
173                 {
174                 PanItem *pi;
175                 gint px, py, gx, gy;
176                 gint x1, y1, x2, y2;
177
178                 px = parent->x + parent->width / 2;
179                 py = parent->y + parent->height / 2;
180
181                 gx = group->x + group->width / 2;
182                 gy = group->y + group->height / 2;
183
184                 x1 = MIN(px, gx);
185                 y1 = MIN(py, gy);
186
187                 x2 = MAX(px, gx + 5);
188                 y2 = MAX(py, gy + 5);
189
190                 pi = pan_item_tri_new(pw, nullptr, x1, y1, x2 - x1, y2 - y1,
191                                       px, py, gx, gy, gx + 5, gy + 5,
192                                       255, 40, 40, 128);
193                 pan_item_tri_border(pi, PAN_BORDER_1 | PAN_BORDER_3,
194                                     255, 0, 0, 128);
195                 }
196
197         pw->list = g_list_concat(group->items, pw->list);
198         group->items = nullptr;
199
200         group->circumference = 0;
201         work = group->children;
202         while (work)
203                 {
204                 FlowerGroup *child;
205
206                 child = static_cast<FlowerGroup *>(work->data);
207                 work = work->next;
208
209                 group->circumference += child->diameter;
210                 }
211
212         work = g_list_last(group->children);
213         while (work)
214                 {
215                 FlowerGroup *child;
216
217                 child = static_cast<FlowerGroup *>(work->data);
218                 work = work->prev;
219
220                 pan_flower_build(pw, child, group);
221                 }
222
223         g_list_free(group->children);
224         g_free(group);
225 }
226
227 static FlowerGroup *pan_flower_group(PanWindow *pw, FileData *dir_fd, gint x, gint y)
228 {
229         FlowerGroup *group;
230         GList *f;
231         GList *d;
232         GList *work;
233         PanItem *pi_box;
234         gint x_start;
235         gint y_height;
236         gint grid_size;
237         gint grid_count;
238
239         if (!filelist_read(dir_fd, &f, &d)) return nullptr;
240         if (!f && !d) return nullptr;
241
242         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
243         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
244
245         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
246
247         pi_box = pan_item_text_new(pw, x, y, dir_fd->path, PAN_TEXT_ATTR_NONE,
248                                    PAN_BORDER_3,
249                                    PAN_TEXT_COLOR, 255);
250
251         y += pi_box->height;
252
253         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
254                                   x, y,
255                                   PAN_BOX_BORDER * 2, PAN_BOX_BORDER * 2,
256                                   PAN_BOX_OUTLINE_THICKNESS,
257                                   PAN_BOX_COLOR, PAN_BOX_ALPHA,
258                                   PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA);
259
260         x += PAN_BOX_BORDER;
261         y += PAN_BOX_BORDER;
262
263         grid_size = static_cast<gint>(sqrt(g_list_length(f)) + 0.9);
264         grid_count = 0;
265         x_start = x;
266         y_height = y;
267
268         work = f;
269         while (work)
270                 {
271                 FileData *fd;
272                 PanItem *pi;
273
274                 fd = static_cast<FileData *>(work->data);
275                 work = work->next;
276
277                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
278                         {
279                         pi = pan_item_image_new(pw, fd, x, y, 10, 10);
280                         x += pi->width + PAN_THUMB_GAP;
281                         if (pi->height > y_height) y_height = pi->height;
282                         }
283                 else
284                         {
285                         pi = pan_item_thumb_new(pw, fd, x, y);
286                         x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
287                         y_height = PAN_THUMB_SIZE;
288                         }
289
290                 grid_count++;
291                 if (grid_count >= grid_size)
292                         {
293                         grid_count = 0;
294                         x = x_start;
295                         y += y_height + PAN_THUMB_GAP;
296                         y_height = 0;
297                         }
298
299                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
300                 }
301
302         group = g_new0(FlowerGroup, 1);
303         group->items = pw->list;
304         pw->list = nullptr;
305
306         group->width = pi_box->width;
307         group->height = pi_box->y + pi_box->height;
308         group->diameter = static_cast<gint>(hypot(group->width, group->height));
309
310         group->children = nullptr;
311
312         work = d;
313         while (work)
314                 {
315                 FileData *fd;
316                 FlowerGroup *child;
317
318                 fd = static_cast<FileData *>(work->data);
319                 work = work->next;
320
321                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
322                         {
323                         child = pan_flower_group(pw, fd, 0, 0);
324                         if (child) group->children = g_list_prepend(group->children, child);
325                         }
326                 }
327
328         if (!f && !group->children)
329                 {
330                 g_list_free_full(group->items, reinterpret_cast<GDestroyNotify>(pan_item_free));
331                 g_free(group);
332                 group = nullptr;
333                 }
334
335         g_list_free(f);
336         filelist_free(d);
337
338         return group;
339 }
340
341 void pan_flower_compute(PanWindow *pw, FileData *dir_fd,
342                         gint *width, gint *height,
343                         gint *scroll_x, gint *scroll_y)
344 {
345         FlowerGroup *group;
346         GList *list;
347
348         group = pan_flower_group(pw, dir_fd, 0, 0);
349         pan_flower_build(pw, group, nullptr);
350
351         pan_flower_size(pw, width, height);
352
353         list = pan_item_find_by_fd(pw, PAN_ITEM_BOX, dir_fd, FALSE, FALSE);
354         if (list)
355                 {
356                 auto pi = static_cast<PanItem *>(list->data);
357                 *scroll_x = pi->x + pi->width / 2;
358                 *scroll_y = pi->y + pi->height / 2;
359                 }
360         g_list_free(list);
361 }
362
363 static void pan_folder_tree_path(PanWindow *pw, FileData *dir_fd,
364                                  gint *x, gint *y, gint *level,
365                                  PanItem *parent,
366                                  gint *width, gint *height)
367 {
368         GList *f;
369         GList *d;
370         GList *work;
371         PanItem *pi_box;
372         gint y_height = 0;
373
374         if (!filelist_read(dir_fd, &f, &d)) return;
375         if (!f && !d) return;
376
377         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
378         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
379
380         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
381
382         *x = PAN_BOX_BORDER + ((*level) * MAX(PAN_BOX_BORDER, PAN_THUMB_GAP));
383
384         pi_box = pan_item_text_new(pw, *x, *y, dir_fd->path, PAN_TEXT_ATTR_NONE,
385                                    PAN_BORDER_3,
386                                    PAN_TEXT_COLOR, 255);
387
388         *y += pi_box->height;
389
390         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
391                                   *x, *y,
392                                   PAN_BOX_BORDER, PAN_BOX_BORDER,
393                                   PAN_BOX_OUTLINE_THICKNESS,
394                                   PAN_BOX_COLOR, PAN_BOX_ALPHA,
395                                   PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA);
396
397         *x += PAN_BOX_BORDER;
398         *y += PAN_BOX_BORDER;
399
400         work = f;
401         while (work)
402                 {
403                 FileData *fd;
404                 PanItem *pi;
405
406                 fd = static_cast<FileData *>(work->data);
407                 work = work->next;
408
409                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
410                         {
411                         pi = pan_item_image_new(pw, fd, *x, *y, 10, 10);
412                         *x += pi->width + PAN_THUMB_GAP;
413                         if (pi->height > y_height) y_height = pi->height;
414                         }
415                 else
416                         {
417                         pi = pan_item_thumb_new(pw, fd, *x, *y);
418                         *x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
419                         y_height = PAN_THUMB_SIZE;
420                         }
421
422                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
423                 }
424
425         if (f) *y = pi_box->y + pi_box->height;
426
427         g_list_free(f);
428
429         work = d;
430         while (work)
431                 {
432                 FileData *fd;
433
434                 fd = static_cast<FileData *>(work->data);
435                 work = work->next;
436
437                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
438                         {
439                         *level = *level + 1;
440                         pan_folder_tree_path(pw, fd, x, y, level, pi_box, width, height);
441                         *level = *level - 1;
442                         }
443                 }
444
445         filelist_free(d);
446
447         pan_item_size_by_item(parent, pi_box, PAN_BOX_BORDER);
448
449         if (*y < pi_box->y + pi_box->height + PAN_BOX_BORDER)
450                 *y = pi_box->y + pi_box->height + PAN_BOX_BORDER;
451
452         pan_item_size_coordinates(pi_box, PAN_BOX_BORDER, width, height);
453 }
454
455 void pan_folder_tree_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
456 {
457         gint x, y;
458         gint level;
459         gint w, h;
460
461         level = 0;
462         x = PAN_BOX_BORDER;
463         y = PAN_BOX_BORDER;
464         w = PAN_BOX_BORDER * 2;
465         h = PAN_BOX_BORDER * 2;
466
467         pan_folder_tree_path(pw, dir_fd, &x, &y, &level, nullptr, &w, &h);
468
469         if (width) *width = w;
470         if (height) *height = h;
471 }
472 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */