1 /***************************************************************************
2 ** Class: OCCViewer_ViewPort
3 ** Descr: Visualisation canvas of SUIT-based application
5 ** Created: UI team, 05.09.00
6 ****************************************************************************/
8 #define QT_CLEAN_NAMESPACE /* avoid definition of INT32 and INT8 */
11 #include "OCCViewer_ViewPort.h"
13 #include "SUIT_Session.h"
20 #include <qpopupmenu.h>
21 #include <qcolordialog.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
30 #include <X11/Xmu/StdCmap.h>
31 #undef QT_CLEAN_NAMESPACE
32 #include <Xw_Window.hxx>
33 #include <Graphic3d_GraphicDevice.hxx>
41 XStandardColormap scmap;
44 CMapEntry::CMapEntry()
51 CMapEntry::~CMapEntry()
54 XFreeColormap( QPaintDevice::x11AppDisplay(), cmap );
57 static QIntDict<CMapEntry> *cmap_dict = 0;
58 static bool mesa_gl = false;
60 static void cleanup_cmaps()
64 cmap_dict->setAutoDelete( true );
69 static Colormap choose_cmap( Display *dpy, XVisualInfo *vi )
73 cmap_dict = new QIntDict<CMapEntry>;
74 const char *v = glXQueryServerString( dpy, vi->screen, GLX_VERSION );
75 mesa_gl = strstr( v,"Mesa" ) != 0;
76 qAddPostRoutine( cleanup_cmaps );
79 CMapEntry *x = cmap_dict->find( (long)vi->visualid );
80 if ( x ) // found colormap for visual
89 cout << "Choosing cmap for vID = " << vi->visualid << endl;
92 if ( vi->visualid == XVisualIDFromVisual( (Visual*)QPaintDevice::x11AppVisual() ) )
95 cout << "Using x11AppColormap" << endl;
97 return QPaintDevice::x11AppColormap();
102 Atom hp_cmaps = XInternAtom( dpy, "_HP_RGB_SMOOTH_MAP_LIST", true );
103 if ( hp_cmaps && vi->visual->c_class == TrueColor && vi->depth == 8 )
105 if ( XGetRGBColormaps( dpy, RootWindow( dpy, vi->screen ), &c, &n, hp_cmaps ) )
108 while ( i < n && x->cmap == 0 )
110 if ( c[i].visualid == vi->visual->visualid )
112 x->cmap = c[i].colormap;
121 #if !defined( _OS_SOLARIS_ )
124 if ( XmuLookupStandardColormap( dpy, vi->screen, vi->visualid, vi->depth, XA_RGB_DEFAULT_MAP, false, true ) )
126 if ( XGetRGBColormaps( dpy, RootWindow( dpy, vi->screen ), &c, &n, XA_RGB_DEFAULT_MAP ) )
129 while ( i < n && x->cmap == 0 )
131 if ( c[i].visualid == vi->visualid )
133 x->cmap = c[i].colormap;
145 // no shared cmap found
146 x->cmap = XCreateColormap( dpy, RootWindow( dpy, vi->screen ), vi->visual, AllocNone );
150 cmap_dict->insert( (long)vi->visualid, x ); // associate cmap with visualid
159 OCCViewer_ViewPort::OCCViewer_ViewPort( QWidget* parent )
160 : QWidget( parent, 0, WRepaintNoErase | WResizeNoErase )
168 OCCViewer_ViewPort::~OCCViewer_ViewPort()
174 Initializes viewport. [ private ]
176 void OCCViewer_ViewPort::initialize()
178 myPopupActions.setAutoDelete( true );
179 myPaintersRedrawing = false;
180 myEnableSketching = false;
181 myEnableTransform = true;
183 setMouseTracking( true );
184 setBackgroundMode( NoBackground );
185 // set focus policy to threat QContextMenuEvent from keyboard
186 setFocusPolicy( StrongFocus );
190 Cleans up the viewport. [ private ]
192 void OCCViewer_ViewPort::cleanup()
197 Selects visual ID for OpenGL window ( X11 specific ). [ protected ]
199 void OCCViewer_ViewPort::selectVisualId()
202 XVisualInfo* pVisualInfo;
205 /* Initialization with the default VisualID */
206 Visual *v = DefaultVisual( x11Display(), DefaultScreen( x11Display() ) );
207 int visualID = XVisualIDFromVisual( v );
209 /* Here we use the settings from Optimizer_ViewInfo::TxglCreateWindow() */
210 int visualAttr[] = { GLX_RGBA, GLX_DEPTH_SIZE, 1, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
211 GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None };
213 pVisualInfo = ::glXChooseVisual( x11Display(), DefaultScreen( x11Display() ), visualAttr );
218 XSetWindowAttributes a;
220 a.colormap = choose_cmap( x11Display(), pVisualInfo ); /* find best colormap */
221 a.background_pixel = backgroundColor().pixel();
222 a.border_pixel = black.pixel();
223 Window p = RootWindow( x11Display(), DefaultScreen( x11Display() ) );
224 if ( parentWidget() )
225 p = parentWidget()->winId();
228 /* if ( type == Type2D ) // creating simple X window for 2d
230 unsigned long xbackground =
231 BlackPixel( x11Display(), DefaultScreen( x11Display() ) );
232 unsigned long xforeground =
233 WhitePixel( x11Display(), DefaultScreen( x11Display() ) );
235 w = XCreateSimpleWindow ( x11Display(), p, x(), y(), width(),
236 height(), 0, xforeground, xbackground );
238 else if ( type == Type3D )
240 w = XCreateWindow( x11Display(), p, x(), y(), width(), height(),
241 0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
242 CWBackPixel | CWBorderPixel | CWColormap, &a );
246 w = XCreateWindow( x11Display(), p, x(), y(), width(), height(),
247 0, pVisualInfo->depth, InputOutput, pVisualInfo->visual,
248 CWBackPixel | CWBorderPixel | CWColormap, &a );
253 if ( XGetWMColormapWindows( x11Display(), topLevelWidget()->winId(), &cmwret, &count ) )
255 cmw = new Window[count+1];
256 memcpy( (char*)cmw, (char*)cmwret, sizeof(Window) * count );
257 XFree( (char*)cmwret );
260 for ( i = 0; i < count; i++ )
262 if ( cmw[i] == winId() ) /* replace old window */
269 if ( i >= count ) /* append new window */
275 cmw = new Window[count];
279 /* Creating new window (with good VisualID) for this widget */
281 XSetWMColormapWindows( x11Display(), topLevelWidget()->winId(), cmw, count );
289 XFree( (char *)pVisualInfo );
291 XFlush( x11Display() );
297 Sets the background 'color'. [ virtual ]
299 void OCCViewer_ViewPort::setBackgroundColor( const QColor& color )
301 QPalette pal = palette();
302 pal.setColor( QColorGroup::Background, color );
305 emit vpChangeBGColor( color );
309 Returns the background color. [ virtual ]
311 QColor OCCViewer_ViewPort::backgroundColor() const
313 return palette().active().background();
317 Returns 'true' if sketching is enabled in this viewport. [ public ]
319 bool OCCViewer_ViewPort::isSketchingEnabled() const
321 return myEnableSketching;
325 Enables / disables sketching [ public ]
327 void OCCViewer_ViewPort::setSketchingEnabled( bool enable )
329 myEnableSketching = enable;
333 Returns 'true' if transformations ( rotation, zoom etc. )
334 are enabled in this viewport. [ public ]
336 bool OCCViewer_ViewPort::isTransformEnabled() const
338 return myEnableTransform;
342 Enables / disables transformations. [ public ]
344 void OCCViewer_ViewPort::setTransformEnabled( bool enable )
346 myEnableTransform = enable;
350 Emits 'mouseEvent' signal. [ virtual protected ]
352 void OCCViewer_ViewPort::mousePressEvent( QMouseEvent *e )
354 emit vpMouseEvent( e );
358 Emits 'mouseEvent' signal. [ virtual protected ]
360 void OCCViewer_ViewPort::mouseMoveEvent( QMouseEvent* e )
362 emit vpMouseEvent( e );
366 Emits 'mouseEvent' signal. [ virtual protected ]
368 void OCCViewer_ViewPort::mouseReleaseEvent( QMouseEvent *e )
370 emit vpMouseEvent( e );
374 Emits 'mouseEvent' signal. [ virtual protected ]
376 void OCCViewer_ViewPort::mouseDoubleClickEvent( QMouseEvent *e )
378 emit vpMouseEvent( e );
382 Emits 'keyEvent' signal. [ virtual protected ]
384 void OCCViewer_ViewPort::keyPressEvent( QKeyEvent *e )
386 emit vpKeyEvent( e );
390 Emits 'keyEvent' signal. [ virtual protected ]
392 void OCCViewer_ViewPort::keyReleaseEvent( QKeyEvent *e )
394 emit vpKeyEvent( e );
398 Repaints the viewport. [ virtual protected ]
400 void OCCViewer_ViewPort::paintEvent( QPaintEvent* )
402 if ( myPaintersRedrawing )
405 emit vpDrawExternal( &p );
406 myPaintersRedrawing = false;
411 Forces to redraw the viewport by an external painter. [ public ]
413 void OCCViewer_ViewPort::redrawPainters()
415 myPaintersRedrawing = true;
420 Updates this view. Does nothing by default. [ virtual public ]
422 void OCCViewer_ViewPort::onUpdate()
427 Creates the popup. [ virtual protected ]
429 /*void OCCViewer_ViewPort::onCreatePopup( QPopupMenu* popup )
433 QtxAction* a = new QtxAction( "", tr( "MEN_VP_CHANGEBGR" ), 0, this );
434 a->setStatusTip( tr( "PRP_VP_CHANGEBGR" ) );
435 connect( a, SIGNAL( activated() ), SLOT( onChangeBgColor()));
436 myPopupActions.append( a );
442 Destroys the popup. [ virtual protected ]
444 /*void OCCViewer_ViewPort::onDestroyPopup( QPopupMenu* popup )
448 for ( QtxAction* a = myPopupActions.first(); a; a = myPopupActions.next() )
449 a->removeFrom( popup );
450 myPopupActions.clear();
455 Sets the background color with color selection dialog. [ virtual protected slot ]
457 void OCCViewer_ViewPort::onChangeBgColor()
459 QColor selColor = QColorDialog::getColor ( backgroundColor(), this );
460 if ( selColor.isValid() )
461 setBackgroundColor( selColor );