libidxgui
/home/rex/ebltrunk/tools/libidxgui/include/gui_thread.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 GUI_THREAD_H_
00034 #define GUI_THREAD_H_
00035 
00036 #include <QPixmap>
00037 #include <QWidget>
00038 #include <QtGui>
00039 #include <QResizeEvent>
00040 #include <math.h>
00041 #include <vector>
00042 
00043 #include "libidx.h"
00044 #include "win.h"
00045 #include "win2d.h"
00046 #include "win3d.h"
00047 #include "idxgui.h"
00048 #include "scroll_box0.h"
00049 #include "defines.h"
00050 
00051 namespace ebl {
00052 
00053   class idxgui;
00056   extern IDXGUIEXPORT ebl::idxgui gui;
00057 
00061 
00062   class IDXGUIEXPORT gui_thread : public QWidget { 
00063     Q_OBJECT
00064   public:
00065     gui_thread(int argc, char **argv, QApplication &qa);
00066     virtual ~gui_thread();
00067 
00070     int pop_key_pressed();
00071 
00073     bool busy_drawing();
00074 
00077     void quit();
00078 
00080   private slots:
00081     void window_destroyed(QObject *obj);
00082     
00085     void set_wupdate(bool update);
00086     
00087     void add_text(const string *s);
00088     void add_arrow(int x1, int y1, int x2, int y2);
00089     void add_flow(idx<float> *flow, int h, int w);
00090     void add_box(float h0, float w0, float h, float w, ubyte r, ubyte g,
00091                  ubyte b, ubyte a, string *s);
00092     void add_ellipse(float h0, float w0, float h, float w,
00093                      ubyte r, ubyte g, ubyte b, ubyte a, string *s);
00094     void add_cross(float h0, float w0, float length, ubyte r, ubyte g,
00095                    ubyte b, ubyte a, string *s);
00096     void set_text_origin(unsigned int h0, unsigned int w0);
00097     void set_text_colors(unsigned char fg_r, unsigned char fg_g, 
00098                          unsigned char fg_b, unsigned char fg_a,
00099                          unsigned char bg_r, unsigned char bg_g, 
00100                          unsigned char bg_b, unsigned char bg_a);
00101     void set_bg_colors(unsigned char r, unsigned char g, 
00102                        unsigned char b);
00104     void set_font_size(int sz);
00105     void updatePixmap(idx<ubyte> *img, uint h0, uint w0);
00106     void add_mask(idx<ubyte> *img, uint h0, uint w0,
00107                   ubyte r, ubyte g, ubyte b, ubyte a);
00108     void appquit();
00109 
00111     void clear();
00112 
00114     void clear_resize();
00115     
00118     void save_window(const string *filename, int wid);
00120     void new_window(const char *wname = NULL, uint h = 0, uint w = 0);
00122     void new_window3d(const char *wname = NULL, uint h = 0, uint w = 0);
00123     void select_window(int wid);
00124     void set_silent(const std::string *filename = NULL);
00125     void add_scroll_box(scroll_box0 *sb);
00127     void set_title(const string *s);
00128 
00131     void freeze_style(bool freeze);
00132 
00134     void freeze_window_size(uint h, uint w);
00135 
00137 
00139     void add_sphere(float x, float y, float z, float radius, string *s,
00140                     int r = 255, int g = 255, int b = 255, int a = 255);
00145     void add_cylinder(float x, float y, float z, float length, float top_radius,
00146                       float base_radius, float a1, float a2, string *s,
00147                       int r = 255, int g = 255, int b = 255, int a = 255,
00148                       bool tops = false);
00150     void draw_text_3d(float x, float y, float z, string *s,
00151                       int r, int g, int b, int a);
00154     void draw_line_3d(float x, float y, float z, float x1, float y1, float z1,
00155                       string *s, int r, int g, int b, int a);
00156 
00158     // class members
00159   public:
00160     idxgui                      &thread;
00161   private:
00162     int                          wcur;
00163     unsigned int                 nwindows;
00164     std::vector<win*>            windows;
00165     bool                         silent;
00166     std::string                  savefname;
00167     bool                         busy; // flag when busy drawing
00168     QMutex                       mutex1;
00169     bool                         bquit; // indicate if we want to quit
00170     QApplication                &qapp; // reference to main QT thread
00171   };
00172 
00177 #define MAIN_QTHREAD(targc, argc, targv, argv)  \
00178   int run_main(targc argc, targv argv);         \
00179   using namespace ebl;                          \
00180   int main(targc argc, targv argv) {            \
00181     gui.thread_init = true;                     \
00182     gui.run_main = &run_main;                   \
00183     gui.thread_init = true;                     \
00184     QApplication a(argc, argv);                 \
00185     a.setQuitOnLastWindowClosed(false);         \
00186     ebl::gui_thread gt(argc, argv, a);          \
00187     gt.thread.start();                          \
00188     a.exec();                                   \
00189     return 0;                                   \
00190   }                                             \
00191   int run_main(targc argc, targv argv)
00192   
00193 
00194 #define WINMAIN_QTHREAD(targc, argc, targv, argv)                       \
00195   int run_main(targc argc, targv argv);                                 \
00196   using namespace ebl;                                                  \
00197   int WINAPI WinMain(HINSTANCE d1, HINSTANCE d2, LPSTR d3, int d4) {    \
00198     LPWSTR *argvw;                                                      \
00199     LPTSTR *argv;                                                       \
00200     targc argc = 0;                                                     \
00201     argvw = CommandLineToArgvW(GetCommandLineW(), &argc);               \
00202     USES_CONVERSION;                                                    \
00203     argv = new LPTSTR[argc];                                            \
00204     for (targc i = 0; i < argc; ++i) {                                  \
00205       argv[i] = W2A(argvw[i]);                                          \
00206     }                                                                   \
00207     gui.thread_init = true;                                             \
00208     gui.run_main = &run_main;                                           \
00209     gui.thread_init = true;                                             \
00210     QApplication a(argc, argv);                                         \
00211     a.setQuitOnLastWindowClosed(false);                                 \
00212     ebl::gui_thread gt(argc, argv), a;                                  \
00213     gt.thread.start();                                                  \
00214     a.exec();                                                           \
00215     LocalFree(argv);                                                    \
00216     for (targc i = 0; i < argc; ++i) {                                  \
00217       if (argv[i]) delete argv[i];                                      \
00218     }                                                                   \
00219     return 0;                                                           \
00220   }                                                                     \
00221   int run_main(targc argc, targv argv)
00222 
00223 #ifdef __WINDOWS__
00224 #define NOCONSOLE_MAIN_QTHREAD WINMAIN_QTHREAD
00225 #else
00226 #define NOCONSOLE_MAIN_QTHREAD MAIN_QTHREAD
00227 #endif
00228   
00229 } // namespace ebl {
00230 
00231 #endif /* GUI_THREAD_H_ */