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 "LIGHTGUI_OBSelector.h"
33 #include "LIGHTGUI_DataOwner.h"
35 #include <SUIT_MessageBox.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_SelectionMgr.h>
39 #include <qinputdialog.h>
40 #include <qstringlist.h>
42 //=================================================================================
43 // function : LIGHTGUI()
44 // purpose : Constructor : sets the default name for the module
45 //=================================================================================
47 : SalomeApp_Module( "LIGHTGUI" )
51 //=================================================================================
52 // function : initialize()
53 // purpose : module intitialization: creates menus, prepares context menu, etc.
54 //=================================================================================
55 void LIGHTGUI::initialize ( CAM_Application* app )
57 SalomeApp_Module::initialize( app );
59 QWidget* parent = application()->desktop();
61 createAction( lgLoadFile, tr( "TOP_LOAD_FILE" ), QIconSet(), tr( "MEN_LOAD_FILE" ),
62 tr( "STB_LOAD_FILE" ), 0, parent, false, this, SLOT( onLoadFile() ) );
63 createAction( lgSaveFile, tr( "TOP_SAVE_FILE" ), QIconSet(), tr( "MEN_SAVE_FILE" ),
64 tr( "STB_SAVE_FILE" ), 0, parent, false, this, SLOT( onSaveFile() ) );
65 createAction( lgEditLine, tr( "TOP_EDIT_LINE" ), QIconSet(), tr( "MEN_EDIT_LINE" ),
66 tr( "STB_EDIT_LINE" ), 0, parent, false, this, SLOT( onEditLine() ) );
67 createAction( lgAddLine, tr( "TOP_ADD_LINE" ), QIconSet(), tr( "MEN_ADD_LINE" ),
68 tr( "STB_ADD_LINE" ), 0, parent, false, this, SLOT( onAddLine() ) );
69 createAction( lgDelLine, tr( "TOP_DEL_LINE" ), QIconSet(), tr( "MEN_DEL_LINE" ),
70 tr( "STB_DEL_LINE" ), 0, parent, false, this, SLOT( onDelLine() ) );
71 createAction( lgClear, tr( "TOP_CLEAR_ALL" ), QIconSet(), tr( "MEN_CLEAR_ALL" ),
72 tr( "STB_CLEAR_ALL" ), 0, parent, false, this, SLOT( onClear() ) );
74 int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
75 createMenu( separator(), aFileMnu, -1, 10 );
76 createMenu( lgLoadFile, aFileMnu, 10 );
77 createMenu( lgSaveFile, aFileMnu, 10 );
79 int aLightMnu = createMenu( tr( "MEN_LIGHT" ), -1, -1, 50 );
80 createMenu( lgEditLine, aLightMnu, 10 );
81 createMenu( lgAddLine, aLightMnu, 10 );
82 createMenu( separator(), aLightMnu, -1, 10 );
83 createMenu( lgDelLine, aLightMnu, 10 );
84 createMenu( lgClear, aLightMnu, 10 );
86 // popup for object browser
88 QString rule = "client='ObjectBrowser' and selcount=1 and type='TextLine'";
90 popupMgr()->insert ( action( lgEditLine ), parentId, 0 );
91 popupMgr()->setRule( action( lgEditLine ), rule, true );
93 popupMgr()->insert ( action( lgAddLine ), parentId, 0 );
94 popupMgr()->setRule( action( lgAddLine ), rule, true );
96 popupMgr()->insert ( separator(), parentId, 0 );
98 popupMgr()->insert ( action( lgDelLine ), parentId, 0 );
99 popupMgr()->setRule( action( lgDelLine ), rule, true );
101 popupMgr()->insert ( action( lgClear ), parentId, 0 );
104 //=================================================================================
105 // function : contextMenuPopup()
106 // purpose : defines context popup menu
107 //=================================================================================
108 void LIGHTGUI::contextMenuPopup ( const QString& client, QPopupMenu* menu, QString& )
110 LIGHTGUI_Selection sel ( client, getApp()->selectionMgr() );
111 popupMgr()->updatePopup( menu, &sel );
114 //=================================================================================
115 // function : windows()
116 // purpose : gets a list of compliant dockable GUI elements
117 //=================================================================================
118 void LIGHTGUI::windows ( QMap<int, int>& aMap ) const
120 aMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
121 aMap.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
124 //=================================================================================
125 // function : engineIOR()
126 // purpose : Returns empty string. It means that this module
127 // should use default engine for its persistence.
128 //=================================================================================
129 QString LIGHTGUI::engineIOR() const
131 return QString( "" );
134 //=================================================================================
135 // function : createDataModel()
136 // purpose : create data model
137 //=================================================================================
138 CAM_DataModel* LIGHTGUI::createDataModel()
140 return new LIGHTGUI_DataModel( this );
143 //=================================================================================
144 // function : selectedLine()
145 // purpose : get the number of selected line (multiple selection is not supported)
146 //=================================================================================
147 int LIGHTGUI::selectedLine()
149 int aPosition = 0; // bad value
151 // Look for selected lines
152 SalomeApp_Application* app = getApp();
153 SalomeApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
155 SUIT_DataOwnerPtrList anOwnersList;
156 mgr->selected( anOwnersList );
158 // Get index of the single selected line
159 if ( anOwnersList.size() == 1 ) {
160 const LIGHTGUI_DataOwner* owner =
161 dynamic_cast<const LIGHTGUI_DataOwner*>( anOwnersList[0].get() );
162 aPosition = owner->lineNb();
168 //=================================================================================
169 // function : activateModule()
170 // purpose : module's activation
171 //=================================================================================
172 bool LIGHTGUI::activateModule ( SUIT_Study* study )
174 bool isDone = SalomeApp_Module::activateModule( study );
175 if ( !isDone ) return false;
177 setMenuShown( true );
179 // enable own ObjectBrowser selector an disable another ones
180 bool isOwnSelector = false;
181 QPtrList<SUIT_Selector> lst;
182 getApp()->selectionMgr()->selectors( lst );
183 for ( QPtrListIterator<SUIT_Selector> it( lst ); it.current(); ++it ) {
184 if ( it.current()->type() == "ObjectBrowser" ) {
185 LIGHTGUI_OBSelector* anOwnSelector = dynamic_cast<LIGHTGUI_OBSelector*>( it.current() );
186 // restore own selector
187 if ( anOwnSelector ) {
189 anOwnSelector->setEnabled( true );
190 isOwnSelector = true;
194 it.current()->setEnabled( false );
199 // create own selector, if it was not found in selection manager
200 if ( !isOwnSelector ) {
201 new LIGHTGUI_OBSelector ( getApp()->objectBrowser(), getApp()->selectionMgr() );
207 //=================================================================================
208 // function : deactivateModule()
209 // purpose : module's deactivation
210 //=================================================================================
211 bool LIGHTGUI::deactivateModule ( SUIT_Study* study )
214 setMenuShown( false );
216 // disable own ObjectBrowser selector an enable another ones
217 QPtrList<SUIT_Selector> lst;
218 getApp()->selectionMgr()->selectors( lst );
219 for ( QPtrListIterator<SUIT_Selector> it( lst ); it.current(); ++it ) {
220 if ( it.current()->type() == "ObjectBrowser" ) {
221 LIGHTGUI_OBSelector* anOwnSelector = dynamic_cast<LIGHTGUI_OBSelector*>( it.current() );
222 if ( anOwnSelector ) {
223 // disable own selector
224 anOwnSelector->setEnabled( false );
228 it.current()->setEnabled( true );
233 return SalomeApp_Module::deactivateModule( study );
236 //=================================================================================
237 // function : onLoadFile()
238 // purpose : "Load File" action slot
239 //=================================================================================
240 void LIGHTGUI::onLoadFile()
242 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
245 QStringList filtersList;
246 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
247 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
249 // Select a file to be loaded
250 QString aFileName = getApp()->getFileName( true, QString::null, filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_LOAD" ), 0 );
251 if ( !aFileName.isEmpty() ) {
253 if ( dm->loadFile( aFileName ) ) {
254 updateObjBrowser( true );
257 SUIT_MessageBox::warn1 ( application()->desktop(),
259 tr( "WRN_LOAD_FAILED" ),
265 //=================================================================================
266 // function : onSaveFile()
267 // purpose : "Save File" action slot
268 //=================================================================================
269 void LIGHTGUI::onSaveFile()
271 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
274 QStringList filtersList;
275 filtersList.append( tr( "LIGHTGUI_MEN_TXT_FILES" ) );
276 filtersList.append( tr( "LIGHTGUI_MEN_ALL_FILES" ) );
278 // Select a file name to dump the lines into
279 QString aFileName = getApp()->getFileName( false, dm->fileName(), filtersList.join( ";;" ), tr( "LIGHTGUI_MEN_DUMP" ), 0 );
280 if ( !aFileName.isEmpty() ) {
282 if ( !dm->dumpFile( aFileName ) ) {
283 SUIT_MessageBox::warn1 ( application()->desktop(),
285 tr( "WRN_DUMP_FAILED" ),
291 //=================================================================================
292 // function : onEditLine()
293 // purpose : "Edit Line" action slot
294 //=================================================================================
295 void LIGHTGUI::onEditLine()
297 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
301 int aPosition = selectedLine();
303 // Check, that position is defined
305 SUIT_MessageBox::warn1 ( application()->desktop(),
307 tr( "WRN_SELECT_LINE" ),
314 QString aText = QInputDialog::getText( tr( "LIGHTGUI_EDIT_LINE" ),
317 dm->getLineText( aPosition ),
319 getApp()->desktop() );
322 // try to change a text of the selected line
323 isOk = dm->setLineText( aPosition, aText );
325 updateObjBrowser( true );
328 SUIT_MessageBox::warn1 ( application()->desktop(),
330 tr( "WRN_EDIT_FAILED" ),
335 //=================================================================================
336 // function : onAddLine()
337 // purpose : "Insert Line" action slot
338 //=================================================================================
339 void LIGHTGUI::onAddLine()
341 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
345 int aPosition = selectedLine();
349 QString aText = QInputDialog::getText( tr( "LIGHTGUI_ADD_LINE" ),
354 getApp()->desktop() );
357 // try to insert/add text line
358 isOk = dm->insertLineBefore( aPosition, aText );
361 updateObjBrowser( true );
364 SUIT_MessageBox::warn1 ( application()->desktop(),
366 tr( "WRN_ADD_FAILED" ),
371 //=================================================================================
372 // function : onDelLine()
373 // purpose : "Delete Line" action slot
374 //=================================================================================
375 void LIGHTGUI::onDelLine()
377 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
381 int aPosition = selectedLine();
383 // Check, that position is defined
385 SUIT_MessageBox::warn1 ( application()->desktop(),
387 tr( "WRN_SELECT_LINE" ),
392 // try to delete line
393 bool isOk = dm->deleteTextLine( aPosition );
395 updateObjBrowser( true );
398 SUIT_MessageBox::warn1 ( application()->desktop(),
400 tr( "WRN_DELETE_FAILED" ),
405 //=================================================================================
406 // function : onClear()
407 // purpose : "Clear Text" action slot
408 //=================================================================================
409 void LIGHTGUI::onClear()
411 LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( dataModel() );
415 updateObjBrowser( true );
418 //=================================================================================
419 // function : createModule()
420 // purpose : exports module object
421 //=================================================================================
423 #define LIGHTGUI_EXPORT __declspec(dllexport)
425 #define LIGHTGUI_EXPORT
429 LIGHTGUI_EXPORT CAM_Module* createModule() {
430 return new LIGHTGUI();