Salome HOME
0020230: Memory performance improvement.
[modules/geom.git] / src / OperationGUI / OperationGUI_MaterialDlg.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : OperationGUI_MaterialDlg.cxx
24 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
25 //
26 #include "OperationGUI_MaterialDlg.h"
27 #include "OperationGUI_PartitionDlg.h"
28
29 #include <DlgRef.h>
30 #include <GEOMBase.h>
31 #include <GeometryGUI.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SalomeApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37
38 //=================================================================================
39 // class    : OperationGUI_MaterialDlg()
40 // purpose  : Constructs a OperationGUI_MaterialDlg which is a child of 'parent', with the 
41 //            name 'name' and widget flags set to 'f'.
42 //            The dialog will by default be modeless, unless you set 'modal' to
43 //            TRUE to construct a modal dialog.
44 //=================================================================================
45 OperationGUI_MaterialDlg::OperationGUI_MaterialDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
46                                                     GEOM::ListOfGO ListShapes, bool modal )
47   : GEOMBase_Skeleton( theGeometryGUI, parent, modal )
48 {
49   myListShapes = ListShapes;
50   
51   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_PARTITION" ) ) );
52
53   setWindowTitle( tr( "GEOM_MATERIAL_TITLE" ) );
54
55   /***************************************************************/
56   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_PARTITION" ) );
57   mainFrame()->RadioButton1->setIcon( image0 );
58   mainFrame()->RadioButton2->setAttribute( Qt::WA_DeleteOnClose );
59   mainFrame()->RadioButton2->close();
60   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
61   mainFrame()->RadioButton3->close();
62
63   GroupPoints = new DlgRef_1List1Spin1Btn( centralWidget() );
64   GroupPoints->GroupBox1->setTitle( tr( "GEOM_PARTITION" ) );
65
66   QStringList columns;
67   columns << tr( "GEOM_MATERIAL_SHAPE" ) << tr( "GEOM_MATERIAL_MATERIAL" );
68   GroupPoints->ListView1->setHeaderLabels( columns );
69   GroupPoints->ListView1->setSelectionMode( QAbstractItemView::ExtendedSelection );
70   GroupPoints->ListView1->setSortingEnabled( false );
71
72   GroupPoints->TextLabel1->setText( tr( "GEOM_MATERIAL_ID" ) );
73   GroupPoints->PushButton1->setText( tr( "GEOM_MATERIAL_SET" ) );
74
75   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
76   layout->setMargin( 0 ); layout->setSpacing( 6 );
77   layout->addWidget( GroupPoints );
78   /***************************************************************/
79
80   setHelpFileName("partition_page.html"); 
81
82   Init();
83 }
84
85 //=================================================================================
86 // function : ~OperationGUI_MaterialDlg()
87 // purpose  : Destroys the object and frees any allocated resources
88 //=================================================================================
89 OperationGUI_MaterialDlg::~OperationGUI_MaterialDlg()
90 {
91   // no need to delete child widgets, Qt does it all for us
92 }
93
94 //=================================================================================
95 // function : Init()
96 // purpose  :
97 //=================================================================================
98 void OperationGUI_MaterialDlg::Init()
99 {
100   // get materials list from the parent dialog
101   OperationGUI_PartitionDlg* aParentDlg =
102     qobject_cast<OperationGUI_PartitionDlg*>( parentWidget() );
103   if ( aParentDlg )
104     myListMaterials = aParentDlg->GetListMaterials();
105
106   /* list filling */
107   for ( int ind = 0; ind < myListShapes.length(); ind++ ) {
108     GEOM::GEOM_Object_var anObject = myListShapes[ind];
109     if ( !anObject->_is_nil() ) {
110       QStringList labels;
111       labels << GEOMBase::GetName( anObject );
112       labels << ( ind < myListMaterials.length() ? 
113                   QString::number( myListMaterials[ind] ) : QString( "0" ) );
114       GroupPoints->ListView1->addTopLevelItem( new QTreeWidgetItem( labels ) );
115     }
116   }
117
118   /* signals and slots connections */
119   connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
120   buttonApply()->setAttribute( Qt::WA_DeleteOnClose );
121   buttonApply()->close();
122   buttonCancel()->setText( tr( "GEOM_BUT_CANCEL" ) );
123   
124   connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetMaterial() ) );
125   
126   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
127            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
128 }
129
130
131 //=================================================================================
132 // function : ClickOnOk()
133 // purpose  :
134 //=================================================================================
135 void OperationGUI_MaterialDlg::ClickOnOk()
136 {
137   SUIT_Session::session()->activeApplication()->putInfo( "" );
138
139   int nbSh = myListShapes.length();  
140   myListMaterials.length( nbSh );
141
142   QTreeWidgetItemIterator it( GroupPoints->ListView1 );
143   for ( int i = 0; *it; it++, i++ )
144     myListMaterials[i] = (*it)->text( 1 ).toInt();
145
146   // set materials list to the parent dialog
147   OperationGUI_PartitionDlg* aParentDlg =
148     qobject_cast<OperationGUI_PartitionDlg*>( parentWidget() );
149   if ( aParentDlg )
150     aParentDlg->SetListMaterials( myListMaterials );
151
152   ClickOnCancel();
153 }
154
155
156 //=================================================================================
157 // function : SelectionIntoArgument()
158 // purpose  : Called when selection as changed or other case
159 //=================================================================================
160 void OperationGUI_MaterialDlg::SelectionIntoArgument()
161 {
162   /*QString aString = ""; // name of selection
163
164   int nbSel = GEOMBase::GetNameOfSelectedIObjects(selectedIO(), aString);
165   if (nbSel < 1) {
166     return;
167   }
168
169   //myGeomBase->ConvertListOfIOInListOfIOR(selectedIO(), myListShapes);
170
171   // no simulation
172   return;*/
173 }
174
175
176 //=================================================================================
177 // function : SetMaterial()
178 // purpose  :
179 //=================================================================================
180 void OperationGUI_MaterialDlg::SetMaterial()
181 {
182   QString aMatIdStr = QString::number( GroupPoints->SpinBox1->value() );
183   QList<QTreeWidgetItem*> selectedItems = GroupPoints->ListView1->selectedItems();
184
185   QListIterator<QTreeWidgetItem*> it( selectedItems );
186   while ( it.hasNext() ) {
187     it.next()->setText( 1, aMatIdStr );
188   }
189 }
190
191
192 //=================================================================================
193 // function : ActivateThisDialog()
194 // purpose  :
195 //=================================================================================
196 void OperationGUI_MaterialDlg::ActivateThisDialog()
197 {
198   GEOMBase_Skeleton::ActivateThisDialog();
199   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
200            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
201   return;
202 }
203
204
205 //=================================================================================
206 // function : enterEvent()
207 // purpose  :
208 //=================================================================================
209 void OperationGUI_MaterialDlg::enterEvent( QEvent* )
210 {
211   if ( !mainFrame()->GroupConstructors->isEnabled() )
212     this->ActivateThisDialog();
213 }