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