/**************************************************************************** Copyright (C) 2002-2006 Gilles Debunne (Gilles.Debunne@imag.fr) This file is part of the QGLViewer library. Version 2.2.4-1, released on December 12, 2006. http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer libQGLViewer is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. libQGLViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with libQGLViewer; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *****************************************************************************/ #include "keyboardAndMouse.h" #if QT_VERSION < 0x040000 # include #else # include #endif #include #include #include using namespace std; // Draws a spiral void Viewer::draw() { const float nbSteps = 80.0; glBegin(GL_QUAD_STRIP); for (float i=0; istate() & Qt::KeyButtonMask); #else const Qt::KeyboardModifiers modifiers = e->modifiers(); #endif // A simple switch on e->key() is not sufficient if we want to take state key into account. // With a switch, it would have been impossible to separate 'F' from 'CTRL+F'. // That's why we use imbricated if...else and a "handled" boolean. bool handled = false; if ((e->key()==Qt::Key_W) && (modifiers==Qt::NoButton)) { wireframe_ = !wireframe_; if (wireframe_) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); handled = true; updateGL(); } else if ((e->key()==Qt::Key_F) && (modifiers==Qt::NoButton)) { flatShading_ = !flatShading_; if (flatShading_) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); handled = true; updateGL(); } // ... and so on with other else/if blocks. if (!handled) QGLViewer::keyPressEvent(e); } /////////////////////////////////////////////////////////// // Define new mouse bindings // // A camera viewpoint menu binded on right button // /////////////////////////////////////////////////////////// void Viewer::mousePressEvent(QMouseEvent* e) { #if QT_VERSION < 0x040000 if ((e->button() == Qt::RightButton) && (e->state() == Qt::NoButton)) #else if ((e->button() == Qt::RightButton) && (e->modifiers() == Qt::NoButton)) #endif { #if QT_VERSION < 0x040000 QPopupMenu menu( this ); menu.insertItem("Camera positions"); menu.insertSeparator(); QMap menuMap; #else QMenu menu( this ); menu.addAction("Camera positions"); menu.addSeparator(); QMap menuMap; #endif bool atLeastOne = false; // We only test the 20 first indexes. This is a limitation. for (unsigned short i=0; i<20; ++i) if (camera()->keyFrameInterpolator(i)) { atLeastOne = true; QString text; if (camera()->keyFrameInterpolator(i)->numberOfKeyFrames() == 1) text = "Position "+QString::number(i); else text = "Path "+QString::number(i); #if QT_VERSION < 0x040000 menuMap[menu.insertItem(text)] = i; #else menuMap[menu.addAction(text)] = i; #endif } if (!atLeastOne) { #if QT_VERSION < 0x040000 menu.insertItem("No position defined"); menu.insertItem("Use to Alt+Fx to define one"); #else menu.addAction("No position defined"); menu.addAction("Use to Alt+Fx to define one"); #endif } #if QT_VERSION < 0x040000 menu.setMouseTracking(true); int select = menu.exec(e->globalPos()); if (atLeastOne && select != -1) camera()->playPath(menuMap[select]); #else QAction* action = menu.exec(e->globalPos()); if (atLeastOne && action) camera()->playPath(menuMap[action]); #endif } else QGLViewer::mousePressEvent(e); } QString Viewer::helpString() const { QString text("

K e y b o a r d A n d M o u s e

"); text += "This example illustrates the mouse and key bindings customization.

"; text += "Use setShortcut() to change standard action key bindings (display of axis, grid or fps, exit shortcut...).

"; text += "Use setMouseBinding() and setWheelBinding() to change standard action mouse bindings "; text += "(camera rotation, translation, object selection...).

"; text += "If you want to define new key or mouse actions, overload keyPressEvent() and/or "; text += "mouse(Press|Move|Release)Event() to define and bind your own new actions. "; text += "Use setKeyDescription() and setMouseBindingDescription() to add a description of your bindings in the help window.

"; text += "In this example, we defined the F and W keys and the right mouse button opens a popup menu. "; text += "See the keyboard and mouse tabs in this help window for the complete bindings description.

"; text += "By the way, exit shortcut has been binded to Ctrl+Q."; return text; }