Salome HOME
Fix problem with 'Glue all coincident edges' option
[modules/geom.git] / src / EntityGUI / EntityGUI_PictureImportDlg.cxx
1 // Copyright (C) 2007-2011  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : EntityGUI_PictureImportDlg.cxx
25 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
26 //
27 #include "EntityGUI_PictureImportDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS.hxx>
40
41 #include <GEOMImpl_Types.hxx>
42 // #include <ShapeRec_FeatureDetector.hxx>
43
44 #include <QFileDialog>
45
46 //=================================================================================
47 // class    : EntityGUI_PictureImportDlg()
48 // purpose  : Constructs a EntityGUI_PictureImportDlg which is a child of 'parent', with the 
49 //            name 'name' and widget flags set to 'f'.
50 //            The dialog will by default be modeless, unless you set 'modal' to
51 //            TRUE to construct a modal dialog.
52 //=================================================================================
53 EntityGUI_PictureImportDlg::EntityGUI_PictureImportDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
54                                     bool modal, Qt::WindowFlags fl )
55   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
56 {
57   setWindowTitle( tr( "GEOM_IMPORT_PICT_TITLE" ) );
58   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PICTURE" ) );
59
60   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
61   mainFrame()->RadioButton2->close();
62   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
63   mainFrame()->RadioButton3->close();
64
65   QGroupBox* GroupFileSel = new QGroupBox( centralWidget() );
66   GroupFileSel->setTitle( tr( "GEOM_FILE" ) );
67   QHBoxLayout* myFileSelGrpLayout = new QHBoxLayout(GroupFileSel );
68   myPushButton = new QPushButton( GroupFileSel );
69   myLineEdit = new QLineEdit( GroupFileSel );
70   myPushButton->setText("...");
71   
72   myFileSelGrpLayout->addWidget( myLineEdit, 1);
73   myFileSelGrpLayout->addWidget( myPushButton, 0);
74   
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( GroupFileSel );
78
79 //   setHelpFileName( "import_picture_page.html" );
80
81   Init();
82 }
83
84
85 //=================================================================================
86 // function : ~EntityGUI_PictureImportDlg()
87 // purpose  : Destroys the object and frees any allocated resources
88 //=================================================================================
89 EntityGUI_PictureImportDlg::~EntityGUI_PictureImportDlg()
90 {  
91 }
92
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void EntityGUI_PictureImportDlg::Init()
99 {
100   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
101   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
102   connect( myPushButton,   SIGNAL( clicked() ), this, SLOT( FileSelectionClicked() ) );
103   
104   resize(100,100);
105   
106   gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
107   gp_Dir aDirZ = gp_Dir(0, 0, 1);
108   gp_Dir aDirX = gp_Dir(1, 0, 0);
109   
110   gp_Ax3 aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
111   
112   myGeomGUI->SetWorkingPlane( aGlobalCS );
113   myGeomGUI->ActiveWorkingPlane();
114   
115   initName( tr( "GEOM_PICTURE" ) );
116 }
117
118 // =================================================================================
119 // function : FileSelectionClicked()
120 // purpose  : 
121 // =================================================================================
122 void EntityGUI_PictureImportDlg::FileSelectionClicked()
123 {
124   QString selFile = QFileDialog::getOpenFileName(this,tr( "SELECT_IMAGE"),"/home", tr("OCC_IMAGE_FILES"));
125   if ( ! selFile.isEmpty() )
126     myLineEdit->setText(selFile);
127 }
128
129 //=================================================================================
130 // function : ClickOnOk()
131 // purpose  :
132 //=================================================================================
133 void EntityGUI_PictureImportDlg::ClickOnOk()
134 {
135   MESSAGE("EntityGUI_PictureImportDlg::ClickOnOk()")
136   setIsApplyAndClose( true );
137   if ( ClickOnApply() )
138     ClickOnCancel();
139 }
140
141 //=================================================================================
142 // function : ClickOnApply()
143 // purpose  :
144 //=================================================================================
145 bool EntityGUI_PictureImportDlg::ClickOnApply()
146 {
147   MESSAGE("EntityGUI_PictureImportDlg::ClickOnApply()")
148   if ( !onAccept() )
149     return false;
150   
151   initName();
152   myLineEdit->setText( "" );
153
154   return true;
155 }
156
157 //=================================================================================
158 // function : createOperation
159 // purpose  :
160 //=================================================================================
161 GEOM::GEOM_IOperations_ptr EntityGUI_PictureImportDlg::createOperation()
162 {
163   return myGeomGUI->GetGeomGen()->GetIBlocksOperations( getStudyId() );
164 }
165
166 //=================================================================================
167 // function : execute
168 // purpose  :
169 //=================================================================================
170 bool EntityGUI_PictureImportDlg::execute( ObjectList& objects )
171 {
172   bool res = false;
173   
174   GEOM::GEOM_Object_var anObj;
175   GEOM::GEOM_IBasicOperations_var aBasicOperations = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
176   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() );
177   QString theImgFileName = myLineEdit->text();
178   if ( theImgFileName.isEmpty() )
179     return res;
180   
181   QPixmap* pixmap = new QPixmap(theImgFileName); 
182   int height            =  pixmap->height();
183   int width             =  pixmap->width();
184   
185   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5*width, -0.5*height, 0 );
186   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ( -0.5*width,  0.5*height, 0 );
187   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5*width,  0.5*height, 0 );
188   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ(  0.5*width, -0.5*height, 0 );
189   
190   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices(P1,P2,P3,P4);
191   getDisplayer()->SetTexture(theImgFileName.toStdString());
192   
193   if ( !aFace->_is_nil() )
194   {
195     objects.push_back( aFace._retn() );
196   }
197   
198   res=true;
199  
200
201   return res;
202 }
203