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