]> Git trees. - libqmvoc.git/commitdiff
Change some things in the way OpenGL selection is done
authorBenoît Jacob <jacob.benoit.1@gmail.com>
Tue, 25 Jul 2006 14:24:47 +0000 (14:24 +0000)
committerBenoît Jacob <jacob.benoit.1@gmail.com>
Tue, 25 Jul 2006 14:24:47 +0000 (14:24 +0000)
It now happens on button press, instead of happening on button release.
The clicked atom is highlighted in a separate color (currently some
shade of purple, tell me what you think) even if it is not selected. The
point of that is that this clicked atom is going to play a very
important role in the mouse navigation system I plan to write. Any
rotation/move will be done with respect to it. So I thought that this
special role of this atom should correspond to a visual highlighting.

M    kalzium/src/kalziumglwidget.h
M    kalzium/src/kalziumglwidget.cpp

svn path=/trunk/KDE/kdeedu/kalzium/src/kalziumglwidget.h; revision=566143

kalzium/kalziumglwidget.cpp
kalzium/kalziumglwidget.h

index 2c7d3d1c5b2bd56c23e592f5b7e1989e19d4d6a2..f4466a3230eef4d7abbd8d4020bebcb74f786a74 100644 (file)
@@ -35,6 +35,7 @@ KalziumGLWidget::KalziumGLWidget( QWidget * parent )
 {
        m_isLeftButtonPressed = false;
        m_movedSinceLeftButtonPressed = false;
+       m_clickedAtom = 0;
        m_molecule = 0;
        m_detail = 0;
        m_displayList = 0;
@@ -206,7 +207,7 @@ void KalziumGLWidget::renderScene( GLenum renderMode,
        glCallList( m_displayList );
 #endif
 
-       renderSelection();
+       renderHighlighting();
 
        if( renderMode == GL_SELECT )
        {
@@ -243,18 +244,34 @@ void KalziumGLWidget::renderBonds()
        }
 }
 
-void KalziumGLWidget::renderSelection()
+void KalziumGLWidget::renderHighlighting()
 {
-       if( ! m_selectedAtoms.count() ) return;
-
-       Color( 0.2, 0.7, 1.0, 0.7 ).applyAsMaterials();
        glEnable( GL_BLEND );
-       foreach(OpenBabel::OBAtom* atom, m_selectedAtoms)
+
+       if( m_clickedAtom )
        {
-               glLoadName( atom->GetIdx() );
-               m_sphere.draw( atom->GetVector(),
-                       0.18 + m_molStyle.getAtomRadius( atom ) );
+               Color( 0.6, 0.0, 1.0, 0.7 ).applyAsMaterials();
+               glLoadName( m_clickedAtom->GetIdx() );
+               m_sphere.draw( m_clickedAtom->GetVector(),
+                       0.18 + m_molStyle.getAtomRadius( m_clickedAtom ) );
        }
+
+       if( m_selectedAtoms.count() )
+       {
+               Color( 0.0, 0.7, 1.0, 0.7 ).applyAsMaterials();
+               glEnable( GL_BLEND );
+               foreach(OpenBabel::OBAtom* atom, m_selectedAtoms)
+               {
+                       if( atom != m_clickedAtom )
+                       {
+                               glLoadName( atom->GetIdx() );
+                               m_sphere.draw( atom->GetVector(),
+                                       0.18 + m_molStyle.getAtomRadius(
+                                               atom ) );
+                       }
+               }
+       }
+
        glDisable( GL_BLEND );
 }
 
@@ -308,7 +325,10 @@ void KalziumGLWidget::mousePressEvent( QMouseEvent * event )
                m_movedSinceLeftButtonPressed = false;
                m_lastDraggingPosition = event->pos ();
                m_initialDraggingPosition = event->pos ();
-
+               computeClickedAtom( event->pos () );
+               if( m_clickedAtom )
+                       kDebug()<<m_clickedAtom->GetIdx()<<endl;
+               updateGL();
        }
 }
 
