Salome HOME
568da59282584e205d9e46cf2472548fca07dcad
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operations.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Operations.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImportImageOp.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_TwoImagesOp.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
32
33 #include <CAM_Application.h>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <QAction>
39 #include <QFileDialog>
40
41 QAction* HYDROGUI_Module::CreateAction( const int theId, const QString& theSuffix, const QString& theImg,
42                                         const int theKey, const bool isToggle, const QString& theSlot )
43 {
44   QString aSlot = theSlot;
45   if( aSlot.isEmpty() )
46     aSlot = SLOT( onOperation() );
47   SUIT_ResourceMgr* aMgr = application()->resourceMgr();
48   std::string anImg = theImg.toStdString();
49   QPixmap aPixmap = theImg.isEmpty() ? QPixmap() : aMgr->loadPixmap( "HYDROGUI", tr( anImg.c_str() ) );
50   std::string aMenu    = ( "MEN_" + theSuffix ).toStdString();
51   std::string aDesktop = ( "DSK_" + theSuffix ).toStdString();
52   std::string aToolbar = ( "STB_" + theSuffix ).toStdString();
53   std::string aSlotStr = aSlot.toStdString();
54   return LightApp_Module::createAction( theId, tr( aMenu.c_str() ), aPixmap,
55     tr( aDesktop.c_str() ), tr( aToolbar.c_str() ),
56                 theKey, application()->desktop(), isToggle, this, aSlotStr.c_str() );
57 }
58
59 void HYDROGUI_Module::CreateActions()
60 {
61   CreateAction( ImportImageId, "IMPORT_IMAGE", "", Qt::CTRL + Qt::Key_I );
62 }
63
64 void HYDROGUI_Module::CreateMenus()
65 {
66   int aHydroMenuIndex = 6; // Edit menu id == 5, View menu id == 10
67   int aHydroId = createMenu( tr( "MEN_DESK_HYDRO" ), -1, -1, aHydroMenuIndex );
68   createMenu( ImportImageId, aHydroId, -1, -1 );
69 }
70
71 void HYDROGUI_Module::CreatePopups()
72 {
73 }
74
75 void HYDROGUI_Module::CreateToolbars()
76 {
77 }
78
79 void HYDROGUI_Module::onOperation()
80 {
81   const QAction* anAction = dynamic_cast<const QAction*>( sender() );
82   int anId = actionId( anAction );
83   if( anId >= 0 )
84     startOperation( anId );
85
86   // tmp
87   if( anId == ImportImageId )
88   {
89     QString aFileName = QFileDialog::getOpenFileName();
90     if( !aFileName.isEmpty() )
91     {
92       int aStudyId = getStudyId();
93       Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
94       if( !aDocument.IsNull() )
95       {
96         Handle(HYDROData_Image) anImageObj =
97           Handle(HYDROData_Image)::DownCast( aDocument->CreateObject( KIND_IMAGE ) );
98         if( !anImageObj.IsNull() )
99         {
100           static int ImageId = 0;
101           anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
102
103           QImage anImage( aFileName );
104           QTransform aTransform;
105           anImageObj->SetImage( anImage );
106           anImageObj->SetTrsf( aTransform );
107
108           getModel()->updateModel();
109         }
110       }
111     }
112   }
113 }
114
115 LightApp_Operation* HYDROGUI_Module::createOperation( const int theId ) const
116 {
117   LightApp_Operation* anOp = 0;
118   HYDROGUI_Module* aModule = const_cast<HYDROGUI_Module*>( this );
119   switch( theId )
120   {
121   case ImportImageId:
122     anOp = new HYDROGUI_ImportImageOp( aModule );
123     break;
124   case FuseId:
125     anOp = new HYDROGUI_TwoImagesOp( aModule, tr( "FUSE_OP" ) );
126     break;
127   case CutId:
128     anOp = new HYDROGUI_TwoImagesOp( aModule, tr( "CUT_OP" ) );
129     break;
130   }
131
132   if( !anOp )
133     anOp = LightApp_Module::createOperation( theId );
134
135   return anOp;
136 }