Salome HOME
bddff0e9cc2febae9e15b793ca9457e649144f4e
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
1 // File:      SalomeApp_Application.cxx
2 // Created:   10/22/2004 3:23:45 PM
3 // Author:    Sergey LITONIN
4 // Copyright (C) CEA 2004
5
6 #include "SalomeApp_PyInterp.h" // WARNING! This include must be the first!
7
8 #include "SalomeApp_Application.h"
9
10 #include "SalomeApp_Study.h"
11 #include "SalomeApp_DataModel.h"
12 #include "SalomeApp_DataObject.h"
13 #include "SalomeApp_EventFilter.h"
14
15 #include "SalomeApp_StudyPropertiesDlg.h"
16
17 #include "SalomeApp_CheckFileDlg.h"
18
19 #include "LightApp_Application.h"
20 #include "LightApp_Preferences.h"
21 #include "LightApp_WidgetContainer.h"
22 #include "LightApp_SelectionMgr.h"
23
24 #include "STD_LoadStudiesDlg.h"
25
26 #include <SUIT_Tools.h>
27 #include <SUIT_Session.h>
28
29 #include <QtxMRUAction.h>
30
31 #include <OB_Browser.h>
32 #include <OB_ListItem.h>
33
34 #include <PythonConsole_PyConsole.h>
35
36 #include <SUIT_FileDlg.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_ActionOperation.h>
40
41 #include <Utils_ORB_INIT.hxx>
42 #include <Utils_SINGLETON.hxx>
43 #include <SALOME_ModuleCatalog_impl.hxx>
44 #include <SALOME_LifeCycleCORBA.hxx>
45
46 #include <qmap.h>
47 #include <qaction.h>
48 #include <qcombobox.h>
49 #include <qlistbox.h>
50 #include <qregexp.h>
51
52 #include "SALOMEDS_StudyManager.hxx"
53 #include "SALOMEDS_SObject.hxx"
54
55 #include "SALOME_ListIteratorOfListIO.hxx"
56 #include "SALOME_ListIO.hxx"
57
58 #include "ToolsGUI_CatalogGeneratorDlg.h"
59 #include "ToolsGUI_RegWidget.h"
60
61 /*!Create new instance of SalomeApp_Application.*/
62 extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
63 {
64   return new SalomeApp_Application();
65 }
66
67 /*
68   Class       : SalomeApp_Application
69   Description : Application containing SalomeApp module or LightApp module
70 */
71
72 /*!Constructor.*/
73 SalomeApp_Application::SalomeApp_Application()
74 : LightApp_Application()
75 {
76 }
77
78 /*!Destructor.
79  *\li Destroy event filter.
80  */
81 SalomeApp_Application::~SalomeApp_Application()
82 {
83   SalomeApp_EventFilter::Destroy();
84 }
85
86 /*!Start application.*/
87 void SalomeApp_Application::start()
88 {
89   LightApp_Application::start();
90
91   SalomeApp_EventFilter::Init();
92 }
93
94 /*!Create actions:*/
95 void SalomeApp_Application::createActions()
96 {
97   LightApp_Application::createActions();
98
99   SUIT_Desktop* desk = desktop();
100   
101   //! Dump study
102   createAction( DumpStudyId, tr( "TOT_DESK_FILE_DUMP_STUDY" ), QIconSet(),
103                 tr( "MEN_DESK_FILE_DUMP_STUDY" ), tr( "PRP_DESK_FILE_DUMP_STUDY" ),
104                 0, desk, false, this, SLOT( onDumpStudy() ) );
105     
106   //! Load script
107   createAction( LoadScriptId, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), QIconSet(),
108                 tr( "MEN_DESK_FILE_LOAD_SCRIPT" ), tr( "PRP_DESK_FILE_LOAD_SCRIPT" ),
109                 0, desk, false, this, SLOT( onLoadScript() ) );
110
111   //! Properties
112   createAction( PropertiesId, tr( "TOT_DESK_PROPERTIES" ), QIconSet(),
113                 tr( "MEN_DESK_PROPERTIES" ), tr( "PRP_DESK_PROPERTIES" ),
114                 0, desk, false, this, SLOT( onProperties() ) );
115
116   //! Catalog Generator
117   createAction( CatalogGenId, tr( "TOT_DESK_CATALOG_GENERATOR" ),  QIconSet(),
118                 tr( "MEN_DESK_CATALOG_GENERATOR" ), tr( "PRP_DESK_CATALOG_GENERATOR" ),
119                 0, desk, false, this, SLOT( onCatalogGen() ) );
120
121   //! Registry Display
122   createAction( RegDisplayId, tr( "TOT_DESK_REGISTRY_DISPLAY" ),  QIconSet(),
123                 tr( "MEN_DESK_REGISTRY_DISPLAY" ), tr( "PRP_DESK_REGISTRY_DISPLAY" ),
124                 0, desk, false, this, SLOT( onRegDisplay() ) );
125
126   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1 );
127
128   createMenu( DumpStudyId, fileMenu, 10, -1 );
129   createMenu( separator(), fileMenu, -1, 15, -1 );
130   createMenu( LoadScriptId, fileMenu, 10, -1 );
131   createMenu( separator(), fileMenu, -1, 15, -1 );
132   createMenu( PropertiesId, fileMenu, 10, -1 );
133   createMenu( separator(), fileMenu, -1, 15, -1 );
134
135   int toolsMenu = createMenu( tr( "MEN_DESK_TOOLS" ), -1, -1, 50 );
136   createMenu( CatalogGenId, toolsMenu, 10, -1 );
137   createMenu( RegDisplayId, toolsMenu, 10, -1 );
138   createMenu( separator(), toolsMenu, -1, 15, -1 );
139 }
140
141 /*! Purpose : SLOT. Open new document with \a aName.*/
142 bool SalomeApp_Application::onOpenDoc( const QString& aName )
143 {
144   bool res = false, toOpen = true, isAlreadyOpen = false;
145
146   // Look among opened studies
147   if (activeStudy()) { // at least one study is opened
148     SUIT_Session* aSession = SUIT_Session::session();
149     QPtrList<SUIT_Application> aAppList = aSession->applications();
150     QPtrListIterator<SUIT_Application> it (aAppList);
151     SUIT_Application* aApp = 0;
152     // iterate on all applications
153     for (; (aApp = it.current()) && !isAlreadyOpen; ++it) {
154       if (aApp->activeStudy()->studyName() == aName) {
155         isAlreadyOpen = true; // Already opened, ask user what to do
156
157         // The document ... is already open.
158         // Do you want to reload it?
159         int aAnswer = SUIT_MessageBox::warn2(desktop(), tr("WRN_WARNING"),
160                                              tr("QUE_DOC_ALREADYOPEN").arg(aName),
161                                              tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
162         if (aAnswer == 1) { // reload
163           if (activeStudy()->studyName() == aName && aAppList.count() > 1) {
164             // Opened in THIS (active) application.
165             STD_Application* app1 = (STD_Application*)aAppList.at(0);
166             STD_Application* app2 = (STD_Application*)aAppList.at(1);
167             if (!app1 || !app2) {
168               // Error
169               return false;
170             }
171             if (app1->activeStudy()->studyName() == aName) {
172               // app1 is this application, we need another one
173               app1 = app2;
174             }
175             // Close document of this application. This application will be destroyed.
176             onCloseDoc(/*ask = */false);
177             // Open the file with another application, as this one will be destroyed.
178             return app1->onOpenDoc(aName);
179           } else {
180             // Opened in another application.
181             STD_Application* app = (STD_Application*)aApp;
182             if (app)
183               app->onCloseDoc(/*ask = */false);
184           }
185         } else { // do not reload
186           // OK, the study will not be reloaded, but we call
187           // CAM_Application::onOpenDoc( aName ) all the same.
188           // It will activate a desktop of the study <aName>.
189         }
190       }
191     }
192   }
193
194   // Look among unloaded studies
195   if (!isAlreadyOpen) {
196     std::vector<std::string> List = studyMgr()->GetOpenStudies();
197
198     QString studyName;
199     for (unsigned int ind = 0; ind < List.size() && !isAlreadyOpen; ind++) {
200       studyName = List[ind].c_str();
201       if (aName == studyName) {
202         // Already exists unloaded, ask user what to do
203         isAlreadyOpen = true;
204
205         // The document ... already exists in the study manager.
206         // Do you want to reload it?
207         int aAnswer = SUIT_MessageBox::warn2(desktop(), tr("WRN_WARNING"),
208                                              tr("QUE_DOC_ALREADYEXIST").arg(aName),
209                                              tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
210         if (aAnswer == 1) {
211           _PTR(Study) aStudy = studyMgr()->GetStudyByName(aName.latin1());
212           if (aStudy)
213             studyMgr()->Close(aStudy);
214         } else {
215           toOpen = false;
216         }
217       }
218     }
219   }
220
221   if (toOpen)
222     res = CAM_Application::onOpenDoc( aName );
223
224   QAction* a = action( MRUId );
225   if ( a && a->inherits( "QtxMRUAction" ) )
226   {
227     QtxMRUAction* mru = (QtxMRUAction*)a;
228     if ( res )
229       mru->insert( aName );
230     else
231       mru->remove( aName );
232   }
233   return res;
234 }
235
236 /*!SLOT. Load document.*/
237 void SalomeApp_Application::onLoadDoc()
238 {
239   QString name, studyname, ext;
240
241   STD_LoadStudiesDlg aDlg( desktop(), TRUE);
242
243   std::vector<std::string> List = studyMgr()->GetOpenStudies();
244
245   SUIT_Session* aSession = SUIT_Session::session();
246   QPtrList<SUIT_Application> aAppList = aSession->applications();
247   SUIT_Application* aApp = 0;
248
249   for (unsigned int ind = 0; ind < List.size(); ind++) {
250      studyname = List[ind].c_str();
251      //Add to list only unloaded studies
252      bool isAlreadyOpen = false;
253      for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
254        {
255          aApp = it.current();
256          if(!aApp || !aApp->activeStudy()) continue;
257          if ( aApp->activeStudy()->studyName() == studyname ) isAlreadyOpen = true;
258        }
259
260      if ( !isAlreadyOpen ) aDlg.ListComponent->insertItem( studyname );
261   }
262
263   int retVal = aDlg.exec();
264   studyname = aDlg.ListComponent->currentText();
265
266   if (retVal == QDialog::Rejected)
267     return;
268
269   if ( studyname.isNull() || studyname.isEmpty() )
270     return;
271
272   name = studyname;
273   name.replace( QRegExp(":"), "/" );
274
275   if( LightApp_Application::onLoadDoc( name ) )
276   {
277      updateWindows();
278      updateViewManagers();
279      updateObjectBrowser(true);
280   }
281 }
282
283
284 /*!SLOT. Load document with \a aName.*/
285 bool SalomeApp_Application::onLoadDoc( const QString& aName )
286 {
287   return LightApp_Application::onLoadDoc( aName );
288 }
289
290 /*!SLOT. Copy objects to study maneger from selection maneger..*/
291 void SalomeApp_Application::onCopy()
292 {
293   SALOME_ListIO list;
294   LightApp_SelectionMgr* mgr = selectionMgr();
295   mgr->selectedObjects(list);
296   
297   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
298   if(study == NULL) return;
299   
300   _PTR(Study) stdDS = study->studyDS();
301   if(!stdDS) return;
302
303   SALOME_ListIteratorOfListIO it( list );
304   if(it.More())
305     {
306       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
307       try {
308         studyMgr()->Copy(so);
309         onSelectionChanged();
310       }
311       catch(...) {
312       }
313     }
314 }
315
316 /*!SLOT. Paste objects to study maneger from selection manager.*/
317 void SalomeApp_Application::onPaste()
318 {
319   SALOME_ListIO list;
320   LightApp_SelectionMgr* mgr = selectionMgr();
321   mgr->selectedObjects(list);
322
323   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
324   if(study == NULL) return;
325
326   _PTR(Study) stdDS = study->studyDS();
327   if(!stdDS) return;
328
329   SALOME_ListIteratorOfListIO it( list );
330   if(it.More())
331     {
332       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
333       try {
334         studyMgr()->Paste(so);
335         updateObjectBrowser( true );
336         updateActions(); //SRN: BugID IPAL9377, case 3
337       }
338       catch(...) {
339       }
340     }
341 }
342
343 /*!Sets enable or disable some actions on selection changed.*/
344 void SalomeApp_Application::onSelectionChanged()
345 {
346    SALOME_ListIO list;
347    LightApp_SelectionMgr* mgr = selectionMgr();
348    mgr->selectedObjects(list);
349
350    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
351    if(study == NULL) return;
352
353    _PTR(Study) stdDS = study->studyDS();
354    if(!stdDS) return;
355
356    QAction* qaction;
357
358    SALOME_ListIteratorOfListIO it( list );
359    if(it.More() && list.Extent() == 1)
360    {
361       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
362
363       qaction = action(EditCopyId);
364       if( so ) {
365         SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(so.get());
366         if ( aSO && studyMgr()->CanCopy(so) ) qaction->setEnabled(true);  
367         else qaction->setEnabled(false);
368       }
369       else qaction->setEnabled(false);
370
371       qaction = action(EditPasteId);
372       if( so ) {
373         SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(so.get());
374         if( aSO && studyMgr()->CanPaste(so) ) qaction->setEnabled(true);
375         qaction->setEnabled(false);
376       }
377       else qaction->setEnabled(false);
378    }
379    else {
380      qaction = action(EditCopyId);
381      qaction->setEnabled(false);
382      qaction = action(EditPasteId);
383      qaction->setEnabled(false);
384    }
385 }
386
387 /*!Delete references.*/
388 void SalomeApp_Application::onDeleteReferences()
389 {
390   SALOME_ListIO aList;
391   LightApp_SelectionMgr* mgr = selectionMgr();
392   mgr->selectedObjects(aList);
393
394   if (aList.Extent() < 1) return;
395
396   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(activeStudy());
397   _PTR(Study) aStudyDS = aStudy->studyDS();
398   _PTR(StudyBuilder) aStudyBuilder = aStudyDS->NewBuilder();
399   _PTR(SObject) anObj;
400
401   for ( SALOME_ListIteratorOfListIO it( aList ); it.More(); it.Next() )
402     {
403       if ( it.Value()->hasEntry() )
404         {
405           _PTR(SObject) aSObject = aStudyDS->FindObjectID( it.Value()->getEntry() );
406           if ( aSObject->ReferencedObject(anObj) )
407             aStudyBuilder->RemoveReference(aSObject);
408         }
409     }
410
411   updateObjectBrowser();
412 }
413
414 /*!Private SLOT. */
415 void SalomeApp_Application::onOpenWith()
416 {
417   QApplication::setOverrideCursor( Qt::waitCursor );
418   SALOME_ListIO aList;
419   LightApp_SelectionMgr* mgr = selectionMgr();
420   mgr->selectedObjects(aList);
421   if (aList.Extent() != 1)
422     {
423       QApplication::restoreOverrideCursor();
424       return;
425     }
426   Handle(SALOME_InteractiveObject) aIObj = aList.First();
427   QString aModuleName(aIObj->getComponentDataType());
428   QString aModuleTitle = moduleTitle(aModuleName);
429   activateModule(aModuleTitle);
430   QApplication::restoreOverrideCursor();
431 }
432
433 //=======================================================================
434 // name    : createNewStudy
435 /*! Purpose : Create new study*/
436 //=======================================================================
437 SUIT_Study* SalomeApp_Application::createNewStudy()
438 {
439   SalomeApp_Study* aStudy = new SalomeApp_Study( this );
440
441   // Set up processing of major study-related events
442   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
443   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
444   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
445   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
446
447   return aStudy;
448 }
449
450 //=======================================================================
451 // name    : updateCommandsStatus
452 /*! Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu*/
453 //=======================================================================
454 void SalomeApp_Application::updateCommandsStatus()
455 {
456   LightApp_Application::updateCommandsStatus();
457
458   // Dump study menu
459   QAction* a = action( DumpStudyId );
460   if ( a )
461     a->setEnabled( activeStudy() );
462
463   // Load script menu
464   a = action( LoadScriptId );
465   if ( a )
466     a->setEnabled( activeStudy() );
467
468   a = action( PropertiesId );
469   if( a )
470     a->setEnabled( activeStudy() );
471
472   a = action(EditCopyId);
473   a->setEnabled(false);
474   a = action(EditPasteId);
475   a->setEnabled(false);
476 }
477
478 /*!Private SLOT. On dump study.*/
479 void SalomeApp_Application::onDumpStudy( )
480 {
481   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
482   if ( !appStudy ) return;
483   _PTR(Study) aStudy = appStudy->studyDS();
484
485   QStringList aFilters;
486   aFilters.append( tr( "PYTHON_FILES_FILTER" ) );
487
488   SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg( desktop(), false, tr("PUBLISH_IN_STUDY"), true, true);
489   fd->setCaption( tr( "TOT_DESK_FILE_DUMP_STUDY" ) );
490   fd->setFilters( aFilters );
491   fd->SetChecked(true);
492   fd->exec();
493   QString aFileName = fd->selectedFile();
494   bool toPublish = fd->IsChecked();
495   delete fd;
496
497   if(!aFileName.isEmpty()) {
498     QFileInfo aFileInfo(aFileName);
499     aStudy->DumpStudy( aFileInfo.dirPath( true ).latin1(), aFileInfo.baseName().latin1(), toPublish );
500   }
501 }
502
503 /*!Private SLOT. On load script.*/
504 void SalomeApp_Application::onLoadScript( )
505 {
506   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
507   if ( !appStudy ) return;
508   _PTR(Study) aStudy = appStudy->studyDS();
509
510   if ( aStudy->GetProperties()->IsLocked() ) {
511     SUIT_MessageBox::warn1 ( desktop(),
512                              QObject::tr("WRN_WARNING"),
513                              QObject::tr("WRN_STUDY_LOCKED"),
514                              QObject::tr("BUT_OK") );
515     return;
516   }
517
518   QStringList filtersList;
519   filtersList.append(tr("PYTHON_FILES_FILTER"));
520   filtersList.append(tr("ALL_FILES_FILTER"));
521
522   QString aFile = SUIT_FileDlg::getFileName( desktop(), "", filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
523
524   if ( !aFile.isEmpty() )
525   {
526     QString command = QString("execfile(\"%1\")").arg(aFile);
527
528     PythonConsole* pyConsole = pythonConsole();
529
530     if ( pyConsole )
531       pyConsole->exec( command );
532   }
533 }
534
535 /*!Gets file filter.
536  *\retval QString "(*.hdf)"
537  */
538 QString SalomeApp_Application::getFileFilter() const
539 {
540   return "(*.hdf)";
541 }
542
543 /*!Create window.*/
544 QWidget* SalomeApp_Application::createWindow( const int flag )
545 {
546   QWidget* wid = LightApp_Application::createWindow(flag);
547
548   SUIT_ResourceMgr* resMgr = resourceMgr();
549
550   if ( flag == WT_ObjectBrowser )
551   {
552     OB_Browser* ob = (OB_Browser*)wid;
553     connect( ob->listView(), SIGNAL( doubleClicked( QListViewItem* ) ), this, SLOT( onDblClick( QListViewItem* ) ) );
554     bool autoSize = resMgr->booleanValue( "ObjectBrowser", "auto_size", false );
555     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
556     {
557       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
558       ob->setColumnShown( i, resMgr->booleanValue( "ObjectBrowser",
559                                                    QString().sprintf( "visibility_column_%d", i ), true ) );
560     }
561     ob->setWidthMode( autoSize ? QListView::Maximum : QListView::Manual );
562   }
563   else if ( flag == WT_PyConsole )
564   {
565     delete wid;
566     wid = 0;
567     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
568     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
569     wid = pyCons;
570     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
571   }
572   return wid;
573 }
574
575 /*!Create preferences.*/
576 void SalomeApp_Application::createPreferences( LightApp_Preferences* pref )
577 {
578   LightApp_Application::createPreferences(pref);
579
580   if ( !pref )
581     return;
582
583   int salomeCat = pref->addPreference( tr( "PREF_CATEGORY_SALOME" ) );
584   int obTab = pref->addPreference( tr( "PREF_TAB_OBJBROWSER" ), salomeCat );
585   int defCols = pref->addPreference( tr( "PREF_GROUP_DEF_COLUMNS" ), obTab );
586   for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
587   {
588     pref->addPreference( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), defCols,
589                          LightApp_Preferences::Bool, "ObjectBrowser", QString().sprintf( "visibility_column_%d", i ) );
590   }
591   pref->setItemProperty( defCols, "columns", 1 );
592
593   int objSetGroup = pref->addPreference( tr( "PREF_OBJ_BROWSER_SETTINGS" ), obTab );
594   pref->addPreference( tr( "PREF_AUTO_SIZE" ), objSetGroup, LightApp_Preferences::Bool, "ObjectBrowser", "auto_size" );
595 }
596
597 /*!Update desktop title.*/
598 void SalomeApp_Application::updateDesktopTitle() {
599   QString aTitle = applicationName();
600   QString aVer = applicationVersion();
601   if ( !aVer.isEmpty() )
602     aTitle += QString( " " ) + aVer;
603
604   if ( activeStudy() )
605   {
606     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
607     if ( !sName.isEmpty() ) {
608       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
609       if ( study ) {
610         _PTR(Study) stdDS = study->studyDS();
611         if(stdDS) {
612           if ( stdDS->GetProperties()->IsLocked() ) {
613             aTitle += QString( " - [%1 (%2)]").arg( sName ).arg( tr( "STUDY_LOCKED" ) );
614           } else {
615             aTitle += QString( " - [%1]" ).arg( sName );
616           }
617         }
618       }
619     }
620   }
621
622   desktop()->setCaption( aTitle );
623 }
624
625 /*!Gets CORBA::ORB_var*/
626 CORBA::ORB_var SalomeApp_Application::orb()
627 {
628   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
629   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
630   return _orb;
631 }
632
633 /*!Create and return SALOMEDS_StudyManager.*/
634 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
635 {
636   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
637   return _sm;
638 }
639
640 /*!Create and return SALOME_NamingService.*/
641 SALOME_NamingService* SalomeApp_Application::namingService()
642 {
643   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
644   return _ns;
645 }
646
647 /*!Create and return SALOME_LifeCycleCORBA.*/
648 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
649 {
650   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
651   return _lcc;
652 }
653
654 /*!Return default engine IOR for light modules*/
655 QString SalomeApp_Application::defaultEngineIOR()
656 {
657   /// Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
658   QString anIOR( "" );
659   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
660   if ( !CORBA::is_nil( anEngine ) )
661     anIOR = orb()->object_to_string( anEngine );
662   return anIOR;
663 }
664
665 /*!Private SLOT. On preferences.*/
666 void SalomeApp_Application::onProperties()
667 {
668   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
669   if( !study )
670     return;
671
672   _PTR(StudyBuilder) SB = study->studyDS()->NewBuilder();
673   SB->NewCommand();
674
675   SalomeApp_StudyPropertiesDlg aDlg( desktop() );
676   int res = aDlg.exec();
677   if( res==QDialog::Accepted && aDlg.isChanged() )
678     SB->CommitCommand();
679   else
680     SB->AbortCommand();
681
682   //study->updateCaptions();
683   updateDesktopTitle();
684   updateActions();
685 }
686
687 /*!Insert items in popup, which necessary for current application*/
688 void SalomeApp_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
689 {
690   LightApp_Application::contextMenuPopup( type, thePopup, title );
691
692   OB_Browser* ob = objectBrowser();
693   if ( !ob || type != ob->popupClientType() )
694     return;
695
696   // Get selected objects
697   SALOME_ListIO aList;
698   LightApp_SelectionMgr* mgr = selectionMgr();
699   mgr->selectedObjects(aList);
700
701   // "Delete reference" item should appear only for invalid references
702
703   // Check if selected objects is invalid references
704   bool isInvalidRefs = true;
705
706   if ( aList.Extent() < 1 )
707     isInvalidRefs = false;
708
709   if ( isInvalidRefs )
710     {
711       SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(activeStudy());
712       _PTR(Study) aStudyDS = aStudy->studyDS();
713       _PTR(SObject) anObj;
714
715       for ( SALOME_ListIteratorOfListIO it( aList ); it.More() && isInvalidRefs; it.Next() )
716         {
717           if ( it.Value()->hasEntry() )
718             {
719               _PTR(SObject) aSObject = aStudyDS->FindObjectID( it.Value()->getEntry() );
720               SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(aSObject.get());
721               if( aSO )
722                 if ( aSObject->ReferencedObject(anObj) == false || !QString(anObj->GetName().c_str()).isEmpty() )
723                   isInvalidRefs = false;
724             }
725         }
726     }
727
728   // Add "Delete refrence" item to popup
729   if ( isInvalidRefs )
730     {
731       thePopup->insertSeparator();
732       thePopup->insertItem( tr( "MEN_DELETE_REFERENCE" ), this, SLOT( onDeleteReferences() ) );
733       return;
734     }
735
736   // "Activate module" item should appear only if it's necessary
737   if (aList.Extent() != 1)
738     return;
739   Handle(SALOME_InteractiveObject) aIObj = aList.First();
740   QString aModuleName(aIObj->getComponentDataType());
741   QString aModuleTitle = moduleTitle(aModuleName);
742   CAM_Module* currentModule = activeModule();
743   if (currentModule && currentModule->moduleName() == aModuleTitle)
744     return;
745   thePopup->insertItem( tr( "MEN_OPENWITH" ), this, SLOT( onOpenWith() ) );
746
747 }
748
749 /*!Update obect browser:
750  1.if 'updateModels' true, update existing data models;
751  2. update "non-existing" (not loaded yet) data models;
752  3. update object browser if it existing */
753 void SalomeApp_Application::updateObjectBrowser( const bool updateModels )
754 {
755   // update existing data models (already loaded SComponents)
756   LightApp_Application::updateObjectBrowser(updateModels);
757
758   // update "non-existing" (not loaded yet) data models
759   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
760   if ( study )
761   {
762     _PTR(Study) stdDS = study->studyDS();
763     if( stdDS )
764     {
765       for ( _PTR(SComponentIterator) it ( stdDS->NewComponentIterator() ); it->More(); it->Next() ) 
766       {
767         _PTR(SComponent) aComponent ( it->Value() );
768
769         if ( aComponent->ComponentDataType() == "Interface Applicative" )
770           continue; // skip the magic "Interface Applicative" component
771
772         SalomeApp_DataModel::BuildTree( aComponent, study->root(), study, /*skipExisitng=*/true );
773       }
774     }
775   }
776
777   if ( objectBrowser() )
778   {
779     objectBrowser()->updateGeometry();
780     objectBrowser()->updateTree();
781   }
782 }
783
784 /*!Display Catalog Genenerator dialog */
785 void SalomeApp_Application::onCatalogGen()
786 {
787   ToolsGUI_CatalogGeneratorDlg aDlg( desktop() );
788   aDlg.exec();
789 }
790
791 /*!Display Registry Display dialog */
792 void SalomeApp_Application::onRegDisplay()
793 {
794   CORBA::ORB_var anOrb = orb();
795   ToolsGUI_RegWidget* regWnd = ToolsGUI_RegWidget::GetRegWidget( anOrb, desktop(), "Registry" );
796   regWnd->show();
797   regWnd->raise();
798   regWnd->setActiveWindow();
799 }
800
801 /*!find original object by double click on item */
802 void SalomeApp_Application::onDblClick( QListViewItem* it )
803 {
804   OB_ListItem* item = dynamic_cast<OB_ListItem*>( it );
805   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
806
807   if( study && item )
808   {
809     SalomeApp_DataObject* obj = dynamic_cast<SalomeApp_DataObject*>( item->dataObject() );
810     if( !obj )
811       return;
812
813     QString entry = obj->entry();
814     _PTR(SObject) sobj = study->studyDS()->FindObjectID( entry.latin1() ), ref;
815
816     if( sobj && sobj->ReferencedObject( ref ) )
817     {
818       entry = ref->GetID().c_str();
819       QListViewItemIterator anIt( item->listView() );
820       for( ; anIt.current(); anIt++ )
821       {
822         OB_ListItem* item = dynamic_cast<OB_ListItem*>( anIt.current() );
823         if( !item )
824           continue;
825
826         SalomeApp_DataObject* original = dynamic_cast<SalomeApp_DataObject*>( item->dataObject() );
827         if( original->entry()!=entry )
828           continue;
829
830         OB_Browser* br = objectBrowser();
831         br->setSelected( original );
832         SUIT_DataObject* p = original->parent();
833         while( p )
834         {
835           br->setOpen( p );
836           p = p->parent();
837         }
838         break;
839       }
840     }
841   }
842 }