Salome HOME
The bathymetry is changed to their base altitude class for geometry objects (Bug...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjSelector.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_ObjSelector.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_PolylineXY.h>
30
31 #include <GraphicsView_Object.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_GVSelector.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_Session.h>
39
40 #include <QLayout>
41 #include <QLineEdit>
42 #include <QToolButton>
43
44 HYDROGUI_ObjSelector::HYDROGUI_ObjSelector( HYDROGUI_Module* theModule,
45                                             const ObjectKind theObjectKind,
46                                             QWidget* theParent,
47                                             const int theObjectFlags)
48 : QAbstractButton( theParent ),
49   myObjectKind( theObjectKind ),
50   myModule( theModule ),
51   myObjectFlags( theObjectFlags )
52 {
53   QHBoxLayout* aLayout = new QHBoxLayout( this );
54   aLayout->setMargin( 0 );
55   aLayout->setSpacing( 5 );
56   myBtn = new QToolButton( this );
57   myBtn->setCheckable( true );
58   myBtn->setChecked( false );
59   myObjName = new QLineEdit( this );
60   myObjName->setReadOnly( true );
61   aLayout->addWidget( myBtn, 0 );
62   aLayout->addWidget( myObjName, 1 );
63
64   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
65   myBtn->setIcon( QIcon( aResMgr->loadPixmap( "HYDRO", tr( "SELECT_ICO" ) ) ) );
66
67   SUIT_SelectionMgr* aSelMgr = theModule->getApp()->selectionMgr();
68
69   connect( myBtn, SIGNAL( toggled( bool ) ), this, SLOT( OnToggled( bool ) ) );
70   connect( aSelMgr, SIGNAL( selectionChanged() ), this, SLOT( OnSelectionChanged() ) );
71 }
72
73 HYDROGUI_ObjSelector::~HYDROGUI_ObjSelector()
74 {
75 }
76
77 void HYDROGUI_ObjSelector::paintEvent( QPaintEvent* )
78 {
79 }
80
81 bool HYDROGUI_ObjSelector::hitButton( const QPoint& thePnt ) const
82 {
83   return false;
84 }
85
86 void HYDROGUI_ObjSelector::OnToggled( bool isChecked )
87 {
88   if( !isChecked )
89     return;
90
91   QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
92   foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
93     if( aSelector != this )
94       aSelector->myBtn->setChecked( false );
95
96   if( isChecked )
97     OnSelectionChanged();
98 }
99
100 void HYDROGUI_ObjSelector::OnSelectionChanged()
101 {
102   if( !myBtn->isChecked() )
103     return;
104
105   QString anObjName;
106   Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( myModule );
107   if( !anObject.IsNull() )
108     if( myObjectKind == KIND_UNKNOWN || myObjectKind == anObject->GetKind() )
109     {
110       if ( myObjectKind == KIND_POLYLINEXY && ( myObjectFlags & ClosedPolyline ) ) {
111         // check if the polyline is closed
112         Handle(HYDROData_PolylineXY) aPolylineObj = 
113           Handle(HYDROData_PolylineXY)::DownCast( anObject );
114         if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() ) {
115           anObjName = aPolylineObj->GetName();
116         }
117       } else {
118         anObjName = anObject->GetName();
119       }
120
121       // Check if the same object has not been selected in other selectors of the same parent widget.
122       if ( !anObjName.isEmpty() )
123       {
124         QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
125         foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
126         {
127           if( aSelector != this  && ( aSelector->GetName() == anObjName ) )
128           {
129             // Forbid selection of the same object
130             emit alreadySelected( anObjName );
131             return;
132           }
133         }
134       }
135     }
136
137   SetName( anObjName );
138 }
139
140 void HYDROGUI_ObjSelector::SetName( const QString& theName )
141 {
142   myObjName->setText( theName );
143   emit selectionChanged();
144 }
145
146 QString HYDROGUI_ObjSelector::GetName() const
147 {
148   return myObjName->text();
149 }
150
151 void HYDROGUI_ObjSelector::Clear()
152 {
153   myObjName->clear();
154   myBtn->setChecked( false );
155 }
156
157 void HYDROGUI_ObjSelector::SetChecked( const bool theState )
158 {
159   myBtn->setChecked( theState );
160 }