GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Apr 17, 2014 7:14 am

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to show GtkImages in a correct hierarchy?
PostPosted: Fri Jan 06, 2012 4:53 pm 
Familiar Face

Joined: Mon Oct 17, 2011 10:18 am
Posts: 26
Hello All,

I will be more than grateful if you could find time for answering me.

I am writing a GUI, in which I receive real time video from several cameras of a robot (using ROS).

Container (Gtk Widget) ---points to---> GtkTable (4x4) ---each of cells points to---> EventBox (GtkWidget)
---points to--->GtkWidget ---points to---> GtkPixbuf ---points to---> IplImage (for each frame of the video).

Until now, I have only showed real time video stream, and everything worked properly.
Here is the code of the main loop.
int main(int argc, char **argv)
   ros::init(argc, argv, "image_listener");
    ros::NodeHandle nh;
   ros::Rate loop_rate(100);                                                                   // Rate class instance.   
   ImageManagmentStruct LeftCameraImageStruct, RightCameraImageStruct, KinectImageStruct;      // Structs for image menagment.      
    // Getting the resizing parameter from the launch file.
    if ( !nh.getParam("resizing_factor", globalResizingFactor) )
        ROS_ERROR ("The resizing factor was not loaded succesfully. Aborting...\n");

    // Initializing the image/camera data structures.   
   dataStructuresInitialization ( &LeftCameraImageStruct, &RightCameraImageStruct, &KinectImageStruct );
    // GTK library initialization
   gtk_init ( &argc, &argv );

    // Creating a top level window and connecting it to a signal.   
    createMainInterfaceWindow ();
    // Creating a fixed container, which will hold all the widgets of the GUI.
    createFixedContainer ();
    // Creating a GtkGrid widget, which will contain all the images.
    createGtkTable ();
    // Attaching the created table to the Container.
    gtk_fixed_put (GTK_FIXED (pGlobalFixedContainer), pGlobalGtkTable, IMAGES_HORIZONTAL_PLACEMENT, IMAGES_VERTIACL_PLACEMENT);
   // Subscribing in order to receive images.
   ros::Subscriber subLeft = nh.subscribe<sensor_msgs::Image> ("leftCameraTopic", 10, boost::bind(imageCallback, _1, globalArrayOfImages[0]));    
    ros::Subscriber subRight = nh.subscribe<sensor_msgs::Image> ("rightCameraTopic", 10, boost::bind(imageCallback, _1, globalArrayOfImages[1]));
    ros::Subscriber subKinect = nh.subscribe<sensor_msgs::Image> ("kinectCameraTopic", 10, boost::bind(imageCallback, _1, globalArrayOfImages[2]));
    while ( ros::ok() )
      // Showing the main window, with the attached image.
      if ( GTK_IS_WIDGET (globalTopLevelWindow) )
         gtk_widget_show_all ( globalTopLevelWindow );       
           ROS_INFO ("The top level window of the application was closed...Exiting the program.\n");
           return 0;
      // Allowing "imageCallback" to be called.
       // Running the main GTK iteration loop.
      // Sleep
       loop_rate.sleep ();   

    return 0;

Zoom in/Zoom out each image independently via mouse clicking.

On a click, hide the GtkTable and create a different GtkWidget with the relevant image inside.

The "gtk_widget_show_all ( globalTopLevelWindow )" in the main loop is WRONG and I need to place it in a different place, I probably need to show the widgets in a different way, but in such a way, that in one hand the video stream will always be updated, and on the other hand, the widgets will be independent...

What is the correct way to achieve the needed goal?

Thank you all in advance,

 Post subject: Re: How to show GtkImages in a correct hierarchy?
PostPosted: Sat Jan 07, 2012 10:39 am 
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 709
Location: UK

gtk_widget_show_all() has been placed in the wrong part of your code. You only need to have it called once to tell GTK that that widget tree is to be shown. By placing it in your loop you are wasting CPU cycles by scanning the tree of widgets. If you need to tell GTK to do a redraw of a widget there are other ways to do this, but since I do not know anything else about your code I can not give specifics.

If you want to reach your goal you will need to do something about your main loop. You could do something like I had shown you in another message thread. As your current implementation will cause problems. Take for example if you start monitoring the mouse button presses and mouse movement, then a button press and a mouse movement could become very unresponsive due to the loop being forced to sleep for 0.01 seconds every iteration.

So thought on how to down zoom in/out.
- Mouse press down, (work out start point from this), mouse drag, mouse button release (work out end point from this), work out the area to zoom to from this.
- Zoom out just a single mouse click - no drag
- Double click to view just one camera and again to switch back

In some ways your application lends its self well to being made multi-threaded. So you would have GTK being in the main thread and ROS in a worker thread. They can then work semi independently of each other only passing data when needed. This way you are not forcing the CPU to sleep and if you have a multi core CPU then you are taking advantage of the cores to do your graphic translations. The big down side of this is that you will need to put in much more thought in the layout of your code so that each thread does not start tripping over each other. This could be a project for much later!


 Post subject: Re: How to show GtkImages in a correct hierarchy?
PostPosted: Mon Jan 09, 2012 7:05 pm 
Familiar Face

Joined: Mon Oct 17, 2011 10:18 am
Posts: 26
Hello Errol and all the others as well,

First of all I wanted to thank you again for your answer, it is professional as always.
I still didnĀ“t realised (implemented) all your suggestions, since I am a little stressed with time those days, but some of your ideas were taken into account.

Now what I intend to do is to hold in a memory two different structures:
1. "The hierarchy"* for the 4x4 grid representation.
2. "The hierarchy"* for a single image representation.

* "The hierarchy" = EventBox (GtkWidget)---points to--->GtkWidget ---points to---> GtkPixbuf ---points to---> IplImage (for each frame of the video).

The idea:
On one click (on one of the "small" images that are hold inside a GtkTable), the 4x4 grid will be hidden, and the "big image" window (container that holds "the hierarchy" for each and every image) will be shown. It will exactly overlap the area of the 4X4 grid, such that for the user it will be seen that a zoom on that specific image (one of the four) was done. The inverse will happen by clicking on the "big image".

The question:
Is it possible to do that in such a way that the hidden container will not "cut/crop" the one I want to show? If it is not possible, what may be a solution (making a widget to be transparent)?

Thanks in advance and have a nice week :),

P.S. If you have time, it would be grate if you could have a look on this post of mine (I tried to find a different approach to my problem/challenge):

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group