1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 // File: LightApp_Dialog.cxx
20 // Author: Alexander SOLOVYOV
22 #include <LightApp_Dialog.h>
23 #include <SUIT_Session.h>
25 #include <qtoolbutton.h>
26 #include <qlineedit.h>
32 LightApp_Dialog::LightApp_Dialog( QWidget* parent, const char* name, bool modal,
33 bool allowResize, const int f, WFlags wf )
34 : QtxDialog( parent, name, modal, allowResize, f, wf ),
35 myIsExclusive( true ),
38 setObjectPixmap( "LightApp", tr( "ICON_SELECT" ) );
44 LightApp_Dialog::~LightApp_Dialog()
51 void LightApp_Dialog::show()
57 \return isExclusive status of selection buttons
59 bool LightApp_Dialog::isExclusive() const
65 Updates "on" state of buttons according to special button
66 \param _id - id of special button (if it is -1, then first selected button will be treated as special)
68 void LightApp_Dialog::updateButtons( const int _id )
75 ObjectMap::const_iterator anIt = myObjects.begin(),
76 aLast = myObjects.end();
77 for( ; anIt!=aLast; anIt++ )
79 QToolButton* but = (QToolButton*)anIt.data().myBtn;
80 if( but && but->isOn() )
92 Sets isExclusive status of selection buttons
93 \param ex - new value of isExclusive status
95 void LightApp_Dialog::setExclusive( const bool ex )
102 Shows object selection widget
103 \param id - identificator of object selection widget
105 void LightApp_Dialog::showObject( const int id )
107 setObjectShown( id, true );
111 Hides object selection widget
112 \param id - identificator of object selection widget
114 void LightApp_Dialog::hideObject( const int id )
116 setObjectShown( id, false );
120 Shows/hides object selection widget
121 \param id - identificator of object selection widget
122 \param shown - if it is true, widget will be shown
124 void LightApp_Dialog::setObjectShown( const int id, const bool shown )
126 if( myObjects.contains( id ) && isObjectShown( id )!=shown )
128 Object& obj = myObjects[ id ];
129 obj.myEdit->setShown( shown );
130 obj.myBtn->setShown( shown );
131 obj.myLabel->setShown( shown );
133 ( ( QToolButton* )obj.myBtn )->setOn( false );
138 \return isShown state of object selection widget
139 \param id - identificator of object selection widget
141 bool LightApp_Dialog::isObjectShown( const int id ) const
143 return myObjects.contains( id ) && myObjects[ id ].myEdit->isShown();
147 Change enable state of object selection widget
148 \param id - identificator of object selection widget
149 \param en - new value of enable state
151 void LightApp_Dialog::setObjectEnabled( const int id, const bool en )
153 if( myObjects.contains( id ) && isObjectEnabled( id )!=en )
155 Object& obj = myObjects[ id ];
156 obj.myEdit->setEnabled( en );
157 obj.myBtn->setEnabled( en );
158 // obj.myLabel->setEnabled( en );
160 ( ( QToolButton* )obj.myBtn )->setOn( false );
165 \return enable state of object selection widget
166 \param id - identificator of object selection widget
168 bool LightApp_Dialog::isObjectEnabled( const int id ) const
170 return myObjects.contains( id ) && myObjects[ id ].myEdit->isEnabled();
174 Passes to all active widgets name, type and id of selected object
175 \param name - name of selected object
176 \param type - type of selected object
177 \param id - id of selected object
178 \param update - is need to update selection description string
180 void LightApp_Dialog::selectObject( const QString& name, const int type, const QString& id, const bool update )
182 QStringList names; names.append( name );
183 TypesList types; types.append( type );
184 QStringList ids; ids.append( id );
185 selectObject( names, types, ids, update );
189 Passes to all active widgets names, types and ids of selected objects
190 \param _names - names of selected objects
191 \param _types - types of selected objects
192 \param _ids - ids of selected objects
193 \param update - is need to update selection description string
195 void LightApp_Dialog::selectObject( const QStringList& _names,
196 const TypesList& _types,
197 const QStringList& _ids,
200 ObjectMap::iterator anIt = myObjects.begin(),
201 aLast = myObjects.end();
202 for( ; anIt!=aLast; anIt++ )
203 if( anIt.data().myBtn->isOn() )
204 selectObject( anIt.key(), _names, _types, _ids, update );
208 \return true if widget has selection
209 \param id - identificator of object selection widget
211 bool LightApp_Dialog::hasSelection( const int id ) const
213 return myObjects.contains( id ) && !myObjects[ id ].myIds.isEmpty();
217 Clears selection of widget
218 \param id - identificator of object selection widget
220 void LightApp_Dialog::clearSelection( const int id )
224 ObjectMap::const_iterator anIt = myObjects.begin(),
225 aLast = myObjects.end();
226 for( ; anIt!=aLast; anIt++ )
227 clearSelection( anIt.key() );
230 else if( myObjects.contains( id ) )
232 myObjects[ id ].myIds.clear();
233 myObjects[ id ].myTypes.clear();
234 myObjects[ id ].myNames.clear();
236 myObjects[ id ].myEdit->setText( QString::null );
237 emit selectionChanged( id );
242 \return object selection widget
243 \param theId - identificator of object selection widget
244 \param theWgId may be "Label", "Btn" or "Control"
246 QWidget* LightApp_Dialog::objectWg( const int theId, const int theWgId ) const
249 if( myObjects.contains( theId ) )
251 if ( theWgId == Label )
252 aResWg = myObjects[ theId ].myLabel;
253 else if ( theWgId == Btn )
254 aResWg = myObjects[ theId ].myBtn;
255 else if ( theWgId == Control )
256 aResWg = myObjects[ theId ].myEdit;
262 \return object selection widget text
263 \param theId - identificator of object selection widget
265 QString LightApp_Dialog::objectText( const int theId ) const
267 return myObjects.contains( theId ) ? myObjects[ theId ].myEdit->text() : "";
271 Sets object selection widget text
272 \param theId - identificator of object selection widget
273 \param theText - new text
275 void LightApp_Dialog::setObjectText( const int theId, const QString& theText )
277 if ( myObjects.contains( theId ) )
278 myObjects[ theId ].myEdit->setText( theText );
282 \return objects selected by widget
283 \param id - identificator of object selection widget
284 \param list - list to be filled by selected objects
286 void LightApp_Dialog::selectedObject( const int id, QStringList& list ) const
288 if( myObjects.contains( id ) )
289 list = myObjects[ id ].myIds;
293 \return selected object id
294 \param id - identificator of object selection widget
296 QString LightApp_Dialog::selectedObject( const int id ) const
298 if ( myObjects.contains( id ) && myObjects[ id ].myIds.count() > 0 )
299 return myObjects[ id ].myIds.first();
305 \return all selected objects
306 \param objs - map: widget id -> string id to be filled with selected objects
308 void LightApp_Dialog::objectSelection( SelectedObjects& objs ) const
311 ObjectMap::const_iterator anIt = myObjects.begin(),
312 aLast = myObjects.end();
313 for( ; anIt!=aLast; anIt++ )
316 selectedObject( anIt.key(), ids );
318 objs.insert( anIt.key(), ids );
323 Creates object selection widget
326 \parent - parent object
327 \id - proposed id for widget (if it is less than 0, the free id will be used)
329 int LightApp_Dialog::createObject( const QString& label, QWidget* parent, const int id )
333 for( nid=0; myObjects.contains( nid ); nid++ );
335 if( !myObjects.contains( nid ) )
337 QLabel* lab = new QLabel( label, parent );
338 myObjects[ nid ].myLabel = lab;
340 QToolButton* but = new QToolButton( parent );
341 but->setIconSet( QIconSet( myPixmap ) );
342 but->setToggleButton( true );
343 but->setMaximumWidth( but->height() );
344 but->setMinimumWidth( but->height() );
345 connect( but, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
346 myObjects[ nid ].myBtn = but;
348 QLineEdit* ne = new QLineEdit( parent );
349 ne->setReadOnly( true );
350 ne->setMinimumWidth( 150 );
351 connect( ne, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
352 myObjects[ nid ].myEdit = ne;
354 myObjects[ nid ].myNI = OneNameOrCount;
360 Changes label of object selection widget
361 \param id - identificator of object selection widget
362 \param label - new text of label
364 void LightApp_Dialog::renameObject( const int id, const QString& label )
366 if( myObjects.contains( id ) )
367 myObjects[ id ].myLabel->setText( label );
371 Sets possible types for widget
372 \param id - identificator of object selection widget
373 \param type1,... - type
375 void LightApp_Dialog::setObjectType( const int id, const int type1, ... )
379 const int* tt = &type1;
386 setObjectType( id, types );
390 Sets possible types for widget
391 \param id - identificator of object selection widget
392 \param list - list of possible types
394 void LightApp_Dialog::setObjectType( const int id, const TypesList& list )
396 if( !myObjects.contains( id ) )
399 TypesList& internal = myObjects[ id ].myPossibleTypes;
402 TypesList::const_iterator anIt = list.begin(),
404 for( ; anIt!=aLast; anIt++ )
405 types.insert( *anIt, 0 );
409 QMap<int,int>::const_iterator aMIt = types.begin(),
410 aMLast = types.end();
411 for( ; aMIt!=aMLast; aMIt++ )
412 internal.append( aMIt.key() );
418 Adds new possible types to object selection widget
419 \param id - identificator of object selection widget
420 \param type1, ... - new types
422 void LightApp_Dialog::addObjectType( const int id, const int type1, const int, ... )
424 TypesList types; objectTypes( id, types );
426 const int* tt = &type1;
433 setObjectType( id, types );
437 Adds new possible types to object selection widget
438 \param id - identificator of object selection widget
439 \param list - new types
441 void LightApp_Dialog::addObjectType( const int id, const TypesList& list )
443 TypesList types = list; objectTypes( id, types );
444 setObjectType( id, types );
448 Adds new possible type to object selection widget
449 \param id - identificator of object selection widget
450 \param type - new type
452 void LightApp_Dialog::addObjectType( const int id, const int type )
454 TypesList types; objectTypes( id, types );
455 types.append( type );
456 setObjectType( id, types );
460 Clears list of possibles types for object selection widget
461 \param id - identificator of object selection widget
463 void LightApp_Dialog::removeObjectType( const int id )
466 setObjectType( id, types );
470 Removes types from list of possibles for object selection widget
471 \param id - identificator of object selection widget
472 \param list - list of types to be removed
474 void LightApp_Dialog::removeObjectType( const int id, const TypesList& list )
476 if( !myObjects.contains( id ) )
479 TypesList& internal = myObjects[ id ].myPossibleTypes;
482 TypesList::const_iterator anIt = internal.begin(),
483 aLast = internal.end();
484 for( ; anIt!=aLast; anIt++ )
485 types.insert( *anIt, 0 );
486 anIt = list.begin(); aLast = list.end();
487 for( ; anIt!=aLast; anIt++ )
488 types.remove( *anIt );
492 QMap<int,int>::const_iterator aMIt = types.begin(),
493 aMLast = types.end();
494 for( ; aMIt!=aMLast; aMIt++ )
495 internal.append( aMIt.key() );
501 Removes type from list of possibles for object selection widget
502 \param id - identificator of object selection widget
503 \param type - type to be removed
505 void LightApp_Dialog::removeObjectType( const int id, const int type )
507 TypesList list; list.append( type );
508 removeObjectType( id, list );
512 \return true if widget has such type
513 \param id - identificator of object selection widget
514 \param type - type to be checked
516 bool LightApp_Dialog::hasObjectType( const int id, const int type ) const
518 if( myObjects.contains( id ) )
519 return myObjects[ id ].myPossibleTypes.contains( type );
525 Returns list of possible types for widget
526 \param id - identificator of object selection widget
527 \param list - list to be filled with possible types
529 void LightApp_Dialog::objectTypes( const int id, TypesList& list ) const
531 if( myObjects.contains( id ) )
533 TypesList::const_iterator anIt = myObjects[ id ].myPossibleTypes.begin(),
534 aLast = myObjects[ id ].myPossibleTypes.end();
535 for( ; anIt!=aLast; anIt++ )
536 list.append( *anIt );
541 SLOT: called if selection button is clicked
543 void LightApp_Dialog::onToggled( bool on )
545 QButton* but = ( QButton* )sender();
551 ObjectMap::const_iterator anIt = myObjects.begin(),
552 aLast = myObjects.end();
553 for( ; anIt!=aLast && id==-1; anIt++ )
554 if( anIt.data().myBtn==but )
561 emit objectActivated( id );
564 emit objectDeactivated( id );
568 Updates selection description of widget
569 \param id - identificator of object selection widget
570 \param emit_signal - if it is true, the signal "selection changed" is emitted
572 void LightApp_Dialog::updateObject( const int id, bool emit_signal )
574 if( hasSelection( id ) )
576 Object& obj = myObjects[ id ];
577 filterTypes( id, obj.myNames, obj.myTypes, obj.myIds );
578 obj.myEdit->setText( selectionDescription( obj.myNames, obj.myTypes, obj.myNI ) );
580 emit selectionChanged( id );
585 Finds in list possible types
586 \param id - identificator of object selection widget
587 \param names - list of selected objects names
588 \param types - list of selected objects types
589 \param ids - list of selected objects ids
591 void LightApp_Dialog::filterTypes( const int id, QStringList& names, TypesList& types, QStringList& ids ) const
593 if( !myObjects.contains( id ) )
596 const Object& obj = myObjects[ id ];
597 if( obj.myPossibleTypes.isEmpty() )
600 QStringList new_names, new_ids;
603 TypesList::const_iterator anIt1 = types.begin(),
605 QStringList::const_iterator anIt2 = names.begin(),
607 for( ; anIt1!=aLast; anIt1++, anIt2++, anIt3++ )
608 if( obj.myPossibleTypes.contains( *anIt1 ) )
610 if( new_types.count()==1 && !multipleSelection( id ) )
613 new_names.append( *anIt2 );
614 new_types.append( *anIt1 );
615 new_ids.append( *anIt3 );
623 \return global resource manager
625 SUIT_ResourceMgr* LightApp_Dialog::resMgr() const
627 return SUIT_Session::session()->resourceMgr();
631 Sets pixmap for all object selection button
634 void LightApp_Dialog::setObjectPixmap( const QPixmap& p )
637 ObjectMap::const_iterator anIt = myObjects.begin(),
638 aLast = myObjects.end();
639 for( ; anIt!=aLast; anIt++ )
640 ( ( QToolButton* )anIt.data().myBtn )->setIconSet( p );
644 Sets pixmap all for object selection button
645 \param section - name of section of resource manager
646 \param file - name of file
648 void LightApp_Dialog::setObjectPixmap( const QString& section, const QString& file )
650 SUIT_ResourceMgr* mgr = resMgr();
652 setObjectPixmap( mgr->loadPixmap( section, file ) );
656 \return true, if it is enable multiple selection
657 \param id - identificator of object selection widget
659 bool LightApp_Dialog::multipleSelection( const int id ) const
661 return nameIndication( id )!=OneName;
665 \return type of name indication
666 \param id - identificator of object selection widget
668 LightApp_Dialog::NameIndication LightApp_Dialog::nameIndication( const int id ) const
670 if( myObjects.contains( id ) )
671 return myObjects[ id ].myNI;
673 return OneNameOrCount;
677 Sets type of name indication
678 \param id - identificator of object selection widget
679 \param ni - new type of name indication
681 void LightApp_Dialog::setNameIndication( const int id, const NameIndication ni )
685 ObjectMap::iterator anIt = myObjects.begin(),
687 aLast = myObjects.end();
688 for( ; anIt!=aLast; anIt++ )
690 anIt.data().myNI = ni;
691 setReadOnly( anIt.key(), isReadOnly( anIt.key() ) );
692 aNext = anIt; aNext++;
693 updateObject( anIt.key(), aNext==aLast );
696 else if( myObjects.contains( id ) )
698 myObjects[ id ].myNI = ni;
699 setReadOnly( id, isReadOnly( id ) );
700 updateObject( id, true );
705 \return string representation of selection by selection data
706 \param names - list of selected objects names
707 \param types - list of selected objects types
708 \param ni - type of name indication
710 QString LightApp_Dialog::selectionDescription( const QStringList& names, const TypesList& types, const NameIndication ni ) const
712 if( names.count()!=types.count() )
713 return "LightApp_Dialog::selectionDescription(): Error!!!";
715 if( names.isEmpty() )
716 return QString::null;
721 return names.first();
725 if( names.count()==1 )
726 return names.first();
728 return countOfTypes( types );
732 return names.join( " " );
736 return countOfTypes( types );
739 return QString::null;
743 \return string representation of count of types
744 \param types - list of types
746 QString LightApp_Dialog::countOfTypes( const TypesList& types ) const
748 QMap<int, int> typesCount;
749 QStringList typeCount;
751 TypesList::const_iterator anIt = types.begin(),
753 for( ; anIt!=aLast; anIt++ )
754 if( typesCount.contains( *anIt ) )
755 typesCount[ *anIt ]++;
757 typesCount[ *anIt ] = 1;
759 QMap<int,int>::const_iterator aMIt = typesCount.begin(),
760 aMLast = typesCount.end();
761 for( ; aMIt!=aMLast; aMIt++ )
762 typeCount.append( QString( "%1 %2" ).arg( aMIt.data() ).arg( typeName( aMIt.key() ) ) );
764 return typeCount.join( ", " );
768 \return reference to type name
769 \param type - integer id of type
771 QString& LightApp_Dialog::typeName( const int type )
773 return myTypeNames[ type ];
777 \return const reference to type name
778 \param type - integer id of type
780 const QString& LightApp_Dialog::typeName( const int type ) const
782 return myTypeNames[ type ];
787 Activates object selection widget
788 \param id - identificator of object selection widget
790 void LightApp_Dialog::activateObject( const int theId )
792 if ( myObjects.contains( theId ) && !myObjects[ theId ].myBtn->isOn() )
793 myObjects[ theId ].myBtn->toggle();
797 Deactivates all object selection widgets
799 void LightApp_Dialog::deactivateAll()
801 ObjectMap::iterator anIt = myObjects.begin(),
802 aLast = myObjects.end();
803 for( ; anIt!=aLast; anIt++ )
805 QToolButton* btn = ( QToolButton* )anIt.data().myBtn;
811 Passes to widget name, type and id of selected object
812 \param id - identificator of object selection widget
813 \param name - name of selected object
814 \param type - type of selected object
815 \param selid - id of selected object
816 \param update - is need to update selection description string
818 void LightApp_Dialog::selectObject( const int id, const QString& name, const int type, const QString& selid, const bool update )
820 QStringList names; names.append( name );
821 TypesList types; types.append( type );
822 QStringList ids; ids.append( selid );
823 selectObject( id, names, types, ids, update );
827 Passes to widget names, types and ids of selected objects
828 \param id - identificator of object selection widget
829 \param _names - names of selected object
830 \param _types - types of selected object
831 \param _ids - ids of selected object
832 \param update - is need to update selection description string
834 void LightApp_Dialog::selectObject( const int id, const QStringList& _names, const TypesList& _types,
835 const QStringList& _ids, const bool update )
837 if( !myObjects.contains( id ) )
840 QStringList names = _names, ids = _ids;
841 TypesList types = _types;
843 filterTypes( id, names, types, ids );
845 Object& obj = myObjects[ id ];
847 obj.myEdit->setText( selectionDescription( names, types, obj.myNI ) );
852 emit selectionChanged( id );
856 Sets read only state of object selection line edit
857 \param id - identificator of object selection widget
858 \param ro - new read only state
860 void LightApp_Dialog::setReadOnly( const int id, const bool ro )
862 if( myObjects.contains( id ) )
863 myObjects[ id ].myEdit->setReadOnly( nameIndication( id )==ListOfNames || nameIndication( id )==OneName ? ro : true );
867 \return read only state of object selection line edit
868 \param id - identificator of object selection widget
870 bool LightApp_Dialog::isReadOnly( const int id ) const
872 if( myObjects.contains( id ) )
873 return myObjects[ id ].myEdit->isReadOnly();
879 SLOT: called if text of object selection line edit is changed
881 void LightApp_Dialog::onTextChanged( const QString& text )
888 if( sender() && sender()->inherits( "QLineEdit" ) )
890 QLineEdit* edit = ( QLineEdit* )sender();
892 ObjectMap::const_iterator anIt = myObjects.begin(),
893 aLast = myObjects.end();
894 for( ; anIt!=aLast; anIt++ )
895 if( anIt.data().myEdit == edit )
898 if( id>=0 && !isReadOnly( id ) )
900 QStringList list = QStringList::split( " ", text );
901 emit objectChanged( id, list );