callbacks.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <gtk/gtk.h>
33 
34 #include <graphviz/gvplugin_device.h>
35 #include <graphviz/gvplugin_render.h>
36 #include <graphviz/gvplugin.h>
37 #include <graphviz/gvc.h>
38 #include <graphviz/gvplugin_layout.h>
39 #include <graphviz/gvcjob.h>
40 #include <graphviz/gvcommon.h>
41 #include <graphviz/gvcext.h>
42 #include <graphviz/types.h>
43 #include <graphviz/graph.h>
44 #include <graphviz/geom.h>
45 
46 #include "callbacks.h"//silly prototypes, this include is useless
47 
48 #include <iostream>
49 
50 //These two includes are needed for the external tree variable
51 #include <mtt/tree.hh>
52 #include <mtt/types_declaration.h>
53 
54 #include <boost/format.hpp>
55 
56 #include <mtt/graph_wrapper.h>
57 
58 using namespace std;
59 using namespace boost;
60 
62 extern GVGraph*graph_context;
63 
66 
67 extern "C" {
68  extern int agrelabel_node(Agnode_t * n, char *newname);
69 }
70 
71 ostream& operator<< (ostream &o, const pointf &i)
72 {
73  return o<<i.x<<" "<<i.y;
74 }
75 
76 ostream& operator<< (ostream &o, const boxf &i)
77 {
78  return o<<"LL: "<<i.LL<<" UR: "<<i.UR;
79 }
80 
81 gboolean DrawingareaExposeEvent(GtkWidget*widget,GdkEventExpose*event,gpointer user_data)
82 {
83  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
84  cairo_t *cr = gdk_cairo_create(widget->window);
85 
86  (job->callbacks->motion)(job, job->pointer);
87 
88  job->context = (void *)cr;
89  job->external_context = true;
90  job->width = widget->allocation.width;
91  job->height = widget->allocation.height;
92 
93 // job->width =widget->allocation.width*2;
94 // job->height =widget->allocation.height*2;
95 
97 
98  if(global_tree.ready_from_draw==false || global_tree.need_layout==false)
99  {
100  (job->callbacks->refresh)(job);
101  cairo_destroy(cr);
102  return false;
103  }
104 
105 // cout<<endl<<endl;
106 // cout<<"Clear all"<<endl;
108 
109 // cout<<endl<<endl;
110 // cout<<"Create new tree"<<endl;
111 
112  for(tree<t_nodePtr>::iterator it = global_tree.begin(); it != global_tree.end(); ++it)
113  {
114 // if((*it)->id!=0 && (*it)->id!=1)
115 // continue;
116 
117  graph_context->addNode((*it)->GetName());
118 
119  switch((*it)->mode)
120  {
121  case t_node::NEW:
122  graph_context->setNodeAttribute((*it)->GetName(),string("color"),string("blue"));
123  break;
124 
125  case t_node::MAIN:
126  graph_context->setNodeAttribute((*it)->GetName(),string("color"),string("black"));
127  break;
128 
129  case t_node::FAILED:
130  graph_context->setNodeAttribute((*it)->GetName(),string("color"),string("red"));
131  break;
132 
133  default:
134  break;
135  }
136 
137  parent=global_tree.parent(it);
138 
139  if(parent!=NULL)
140  graph_context->addEdge((*parent)->GetName(),(*it)->GetName());
141  }
142 
143 // cout<<endl;
144 // cout<<"Tree created applying layout"<<endl;
145 
147  global_tree.need_layout=false;
148 
149  (job->callbacks->refresh)(job);
150 
151  cairo_destroy(cr);
152 
153  return FALSE;
154 }
155 
156 gboolean DrawingareaButtonPressEvent(GtkWidget*widget,GdkEventButton *event,gpointer user_data)
157 {
158  static bool init=true;
159  static bool dragging=false;
160 
161  if(init)
162  {
163  g_object_set_data(G_OBJECT(widget),"dragging",&dragging);
164  init=false;
165  }
166 
167  if(event->type==GDK_BUTTON_PRESS)
168  dragging=true;
169  else if(event->type==GDK_BUTTON_RELEASE)
170  dragging=false;
171 
172  return true;
173 }
174 
175 gboolean DrawingareaMotionNotifyEvent(GtkWidget*widget,GdkEventMotion*event,gpointer user_data)
176 {
177  static bool first_drag=true;
178 
179  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
180 
181  bool *dragging_p = (bool*)g_object_get_data(G_OBJECT(widget),"dragging");
182  if(!dragging_p)
183  return FALSE;//the draging variable was not been set yet, it is set by the button press event handler
184 
185  bool dragging=*dragging_p;
186 
187  static pointf diff;
188 
189  double dpix=-1/job->devscale.x;//can also be interpreted as scroling speed
190  double dpiy=-1/job->devscale.y;//can also be interpreted as scroling speed
191 
192  diff.x=event->x-job->pointer.x;
193  diff.y=event->y-job->pointer.y;
194 
195  job->pointer.x = event->x;
196  job->pointer.y = event->y;
197 
198  gtk_widget_queue_draw(widget);
199 
200  if(dragging)
201  {
202  if(first_drag)
203  {
204  diff.x=0;
205  diff.y=0;
206  first_drag=false;
207  }
208 
209  job->focus.x+= diff.x*dpix/job->zoom;
210  job->focus.y+= diff.y*dpiy/job->zoom;
211 
212  double x_lim = (0.37*job->width-1)/job->zoom;
213  double y_lim = (0.37*job->height-1)/job->zoom;
214 
215 // cout<<"Dpi: "<<job->dpi<<endl;
216 // cout<<"View: "<<job->view<<endl;
217 // cout<<"translation: "<<job->translation<<endl;
218 // cout<<"devscale: "<<job->devscale<<endl;
219 // cout<<"focus: "<<job->focus<<endl;
220 // cout<<"scale: "<<job->scale<<endl;
221 
222  if(job->focus.x>= x_lim)
223  job->focus.x= x_lim;
224 
225  if(job->focus.y>= y_lim)
226  job->focus.y= y_lim;
227  }
228 
229  return true;
230 }
231 
232 gboolean DrawingareaScrollEvent(GtkWidget*widget,GdkEventScroll *event,gpointer user_data)
233 {
234  if(event->type==GDK_SCROLL)
235  {
236  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
237 
238  pointf pointer;
239  pointer.x=event->x;
240  pointer.y=event->y;
241 
242  if(event->direction==GDK_SCROLL_UP)
243  (job->callbacks->button_press)(job,4, pointer);
244  else if(event->direction==GDK_SCROLL_DOWN)
245  (job->callbacks->button_press)(job,5, pointer);
246 
247  double x_lim = (0.37*job->width-1)/job->zoom;
248  double y_lim = (0.37*job->height-1)/job->zoom;
249 
250  if(job->focus.x>= x_lim)
251  job->focus.x= x_lim;
252 
253  if(job->focus.y>= y_lim)
254  job->focus.y= y_lim;
255  }
256 
257  return true;
258 }
259 
260 gboolean WindowDeleteEvent(GtkWidget*widget,GdkEvent*event,gpointer user_data)
261 {
262  gtk_main_quit();
263  return true;
264 }
265 
266 gboolean DrawingareaConfigureEvent(GtkWidget*widget,GdkEventConfigure*event,gpointer user_data)
267 {
268  GVJ_t *job;
269  double zoom_to_fit;
270 
271 /*FIXME - should allow for margins */
272 /* - similar zoom_to_fit code exists in: */
273 /* plugin/gtk/callbacks.c */
274 /* plugin/xlib/gvdevice_xlib.c */
275 /* lib/gvc/gvevent.c */
276 
277  job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
278 
279  if (! job->has_been_rendered)
280  {
281  zoom_to_fit = MIN((double) event->width / (double) job->width,(double) event->height / (double) job->height);
282  if (zoom_to_fit < 1.0) /* don't make bigger */
283  job->zoom *= zoom_to_fit;
284  }else if(job->fit_mode)
285  {
286  zoom_to_fit = MIN((double) event->width / (double) job->width, (double) event->height / (double) job->height);
287  job->zoom *= zoom_to_fit;
288  }
289 
290  if ((unsigned int)event->width > job->width || (unsigned int)event->height > job->height)
291  job->has_grown = TRUE;
292 
293  job->width = event->width;
294  job->height = event->height;
295  job->needs_refresh = TRUE;
296 
297  return FALSE;
298 }
An object containing a libgraph graph and its associated nodes and edges. .
Definition: graph_wrapper.h:54
gboolean WindowDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
Definition: callbacks.cpp:260
int agrelabel_node(Agnode_t *n, char *newname)
void clearNodes()
Clear the whole graph.
tree< t_nodePtr > global_tree
External variable linking to the main tree pointer in pmht.cpp.
Definition: pmht.cpp:37
void addNode(const string &name)
Add a new node to the graph.
ostream & operator<<(ostream &o, const pointf &i)
Definition: callbacks.cpp:71
GVGraph * graph_context
External variable linking to the main graph context created in the mht main source code pmht...
Definition: mht.cpp:43
Header for type declaration, only constant velocity Kalman filter.
gboolean DrawingareaExposeEvent(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
Definition: callbacks.cpp:81
gboolean DrawingareaScrollEvent(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
Definition: callbacks.cpp:232
gboolean DrawingareaButtonPressEvent(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
Definition: callbacks.cpp:156
GVGraph class declaration.
gboolean DrawingareaConfigureEvent(GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
Definition: callbacks.cpp:266
Definition: tree.h:71
int setNodeAttribute(string name, string attribute, string value)
Sets node attributes.
gboolean DrawingareaMotionNotifyEvent(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
Definition: callbacks.cpp:175
Depth-first iterator, first accessing the node, then its children.
Definition: tree.h:128
Callback header, only function prototypes.
void addEdge(const string &source, const string &target)
Add a new edge.
void applyLayout()
Apply a new layout.


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18