1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, 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.
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
22 // File: LightApp_Dialog.cxx
23 // Author: Alexander SOLOVYOV
25 #include "LightApp_Dialog.h"
26 #include <SUIT_Session.h>
27 #include <SUIT_ResourceMgr.h>
29 #include <QAbstractButton>
30 #include <QToolButton>
37 LightApp_Dialog::LightApp_Dialog( QWidget* parent, const char* name, bool modal,
38 bool allowResize, const int f, Qt::WindowFlags wf )
39 : QtxDialog( parent, modal, allowResize, f, wf ),
40 myIsExclusive( true ),
43 setObjectName( name );
44 setObjectPixmap( "LightApp", tr( "ICON_SELECT" ) );
50 LightApp_Dialog::~LightApp_Dialog()
57 void LightApp_Dialog::show()
63 \return isExclusive status of selection buttons
65 bool LightApp_Dialog::isExclusive() const
71 Updates "on" state of buttons according to special button
72 \param _id - id of special button (if it is -1, then first selected button will be treated as special)
74 void LightApp_Dialog::updateButtons( const int _id )
81 ObjectMap::const_iterator anIt = myObjects.begin(),
82 aLast = myObjects.end();
83 for( ; anIt!=aLast; anIt++ )
85 QToolButton* but = (QToolButton*)anIt.value().myBtn;
86 if( but && but->isChecked() )
92 but->setChecked( false );
98 Sets isExclusive status of selection buttons
99 \param ex - new value of isExclusive status
101 void LightApp_Dialog::setExclusive( const bool ex )
108 Shows object selection widget
109 \param id - identificator of object selection widget
111 void LightApp_Dialog::showObject( const int id )
113 setObjectShown( id, true );
117 Hides object selection widget
118 \param id - identificator of object selection widget
120 void LightApp_Dialog::hideObject( const int id )
122 setObjectShown( id, false );
126 Shows/hides object selection widget
127 \param id - identificator of object selection widget
128 \param shown - if it is true, widget will be shown
130 void LightApp_Dialog::setObjectShown( const int id, const bool shown )
132 if( myObjects.contains( id ) && isObjectShown( id )!=shown )
134 Object& obj = myObjects[ id ];
135 obj.myEdit->setShown( shown );
136 obj.myBtn->setShown( shown );
137 obj.myLabel->setShown( shown );
139 ( ( QToolButton* )obj.myBtn )->setChecked( false );
144 \return isShown state of object selection widget
145 \param id - identificator of object selection widget
147 bool LightApp_Dialog::isObjectShown( const int id ) const
149 return myObjects.contains( id ) &&
150 ( myObjects[ id ].myEdit->isVisible() ||
151 myObjects[ id ].myEdit->isVisibleTo( myObjects[ id ].myEdit->parentWidget() ) );
155 Change enable state of object selection widget
156 \param id - identificator of object selection widget
157 \param en - new value of enable state
159 void LightApp_Dialog::setObjectEnabled( const int id, const bool en )
161 if( myObjects.contains( id ) && isObjectEnabled( id )!=en )
163 Object& obj = myObjects[ id ];
164 obj.myEdit->setEnabled( en );
165 obj.myBtn->setEnabled( en );
166 // obj.myLabel->setEnabled( en );
168 ( ( QToolButton* )obj.myBtn )->setChecked( false );
173 \return enable state of object selection widget
174 \param id - identificator of object selection widget
176 bool LightApp_Dialog::isObjectEnabled( const int id ) const
178 return myObjects.contains( id ) && myObjects[ id ].myEdit->isEnabled();
182 Passes to all active widgets name, type and id of selected object
183 \param name - name of selected object
184 \param type - type of selected object
185 \param id - id of selected object
186 \param update - is need to update selection description string
188 void LightApp_Dialog::selectObject( const QString& name, const int type, const QString& id, const bool update )
190 QStringList names; names.append( name );
191 TypesList types; types.append( type );
192 QStringList ids; ids.append( id );
193 selectObject( names, types, ids, update );
197 Passes to all active widgets names, types and ids of selected objects
198 \param _names - names of selected objects
199 \param _types - types of selected objects
200 \param _ids - ids of selected objects
201 \param update - is need to update selection description string
203 void LightApp_Dialog::selectObject( const QStringList& _names,
204 const TypesList& _types,
205 const QStringList& _ids,
208 ObjectMap::iterator anIt = myObjects.begin(),
209 aLast = myObjects.end();
210 for( ; anIt!=aLast; anIt++ )
211 if( anIt.value().myBtn->isChecked() )
212 selectObject( anIt.key(), _names, _types, _ids, update );
216 \return true if widget has selection
217 \param id - identificator of object selection widget
219 bool LightApp_Dialog::hasSelection( const int id ) const
221 return myObjects.contains( id ) && !myObjects[ id ].myIds.isEmpty();
225 Clears selection of widget
226 \param id - identificator of object selection widget
228 void LightApp_Dialog::clearSelection( const int id )
232 ObjectMap::const_iterator anIt = myObjects.begin(),
233 aLast = myObjects.end();
234 for( ; anIt!=aLast; anIt++ )
235 clearSelection( anIt.key() );
238 else if( myObjects.contains( id ) )
240 myObjects[ id ].myIds.clear();
241 myObjects[ id ].myTypes.clear();
242 myObjects[ id ].myNames.clear();
244 myObjects[ id ].myEdit->setText( QString() );
245 emit selectionChanged( id );
250 \return object selection widget
251 \param theId - identificator of object selection widget
252 \param theWgId may be "Label", "Btn" or "Control"
254 QWidget* LightApp_Dialog::objectWg( const int theId, const int theWgId ) const
257 if( myObjects.contains( theId ) )
259 if ( theWgId == Label )
260 aResWg = myObjects[ theId ].myLabel;
261 else if ( theWgId == Btn )
262 aResWg = myObjects[ theId ].myBtn;
263 else if ( theWgId == Control )
264 aResWg = myObjects[ theId ].myEdit;
270 \return object selection widget text
271 \param theId - identificator of object selection widget
273 QString LightApp_Dialog::objectText( const int theId ) const
275 return myObjects.contains( theId ) ? myObjects[ theId ].myEdit->text() : "";
279 Sets object selection widget text
280 \param theId - identificator of object selection widget
281 \param theText - new text
283 void LightApp_Dialog::setObjectText( const int theId, const QString& theText )
285 if ( myObjects.contains( theId ) )
286 myObjects[ theId ].myEdit->setText( theText );
290 \return objects selected by widget
291 \param id - identificator of object selection widget
292 \param list - list to be filled by selected objects
294 void LightApp_Dialog::selectedObject( const int id, QStringList& list ) const
296 if( myObjects.contains( id ) )
297 list = myObjects[ id ].myIds;
301 \return selected object id
302 \param id - identificator of object selection widget
304 QString LightApp_Dialog::selectedObject( const int id ) const
306 if ( myObjects.contains( id ) && myObjects[ id ].myIds.count() > 0 )
307 return myObjects[ id ].myIds.first();
313 \return all selected objects
314 \param objs - map: widget id -> string id to be filled with selected objects
316 void LightApp_Dialog::objectSelection( SelectedObjects& objs ) const
319 ObjectMap::const_iterator anIt = myObjects.begin(),
320 aLast = myObjects.end();
321 for( ; anIt!=aLast; anIt++ )
324 selectedObject( anIt.key(), ids );
326 objs.insert( anIt.key(), ids );
331 Creates object selection widget
334 \parent - parent object
335 \id - proposed id for widget (if it is less than 0, the free id will be used)
337 int LightApp_Dialog::createObject( const QString& label, QWidget* parent, const int id )
341 for( nid=0; myObjects.contains( nid ); nid++ );
343 if( !myObjects.contains( nid ) )
345 QLabel* lab = new QLabel( label, parent );
346 myObjects[ nid ].myLabel = lab;
348 QToolButton* but = new QToolButton( parent );
349 but->setIcon( QIcon( myPixmap ) );
350 but->setCheckable( true );
351 but->setMaximumWidth( but->height() );
352 but->setMinimumWidth( but->height() );
353 connect( but, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
354 myObjects[ nid ].myBtn = but;
356 QLineEdit* ne = new QLineEdit( parent );
357 ne->setReadOnly( true );
358 ne->setMinimumWidth( 150 );
359 connect( ne, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
360 myObjects[ nid ].myEdit = ne;
362 myObjects[ nid ].myNI = OneNameOrCount;
368 Changes label of object selection widget
369 \param id - identificator of object selection widget
370 \param label - new text of label
372 void LightApp_Dialog::renameObject( const int id, const QString& label )
374 if( myObjects.contains( id ) )
375 myObjects[ id ].myLabel->setText( label );
379 Sets possible types for widget
380 \param id - identificator of object selection widget
381 \param type1,... - type
383 void LightApp_Dialog::setObjectType( const int id, const int type1, ... )
387 const int* tt = &type1;
394 setObjectType( id, types );
398 Sets possible types for widget
399 \param id - identificator of object selection widget
400 \param list - list of possible types
402 void LightApp_Dialog::setObjectType( const int id, const TypesList& list )
404 if( !myObjects.contains( id ) )
407 TypesList& internal = myObjects[ id ].myPossibleTypes;
410 TypesList::const_iterator anIt = list.begin(),
412 for( ; anIt!=aLast; anIt++ )
413 types.insert( *anIt, 0 );
417 QMap<int,int>::const_iterator aMIt = types.begin(),
418 aMLast = types.end();
419 for( ; aMIt!=aMLast; aMIt++ )
420 internal.append( aMIt.key() );
426 Adds new possible types to object selection widget
427 \param id - identificator of object selection widget
428 \param type1, ... - new types
430 void LightApp_Dialog::addObjectType( const int id, const int type1, const int, ... )
432 TypesList types; objectTypes( id, types );
434 const int* tt = &type1;
441 setObjectType( id, types );
445 Adds new possible types to object selection widget
446 \param id - identificator of object selection widget
447 \param list - new types
449 void LightApp_Dialog::addObjectType( const int id, const TypesList& list )
451 TypesList types = list; objectTypes( id, types );
452 setObjectType( id, types );
456 Adds new possible type to object selection widget
457 \param id - identificator of object selection widget
458 \param type - new type
460 void LightApp_Dialog::addObjectType( const int id, const int type )
462 TypesList types; objectTypes( id, types );
463 types.append( type );
464 setObjectType( id, types );
468 Clears list of possibles types for object selection widget
469 \param id - identificator of object selection widget
471 void LightApp_Dialog::removeObjectType( const int id )
474 setObjectType( id, types );
478 Removes types from list of possibles for object selection widget
479 \param id - identificator of object selection widget
480 \param list - list of types to be removed
482 void LightApp_Dialog::removeObjectType( const int id, const TypesList& list )
484 if( !myObjects.contains( id ) )
487 TypesList& internal = myObjects[ id ].myPossibleTypes;
490 TypesList::const_iterator anIt = internal.begin(),
491 aLast = internal.end();
492 for( ; anIt!=aLast; anIt++ )
493 types.insert( *anIt, 0 );
494 anIt = list.begin(); aLast = list.end();
495 for( ; anIt!=aLast; anIt++ )
496 types.remove( *anIt );
500 QMap<int,int>::const_iterator aMIt = types.begin(),
501 aMLast = types.end();
502 for( ; aMIt!=aMLast; aMIt++ )
503 internal.append( aMIt.key() );
509 Removes type from list of possibles for object selection widget
510 \param id - identificator of object selection widget
511 \param type - type to be removed
513 void LightApp_Dialog::removeObjectType( const int id, const int type )
515 TypesList list; list.append( type );
516 removeObjectType( id, list );
520 \return true if widget has such type
521 \param id - identificator of object selection widget
522 \param type - type to be checked
524 bool LightApp_Dialog::hasObjectType( const int id, const int type ) const
526 if( myObjects.contains( id ) )
527 return myObjects[ id ].myPossibleTypes.contains( type );
533 Returns list of possible types for widget
534 \param id - identificator of object selection widget
535 \param list - list to be filled with possible types
537 void LightApp_Dialog::objectTypes( const int id, TypesList& list ) const
539 if( myObjects.contains( id ) )
541 TypesList::const_iterator anIt = myObjects[ id ].myPossibleTypes.begin(),
542 aLast = myObjects[ id ].myPossibleTypes.end();
543 for( ; anIt!=aLast; anIt++ )
544 list.append( *anIt );
549 SLOT: called if selection button is clicked
551 void LightApp_Dialog::onToggled( bool on )
553 QAbstractButton* but = ( QAbstractButton* )sender();
559 ObjectMap::const_iterator anIt = myObjects.begin(),
560 aLast = myObjects.end();
561 for( ; anIt!=aLast && id==-1; anIt++ )
562 if( anIt.value().myBtn==but )
569 emit objectActivated( id );
572 emit objectDeactivated( id );
576 Updates selection description of widget
577 \param id - identificator of object selection widget
578 \param emit_signal - if it is true, the signal "selection changed" is emitted
580 void LightApp_Dialog::updateObject( const int id, bool emit_signal )
582 if( hasSelection( id ) )
584 Object& obj = myObjects[ id ];
585 filterTypes( id, obj.myNames, obj.myTypes, obj.myIds );
586 obj.myEdit->setText( selectionDescription( obj.myNames, obj.myTypes, obj.myNI ) );
588 emit selectionChanged( id );
593 Finds in list possible types
594 \param id - identificator of object selection widget
595 \param names - list of selected objects names
596 \param types - list of selected objects types
597 \param ids - list of selected objects ids
599 void LightApp_Dialog::filterTypes( const int id, QStringList& names, TypesList& types, QStringList& ids ) const
601 if( !myObjects.contains( id ) )
604 const Object& obj = myObjects[ id ];
605 if( obj.myPossibleTypes.isEmpty() )
608 QStringList new_names, new_ids;
611 TypesList::const_iterator anIt1 = types.begin(),
613 QStringList::const_iterator anIt2 = names.begin(),
615 for( ; anIt1!=aLast; anIt1++, anIt2++, anIt3++ )
616 if( obj.myPossibleTypes.contains( *anIt1 ) )
618 if( new_types.count()==1 && !multipleSelection( id ) )
621 new_names.append( *anIt2 );
622 new_types.append( *anIt1 );
623 new_ids.append( *anIt3 );
631 \return global resource manager
633 SUIT_ResourceMgr* LightApp_Dialog::resMgr() const
635 return SUIT_Session::session()->resourceMgr();
639 Sets pixmap for all object selection button
642 void LightApp_Dialog::setObjectPixmap( const QPixmap& p )
645 ObjectMap::const_iterator anIt = myObjects.begin(),
646 aLast = myObjects.end();
647 for( ; anIt!=aLast; anIt++ )
648 ( ( QToolButton* )anIt.value().myBtn )->setIcon( p );
652 Sets pixmap all for object selection button
653 \param section - name of section of resource manager
654 \param file - name of file
656 void LightApp_Dialog::setObjectPixmap( const QString& section, const QString& file )
658 SUIT_ResourceMgr* mgr = resMgr();
660 setObjectPixmap( mgr->loadPixmap( section, file ) );
664 \return true, if it is enable multiple selection
665 \param id - identificator of object selection widget
667 bool LightApp_Dialog::multipleSelection( const int id ) const
669 return nameIndication( id )!=OneName;
673 \return type of name indication
674 \param id - identificator of object selection widget
676 LightApp_Dialog::NameIndication LightApp_Dialog::nameIndication( const int id ) const
678 if( myObjects.contains( id ) )
679 return myObjects[ id ].myNI;
681 return OneNameOrCount;
685 Sets type of name indication
686 \param id - identificator of object selection widget
687 \param ni - new type of name indication
689 void LightApp_Dialog::setNameIndication( const int id, const NameIndication ni )
693 ObjectMap::iterator anIt = myObjects.begin(),
695 aLast = myObjects.end();
696 for( ; anIt!=aLast; anIt++ )
698 anIt.value().myNI = ni;
699 setReadOnly( anIt.key(), isReadOnly( anIt.key() ) );
700 aNext = anIt; aNext++;
701 updateObject( anIt.key(), aNext==aLast );
704 else if( myObjects.contains( id ) )
706 myObjects[ id ].myNI = ni;
707 setReadOnly( id, isReadOnly( id ) );
708 updateObject( id, true );
713 \return string representation of selection by selection data
714 \param names - list of selected objects names
715 \param types - list of selected objects types
716 \param ni - type of name indication
718 QString LightApp_Dialog::selectionDescription( const QStringList& names, const TypesList& types, const NameIndication ni ) const
720 if( names.count()!=types.count() )
721 return "LightApp_Dialog::selectionDescription(): Error!!!";
723 if( names.isEmpty() )
729 return names.first();
733 if( names.count()==1 )
734 return names.first();
736 return countOfTypes( types );
740 return names.join( " " );
744 return countOfTypes( types );
751 \return string representation of count of types
752 \param types - list of types
754 QString LightApp_Dialog::countOfTypes( const TypesList& types ) const
756 QMap<int, int> typesCount;
757 QStringList typeCount;
759 TypesList::const_iterator anIt = types.begin(),
761 for( ; anIt!=aLast; anIt++ )
762 if( typesCount.contains( *anIt ) )
763 typesCount[ *anIt ]++;
765 typesCount[ *anIt ] = 1;
767 QMap<int,int>::const_iterator aMIt = typesCount.begin(),
768 aMLast = typesCount.end();
769 for( ; aMIt!=aMLast; aMIt++ )
770 typeCount.append( QString( "%1 %2" ).arg( aMIt.value() ).arg( typeName( aMIt.key() ) ) );
772 return typeCount.join( ", " );
776 \return reference to type name
777 \param type - integer id of type
779 QString& LightApp_Dialog::typeName( const int type )
781 return myTypeNames[ type ];
785 \return const reference to type name
786 \param type - integer id of type
788 const QString LightApp_Dialog::typeName( const int type ) const
790 return myTypeNames[ type ];
795 Activates object selection widget
796 \param id - identificator of object selection widget
798 void LightApp_Dialog::activateObject( const int theId )
800 if ( myObjects.contains( theId ) && !myObjects[ theId ].myBtn->isChecked() )
801 myObjects[ theId ].myBtn->toggle();
805 Deactivates all object selection widgets
807 void LightApp_Dialog::deactivateAll()
809 ObjectMap::iterator anIt = myObjects.begin(),
810 aLast = myObjects.end();
811 for( ; anIt!=aLast; anIt++ )
813 QToolButton* btn = ( QToolButton* )anIt.value().myBtn;
814 btn->setChecked( false );
819 Passes to widget name, type and id of selected object
820 \param id - identificator of object selection widget
821 \param name - name of selected object
822 \param type - type of selected object
823 \param selid - id of selected object
824 \param update - is need to update selection description string
826 void LightApp_Dialog::selectObject( const int id, const QString& name, const int type, const QString& selid, const bool update )
828 QStringList names; names.append( name );
829 TypesList types; types.append( type );
830 QStringList ids; ids.append( selid );
831 selectObject( id, names, types, ids, update );
835 Passes to widget names, types and ids of selected objects
836 \param id - identificator of object selection widget
837 \param _names - names of selected object
838 \param _types - types of selected object
839 \param _ids - ids of selected object
840 \param update - is need to update selection description string
842 void LightApp_Dialog::selectObject( const int id, const QStringList& _names, const TypesList& _types,
843 const QStringList& _ids, const bool update )
845 if( !myObjects.contains( id ) )
848 QStringList names = _names, ids = _ids;
849 TypesList types = _types;
851 filterTypes( id, names, types, ids );
853 Object& obj = myObjects[ id ];
855 obj.myEdit->setText( selectionDescription( names, types, obj.myNI ) );
860 emit selectionChanged( id );
864 Sets read only state of object selection line edit
865 \param id - identificator of object selection widget
866 \param ro - new read only state
868 void LightApp_Dialog::setReadOnly( const int id, const bool ro )
870 if( myObjects.contains( id ) )
871 myObjects[ id ].myEdit->setReadOnly( nameIndication( id )==ListOfNames || nameIndication( id )==OneName ? ro : true );
875 \return read only state of object selection line edit
876 \param id - identificator of object selection widget
878 bool LightApp_Dialog::isReadOnly( const int id ) const
880 if( myObjects.contains( id ) )
881 return myObjects[ id ].myEdit->isReadOnly();
887 SLOT: called if text of object selection line edit is changed
889 void LightApp_Dialog::onTextChanged( const QString& text )
896 if( sender() && sender()->inherits( "QLineEdit" ) )
898 QLineEdit* edit = ( QLineEdit* )sender();
900 ObjectMap::const_iterator anIt = myObjects.begin(),
901 aLast = myObjects.end();
902 for( ; anIt!=aLast; anIt++ )
903 if( anIt.value().myEdit == edit )
906 if( id>=0 && !isReadOnly( id ) )
908 QStringList list = text.split( " ", QString::SkipEmptyParts );
909 emit objectChanged( id, list );