1 // LIGHT : sample (no-corba-engine) SALOME module
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 // Author : Julia DOROVSKIKH
30 #include "LIGHTGUI_Selection.h"
31 #include "LIGHTGUI_DataModel.h"
32 #include "LightApp_DataOwner.h"
34 #include <SUIT_MessageBox.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include "LightApp_Selection.h"
39 #include <qinputdialog.h>
40 #include <qstringlist.h>
42 #include <LIGHTGUI_TextPrs.hxx>
43 #include <OCCViewer_ViewManager.h>
44 #include <SOCC_ViewModel.h>
47 //=================================================================================
48 // function : LIGHTGUI()
49 // purpose : Constructor : sets the default name for the module
50 //=================================================================================
52 : LightApp_Module( "LIGHTGUI" )
56 //=================================================================================
57 // function : initialize()
58 // purpose : module intitialization: creates menus, prepares context menu, etc.
59 //=================================================================================
60 void LIGHTGUI::initialize ( CAM_Application* app )
62 LightApp_Module::initialize( app );
64 QWidget* parent = application()->desktop();
66 createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIconSet(), tr( "MEN_LOAD_FILE" ),
67 tr( "STB_LOAD_FILE" ), 0, parent, false, this, SLOT( onLoadFile() ) );
68 createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIconSet(), tr( "MEN_DISPLAY_LINE" ),
69 tr( "STB_DISPLAY_LINE" ), 0, parent, false, this, SLOT( onDisplayLine() ) );
70 createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIconSet(), tr( "MEN_SAVE_FILE" ),
71 tr( "STB_SAVE_FILE" ), 0, parent, false, this, SLOT( onSaveFile() ) );
72 createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIconSet(), tr( "MEN_EDIT_LINE" ),
73 tr( "STB_EDIT_LINE" ), 0, parent, false, this, SLOT( onEditLine() ) );
74 createAction( lgAddLine, tr( "TOP_ADD_LINE" ), QIconSet(), tr( "MEN_ADD_LINE" ),
75 tr( "STB_ADD_LINE" ), 0, parent, false, this, SLOT( onAddLine() ) );
76 createAction( lgDelLine, tr( "TOP_DEL_LINE" ), QIconSet(), tr( "MEN_DEL_LINE" ),
77 tr( "STB_DEL_LINE" ), 0, parent, false, this, SLOT( onDelLine() ) );
78 createAction( lgClear, tr( "TOP_CLEAR_ALL" ), QIconSet(), tr( "MEN_CLEAR_ALL" ),
79 tr( "STB_CLEAR_ALL" ), 0, parent, false, this, SLOT( onClear() ) );
81 int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
82 createMenu( separator(), aFileMnu, -1, 10 );
83 createMenu( lgLoadFile, aFileMnu, 10 );
84 createMenu( lgSaveFile, aFileMnu, 10 );
86 int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
87 createMenu( lgDisplayLine, aLightMnu, 10 );
88 createMenu( lgEditLine, aLightMnu, 10 );
89 createMenu( lgAddLine, aLightMnu, 10 );
90 createMenu( separator(), aLightMnu, -1, 10 );
91 createMenu( lgDelLine, aLightMnu, 10 );
92 createMenu( lgClear, aLightMnu, 10 );
94 // popup for object browser
96 QString rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
98 popupMgr()->insert ( action( lgDisplayLine ), parentId, 0 );
99 popupMgr()->setRule( action( lgDisplayLine ), rule, true );
101 popupMgr()->insert ( action( lgEditLine ), parentId, 0 );
102 popupMgr()->setRule( action( lgEditLine ), rule, true );
104 popupMgr()->insert ( action( lgAddLine ), parentId, 0 );
105 popupMgr()->setRule( action( lgAddLine ), rule, true );
107 popupMgr()->insert ( separator(), parentId, 0 );
109 popupMgr()->insert ( action( lgDelLine ), parentId, 0 );
110 popupMgr()->setRule( action( lgDelLine ), rule, true );
112 popupMgr()->insert ( action( lgClear ), parentId, 0 );
115 //=================================================================================
116 // function : iconName()
117 // purpose : Module icon name
118 //=================================================================================
119 QString LIGHTGUI::iconName() const
121 return QObject::tr( "ICON_LIGHT" );
124 //=================================================================================
125 // function : contextMenuPopup()
126 // purpose : defines context popup menu
127 //=================================================================================
128 void LIGHTGUI::contextMenuPopup ( const QString& client, QPopupMenu* menu, QString& str)
130 LIGHTGUI_Selection* sel = new LIGHTGUI_Selection();
131 sel->init( client, getApp()->selectionMgr() );
132 popupMgr()->updatePopup( menu, sel );
136 //=================================================================================
137 // function : windows()
138 // purpose : gets a list of compliant dockable GUI elements
139 //=================================================================================
140 void LIGHTGUI::windows ( QMap<int, int>& aMap ) const
142 aMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::DockLeft );
143 aMap.insert( LightApp_Application::WT_PyConsole, Qt::DockBottom );
146 //=================================================================================
147 // function : createDataModel()
148 // purpose : create data model
149 //=================================================================================
150 CAM_DataModel* LIGHTGUI::createDataModel()
152 return new LIGHTGUI_DataModel( this );
155 //=================================================================================
156 // function : selectedLine()
157 // purpose : get the number of selected line (multiple selection is not supported)
158 //=================================================================================
159 int LIGHTGUI::selectedLine()
161 int aPosition = 0; // bad value
163 // Look for selected lines
164 LightApp_Application* app = getApp();
165 LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
167 SUIT_DataOwnerPtrList anOwnersList;
168 mgr->selected( anOwnersList );
170 // Get index of the single selected line
171 if ( anOwnersList.size() == 1 ) {
172 const LightApp_DataOwner* owner =
173 dynamic_cast<const LightApp_DataOwner*>( anOwnersList[0].get() );
174 QString anEntry = owner->entry();
175 int anIndex = anEntry.find("_");
176 aPosition = (anEntry.mid(anIndex+1, anEntry.length() - anIndex)).toInt();//lineNb();
182 //=================================================================================
183 // function : activateModule()
184 // purpose : module's activation
185 //=================================================================================
186 bool LIGHTGUI::activateModule ( SUIT_Study* study )
188 bool isDone = LightApp_Module::activateModule( study );
189 if ( !isDone ) return false;
191 setMenuShown( true );
196 //=================================================================================
197 // function : deactivateModule()
198 // purpose : module's deactivation
199 //=================================================================================
200 bool LIGHTGUI::deactivateModule ( SUIT_Study* study )
203 setMenuShown( false );
205 return LightApp_Module::deactivateModule( study );
208 //=================================================================================
209 // function : onLoadFile()
210 // purpose : "Load File" action slot
211 //=================================================================================
212 void LIGHTGUI::onLoadFile()
214 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
217 QStringList filtersList;
218 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
219 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
221 // Select a file to be loaded
222 QString aFileName = getApp()->getFileName( true, QString::null, filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
223 if ( !aFileName.isEmpty() ) {
225 if ( dm->loadFile( aFileName ) ) {
226 updateObjBrowser( true );
229 SUIT_MessageBox::warn1 ( application()->desktop(),
231 tr( "WRN_LOAD_FAILED" ),
237 //=================================================================================
238 // function : onSaveFile()
239 // purpose : "Save File" action slot
240 //=================================================================================
241 void LIGHTGUI::onSaveFile()
243 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
246 QStringList filtersList;
247 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
248 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
250 // Select a file name to dump the lines into
251 QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
252 if ( !aFileName.isEmpty() ) {
254 if ( !dm->dumpFile( aFileName ) ) {
255 SUIT_MessageBox::warn1 ( application()->desktop(),
257 tr( "WRN_DUMP_FAILED" ),
263 //=================================================================================
264 // function : onDisplayLine()
265 // purpose : "Display Line" action slot
266 //=================================================================================
267 void LIGHTGUI::onDisplayLine()
269 OCCViewer_ViewManager* aMgr = (OCCViewer_ViewManager*)getApp()->getViewManager("OCCViewer", true);
270 SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();
274 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
276 int aPosition = selectedLine();
277 QString aLine = dm->getLineText( aPosition );
278 double aX = 0, aY = 0, aZ = 0;
279 QString aStrId = QString("LIGHTGUI_%1").arg(aPosition);
281 Handle(LIGHTGUI_TextPrs) aPrs = new LIGHTGUI_TextPrs( aLine.isNull() ? "" : aLine, gp_Pnt( aX, aY, aZ ) );
282 aPrs->SetOwner(new SALOME_InteractiveObject( aStrId, "" ) );
283 SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs() ); // aStringID is an "entry"
286 prs->AddObject( aPrs );
287 aViewer->Display( prs );
292 //=================================================================================
293 // function : onEditLine()
294 // purpose : "Edit Line" action slot
295 //=================================================================================
296 void LIGHTGUI::onEditLine()
298 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
302 int aPosition = selectedLine();
304 // Check, that position is defined
306 SUIT_MessageBox::warn1 ( application()->desktop(),
308 tr( "WRN_SELECT_LINE" ),
315 QString aText = QInputDialog::getText( tr( "LIGHTGUI_EDIT_LINE" ),
318 dm->getLineText( aPosition ),
320 getApp()->desktop() );
323 // try to change a text of the selected line
324 isOk = dm->setLineText( aPosition, aText );
326 updateObjBrowser( true );
329 SUIT_MessageBox::warn1 ( application()->desktop(),
331 tr( "WRN_EDIT_FAILED" ),
336 //=================================================================================
337 // function : onAddLine()
338 // purpose : "Insert Line" action slot
339 //=================================================================================
340 void LIGHTGUI::onAddLine()
342 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
346 int aPosition = selectedLine();
350 QString aText = QInputDialog::getText( tr( "LIGHTGUI_ADD_LINE" ),
355 getApp()->desktop() );
358 // try to insert/add text line
359 isOk = dm->insertLineBefore( aPosition, aText );
362 updateObjBrowser( true );
365 SUIT_MessageBox::warn1 ( application()->desktop(),
367 tr( "WRN_ADD_FAILED" ),
372 //=================================================================================
373 // function : onDelLine()
374 // purpose : "Delete Line" action slot
375 //=================================================================================
376 void LIGHTGUI::onDelLine()
378 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
382 int aPosition = selectedLine();
384 // Check, that position is defined
386 SUIT_MessageBox::warn1 ( application()->desktop(),
388 tr( "WRN_SELECT_LINE" ),
393 // try to delete line
394 bool isOk = dm->deleteTextLine( aPosition );
396 updateObjBrowser( true );
399 SUIT_MessageBox::warn1 ( application()->desktop(),
401 tr( "WRN_DELETE_FAILED" ),
406 //=================================================================================
407 // function : onClear()
408 // purpose : "Clear Text" action slot
409 //=================================================================================
410 void LIGHTGUI::onClear()
412 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
416 updateObjBrowser( true );
419 //=================================================================================
420 // function : createModule()
421 // purpose : exports module object
422 //=================================================================================
424 #define LIGHTGUI_EXPORT __declspec(dllexport)
426 #define LIGHTGUI_EXPORT
430 LIGHTGUI_EXPORT CAM_Module* createModule() {
431 return new LIGHTGUI();