@@ -318,21 +338,17 @@ void KalziumGLWidget::mouseReleaseEvent( QMouseEvent * event )
        {
                m_isLeftButtonPressed = false;
 
-               if( ! m_movedSinceLeftButtonPressed )
+               if( m_clickedAtom && ! m_movedSinceLeftButtonPressed )
                {
-                       OBAtom *atomUnderMouse =
-                               getAtomUnderMouse( event->pos() );
-                       if( atomUnderMouse )
+                       if( m_selectedAtoms.contains( m_clickedAtom ) )
                        {
-                               if( m_selectedAtoms.contains( atomUnderMouse ) )
-                               {
-                                       m_selectedAtoms.removeAll(
-                                               atomUnderMouse );
-                               }
-                               else m_selectedAtoms.append( atomUnderMouse );
+                               m_selectedAtoms.removeAll(
+                                       m_clickedAtom );
                        }
+                       else m_selectedAtoms.append( m_clickedAtom );
                        updateGL();
                }
+               m_clickedAtom = 0;
        }
 }
 
@@ -488,6 +504,7 @@ void KalziumGLWidget::slotSetMolecule( OpenBabel::OBMol* molecule )
        m_molecule = molecule;
        m_haveToRecompileDisplayList = true;
        m_selectedAtoms.clear();
+       m_clickedAtom = 0;
        prepareMoleculeData();
        setupObjects();
        updateGL();
@@ -569,10 +586,11 @@ void KalziumGLWidget::slotAtomsSelected( QList<OpenBabel::OBAtom*> atoms )
        updateGL();
 }
 
-OpenBabel::OBAtom * KalziumGLWidget::getAtomUnderMouse(
+void KalziumGLWidget::computeClickedAtom(
        const QPoint & mousePosition )
 {
-       if( ! m_molecule ) return 0;
+       m_clickedAtom = 0;
+       if( ! m_molecule ) return;
 
        const GLsizei selectionBufferSize = 1024;
        GLuint selectionBuffer[selectionBufferSize];
@@ -589,7 +607,6 @@ OpenBabel::OBAtom * KalziumGLWidget::getAtomUnderMouse(
                minZ = 0xffffffff,
                *ptrNames,
                numberOfNames = 0;
-       printf ("hits = %d\n", numberOfHits);
        for( i = 0; i < numberOfHits; i++ )
        {
                names = *ptr;
@@ -604,9 +621,11 @@ OpenBabel::OBAtom * KalziumGLWidget::getAtomUnderMouse(
        }
 
        for( j = 0, ptr = ptrNames; j < numberOfNames; j++, ptr++ )
-               if( *ptr ) return m_molecule->GetAtom( *ptr );
-
-       return 0;
+               if( *ptr )
+               {
+                       m_clickedAtom = m_molecule->GetAtom( *ptr );
+                       return;
+               }
 }
 
 #include "kalziumglwidget.moc"
index f5f04b93d6810b99f6123da6037d59bdb8304309..1179a10097cc0dc4acd5c095f075d063e8be6f1c 100644 (file)
@@ -57,6 +57,8 @@ class KalziumGLWidget : public QGLWidget
                QPoint m_lastDraggingPosition;
                QPoint m_initialDraggingPosition;
 
+               OpenBabel::OBAtom *m_clickedAtom;
+
                /**
                 * Stores the rotation that is applied to the model.
                 */
@@ -65,7 +67,7 @@ class KalziumGLWidget : public QGLWidget
                /**
                 * The molecule which is displayed
                 */
-               OpenBabel::OBMolm_molecule;
+               OpenBabel::OBMol *m_molecule;
 
                /**
                 * approximate radius of the molecule,
@@ -195,7 +197,7 @@ class KalziumGLWidget : public QGLWidget
                virtual void paintGL();
                virtual void renderAtoms();
                virtual void renderBonds();
-               virtual void renderSelection();
+               virtual void renderHighlighting();
                virtual void FPSCounter();
 
                /**
@@ -241,8 +243,7 @@ class KalziumGLWidget : public QGLWidget
                 */
                void setMolStyle( int style );
 
-               OpenBabel::OBAtom * getAtomUnderMouse(
-                       const QPoint & mousePosition );
+               void computeClickedAtom( const QPoint & mousePosition );
 };
 #endif // KALZIUMGLWIDGET_H