libidxgui
/home/rex/ebltrunk/tools/libidxgui/include/idxgui.h
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef IDXGUI_H_
00034 #define IDXGUI_H_
00035 
00036 #include <QtCore/QThread>
00037 #include <QtCore/QWaitCondition>
00038 #include <QtGui/QtGui>
00039 
00040 #include <ostream>
00041 #include "libidx.h"
00042 #include "scroll_box0.h"
00043 #include "gui_thread.h"
00044 #include "defines.h"
00045 
00046 using namespace std;
00047 
00048 namespace ebl {
00049 
00050   class EXPORT gui_thread;
00051   template <class T1, class T2> class ManipInfra;
00052 
00053   class idxgui : public QThread, public ostringstream {
00054     Q_OBJECT
00055   private:
00056     int                   argc;
00057     char                **argv;
00058     int                   nwid;
00059     const uint   *nwindows; // owned by gui_thread
00060     gui_thread           *gt;
00061     QMutex                mutex1;
00062 
00063   public:
00064     bool                  thread_init;
00065     bool                  cout_output;
00066     int                 (*run_main)(int, char**);
00067     bool                  main_done;    
00068 
00069   public:
00070     idxgui();
00071     EXPORT void init(int argc_, char **argv_, const uint *nwindows,
00072                      gui_thread *gt_);
00073     virtual ~idxgui();
00074 
00076     EXPORT int new_window(const char *wname = NULL, uint h = 0, uint w = 0);
00077 
00079     EXPORT int new_window3d(const char *wname = NULL, uint h = 0, uint w = 0);
00080 
00082     EXPORT void select_window(int wid);
00083 
00085     template<class T> friend
00086       idxgui& operator<<(idxgui& r, const T val);
00087 
00089     EXPORT void draw_arrow(int h1, int w1, int h2, int w2);
00091     EXPORT void draw_flow(idx<float> &flow, int h, int w);
00092 
00096     EXPORT void draw_box(float h0, float w0, float h, float w, ubyte r = 255,
00097                          ubyte g = 255, ubyte b = 255, ubyte a = 255,
00098                          string *s = NULL);
00102     EXPORT void draw_cross(float h0, float w0, float length, ubyte r = 255,
00103                            ubyte g = 255, ubyte b = 255, ubyte a = 255,
00104                            string *s = NULL);
00108     EXPORT void draw_ellipse(float h0, float w0, float h, float w,
00109                              ubyte r = 255, ubyte g = 255, ubyte b = 255,
00110                              ubyte a = 255, string *s = NULL);
00111 
00113     EXPORT void set_silent();
00115     EXPORT void set_silent(const std::string *filename);
00117     EXPORT void set_silent(const char *filename);
00118 
00121     EXPORT void disable_updates();
00122 
00125     EXPORT void enable_updates();
00126 
00128     EXPORT void quit();
00129     
00131     EXPORT void clear();
00132 
00134     EXPORT void clear_resize();
00135 
00138     EXPORT void save_window(const char *filename, int wid = -1);
00139 
00151     template<class T>
00152       void draw_matrix(idx<T> &im, uint h0, uint w0, 
00153                        double zoomh, double zoomw, T minv = 0, T maxv = 0);
00154 
00157     template<class T>
00158       void draw_matrix(idx<T> &im, uint h0 = 0, uint w0 = 0);
00159 
00166     EXPORT void draw_matrix_unsafe(idx<ubyte> &im, uint h0 = 0, uint w0 = 0); 
00167     
00169     template<class T>
00170       void draw_matrix_frame(idx<T> &im, ubyte r, ubyte g, ubyte b,
00171                              uint h0 = 0, uint w0 = 0, 
00172                              double zoomh = 1.0, double zoomw = 1.0,
00173                              T minv = 0, T maxv = 0);
00174 
00176     template<class T>
00177       void draw_matrix(idx<T> &im, const char *str, 
00178                        uint h0 = 0, uint w0 = 0, 
00179                        double zoomh = 1.0, double zoomw = 1.0,
00180                        T minv = 0, T maxv = 0);
00181 
00183     template<class T>
00184       void draw_mask(idx<T> &im, uint h0 = 0, uint w0 = 0, 
00185                      double zoomh = 1.0, double zoomw = 1.0,
00186                      ubyte r = 255, ubyte g = 0, ubyte b = 0, ubyte a = 127,
00187                      T threshold = 0.0);
00188     
00192     EXPORT void draw_text(std::string *s);
00193 
00197     EXPORT void draw_text(std::string *s, uint h0, uint w0);
00198 
00202     EXPORT void set_text_origin(uint h0, uint w0);
00203 
00210     EXPORT void set_text_colors(unsigned char fg_r, unsigned char fg_g, 
00211                                 unsigned char fg_b, unsigned char fg_a,
00212                                 unsigned char bg_r, unsigned char bg_g, 
00213                                 unsigned char bg_b, unsigned char bg_a);
00215     EXPORT void set_text_colors(int fg_r, int fg_g, 
00216                                 int fg_b, int fg_a,
00217                                 int bg_r, int bg_g, 
00218                                 int bg_b, int bg_a);
00219 
00221     EXPORT void set_bg_colors(unsigned char r, unsigned char g, 
00222                               unsigned char b);
00223 
00225     EXPORT void set_font_size(int sz);
00226 
00231     EXPORT void set_cout_and_gui();
00232     
00236     EXPORT void set_gui_only();
00237 
00238     EXPORT void add_scroll_box(scroll_box0* sb);
00239 
00241     EXPORT void set_window_title(const char *s);
00242 
00245     EXPORT void freeze_style(bool freeze);
00246 
00248     EXPORT void freeze_window_size(uint h, uint w);
00249 
00252     EXPORT int pop_key_pressed();
00253     
00255     EXPORT bool busy_drawing();
00256     
00257     // 3d calls ////////////////////////////////////////////////////////////////
00258 
00260     EXPORT void draw_sphere(float x, float y, float z, float radius,
00261                             const char *s = NULL,
00262                             int r = 255, int g = 255, int b = 255, int a = 255);
00267     EXPORT void draw_cylinder(float x, float y, float z, float length,
00268                               float top_radius, float base_radius, float a1,
00269                               float a2, const char *s = NULL,
00270                               int r = 255, int g = 255,
00271                               int b = 255, int a = 255, bool tops = false);
00273     EXPORT void draw_text_3d(float x, float y, float z, const char *s,
00274                              int r = 255, int g = 255, int b = 255, int a =255);
00277     EXPORT void draw_line_3d(float x, float y, float z,
00278                              float x1, float y1, float z1,
00279                              const char *s = NULL, int r = 255, int g = 255,
00280                              int b = 255, int a = 255);
00281     
00282   private:
00283     // check that user used MAIN_QTHREAD instead of regular main
00284     void check_init(); 
00285 
00286   signals:
00287     EXPORT void gui_drawImage(idx<ubyte> *img, uint h0, uint w0);
00288     EXPORT void gui_draw_mask(idx<ubyte> *img, uint h0, uint w0,
00289                        ubyte r, ubyte g, ubyte b, ubyte a);
00290     EXPORT void appquit();
00291     EXPORT void gui_clear();
00292     EXPORT void gui_clear_resize();
00293     EXPORT void gui_save_window(const string *filename, int wid);
00294     EXPORT void gui_new_window(const char *wname, uint h, uint w);
00295     EXPORT void gui_new_window3d(const char *wname, uint h, uint w);
00296     EXPORT void gui_select_window(int wid);
00297     EXPORT void gui_add_text(const string *s);
00298     EXPORT void gui_add_arrow(int h1, int w1, int h2, int w2);
00299     EXPORT void gui_add_flow(idx<float> *flow, int h, int w);
00300     EXPORT void gui_add_box(float h0, float w0, float h, float w, ubyte r,
00301                             ubyte g, ubyte b, ubyte a, string *s);
00302     EXPORT void gui_add_cross(float h0, float w0, float length, ubyte r,
00303                             ubyte g, ubyte b, ubyte a, string *s);
00304     EXPORT void gui_add_ellipse(float h0, float w0, float h, float w,
00305                                 ubyte r, ubyte g, ubyte b, ubyte a, string *s);
00306     EXPORT void gui_set_text_origin(uint h0, uint w0);
00307     EXPORT void gui_set_text_colors(unsigned char fg_r, unsigned char fg_g, 
00308                              unsigned char fg_b, unsigned char fg_a,
00309                              unsigned char bg_r, unsigned char bg_g, 
00310                              unsigned char bg_b, unsigned char bg_a);
00311     EXPORT void gui_set_bg_colors(unsigned char r, unsigned char g, 
00312                            unsigned char b);
00313     EXPORT void gui_set_font_size(int);
00314     EXPORT void gui_set_silent(const std::string *filename);
00315     EXPORT void gui_set_wupdate(bool update);
00316     EXPORT void gui_freeze_style(bool freeze);
00317     EXPORT void gui_freeze_window_size(uint h, uint w);
00318     EXPORT void gui_add_scroll_box(scroll_box0 *sb);
00319     EXPORT void gui_set_title(const string *s);
00320     // 3d signals //////////////////////////////////////////////////////////////
00321     EXPORT void gui_add_sphere(float,float,float,float, string *s,
00322                                int,int,int,int);
00323     EXPORT void gui_add_cylinder(float x, float y, float z, float length,
00324                                  float top_radius, float base_radius,
00325                                  float a1, float a2, string *s,
00326                                  int r, int g, int b, int a, bool tops);
00327     EXPORT void gui_draw_text_3d(float x, float y, float z, string* s,
00328                                  int r, int g, int b, int a);
00329     EXPORT void gui_draw_line_3d(float x, float y, float z,
00330                                  float x1, float y1, float z1, string* s,
00331                                  int r, int g, int b, int a);
00332     
00333   protected:
00334     virtual void run();
00335   };
00336 
00337 
00338   template<class T1, class T2>
00339   class ManipInfra {
00340   public:
00341     ManipInfra(idxgui& (*pFun) (idxgui&))
00342       : manipFun0(pFun), val1(0), val2(0), nval(0) {}
00343     ManipInfra(idxgui& (*pFun) (idxgui&, T1), T1 val1_)
00344       : manipFun1(pFun), val1(val1_), val2(0), nval(1) {}
00345     ManipInfra(idxgui& (*pFun) (idxgui&, T1, T2), 
00346                T1 val1_, T2 val2_)
00347       : manipFun2(pFun), val1(val1_), val2(val2_), nval(2) {}
00348     ManipInfra(idxgui& (*pFun) (idxgui&, T1, T2, T2, T2, T2, T2, T2, T2), 
00349                T1 val1_, T2 val2_, T2 val3_, T2 val4_, 
00350                T2 val5_, T2 val6_, T2 val7_, T2 val8_)
00351       : manipFun8(pFun), val1(val1_), val2(val2_), val3(val3_), val4(val4_), 
00352         val5(val5_), val6(val6_), val7(val7_), val8(val8_), nval(8) {}
00353 
00354     void operator() (idxgui& r) const {
00355       switch (nval) {
00356       case 0: manipFun0(r); break ;
00357       case 1: manipFun1(r, val1); break ;
00358       case 2: manipFun2(r, val1, val2); break ;
00359       case 8: manipFun8(r, val1, val2, val3, val4, val5, val6, val7, val8);
00360         break ;
00361       default: eblerror("unknown mode");
00362       }
00363     }
00364   private:
00365     idxgui& (*manipFun0) (idxgui&);
00366     idxgui& (*manipFun1) (idxgui&, T1);
00367     idxgui& (*manipFun2) (idxgui&, T1, T2);
00368     idxgui& (*manipFun8) (idxgui&, T1, T2, T2, T2, T2, T2, T2, T2);
00369     T1 val1;
00370     T2 val2, val3, val4, val5, val6, val7, val8;
00371     int nval;
00372   };
00373 
00374   template class ManipInfra<int,int>;
00375   template class ManipInfra<uint,uint>;
00376   EXPORT idxgui& operator<<(idxgui& r, const ManipInfra<uint,uint> &manip);
00377   EXPORT idxgui& operator<<(idxgui& r, const ManipInfra<int,int> &manip);
00378 
00382   EXPORT ManipInfra<uint,uint> at(uint h0, uint w0);
00383   EXPORT idxgui& att(idxgui& r, uint h0, uint w0);
00384 
00387   EXPORT ManipInfra<int,int> cout_and_gui();
00388   EXPORT idxgui& fcout_and_gui(idxgui& r);
00389 
00392   EXPORT ManipInfra<int, int> gui_only();
00393   EXPORT idxgui& fgui_only(idxgui& r);
00394 
00401   EXPORT ManipInfra<unsigned char, unsigned char> 
00402     black_on_white(unsigned char fg_a = 255, unsigned char bg_a = 255);
00403   EXPORT idxgui& fblack_on_white(idxgui& r, unsigned char fg_a, 
00404                           unsigned char bg_a);
00405 
00408   EXPORT idxgui& fwhite_on_transparent(idxgui& r);
00409   EXPORT ManipInfra<int, int> white_on_transparent();
00410 
00416   EXPORT ManipInfra<unsigned char, unsigned char> 
00417     set_colors(unsigned char fg_r, unsigned char fg_g, 
00418                unsigned char fg_b, unsigned char fg_a,
00419                unsigned char bg_r, unsigned char bg_g, 
00420                unsigned char bg_b, unsigned char bg_a);
00421   EXPORT idxgui& fset_colors(idxgui& r, 
00422                              unsigned char fg_r, unsigned char fg_g, 
00423                              unsigned char fg_b, unsigned char fg_a,
00424                              unsigned char bg_r, unsigned char bg_g, 
00425                              unsigned char bg_b, unsigned char bg_a);
00426 
00427 } // namespace ebl {
00428 
00429 #include "idxgui.hpp"
00430 
00431 #endif /* IDXGUI_H_ */