1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: LightApp_Dialog.cxx
24 // Author: Alexander SOLOVYOV
26 #include "LightApp_Dialog.h"
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
30 #include <QAbstractButton>
31 #include <QToolButton>
38 LightApp_Dialog::LightApp_Dialog( QWidget* parent, const char* name, bool modal,
39 bool allowResize, const int f, Qt::WindowFlags wf )
40 : QtxDialog( parent, modal, allowResize, f, wf ),
41 myIsExclusive( true ),
44 setObjectName( name );
45 setObjectPixmap( "LightApp", tr( "ICON_SELECT" ) );
51 LightApp_Dialog::~LightApp_Dialog()
58 void LightApp_Dialog::show()
64 \return isExclusive status of selection buttons
66 bool LightApp_Dialog::isExclusive() const
72 Updates "on" state of buttons according to special button
73 \param _id - id of special button (if it is -1, then first selected button will be treated as special)
75 void LightApp_Dialog::updateButtons( const int _id )
82 ObjectMap::const_iterator anIt = myObjects.begin(),
83 aLast = myObjects.end();
84 for( ; anIt!=aLast; anIt++ )
86 QToolButton* but = (QToolButton*)anIt.value().myBtn;
87 if( but && but->isChecked() )
93 but->setChecked( false );
99 Sets isExclusive status of selection buttons
100 \param ex - new value of isExclusive status
102 void LightApp_Dialog::setExclusive( const bool ex )
109 Shows object selection widget
110 \param id - identificator of object selection widget
112 void LightApp_Dialog::showObject( const int id )
114 setObjectShown( id, true );
118 Hides object selection widget
119 \param id - identificator of object selection widget
121 void LightApp_Dialog::hideObject( const int id )
123 setObjectShown( id, false );
127 Shows/hides object selection widget
128 \param id - identificator of object selection widget
129 \param shown - if it is true, widget will be shown
131 void LightApp_Dialog::setObjectShown( const int id, const bool shown )
133 if( myObjects.contains( id ) && isObjectShown( id )!=shown )
135 Object& obj = myObjects[ id ];
136 obj.myEdit->setVisible( shown );
137 obj.myBtn->setVisible( shown );
138 obj.myLabel->setVisible( shown );
140 ( ( QToolButton* )obj.myBtn )->setChecked( false );
145 \return isShown state of object selection widget
146 \param id - identificator of object selection widget
148 bool LightApp_Dialog::isObjectShown( const int id ) const
150 return myObjects.contains( id ) &&
151 ( myObjects[ id ].myEdit->isVisible() ||
152 myObjects[ id ].myEdit->isVisibleTo( myObjects[ id ].myEdit->parentWidget() ) );
156 Change enable state of object selection widget
157 \param id - identificator of object selection widget
158 \param en - new value of enable state
160 void LightApp_Dialog::setObjectEnabled( const int id, const bool en )
162 if( myObjects.contains( id ) && isObjectEnabled( id )!=en )
164 Object& obj = myObjects[ id ];
165 obj.myEdit->setEnabled( en );
166 obj.myBtn->setEnabled( en );
167 // obj.myLabel->setEnabled( en );
169 ( ( QToolButton* )obj.myBtn )->setChecked( false );
174 \return enable state of object selection widget
175 \param id - identificator of object selection widget
177 bool LightApp_Dialog::isObjectEnabled( const int id ) const
179 return myObjects.contains( id ) && myObjects[ id ].myEdit->isEnabled();
183 Passes to all active widgets name, type and id of selected object
184 \param name - name of selected object
185 \param type - type of selected object
186 \param id - id of selected object
187 \param update - is need to update selection description string
189 void LightApp_Dialog::selectObject( const QString& name, const int type, const QString& id, const bool update )
191 QStringList names; names.append( name );
192 TypesList types; types.append( type );
193 QStringList ids; ids.append( id );
194 selectObject( names, types, ids, update );
198 Passes to all active widgets names, types and ids of selected objects
199 \param _names - names of selected objects
200 \param _types - types of selected objects
201 \param _ids - ids of selected objects
202 \param update - is need to update selection description string
204 void LightApp_Dialog::selectObject( const QStringList& _names,
205 const TypesList& _types,
206 const QStringList& _ids,
209 ObjectMap::iterator anIt = myObjects.begin(),
210 aLast = myObjects.end();
211 for( ; anIt!=aLast; anIt++ )
212 if( anIt.value().myBtn->isChecked() )
213 selectObject( anIt.key(), _names, _types, _ids, update );
217 \return true if widget has selection
218 \param id - identificator of object selection widget
220 bool LightApp_Dialog::hasSelection( const int id ) const
222 return myObjects.contains( id ) && !myObjects[ id ].myIds.isEmpty();
226 Clears selection of widget
227 \param id - identificator of object selection widget
229 void LightApp_Dialog::clearSelection( const int id )
233 ObjectMap::const_iterator anIt = myObjects.begin(),
234 aLast = myObjects.end();
235 for( ; anIt!=aLast; anIt++ )
236 clearSelection( anIt.key() );
239 else if( myObjects.contains( id ) )
241 myObjects[ id ].myIds.clear();
242 myObjects[ id ].myTypes.clear();
243 myObjects[ id ].myNames.clear();
245 myObjects[ id ].myEdit->setText( QString() );
246 emit selectionChanged( id );
251 \return object selection widget
252 \param theId - identificator of object selection widget
253 \param theWgId may be "Label", "Btn" or "Control"
255 QWidget* LightApp_Dialog::objectWg( const int theId, const int theWgId ) const
258 if( myObjects.contains( theId ) )
260 if ( theWgId == Label )
261 aResWg = myObjects[ theId ].myLabel;
262 else if ( theWgId == Btn )
263 aResWg = myObjects[ theId ].myBtn;
264 else if ( theWgId == Control )
265 aResWg = myObjects[ theId ].myEdit;
271 \return object selection widget text
272 \param theId - identificator of object selection widget
274 QString LightApp_Dialog::objectText( const int theId ) const
276 return myObjects.contains( theId ) ? myObjects[ theId ].myEdit->text() : "";
280 Sets object selection widget text
281 \param theId - identificator of object selection widget
282 \param theText - new text
284 void LightApp_Dialog::setObjectText( const int theId, const QString& theText )
286 if ( myObjects.contains( theId ) )
287 myObjects[ theId ].myEdit->setText( theText );
291 \return objects selected by widget
292 \param id - identificator of object selection widget
293 \param list - list to be filled by selected objects
295 void LightApp_Dialog::selectedObject( const int id, QStringList& list ) const
297 if( myObjects.contains( id ) )
298 list = myObjects[ id ].myIds;
302 \return selected object id
303 \param id - identificator of object selection widget
305 QString LightApp_Dialog::selectedObject( const int id ) const
307 if ( myObjects.contains( id ) && myObjects[ id ].myIds.count() > 0 )
308 return myObjects[ id ].myIds.first();
314 \return all selected objects
315 \param objs - map: widget id -> string id to be filled with selected objects
317 void LightApp_Dialog::objectSelection( SelectedObjects& objs ) const
320 ObjectMap::const_iterator anIt = myObjects.begin(),
321 aLast = myObjects.end();
322 for( ; anIt!=aLast; anIt++ )
325 selectedObject( anIt.key(), ids );
327 objs.insert( anIt.key(), ids );
332 Creates object selection widget
335 \parent - parent object
336 \id - proposed id for widget (if it is less than 0, the free id will be used)
338 int LightApp_Dialog::createObject( const QString& label, QWidget* parent, const int id )
342 for( nid=0; myObjects.contains( nid ); nid++ );
344 if( !myObjects.contains( nid ) )
346 QLabel* lab = new QLabel( label, parent );
347 myObjects[ nid ].myLabel = lab;
349 QToolButton* but = new QToolButton( parent );
350 but->setIcon( QIcon( myPixmap ) );
351 but->setCheckable( true );
352 but->setMaximumWidth( but->height() );
353 but->setMinimumWidth( but->height() );
354 connect( but, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
355 myObjects[ nid ].myBtn = but;
357 QLineEdit* ne = new QLineEdit( parent );
358 ne->setReadOnly( true );
359 ne->setMinimumWidth( 150 );
360 connect( ne, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
361 myObjects[ nid ].myEdit = ne;
363 myObjects[ nid ].myNI = OneNameOrCount;
369 Changes label of object selection widget
370 \param id - identificator of object selection widget
371 \param label - new text of label
373 void LightApp_Dialog::renameObject( const int id, const QString& label )
375 if( myObjects.contains( id ) )
376 myObjects[ id ].myLabel->setText( label );
380 Sets possible types for widget
381 \param id - identificator of object selection widget
382 \param type1,... - type
384 void LightApp_Dialog::setObjectType( const int id, const int type1, ... )
388 const int* tt = &type1;
395 setObjectType( id, types );
399 Sets possible types for widget
400 \param id - identificator of object selection widget
401 \param list - list of possible types
403 void LightApp_Dialog::setObjectType( const int id, const TypesList& list )
405 if( !myObjects.contains( id ) )
408 TypesList& internal = myObjects[ id ].myPossibleTypes;
411 TypesList::const_iterator anIt = list.begin(),
413 for( ; anIt!=aLast; anIt++ )
414 types.insert( *anIt, 0 );
418 QMap<int,int>::const_iterator aMIt = types.begin(),
419 aMLast = types.end();
420 for( ; aMIt!=aMLast; aMIt++ )
421 internal.append( aMIt.key() );
427 Adds new possible types to object selection widget
428 \param id - identificator of object selection widget
429 \param type1, ... - new types
431 void LightApp_Dialog::addObjectType( const int id, const int type1, const int, ... )
433 TypesList types; objectTypes( id, types );
435 const int* tt = &type1;
442 setObjectType( id, types );
446 Adds new possible types to object selection widget
447 \param id - identificator of object selection widget
448 \param list - new types
450 void LightApp_Dialog::addObjectType( const int id, const TypesList& list )
452 TypesList types = list; objectTypes( id, types );
453 setObjectType( id, types );
457 Adds new possible type to object selection widget
458 \param id - identificator of object selection widget
459 \param type - new type
461 void LightApp_Dialog::addObjectType( const int id, const int type )
463 TypesList types; objectTypes( id, types );
464 types.append( type );
465 setObjectType( id, types );
469 Clears list of possibles types for object selection widget
470 \param id - identificator of object selection widget
472 void LightApp_Dialog::removeObjectType( const int id )
475 setObjectType( id, types );
479 Removes types from list of possibles for object selection widget
480 \param id - identificator of object selection widget
481 \param list - list of types to be removed
483 void LightApp_Dialog::removeObjectType( const int id, const TypesList& list )
485 if( !myObjects.contains( id ) )
488 TypesList& internal = myObjects[ id ].myPossibleTypes;
491 TypesList::const_iterator anIt = internal.begin(),
492 aLast = internal.end();
493 for( ; anIt!=aLast; anIt++ )
494 types.insert( *anIt, 0 );
495 anIt = list.begin(); aLast = list.end();
496 for( ; anIt!=aLast; anIt++ )
497 types.remove( *anIt );
501 QMap<int,int>::const_iterator aMIt = types.begin(),
502 aMLast = types.end();
503 for( ; aMIt!=aMLast; aMIt++ )
504 internal.append( aMIt.key() );
510 Removes type from list of possibles for object selection widget
511 \param id - identificator of object selection widget
512 \param type - type to be removed
514 void LightApp_Dialog::removeObjectType( const int id, const int type )
516 TypesList list; list.append( type );
517 removeObjectType( id, list );
521 \return true if widget has such type
522 \param id - identificator of object selection widget
523 \param type - type to be checked
525 bool LightApp_Dialog::hasObjectType( const int id, const int type ) const
527 if( myObjects.contains( id ) )
528 return myObjects[ id ].myPossibleTypes.contains( type );
534 Returns list of possible types for widget
535 \param id - identificator of object selection widget
536 \param list - list to be filled with possible types
538 void LightApp_Dialog::objectTypes( const int id, TypesList& list ) const
540 if( myObjects.contains( id ) )
542 TypesList::const_iterator anIt = myObjects[ id ].myPossibleTypes.begin(),
543 aLast = myObjects[ id ].myPossibleTypes.end();
544 for( ; anIt!=aLast; anIt++ )
545 list.append( *anIt );
550 SLOT: called if selection button is clicked
552 void LightApp_Dialog::onToggled( bool on )
554 QAbstractButton* but = ( QAbstractButton* )sender();
560 ObjectMap::const_iterator anIt = myObjects.begin(),
561 aLast = myObjects.end();
562 for( ; anIt!=aLast && id==-1; anIt++ )
563 if( anIt.value().myBtn==but )
571 emit objectActivated( id );
574 emit objectDeactivated( id );
579 Updates selection description of widget
580 \param id - identificator of object selection widget
581 \param emit_signal - if it is true, the signal "selection changed" is emitted
583 void LightApp_Dialog::updateObject( const int id, bool emit_signal )
585 if( hasSelection( id ) )
587 Object& obj = myObjects[ id ];
588 filterTypes( id, obj.myNames, obj.myTypes, obj.myIds );
589 obj.myEdit->setText( selectionDescription( obj.myNames, obj.myTypes, obj.myNI ) );
591 emit selectionChanged( id );
596 Finds in list possible types
597 \param id - identificator of object selection widget
598 \param names - list of selected objects names
599 \param types - list of selected objects types
600 \param ids - list of selected objects ids
602 void LightApp_Dialog::filterTypes( const int id, QStringList& names, TypesList& types, QStringList& ids ) const
604 if( !myObjects.contains( id ) )
607 const Object& obj = myObjects[ id ];
608 if( obj.myPossibleTypes.isEmpty() )
611 QStringList new_names, new_ids;
614 TypesList::const_iterator anIt1 = types.begin(),
616 QStringList::const_iterator anIt2 = names.begin(),
618 for( ; anIt1!=aLast; anIt1++, anIt2++, anIt3++ )
619 if( obj.myPossibleTypes.contains( *anIt1 ) )
621 if( new_types.count()==1 && !multipleSelection( id ) )
624 new_names.append( *anIt2 );
625 new_types.append( *anIt1 );
626 new_ids.append( *anIt3 );
634 \return global resource manager
636 SUIT_ResourceMgr* LightApp_Dialog::resMgr() const
638 return SUIT_Session::session()->resourceMgr();
642 Sets pixmap for all object selection button
645 void LightApp_Dialog::setObjectPixmap( const QPixmap& p )
648 ObjectMap::const_iterator anIt = myObjects.begin(),
649 aLast = myObjects.end();
650 for( ; anIt!=aLast; anIt++ )
651 ( ( QToolButton* )anIt.value().myBtn )->setIcon( p );
655 Sets pixmap all for object selection button
656 \param section - name of section of resource manager
657 \param file - name of file
659 void LightApp_Dialog::setObjectPixmap( const QString& section, const QString& file )
661 SUIT_ResourceMgr* mgr = resMgr();
663 setObjectPixmap( mgr->loadPixmap( section, file ) );
667 \return true, if it is enable multiple selection
668 \param id - identificator of object selection widget
670 bool LightApp_Dialog::multipleSelection( const int id ) const
672 return nameIndication( id )!=OneName;
676 \return type of name indication
677 \param id - identificator of object selection widget
679 LightApp_Dialog::NameIndication LightApp_Dialog::nameIndication( const int id ) const
681 if( myObjects.contains( id ) )
682 return myObjects[ id ].myNI;
684 return OneNameOrCount;
688 Sets type of name indication
689 \param id - identificator of object selection widget
690 \param ni - new type of name indication
692 void LightApp_Dialog::setNameIndication( const int id, const NameIndication ni )
696 ObjectMap::iterator anIt = myObjects.begin(),
698 aLast = myObjects.end();
699 for( ; anIt!=aLast; anIt++ )
701 anIt.value().myNI = ni;
702 setReadOnly( anIt.key(), isReadOnly( anIt.key() ) );
703 aNext = anIt; aNext++;
704 updateObject( anIt.key(), aNext==aLast );
707 else if( myObjects.contains( id ) )
709 myObjects[ id ].myNI = ni;
710 setReadOnly( id, isReadOnly( id ) );
711 updateObject( id, true );
716 \return string representation of selection by selection data
717 \param names - list of selected objects names
718 \param types - list of selected objects types
719 \param ni - type of name indication
721 QString LightApp_Dialog::selectionDescription( const QStringList& names, const TypesList& types, const NameIndication ni ) const
723 if( names.count()!=types.count() )
724 return "LightApp_Dialog::selectionDescription(): Error!!!";
726 if( names.isEmpty() )
732 return names.first();
736 if( names.count()==1 )
737 return names.first();
739 return countOfTypes( types );
743 return names.join( " " );
747 return countOfTypes( types );
754 \return string representation of count of types
755 \param types - list of types
757 QString LightApp_Dialog::countOfTypes( const TypesList& types ) const
759 QMap<int, int> typesCount;
760 QStringList typeCount;
762 TypesList::const_iterator anIt = types.begin(),
764 for( ; anIt!=aLast; anIt++ )
765 if( typesCount.contains( *anIt ) )
766 typesCount[ *anIt ]++;
768 typesCount[ *anIt ] = 1;
770 QMap<int,int>::const_iterator aMIt = typesCount.begin(),
771 aMLast = typesCount.end();
772 for( ; aMIt!=aMLast; aMIt++ )
773 typeCount.append( QString( "%1 %2" ).arg( aMIt.value() ).arg( typeName( aMIt.key() ) ) );
775 return typeCount.join( ", " );
779 \return reference to type name
780 \param type - integer id of type
782 QString& LightApp_Dialog::typeName( const int type )
784 return myTypeNames[ type ];
788 \return const reference to type name
789 \param type - integer id of type
791 const QString LightApp_Dialog::typeName( const int type ) const
793 return myTypeNames[ type ];
798 Activates object selection widget
799 \param id - identificator of object selection widget
801 void LightApp_Dialog::activateObject( const int theId )
803 if ( myObjects.contains( theId ) && !myObjects[ theId ].myBtn->isChecked() )
804 myObjects[ theId ].myBtn->toggle();
808 Deactivates all object selection widgets
810 void LightApp_Dialog::deactivateAll()
812 ObjectMap::iterator anIt = myObjects.begin(),
813 aLast = myObjects.end();
814 for( ; anIt!=aLast; anIt++ )
816 QToolButton* btn = ( QToolButton* )anIt.value().myBtn;
817 btn->setChecked( false );
822 Passes to widget name, type and id of selected object
823 \param id - identificator of object selection widget
824 \param name - name of selected object
825 \param type - type of selected object
826 \param selid - id of selected object
827 \param update - is need to update selection description string
829 void LightApp_Dialog::selectObject( const int id, const QString& name, const int type, const QString& selid, const bool update )
831 QStringList names; names.append( name );
832 TypesList types; types.append( type );
833 QStringList ids; ids.append( selid );
834 selectObject( id, names, types, ids, update );
838 Passes to widget names, types and ids of selected objects
839 \param id - identificator of object selection widget
840 \param _names - names of selected object
841 \param _types - types of selected object
842 \param _ids - ids of selected object
843 \param update - is need to update selection description string
845 void LightApp_Dialog::selectObject( const int id, const QStringList& _names, const TypesList& _types,
846 const QStringList& _ids, const bool update )
848 if( !myObjects.contains( id ) )
851 QStringList names = _names, ids = _ids;
852 TypesList types = _types;
854 filterTypes( id, names, types, ids );
856 Object& obj = myObjects[ id ];
858 obj.myEdit->setText( selectionDescription( names, types, obj.myNI ) );
863 emit selectionChanged( id );
867 Sets read only state of object selection line edit
868 \param id - identificator of object selection widget
869 \param ro - new read only state
871 void LightApp_Dialog::setReadOnly( const int id, const bool ro )
873 if( myObjects.contains( id ) )
874 myObjects[ id ].myEdit->setReadOnly( nameIndication( id )==ListOfNames || nameIndication( id )==OneName ? ro : true );
878 \return read only state of object selection line edit
879 \param id - identificator of object selection widget
881 bool LightApp_Dialog::isReadOnly( const int id ) const
883 if( myObjects.contains( id ) )
884 return myObjects[ id ].myEdit->isReadOnly();
890 SLOT: called if text of object selection line edit is changed
892 void LightApp_Dialog::onTextChanged( const QString& text )
899 if( sender() && sender()->inherits( "QLineEdit" ) )
901 QLineEdit* edit = ( QLineEdit* )sender();
903 ObjectMap::const_iterator anIt = myObjects.begin(),
904 aLast = myObjects.end();
905 for( ; anIt!=aLast; anIt++ )
906 if( anIt.value().myEdit == edit )
909 if( id>=0 && !isReadOnly( id ) )
911 QStringList list = text.split( " ", QString::SkipEmptyParts );
912 emit objectChanged( id, list );