Salome HOME
Copyright update 2022
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewPort.cxx
old mode 100755 (executable)
new mode 100644 (file)
index a975fe0..9316db5
@@ -1,25 +1,26 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library 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
-//  Lesser General Public License for more details.
+// This library 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
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-#if !defined WNT
+
+#if !defined WIN32
 #define QT_CLEAN_NAMESPACE         /* avoid definition of INT32 and INT8 */
 #endif
 
@@ -39,7 +40,7 @@
 
 #include <stdlib.h>
 
-#if !defined WNT
+#if !defined WIN32 && !defined __APPLE__
 #include <QX11Info>
 #include <GL/glx.h>
 #include <X11/Xlib.h>
 #include <X11/Xmu/StdCmap.h>
 #undef QT_CLEAN_NAMESPACE
 #include <Xw_Window.hxx>
-#include <Graphic3d_GraphicDevice.hxx>
 
 struct CMapEntry
 {
-       CMapEntry();
-       ~CMapEntry();
-       Colormap          cmap;
-       bool              alloc;
-       XStandardColormap scmap;
+  CMapEntry();
+  ~CMapEntry();
+  Colormap          cmap;
+  bool              alloc;
+  XStandardColormap scmap;
 };
 
 /*!
@@ -64,9 +64,9 @@ struct CMapEntry
 */
 CMapEntry::CMapEntry()
 {
-       cmap = 0;
-       alloc = false;
-       scmap.colormap = 0;
+  cmap = 0;
+  alloc = false;
+  scmap.colormap = 0;
 }
 
 /*!
@@ -74,8 +74,8 @@ CMapEntry::CMapEntry()
 */
 CMapEntry::~CMapEntry()
 {
-       if ( alloc )
-         XFreeColormap( QX11Info::display(), cmap );
+  if ( alloc )
+    XFreeColormap( QX11Info::display(), cmap );
 }
 
 static QMultiHash<int,CMapEntry> *cmap_dict = 0;
@@ -83,13 +83,13 @@ static bool mesa_gl = false;
 
 static void cleanup_cmaps()
 {
-       if ( !cmap_dict )
-               return;
-       //while (!cmap_dict->isEmpty())
-       //  cmap_dict->erase(cmap_dict->begin());
-       cmap_dict->clear();
-       delete cmap_dict;
-       cmap_dict = 0;
+  if ( !cmap_dict )
+    return;
+  //while (!cmap_dict->isEmpty())
+  //  cmap_dict->erase(cmap_dict->begin());
+  cmap_dict->clear();
+  delete cmap_dict;
+  cmap_dict = 0;
 }
 
 static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
@@ -115,12 +115,12 @@ static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
   //cout << "Choosing cmap for vID = " << vi->visualid << endl;
   //#endif
 
-  if ( vi->visualid == XVisualIDFromVisual( (Visual*)QX11Info::appVisual() ) )
+  if ( vi->visualid == XVisualIDFromVisual( XDefaultVisual( QX11Info::display(), -1 ) ) )
   {
 #ifdef DEBUG
-    cout << "Using x11AppColormap" << endl;
+//    cout << "Using XDefaultColormap" << endl;
 #endif
-    return QX11Info::appColormap();
+    return XDefaultColormap( QX11Info::display(), -1 );
   }
 
   if ( mesa_gl )
@@ -130,17 +130,17 @@ static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
     {
       if ( XGetRGBColormaps( dpy, RootWindow( dpy, vi->screen ), &c, &n, hp_cmaps ) )
       {
-       i = 0;
-       while ( i < n && x.cmap == 0 )
-       {
-         if ( c[i].visualid == vi->visual->visualid )
-         {
-           x.cmap = c[i].colormap;
-           x.scmap = c[i];
-         }
-         i++;
-       }
-       XFree( (char*)c );
+        i = 0;
+        while ( i < n && x.cmap == 0 )
+        {
+          if ( c[i].visualid == vi->visual->visualid )
+          {
+            x.cmap = c[i].colormap;
+            x.scmap = c[i];
+          }
+          i++;
+        }
+        XFree( (char*)c );
       }
     }
   }
@@ -151,17 +151,17 @@ static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
     {
       if ( XGetRGBColormaps( dpy, RootWindow( dpy, vi->screen ), &c, &n, XA_RGB_DEFAULT_MAP ) )
       {
-       i = 0;
-       while ( i < n && x.cmap == 0 )
-       {
-         if ( c[i].visualid == vi->visualid )
-         {
-           x.cmap = c[i].colormap;
-           x.scmap = c[i];
-         }
-         i++;
-       }
-       XFree( (char *)c );
+        i = 0;
+        while ( i < n && x.cmap == 0 )
+        {
+          if ( c[i].visualid == vi->visualid )
+          {
+            x.cmap = c[i].colormap;
+            x.scmap = c[i];
+          }
+          i++;
+        }
+        XFree( (char *)c );
       }
     }
   }
