1 // Copyright (C) 2005-2013 OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // LIGHT : sample (no-corba-engine) SALOME module
21 // File : LIGHTGUI.cxx
22 // Author : Julia DOROVSKIKH
26 #include <LIGHT_version.h>
28 #include "LIGHTGUI_Selection.h"
29 #include "LIGHTGUI_DataModel.h"
30 #include "LightApp_DataOwner.h"
31 #include "LIGHTGUI_TextPrs.hxx"
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_Desktop.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_Selection.h>
38 #include <OCCViewer_ViewManager.h>
39 #include <SOCC_ViewModel.h>
42 #include <QInputDialog>
43 #include <QStringList>
50 \brief Implementation of the sample light (no-CORBA-engine)
55 \brief Constructor. Sets the default name for the module.
58 : LightApp_Module( "LIGHTGUI" )
70 \brief Initialize module. Creates menus, prepares context menu, etc.
71 \param app application instance
73 void LIGHTGUI::initialize( CAM_Application* app )
75 LightApp_Module::initialize( app );
77 SUIT_Desktop* desk = application()->desktop();
79 createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIcon(), tr( "MEN_LOAD_FILE" ),
80 tr( "STB_LOAD_FILE" ), 0, desk, false, this, SLOT( onLoadFile() ) );
81 createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIcon(), tr( "MEN_DISPLAY_LINE" ),
82 tr( "STB_DISPLAY_LINE" ), 0, desk, false, this, SLOT( onDisplayLine() ) );
83 createAction( lgEraseLine, tr( "TOP_ERASE_LINE" ), QIcon(), tr( "MEN_ERASE_LINE" ),
84 tr( "STB_ERASE_LINE" ), 0, desk, false, this, SLOT( onEraseLine() ) );
85 createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIcon(), tr( "MEN_SAVE_FILE" ),
86 tr( "STB_SAVE_FILE" ), 0, desk, false, this, SLOT( onSaveFile() ) );
87 createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIcon(), tr( "MEN_EDIT_LINE" ),
88 tr( "STB_EDIT_LINE" ), 0, desk, false, this, SLOT( onEditLine() ) );
89 createAction( lgAddLine, tr( "TOP_ADD_LINE" ), QIcon(), tr( "MEN_ADD_LINE" ),
90 tr( "STB_ADD_LINE" ), 0, desk, false, this, SLOT( onAddLine() ) );
91 createAction( lgDelLine, tr( "TOP_DEL_LINE" ), QIcon(), tr( "MEN_DEL_LINE" ),
92 tr( "STB_DEL_LINE" ), 0, desk, false, this, SLOT( onDelLine() ) );
93 createAction( lgClear, tr( "TOP_CLEAR_ALL" ), QIcon(), tr( "MEN_CLEAR_ALL" ),
94 tr( "STB_CLEAR_ALL" ), 0, desk, false, this, SLOT( onClear() ) );
96 int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
97 createMenu( separator(), aFileMnu, -1, 10 );
98 createMenu( lgLoadFile, aFileMnu, 10 );
99 createMenu( lgSaveFile, aFileMnu, 10 );
101 int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
102 createMenu( lgAddLine, aLightMnu, 10 );
103 createMenu( lgEditLine, aLightMnu, 10 );
104 createMenu( lgDelLine, aLightMnu, 10 );
105 createMenu( separator(), aLightMnu, -1, 10 );
106 createMenu( lgClear, aLightMnu, 10 );
108 QString rule = "(client='ObjectBrowser' or client='OCCViewer') and selcount=1 and type='TextLine' and !empty";
110 popupMgr()->insert ( action( lgDisplayLine ), -1, 0 );
111 popupMgr()->setRule( action( lgDisplayLine ), rule + " and !visible" );
113 popupMgr()->insert ( action( lgEraseLine ), -1, 0 );
114 popupMgr()->setRule( action( lgEraseLine ), rule + " and activeView='OCCViewer' and visible" );
116 rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
118 popupMgr()->insert ( action( lgEditLine ), -1, 0 );
119 popupMgr()->setRule( action( lgEditLine ), rule );
121 popupMgr()->insert ( action( lgAddLine ), -1, 0 );
122 popupMgr()->setRule( action( lgAddLine ), rule );
124 popupMgr()->insert ( separator(), -1, 0 );
126 popupMgr()->insert ( action( lgDelLine ), -1, 0 );
127 popupMgr()->setRule( action( lgDelLine ), rule );
129 rule = "client='ObjectBrowser'";
131 popupMgr()->insert ( action( lgClear ), -1, 0 );
132 popupMgr()->setRule( action( lgClear ), rule );
136 \brief Get list of compliant dockable GUI elements
137 \param m map to be filled in ("type":"default_position")
139 void LIGHTGUI::windows( QMap<int, int>& m ) const
141 m.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
142 m.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
146 \brief Create custom popup menu selection object.
147 \return new selected object
149 LightApp_Selection* LIGHTGUI::createSelection() const
151 return new LIGHTGUI_Selection();
155 \brief Create data model.
156 \return module specific data model
158 CAM_DataModel* LIGHTGUI::createDataModel()
160 return new LIGHTGUI_DataModel( this );
164 \brief Get the identifier of the currently selected object.
165 \return ID of the currently selected line or -1 if not appropriate
166 object (or multiple objects) is selected.
168 int LIGHTGUI::selectedLine()
170 int id = -1; // bad value
172 // Look for selected lines
173 LightApp_Application* app = getApp();
174 LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : 0;
176 SUIT_DataOwnerPtrList anOwnersList;
177 mgr->selected( anOwnersList );
179 // Get index of the single selected line
180 if ( anOwnersList.size() == 1 ) {
181 const LightApp_DataOwner* owner =
182 dynamic_cast<const LightApp_DataOwner*>( anOwnersList[0].get() );
183 QString anEntry = owner->entry();
184 id = LIGHTGUI_DataModel::id( anEntry );
191 \brief Display the object with the specified identifier.
193 \param allViewers if \c true the object is displayed in all existing viewers
194 (OCC viewers are supported only)
195 \param upd if \c true, update the viewer(s)
197 void LIGHTGUI::displayLine( const int id, const bool allViewers, const bool upd )
199 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
200 if ( dm && id > 0 ) {
201 ViewManagerList viewers;
203 getApp()->viewManagers( "OCCViewer", viewers );
205 viewers.append( getApp()->getViewManager( "OCCViewer", true ) );
207 for ( ViewManagerList::iterator it = viewers.begin(); it != viewers.end(); ++it ) {
208 OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( *it );
209 if ( !aMgr ) continue;
210 SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();
211 if ( !aViewer ) continue;
212 QString aLine = dm->getLineText( id );
213 QString entry = LIGHTGUI_DataModel::entry( id );
214 SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs( entry.toLatin1() ) );
216 double aX = 0, aY = dm->lineNb( id ) * LIGHTGUI_TextPrs::TextSize(), aZ = 0;
217 aViewer->Erase( prs, true );
218 Handle(LIGHTGUI_TextPrs) aPrs = new LIGHTGUI_TextPrs( aLine.toLatin1(), gp_Pnt( aX, aY, aZ ) );
219 aPrs->SetOwner( new SALOME_InteractiveObject( entry.toLatin1(), "" ) );
221 prs->AddObject( aPrs );
222 aViewer->Display( prs );
231 \brief Erase the object with the specified identifier.
233 \param allViewers if \c true the object is erased in all existing viewers
234 (OCC viewers are supported only)
235 \param upd if \c true, update the viewer(s)
237 void LIGHTGUI::eraseLine( const int id, const bool allViewers, const bool upd )
239 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
240 if ( dm && id > 0 ) {
241 ViewManagerList viewers;
243 getApp()->viewManagers( "OCCViewer", viewers );
245 viewers.append( getApp()->getViewManager( "OCCViewer", true ) );
247 for ( ViewManagerList::iterator it = viewers.begin(); it != viewers.end(); ++it ) {
248 OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( *it );
249 if ( !aMgr ) continue;
250 SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();
251 if ( !aViewer ) continue;
252 QString entry = LIGHTGUI_DataModel::entry( id );
253 SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs( entry.toLatin1() ) );
255 aViewer->Erase( prs, false );
264 \brief Update the object presentation.
266 \param allViewers if \c true the object is updated in all existing viewers
267 (OCC viewers are supported only)
268 \param upd if \c true, update the viewer(s)
270 void LIGHTGUI::updateLine( const int id, const bool allViewers, const bool upd )
272 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
273 if ( dm && id > 0 ) {
274 ViewManagerList viewers;
276 getApp()->viewManagers( "OCCViewer", viewers );
278 viewers.append( getApp()->getViewManager( "OCCViewer", true ) );
280 for ( ViewManagerList::iterator it = viewers.begin(); it != viewers.end(); ++it ) {
281 OCCViewer_ViewManager* aMgr = dynamic_cast<OCCViewer_ViewManager*>( *it );
282 if ( !aMgr ) continue;
283 SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();
284 if ( !aViewer ) continue;
285 QString aLine = dm->getLineText( id );
286 QString entry = LIGHTGUI_DataModel::entry( id );
287 if ( aViewer->isVisible( new SALOME_InteractiveObject( entry.toLatin1(), "" ) ) ) {
288 SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs( entry.toLatin1() ) );
290 if ( aLine.isEmpty() ) {
291 aViewer->Erase( prs, false );
294 double aX = 0, aY = dm->lineNb( id ) * LIGHTGUI_TextPrs::TextSize(), aZ = 0;
295 aViewer->Erase( prs, true );
296 Handle(LIGHTGUI_TextPrs) aPrs = new LIGHTGUI_TextPrs( aLine.toLatin1(), gp_Pnt( aX, aY, aZ ) );
297 aPrs->SetOwner( new SALOME_InteractiveObject( entry.toLatin1(), "" ) );
299 prs->AddObject( aPrs );
300 aViewer->Display( prs );
311 \brief Erase objects.
312 \param l objects IDs list
313 \param allViewers if \c true the objects are erased in all existing viewers
314 (OCC viewers are supported only)
316 void LIGHTGUI::eraseLines( const QList<int>& l, const bool allViewers )
318 for ( QList<int>::const_iterator it = l.begin(); it != l.end(); ++it )
319 eraseLine( *it, allViewers );
323 \brief Update objects presentations.
324 \param l objects IDs list
325 \param allViewers if \c true the objects are updated in all existing viewers
326 (OCC viewers are supported only)
328 void LIGHTGUI::updateLines( const QList<int>& l, const bool allViewers )
330 for ( QList<int>::const_iterator it = l.begin(); it != l.end(); ++it )
331 updateLine( *it, allViewers );
335 \brief Activate module.
336 \param study current study
337 \return \c true if activaion is done successfully or 0 to prevent
340 bool LIGHTGUI::activateModule( SUIT_Study* study )
342 bool isDone = LightApp_Module::activateModule( study );
343 if ( !isDone ) return false;
345 setMenuShown( true );
351 \brief Deactivate module.
352 \param study current study
353 \return \c true if deactivaion is done successfully or 0 to prevent
354 deactivation on error
356 bool LIGHTGUI::deactivateModule( SUIT_Study* study )
359 setMenuShown( false );
361 return LightApp_Module::deactivateModule( study );
365 \brief Called when "load text file" action is activated.
367 void LIGHTGUI::onLoadFile()
369 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
372 QStringList filtersList;
373 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
374 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
376 // Select a file to be loaded
377 QString aFileName = getApp()->getFileName( true, QString(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
378 if ( !aFileName.isEmpty() ) {
380 QList<int> l = dm->getIds();
381 if ( dm->loadFile( aFileName ) ) {
382 eraseLines( l, true );
383 updateObjBrowser( true );
386 SUIT_MessageBox::warning ( application()->desktop(),
388 tr( "WRN_LOAD_FAILED" ) );
394 \brief Called when "save text file" action is activated.
396 void LIGHTGUI::onSaveFile()
398 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
401 QStringList filtersList;
402 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
403 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
405 // Select a file name to dump the lines into
406 QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
407 if ( !aFileName.isEmpty() ) {
409 if ( !dm->dumpFile( aFileName ) ) {
410 SUIT_MessageBox::warning ( application()->desktop(),
412 tr( "WRN_DUMP_FAILED" ) );
418 \brief Called when "display selected line" action is activated.
420 void LIGHTGUI::onDisplayLine()
422 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
426 int id = selectedLine();
427 QString aLine = dm->getLineText( id );
429 // Check, that position is defined
430 if ( id == -1 || aLine.isEmpty() ) {
431 SUIT_MessageBox::warning ( application()->desktop(),
433 tr( "WRN_SELECT_LINE" ) );
440 \brief Called when "erase selected line" action is activated.
442 void LIGHTGUI::onEraseLine()
444 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
448 int id = selectedLine();
449 QString aLine = dm->getLineText( id );
451 // Check, that position is defined
452 if ( id == -1 || aLine.isEmpty() ) {
453 SUIT_MessageBox::warning ( application()->desktop(),
455 tr( "WRN_SELECT_LINE" ) );
462 \brief Called when "edit selected line" action is activated.
464 void LIGHTGUI::onEditLine()
466 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
470 int id = selectedLine();
472 // Check, that position is defined
474 SUIT_MessageBox::warning ( application()->desktop(),
476 tr( "WRN_SELECT_LINE" ) );
482 QString aText = QInputDialog::getText( application()->desktop(),
483 tr( "LIGHTGUI_EDIT_LINE" ),
486 dm->getLineText( id ),
490 // try to change a text of the selected line
492 isOk = dm->setLineText( id, aText );
495 updateLine( id, true );
496 updateObjBrowser( true );
499 SUIT_MessageBox::warning ( application()->desktop(),
501 tr( "WRN_EDIT_FAILED" ) );
506 \brief Called when "insert new line" action is activated.
508 void LIGHTGUI::onAddLine()
510 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
514 int id = selectedLine();
518 QString aText = QInputDialog::getText( getApp()->desktop(),
519 tr( "LIGHTGUI_ADD_LINE" ),
526 QList<int> l = dm->getIds();
528 // try to insert/add text line
529 isOk = dm->insertLineBefore( id, aText );
532 updateLines( l, true );
533 updateObjBrowser( true );
536 SUIT_MessageBox::warning ( application()->desktop(),
538 tr( "WRN_ADD_FAILED" ) );
543 \brief Called when "delete selected line" action is activated.
545 void LIGHTGUI::onDelLine()
547 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
551 int id = selectedLine();
553 // Check, that position is defined
555 SUIT_MessageBox::warning ( application()->desktop(),
557 tr( "WRN_SELECT_LINE" ) );
561 QList<int> l = dm->getIds();
563 // try to delete line
564 bool isOk = dm->deleteTextLine( id );
566 updateLines( l, true );
567 updateObjBrowser( true );
570 SUIT_MessageBox::warning ( application()->desktop(),
572 tr( "WRN_DELETE_FAILED" ) );
577 \brief Called when "clear all contents" action is activated.
579 void LIGHTGUI::onClear()
581 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
584 QList<int> l = dm->getIds();
585 eraseLines( l, true );
589 updateObjBrowser( true );
593 \fn CAM_Module* createModule();
594 \brief Export module instance (factory function).
595 \return new created instance of the module
599 #define LIGHTGUI_EXPORT __declspec(dllexport)
601 #define LIGHTGUI_EXPORT
605 LIGHTGUI_EXPORT CAM_Module* createModule() {
606 return new LIGHTGUI();
609 LIGHTGUI_EXPORT char* getModuleVersion() {
610 return (char*)LIGHT_VERSION_STR;