core/vgui/impl/qt/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 
00006 #include <qgl.h>
00007 #include <vcl_iostream.h>
00008 
00009 vgui_qt_adaptor::vgui_qt_adaptor(QWidget* parent, const char* name)
00010    : QGLWidget(QGLFormat(DoubleBuffer|DepthBuffer|Rgba|AlphaChannel|
00011                          AccumBuffer|StencilBuffer|NoStereoBuffers|
00012                          DirectRendering|HasOverlay), parent, name),
00013 //    : QGLWidget(parent, name)
00014      ovl_helper(0),
00015      use_overlay_helper(true),
00016      idle_request_posted_(false)
00017 {
00018    this->setMouseTracking(true);
00019    this->setFocusPolicy(QWidget::StrongFocus);
00020    this->setAutoBufferSwap(false);
00021 
00022    // Check the requested GL format
00023    QGLFormat format = this-> format ();
00024 
00025    if (!format. doubleBuffer ())
00026      vcl_cerr << "vgui_qt_adaptor: got single buffer\n";
00027 
00028    if (!format. depth ())
00029      vcl_cerr << "vgui_qt_adaptor: no depth buffer\n";
00030 
00031    if (!format. rgba ())
00032      vcl_cerr << "vgui_qt_adaptor: index color\n";
00033 
00034    if (!format. directRendering ())
00035      vcl_cerr << "vgui_qt_adaptor: no direct rendering\n";
00036 
00037    // Set up idle time
00038    idle_timer_ = new QTimer (this, "vgui_qt_adaptor_idle_timer");
00039    idle_timer_-> start (0);
00040    connect (idle_timer_, SIGNAL(timeout()), this, SLOT(idle_slot()));
00041 }
00042 
00043 //------------------------------------------------------------------------------
00044 vgui_qt_adaptor::~vgui_qt_adaptor()
00045 {
00046    if (ovl_helper)
00047      delete ovl_helper;
00048    ovl_helper = 0;
00049    dispatch_to_tableau(vgui_DESTROY);
00050 }
00051 
00052 //------------------------------------------------------------------------------
00053 bool
00054 vgui_qt_adaptor::dispatch_to_tableau (const vgui_event &event)
00055 {
00056   if (ovl_helper)
00057     return ovl_helper->dispatch(event);
00058   else
00059     return vgui_adaptor::dispatch_to_tableau(event);
00060 }
00061 
00062 //------------------------------------------------------------------------------
00063 void vgui_qt_adaptor::paintGL()
00064 {
00065    if (this->doubleBuffer())
00066      glDrawBuffer(GL_BACK);
00067    else
00068      glDrawBuffer(GL_FRONT);
00069    dispatch_to_tableau(vgui_DRAW);
00070    swap_buffers ();
00071 }
00072 
00073 void vgui_qt_adaptor::post_overlay_redraw()
00074 {
00075    if (use_overlay_helper)
00076    {
00077      if (!ovl_helper)
00078        ovl_helper = new vgui_overlay_helper(this);
00079      ovl_helper->post_overlay_redraw();
00080    }
00081    else
00082    {
00083      updateOverlayGL();
00084    }
00085 }
00086 
00087 void vgui_qt_adaptor::idle_slot ()
00088 {
00089    if (idle_request_posted_)
00090      idle_request_posted_ =  dispatch_to_tableau( vgui_event( vgui_IDLE ) );
00091 }
00092 
00093 //------------------------------------------------------------------------------
00094 void vgui_qt_adaptor::post_idle_request()
00095 {
00096    idle_request_posted_ = true;
00097 }
00098 
00099 //------------------------------------------------------------------------------
00100 void vgui_qt_adaptor::paintOverlayGL()
00101 {
00102    if (this->doubleBuffer())
00103      glDrawBuffer(GL_BACK);
00104    else
00105      glDrawBuffer(GL_FRONT);
00106    dispatch_to_tableau(vgui_DRAW_OVERLAY);
00107    swap_buffers ();
00108 }
00109 
00110 //------------------------------------------------------------------------------
00111 void vgui_qt_adaptor::resizeGL(int w, int h)
00112 {
00113    make_current();
00114 
00115    vgui_adaptor_mixin::width  = QGLWidget::width();
00116    vgui_adaptor_mixin::height = QGLWidget::height();
00117 
00118    dispatch_to_tableau(vgui_RESHAPE);
00119 }
00120 
00121 //------------------------------------------------------------------------------
00122 void vgui_qt_adaptor::mouseMoveEvent   (QMouseEvent* e)
00123 {
00124    vgui_event ev = translate(e);
00125    ev.type = vgui_MOTION;
00126    dispatch_to_tableau(ev);
00127 }
00128 
00129 //------------------------------------------------------------------------------
00130 void vgui_qt_adaptor::mousePressEvent  (QMouseEvent* e)
00131 {
00132    vgui_event ev = translate(e);
00133    ev.type = vgui_BUTTON_DOWN;
00134 
00135    // popup
00136    if (ev.button   == this->popup_button &&
00137        ev.modifier == this->popup_modifier)
00138    {
00139       vgui_popup_params params;
00140       params.x = ev.wx;
00141       params.y = ev.wy;
00142       QPopupMenu* pm = new vgui_qt_menu(this->get_total_popup(params));
00143 
00144       pm->popup(QWidget::mapToGlobal(QPoint(e->x(), e->y())));
00145 
00146       return;
00147    }
00148 
00149    dispatch_to_tableau(ev);
00150 }
00151 
00152 //------------------------------------------------------------------------------
00153 void vgui_qt_adaptor::mouseReleaseEvent(QMouseEvent* e)
00154 {
00155    vgui_event ev = translate(e);
00156    ev.type = vgui_BUTTON_UP;
00157    dispatch_to_tableau(ev);
00158 }
00159 
00160 //------------------------------------------------------------------------------
00161 void vgui_qt_adaptor::keyPressEvent   (QKeyEvent* e)
00162 {
00163    vgui_event ev = translate(e);
00164    ev.type = vgui_KEY_PRESS;
00165    dispatch_to_tableau(ev);
00166 }
00167 
00168 void vgui_qt_adaptor::keyReleaseEvent (QKeyEvent* e)
00169 {
00170    vgui_event ev = translate(e);
00171    ev.type = vgui_KEY_RELEASE;
00172    dispatch_to_tableau(ev);
00173 }
00174 
00175 //------------------------------------------------------------------------------
00176 void vgui_qt_adaptor::wheelEvent      (QWheelEvent* e)
00177 {
00178    vgui_event ev = translate(e);
00179 
00180    if (e->delta() > 0) ev.type = vgui_WHEEL_UP;
00181    else                ev.type = vgui_WHEEL_DOWN;
00182 
00183    dispatch_to_tableau(ev);
00184 }
00185 
00186 //------------------------------------------------------------------------------
00187 void vgui_qt_adaptor::windowActivationChange (bool oldActive)
00188 {
00189    vgui_event ev;
00190 
00191    if (!oldActive)
00192     ev.type = vgui_FOCUSGAINED;
00193    else
00194     ev.type = vgui_FOCUSLOST;
00195    dispatch_to_tableau(ev);
00196 }
00197 
00198 //------------------------------------------------------------------------------
00199 vgui_event vgui_qt_adaptor::translate(QMouseEvent* e)
00200 {
00201    vgui_event ev;
00202    ev. button = vgui_BUTTON_NULL;
00203    if (e-> button () & LeftButton) ev. button = vgui_LEFT;
00204    if (e-> button () & RightButton) ev. button = vgui_RIGHT;
00205    if (e-> button () & MidButton) ev. button = vgui_MIDDLE;
00206 
00207    int state = vgui_MODIFIER_NULL;
00208    if (e-> state () & ControlButton) state |= vgui_CTRL;
00209    if (e-> state () & AltButton) state |= vgui_ALT;
00210    if (e-> state () & ShiftButton) state |= vgui_SHIFT;
00211    ev. modifier = vgui_modifier (state);
00212    ev. wx = e-> x ();
00213    ev. wy = QGLWidget::height () - e-> y ();
00214    return ev;
00215 }
00216 
00217 vgui_event vgui_qt_adaptor::translate(QKeyEvent* e)
00218 {
00219    vgui_event ev;
00220    int state = vgui_MODIFIER_NULL;
00221    if (e-> state () & ControlButton) state |= vgui_CTRL;
00222    if (e-> state () & AltButton) state |= vgui_ALT;
00223    if (e-> state () & ShiftButton) state |= vgui_SHIFT;
00224    ev. modifier = vgui_modifier (state);
00225 
00226    vgui_key key = vgui_KEY_NULL;
00227 
00228    switch (e-> key())
00229    {
00230    case Qt::Key_Prior:
00231      key = vgui_PAGE_UP;
00232      break;
00233    case Qt::Key_Next:
00234      key = vgui_PAGE_DOWN;
00235      break;
00236    case Qt::Key_Home:
00237      key = vgui_HOME;
00238      break;
00239    case Qt::Key_End:
00240      key = vgui_END;
00241      break;
00242    case Qt::Key_Delete:
00243      key = vgui_DELETE;
00244      break;
00245    case Qt::Key_Insert:
00246      key = vgui_INSERT;
00247      break;
00248    case Qt::Key_Up:
00249      key = vgui_CURSOR_UP;
00250      break;
00251    case Qt::Key_Down:
00252      key = vgui_CURSOR_DOWN;
00253      break;
00254    case Qt::Key_Left:
00255      key = vgui_CURSOR_LEFT;
00256      break;
00257    case Qt::Key_Right:
00258      key = vgui_CURSOR_RIGHT;
00259      break;
00260    default:
00261      key = vgui_key (e-> ascii ());
00262      break;
00263    }
00264 
00265    ev. set_key (key);
00266    ev. ascii_char = key;
00267 
00268    return ev;
00269 }
00270 
00271 vgui_event vgui_qt_adaptor::translate(QWheelEvent* e)
00272 {
00273    vgui_event ev;
00274    int state = vgui_MODIFIER_NULL;
00275    if (e-> state () & ControlButton) state |= vgui_CTRL;
00276    if (e-> state () & AltButton) state |= vgui_ALT;
00277    if (e-> state () & ShiftButton) state |= vgui_SHIFT;
00278    ev. modifier = vgui_modifier (state);
00279    ev. wx = e-> x ();
00280    ev. wy = QGLWidget::height () - e-> y ();
00281    return ev;
00282 }