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