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 );
142 //createMenu( separator(), aHydroId );
143 //createMenu( TestSplitId, aHydroId, -1, -1 );
146 void HYDROGUI_Module::createPopups()
150 void HYDROGUI_Module::createToolbars()
152 int aToolBar = createTool( tr( "MEN_DESK_HYDRO" ) );
153 createTool( UndoId, aToolBar );
154 createTool( RedoId, aToolBar );
157 void HYDROGUI_Module::createUndoRedoActions()
159 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
161 QtxListAction* anEditUndo = new QtxListAction( tr( "MEN_UNDO" ),
162 aResMgr->loadPixmap( "HYDRO", tr( "UNDO_ICO" ) ), tr( "DSK_UNDO" ),
163 Qt::CTRL + Qt::Key_Z, application()->desktop() );
165 QtxListAction* anEditRedo = new QtxListAction( tr( "MEN_REDO" ),
166 aResMgr->loadPixmap( "HYDRO", tr( "REDO_ICO" ) ), tr( "DSK_REDO" ),
167 Qt::CTRL + Qt::Key_Y, application()->desktop() );
169 registerAction( UndoId, anEditUndo );
170 registerAction( RedoId, anEditRedo );
172 anEditUndo->setComment( tr( "STB_UNDO" ) );
173 anEditRedo->setComment( tr( "STB_REDO" ) );
175 connect( anEditUndo, SIGNAL( triggered( int ) ), this, SLOT( onUndo( int ) ) );
176 connect( anEditRedo, SIGNAL( triggered( int ) ), this, SLOT( onRedo( int ) ) );
178 QAction* aTestSplit = new QAction( "Test split", application()->desktop() );
179 registerAction( TestSplitId, aTestSplit );
180 connect( aTestSplit, SIGNAL( triggered() ), this, SLOT( onTestSplit() ) );
183 void HYDROGUI_Module::updateUndoRedoControls()
185 HYDROGUI_DataModel* aModel = getDataModel();
187 QtxListAction* aUndoAction = (QtxListAction*)action( UndoId );
188 QtxListAction* aRedoAction = (QtxListAction*)action( RedoId );
190 bool aCanUndo = aModel->canUndo();
191 bool aCanRedo = aModel->canRedo();
194 aUndoAction->addNames( aModel->undoNames() );
195 aUndoAction->setEnabled( aCanUndo );
198 aRedoAction->addNames( aModel->redoNames() );
199 aRedoAction->setEnabled( aCanRedo );
202 void HYDROGUI_Module::onOperation()
204 const QAction* anAction = dynamic_cast<const QAction*>( sender() );
205 int anId = actionId( anAction );
207 startOperation( anId );
210 bool HYDROGUI_Module::onUndo( int theNumActions )
212 QApplication::setOverrideCursor( Qt::WaitCursor );
214 HYDROGUI_DataModel* aModel = getDataModel();
217 while( theNumActions > 0 )
219 if( !aModel->undo() )
228 QApplication::restoreOverrideCursor();
232 bool HYDROGUI_Module::onRedo( int theNumActions )
234 QApplication::setOverrideCursor( Qt::WaitCursor );
236 HYDROGUI_DataModel* aModel = getDataModel();
239 while( theNumActions > 0 )
241 if( !aModel->redo() )
250 QApplication::restoreOverrideCursor();
254 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
256 LightApp_Operation* anOp = 0;
257 HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
260 case SaveVisualStateId:
261 case LoadVisualStateId:
262 anOp = new HYDROGUI_VisualStateOp( aModule, theId == LoadVisualStateId );
266 anOp = new HYDROGUI_CopyPasteOp( aModule, theId == PasteId );
269 case EditImportedImageId:
270 anOp = new HYDROGUI_ImportImageOp( aModule, theId == EditImportedImageId );
273 anOp = new HYDROGUI_ObserveImageOp( aModule );
276 anOp = new HYDROGUI_ExportImageOp( aModule );
279 anOp = new HYDROGUI_UpdateImageOp( aModule );
281 case CreatePolylineId:
283 anOp = new HYDROGUI_PolylineOp( aModule, theId == EditPolylineId );
285 case ImportBathymetryId:
286 anOp = new HYDROGUI_ImportBathymetryOp( aModule );
290 anOp = new HYDROGUI_ZoneOp( aModule, theId == EditZoneId );
292 case CreateCalculationId:
293 case EditCalculationId:
294 anOp = new HYDROGUI_CalculationOp( aModule, theId == EditCalculationId );
297 case EditFusedImageId:
298 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Fuse, theId == EditFusedImageId );
302 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Cut, theId == EditCutImageId );
305 case EditSplittedImageId:
306 anOp = new HYDROGUI_TwoImagesOp( aModule, HYDROGUI_TwoImagesOp::Split, theId == EditSplittedImageId );
309 anOp = new HYDROGUI_DeleteOp( aModule );
316 anOp = new HYDROGUI_ShowHideOp( aModule, theId );
321 anOp = LightApp_Module::createOperation( theId );
326 //-----------------------------------------------------------------------------
328 //-----------------------------------------------------------------------------
329 #include <HYDROData_Iterator.h>
330 #include <HYDROGUI_Tool.h>
331 void HYDROGUI_Module::onTestSplit()
333 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( getStudyId() );
335 aDocument->StartOperation();
337 HYDROData_SequenceOfObjects aZoneList;
338 HYDROData_Iterator anIterator( aDocument, KIND_ZONE );
339 for( ; anIterator.More(); anIterator.Next() )
340 aZoneList.Append( anIterator.Current() );
342 HYDROGUI_SplitZonesTool::SplitDataList aSplitDataList =
343 HYDROGUI_SplitZonesTool::SplitZones( aZoneList, NULL );
346 HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplitDataList );
347 while( anIter.hasNext() )
349 const HYDROGUI_SplitZonesTool::SplitData& aSplitData = anIter.next();
350 const QPainterPath& aPath = aSplitData.Path;
351 const QStringList& aZoneNames = aSplitData.ZoneNames;
353 Handle(HYDROData_Polyline) aPolyline =
354 Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
355 if( !aPolyline.IsNull() )
357 aPolyline->SetName( QString( "SplitPolyline_%1" ).arg( ++anIndex ) );
358 aPolyline->setDimension( 2 );
360 QList<PolylineSection> aPolylineData;
361 for( int i = 0, n = aPath.elementCount(); i < n; i++ )
363 const QPainterPath::Element anElement = aPath.elementAt( i );
364 switch( anElement.type )
366 case QPainterPath::MoveToElement:
367 aPolylineData.append( PolylineSection() );
369 case QPainterPath::LineToElement:
370 if( !aPolylineData.isEmpty() )
372 PolylineSection& aSection = aPolylineData.last();
373 aSection.myCoords << anElement.x;
374 aSection.myCoords << anElement.y;
377 case QPainterPath::CurveToElement: // currently not supported
382 aPolyline->setPolylineData( aPolylineData );
385 Handle(HYDROData_Zone) aZone =
386 Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
387 if( !aZone.IsNull() )
389 aZone->SetName( QString( "SplitZone_%1" ).arg( anIndex ) );
390 aZone->SetPolyline( aPolyline );
392 aZone->SetBorderColor( Qt::black );
395 int aR = 0, aG = 0, aB = 0;
396 QStringListIterator aZoneNameIter( aZoneNames );
397 while( aZoneNameIter.hasNext() )
399 const QString& aZoneName = aZoneNameIter.next();
400 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
401 HYDROGUI_Tool::FindObjectByName( this, aZoneName, KIND_ZONE ) );
402 if( !aRefZone.IsNull() )
404 QColor aRefColor = aRefZone->GetFillingColor();
405 aR += aRefColor.red();
406 aG += aRefColor.green();
407 aB += aRefColor.blue();
413 QColor aFillingColor( aR / aCounter, aG / aCounter, aB / aCounter );
414 aZone->SetFillingColor( aFillingColor );
419 aDocument->CommitOperation();