MyArea.cc
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           WMPSNIFFER          --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: MyArea.cc
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2012, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 
00037 #include "config.h"
00038 #include "MyArea.hh"
00039 #include <cassert>
00040 #include "misc.h"
00041 #include <gtkmm/drawingarea.h>
00042 
00043 using namespace std;
00044 
00045 #define X_BORDER 30.0
00046 #define Y_BORDER 15.0
00047 class MyArea * my_area_c_wrapper;
00048 
00049 MyArea::MyArea() : DrawingArea() {
00050         pthread_mutex_init(&sem, NULL);
00051         Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap ();
00052         cols[0] = Gdk::Color("black");
00053         cols[2] = Gdk::Color("red");
00054         cols[3] = Gdk::Color("green");
00055         cols[1] = Gdk::Color("orange");
00056         cols[4] = Gdk::Color("blue");
00057         cols[5] = Gdk::Color("yellow");
00058         cols[6] = Gdk::Color("orange");
00059         cols[7] = Gdk::Color("forest green");
00060         cols[8] = Gdk::Color("violet");
00061         cols[9] = Gdk::Color("brown");
00062         cols[10]= Gdk::Color("dark sea green");
00063         cols[11]= Gdk::Color("dodger blue");
00064 
00065         for (int i=0;i<12;i++){
00066                 colormap->alloc_color(cols[i]);
00067         }
00068         this->set_events(
00069         Gdk::BUTTON_PRESS_MASK |    //mouse button down
00070         Gdk::BUTTON_RELEASE_MASK |    //mouse button up
00071         Gdk::POINTER_MOTION_MASK);
00072         my_area_c_wrapper=this;
00073         selected=-1;
00074         mouse_on=-1;
00075         this->signal_button_press_event().connect(sigc::mem_fun(*this, &MyArea::on_MyArea_button_press_event), false);
00076         this->signal_button_release_event().connect(sigc::mem_fun(*this, &MyArea::on_MyArea_button_release_event), false);
00077         this->signal_motion_notify_event().connect(sigc::mem_fun(*this, &MyArea::on_MyArea_motion_notify_event), false);
00078 }
00079 
00080 void MyArea::beginInsert(){
00081 
00082 }
00083 
00084 void MyArea::delete_older(){
00085         WAIT(&sem);
00086         if (frameVec.size()>0)
00087                 frameVec.erase( frameVec.begin() );
00088         SIGNAL(&sem);
00089 }
00090 
00091 int MyArea::get_older_offset(){
00092         if (frameVec.size()>0)
00093                 return frameVec.at(0).pos_in_file;
00094                 else return -1;
00095 }
00096 int MyArea::insertOne(int from, int to, unsigned long long time ,int pos_in_file,unsigned long serial,int col, int nt){
00097         WAIT(&sem);
00098         Frame f;
00099         f.src=from;
00100         f.dest=to;
00101         f.time=time;
00102         f.serial=serial;
00103         f.nt=nt;
00104         f.col=col;
00105         f.mouseover=0;
00106         f.pos_in_file=pos_in_file;
00107         f.mouseover=1;
00108         f.cross=false;
00109         f.mark=false;
00110         f.left_cross=false;
00111         if (frameVec.size()>0) frameVec.back().mouseover=0;
00112         frameVec.push_back(f);
00113         setActive(f.src);
00114         compute_environment();
00115         int res = frameVec.size()-1;
00116         SIGNAL(&sem);
00117         return res;
00118 }
00119 int MyArea::insertOne(int from, int to, long time ,int pos_in_file,int col, int nt){
00120         WAIT(&sem);
00121         Frame f;
00122         f.src=from;
00123         f.dest=to;
00124         f.time=time;
00125         f.nt=nt;
00126         f.col=col;
00127         f.mouseover=0;
00128         f.pos_in_file=pos_in_file;
00129         f.mouseover=1;
00130         f.cross=false;
00131         f.mark=false;
00132         f.left_cross=false;
00133         if (frameVec.size()>0) frameVec.back().mouseover=0;
00134         frameVec.push_back(f);
00135         setActive(f.src);
00136         compute_environment();
00137         int res = frameVec.size()-1;
00138         SIGNAL(&sem);
00139         return res;
00140 }
00141 
00142 void MyArea::addDot(int frame, int node, int color){
00143         WAIT(&sem);
00144         Dot f;
00145         f.node=node;
00146         f.col=color;
00147         frameVec.at(frame).dot.push_back(f);
00148         SIGNAL(&sem);
00149 }
00150 
00151 void MyArea::addMark(int frame){
00152         WAIT(&sem);
00153         frameVec.at(frame).mark=true;
00154         SIGNAL(&sem);
00155 }
00156 
00157 void MyArea::addCross(int frame){
00158         WAIT(&sem);
00159         frameVec.at(frame).cross=true;
00160         SIGNAL(&sem);
00161 }
00162 
00163 void MyArea::addLeftCross(int frame){
00164         WAIT(&sem);
00165         frameVec.at(frame).left_cross=true;
00166         SIGNAL(&sem);
00167 }
00168 
00169 
00170 void MyArea::insert(int from, int to, long time ,int col){
00171         WAIT(&sem);
00172         Frame f;
00173         f.src=from;
00174         f.dest=to;
00175         f.time=time;
00176         f.col=col;
00177         f.mouseover=1;
00178         frameVec.back().mouseover=0;
00179         frameVec.push_back(f);
00180         setActive(f.src);
00181         compute_environment();
00182         SIGNAL(&sem);
00183 }
00184 
00185 void MyArea::endInsert(){
00186         WAIT(&sem);
00187         compute_environment();
00188         SIGNAL(&sem);
00189 }
00190 
00191 void MyArea::setNumOfNodes(int n){
00192         WAIT(&sem);
00193         num_nodes=n;
00194         activeVec.resize(n);
00195         SIGNAL(&sem);
00196 }
00197 
00198 bool MyArea::timer_callback(){
00199         return 1;
00200 }
00201 
00202 void MyArea::on_realize(){
00203         Gtk::DrawingArea::on_realize();
00204         window = get_window();
00205         gc = Gdk::GC::create(window);
00206         window->clear();
00207 }
00208 int MyArea::set_selected(int n){
00209         WAIT(&sem);
00210         for (unsigned int i=0;i<frameVec.size();i++){
00211                 frameVec.at(i).mouseover=0;
00212                 if (frameVec.at(i).pos_in_file==n){
00213                         selected=i;
00214                         frameVec.at(i).mouseover=1;
00215                 }
00216         }
00217         SIGNAL(&sem);
00218         return 0;
00219 }
00220 
00221 void MyArea::set_max_size(int _max_size){
00222         WAIT(&sem);
00223         max_size=_max_size;
00224         SIGNAL(&sem);
00225 }
00226 
00227 int MyArea::get_y_pos(int node_id){
00228         int height=this->get_height();
00229         int y_sep=(int) ((height - 2*Y_BORDER )/(num_nodes-1));
00230         return (int) (Y_BORDER+node_id*y_sep);
00231 }
00232 
00233 void MyArea::compute_environment(){
00234         max=0;min=2^15;
00235         for (unsigned int i=0;i<frameVec.size();i++){
00236                 if (frameVec.at(i).time > max){
00237                         max=frameVec.at(i).time;
00238                 }
00239                 if (frameVec.at(i).time < min){
00240                         min=frameVec.at(i).time;
00241                 }
00242         }
00243 }
00244 
00245 int idx=0;
00246 void MyArea::paint(){
00247         WAIT(&sem);
00248         Glib::RefPtr<Pango::Layout> layout = create_pango_layout("");
00249         Pango::FontDescription *timesnr_9= new Pango::FontDescription("Times new roman 9");
00250         Pango::FontDescription *timesnr_10= new Pango::FontDescription("Times new roman bold 9");
00251     char tmp[64];
00252 
00253         gc->set_foreground(cols[0]);
00254         for (int i=0;i< num_nodes;i++){
00255                 window->draw_line(gc,15,get_y_pos(i),this->get_width()-15,get_y_pos(i));
00256                 if (!activeVec.at(i)){
00257                         layout->set_font_description(*timesnr_9);
00258                 }else{
00259                         layout->set_font_description(*timesnr_10);
00260                 }
00261                 sprintf(tmp,"%d",i);//+idx++);
00262             layout->set_markup(tmp);
00263             get_window()->draw_layout(Gdk::GC::create(get_window()),8,get_y_pos(i)-6, layout);
00264             get_window()->draw_layout(Gdk::GC::create(get_window()),this->get_width()-11,get_y_pos(i)-6, layout);
00265         }
00266         if (max==min) {
00267                 SIGNAL(&sem);
00268                 return;
00269         }
00270         int width=(int) (this->get_width()-2*X_BORDER);
00271         double factor;
00272         if (0){//(bytime){
00273                 factor=(width/(max-min));
00274         } else{
00275                 if (frameVec.size()>1){
00276                         factor = (double) width / (double)(frameVec.size()-1);
00277                 }else{
00278                         factor = (double) width;
00279                 }
00280         }
00281         for (unsigned int i=0;i< frameVec.size();i++){
00282                 double x_pose = (double)X_BORDER + factor * (double) i;
00283                 frameVec.at(i).x_draw=(int) x_pose;
00284                 frameVec.at(i).y1_draw=get_y_pos(frameVec.at(i).src);
00285                 frameVec.at(i).y2_draw=get_y_pos(frameVec.at(i).dest);
00286                 for (unsigned int j=0; j< frameVec.at(i).dot.size();j++){
00287 
00288                         frameVec.at(i).dot.at(j).x_draw=(int) x_pose;
00289                         frameVec.at(i).dot.at(j).y_draw=get_y_pos(frameVec.at(i).dot.at(j).node);
00290                 }
00291                 draw_frame(i);
00292         }
00293 
00294         SIGNAL(&sem);
00295 }
00296 
00297 void MyArea::draw_frame(int i){
00298         gc->set_foreground(cols[frameVec.at(i).col]);
00299         int xpos=frameVec.at(i).x_draw;
00300         int y1=frameVec.at(i).y1_draw;
00301         int y2=frameVec.at(i).y2_draw;
00302         window->draw_line(gc,xpos,y1,xpos,y2);
00303         if (y1>y2){
00304                 if (frameVec.at(i).nt != 2){
00305                         window->draw_line(gc,xpos-5,y2+5,xpos,y2);
00306                         window->draw_line(gc,xpos+5,y2+5,xpos,y2);
00307                         if (frameVec.at(i).nt) window->draw_line(gc,xpos-5,y2+5,xpos+5,y2+5);
00308                 }
00309         }else{
00310                 if (frameVec.at(i).nt != 2){
00311                         window->draw_line(gc,xpos-5,y2-5,xpos,y2);
00312                         window->draw_line(gc,xpos+5,y2-5,xpos,y2);
00313                         if (frameVec.at(i).nt) window->draw_line(gc,xpos-5,y2-5,xpos+5,y2-5);
00314                 }
00315         }
00316         //if (frameVec.at(i).mouseover==2)gc->set_foreground(cols[0]);
00317         if (frameVec.at(i).mouseover){
00318                 window->draw_line(gc,xpos-1,y1+1,xpos-1,y2+1);
00319                 window->draw_line(gc,xpos+1,y1+1,xpos+1,y2+1);
00320         }
00321 
00322         if (frameVec.at(i).mark){
00323                 if (y1 < y2){
00324                         window->draw_line(gc,xpos-3,5+(y1+y2)/2,xpos+3,5+(y1+y2)/2);
00325                 }else{
00326                         window->draw_line(gc,xpos-3,-5+(y1+y2)/2,xpos+3,-5+(y1+y2)/2);
00327                 }
00328         }
00329 
00330         if (frameVec.at(i).cross){
00331                 window->draw_line(gc,xpos-3,+3+(y1+y2)/2,xpos+3,-3+(y1+y2)/2);
00332         }
00333         if (frameVec.at(i).left_cross){
00334                 window->draw_line(gc,xpos-3,-3+(y1+y2)/2,xpos+3,+3+(y1+y2)/2);
00335         }
00336 
00337         for (unsigned int j=0; j< frameVec.at(i).dot.size(); j++){
00338                 Dot d = frameVec.at(i).dot.at(j);
00339                 gc->set_foreground(cols[d.col]);
00340                 window->draw_point(gc,d.x_draw-1,d.y_draw);
00341                 window->draw_point(gc,d.x_draw+1,d.y_draw);
00342                 window->draw_point(gc,d.x_draw,d.y_draw-1);
00343                 window->draw_point(gc,d.x_draw,d.y_draw+1);
00344                 window->draw_point(gc,d.x_draw-2,d.y_draw);
00345                 window->draw_point(gc,d.x_draw+2,d.y_draw);
00346                 window->draw_point(gc,d.x_draw,d.y_draw-2);
00347                 window->draw_point(gc,d.x_draw,d.y_draw+2);
00348                 window->draw_point(gc,d.x_draw-1,d.y_draw-1);
00349                 window->draw_point(gc,d.x_draw-1,d.y_draw+1);
00350                 window->draw_point(gc,d.x_draw+1,d.y_draw+1);
00351                 window->draw_point(gc,d.x_draw+1,d.y_draw-1);
00352         }
00353 }
00354 
00355 bool MyArea::on_MyArea_motion_notify_event(GdkEventMotion* ev){
00356         WAIT(&sem);
00357         mouse_on=-1;
00358         for (unsigned int i=0; i<frameVec.size(); i++) {
00359                 if (i!=selected) frameVec.at(i).mouseover=0;
00360                 if (abs((frameVec.at(i).x_draw-(int)ev->x))<5){
00361                         frameVec.at(i).mouseover=2;
00362                         mouse_on=i;
00363                 }
00364         }
00365         SIGNAL(&sem);
00366         return 0;
00367 }
00368 int MyArea::get_selected_serial(){
00369         WAIT(&sem);
00370         if (selected>=0 && (unsigned int) selected < frameVec.size()){
00371                 int ret = frameVec.at(selected).serial;
00372                 SIGNAL(&sem);
00373                 return ret;
00374         }
00375         SIGNAL(&sem);
00376         return -1;
00377 }
00378 long MyArea::get_selected_time(){
00379         WAIT(&sem);
00380         if (selected>=0 && selected< frameVec.size()){
00381                 long ret = frameVec.at(selected).time;
00382                 SIGNAL(&sem);
00383                 return ret;
00384         }
00385         SIGNAL(&sem);
00386         return -1;
00387 }
00388 long MyArea::get_mouse_on_time(){
00389         WAIT(&sem);
00390         if (mouse_on>=0 && mouse_on< frameVec.size()){
00391                 long ret = frameVec.at(mouse_on).time;
00392                 SIGNAL(&sem);
00393                 return ret;
00394         }
00395         SIGNAL(&sem);
00396         return -1;
00397 }
00398 
00399 int MyArea::get_mouse_on_serial(){
00400         if (mouse_on>=0 && mouse_on< frameVec.size()){
00401                 int ret = frameVec.at(mouse_on).serial;
00402                 SIGNAL(&sem);
00403                 return ret;
00404         }
00405         SIGNAL(&sem);
00406         return -1;
00407 }
00408 
00409 void MyArea::clean_window(){
00410         WAIT(&sem);
00411         frameVec.clear();
00412         SIGNAL(&sem);
00413 }
00414 
00415 bool MyArea::on_expose_event(GdkEventExpose* e){
00416         //fprintf(stderr,"expose\n");
00417         paint();
00418     return true;
00419 }
00420 
00421 int MyArea::get_selected(){
00422         return selected;
00423 }
00424 
00425 int MyArea::get_offset_of_selected(){
00426         WAIT(&sem);
00427         if (selected<0 || frameVec.size()<selected) {
00428                 SIGNAL(&sem);
00429                 return -1;
00430         }
00431         int ret = frameVec.at(selected).pos_in_file;
00432         SIGNAL(&sem);
00433         return ret;
00434 }
00435 
00436 int MyArea::get_selected(int & from, int& to, int &col){
00437         WAIT(&sem);
00438         if (selected<0 || frameVec.size()<selected) {
00439                 SIGNAL(&sem);
00440                 return -1;
00441         }
00442         from=frameVec.at(selected).src;
00443         to=frameVec.at(selected).dest;
00444         col=frameVec.at(selected).col;
00445         SIGNAL(&sem);
00446         return selected;
00447 }
00448  bool MyArea::on_MyArea_button_press_event(GdkEventButton *ev){
00449          WAIT(&sem);
00450          for (int i=0;i<frameVec.size();i++){
00451                 frameVec.at(i).mouseover=0;
00452                 if (abs((int)ev->x-frameVec.at(i).x_draw)<5){
00453                         frameVec.at(i).mouseover=1;
00454                         selected=i;
00455                 }
00456          }
00457          SIGNAL(&sem);
00458          return false;
00459 }
00460 bool MyArea::on_MyArea_button_release_event(GdkEventButton *ev){
00461         return true;
00462 }
00463 
00464 void MyArea::resetActive(){
00465          WAIT(&sem);
00466         for (int i = 0 ; i< num_nodes; i++){
00467                 activeVec.at(i)=false;
00468         }
00469         SIGNAL(&sem);
00470 }
00471 void MyArea::setActive(int i){
00472         activeVec.at(i)=true;
00473 }


ros_rt_wmp_sniffer
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:57