Thumb - Generating thumbnails with C++ Binding

This example shows how to create a simple thumbnail object with Elementary C++ Binding.

The first part consists of including the headers. In this case we need Elementary C++ binding, iostream and sstream libraries.

#include <Elementary.hh>
#include <iostream>
#include <sstream>

Attention
All necessary Enlightenment, Elementary, C and/or C++ headers should be include here as well.

Starting the main code and telling elementary that we need Ethumb to generate the thumbnails:

EAPI_MAIN int
elm_main(int argc, char *argv[])
{

Then, we use app_info_set to access the image that we are using for this example.

elm_app_info_set(reinterpret_cast<void*>(elm_main), "elementary", "images/plant_01.jpg");

Now let's set the elm_policy, which defines for a given policy group/identifier a new policy's value, respectively. In this example the only policy we need to set a value for is ELM_POLICY_QUIT, possibles values for it are:

elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN);

As you can see, the policy we chose was to quit when the last win is hidden as opose to examples with the C bindings where we perpetually set it to quit when last win was closed. This changed was necessary because in C++ binding as the elm mainloop stop running all object are destroyed, references are unreferenced and events are stopped at ELM_MAIN().

See also
For more details consult elm_policy_set

Next step is creating an elementary window, in this example we use the C++ binding method with the elm_win_util_standard_add that is a elm_win_legacy function, better explained below. And then we set the autohide state for it.

elm_win_util_standard_add (const char *name, const char *tittle) Adds a window object with standard setup. Parameters:

This creates a window but also puts in a standard background with elm_bg_add(), as well as setting the window title to title. The window type created is of type ELM_WIN_BASIC, with the NULL as the parent widget. Returns the created object or NULL on failure.

The autohide works similarly to autodel, automatically handling "delete,request" signals when set to true, with the difference that it will hide the window, instead of destroying it.

It is specially designed to work together with ELM_POLICY_QUIT_LAST_WINDOW_HIDDEN which allows exiting Elementary's main loop when all the windows are hidden.

win.title_set("Thumbnailer");
win.autohide_set(true);

Note
autodel and autohide are not mutually exclusive. The window will be destructed if both autodel and autohide is set to EINA_TRUE or true.

Creating our thumb and setting it's parent, using C++ method.

::elm::thumb thumb(efl::eo::parent = win);

For our callbacks we are using lambda type functions to create then, note that all three only show a message, for when our thumb generation is starting, stoping and it's return error.

auto generation_started = std::bind([] { std::cout << "thumbnail generation started." << std::endl; });
auto generation_finished = std::bind([] { std::cout << "thumbnail generation finished." << std::endl; });
auto generation_error = std::bind([] { std::cout << "thumbnail generation error." << std::endl; });
thumb.callback_generate_start_add( generation_started );
thumb.callback_generate_stop_add( generation_finished );
thumb.callback_generate_error_add( generation_error );

Note
To learn more about Lambda Function and its use in Elementary consult Lambda Functions with Elementary - C++11.

Continuing with our thumb, we'll set a size, set it to not be editable, set the file and after that, we can start creating thumbnail objects. They are very similar to image or icon objects:

thumb.size_set(160, 160);
thumb.editable_set(false);
std::stringstream ss;
ss << elm_app_data_dir_get() << "/images/plant_01.jpg";
thumb.file_set(ss.str(), "image");
thumb.reload();

As you can see, the main different function here is reload(), which will check if the options of the Ethumb client have changed. If so, it will re-generate the thumbnail, and show the new one.

Notice in this example that the thumbnail object is displayed on the size of the window (320x320 pixels), but the thumbnail generated and stored has size 160x160 pixels. That's why the picture seems upscaled.

Ideally, you will be generating thumbnails with the size that you will be using them.

Finishing with thumb we set the weight hint. To better understand, the function size_hint_weight_set for C++ bindings originated from C bindings function evas_object_size_hint_weight_set, that is EFL Evas type function. With this function we set the hints for an object's weight. The parameters are:

This is not a size enforcement in any way, it's just a hint that should be used whenever appropriate. This is a hint on how a container object should resize a given child within its area.

Containers may adhere to the simpler logic of just expanding the child object's dimensions to fit its own (see the EVAS_HINT_EXPAND helper weight macro in the EFL Evas Documentation) or the complete one of taking each child's weight hint as real weights to how much of its size to allocate for them in each axis. A container is supposed to, after normalizing the weights of its children (with weight hints), distribute the space it has to layout them by those factors – most weighted children get larger in this process than the least ones.

thumb.size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);

Note
Default weight hint values are 0.0, for both axis.

Then we add the thumb as a resize-object to win informing that when the size of the win changes so should the thumb's size. Remember always to set the thumb visibility to true.

Now we only have to set the size for our window and make it visible.

And finally, start the elm mainloop, starting to handle events and drawing operations.

The full source code can be found at thumb_cxx_example_01.cc