1 #include "ApplicationCommon.h"
3 #include "DocumentCommon.h"
11 #include <QMdiSubWindow>
12 #include <QMessageBox>
13 #include <QApplication>
14 #include <QSignalMapper>
16 #include <Graphic3d_GraphicDriver.hxx>
17 #include <OpenGl_GraphicDriver.hxx>
21 static ApplicationCommonWindow* stApp = 0;
22 static QMdiArea* stWs = 0;
24 ApplicationCommonWindow::ApplicationCommonWindow()
33 myIsDocuments = false;
35 // create and define the central widget
36 QFrame* vb = new QFrame( this );
38 QVBoxLayout *layout = new QVBoxLayout( vb );
39 layout->setMargin( 0 );
41 vb->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
42 stWs = new QMdiArea( vb );
43 layout->addWidget( stWs );
44 setCentralWidget( vb );
46 createStandardOperations();
47 createCasCadeOperations();
49 statusBar()->showMessage( QObject::tr("INF_READY"), 5000 );
53 ApplicationCommonWindow::~ApplicationCommonWindow()
57 void ApplicationCommonWindow::createStandardOperations()
59 QPixmap newIcon, helpIcon, closeIcon;
61 QString dir = getResourceDir() + QString( "/" );
63 newIcon = QPixmap( dir + QObject::tr("ICON_NEW") );
64 helpIcon = QPixmap( dir + QObject::tr("ICON_HELP") );
65 closeIcon = QPixmap( dir + QObject::tr("ICON_CLOSE") );
67 QAction * fileNewAction, * fileCloseAction, * filePrefUseVBOAction,
68 * fileQuitAction, * viewToolAction, * viewStatusAction, * helpAboutAction;
70 fileNewAction = new QAction( newIcon, QObject::tr("MNU_NEW"), this );
71 fileNewAction->setToolTip( QObject::tr("TBR_NEW") );
72 fileNewAction->setStatusTip( QObject::tr("TBR_NEW") );
73 fileNewAction->setShortcut( QObject::tr("CTRL+N") );
74 connect( fileNewAction, SIGNAL( triggered() ) , this, SLOT( onNewDoc() ) );
75 myStdActions.insert( FileNewId, fileNewAction );
77 fileCloseAction = new QAction( closeIcon, QObject::tr("MNU_CLOSE"), this );
78 fileCloseAction->setToolTip( QObject::tr("TBR_CLOSE") );
79 fileCloseAction->setStatusTip( QObject::tr("TBR_CLOSE") );
80 fileCloseAction->setShortcut( QObject::tr("CTRL+W") );
81 connect( fileCloseAction, SIGNAL( triggered() ) , this, SLOT( onCloseWindow() ) );
82 myStdActions.insert( FileCloseId, fileCloseAction );
84 filePrefUseVBOAction = new QAction( QObject::tr("MNU_USE_VBO"), this );
85 filePrefUseVBOAction->setToolTip( QObject::tr("TBR_USE_VBO") );
86 filePrefUseVBOAction->setStatusTip( QObject::tr("TBR_USE_VBO") );
87 filePrefUseVBOAction->setCheckable( true );
88 filePrefUseVBOAction->setChecked( true );
89 connect( filePrefUseVBOAction, SIGNAL( triggered() ) , this, SLOT( onUseVBO() ) );
90 myStdActions.insert( FilePrefUseVBOId, filePrefUseVBOAction );
92 fileQuitAction = new QAction( QObject::tr("MNU_QUIT"), this );
93 fileQuitAction->setToolTip( QObject::tr("TBR_QUIT") );
94 fileQuitAction->setStatusTip( QObject::tr("TBR_QUIT") );
95 fileQuitAction->setShortcut( QObject::tr("CTRL+Q") );
96 connect( fileQuitAction, SIGNAL( triggered() ) , qApp, SLOT( closeAllWindows() ) );
97 myStdActions.insert( FileQuitId, fileQuitAction );
99 viewToolAction = new QAction( QObject::tr("MNU_TOOL_BAR"), this );
100 viewToolAction->setToolTip( QObject::tr("TBR_TOOL_BAR") );
101 viewToolAction->setStatusTip( QObject::tr("TBR_TOOL_BAR") );
102 connect( viewToolAction, SIGNAL( triggered() ) , this, SLOT( onViewToolBar() ));
103 viewToolAction->setCheckable( true );
104 viewToolAction->setChecked( true );
105 myStdActions.insert( ViewToolId, viewToolAction );
107 viewStatusAction = new QAction( QObject::tr("MNU_STATUS_BAR"), this );
108 viewStatusAction->setToolTip( QObject::tr("TBR_STATUS_BAR") );
109 viewStatusAction->setStatusTip( QObject::tr("TBR_STATUS_BAR") );
110 connect( viewStatusAction, SIGNAL( triggered() ), this, SLOT( onViewStatusBar() ));
111 viewStatusAction->setCheckable( true );
112 viewStatusAction->setChecked( true );
113 myStdActions.insert( ViewStatusId, viewStatusAction );
115 helpAboutAction = new QAction( helpIcon, QObject::tr("MNU_ABOUT"), this );
116 helpAboutAction->setToolTip( QObject::tr( "TBR_ABOUT" ) );
117 helpAboutAction->setStatusTip( QObject::tr( "TBR_ABOUT" ) );
118 helpAboutAction->setShortcut( QObject::tr( "F1" ) );
119 connect( helpAboutAction, SIGNAL( triggered() ) , this, SLOT( onAbout() ) );
120 myStdActions.insert( HelpAboutId, helpAboutAction );
122 // create preferences menu
123 QMenu* aPrefMenu = new QMenu( QObject::tr("MNU_PREFERENCES") );
124 aPrefMenu->addAction( filePrefUseVBOAction );
126 // populate a menu with all actions
127 myFilePopup = new QMenu( this );
128 myFilePopup = menuBar()->addMenu( QObject::tr("MNU_FILE") );
129 myFilePopup->addAction( fileNewAction );
130 myFilePopup->addAction( fileCloseAction );
131 myFileSeparator = myFilePopup->addSeparator();
132 myFilePopup->addMenu( aPrefMenu );
133 myFileSeparator = myFilePopup->addSeparator();
134 myFilePopup->addAction( fileQuitAction );
137 QMenu * view = new QMenu( this );
139 view = menuBar()->addMenu( QObject::tr("MNU_VIEW") );
140 view->addAction( viewToolAction );
141 view->addAction( viewStatusAction );
144 QMenu * help = new QMenu( this );
145 menuBar()->addSeparator();
146 help = menuBar()->addMenu( QObject::tr("MNU_HELP") );
147 help->addAction( helpAboutAction );
149 // populate a tool bar with some actions
150 myStdToolBar = addToolBar( tr( "File Operations" ) );
151 myStdToolBar->addAction( fileNewAction );
152 myStdToolBar->addAction( helpAboutAction );
154 myStdActions.at(FileCloseId)->setEnabled(myDocuments.count() > 0);
156 myStdActions.at(FilePrefUseVBOId)->setEnabled( true );
159 void ApplicationCommonWindow::createCasCadeOperations()
163 // populate a tool bar with some actions
164 myCasCadeBar = addToolBar( tr( "Shape Operations" ) );
166 QString dir = ApplicationCommonWindow::getResourceDir() + QString( "/" );
169 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_WIRE") ), QObject::tr("MNU_TOOL_WIRE"), this );
170 a->setToolTip( QObject::tr("TBR_TOOL_WIRE") );
171 a->setStatusTip( QObject::tr("TBR_TOOL_WIRE") );
172 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
173 myToolActions.insert( ToolWireframeId, a );
175 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_SHAD") ), QObject::tr("MNU_TOOL_SHAD"), this );
176 a->setToolTip( QObject::tr("TBR_TOOL_SHAD") );
177 a->setStatusTip( QObject::tr("TBR_TOOL_SHAD") );
178 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
179 myToolActions.insert( ToolShadingId, a );
181 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_COLOR") ), QObject::tr("MNU_TOOL_COLOR"), this );
182 a->setToolTip( QObject::tr("TBR_TOOL_COLOR") );
183 a->setStatusTip( QObject::tr("TBR_TOOL_COLOR") );
184 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
185 myToolActions.insert( ToolColorId, a );
187 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_MATER") ), QObject::tr("MNU_TOOL_MATER"), this );
188 a->setToolTip( QObject::tr("TBR_TOOL_MATER") );
189 a->setStatusTip( QObject::tr("TBR_TOOL_MATER") );
190 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
191 myToolActions.insert( ToolMaterialId, a );
193 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_TRANS") ), QObject::tr("MNU_TOOL_TRANS"), this );
194 a->setToolTip( QObject::tr("TBR_TOOL_TRANS") );
195 a->setStatusTip( QObject::tr("TBR_TOOL_TRANS") );
196 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
197 myToolActions.insert( ToolTransparencyId, a );
199 a = new QAction( QPixmap( dir+QObject::tr("ICON_TOOL_DEL") ), QObject::tr("MNU_TOOL_DEL"), this );
200 a->setToolTip( QObject::tr("TBR_TOOL_DEL") );
201 a->setStatusTip( QObject::tr("TBR_TOOL_DEL") );
202 connect( a, SIGNAL( triggered() ) , this, SLOT( onToolAction() ) );
203 myToolActions.insert( ToolDeleteId, a );
205 QSignalMapper* sm = new QSignalMapper( this );
206 connect( sm, SIGNAL( mapped( int ) ), this, SLOT( onSetMaterial( int ) ) );
208 a = new QAction( QObject::tr("MNU_BRASS"), this );
209 a->setToolTip( QObject::tr("TBR_BRASS") );
210 a->setStatusTip( QObject::tr("TBR_BRASS") );
211 sm->setMapping( a,(int)Graphic3d_NOM_BRASS );
212 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
213 myMaterialActions.insert( Graphic3d_NOM_BRASS, a );
215 a = new QAction( QObject::tr("MNU_BRONZE"), this );
216 a->setToolTip( QObject::tr("TBR_BRONZE") );
217 a->setStatusTip( QObject::tr("TBR_BRONZE") );
218 sm->setMapping( a, ( int )Graphic3d_NOM_BRONZE );
219 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
220 myMaterialActions.insert( Graphic3d_NOM_BRONZE, a );
222 a = new QAction( QObject::tr("MNU_COPPER"), this );
223 a->setToolTip( QObject::tr("TBR_COPPER") );
224 a->setStatusTip( QObject::tr("TBR_COPER") );
225 sm->setMapping( a, ( int )Graphic3d_NOM_COPPER );
226 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
227 myMaterialActions.insert( Graphic3d_NOM_COPPER, a );
229 a = new QAction( QObject::tr("MNU_GOLD"), this );
230 a->setToolTip( QObject::tr("TBR_GOLD") );
231 a->setStatusTip( QObject::tr("TBR_GOLD") );
232 sm->setMapping( a, ( int )Graphic3d_NOM_GOLD );
233 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
234 myMaterialActions.insert( Graphic3d_NOM_GOLD, a );
236 a = new QAction( QObject::tr("MNU_PEWTER"), this );
237 a->setToolTip( QObject::tr("TBR_PEWTER") );
238 a->setStatusTip( QObject::tr("TBR_PEWTER") );
239 sm->setMapping( a, ( int )Graphic3d_NOM_PEWTER );
240 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
241 myMaterialActions.insert( Graphic3d_NOM_PEWTER, a );
243 a = new QAction( QObject::tr("MNU_PLASTER"), this );
244 a->setToolTip( QObject::tr("TBR_PLASTER") );
245 a->setStatusTip( QObject::tr("TBR_PLASTER") );
246 sm->setMapping( a, ( int )Graphic3d_NOM_PLASTER );
247 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
248 myMaterialActions.insert( Graphic3d_NOM_PLASTER, a );
250 a = new QAction( QObject::tr("MNU_PLASTIC"), this );
251 a->setToolTip( QObject::tr("TBR_PLASTIC") );
252 a->setStatusTip( QObject::tr("TBR_PLASTIC") );
253 sm->setMapping( a, ( int )Graphic3d_NOM_PLASTIC );
254 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
255 myMaterialActions.insert( Graphic3d_NOM_PLASTIC, a );
257 a = new QAction( QObject::tr("MNU_SILVER"), this );
258 a->setToolTip( QObject::tr("TBR_SILVER") );
259 a->setStatusTip( QObject::tr("TBR_SILVER") );
260 sm->setMapping( a, ( int )Graphic3d_NOM_SILVER );
261 connect( a, SIGNAL( triggered() ), sm, SLOT( map() ) );
262 myMaterialActions.insert( Graphic3d_NOM_SILVER, a );
264 for ( int i = 0; i < myToolActions.size(); i++ )
265 myCasCadeBar->addAction( myToolActions.at( i ) );
266 myCasCadeBar->hide();
269 QList<QAction*>* ApplicationCommonWindow::getToolActions()
271 return &myToolActions;
274 QList<QAction*>* ApplicationCommonWindow::getMaterialActions()
276 return &myMaterialActions;
279 void ApplicationCommonWindow::createWindowPopup()
281 if ( !myWindowPopup )
283 myWindowPopup = new QMenu( QObject::tr( "MNU_WINDOW" ), this );
284 connect( myWindowPopup, SIGNAL( aboutToShow() ),
285 this, SLOT( windowsMenuAboutToShow() ) );
289 void ApplicationCommonWindow::windowsMenuAboutToShow()
291 myWindowPopup->clear();
294 QString dir = getResourceDir() + QString( "/" );
296 a = new QAction( QPixmap( dir + QObject::tr( "ICON_WINDOW_NEW3D" ) ), QObject::tr( "MNU_WINDOW_NEW3D" ), this );
297 a->setToolTip( QObject::tr( "TBR_WINDOW_NEW3D" ) );
298 a->setStatusTip( QObject::tr( "TBR_WINDOW_NEW3D" ) );
299 connect( a, SIGNAL( triggered() ), this, SLOT( onCreateNewView() ) );
300 myWindowPopup->addAction( a );
302 a = new QAction( QPixmap( dir + QObject::tr( "ICON_WINDOW_CASCADE" ) ), QObject::tr( "MNU_WINDOW_CASCADE" ), this );
303 a->setToolTip( QObject::tr( "TBR_WINDOW_CASCADE" ) );
304 a->setStatusTip( QObject::tr( "TBR_WINDOW_CASCADE" ) );
305 connect( a, SIGNAL( triggered() ), stWs, SLOT( cascade() ) );
306 myWindowPopup->addAction( a );
308 a = new QAction( QPixmap( dir + QObject::tr( "ICON_WINDOW_TILE" ) ), QObject::tr( "MNU_WINDOW_TILE" ), this );
309 a->setToolTip( QObject::tr( "TBR_WINDOW_TILE" ) );
310 a->setStatusTip( QObject::tr( "TBR_WINDOW_TILE" ) );
311 connect( a, SIGNAL( triggered() ), stWs, SLOT( tile() ) );
312 myWindowPopup->addAction( a );
314 myWindowPopup->addSeparator();
315 QList<QMdiSubWindow *> windows = stWs->subWindowList();
316 for (int i = 0; i < windows.count(); ++i)
318 QAction* aAction = new QAction( windows.at(i)->windowTitle(), this );
319 aAction->setCheckable( true );
320 aAction->setData( i );
321 myWindowPopup->addAction( aAction );
322 connect( aAction, SIGNAL( toggled( bool ) ), this, SLOT( windowsMenuActivated( bool ) ) );
323 aAction->setChecked( stWs->activeSubWindow() == windows.at(i) );
327 void ApplicationCommonWindow::windowsMenuActivated( bool checked )
329 QAction* aSender = qobject_cast<QAction*>( sender() );
332 QWidget * w = stWs->subWindowList().at( aSender->data().toInt() );
337 QMdiArea * ApplicationCommonWindow::getWorkspace()
342 ApplicationCommonWindow* ApplicationCommonWindow::getApplication()
347 void ApplicationCommonWindow::updateFileActions()
349 if ( myDocuments.isEmpty() )
351 if ( !myIsDocuments )
353 QAction *fileQuitAction, *windowAction;
354 QList<QAction *> aListActions = myFilePopup->actions();
355 for ( int i = 0; i < aListActions.size(); i++ )
357 if( aListActions.at( i )->text() == QObject::tr("MNU_QUIT") )
359 fileQuitAction = aListActions.at( i );
364 if( !fileQuitAction )
367 myIsDocuments = true;
368 myCasCadeBar->show();
370 QList<QAction *> aListMenuActions = menuBar()->actions();
371 for ( int i = 0; i < aListMenuActions.size(); i++ )
373 if( aListMenuActions.at( i )->text() == QObject::tr("MNU_HELP") )
375 windowAction= aListMenuActions.at( i );
383 menuBar()->insertMenu( windowAction, myWindowPopup );
387 myIsDocuments = false;
388 myCasCadeBar->hide();
389 menuBar()->removeAction( myWindowPopup->menuAction() );
394 DocumentCommon* ApplicationCommonWindow::createNewDocument()
396 return new DocumentCommon( ++myNbDocuments, this );
399 int& ApplicationCommonWindow::getNbDocument()
401 return myNbDocuments;
404 DocumentCommon* ApplicationCommonWindow::onNewDoc()
408 DocumentCommon* aDoc = createNewDocument();
409 aDoc->onCreateNewView();
410 onSelectionChanged();
412 connect (aDoc, SIGNAL (sendCloseDocument (DocumentCommon*) ),
413 this, SLOT (onCloseDocument (DocumentCommon*)));
414 connect (stWs, SIGNAL (windowActivated (QWidget*)),
415 this, SLOT (onWindowActivated (QWidget*)));
416 connect (aDoc, SIGNAL (selectionChanged()),
417 this, SLOT (onSelectionChanged()));
419 myDocuments.append (aDoc);
420 myStdActions.at (FileCloseId)->setEnabled (myDocuments.count() > 0);
425 void ApplicationCommonWindow::onCloseWindow()
427 stWs->activeSubWindow()->close();
430 void ApplicationCommonWindow::onUseVBO()
432 MDIWindow* aWindow = qobject_cast<MDIWindow*> (stWs->activeSubWindow()->widget());
437 Handle(AIS_InteractiveContext) aContextAIS = aWindow->getDocument()->getContext();
439 if (aContextAIS.IsNull())
442 Handle(OpenGl_GraphicDriver) aDriver =
443 Handle(OpenGl_GraphicDriver)::DownCast (aContextAIS->CurrentViewer()->Driver());
445 if (!aDriver.IsNull())
447 aDriver->ChangeOptions().vboDisable = Standard_True;
451 void ApplicationCommonWindow::onCloseDocument(DocumentCommon* theDoc)
453 myDocuments.removeAll( theDoc );
454 theDoc->removeViews();
457 myStdActions.at(FileCloseId)->setEnabled(myDocuments.count() > 0);
460 void ApplicationCommonWindow::onViewToolBar()
462 bool show = myStdActions.at( ViewToolId )->isChecked();
463 if ( show == myStdToolBar->isVisible() )
466 myStdToolBar->show();
468 myStdToolBar->hide();
471 void ApplicationCommonWindow::onViewStatusBar()
473 bool show = myStdActions.at( ViewStatusId )->isChecked();
474 if ( show == statusBar()->isVisible() )
482 void ApplicationCommonWindow::onAbout()
484 QMessageBox::information( this, QObject::tr( "TIT_ABOUT" ), QObject::tr( "INF_ABOUT" ), QObject::tr("BTN_OK" ),
485 QString::null, QString::null, 0, 0 );
488 void ApplicationCommonWindow::onCreateNewView()
490 MDIWindow* window = qobject_cast< MDIWindow* >( stWs->activeSubWindow()->widget() );
491 window->getDocument()->onCreateNewView();
494 void ApplicationCommonWindow::onWindowActivated ( QWidget * w )
501 MDIWindow* window = qobject_cast< MDIWindow* >(w);
503 window->onWindowActivated();
506 void ApplicationCommonWindow::onToolAction()
508 QAction* sentBy = (QAction*) sender();
509 QMdiArea* ws = ApplicationCommonWindow::getWorkspace();
510 DocumentCommon* doc = qobject_cast<MDIWindow*>( ws->activeSubWindow()->widget() )->getDocument();
512 if( sentBy == myToolActions.at( ToolWireframeId ) )
515 if( sentBy == myToolActions.at( ToolShadingId ) )
518 if( sentBy == myToolActions.at( ToolColorId ) )
521 if( sentBy == myToolActions.at( ToolMaterialId ) )
524 if( sentBy == myToolActions.at( ToolTransparencyId ) )
525 doc->onTransparency();
527 if( sentBy == myToolActions.at( ToolDeleteId ) )
531 void ApplicationCommonWindow::onSelectionChanged()
533 QMdiArea* ws = ApplicationCommonWindow::getWorkspace();
536 if( !qobject_cast<MDIWindow*>( ws->activeSubWindow()->widget() ) )
539 doc = ( qobject_cast<MDIWindow*>( ws->activeSubWindow()->widget() ) )->getDocument();
540 Handle(AIS_InteractiveContext) context = doc->getContext();
542 bool OneOrMoreInShading = false;
543 bool OneOrMoreInWireframe = false;
544 int numSel = context->NbSelected();
547 for ( context->InitSelected(); context->MoreSelected(); context->NextSelected() )
549 if ( context->IsDisplayed( context->SelectedInteractive(), 1 ) )
550 OneOrMoreInShading = true;
551 if ( context->IsDisplayed( context->SelectedInteractive(), 0 ) )
552 OneOrMoreInWireframe = true;
554 myToolActions.at( ToolWireframeId )->setEnabled( OneOrMoreInShading );
555 myToolActions.at( ToolShadingId )->setEnabled( OneOrMoreInWireframe );
556 myToolActions.at( ToolColorId )->setEnabled( true );
557 myToolActions.at( ToolMaterialId )->setEnabled( true );
558 myToolActions.at( ToolTransparencyId )->setEnabled( OneOrMoreInShading );
559 myToolActions.at( ToolDeleteId )->setEnabled( true );
563 myToolActions.at( ToolWireframeId )->setEnabled( false );
564 myToolActions.at( ToolShadingId )->setEnabled( false );
565 myToolActions.at( ToolColorId )->setEnabled( false );
566 myToolActions.at( ToolMaterialId )->setEnabled( false );
567 myToolActions.at( ToolTransparencyId )->setEnabled( false );
568 myToolActions.at( ToolDeleteId )->setEnabled( false );
572 void ApplicationCommonWindow::onSetMaterial( int theMaterial )
574 QMdiArea* ws = getWorkspace();
575 DocumentCommon* doc = qobject_cast<MDIWindow*>( ws->activeSubWindow()->widget() )->getDocument();
576 doc->onMaterial( theMaterial );
579 QString ApplicationCommonWindow::getResourceDir()
581 static QString aResourceDir =
582 QString::fromUtf8 (qgetenv ("CSF_ResourcesDefaults").constData());
587 void ApplicationCommonWindow::resizeEvent( QResizeEvent* e )
589 QMainWindow::resizeEvent( e );
590 statusBar()->setSizeGripEnabled( !isMaximized() );
593 bool ApplicationCommonWindow::isDocument()
595 return myIsDocuments;
598 QMenu* ApplicationCommonWindow::getFilePopup()
603 QAction* ApplicationCommonWindow::getFileSeparator()
605 return myFileSeparator;
608 QToolBar* ApplicationCommonWindow::getCasCadeBar()