core/vgui/impl/qt4/vgui_qt_adaptor.cxx
Go to the documentation of this file.
00001 #include "vgui_qt_adaptor.h"
00002 #include "vgui_qt_menu.h"
00003 
00004 #include <vgui/vgui_popup_params.h>
00005 #include <vcl_iostream.h>
00006 
00007 #include <QMenu>
00008 
00009 using namespace QGL;
00010 using namespace Qt;
00011 
00012 vgui_qt_adaptor::vgui_qt_adaptor(QWidget* parent)
00013 //Note: enabling overlays with "HasOverlay" causes a seg fault on Mac OS X 10.4
00014    : QGLWidget(QGLFormat(DoubleBuffer|DepthBuffer|Rgba|AlphaChannel|
00015                          AccumBuffer|StencilBuffer|NoStereoBuffers|
00016                          DirectRendering), parent),
00017      ovl_helper(0),
00018      use_overlay_helper(true),
00019      idle_request_posted_(false)
00020 {
00021    this->setMouseTracking(true);
00022    this->setFocusPolicy(Qt::StrongFocus);
00023    this->setAutoBufferSwap(false);
00024 
00025    // Check the requested GL format
00026    QGLFormat format = this-> format ();
00027 
00028    if (!format. doubleBuffer ())
00029      vcl_cerr << "vgui_qt_adaptor: got single buffer\n";
00030 
00031    if (!format. depth ())
00032      vcl_cerr << "vgui_qt_adaptor: no depth buffer\n";
00033 
00034    if (!format. rgba ())
00035      vcl_cerr << "vgui_qt_adaptor: index color\n";
00036 
00037    if (!format. directRendering ())
00038      vcl_cerr << "vgui_qt_adaptor: no direct rendering\n";
00039 
00040    // Set up idle time
00041    idle_timer_ = new QTimer (this);
00042    idle_timer_-> start (0);
00043    connect (idle_timer_, SIGNAL(timeout()), this, SLOT(idle_slot()));
00044 }
00045 
00046 //------------------------------------------------------------------------------
00047 vgui_qt_adaptor::~vgui_qt_adaptor()
00048 {
00049    if (ovl_helper)
00050      delete ovl_helper;
00051    ovl_helper = 0;
00052    dispatch_to_tableau(vgui_DESTROY);
00053    
00054    for(vcl_map<int, vgui_qt_internal_timer*>::iterator it = timers_.begin();
00055        it != timers_.end(); ++it){
00056       delete it->second;
00057    }
00058 }
00059 
00060 //------------------------------------------------------------------------------
00061 bool
00062 vgui_qt_adaptor::dispatch_to_tableau (const vgui_event &event)
00063 {
00064   if (ovl_helper)
00065     return ovl_helper->dispatch(event);
00066   else
00067     return vgui_adaptor::dispatch_to_tableau(event);
00068 }
00069 
00070 //------------------------------------------------------------------------------
00071 void vgui_qt_adaptor::paintGL()
00072 {
00073    if (this->doubleBuffer())
00074      glDrawBuffer(GL_BACK);
00075    else
00076      glDrawBuffer(GL_FRONT);
00077    dispatch_to_tableau(vgui_DRAW);
00078    swap_buffers ();
00079 }
00080 
00081 void vgui_qt_adaptor::post_overlay_redraw()
00082 {
00083    if (use_overlay_helper)
00084    {
00085      if (!ovl_helper)
00086        ovl_helper = new vgui_overlay_helper(this);
00087      ovl_helper->post_overlay_redraw();
00088    }
00089    else
00090    {
00091      updateOverlayGL();
00092    }
00093 }
00094 
00095 void vgui_qt_adaptor::idle_slot ()
00096 {
00097    if (idle_request_posted_)
00098      idle_request_posted_ =  dispatch_to_tableau( vgui_event( vgui_IDLE ) );
00099 }
00100 
00101 //------------------------------------------------------------------------------
00102 void vgui_qt_adaptor::post_idle_request()
00103 {
00104    idle_request_posted_ = true;
00105 }
00106 
00107 //------------------------------------------------------------------------------
00108 void vgui_qt_adaptor::post_timer(float timeout, int name)
00109 {
00110    vcl_map<int, vgui_qt_internal_timer*>::iterator it = timers_.find( name );
00111    if( it == timers_.end() )  
00112      timers_[name] = new vgui_qt_internal_timer(this,name);
00113    QTimer::singleShot(static_cast<int>(timeout), timers_[name], SLOT(activate()));
00114 }
00115 
00116 //------------------------------------------------------------------------------
00117 void vgui_qt_adaptor::kill_timer(int name)
00118 {
00119    vcl_map<int, vgui_qt_internal_timer*>::iterator it = timers_.find( name );
00120    if( it != timers_.end() ){
00121       delete it->second;
00122       timers_.erase(it);
00123    }
00124 }
00125 
00126 //------------------------------------------------------------------------------
00127 void vgui_qt_adaptor::paintOverlayGL()
00128 {
00129    if (this->doubleBuffer())
00130      glDrawBuffer(GL_BACK);
00131    else
00132      glDrawBuffer(GL_FRONT);
00133    dispatch_to_tableau(vgui_DRAW_OVERLAY);
00134    swap_buffers ();
00135 }
00136 
00137 //------------------------------------------------------------------------------
00138 void vgui_qt_adaptor::resizeGL(int w, int h)
00139 {
00140    make_current();
00141    vgui_adaptor_mixin::width  = QGLWidget::width();
00142    vgui_adaptor_mixin::height = QGLWidget::height();
00143 
00144    dispatch_to_tableau(vgui_RESHAPE);
00145 }
00146 
00147 //------------------------------------------------------------------------------
00148 void vgui_qt_adaptor::mouseMoveEvent   (QMouseEvent* e)
00149 {
00150    vgui_event ev = translate(e);
00151    ev.type = vgui_MOTION;
00152    dispatch_to_tableau(ev);
00153 }
00154 
00155 //------------------------------------------------------------------------------
00156 void vgui_qt_adaptor::mousePressEvent  (QMouseEvent* e)
00157 {
00158    vgui_event ev = translate(e);
00159    ev.type = vgui_BUTTON_DOWN;
00160 
00161    // popup
00162    if (ev.button   == this->popup_button &&
00163        ev.modifier == this->popup_modifier)
00164    {
00165       vgui_popup_params params;
00166       params.x = ev.wx;
00167       params.y = ev.wy;
00168       QMenu* pm = new vgui_qt_menu(this->get_total_popup(params),this);
00169 
00170       pm->popup(QWidget::mapToGlobal(QPoint(e->x(), e->y())));
00171 
00172       return;
00173    }
00174 
00175    dispatch_to_tableau(ev);
00176 }
00177 
00178 //------------------------------------------------------------------------------
00179 void vgui_qt_adaptor::mouseReleaseEvent(QMouseEvent* e)
00180 {
00181    vgui_event ev = translate(e);
00182    ev.type = vgui_BUTTON_UP;
00183    dispatch_to_tableau(ev);
00184 }
00185 
00186 //------------------------------------------------------------------------------
00187 void vgui_qt_adaptor::keyPressEvent   (QKeyEvent* e)
00188 {
00189    vgui_event ev = translate(e);
00190    ev.type = vgui_KEY_PRESS;
00191    dispatch_to_tableau(ev);
00192 }
00193 
00194 void vgui_qt_adaptor::keyReleaseEvent (QKeyEvent* e)
00195 {
00196    vgui_event ev = translate(e);
00197    ev.type = vgui_KEY_RELEASE;
00198    dispatch_to_tableau(ev);
00199 }
00200 
00201 //------------------------------------------------------------------------------
00202 void vgui_qt_adaptor::wheelEvent      (QWheelEvent* e)
00203 {
00204    vgui_event ev = translate(e);
00205 
00206    if (e->delta() > 0) ev.type = vgui_WHEEL_UP;
00207    else                ev.type = vgui_WHEEL_DOWN;
00208 
00209    dispatch_to_tableau(ev);
00210 }
00211 
00212 //------------------------------------------------------------------------------
00213 void vgui_qt_adaptor::windowActivationChange (bool oldActive)
00214 {
00215    vgui_event ev;
00216 
00217    if (!oldActive)
00218     ev.type = vgui_FOCUSGAINED;
00219    else
00220     ev.type = vgui_FOCUSLOST;
00221    dispatch_to_tableau(ev);
00222 }
00223 
00224 //------------------------------------------------------------------------------
00225 void vgui_qt_internal_timer::activate() 
00226 { 
00227    if(adaptor){
00228       vgui_event e(vgui_TIMER);
00229       e.timer_id = id;
00230       adaptor->dispatch_to_tableau(e);
00231    }
00232 }
00233 
00234 //------------------------------------------------------------------------------
00235 vgui_event vgui_qt_adaptor::translate(QMouseEvent* e)
00236 {
00237    vgui_event ev;
00238    ev. modifier = translate(e->modifiers());
00239    ev. button = vgui_BUTTON_NULL;
00240    if (e-> button () & Qt::LeftButton) ev. button = vgui_LEFT;
00241    if (e-> button () & Qt::RightButton) ev. button = vgui_RIGHT;
00242    if (e-> button () & Qt::MidButton) ev. button = vgui_MIDDLE;
00243 
00244    ev. wx = e-> x ();
00245    ev. wy = QGLWidget::height() - e-> y ();
00246    return ev;
00247 }
00248 
00249 vgui_event vgui_qt_adaptor::translate(QKeyEvent* e)
00250 {
00251    vgui_event ev;
00252    ev. modifier = translate(e->modifiers());
00253    ev.set_key(translate(Qt::Key(e->key())));
00254 
00255    return ev;
00256 }
00257 
00258 vgui_event vgui_qt_adaptor::translate(QWheelEvent* e)
00259 {
00260    vgui_event ev;
00261    ev. modifier = translate(e->modifiers());
00262    ev. wx = e-> x ();
00263    ev. wy = QGLWidget::height () - e-> y ();
00264    return ev;
00265 }
00266 
00267 vgui_key vgui_qt_adaptor::translate(Qt::Key k)
00268 {
00269   switch (k)
00270   {
00271     case Qt::Key_Escape:
00272       return vgui_ESCAPE;
00273     case Qt::Key_Tab:
00274       return vgui_TAB;
00275     case Qt::Key_Return:
00276       return vgui_RETURN;
00277     case Qt::Key_Enter:
00278       return vgui_NEWLINE;
00279     case Qt::Key_F1:
00280       return vgui_F1;
00281     case Qt::Key_F2:
00282       return vgui_F2;
00283     case Qt::Key_F3:
00284       return vgui_F3;
00285     case Qt::Key_F4:
00286       return vgui_F4;
00287     case Qt::Key_F5:
00288       return vgui_F5;
00289     case Qt::Key_F6:
00290       return vgui_F6;
00291     case Qt::Key_F7:
00292       return vgui_F7;
00293     case Qt::Key_F8:
00294       return vgui_F8;
00295     case Qt::Key_F9:
00296       return vgui_F9;
00297     case Qt::Key_F10:
00298       return vgui_F10;
00299     case Qt::Key_F11:
00300       return vgui_F11;
00301     case Qt::Key_F12:
00302       return vgui_F12;
00303     case Qt::Key_Left:
00304       return vgui_CURSOR_LEFT;
00305     case Qt::Key_Up:
00306       return vgui_CURSOR_UP;
00307     case Qt::Key_Right:
00308       return vgui_CURSOR_RIGHT;
00309     case Qt::Key_Down:
00310       return vgui_CURSOR_DOWN;
00311     case Qt::Key_PageUp:
00312       return vgui_PAGE_UP;
00313     case Qt::Key_PageDown:
00314       return vgui_PAGE_DOWN;
00315     case Qt::Key_Home:
00316       return vgui_HOME;
00317     case Qt::Key_End:
00318       return vgui_END;
00319     case Qt::Key_Delete:
00320       return vgui_DELETE;
00321     case Qt::Key_Insert:
00322       return vgui_INSERT;
00323     default:
00324       return vgui_key (static_cast<unsigned char>(k));
00325   }
00326 
00327   return vgui_KEY_NULL;
00328 }
00329 
00330 vgui_modifier vgui_qt_adaptor::translate(Qt::KeyboardModifiers m)
00331 {
00332   int mod = vgui_MODIFIER_NULL;
00333   if(m & Qt::CTRL)   mod |= vgui_CTRL;
00334   if(m & Qt::SHIFT)  mod |= vgui_SHIFT;
00335   if(m & Qt::META)   mod |= vgui_META;
00336   if(m & Qt::ALT)    mod |= vgui_ALT;
00337 
00338   return vgui_modifier(mod);
00339 }
00340 
00341 Qt::Key vgui_qt_adaptor::translate(vgui_key k)
00342 {
00343   switch (k)
00344   {
00345     case vgui_ESCAPE:
00346       return Qt::Key_Escape;
00347     case vgui_TAB:
00348       return Qt::Key_Tab;
00349     case vgui_RETURN:
00350       return Qt::Key_Return;
00351     case vgui_NEWLINE:
00352       return Qt::Key_Enter;
00353     case vgui_F1:
00354       return Qt::Key_F1;
00355     case vgui_F2:
00356       return Qt::Key_F2;
00357     case vgui_F3:
00358       return Qt::Key_F3;
00359     case vgui_F4:
00360       return Qt::Key_F4;
00361     case vgui_F5:
00362       return Qt::Key_F5;
00363     case vgui_F6:
00364       return Qt::Key_F6;
00365     case vgui_F7:
00366       return Qt::Key_F7;
00367     case vgui_F8:
00368       return Qt::Key_F8;
00369     case vgui_F9:
00370       return Qt::Key_F9;
00371     case vgui_F10:
00372       return Qt::Key_F10;
00373     case vgui_F11:
00374       return Qt::Key_F11;
00375     case vgui_F12:
00376       return Qt::Key_F12;
00377     case vgui_CURSOR_LEFT:
00378       return Qt::Key_Left;
00379     case vgui_CURSOR_UP:
00380       return Qt::Key_Up;
00381     case vgui_CURSOR_RIGHT:
00382       return Qt::Key_Right;
00383     case vgui_CURSOR_DOWN:
00384       return Qt::Key_Down;
00385     case vgui_PAGE_UP:
00386       return Qt::Key_PageUp;
00387     case vgui_PAGE_DOWN:
00388       return Qt::Key_PageDown;
00389     case vgui_HOME:
00390       return Qt::Key_Home;
00391     case vgui_END:
00392       return Qt::Key_End;
00393     case vgui_DELETE:
00394       return Qt::Key_Delete;
00395     case vgui_INSERT:
00396       return Qt::Key_Insert;
00397     default:
00398       return Qt::Key(k);
00399   }
00400 
00401   return Qt::Key_unknown;
00402 }
00403 
00404 Qt::KeyboardModifiers vgui_qt_adaptor::translate(vgui_modifier m)
00405 {
00406   Qt::KeyboardModifiers mod = Qt::NoModifier;
00407   if(m & vgui_CTRL)   mod |= Qt::ControlModifier;
00408   if(m & vgui_SHIFT)  mod |= Qt::ShiftModifier;
00409   if(m & vgui_META)   mod |= Qt::MetaModifier;
00410   if(m & vgui_ALT)    mod |= Qt::AltModifier;
00411 
00412   return mod;
00413 }