1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Operations.h"
25 #include "HYDROGUI_CopyPasteOp.h"
26 #include "HYDROGUI_DataModel.h"
27 #include "HYDROGUI_DeleteOp.h"
28 #include "HYDROGUI_ExportImageOp.h"
29 #include "HYDROGUI_ImportImageOp.h"
30 #include "HYDROGUI_ImportBathymetryOp.h"
31 #include "HYDROGUI_CalculationOp.h"
32 #include "HYDROGUI_Module.h"
33 #include "HYDROGUI_ObserveImageOp.h"
34 #include "HYDROGUI_PolylineOp.h"
35 #include "HYDROGUI_ShowHideOp.h"
36 #include "HYDROGUI_SplitZonesTool.h"
37 #include "HYDROGUI_TwoImagesOp.h"
38 #include "HYDROGUI_UpdateFlags.h"
39 #include "HYDROGUI_UpdateImageOp.h"
40 #include "HYDROGUI_VisualStateOp.h"
41 #include "HYDROGUI_ZoneOp.h"
43 #include <CAM_Application.h>
45 #include <QtxListAction.h>
47 #include <SUIT_Desktop.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_Session.h>
52 #include <QApplication>
54 QAction* HYDROGUI_Module::createAction( const int theId, const QString& theSuffix, const QString& theImg,
55 const int theKey, const bool isToggle, const QString& theSlot )
57 QString aSlot = theSlot;
59 aSlot = SLOT( onOperation() );
60 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61 std::string anImg = theImg.toStdString();
62 QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aResMgr->loadPixmap( "HYDRO", tr( anImg.c_str() ) );
63 std::string aMenu = ( "MEN_" + theSuffix ).toStdString();
64 std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
65 std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
66 std::string aSlotStr = aSlot.toStdString();
67 return LightApp_Module::createAction( theId, tr( aMenu.c_str() ), aPixmap,
68 tr( aDesktop.c_str() ), tr( aToolbar.c_str() ),
69 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
72 void HYDROGUI_Module::createActions()
74 createAction( SaveVisualStateId, "SAVE_VISUAL_STATE" );
75 createAction( LoadVisualStateId, "LOAD_VISUAL_STATE" );
77 createAction( CopyId, "COPY", "", Qt::CTRL + Qt::Key_C );
78 createAction( PasteId, "PASTE", "", Qt::CTRL + Qt::Key_V );
80 createAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
81 createAction( EditImportedImageId, "EDIT_IMPORTED_IMAGE" );
82 createAction( ObserveImageId, "OBSERVE_IMAGE" );
83 createAction( ExportImageId, "EXPORT_IMAGE" );
84 createAction( UpdateImageId, "UPDATE_IMAGE" );
86 createAction( CreatePolylineId, "CREATE_POLYLINE" );
87 createAction( EditPolylineId, "EDIT_POLYLINE" );
89 createAction( ImportBathymetryId, "IMPORT_BATHYMETRY", "", Qt::CTRL + Qt::Key_B );
91 createAction( CreateZoneId, "CREATE_ZONE" );
92 createAction( EditZoneId, "EDIT_ZONE" );
94 createAction( CreateCalculationId, "CREATE_CALCULATION" );
95 createAction( EditCalculationId, "EDIT_CALCULATION" );
97 createAction( FuseImagesId, "FUSE_IMAGES" );
98 createAction( EditFusedImageId, "EDIT_FUSED_IMAGE" );
100 createAction( CutImagesId, "CUT_IMAGES" );
101 createAction( EditCutImageId, "EDIT_CUT_IMAGE" );
103 createAction( SplitImageId, "SPLIT_IMAGE" );
104 createAction( EditSplittedImageId, "EDIT_SPLITTED_IMAGE" );
106 createAction( DeleteId, "DELETE", "", Qt::Key_Delete );
108 createAction( ShowId, "SHOW" );
109 createAction( ShowOnlyId, "SHOW_ONLY" );
110 createAction( ShowAllId, "SHOW_ALL" );
111 createAction( HideId, "HIDE" );
112 createAction( HideAllId, "HIDE_ALL" );
115 void HYDROGUI_Module::createMenus()
117 int aFileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
118 int aCustomPos = 5; // to insert custom actions after "Save as" and before "Preferences"
119 createMenu( SaveVisualStateId, aFileMenu, aCustomPos );
120 createMenu( separator(), aFileMenu, -1, aCustomPos );
122 int anEditMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 5 );
123 createMenu( UndoId, anEditMenu );
124 createMenu( RedoId, anEditMenu );
125 createMenu( separator(), anEditMenu );
126 createMenu( CopyId, anEditMenu );
127 createMenu( PasteId, anEditMenu );
129 int aHydroMenu = 6; // Edit menu id == 5, View menu id == 10
130 int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenu );
131 createMenu( ImportImageId, aHydroId, -1, -1 );
132 createMenu( ImportBathymetryId, aHydroId, -1, -1 );
133 createMenu( CreatePolylineId, aHydroId, -1, -1 );
134 createMenu( CreateZoneId, aHydroId, -1, -1 );
135 createMenu( CreateCalculationId, aHydroId, -1, -1 );
136 createMenu( separator(), aHydroId );
137 createMenu( FuseImagesId, aHydroId, -1, -1 );
138 createMenu( CutImagesId, aHydroId, -1, -1 );
139 createMenu( SplitImageId, aHydroId, -1, -1 );
141 createMenu( separator(), aHydroId );
142 createMenu( TestSplitId, aHydroId, -1, -1 );
145 void HYDROGUI_Module::createPopups()
149 void HYDROGUI_Module::createToolbars()
151 int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
152 createTool( UndoId, aToolBar );
153 createTool( RedoId, aToolBar );
156 void HYDROGUI_Module::createUndoRedoActions()
158 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
160 QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
161 aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
162 Qt::CTRL + Qt::Key_Z, application()->desktop() );
164 QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
165 aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
166 Qt::CTRL + Qt::Key_Y, application()->desktop() );
168 registerAction( UndoId, anEditUndo );
169 registerAction( RedoId, anEditRedo );
171 anEditUndo->setComment( tr( "STB_UNDO" ) );
172 anEditRedo->setComment( tr( "STB_REDO" ) );
174 connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
175 connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
177 QAction* aTestSplit = new QAction( "Test split", application()->desktop() );
178 registerAction( TestSplitId, aTestSplit );
179 connect( aTestSplit, SIGNAL( triggered() ), this, SLOT( onTestSplit() ) );
182 void HYDROGUI_Module::updateUndoRedoControls()
184 HYDROGUI_DataModel* aModel = getDataModel();
186 QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
187 QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
189 bool aCanUndo = aModel->canUndo();
190 bool aCanRedo = aModel->canRedo();
193 aUndoAction->addNames( aModel->undoNames() );
194 aUndoAction->setEnabled( aCanUndo );
197 aRedoAction->addNames( aModel->redoNames() );
198 aRedoAction->setEnabled( aCanRedo );
201 void HYDROGUI_Module::onOperation()
203 const QAction* anAction = dynamic_cast<const QAction*>( sender() );
204 int anId = actionId( anAction );
206 startOperation( anId );
209 bool HYDROGUI_Module::onUndo( int theNumActions )
211 QApplication::setOverrideCursor( Qt::WaitCursor );
213 HYDROGUI_DataModel* aModel = getDataModel();
216 while( theNumActions > 0 )
218 if( !aModel->undo() )
227 QApplication::restoreOverrideCursor();
231 bool HYDROGUI_Module::onRedo( int theNumActions )
233 QApplication::setOverrideCursor( Qt::WaitCursor );
235 HYDROGUI_DataModel* aModel = getDataModel();
238 while( theNumActions > 0 )
240 if( !aModel->redo() )
249 QApplication::restoreOverrideCursor();
253 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
255 LightApp_Operation* anOp = 0;
256 HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
259 case SaveVisualStateId:
260 case LoadVisualStateId:
261 anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
265 anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
268 case EditImportedImageId:
269 anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
272 anOp = new HYDROGUI_ObserveImageOp( aModule );
275 anOp = new HYDROGUI_ExportImageOp( aModule );
278 anOp = new HYDROGUI_UpdateImageOp( aModule );
280 case CreatePolylineId:
282 anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
284 case ImportBathymetryId:
285 anOp = new HYDROGUI_ImportBathymetryOp( aModule );
289 anOp = new HYDROGUI_ZoneOp( aModule, theId == EditZoneId );
291 case CreateCalculationId:
292 case EditCalculationId:
293 anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
296 case EditFusedImageId:
297 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
301 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
304 case EditSplittedImageId:
305 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
308 anOp = new HYDROGUI_DeleteOp( aModule );
315 anOp = new HYDROGUI_ShowHideOp( aModule, theId );
320 anOp = LightApp_Module::createOperation( theId );
325 //-----------------------------------------------------------------------------
327 //-----------------------------------------------------------------------------
328 #include <HYDROData_Iterator.h>
329 #include <HYDROGUI_Tool.h>
330 void HYDROGUI_Module::onTestSplit()
332 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( getStudyId() );
334 aDocument->StartOperation();
336 HYDROData_SequenceOfObjects aZoneList;
337 HYDROData_Iterator anIterator( aDocument, KIND_ZONE );
338 for( ; anIterator.More(); anIterator.Next() )
339 aZoneList.Append( anIterator.Current() );
341 HYDROGUI_SplitZonesTool::SplitDataList aSplitDataList =
342 HYDROGUI_SplitZonesTool::SplitZones( aZoneList, NULL );
345 HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplitDataList );
346 while( anIter.hasNext() )
348 const HYDROGUI_SplitZonesTool::SplitData& aSplitData = anIter.next();
349 const QPainterPath& aPath = aSplitData.Path;
350 const QStringList& aZoneNames = aSplitData.ZoneNames;
352 Handle(HYDROData_Polyline) aPolyline =
353 Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
354 if( !aPolyline.IsNull() )
356 aPolyline->SetName( QString( "SplitPolyline_%1" ).arg( ++anIndex ) );
357 aPolyline->setDimension( 2 );
359 QList<PolylineSection> aPolylineData;
360 for( int i = 0, n = aPath.elementCount(); i < n; i++ )
362 const QPainterPath::Element anElement = aPath.elementAt( i );
363 switch( anElement.type )
365 case QPainterPath::MoveToElement:
366 aPolylineData.append( PolylineSection() );
368 case QPainterPath::LineToElement:
369 if( !aPolylineData.isEmpty() )
371 PolylineSection& aSection = aPolylineData.last();
372 aSection.myCoords << anElement.x;
373 aSection.myCoords << anElement.y;
376 case QPainterPath::CurveToElement: // currently not supported
381 aPolyline->setPolylineData( aPolylineData );
384 Handle(HYDROData_Zone) aZone =
385 Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
386 if( !aZone.IsNull() )
388 aZone->SetName( QString( "SplitZone_%1" ).arg( anIndex ) );
389 aZone->SetPolyline( aPolyline );
391 aZone->SetBorderColor( Qt::black );
394 int aR = 0, aG = 0, aB = 0;
395 QStringListIterator aZoneNameIter( aZoneNames );
396 while( aZoneNameIter.hasNext() )
398 const QString& aZoneName = aZoneNameIter.next();
399 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
400 HYDROGUI_Tool::FindObjectByName( this, aZoneName, KIND_ZONE ) );
401 if( !aRefZone.IsNull() )
403 QColor aRefColor = aRefZone->GetFillingColor();
404 aR += aRefColor.red();
405 aG += aRefColor.green();
406 aB += aRefColor.blue();
412 QColor aFillingColor( aR / aCounter, aG / aCounter, aB / aCounter );
413 aZone->SetFillingColor( aFillingColor );
418 aDocument->CommitOperation();