Salome HOME
LOT 15
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObjSelector.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ObjSelector.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool2.h"
24
25 #include <HYDROData_PolylineXY.h>
26
27 #include <GraphicsView_Object.h>
28
29 #include <LightApp_Application.h>
30 #include <LightApp_GVSelector.h>
31 #include <LightApp_SelectionMgr.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35
36 #include <QLayout>
37 #include <QLineEdit>
38 #include <QToolButton>
39
40 HYDROGUI_ObjSelector::HYDROGUI_ObjSelector( HYDROGUI_Module* theModule,
41                                             const ObjectKind theObjectKind,
42                                             QWidget* theParent,
43                                             const int theObjectFlags)
44 : QAbstractButton( theParent ),
45   myObjectKind( theObjectKind ),
46   myModule( theModule ),
47   myObjectFlags( theObjectFlags )
48 {
49   QHBoxLayout* aLayout = new QHBoxLayout( this );
50   aLayout->setMargin( 0 );
51   aLayout->setSpacing( 5 );
52   myBtn = new QToolButton( this );
53   myBtn->setCheckable( true );
54   myBtn->setChecked( false );
55   myObjName = new QLineEdit( this );
56   myObjName->setReadOnly( true );
57   aLayout->addWidget( myBtn, 0 );
58   aLayout->addWidget( myObjName, 1 );
59
60   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61   myBtn->setIcon( QIcon( aResMgr->loadPixmap( "HYDRO", tr( "SELECT_ICO" ) ) ) );
62
63   SUIT_SelectionMgr* aSelMgr = theModule->getApp()->selectionMgr();
64
65   connect( myBtn, SIGNAL( toggled( bool ) ), this, SLOT( OnToggled( bool ) ) );
66   connect( aSelMgr, SIGNAL( selectionChanged() ), this, SLOT( OnSelectionChanged() ) );
67 }
68
69 HYDROGUI_ObjSelector::~HYDROGUI_ObjSelector()
70 {
71 }
72
73 void HYDROGUI_ObjSelector::paintEvent( QPaintEvent* )
74 {
75 }
76
77 bool HYDROGUI_ObjSelector::hitButton( const QPoint& thePnt ) const
78 {
79   return false;
80 }
81
82 void HYDROGUI_ObjSelector::OnToggled( bool isChecked )
83 {
84   if( !isChecked )
85     return;
86
87   QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
88   foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
89     if( aSelector != this )
90       aSelector->myBtn->setChecked( false );
91
92   if( isChecked )
93     OnSelectionChanged();
94 }
95
96 void HYDROGUI_ObjSelector::OnSelectionChanged()
97 {
98   if( !myBtn->isChecked() )
99     return;
100
101   QString anObjName;
102   Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( myModule );
103   if( !anObject.IsNull() )
104     if( myObjectKind == KIND_UNKNOWN || myObjectKind == anObject->GetKind() )
105     {
106       if ( myObjectKind == KIND_POLYLINEXY && ( myObjectFlags & ClosedPolyline ) ) {
107         // check if the polyline is closed
108         Handle(HYDROData_PolylineXY) aPolylineObj = 
109           Handle(HYDROData_PolylineXY)::DownCast( anObject );
110         if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() ) {
111           anObjName = aPolylineObj->GetName();
112         }
113       } else {
114         anObjName = anObject->GetName();
115       }
116
117       // Check if the same object has not been selected in other selectors of the same parent widget.
118       if ( !anObjName.isEmpty() )
119       {
120         QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
121         foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
122         {
123           if( aSelector != this  && ( aSelector->GetName() == anObjName ) )
124           {
125             // Forbid selection of the same object
126             emit alreadySelected( anObjName );
127             return;
128           }
129         }
130       }
131     }
132
133   SetName( anObjName );
134 }
135
136 void HYDROGUI_ObjSelector::SetName( const QString& theName )
137 {
138   myObjName->setText( theName );
139   emit selectionChanged();
140 }
141
142 QString HYDROGUI_ObjSelector::GetName() const
143 {
144   return myObjName->text();
145 }
146
147 void HYDROGUI_ObjSelector::Clear()
148 {
149   myObjName->clear();
150   myBtn->setChecked( false );
151 }
152
153 void HYDROGUI_ObjSelector::SetChecked( const bool theState )
154 {
155   myBtn->setChecked( theState );
156 }