@@ -185,7 +185,7 @@ static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
 OCCViewer_ViewPort::OCCViewer_ViewPort( QWidget* parent )
 : QWidget( parent )
 {
-       initialize();
+  initialize();
 }
 
 /*!
@@ -193,7 +193,7 @@ OCCViewer_ViewPort::OCCViewer_ViewPort( QWidget* parent )
 */
 OCCViewer_ViewPort::~OCCViewer_ViewPort()
 {
-       cleanup();
+  cleanup();
 }
 
 /*!
@@ -225,98 +225,99 @@ void OCCViewer_ViewPort::cleanup()
 */
 void OCCViewer_ViewPort::selectVisualId()
 {
-#if !defined WNT
-       XVisualInfo* pVisualInfo;
-       if ( QX11Info::display() )
-       {
-               /* Initialization with the default VisualID */
-               Visual *v = DefaultVisual( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
-               int visualID = XVisualIDFromVisual( v );
-
-               /*  Here we use the settings from Optimizer_ViewInfo::TxglCreateWindow() */
-               int visualAttr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 1, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
-                                                        GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None };
-
-               pVisualInfo = ::glXChooseVisual( QX11Info::display(), DefaultScreen( QX11Info::display() ), visualAttr );
-
-               if ( isVisible() )
-                 hide();
-
-               XSetWindowAttributes a;
-
-               a.colormap = choose_cmap( QX11Info::display(), pVisualInfo );       /* find best colormap */
-               a.background_pixel = QColormap::instance().pixel( backgroundColor() );
-               a.border_pixel = QColormap::instance().pixel( Qt::black );
-               Window p = RootWindow( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
-               if ( parentWidget() )
-                 p = parentWidget()->winId();
-
-               Window w;
-       /*        if ( type == Type2D )  // creating simple X window for 2d
-        {
-            unsigned long xbackground =
-                BlackPixel( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
-            unsigned long xforeground =
-                WhitePixel( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
+#if !defined WIN32 && !defined __APPLE__
+  XVisualInfo* pVisualInfo;
+  if ( QX11Info::display() )
+  {
+    /* Initialization with the default VisualID */
+    Visual *v = DefaultVisual( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
+    /*int visualID = */XVisualIDFromVisual( v );
+    
+    /*  Here we use the settings from Optimizer_ViewInfo::TxglCreateWindow() */
+    int visualAttr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 1, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
+                          GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None };
+    
+    pVisualInfo = ::glXChooseVisual( QX11Info::display(), DefaultScreen( QX11Info::display() ), visualAttr );
+    
+    if ( isVisible() )
+      hide();
+    
+    XSetWindowAttributes a;
+    
+    a.colormap = choose_cmap( QX11Info::display(), pVisualInfo );       /* find best colormap */
+    a.background_pixel = QColormap::instance().pixel( backgroundColor() );
+    a.border_pixel = QColormap::instance().pixel( Qt::black );
+    Window p = RootWindow( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
+    if ( parentWidget() )
+      p = parentWidget()->winId();
+    
+    Window w;
+    /*
+    if ( type == Type2D )  // creating simple X window for 2d
+    {
+      unsigned long xbackground =
+          BlackPixel( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
+      unsigned long xforeground =
+          WhitePixel( QX11Info::display(), DefaultScreen( QX11Info::display() ) );
 
-            w = XCreateSimpleWindow ( QX11Info::display(), p, x(), y(), width(),
-                                      height(), 0, xforeground, xbackground );
-        }
-        else if ( type == Type3D )
-        {
-                   w = XCreateWindow( QX11Info::display(), p,  x(), y(), width(), height(),
-                               0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
-                               CWBackPixel | CWBorderPixel | CWColormap, &a );
-               }
-               else
-               return;*/
-       w = XCreateWindow( QX11Info::display(), p,  x(), y(), width(), height(),
-                          0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
-                          CWBackPixel | CWBorderPixel | CWColormap, &a );
-       
-               Window *cmw;
-               Window *cmwret;
-               int count;
-               if ( XGetWMColormapWindows( QX11Info::display(), topLevelWidget()->winId(), &cmwret, &count ) )
-               {
-                       cmw = new Window[count+1];
-                       memcpy( (char*)cmw, (char*)cmwret, sizeof(Window) * count );
-                       XFree( (char*)cmwret );
-            int i;
-
-                       for ( i = 0; i < count; i++ )
-                       {
-                               if ( cmw[i] == winId() ) /* replace old window */
-                               {
-                                       cmw[i] = w;
-                                       break;
-                               }
-                       }
-
-                       if ( i >= count )                        /* append new window */
-                               cmw[count++] = w;
-               }
-               else
-               {
-                       count = 1;
-                       cmw = new Window[count];
-                       cmw[0] = w;
-               }
-
-               /* Creating new window (with good VisualID) for this widget */
-               create(w);
-               XSetWMColormapWindows( QX11Info::display(), topLevelWidget()->winId(), cmw, count );
-               delete[] cmw;
-
-               if ( isVisible() )
-            show();
-
-               if ( pVisualInfo )
+      w = XCreateSimpleWindow ( QX11Info::display(), p, x(), y(), width(),
+                                height(), 0, xforeground, xbackground );
+    }
+    else if ( type == Type3D )
+    {
+      w = XCreateWindow( QX11Info::display(), p,  x(), y(), width(), height(),
+                          0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
+                          CWBackPixel | CWBorderPixel | CWColormap, &a );
+    }
+    else
+      return;
+    */
+    w = XCreateWindow( QX11Info::display(), p,  x(), y(), width(), height(),
+                      0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
+                      CWBackPixel | CWBorderPixel | CWColormap, &a );
+  
+    Window *cmw;
+    Window *cmwret;
+    int count;
+    if ( XGetWMColormapWindows( QX11Info::display(), topLevelWidget()->winId(), &cmwret, &count ) )
+    {
+      cmw = new Window[count+1];
+      memcpy( (char*)cmw, (char*)cmwret, sizeof(Window) * count );
+      XFree( (char*)cmwret );
+      int i;
+
+      for ( i = 0; i < count; i++ )
+      {
+        if ( cmw[i] == winId() ) /* replace old window */
         {
-                       XFree( (char *)pVisualInfo );
-               }
-               XFlush( QX11Info::display() );
-       }
+          cmw[i] = w;
+          break;
+        }
+      }
+
+      if ( i >= count )                        /* append new window */
+        cmw[count++] = w;
+    }
+    else
+    {
+      count = 1;
+      cmw = new Window[count];
+      cmw[0] = w;
+    }
+
+    /* Creating new window (with good VisualID) for this widget */
+    create(w);
+    XSetWMColormapWindows( QX11Info::display(), topLevelWidget()->winId(), cmw, count );
+    delete[] cmw;
+
+    if ( isVisible() )
+      show();
+
+    if ( pVisualInfo )
+      XFree( (char *)pVisualInfo );
+
+    XFlush( QX11Info::display() );
+  }
 #endif
 }
 
