Skip to content

Commit

Permalink
act_on : refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlicVB committed Feb 27, 2025
1 parent 3a79644 commit db23ca5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 146 deletions.
248 changes: 106 additions & 142 deletions src/common/act_on.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <glib.h>
#include <sqlite3.h>

// get the algorithm set in preference
// this take care of the different startup values if not already defined
// direct access to the pref should not be used !
dt_act_on_algorithm_t dt_act_on_get_algorithm()
{
if(!dt_conf_key_defined("plugins/lighttable/act_on"))
Expand Down Expand Up @@ -98,6 +101,23 @@ static void _insert_in_list(GList **list,
}
}

// insert all the active images in the given list
static void _insert_active_images_in_list(GList **list,
gboolean only_visible)
{
for(GSList *ll = darktable.view_manager->active_images;
ll;
ll = g_slist_next(ll))
{
const int id = GPOINTER_TO_INT(ll->data);
_insert_in_list(list, id, only_visible);
// be absolutely sure we have the id in the list (in darkroom,
// the active image can be out of collection)
if(!only_visible)
_insert_in_list(list, id, TRUE);
}
}

// test if the cache is still valid
static gboolean _test_cache(dt_act_on_cache_t *cache)
{
Expand All @@ -108,7 +128,7 @@ static gboolean _test_cache(dt_act_on_cache_t *cache)
&& cache->inside_table == dt_ui_thumbtable(darktable.gui->ui)->mouse_inside
&& dt_slist_length_equal(cache->active_imgs, darktable.view_manager->active_images))
{
// we test active images if mouse outside table
// we test active images if mouse is outside table
gboolean ok = TRUE;
if(!dt_ui_thumbtable(darktable.gui->ui)->mouse_inside && cache->active_imgs)
{
Expand All @@ -130,11 +150,53 @@ static gboolean _test_cache(dt_act_on_cache_t *cache)
return FALSE;
}

// register a new cache with the given values
static void _cache_update_register(const gboolean only_visible,
const gboolean ordered,
const gboolean inside_sel,
const dt_imgid_t mouseover,
GList *images)
{
dt_act_on_cache_t *cache;
if(only_visible)
cache = &darktable.view_manager->act_on_cache_visible;
else
cache = &darktable.view_manager->act_on_cache_all;

// let's register the new list as cached
cache->image_over_inside_sel = inside_sel;
cache->ordered = ordered;
cache->image_over = mouseover;
GList *ltmp = cache->images;
cache->images = images;
g_list_free(ltmp);
cache->images_nb = g_list_length(cache->images);
GSList *sl = cache->active_imgs;
cache->active_imgs = g_slist_copy(darktable.view_manager->active_images);
g_slist_free(sl);
cache->inside_table = dt_ui_thumbtable(darktable.gui->ui)->mouse_inside;
cache->ok = TRUE;

// if needed, we show the list of cached images in terminal
if((darktable.unmuted & DT_DEBUG_ACT_ON) == DT_DEBUG_ACT_ON)
{
gchar *tx = g_strdup_printf("[images to act on - %s] new cache (%s) : ",
(dt_act_on_get_algorithm() == DT_ACT_ON_HOVER) ? "hover" : "selection",
only_visible ? "visible" : "all");

for(GList *ll = images;
ll;
ll = g_list_next(ll)) dt_util_str_cat(&tx, "%d ", GPOINTER_TO_INT(ll->data));
dt_print(DT_DEBUG_ACT_ON, "%s", tx);
g_free(tx);
}
}

// cache the list of images to act on during global changes (libs, accels)
// return TRUE if the cache is updated, FALSE if it's still up to date
static gboolean _hover_cache_update(const gboolean only_visible,
const gboolean force,
const gboolean ordered)
static gboolean _cache_update_hover(const gboolean only_visible,
const gboolean force,
const gboolean ordered)
{
/** Here's how it works
*
Expand Down Expand Up @@ -231,17 +293,7 @@ static gboolean _hover_cache_update(const gboolean only_visible,
if(darktable.view_manager->active_images)
{
// column 5
for(GSList *ll = darktable.view_manager->active_images;
ll;
ll = g_slist_next(ll))
{
const int id = GPOINTER_TO_INT(ll->data);
_insert_in_list(&l, id, only_visible);
// be absolutely sure we have the id in the list (in darkroom,
// the active image can be out of collection)
if(!only_visible)
_insert_in_list(&l, id, TRUE);
}
_insert_active_images_in_list(&l, only_visible);
}
else
{
Expand All @@ -252,37 +304,13 @@ static gboolean _hover_cache_update(const gboolean only_visible,
}

// let's register the new list as cached
cache->image_over_inside_sel = inside_sel;
cache->ordered = ordered;
cache->image_over = mouseover;
GList *ltmp = cache->images;
cache->images = l;
g_list_free(ltmp);
cache->images_nb = g_list_length(cache->images);
GSList *sl = cache->active_imgs;
cache->active_imgs = g_slist_copy(darktable.view_manager->active_images);
g_slist_free(sl);
cache->inside_table = dt_ui_thumbtable(darktable.gui->ui)->mouse_inside;
cache->ok = TRUE;

// if needed, we show the list of cached images in terminal
if((darktable.unmuted & DT_DEBUG_ACT_ON) == DT_DEBUG_ACT_ON)
{
gchar *tx = g_strdup_printf
("[images to act on - hover] new cache (%s) : ", only_visible ? "visible" : "all");

for(GList *ll = l;
ll;
ll = g_list_next(ll)) dt_util_str_cat(&tx, "%d ", GPOINTER_TO_INT(ll->data));
dt_print(DT_DEBUG_ACT_ON, "%s", tx);
g_free(tx);
}
_cache_update_register(only_visible, ordered, inside_sel, mouseover, l);

return TRUE;
}
static gboolean _selection_cache_update(const gboolean only_visible,
const gboolean force,
const gboolean ordered)
static gboolean _cache_update_selection(const gboolean only_visible,
const gboolean force,
const gboolean ordered)
{
/** Here's how it works
*
Expand Down Expand Up @@ -326,17 +354,7 @@ static gboolean _selection_cache_update(const gboolean only_visible,
else
{
// column 3
for(GSList *ll = darktable.view_manager->active_images;
ll;
ll = g_slist_next(ll))
{
const int id = GPOINTER_TO_INT(ll->data);
_insert_in_list(&l, id, only_visible);
// be absolutely sure we have the id in the list (in darkroom,
// the active image can be out of collection)
if(!only_visible)
_insert_in_list(&l, id, TRUE);
}
_insert_active_images_in_list(&l, only_visible);
}
}
else
Expand All @@ -346,31 +364,7 @@ static gboolean _selection_cache_update(const gboolean only_visible,
}

// let's register the new list as cached
// cache->image_over_inside_sel = ; // not used in this algorithm
cache->ordered = ordered;
// cache->image_over = ; // not used in this algorithm
GList *ltmp = cache->images;
cache->images = l;
g_list_free(ltmp);
cache->images_nb = g_list_length(cache->images);
GSList *sl = cache->active_imgs;
cache->active_imgs = g_slist_copy(darktable.view_manager->active_images);
g_slist_free(sl);
cache->inside_table = dt_ui_thumbtable(darktable.gui->ui)->mouse_inside;
cache->ok = TRUE;

// if needed, we show the list of cached images in terminal
if((darktable.unmuted & DT_DEBUG_ACT_ON) == DT_DEBUG_ACT_ON)
{
gchar *tx = g_strdup_printf
("[images to act on - selection] new cache (%s) : ", only_visible ? "visible" : "all");

for(GList *ll = l;
ll;
ll = g_list_next(ll)) dt_util_str_cat(&tx, "%d ", GPOINTER_TO_INT(ll->data));
dt_print(DT_DEBUG_ACT_ON, "%s", tx);
g_free(tx);
}
_cache_update_register(only_visible, ordered, FALSE, NO_IMGID, l);

return TRUE;
}
Expand All @@ -379,9 +373,9 @@ static gboolean _cache_update(const gboolean only_visible,
const gboolean ordered)
{
if(dt_act_on_get_algorithm() == DT_ACT_ON_HOVER)
return _hover_cache_update(only_visible, force, ordered);
return _cache_update_hover(only_visible, force, ordered);
else
return _selection_cache_update(only_visible, force, ordered);
return _cache_update_selection(only_visible, force, ordered);
}

// get the list of images to act on during global changes (libs, accels)
Expand All @@ -408,9 +402,28 @@ GList *dt_act_on_get_images(const gboolean only_visible,
return l;
}

// return the list of imageid separated by a comma
// it's used in query
static gchar *_get_query_from_list(GList *l)
{
gchar *images = NULL;
for(; l; l = g_list_next(l))
{
dt_util_str_cat(&images, "%d,", GPOINTER_TO_INT(l->data));
}
if(images)
{
// remove trailing comma
images[strlen(images) - 1] = '\0';
}
else
images = g_strdup(" ");
return images;
}

// get the query to retrieve images to act on. this is useful to
// speedup actions if they already use sqlite queries
static gchar *_hover_get_query(const gboolean only_visible)
static gchar *_get_query_hover(const gboolean only_visible)
{
/** Here's how it works
*
Expand Down Expand Up @@ -478,16 +491,7 @@ static gchar *_hover_get_query(const gboolean only_visible)
if(darktable.view_manager->active_images)
{
// column 5
for(GSList *ll = darktable.view_manager->active_images;
ll;
ll = g_slist_next(ll))
{
const int id = GPOINTER_TO_INT(ll->data);
_insert_in_list(&l, id, only_visible);
// be absolutely sure we have the id in the list (in darkroom,
// the active image can be out of collection)
if(!only_visible) _insert_in_list(&l, id, TRUE);
}
_insert_active_images_in_list(&l, only_visible);
}
else
{
Expand All @@ -498,21 +502,9 @@ static gchar *_hover_get_query(const gboolean only_visible)

// if we don't return the selection, we return the list of imgid separated by comma
// in the form it can be used inside queries
gchar *images = NULL;
for(; l; l = g_list_next(l))
{
dt_util_str_cat(&images, "%d,", GPOINTER_TO_INT(l->data));
}
if(images)
{
// remove trailing comma
images[strlen(images) - 1] = '\0';
}
else
images = g_strdup(" ");
return images;
return _get_query_from_list(l);
}
static gchar *_selection_get_query(const gboolean only_visible)
static gchar *_get_query_selection(const gboolean only_visible)
{
/** Here's how it works
*
Expand Down Expand Up @@ -542,16 +534,7 @@ static gchar *_selection_get_query(const gboolean only_visible)
else
{
// column 3
for(GSList *ll = darktable.view_manager->active_images;
ll;
ll = g_slist_next(ll))
{
const int id = GPOINTER_TO_INT(ll->data);
_insert_in_list(&l, id, only_visible);
// be absolutely sure we have the id in the list (in darkroom,
// the active image can be out of collection)
if(!only_visible) _insert_in_list(&l, id, TRUE);
}
_insert_active_images_in_list(&l, only_visible);
}

}
Expand All @@ -563,37 +546,18 @@ static gchar *_selection_get_query(const gboolean only_visible)

// if we don't return the selection, we return the list of imgid separated by comma
// in the form it can be used inside queries
gchar *images = NULL;
for(; l; l = g_list_next(l))
{
dt_util_str_cat(&images, "%d,", GPOINTER_TO_INT(l->data));
}
if(images)
{
// remove trailing comma
images[strlen(images) - 1] = '\0';
}
else
images = g_strdup(" ");
return images;
return _get_query_from_list(l);
}
gchar *dt_act_on_get_query(const gboolean only_visible)
{
if(dt_act_on_get_algorithm() == DT_ACT_ON_HOVER)
return _hover_get_query(only_visible);
else
return _selection_get_query(only_visible);
}
gchar *dt_act_on_get_query_force(const gboolean only_visible, dt_act_on_algorithm_t algorithm)
{
if(algorithm == DT_ACT_ON_HOVER)
return _hover_get_query(only_visible);
return _get_query_hover(only_visible);
else
return _selection_get_query(only_visible);
return _get_query_selection(only_visible);
}

// get the main image to act on during global changes (libs, accels)
static dt_imgid_t _hover_get_main_image()
static dt_imgid_t _get_main_image_hover()
{
/** Here's how it works -- same as for list, except we don't care
* about mouse inside selection or table
Expand Down Expand Up @@ -645,7 +609,7 @@ static dt_imgid_t _hover_get_main_image()

return ret;
}
static dt_imgid_t _selection_get_main_image()
static dt_imgid_t _get_main_image_selection()
{
/** Here's how it works -- same as for list, except we don't care
* about mouse inside selection or table
Expand Down Expand Up @@ -691,9 +655,9 @@ static dt_imgid_t _selection_get_main_image()
dt_imgid_t dt_act_on_get_main_image()
{
if(dt_act_on_get_algorithm() == DT_ACT_ON_HOVER)
return _hover_get_main_image();
return _get_main_image_hover();
else
return _selection_get_main_image();
return _get_main_image_selection();
}

// get only the number of images to act on
Expand Down
1 change: 0 additions & 1 deletion src/common/act_on.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ dt_act_on_algorithm_t dt_act_on_get_algorithm();
// The list needs to be freed by the caller
GList *dt_act_on_get_images(const gboolean only_visible, const gboolean force, const gboolean ordered);
gchar *dt_act_on_get_query(const gboolean only_visible);
gchar *dt_act_on_get_query_force(const gboolean only_visible, dt_act_on_algorithm_t algorithm);

// get the main image to act on during global changes (libs, accels)
dt_imgid_t dt_act_on_get_main_image();
Expand Down
2 changes: 1 addition & 1 deletion src/dtgtk/culling.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ void dt_culling_init(dt_culling_t *table,
* act_on main image or fallback_offset if no image
*
* For the automatic detection of navigation in selection :
* culling dynamic mode => OFF
* culling dynamic mode => OFF (whatever the restriction value)
* first image in selection AND selection > 1 => ON
* otherwise => OFF
*
Expand Down
Loading

0 comments on commit db23ca5

Please sign in to comment.