-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
snapshots.c
1257 lines (1047 loc) · 35.6 KB
/
snapshots.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2011-2024 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/darktable.h"
#include "bauhaus/bauhaus.h"
#include "common/debug.h"
#include "common/file_location.h"
#include "common/history_snapshot.h"
#include "control/conf.h"
#include "control/control.h"
#include "develop/develop.h"
#include "gui/accelerators.h"
#include "gui/gtk.h"
#include "gui/draw.h"
#include "libs/lib.h"
#include "libs/lib_api.h"
DT_MODULE(1)
#define HANDLE_SIZE 0.02
#define MAX_SNAPSHOT 10
// the snapshot offset in the memory table to use an area not used by the
// undo/redo support.
#define SNAPSHOT_ID_OFFSET 0xFFFFFF00
/* a snapshot */
typedef struct dt_lib_snapshot_t
{
GtkWidget *button;
GtkWidget *num;
GtkWidget *status;
GtkWidget *name;
GtkWidget *entry;
GtkWidget *restore_button;
GtkWidget *bbox;
char *module;
char *label;
dt_view_context_t ctx;
dt_imgid_t imgid;
uint32_t history_end;
uint32_t id;
uint8_t *buf;
float scale;
size_t width, height;
float zoom_x, zoom_y;
} dt_lib_snapshot_t;
typedef struct dt_lib_snapshots_t
{
GtkWidget *snapshots_box;
int selected;
gboolean snap_requested;
guint expose_again_timeout_id;
/* current active snapshots */
uint32_t num_snapshots;
/* snapshots */
dt_lib_snapshot_t snapshot[MAX_SNAPSHOT];
/* change snapshot overlay controls */
gboolean dragging, vertical, inverted, panning;
double vp_width, vp_height, vp_xpointer, vp_ypointer, vp_xrotate, vp_yrotate;
gboolean on_going;
GtkWidget *take_button;
} dt_lib_snapshots_t;
/* callback for take snapshot */
static void _lib_snapshots_add_button_clicked_callback(GtkWidget *widget,
gpointer user_data);
static void _lib_snapshots_toggled_callback(GtkToggleButton *widget,
gpointer user_data);
static void _lib_snapshots_restore_callback(GtkButton *widget,
gpointer user_data);
const char *name(dt_lib_module_t *self)
{
return _("snapshots");
}
dt_view_type_flags_t views(dt_lib_module_t *self)
{
return DT_VIEW_DARKROOM;
}
uint32_t container(dt_lib_module_t *self)
{
return DT_UI_CONTAINER_PANEL_LEFT_CENTER;
}
int position(const dt_lib_module_t *self)
{
return 1000;
}
enum _lib_snapshot_button_items
{
_SNAPSHOT_BUTTON_NUM,
_SNAPSHOT_BUTTON_STATUS,
_SNAPSHOT_BUTTON_NAME,
_SNAPSHOT_BUTTON_ENTRY,
} _lib_snapshot_button_items;
static GtkWidget *_lib_snapshot_button_get_item(GtkWidget *button, const int num)
{
GtkWidget *cont = gtk_bin_get_child(GTK_BIN(button));
GList *items = gtk_container_get_children(GTK_CONTAINER(cont));
return (GtkWidget *)g_list_nth_data(items, num);
}
// draw snapshot sign
static void _draw_sym(cairo_t *cr,
const float x,
const float y,
const gboolean vertical,
const gboolean inverted)
{
const double inv = inverted ? -0.1 : 1.0;
PangoRectangle ink;
PangoFontDescription *desc =
pango_font_description_copy_static(darktable.bauhaus->pango_font_desc);
pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD);
pango_font_description_set_absolute_size(desc, DT_PIXEL_APPLY_DPI(12) * PANGO_SCALE);
PangoLayout *layout = pango_cairo_create_layout(cr);
pango_layout_set_font_description(layout, desc);
pango_layout_set_text(layout, C_("snapshot sign", "S"), -1);
pango_layout_get_pixel_extents(layout, &ink, NULL);
if(vertical)
cairo_move_to(cr, x - (inv * ink.width * 1.2f),
y - (ink.height / 2.0f) - DT_PIXEL_APPLY_DPI(3));
else
cairo_move_to(cr, x - (ink.width / 2.0),
y + (-inv * (ink.height * 1.2f) - DT_PIXEL_APPLY_DPI(2)));
dt_draw_set_color_overlay(cr, FALSE, 0.9);
pango_cairo_show_layout(cr, layout);
pango_font_description_free(desc);
g_object_unref(layout);
}
static gboolean _snap_expose_again(gpointer user_data)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)user_data;
d->expose_again_timeout_id = 0;
d->snap_requested = TRUE;
dt_control_queue_redraw_center();
return FALSE;
}
/* expose snapshot over center viewport */
void gui_post_expose(dt_lib_module_t *self,
cairo_t *cri,
const int32_t width,
const int32_t height,
const int32_t pointerx,
const int32_t pointery)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
dt_develop_t *dev = darktable.develop;
if(d->selected >= 0)
{
dt_lib_snapshot_t *snap = &d->snapshot[d->selected];
const dt_view_context_t ctx = dt_view_get_context_hash();
// if a new snapshot is needed, do this now
if(d->snap_requested && snap->ctx == ctx)
{
dt_free_align(snap->buf);
snap->buf = NULL;
// export image with proper size
dt_dev_image(snap->imgid, width, height,
snap->history_end,
&snap->buf, &snap->scale,
&snap->width, &snap->height,
&snap->zoom_x, &snap->zoom_y,
snap->id, NULL, DT_DEVICE_NONE, FALSE);
d->snap_requested = FALSE;
d->expose_again_timeout_id = 0;
}
// if ctx has changed, get a new snapshot at the right zoom
// level. this is using a time out to ensure we don't try to
// create many snapshot while zooming (this is slow), so we wait
// to the zoom level to be stabilized to create the new snapshot.
if(snap->ctx != ctx
|| !snap->buf)
{
// request a new snapshot in the following conditions:
// 1. we are not panning
// 2. the mouse is not over the center area, probably panning
// with the navigation module
snap->ctx = ctx;
if(!d->panning && dev->darkroom_mouse_in_center_area)
d->snap_requested = TRUE;
if(d->expose_again_timeout_id != 0)
g_source_remove(d->expose_again_timeout_id);
d->expose_again_timeout_id = g_timeout_add(150, _snap_expose_again, d);
}
float pzx, pzy, zoom_scale;
dt_dev_get_pointer_zoom_pos(&dev->full, 0, 0, &pzx, &pzy, &zoom_scale);
pzx = fmin(pzx + 0.5f, 0.0f);
pzy = fmin(pzy + 0.5f, 0.0f);
d->vp_width = width;
d->vp_height = height;
const double lx = width * d->vp_xpointer;
const double ly = height * d->vp_ypointer;
const double size = DT_PIXEL_APPLY_DPI(d->inverted ? -15 : 15);
// clear background
dt_gui_gtk_set_source_rgb(cri, DT_GUI_COLOR_DARKROOM_BG);
if(d->vertical)
{
if(d->inverted)
cairo_rectangle(cri, lx, 0, width - lx, height);
else
cairo_rectangle(cri, 0, 0, lx, height);
}
else
{
if(d->inverted)
cairo_rectangle(cri, 0, ly, width, height - ly);
else
cairo_rectangle(cri, 0, 0, width, ly);
}
cairo_clip(cri);
cairo_fill(cri);
if(snap->buf)
{
dt_view_paint_surface(cri, width, height, &dev->full, DT_WINDOW_MAIN,
snap->buf, snap->scale, snap->width, snap->height,
snap->zoom_x, snap->zoom_y);
}
cairo_reset_clip(cri);
// draw the split line using the selected overlay color
dt_draw_set_color_overlay(cri, TRUE, 0.7);
cairo_set_line_width(cri, 1.);
if(d->vertical)
{
const float iheight = dev->preview_pipe->backbuf_height * zoom_scale;
const double offset = (double)(iheight * (-pzy));
const double center = (fabs(size) * 2.0) + offset;
// line
cairo_move_to(cri, lx, 0.0f);
cairo_line_to(cri, lx, height);
cairo_stroke(cri);
if(!d->dragging)
{
// triangle
cairo_move_to(cri, lx, center - size);
cairo_line_to(cri, lx - (size * 1.2), center);
cairo_line_to(cri, lx, center + size);
cairo_close_path(cri);
cairo_fill(cri);
// symbol
_draw_sym(cri, lx, center, TRUE, d->inverted);
}
}
else
{
const float iwidth = dev->preview_pipe->backbuf_width * zoom_scale;
const double offset = (double)(iwidth * (-pzx));
const double center = (fabs(size) * 2.0) + offset;
// line
cairo_move_to(cri, 0.0f, ly);
cairo_line_to(cri, width, ly);
cairo_stroke(cri);
if(!d->dragging)
{
// triangle
cairo_move_to(cri, center - size, ly);
cairo_line_to(cri, center, ly - (size * 1.2));
cairo_line_to(cri, center + size, ly);
cairo_close_path(cri);
cairo_fill(cri);
// symbol
_draw_sym(cri, center, ly, FALSE, d->inverted);
}
}
/* if mouse over control lets draw center rotate control, hide if split is dragged */
if(!d->dragging)
{
const double s = fmin(24, width * HANDLE_SIZE);
const gint rx = (d->vertical ? width * d->vp_xpointer : width * 0.5) - (s * 0.5);
const gint ry = (d->vertical ? height * 0.5 : height * d->vp_ypointer) - (s * 0.5);
const gboolean display_rotation =
(abs(pointerx - rx) < 40)
&& (abs(pointery - ry) < 40);
dt_draw_set_color_overlay(cri, TRUE, display_rotation ? 1.0 : 0.3);
cairo_set_line_width(cri, 0.5);
dtgtk_cairo_paint_refresh(cri, rx, ry, s, s, 0, NULL);
}
d->on_going = FALSE;
}
}
int button_released(struct dt_lib_module_t *self,
const double x,
const double y,
const int which,
const uint32_t state)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
if(d->panning)
{
d->panning = FALSE;
return 0;
}
if(d->selected >= 0)
{
d->dragging = FALSE;
return 1;
}
return 0;
}
static int _lib_snapshot_rotation_cnt = 0;
int button_pressed(struct dt_lib_module_t *self,
const double x,
const double y,
const double pressure,
const int which,
const int type,
const uint32_t state)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
if(darktable.develop->darkroom_skip_mouse_events)
{
d->panning = TRUE;
return 0;
}
if(d->selected >= 0 && which != GDK_BUTTON_MIDDLE)
{
if(d->on_going) return 1;
const double xp = x / d->vp_width;
const double yp = y / d->vp_height;
/* do the split rotating */
const double hhs = HANDLE_SIZE * 0.5;
if(((d->vertical
&& xp > d->vp_xpointer - hhs
&& xp < d->vp_xpointer + hhs)
&& yp > 0.5 - hhs && yp < 0.5 + hhs)
|| ((!d->vertical && yp > d->vp_ypointer - hhs
&& yp < d->vp_ypointer + hhs)
&& xp > 0.5 - hhs && xp < 0.5 + hhs)
|| (d->vp_xrotate > xp - hhs
&& d->vp_xrotate <= xp + hhs
&& d->vp_yrotate > yp - hhs
&& d->vp_yrotate <= yp + hhs))
{
/* let's rotate */
_lib_snapshot_rotation_cnt++;
d->vertical = !d->vertical;
if(_lib_snapshot_rotation_cnt % 2) d->inverted = !d->inverted;
d->vp_xpointer = xp;
d->vp_ypointer = yp;
d->vp_xrotate = xp;
d->vp_yrotate = yp;
d->on_going = TRUE;
dt_control_queue_redraw_center();
}
/* do the dragging !? */
else
{
d->dragging = TRUE;
d->vp_ypointer = yp;
d->vp_xpointer = xp;
d->vp_xrotate = 0.0;
d->vp_yrotate = 0.0;
dt_control_queue_redraw_center();
}
return 1;
}
return 0;
}
int mouse_moved(dt_lib_module_t *self,
const double x,
const double y,
const double pressure,
const int which)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
// if panning, do not handle here, let darkroom do the job
if(d->panning) return 0;
if(d->selected >= 0)
{
const double xp = x / d->vp_width;
const double yp = y / d->vp_height;
/* update x pointer */
if(d->dragging)
{
d->vp_xpointer = xp;
d->vp_ypointer = yp;
}
dt_control_queue_redraw_center();
return 1;
}
return 0;
}
static void _lib_snapshots_toggle_last(dt_action_t *action)
{
dt_lib_snapshots_t *d = dt_action_lib(action)->data;
const int32_t index = d->num_snapshots - 1;
if(d->num_snapshots)
gtk_toggle_button_set_active
(GTK_TOGGLE_BUTTON(d->snapshot[index].button),
!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(d->snapshot[index].button)));
}
static int _look_for_widget(dt_lib_module_t *self, GtkWidget *widget, gboolean entry)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
for(int k=0; k<MAX_SNAPSHOT; k++)
{
if((entry ? d->snapshot[k].entry : d->snapshot[k].button) == widget)
return k;
}
return 0;
}
static void _entry_activated_callback(GtkEntry *entry, gpointer *user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
const int index = _look_for_widget(self, (GtkWidget *)entry, TRUE);
const char *txt = gtk_entry_get_text(GTK_ENTRY(d->snapshot[index].entry));
char *label = dt_history_get_name_label(d->snapshot[index].module, txt, TRUE);
gtk_label_set_markup(GTK_LABEL(d->snapshot[index].name), label);
g_free(label);
gtk_widget_hide(d->snapshot[index].entry);
gtk_widget_show(d->snapshot[index].name);
gtk_widget_grab_focus(d->snapshot[index].button);
}
static gboolean _lib_button_button_pressed_callback(GtkWidget *widget,
GdkEventButton *event,
gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
const int index = _look_for_widget(self, widget, FALSE);
if(dt_modifier_is(event->state, GDK_CONTROL_MASK))
{
gtk_widget_hide(d->snapshot[index].name);
gtk_widget_show(d->snapshot[index].entry);
gtk_widget_grab_focus(d->snapshot[index].entry);
}
gtk_widget_set_focus_on_click(widget, FALSE);
return gtk_widget_has_focus(d->snapshot[index].entry);
}
static void _init_snapshot_entry(dt_lib_module_t *self, dt_lib_snapshot_t *s)
{
/* create snapshot button */
s->button = gtk_toggle_button_new();
gtk_widget_set_name(s->button, "snapshot-button");
g_signal_connect(G_OBJECT(s->button), "toggled",
G_CALLBACK(_lib_snapshots_toggled_callback), self);
g_signal_connect(G_OBJECT(s->button), "button-press-event",
G_CALLBACK(_lib_button_button_pressed_callback), self);
s->num = gtk_label_new("");
gtk_widget_set_name(s->num, "history-number");
dt_gui_add_class(s->num, "dt_monospace");
s->status = gtk_label_new("");
dt_gui_add_class(s->status, "dt_monospace");
s->name = gtk_label_new("");
gtk_label_set_ellipsize(GTK_LABEL(s->name), PANGO_ELLIPSIZE_MIDDLE);
gtk_widget_set_halign(s->name, GTK_ALIGN_START);
s->entry = gtk_entry_new();
gtk_widget_set_halign(s->entry, GTK_ALIGN_START);
g_signal_connect(G_OBJECT(s->entry), "activate",
G_CALLBACK(_entry_activated_callback), self);
s->restore_button = gtk_button_new_with_label("⤓");
gtk_widget_set_tooltip_text(s->restore_button,
_("restore snapshot into current history"));
g_signal_connect(G_OBJECT(s->restore_button), "clicked",
G_CALLBACK(_lib_snapshots_restore_callback), self);
}
static void _clear_snapshot_entry(dt_lib_snapshot_t *s)
{
// delete corresponding entry from the database
dt_history_snapshot_clear(s->imgid, s->id);
s->ctx = 0;
s->imgid = NO_IMGID;
s->history_end = -1;
if(s->button)
{
GtkWidget *lstatus = _lib_snapshot_button_get_item(s->button, _SNAPSHOT_BUTTON_STATUS);
gtk_widget_set_tooltip_text(s->button, "");
gtk_widget_set_tooltip_text(lstatus, "");
gtk_widget_hide(s->button);
gtk_widget_hide(s->restore_button);
}
g_free(s->module);
g_free(s->label);
dt_free_align(s->buf);
s->module = NULL;
s->label = NULL;
s->buf = NULL;
}
static void _clear_snapshots(dt_lib_module_t *self)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
d->selected = -1;
darktable.lib->proxy.snapshots.enabled = FALSE;
d->snap_requested = FALSE;
for(uint32_t k = 0; k < d->num_snapshots; k++)
{
dt_lib_snapshot_t *s = &d->snapshot[k];
s->id = SNAPSHOT_ID_OFFSET | k;
_clear_snapshot_entry(s);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(s->button), FALSE);
}
d->num_snapshots = 0;
gtk_widget_set_sensitive(d->take_button, TRUE);
dt_control_queue_redraw_center();
}
void gui_reset(dt_lib_module_t *self)
{
_clear_snapshots(self);
}
static void _signal_profile_changed(gpointer instance,
const uint8_t profile_type,
gpointer user_data)
{
// when the display profile is changed, make sure we recreate the snapshot
if(profile_type == DT_COLORSPACES_PROFILE_TYPE_DISPLAY)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
if(d->selected >= 0)
d->snap_requested = TRUE;
dt_control_queue_redraw_center();
}
}
static void _remove_snapshot_entry(dt_lib_module_t *self, const uint32_t index)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
// First clean the entry
_clear_snapshot_entry(&d->snapshot[index]);
// Repack all entries
for(uint32_t k = index; k < MAX_SNAPSHOT-1; k++)
{
memcpy(&d->snapshot[k], &d->snapshot[k+1], sizeof(dt_lib_snapshot_t));
}
// And finally clear last entry
_clear_snapshot_entry(&d->snapshot[MAX_SNAPSHOT-1]);
// And dedup widgets by initializing the last entry
_init_snapshot_entry(self, &d->snapshot[MAX_SNAPSHOT-1]);
// We have one less snapshot
d->num_snapshots--;
// If the remove image snapshot was selected, unselect it
if(d->selected == index)
d->selected = -1;
}
static void _signal_image_removed(gpointer instance,
const dt_imgid_t imgid,
gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
uint32_t k = 0;
while(k < MAX_SNAPSHOT)
{
dt_lib_snapshot_t *s = &d->snapshot[k];
if(s->imgid == imgid)
{
_remove_snapshot_entry(self, k);
dt_control_log(_("snapshots for removed image have been deleted"));
}
else
k++;
}
}
static void _signal_image_changed(gpointer instance, gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
const dt_imgid_t imgid = darktable.develop->image_storage.id;
for(uint32_t k = 0; k < MAX_SNAPSHOT; k++)
{
dt_lib_snapshot_t *s = &d->snapshot[k];
if(!dt_is_valid_imgid(s->imgid))
continue;
GtkWidget *b = d->snapshot[k].button;
GtkWidget *st = _lib_snapshot_button_get_item(b, _SNAPSHOT_BUTTON_STATUS);
char stat[8] = { 0 };
if(s->imgid == imgid)
{
g_strlcpy(stat, " ", sizeof(stat));
gtk_widget_set_tooltip_text(b, "");
gtk_widget_set_tooltip_text(st, "");
}
else
{
g_strlcpy(stat, "↗", sizeof(stat));
char tooltip[128] = { 0 };
// tooltip
char *name = dt_image_get_filename(s->imgid);
snprintf(tooltip, sizeof(tooltip),
_("↗ %s '%s'"), _("this snapshot was taken from"), name);
g_free(name);
gtk_widget_set_tooltip_text(b, tooltip);
gtk_widget_set_tooltip_text(st, tooltip);
}
gtk_label_set_text(GTK_LABEL(st), stat);
}
dt_control_queue_redraw_center();
}
void gui_init(dt_lib_module_t *self)
{
/* initialize ui widgets */
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)g_malloc0(sizeof(dt_lib_snapshots_t));
self->data = (void *)d;
/* initialize snapshot storages */
d->vp_xpointer = 0.5;
d->vp_ypointer = 0.5;
d->vp_xrotate = 0.0;
d->vp_yrotate = 0.0;
d->vertical = TRUE;
d->on_going = FALSE;
d->panning = FALSE;
d->selected = -1;
d->snap_requested = FALSE;
d->expose_again_timeout_id = 0;
d->num_snapshots = 0;
darktable.lib->proxy.snapshots.enabled = FALSE;
/* initialize ui containers */
self->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
d->snapshots_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
/* create take snapshot button */
d->take_button = dt_action_button_new
(self,
N_("take snapshot"),
_lib_snapshots_add_button_clicked_callback, self,
_("take snapshot to compare with another image "
"or the same image at another stage of development"), 0, 0);
/*
* initialize snapshots
*/
char localtmpdir[PATH_MAX] = { 0 };
dt_loc_get_tmp_dir(localtmpdir, sizeof(localtmpdir));
for(int k = 0; k < MAX_SNAPSHOT; k++)
{
dt_lib_snapshot_t *s = &d->snapshot[k];
s->id = SNAPSHOT_ID_OFFSET | k;
_clear_snapshot_entry(s);
_init_snapshot_entry(self, s);
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
// 4 items inside box, num, status, name, label
gtk_box_pack_start(GTK_BOX(box), s->num, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), s->status, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(box), s->name, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(box), s->entry, TRUE, TRUE, 0);
gtk_widget_show_all(box);
// hide entry, will be used only when editing
gtk_widget_hide(s->entry);
gtk_container_add(GTK_CONTAINER(s->button), box);
// add snap button and restore button
s->bbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_pack_start(GTK_BOX(s->bbox), s->button, TRUE, TRUE, 0);
gtk_box_pack_end(GTK_BOX(s->bbox), s->restore_button, FALSE, FALSE, 0);
/* add button to snapshot box */
gtk_box_pack_end(GTK_BOX(d->snapshots_box), s->bbox, FALSE, FALSE, 0);
/* prevent widget to show on external show all */
gtk_widget_set_no_show_all(s->button, TRUE);
gtk_widget_set_no_show_all(s->restore_button, TRUE);
}
/* add snapshot box and take snapshot button to widget ui*/
gtk_box_pack_start(GTK_BOX(self->widget),
dt_ui_resize_wrap(d->snapshots_box, 1,
"plugins/darkroom/snapshots/windowheight"),
TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(self->widget), d->take_button, TRUE, TRUE, 0);
dt_action_register(DT_ACTION(self), N_("toggle last snapshot"),
_lib_snapshots_toggle_last, 0, 0);
DT_DEBUG_CONTROL_SIGNAL_CONNECT(darktable.signals, DT_SIGNAL_CONTROL_PROFILE_USER_CHANGED,
G_CALLBACK(_signal_profile_changed), self);
DT_DEBUG_CONTROL_SIGNAL_CONNECT(darktable.signals, DT_SIGNAL_DEVELOP_IMAGE_CHANGED,
G_CALLBACK(_signal_image_changed), self);
DT_DEBUG_CONTROL_SIGNAL_CONNECT(darktable.signals, DT_SIGNAL_IMAGE_REMOVED,
G_CALLBACK(_signal_image_removed), self);
}
void gui_cleanup(dt_lib_module_t *self)
{
_clear_snapshots(self);
g_free(self->data);
self->data = NULL;
}
static void _lib_snapshots_add_button_clicked_callback(GtkWidget *widget,
gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
// first make sure the current history is properly written
dt_dev_write_history(darktable.develop);
dt_lib_snapshot_t *s = &d->snapshot[d->num_snapshots];
// set new snapshot_id, to not clash with the undo snapshot make the snapshot
// id at a specific offset.
s->id = SNAPSHOT_ID_OFFSET | d->num_snapshots;
_clear_snapshot_entry(s);
if(darktable.develop->history_end > 0)
{
dt_dev_history_item_t *history_item =
g_list_nth_data(darktable.develop->history,
darktable.develop->history_end - 1);
if(history_item && history_item->module)
{
s->module = g_strdup(history_item->module->name());
if(strlen(history_item->multi_name) > 0
&& history_item->multi_name[0] != ' ')
{
s->label = g_strdup(history_item->multi_name);
}
}
else
s->module = g_strdup(_("unknown"));
}
else
s->module = g_strdup(_("original"));
s->history_end = darktable.develop->history_end;
s->imgid = darktable.develop->image_storage.id;
dt_history_snapshot_create(s->imgid, s->id, s->history_end);
GtkLabel *lnum =
(GtkLabel *)_lib_snapshot_button_get_item(s->button, _SNAPSHOT_BUTTON_NUM);
GtkLabel *lstatus =
(GtkLabel *)_lib_snapshot_button_get_item(s->button, _SNAPSHOT_BUTTON_STATUS);
GtkLabel *lname =
(GtkLabel *)_lib_snapshot_button_get_item(s->button, _SNAPSHOT_BUTTON_NAME);
GtkEntry *lentry =
(GtkEntry *)_lib_snapshot_button_get_item(s->button, _SNAPSHOT_BUTTON_ENTRY);
char num[8];
g_snprintf(num, sizeof(num), "%2u", s->history_end);
gtk_label_set_text(lnum, num);
gtk_label_set_text(lstatus, " ");
char *txt = dt_history_get_name_label(s->module, s->label, TRUE);
gtk_label_set_markup(lname, txt);
gtk_entry_set_text(lentry, s->label ? s->label : "");
gtk_widget_grab_focus(s->button);
g_free(txt);
/* update slots used */
d->num_snapshots++;
/* show active snapshot slots */
for(uint32_t k = 0; k < d->num_snapshots; k++)
{
gtk_widget_show(d->snapshot[k].button);
gtk_widget_show(d->snapshot[k].restore_button);
}
if(d->num_snapshots == MAX_SNAPSHOT)
gtk_widget_set_sensitive(d->take_button, FALSE);
}
static int _lib_snapshots_get_activated(dt_lib_module_t *self, GtkWidget *widget)
{
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
for(uint32_t k = 0; k < d->num_snapshots; k++)
if(widget == d->snapshot[k].button
|| widget == d->snapshot[k].restore_button)
return k;
return -1;
}
static void _lib_snapshots_toggled_callback(GtkToggleButton *widget, gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
if(darktable.gui->reset) return;
++darktable.gui->reset;
d->selected = -1;
/* check if snapshot is activated */
if(gtk_toggle_button_get_active(widget))
{
d->selected = _lib_snapshots_get_activated(self, GTK_WIDGET(widget));
/* lets deactivate all togglebuttons except for self */
for(uint32_t k = 0; k < d->num_snapshots; k++)
if(d->selected != k)
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(d->snapshot[k].button), FALSE);
}
darktable.lib->proxy.snapshots.enabled = d->selected >= 0;
--darktable.gui->reset;
/* redraw center view */
dt_control_queue_redraw_center();
}
static void _lib_snapshots_restore_callback(GtkButton *widget, gpointer user_data)
{
dt_lib_module_t *self = (dt_lib_module_t *)user_data;
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
const int restore_idx = _lib_snapshots_get_activated(self, GTK_WIDGET(widget));
dt_lib_snapshot_t *s = &d->snapshot[restore_idx];
const dt_imgid_t imgid = s->imgid;
dt_history_snapshot_restore(imgid, s->id, s->history_end);
dt_dev_undo_start_record(darktable.develop);
// reload history and set back snapshot history end
dt_dev_reload_history_items(darktable.develop);
dt_dev_pixelpipe_rebuild(darktable.develop);
darktable.develop->history_end = s->history_end;
dt_dev_pop_history_items(darktable.develop, darktable.develop->history_end);
dt_ioppr_resync_modules_order(darktable.develop);
dt_dev_modulegroups_set(darktable.develop,
dt_dev_modulegroups_get(darktable.develop));
dt_image_update_final_size(imgid);
dt_dev_write_history(darktable.develop);
/* signal history changed */
dt_dev_undo_end_record(darktable.develop);
}
#ifdef USE_LUA
typedef enum
{
SNS_LEFT,
SNS_RIGHT,
SNS_TOP,
SNS_BOTTOM,
} snapshot_direction_t;
static int direction_member(lua_State *L)
{
dt_lib_module_t *self = *(dt_lib_module_t **)lua_touserdata(L, 1);
dt_lib_snapshots_t *d = (dt_lib_snapshots_t *)self->data;
if(lua_gettop(L) != 3)
{
snapshot_direction_t result;
if(!d->vertical && !d->inverted)
{
result = SNS_TOP;
}
else if(!d->vertical && d->inverted)
{
result = SNS_BOTTOM;