@@ -325,11 +326,11 @@ void OCCViewer_ViewPort::selectVisualId()
 */
 void OCCViewer_ViewPort::setBackgroundColor( const QColor& color )
 {
-    QPalette pal = palette();
-    pal.setColor( QPalette::Background, color );
-    setPalette( pal );
-    repaint();
-    emit vpChangeBGColor( color );
+  QPalette pal = palette();
+  pal.setColor( QPalette::Background, color );
+  setPalette( pal );
+  repaint();
+  emit vpChangeBGColor( color );
 }
 
 /*!
@@ -337,7 +338,7 @@ void OCCViewer_ViewPort::setBackgroundColor( const QColor& color )
 */
 QColor OCCViewer_ViewPort::backgroundColor() const
 {
-    return palette().color( QPalette::Active, QPalette::Background );
+  return palette().color( QPalette::Active, QPalette::Background );
 }
 
 /*!
@@ -345,7 +346,7 @@ QColor OCCViewer_ViewPort::backgroundColor() const
 */
 bool OCCViewer_ViewPort::isSketchingEnabled() const
 {
-    return myEnableSketching;
+  return myEnableSketching;
 }
 
 /*!
@@ -353,7 +354,7 @@ bool OCCViewer_ViewPort::isSketchingEnabled() const
 */
 void OCCViewer_ViewPort::setSketchingEnabled( bool enable )
 {
-       myEnableSketching = enable;
+  myEnableSketching = enable;
 }
 
 /*!
@@ -362,7 +363,7 @@ void OCCViewer_ViewPort::setSketchingEnabled( bool enable )
 */
 bool OCCViewer_ViewPort::isTransformEnabled() const
 {
-    return myEnableTransform;
+  return myEnableTransform;
 }
 
 /*!
@@ -370,7 +371,7 @@ bool OCCViewer_ViewPort::isTransformEnabled() const
 */
 void OCCViewer_ViewPort::setTransformEnabled( bool enable )
 {
-       myEnableTransform = enable;
+  myEnableTransform = enable;
 }
 
 /*!
@@ -386,7 +387,7 @@ void OCCViewer_ViewPort::mousePressEvent( QMouseEvent *e )
 */
 void OCCViewer_ViewPort::mouseMoveEvent( QMouseEvent* e )
 {
-    emit vpMouseEvent( e );
+  emit vpMouseEvent( e );
 }
 
 /*!
@@ -394,7 +395,7 @@ void OCCViewer_ViewPort::mouseMoveEvent( QMouseEvent* e )
 */
 void OCCViewer_ViewPort::mouseReleaseEvent( QMouseEvent *e )
 {
-    emit vpMouseEvent( e );
+  emit vpMouseEvent( e );
 }
 
 /*!
@@ -402,7 +403,7 @@ void OCCViewer_ViewPort::mouseReleaseEvent( QMouseEvent *e )
 */
 void OCCViewer_ViewPort::mouseDoubleClickEvent( QMouseEvent *e )
 {
-    emit vpMouseEvent( e );
+  emit vpMouseEvent( e );
 }
 
 /*!
@@ -410,7 +411,7 @@ void OCCViewer_ViewPort::mouseDoubleClickEvent( QMouseEvent *e )
 */
 void OCCViewer_ViewPort::keyPressEvent( QKeyEvent *e )
 {
-    emit vpKeyEvent( e );
+  emit vpKeyEvent( e );
 }
 
 /*!
@@ -418,7 +419,7 @@ void OCCViewer_ViewPort::keyPressEvent( QKeyEvent *e )
 */
 void OCCViewer_ViewPort::keyReleaseEvent( QKeyEvent *e )
 {
-    emit vpKeyEvent( e );
+  emit vpKeyEvent( e );
 }
 
 /*!
@@ -426,12 +427,12 @@ void OCCViewer_ViewPort::keyReleaseEvent( QKeyEvent *e )
 */
 void OCCViewer_ViewPort::paintEvent( QPaintEvent* )
 {
-       if ( myPaintersRedrawing )
-       {
-               QPainter p( this );
-               emit vpDrawExternal( &p );
-               myPaintersRedrawing = false;
-       }
+  if ( myPaintersRedrawing )
+  {
+    QPainter p( this );
+    emit vpDrawExternal( &p );
+    myPaintersRedrawing = false;
+  }
 }
 
 /*!
@@ -439,8 +440,8 @@ void OCCViewer_ViewPort::paintEvent( QPaintEvent* )
 */
 void OCCViewer_ViewPort::redrawPainters()
 {
-       myPaintersRedrawing = true;
-       repaint();
+  myPaintersRedrawing = true;
+  repaint();
 }
 
 /*!
@@ -451,41 +452,29 @@ void OCCViewer_ViewPort::onUpdate()
 }
 
 /*!
-    Creates the popup. [ virtual protected ]
+  Get paint engine for the OpenGL viewer. [ virtual public ]
 */
