Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

testGUI.cxx

Go to the documentation of this file.
00001 //  Simple main to test some features of the GUI interface
00002 #include <string> // make sure gets loaded first
00003 #include "control/EventLoop.h"
00004 #include "control/CompoundCommand.h"
00005 #include "control/SimpleCommand.h"
00006 #include "control/PrintControl.h"
00007 
00008 #include "geometry/Box.h"
00009 #include "geometry/Cone.h"
00010 #include "geometry/Ray.h"
00011 
00012 #include "gui/GUI.h"
00013 #include "gui/DisplayControl.h"
00014 #include "gui/ConeRep.h"
00015 
00016 #ifdef HAVE_NEW_IOSTREAMS
00017 # include <strstream>
00018 #else
00019 # include <strstream.h>
00020 #endif
00021 
00022 GUI* theGUI;   // make the theGUI available below
00023 DisplayControl* g_display;
00024 
00025 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00026 // create a Command for use in the menu below
00027 class GenerateText : public Command  {
00028 public:
00029     GenerateText(std::ostream& out):_out(out),_line(0){}
00030     void execute()
00031     {  for(int i=0; i<10; i++)
00032     _out << "line "<< ++_line << '\n';
00033     _out.flush();
00034     } ;
00035 private:
00036     std::ostream& _out;
00037     int _line;
00038 };
00039 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00040 class OpenFile : public Command {
00041 public:
00042     OpenFile(GUI* gui) :_gui(gui){}
00043     void execute()
00044     { const char* name = _gui->askForFileName("","","");
00045     if( name ) std::cout << "you chose: " << name << '\n';
00046     else       std::cout << "no choice!\n";
00047     }
00048     GUI* _gui;
00049 };
00050 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00051 class TestInput : public Command {
00052 public: TestInput(GUI* gui) :_gui(gui){}
00053         void execute()
00054         { char* test = _gui->askUser("enter a string:","(default)");
00055         _gui->inform(test);
00056         //delete test;
00057         }
00058         GUI* _gui;
00059 };
00060 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00061 class Trigger : public Command
00062 {
00063 public:
00064     Trigger(GUI* gui):_gui(gui),count(0){};
00065     void execute()
00066     {
00067         std::strstream line; line << "Event "<< ++ count << '\0';
00068         _gui->setTitle(line.str());
00069     }
00070     GUI* _gui;
00071     long count;
00072 };
00073 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00074 class CheckStatus : public Command
00075 {
00076 public:
00077     CheckStatus(LoopCommand* loop, GUI* gui):_loop(loop),_gui(gui){}
00078     void execute()
00079     {
00080         switch (_loop->status())
00081         {
00082         case LoopCommand::STOPPED:
00083             _gui->inform("Stopped normally"); break;
00084         case LoopCommand::ABORTED:
00085             _gui->inform("aborted"); break;
00086         case LoopCommand::PAUSED:
00087             _gui->inform("loop paused"); break;
00088         default:
00089             _gui->inform("unexpected termination");
00090         }
00091     }
00092 private:
00093     LoopCommand* _loop;
00094     GUI* _gui;
00095 };
00096 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00097 class Lines : public gui::DisplayRep {
00098 public:
00099     Lines( const Hep3Vector& p, const Volume& v )
00100         : m_p(p), m_v(v){
00101                 
00102         // make a key update the display
00103         CompoundCommand* cmd = 
00104             new CompoundCommand(new SimpleCommand<DisplayControl>(g_display, &DisplayControl::clear));
00105         cmd->append(new SimpleCommand<DisplayControl>(g_display, &DisplayControl::update));
00106         Menu::instance()->register_key('c',cmd);
00107     }
00108     
00109     void draw_a_line(const Vector& n){
00110         Point p( m_p.x(),m_p.y(), m_p.z() );
00111         
00112         if( !m_v.inside(p) ){
00113             // starting outside
00114             setColor("green");
00115             move_to(p);
00116             double d = m_v.distanceToEnter(Ray(p, n), 100);
00117             if (d == FLT_MAX) {
00118                 // missed, apparently
00119                 d = 100;
00120                 line_to(p+d*n);
00121                 setColor("black");
00122                 return;
00123             }else {
00124                 // make a line to instersection
00125                 Point p2 = p+d*n;
00126                 line_to(p2);
00127                 p = p2;
00128             }
00129         }
00130         // starting, or now inside - move to exit surface
00131         setColor("red");
00132         move_to(p);  // if not already
00133         double d = m_v.distanceToLeave(Ray(p, n), 100);
00134         Point p2 = p+d*n;
00135         line_to(p2);
00136         p = p2;
00137         
00138         // now outside -- finish up
00139         setColor("green");
00140         move_to(p);
00141         line_to(p+100*n);
00142         setColor("black");
00143     }
00144     
00145     
00146     void update() {
00147         Vector n(0,0,1);
00148         int count = 30;
00149         double delta = 2*M_PI/count;
00150         
00151         for(int i =0; i<count; ++i, n.rotateY(delta)){
00152             draw_a_line(n);
00153         }
00154         draw_a_line(Vector(0,1,0));
00155         draw_a_line(Vector(0,-1,0));
00156         
00157     }
00158     
00159 private:
00160     const Hep3Vector& m_p;
00161     const Volume& m_v;
00162 };
00163 
00164 
00165 //----------------------------------------------------------------------------
00166 //              main
00167 
00168 int main(int argc, char* argv[])
00169 {
00170     
00171     // create an instance of a GUI subclass, using the "factory"
00172     theGUI = GUI::createGUI("testGUI","testGUI");
00173     // get the Menu object
00174     Menu& menu = *Menu::instance();
00175     PrintControl printer(theGUI->textWindow("test"));
00176     
00177     
00178     
00179     // create display controller
00180     DisplayControl display(menu, theGUI->graphicsWindow());
00181     
00182     g_display = & display; // global
00183     
00184     
00185     // test a key-stroke command
00186     menu.register_key('q', new SimpleCommand<GUI>(theGUI, &GUI::quit));
00187     
00188     // create text and display objects
00189     std::ostream& cout = *theGUI->textWindow("Text Window for this test");
00190     
00191     
00192     // create object to test
00193     //Box box(50,50,50); 
00194     
00195     Cone cone(50, 5, 10, 30, 40);
00196     
00197     const Volume& vol =  cone;
00198     
00199     display.add(new ConeRep(cone), "object to test" );
00200     display.add(new Lines(display.reference_point(), vol), "lines to object");
00201     
00202     // add a top-level menu item
00203     SubMenu& top = menu.sub_menu("User menu");
00204     // put a menu under that
00205     SubMenu& sub = top.sub_menu("user sub-menu");
00206     // finally, add a command
00207     sub.add_button("trigger", new Trigger(theGUI)); 
00208     
00209     // test the filemenu
00210     menu.file_menu().add_button("Open", new OpenFile(theGUI));
00211     
00212     // create an event loop, which will register various commands to manage it
00213     EventLoop loop(new Trigger(theGUI));
00214     
00215     // finally, run it
00216     Menu::instance()->run();
00217     return 0;
00218 }
00219 //===============================================================
00220 void WARNING(const char* s){GUI::instance()->inform(s);}
00221 void FATAL(const char* s){GUI::instance()->inform(s);}//cerr << s << endl; exit(-1);}
00222 //===============================================================
00223 #if  defined(WIN32)   
00224 extern "C" int UserMain(int argc, char *argv[]){
00225     return main(argc, argv);
00226 }
00227 #endif
00228 
00229 
00230 

Generated at Wed Nov 21 12:20:26 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000