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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ObjSelector.h"
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool2.h"
25 #include <HYDROData_PolylineXY.h>
27 #include <GraphicsView_Object.h>
29 #include <LightApp_Application.h>
30 #include <LightApp_GVSelector.h>
31 #include <LightApp_SelectionMgr.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
38 #include <QToolButton>
40 HYDROGUI_ObjSelector::HYDROGUI_ObjSelector( HYDROGUI_Module* theModule,
41 const ObjectKind theObjectKind,
43 const int theObjectFlags)
44 : QAbstractButton( theParent ),
45 myObjectKind( theObjectKind ),
46 myModule( theModule ),
47 myObjectFlags( theObjectFlags )
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 );
60 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
61 myBtn->setIcon( QIcon( aResMgr->loadPixmap( "HYDRO", tr( "SELECT_ICO" ) ) ) );
63 SUIT_SelectionMgr* aSelMgr = theModule->getApp()->selectionMgr();
65 connect( myBtn, SIGNAL( toggled( bool ) ), this, SLOT( OnToggled( bool ) ) );
66 connect( aSelMgr, SIGNAL( selectionChanged() ), this, SLOT( OnSelectionChanged() ) );
69 HYDROGUI_ObjSelector::~HYDROGUI_ObjSelector()
73 void HYDROGUI_ObjSelector::paintEvent( QPaintEvent* )
77 bool HYDROGUI_ObjSelector::hitButton( const QPoint& thePnt ) const
82 void HYDROGUI_ObjSelector::OnToggled( bool isChecked )
87 QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
88 foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
89 if( aSelector != this )
90 aSelector->myBtn->setChecked( false );
96 void HYDROGUI_ObjSelector::OnSelectionChanged()
98 if( !myBtn->isChecked() )
102 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::GetSelectedObject( myModule );
103 if( !anObject.IsNull() )
104 if( myObjectKind == KIND_UNKNOWN || myObjectKind == anObject->GetKind() )
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();
114 anObjName = anObject->GetName();
117 // Check if the same object has not been selected in other selectors of the same parent widget.
118 if ( !anObjName.isEmpty() )
120 QList<HYDROGUI_ObjSelector*> aSelectors = parentWidget()->findChildren<HYDROGUI_ObjSelector*>();
121 foreach( HYDROGUI_ObjSelector* aSelector, aSelectors )
123 if( aSelector != this && ( aSelector->GetName() == anObjName ) )
125 // Forbid selection of the same object
126 emit alreadySelected( anObjName );
133 SetName( anObjName );
136 void HYDROGUI_ObjSelector::SetName( const QString& theName )
138 myObjName->setText( theName );
139 emit selectionChanged();
142 QString HYDROGUI_ObjSelector::GetName() const
144 return myObjName->text();
147 void HYDROGUI_ObjSelector::Clear()
150 myBtn->setChecked( false );
153 void HYDROGUI_ObjSelector::SetChecked( const bool theState )
155 myBtn->setChecked( theState );