Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gui.cpp

Go to the documentation of this file.
00001 // #include "planar_map.hpp"
00002 #include "gui.hpp"
00003 
00004 #include <CGAL/Bbox_2.h>
00005 #include <CGAL/IO/Qt_widget.h>
00006 #include <CGAL/IO/Qt_widget_standard_toolbar.h>
00007 #include <qpixmap.h>
00008 #include <qimage.h>
00009 
00010 using namespace Arak;
00011 
00012 #include "gui.moc"
00013 
00014 QApplication* Arak::InitGui(int argc,
00015           char** argv, 
00016           int width,
00017           int height, 
00018           Coloring& c, 
00019           ArakProcess& p,
00020           GridColorEstimator& e,
00021           QMutex& mutex,
00022           bool toolbar,
00023           double refreshRateHz) {
00024   ColoringQTLayer* layer = new ColoringQTLayer(c, p, e, mutex);
00025   QApplication* app = new QApplication(argc, argv);
00026   QMainWindow* win = new QMainWindow();
00027   app->setMainWidget(win);
00028   win->resize(width, height);
00029   win->show();
00030 
00031   ColoringQTWidget* w = new ColoringQTWidget(*win, *layer, refreshRateHz);
00032   w->attach(layer);
00033   win->setCentralWidget(w);
00034   CGAL::Bbox_2 bbox = c.boundary().bbox();
00035   w->set_window(bbox.xmin(),
00036     bbox.xmax(),
00037     bbox.ymin(),
00038     bbox.ymax());
00039   w->setFilled(true);
00040   w->resize(width, height);
00041   w->show();
00042 
00043   if (toolbar) {
00044     CGAL::Qt_widget_standard_toolbar *stoolbar;
00045     stoolbar = 
00046       new CGAL::Qt_widget_standard_toolbar(w, win, "Standard Toolbar");
00047   }
00048 
00049   layer->draw();
00050 
00051   return app;
00052 }
00053 
00054 ColoringQTLayer::ColoringQTLayer(Coloring& c, 
00055          ArakProcess& p, 
00056          GridColorEstimator& e,
00057          QMutex& mutex) 
00058   : c(c), p(p), e(e), mutex(mutex), curType(COLORING) {
00059 }
00060 
00061 void ColoringQTLayer::draw() {
00062   mutex.lock();
00063   widget->lock();
00064 
00065   // Draw the white background.
00066   *widget << CGAL::BackgroundColor(CGAL::WHITE);
00067 
00068   switch (curType) {
00069   case PROC_AND_BD:
00070     p.visualize(*widget);
00071     c.visualize(*widget, true, true, false, false);
00072     break;
00073   case COLORING:
00074     c.visualize(*widget, false, false, true, false);
00075     break;
00076   case PT_COL_EST_AND_BD:
00077     e.visualize(*widget);
00078     c.visualize(*widget, true, true, false, false);
00079     break;
00080   case QUERY_PTS_AND_BD:
00081     c.visualize(*widget, true, true, false, true);
00082     break;
00083   default:
00084     assert(false);
00085   }
00086   
00087   widget->unlock();
00088   mutex.unlock();
00089 }
00090 
00091 ColoringQTWidget::ColoringQTWidget(QMainWindow& win, 
00092            ColoringQTLayer& layer,
00093            double refreshRateHz)
00094   : CGAL::Qt_widget(&win) {
00095   this->layer = &layer;
00096   this->timer = new QTimer(this);
00097   connect(timer, SIGNAL(timeout()), SLOT(timeout()));
00098   timer->start(int(1000.0 / refreshRateHz));
00099 }
00100 
00101 void ColoringQTWidget::timeout() {
00102   layer->draw();
00103 }
00104 
00105 void ColoringQTWidget::mousePressEvent(QMouseEvent *e) {
00106   Qt_widget::mousePressEvent(e);
00107   if(e->button() == Qt::RightButton) {
00108     if (layer->curType == PROC_AND_BD) {
00109       layer->curType = COLORING;
00110     } else if (layer->curType == COLORING) {
00111       layer->curType = PT_COL_EST_AND_BD;
00112     } else if (layer->curType == PT_COL_EST_AND_BD) {
00113       layer->curType = QUERY_PTS_AND_BD;
00114       // layer->curType = PROC_AND_BD;
00115     } else if (layer->curType == QUERY_PTS_AND_BD) {
00116       layer->curType = PROC_AND_BD;
00117     }
00118   }
00119   /*
00120   {
00121     Point_2 p = Point_2(x_real(e->x()), y_real(e->y()));
00122     if(fsqtl->fs->map.number_of_halfedges() == 0)
00123     {
00124       fsqtl->fs->Set_free_face(p);
00125       fsqtl->fs->Compute_frontiers();
00126 
00127       if(fsqtl->fs->final_path.size() > 0)
00128       {
00129         fsqtl->istate = fsqtl->fs->final_path.begin();
00130         fsqtl->fs->Make_polygons_from_info_state(fsqtl->polygons,
00131                                                  fsqtl->labels,
00132                                                  *fsqtl->istate);
00133         fsqtl->save_img = true;
00134       }
00135     }
00136     else
00137     {
00138       if(fsqtl->fs->final_path.size() > 0)
00139       {
00140         fsqtl->istate++;
00141         if(fsqtl->istate == fsqtl->fs->final_path.end())
00142           fsqtl->istate = fsqtl->fs->final_path.begin();
00143         fsqtl->fs->Make_polygons_from_info_state(fsqtl->polygons,
00144                                                  fsqtl->labels,
00145                                                  *fsqtl->istate);
00146         fsqtl->save_img = true;
00147       }
00148     }
00149   }
00150   else if(e->button() == Qt::RightButton)
00151   {
00152     Point_2 p = Point_2(x_real(e->x()), y_real(e->y()));
00153     if(!this->setting_theta)
00154     {
00155       fsqtl->fs->robot = p;
00156       fsqtl->fs->Set_free_face(fsqtl->fs->robot);
00157       this->setting_theta = true;
00158     }
00159     else
00160     {
00161       Vertex_iteratorList vlist;
00162       GapEdgeList glist;
00163       fsqtl->fs->gap_edges.clear();
00164       fsqtl->fs->Compute_gap_edges(&vlist,glist,fsqtl->fs->robot);
00165       Direction_2 dir(Ray_2(fsqtl->fs->robot,p));
00166       Direction_2 min_dir = fsqtl->fs->min_phi_tx(dir);
00167       Direction_2 max_dir = fsqtl->fs->max_phi_tx(dir);
00168       fsqtl->fs->Add_FOV_boundaries(fsqtl->fs->gap_edges,glist,
00169                                     fsqtl->fs->robot,min_dir,max_dir);
00170       this->setting_theta = false;
00171     }
00172   }
00173   else if(e->button() == Qt::MidButton)
00174   {
00175     if(fsqtl->draw_boundary)
00176       fsqtl->draw_boundary = false;
00177     else
00178       fsqtl->draw_boundary = true;
00179   }
00180   redraw();
00181   if(fsqtl->save_img)
00182   {
00183     //QPixmap qp = QPixmap::grabWindow(this->winId(),0,50,-1,this->height());
00184     QPixmap qp = QPixmap::grabWindow(this->winId());
00185     QImage qi = qp.convertToImage();
00186     char fname[32];
00187     sprintf(fname,"plan-phi-img-%03d.png",fsqtl->img_idx++);
00188     std::cout << "Saving screenshot:" << fname << ":" << std::endl;
00189     if(!qi.save(fname, "PNG"))
00190       std::cout << "QImage::save() failed" << std::endl;
00191     fsqtl->save_img = false;
00192   }
00193   */
00194 }
00195 
00196 

Generated on Wed May 25 14:39:17 2005 for Arak by doxygen 1.3.6