callbacks_mht.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 #include <boost/format.hpp>
51 
52 #include <mtt/hypothesisTree.h>
53 
54 #include <mtt/graph_wrapper.h>
55 
56 using namespace std;
57 using namespace boost;
58 
60 extern GVGraph*graph_context;
61 
64 
65 extern "C" {
66  extern int agrelabel_node(Agnode_t * n, char *newname);
67 }
68 
69 ostream& operator<< (ostream &o, const pointf &i)
70 {
71  return o<<i.x<<" "<<i.y;
72 }
73 
74 ostream& operator<< (ostream &o, const boxf &i)
75 {
76  return o<<"LL: "<<i.LL<<" UR: "<<i.UR;
77 }
78 
79 
80 gboolean drawGraph(void)
81 {
82  if(pthread_mutex_trylock(&(htreePtr->_draw_mutex))!=0)//its locked
83  return false;
84 
85  if(htreePtr->need_layout==false)
86  {
87  pthread_mutex_unlock(&(htreePtr->_draw_mutex));
88  return false;
89  }
90 
92 
95 
97 
98  for(it = htreePtr->begin(); it != htreePtr->end(); ++it)
99  {
100  assert(*it!=0);
101 
102  graph_context->addNode((*it)->nameUI());
103 
104  for(uint a=0;a<(*it)->_attribute_names.size();++a)
105  graph_context->setNodeAttribute((*it)->nameUI(),(*it)->_attribute_names[a],(*it)->_attribute_values[a]);
106 
107  parent=htreePtr->parent(it);
108 
109  if(parent!=NULL)
110  graph_context->addEdge((*parent)->nameUI(),(*it)->nameUI());
111  }
112 
114 
115  htreePtr->need_layout=false;
116 
117  pthread_mutex_unlock(&(htreePtr->_draw_mutex));
118 
119  return true;
120 }
121 
122 gboolean DrawingareaExposeEvent(GtkWidget*widget,GdkEventExpose*event,gpointer user_data)
123 {
124 // cout<<"in expose"<<endl;
125  pthread_mutex_lock(&(htreePtr->_draw_mutex));
126 
127  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
128  cairo_t *cr = gdk_cairo_create(widget->window);
129 
130  (job->callbacks->motion)(job, job->pointer);
131 
132  job->context = (void *)cr;
133  job->external_context = true;
134  job->width = widget->allocation.width;
135  job->height = widget->allocation.height;
136 
137 // cout<<"refresh"<<endl;
138  (job->callbacks->refresh)(job);
139 
140 // cout<<"destroy"<<endl;
141  cairo_destroy(cr);
142 
143  pthread_mutex_unlock(&(htreePtr->_draw_mutex));
144 // cout<<"out of DrawingareaExposeEvent"<<endl;
145  return true;
146 }
147 
148 gboolean DrawingareaButtonPressEvent(GtkWidget*widget,GdkEventButton *event,gpointer user_data)
149 {
150  static bool init=true;
151  static bool dragging=false;
152 
153  if(init)
154  {
155  g_object_set_data(G_OBJECT(widget),"dragging",&dragging);
156  init=false;
157  }
158 
159  if(event->type==GDK_BUTTON_PRESS)
160  dragging=true;
161  else if(event->type==GDK_BUTTON_RELEASE)
162  dragging=false;
163 
164  return true;
165 }
166 
167 gboolean DrawingareaMotionNotifyEvent(GtkWidget*widget,GdkEventMotion*event,gpointer user_data)
168 {
169  static bool first_drag=true;
170 
171  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
172 
173  bool *dragging_p = (bool*)g_object_get_data(G_OBJECT(widget),"dragging");
174  if(!dragging_p)
175  return FALSE;//the draging variable was not been set yet, it is set by the button press event handler
176 
177  bool dragging=*dragging_p;
178 
179  static pointf diff;
180 
181  double dpix=-1/job->devscale.x;//can also be interpreted as scroling speed
182  double dpiy=-1/job->devscale.y;//can also be interpreted as scroling speed
183 
184  diff.x=event->x-job->pointer.x;
185  diff.y=event->y-job->pointer.y;
186 
187  job->pointer.x = event->x;
188  job->pointer.y = event->y;
189 
190  gtk_widget_queue_draw(widget);
191 
192  if(dragging)
193  {
194  if(first_drag)
195  {
196  diff.x=0;
197  diff.y=0;
198  first_drag=false;
199  }
200 
201  job->focus.x+= diff.x*dpix/job->zoom;
202  job->focus.y+= diff.y*dpiy/job->zoom;
203 
204  double x_lim = (0.37*job->width-1)/job->zoom;
205  double y_lim = (0.37*job->height-1)/job->zoom;
206 
207 // cout<<"Dpi: "<<job->dpi<<endl;
208 // cout<<"View: "<<job->view<<endl;
209 // cout<<"translation: "<<job->translation<<endl;
210 // cout<<"devscale: "<<job->devscale<<endl;
211 // cout<<"focus: "<<job->focus<<endl;
212 // cout<<"scale: "<<job->scale<<endl;
213 
214  if(job->focus.x>= x_lim)
215  job->focus.x= x_lim;
216 
217  if(job->focus.y>= y_lim)
218  job->focus.y= y_lim;
219  }
220 
221  return true;
222 }
223 
224 gboolean DrawingareaScrollEvent(GtkWidget*widget,GdkEventScroll *event,gpointer user_data)
225 {
226  if(event->type==GDK_SCROLL)
227  {
228  GVJ_t *job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
229 
230  pointf pointer;
231  pointer.x=event->x;
232  pointer.y=event->y;
233 
234  if(event->direction==GDK_SCROLL_UP)
235  (job->callbacks->button_press)(job,4, pointer);
236  else if(event->direction==GDK_SCROLL_DOWN)
237  (job->callbacks->button_press)(job,5, pointer);
238 
239  double x_lim = (0.37*job->width-1)/job->zoom;
240  double y_lim = (0.37*job->height-1)/job->zoom;
241 
242  if(job->focus.x>= x_lim)
243  job->focus.x= x_lim;
244 
245  if(job->focus.y>= y_lim)
246  job->focus.y= y_lim;
247  }
248 
249  return true;
250 }
251 
252 gboolean WindowDeleteEvent(GtkWidget*widget,GdkEvent*event,gpointer user_data)
253 {
254  gtk_main_quit();
255  return true;
256 }
257 
258 gboolean DrawingareaConfigureEvent(GtkWidget*widget,GdkEventConfigure*event,gpointer user_data)
259 {
260  GVJ_t *job;
261  double zoom_to_fit;
262 
263 /*FIXME - should allow for margins */
264 /* - similar zoom_to_fit code exists in: */
265 /* plugin/gtk/callbacks.c */
266 /* plugin/xlib/gvdevice_xlib.c */
267 /* lib/gvc/gvevent.c */
268 
269  job = (GVJ_t *)g_object_get_data(G_OBJECT(widget),"job");
270 
271  if (! job->has_been_rendered)
272  {
273  zoom_to_fit = MIN((double) event->width / (double) job->width,(double) event->height / (double) job->height);
274  if (zoom_to_fit < 1.0) /* don't make bigger */
275  job->zoom *= zoom_to_fit;
276  }else if(job->fit_mode)
277  {
278  zoom_to_fit = MIN((double) event->width / (double) job->width, (double) event->height / (double) job->height);
279  job->zoom *= zoom_to_fit;
280  }
281 
282  if ((unsigned int)event->width > job->width || (unsigned int)event->height > job->height)
283  job->has_grown = TRUE;
284 
285  job->width = event->width;
286  job->height = event->height;
287  job->needs_refresh = TRUE;
288 
289  return FALSE;
290 }
gboolean WindowDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
An object containing a libgraph graph and its associated nodes and edges. .
Definition: graph_wrapper.h:54
gboolean DrawingareaMotionNotifyEvent(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
gboolean DrawingareaScrollEvent(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
gboolean DrawingareaConfigureEvent(GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
GVGraph * graph_context
External variable linking to the main graph context created in the mht main source code pmht...
Definition: mht.cpp:43
void clearNodes()
Clear the whole graph.
gboolean drawGraph(void)
void addNode(const string &name)
Add a new node to the graph.
gboolean DrawingareaExposeEvent(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
boost::shared_ptr< HypothesisTree > hypothesisTreePtr
Shared pointer to the HypothesisTree class.
ostream & operator<<(ostream &o, const pointf &i)
gboolean DrawingareaButtonPressEvent(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
int agrelabel_node(Agnode_t *n, char *newname)
GVGraph class declaration.
int setNodeAttribute(string name, string attribute, string value)
Sets node attributes.
void freeLayout()
Free the layout This function should be called before any modifications to the current graph...
Callback header, only function prototypes.
void addEdge(const string &source, const string &target)
Add a new edge.
pre_order_iterator iterator
The default iterator types throughout the tree class.
Definition: tree.h:184
void applyLayout()
Apply a new layout.
hypothesisTreePtr htreePtr
External variable linking to the Mht context class (main workhorse of the mht algorithm) ...
Definition: mht.cpp:50
Hypotheses tree class declaration.


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