-/*void OCCViewer_ViewPort::onCreatePopup( QPopupMenu* popup )
+QPaintEngine* OCCViewer_ViewPort::paintEngine() const
 {
-       if ( popup )
-       {
-               QtxAction* a = new QtxAction( "", tr( "MEN_VP_CHANGEBGR" ), 0, this );
-        a->setStatusTip( tr( "PRP_VP_CHANGEBGR" ) );
-               connect( a, SIGNAL( activated() ), SLOT( onChangeBgColor()));
-               myPopupActions.append( a );
-               a->addTo( popup );
-       }
-}*/
+  return 0;
+}
 
 /*!
-    Destroys the popup. [ virtual protected ]
+  Performs synchronization of view parameters with the specified view.
+  Returns \c true if synchronization is done successfully or \c false otherwise.
+  Default implementation does nothing (return \c false)
 */
-/*void OCCViewer_ViewPort::onDestroyPopup( QPopupMenu* popup )
+bool OCCViewer_ViewPort::synchronize( OCCViewer_ViewPort* )
 {
-       if ( popup )
-       {
-               for ( QtxAction* a = myPopupActions.first(); a; a = myPopupActions.next() )
-                       a->removeFrom( popup );
-               //while (!myPopupActions.isEmpty())
-               //  delete myPopupActions.takeFirst();
-               myPopupActions.clear();
-       }
-}*/
+  return false;
+}
 
 /*!
     Sets the background color with color selection dialog. [ virtual protected slot ]
 */
-void OCCViewer_ViewPort::onChangeBgColor()
+void OCCViewer_ViewPort::onChangeBackground()
 {
-    QColor selColor = QColorDialog::getColor ( backgroundColor(), this );
-    if ( selColor.isValid() )
-        setBackgroundColor( selColor );
+  QColor selColor = QColorDialog::getColor ( backgroundColor(), this );
+  if ( selColor.isValid() )
+    setBackgroundColor( selColor );
 }