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( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIconSet(), tr( "MEN_SAVE_FILE" ),
69 tr( "STB_SAVE_FILE" ), 0, parent, false, this, SLOT( onSaveFile() ) );
70 createAction( lgDisplayLine, tr( "TOP_DISPLAY_LINE" ), QIconSet(), tr( "MEN_DISPLAY_LINE" ),
71 tr( "STB_DISPLAY_LINE" ), 0, parent, false, this, SLOT( onDisplayLine() ) );
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 : contextMenuPopup()
117 // purpose : defines context popup menu
118 //=================================================================================
119 void LIGHTGUI::contextMenuPopup ( const QString& client, QPopupMenu* menu, QString& str)
121 LIGHTGUI_Selection* sel = new LIGHTGUI_Selection();
122 sel->init( client, getApp()->selectionMgr() );
123 popupMgr()->updatePopup( menu, sel );
127 //=================================================================================
128 // function : windows()
129 // purpose : gets a list of compliant dockable GUI elements
130 //=================================================================================
131 void LIGHTGUI::windows ( QMap<int, int>& aMap ) const
133 aMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::DockLeft );
134 aMap.insert( LightApp_Application::WT_PyConsole, Qt::DockBottom );
137 //=================================================================================
138 // function : createDataModel()
139 // purpose : create data model
140 //=================================================================================
141 CAM_DataModel* LIGHTGUI::createDataModel()
143 return new LIGHTGUI_DataModel( this );
146 //=================================================================================
147 // function : selectedLine()
148 // purpose : get the number of selected line (multiple selection is not supported)
149 //=================================================================================
150 int LIGHTGUI::selectedLine()
152 int aPosition = 0; // bad value
154 // Look for selected lines
155 LightApp_Application* app = getApp();
156 LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
158 SUIT_DataOwnerPtrList anOwnersList;
159 mgr->selected( anOwnersList );
161 // Get index of the single selected line
162 if ( anOwnersList.size() == 1 ) {
163 const LightApp_DataOwner* owner =
164 dynamic_cast<const LightApp_DataOwner*>( anOwnersList[0].get() );
165 QString anEntry = owner->entry();
166 int anIndex = anEntry.find("_");
167 aPosition = (anEntry.mid(anIndex+1, anEntry.length() - anIndex)).toInt();//lineNb();
173 //=================================================================================
174 // function : activateModule()
175 // purpose : module's activation
176 //=================================================================================
177 bool LIGHTGUI::activateModule ( SUIT_Study* study )
179 bool isDone = LightApp_Module::activateModule( study );
180 if ( !isDone ) return false;
182 setMenuShown( true );
187 //=================================================================================
188 // function : deactivateModule()
189 // purpose : module's deactivation
190 //=================================================================================
191 bool LIGHTGUI::deactivateModule ( SUIT_Study* study )
194 setMenuShown( false );
196 return LightApp_Module::deactivateModule( study );
199 //=================================================================================
200 // function : onLoadFile()
201 // purpose : "Load File" action slot
202 //=================================================================================
203 void LIGHTGUI::onLoadFile()
205 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
208 QStringList filtersList;
209 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
210 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
212 // Select a file to be loaded
213 QString aFileName = getApp()->getFileName( true, QString::null, filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
214 if ( !aFileName.isEmpty() ) {
216 if ( dm->loadFile( aFileName ) ) {
217 updateObjBrowser( true );
220 SUIT_MessageBox::warn1 ( application()->desktop(),
222 tr( "WRN_LOAD_FAILED" ),
228 //=================================================================================
229 // function : onSaveFile()
230 // purpose : "Save File" action slot
231 //=================================================================================
232 void LIGHTGUI::onSaveFile()
234 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
237 QStringList filtersList;
238 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
239 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
241 // Select a file name to dump the lines into
242 QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
243 if ( !aFileName.isEmpty() ) {
245 if ( !dm->dumpFile( aFileName ) ) {
246 SUIT_MessageBox::warn1 ( application()->desktop(),
248 tr( "WRN_DUMP_FAILED" ),
253 //=================================================================================
254 // function : onDisplayLine()
255 // purpose : "Display Line" action slot
256 //=================================================================================
257 void LIGHTGUI::onDisplayLine()
259 OCCViewer_ViewManager* aMgr = (OCCViewer_ViewManager*)getApp()->getViewManager("OCCViewer", true);
260 SOCC_Viewer* aViewer = (SOCC_Viewer*)aMgr->getViewModel();
264 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
266 int aPosition = selectedLine();
267 QString aLine = dm->getLineText( aPosition );
268 double aX = 0, aY = 0, aZ = 0;
269 QString aStrId = QString("LIGHTGUI_%1").arg(aPosition);
271 Handle(LIGHTGUI_TextPrs) aPrs = new LIGHTGUI_TextPrs( aLine, gp_Pnt( aX, aY, aZ ) );
272 aPrs->SetOwner(new SALOME_InteractiveObject( aStrId, "" ) );
273 SOCC_Prs* prs = dynamic_cast<SOCC_Prs*>( aViewer->CreatePrs() ); // aStringID is an "entry"
276 prs->AddObject( aPrs );
277 aViewer->Display( prs );
279 getApp()->updateObjectBrowser();
283 //=================================================================================
284 // function : onEditLine()
285 // purpose : "Edit Line" action slot
286 //=================================================================================
287 void LIGHTGUI::onEditLine()
289 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
293 int aPosition = selectedLine();
295 // Check, that position is defined
297 SUIT_MessageBox::warn1 ( application()->desktop(),
299 tr( "WRN_SELECT_LINE" ),
306 QString aText = QInputDialog::getText( tr( "LIGHTGUI_EDIT_LINE" ),
309 dm->getLineText( aPosition ),
311 getApp()->desktop() );
314 // try to change a text of the selected line
315 isOk = dm->setLineText( aPosition, aText );
317 updateObjBrowser( true );
320 SUIT_MessageBox::warn1 ( application()->desktop(),
322 tr( "WRN_EDIT_FAILED" ),
327 //=================================================================================
328 // function : onAddLine()
329 // purpose : "Insert Line" action slot
330 //=================================================================================
331 void LIGHTGUI::onAddLine()
333 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
337 int aPosition = selectedLine();
341 QString aText = QInputDialog::getText( tr( "LIGHTGUI_ADD_LINE" ),
346 getApp()->desktop() );
349 // try to insert/add text line
350 isOk = dm->insertLineBefore( aPosition, aText );
353 updateObjBrowser( true );
356 SUIT_MessageBox::warn1 ( application()->desktop(),
358 tr( "WRN_ADD_FAILED" ),
363 //=================================================================================
364 // function : onDelLine()
365 // purpose : "Delete Line" action slot
366 //=================================================================================
367 void LIGHTGUI::onDelLine()
369 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
373 int aPosition = selectedLine();
375 // Check, that position is defined
377 SUIT_MessageBox::warn1 ( application()->desktop(),
379 tr( "WRN_SELECT_LINE" ),
384 // try to delete line
385 bool isOk = dm->deleteTextLine( aPosition );
387 updateObjBrowser( true );
390 SUIT_MessageBox::warn1 ( application()->desktop(),
392 tr( "WRN_DELETE_FAILED" ),
397 //=================================================================================
398 // function : onClear()
399 // purpose : "Clear Text" action slot
400 //=================================================================================
401 void LIGHTGUI::onClear()
403 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
407 updateObjBrowser( true );
410 //=================================================================================
411 // function : createModule()
412 // purpose : exports module object
413 //=================================================================================
415 #define LIGHTGUI_EXPORT __declspec(dllexport)
417 #define LIGHTGUI_EXPORT
421 LIGHTGUI_EXPORT CAM_Module* createModule() {
422 return new LIGHTGUI();