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>
30 Class : LightApp_Dialog
31 Description : Base class for all dialogs
34 //=======================================================================
35 // name : LightApp_Dialog
36 // Purpose : Constructor
37 //=======================================================================
38 LightApp_Dialog::LightApp_Dialog( QWidget* parent, const char* name, bool modal,
39 bool allowResize, const int f, WFlags wf )
40 : QtxDialog( parent, name, modal, allowResize, f, wf ),
41 myIsExclusive( true ),
44 setObjectPixmap( "LightApp", tr( "ICON_SELECT" ) );
47 //=======================================================================
48 // name : ~LightApp_Dialog
49 // Purpose : Destructor
50 //=======================================================================
51 LightApp_Dialog::~LightApp_Dialog()
55 //=======================================================================
58 //=======================================================================
59 void LightApp_Dialog::show()
64 //=======================================================================
67 //=======================================================================
68 bool LightApp_Dialog::isExclusive() const
73 //=======================================================================
74 // name : updateButtons
76 //=======================================================================
77 void LightApp_Dialog::updateButtons( const int _id )
84 ObjectMap::const_iterator anIt = myObjects.begin(),
85 aLast = myObjects.end();
86 for( ; anIt!=aLast; anIt++ )
88 QToolButton* but = (QToolButton*)anIt.data().myBtn;
89 if( but && but->isOn() )
100 //=======================================================================
101 // name : setExclusive
103 //=======================================================================
104 void LightApp_Dialog::setExclusive( const bool ex )
110 //=======================================================================
113 //=======================================================================
114 void LightApp_Dialog::showObject( const int id )
116 setObjectShown( id, true );
119 //=======================================================================
122 //=======================================================================
123 void LightApp_Dialog::hideObject( const int id )
125 setObjectShown( id, false );
128 //=======================================================================
129 // name : setObjectShown
131 //=======================================================================
132 void LightApp_Dialog::setObjectShown( const int id, const bool shown )
134 if( myObjects.contains( id ) && isObjectShown( id )!=shown )
136 Object& obj = myObjects[ id ];
137 obj.myEdit->setShown( shown );
138 obj.myBtn->setShown( shown );
139 obj.myLabel->setShown( shown );
141 ( ( QToolButton* )obj.myBtn )->setOn( false );
145 //=======================================================================
146 // name : isObjectShown
148 //=======================================================================
149 bool LightApp_Dialog::isObjectShown( const int id ) const
151 return myObjects.contains( id ) && myObjects[ id ].myEdit->isShown();
154 //=======================================================================
155 // name : setObjectEnabled
157 //=======================================================================
158 void LightApp_Dialog::setObjectEnabled( const int id, const bool en )
160 if( myObjects.contains( id ) && isObjectEnabled( id )!=en )
162 Object& obj = myObjects[ id ];
163 obj.myEdit->setEnabled( en );
164 obj.myBtn->setEnabled( en );
165 // obj.myLabel->setEnabled( en );
167 ( ( QToolButton* )obj.myBtn )->setOn( false );
171 //=======================================================================
172 // name : isObjectEnabled
174 //=======================================================================
175 bool LightApp_Dialog::isObjectEnabled( const int id ) const
177 return myObjects.contains( id ) && myObjects[ id ].myEdit->isEnabled();
180 //=======================================================================
181 // name : selectObject
183 //=======================================================================
184 void LightApp_Dialog::selectObject( const QString& name, const int type, const QString& id, const bool update )
186 QStringList names; names.append( name );
187 TypesList types; types.append( type );
188 QStringList ids; ids.append( id );
189 selectObject( names, types, ids, update );
192 //=======================================================================
193 // name : selectObject
195 //=======================================================================
196 void LightApp_Dialog::selectObject( const QStringList& _names,
197 const TypesList& _types,
198 const QStringList& _ids,
201 ObjectMap::iterator anIt = myObjects.begin(),
202 aLast = myObjects.end();
203 for( ; anIt!=aLast; anIt++ )
204 if( anIt.data().myBtn->isOn() )
205 selectObject( anIt.key(), _names, _types, _ids, update );
208 //=======================================================================
209 // name : hasSelection
211 //=======================================================================
212 bool LightApp_Dialog::hasSelection( const int id ) const
214 return myObjects.contains( id ) && !myObjects[ id ].myIds.isEmpty();
217 //=======================================================================
218 // name : clearSelection
220 //=======================================================================
221 void LightApp_Dialog::clearSelection( const int id )
225 ObjectMap::const_iterator anIt = myObjects.begin(),
226 aLast = myObjects.end();
227 for( ; anIt!=aLast; anIt++ )
228 clearSelection( anIt.key() );
231 else if( myObjects.contains( id ) )
233 myObjects[ id ].myIds.clear();
234 myObjects[ id ].myTypes.clear();
235 myObjects[ id ].myNames.clear();
237 myObjects[ id ].myEdit->setText( QString::null );
238 emit selectionChanged( id );
242 //=======================================================================
245 //=======================================================================
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;
261 //=======================================================================
264 //=======================================================================
265 QString LightApp_Dialog::objectText( const int theId ) const
267 return myObjects.contains( theId ) ? myObjects[ theId ].myEdit->text() : "";
270 //=======================================================================
271 // name : setObjectText
273 //=======================================================================
274 void LightApp_Dialog::setObjectText( const int theId, const QString& theText )
276 if ( myObjects.contains( theId ) )
277 myObjects[ theId ].myEdit->setText( theText );
280 //=======================================================================
281 // name : selectedObject
283 //=======================================================================
284 void LightApp_Dialog::selectedObject( const int id, QStringList& list ) const
286 if( myObjects.contains( id ) )
287 list = myObjects[ id ].myIds;
290 //=======================================================================
291 // name : selectedObject
293 //=======================================================================
294 QString LightApp_Dialog::selectedObject( const int id ) const
296 if ( myObjects.contains( id ) && myObjects[ id ].myIds.count() > 0 )
297 return myObjects[ id ].myIds.first();
302 //=======================================================================
303 // name : objectSelection
305 //=======================================================================
306 void LightApp_Dialog::objectSelection( SelectedObjects& objs ) const
309 ObjectMap::const_iterator anIt = myObjects.begin(),
310 aLast = myObjects.end();
311 for( ; anIt!=aLast; anIt++ )
314 selectedObject( anIt.key(), ids );
316 objs.insert( anIt.key(), ids );
320 //=======================================================================
321 // name : createObject
323 //=======================================================================
324 int LightApp_Dialog::createObject( const QString& label, QWidget* parent, const int id )
328 for( nid=0; myObjects.contains( nid ); nid++ );
330 if( !myObjects.contains( nid ) )
332 QLabel* lab = new QLabel( label, parent );
333 myObjects[ nid ].myLabel = lab;
335 QToolButton* but = new QToolButton( parent );
336 but->setIconSet( QIconSet( myPixmap ) );
337 but->setToggleButton( true );
338 but->setMaximumWidth( but->height() );
339 but->setMinimumWidth( but->height() );
340 connect( but, SIGNAL( toggled( bool ) ), this, SLOT( onToggled( bool ) ) );
341 myObjects[ nid ].myBtn = but;
343 QLineEdit* ne = new QLineEdit( parent );
344 ne->setReadOnly( true );
345 ne->setMinimumWidth( 150 );
346 connect( ne, SIGNAL( textChanged( const QString& ) ), this, SLOT( onTextChanged( const QString& ) ) );
347 myObjects[ nid ].myEdit = ne;
349 myObjects[ nid ].myNI = OneNameOrCount;
354 //=======================================================================
355 // name : renameObject
357 //=======================================================================
358 void LightApp_Dialog::renameObject( const int id, const QString& label )
360 if( myObjects.contains( id ) )
361 myObjects[ id ].myLabel->setText( label );
364 //=======================================================================
365 // name : setObjectType
367 //=======================================================================
368 void LightApp_Dialog::setObjectType( const int id, const int type1, ... )
372 const int* tt = &type1;
379 setObjectType( id, types );
382 //=======================================================================
383 // name : setObjectType
385 //=======================================================================
386 void LightApp_Dialog::setObjectType( const int id, const TypesList& list )
388 if( !myObjects.contains( id ) )
391 TypesList& internal = myObjects[ id ].myPossibleTypes;
394 TypesList::const_iterator anIt = list.begin(),
396 for( ; anIt!=aLast; anIt++ )
397 types.insert( *anIt, 0 );
401 QMap<int,int>::const_iterator aMIt = types.begin(),
402 aMLast = types.end();
403 for( ; aMIt!=aMLast; aMIt++ )
404 internal.append( aMIt.key() );
409 //=======================================================================
410 // name : addObjectType
412 //=======================================================================
413 void LightApp_Dialog::addObjectType( const int id, const int type1, const int, ... )
415 TypesList types; objectTypes( id, types );
417 const int* tt = &type1;
424 setObjectType( id, types );
427 //=======================================================================
428 // name : addObjectType
430 //=======================================================================
431 void LightApp_Dialog::addObjectType( const int id, const TypesList& list )
433 TypesList types = list; objectTypes( id, types );
434 setObjectType( id, types );
437 //=======================================================================
438 // name : addObjectType
440 //=======================================================================
441 void LightApp_Dialog::addObjectType( const int id, const int type )
443 TypesList types; objectTypes( id, types );
444 types.append( type );
445 setObjectType( id, types );
448 //=======================================================================
449 // name : removeObjectType
451 //=======================================================================
452 void LightApp_Dialog::removeObjectType( const int id )
455 setObjectType( id, types );
458 //=======================================================================
459 // name : removeObjectType
461 //=======================================================================
462 void LightApp_Dialog::removeObjectType( const int id, const TypesList& list )
464 if( !myObjects.contains( id ) )
467 TypesList& internal = myObjects[ id ].myPossibleTypes;
470 TypesList::const_iterator anIt = internal.begin(),
471 aLast = internal.end();
472 for( ; anIt!=aLast; anIt++ )
473 types.insert( *anIt, 0 );
474 anIt = list.begin(); aLast = list.end();
475 for( ; anIt!=aLast; anIt++ )
476 types.remove( *anIt );
480 QMap<int,int>::const_iterator aMIt = types.begin(),
481 aMLast = types.end();
482 for( ; aMIt!=aMLast; aMIt++ )
483 internal.append( aMIt.key() );
488 //=======================================================================
489 // name : removeObjectType
491 //=======================================================================
492 void LightApp_Dialog::removeObjectType( const int id, const int type )
494 TypesList list; list.append( type );
495 removeObjectType( id, list );
498 //=======================================================================
499 // name : hasObjectType
501 //=======================================================================
502 bool LightApp_Dialog::hasObjectType( const int id, const int type ) const
504 if( myObjects.contains( id ) )
505 return myObjects[ id ].myPossibleTypes.contains( type );
510 //=======================================================================
511 // name : objectTypes
513 //=======================================================================
514 void LightApp_Dialog::objectTypes( const int id, TypesList& list ) const
516 if( myObjects.contains( id ) )
518 TypesList::const_iterator anIt = myObjects[ id ].myPossibleTypes.begin(),
519 aLast = myObjects[ id ].myPossibleTypes.end();
520 for( ; anIt!=aLast; anIt++ )
521 list.append( *anIt );
525 //=======================================================================
528 //=======================================================================
529 void LightApp_Dialog::onToggled( bool on )
531 QButton* but = ( QButton* )sender();
537 ObjectMap::const_iterator anIt = myObjects.begin(),
538 aLast = myObjects.end();
539 for( ; anIt!=aLast && id==-1; anIt++ )
540 if( anIt.data().myBtn==but )
547 emit objectActivated( id );
550 emit objectDeactivated( id );
553 //=======================================================================
554 // name : updateObject
556 //=======================================================================
557 void LightApp_Dialog::updateObject( const int id, bool emit_signal )
559 if( hasSelection( id ) )
561 Object& obj = myObjects[ id ];
562 filterTypes( id, obj.myNames, obj.myTypes, obj.myIds );
563 obj.myEdit->setText( selectionDescription( obj.myNames, obj.myTypes, obj.myNI ) );
565 emit selectionChanged( id );
569 //=======================================================================
570 // name : filterTypes
572 //=======================================================================
573 void LightApp_Dialog::filterTypes( const int id, QStringList& names, TypesList& types, QStringList& ids ) const
575 if( !myObjects.contains( id ) )
578 const Object& obj = myObjects[ id ];
579 if( obj.myPossibleTypes.isEmpty() )
582 QStringList new_names, new_ids;
585 TypesList::const_iterator anIt1 = types.begin(),
587 QStringList::const_iterator anIt2 = names.begin(),
589 for( ; anIt1!=aLast; anIt1++, anIt2++, anIt3++ )
590 if( obj.myPossibleTypes.contains( *anIt1 ) )
592 if( new_types.count()==1 && !multipleSelection( id ) )
595 new_names.append( *anIt2 );
596 new_types.append( *anIt1 );
597 new_ids.append( *anIt3 );
604 //=======================================================================
607 //=======================================================================
608 SUIT_ResourceMgr* LightApp_Dialog::resMgr() const
610 return SUIT_Session::session()->resourceMgr();
613 //=======================================================================
614 // name : setObjectPixmap
616 //=======================================================================
617 void LightApp_Dialog::setObjectPixmap( const QPixmap& p )
620 ObjectMap::const_iterator anIt = myObjects.begin(),
621 aLast = myObjects.end();
622 for( ; anIt!=aLast; anIt++ )
623 ( ( QToolButton* )anIt.data().myBtn )->setIconSet( p );
626 //=======================================================================
627 // name : setObjectPixmap
629 //=======================================================================
630 void LightApp_Dialog::setObjectPixmap( const QString& section, const QString& file )
632 SUIT_ResourceMgr* mgr = resMgr();
634 setObjectPixmap( mgr->loadPixmap( section, file ) );
637 //=======================================================================
638 // name : multipleSelection
640 //=======================================================================
641 bool LightApp_Dialog::multipleSelection( const int id ) const
643 return nameIndication( id )!=OneName;
646 //=======================================================================
647 // name : nameIndication
649 //=======================================================================
650 LightApp_Dialog::NameIndication LightApp_Dialog::nameIndication( const int id ) const
652 if( myObjects.contains( id ) )
653 return myObjects[ id ].myNI;
655 return OneNameOrCount;
658 //=======================================================================
659 // name : setNameIndication
661 //=======================================================================
662 void LightApp_Dialog::setNameIndication( const int id, const NameIndication ni )
666 ObjectMap::iterator anIt = myObjects.begin(),
668 aLast = myObjects.end();
669 for( ; anIt!=aLast; anIt++ )
671 anIt.data().myNI = ni;
672 setReadOnly( anIt.key(), isReadOnly( anIt.key() ) );
673 aNext = anIt; aNext++;
674 updateObject( anIt.key(), aNext==aLast );
677 else if( myObjects.contains( id ) )
679 myObjects[ id ].myNI = ni;
680 setReadOnly( id, isReadOnly( id ) );
681 updateObject( id, true );
685 //=======================================================================
686 // name : selectionDescription
688 //=======================================================================
689 QString LightApp_Dialog::selectionDescription( const QStringList& names, const TypesList& types, const NameIndication ni ) const
691 if( names.count()!=types.count() )
692 return "LightApp_Dialog::selectionDescription(): Error!!!";
694 if( names.isEmpty() )
695 return QString::null;
700 return names.first();
704 if( names.count()==1 )
705 return names.first();
707 return countOfTypes( types );
711 return names.join( " " );
715 return countOfTypes( types );
718 return QString::null;
721 //=======================================================================
722 // name : countOfTypes
724 //=======================================================================
725 QString LightApp_Dialog::countOfTypes( const TypesList& types ) const
727 QMap<int, int> typesCount;
728 QStringList typeCount;
730 TypesList::const_iterator anIt = types.begin(),
732 for( ; anIt!=aLast; anIt++ )
733 if( typesCount.contains( *anIt ) )
734 typesCount[ *anIt ]++;
736 typesCount[ *anIt ] = 1;
738 QMap<int,int>::const_iterator aMIt = typesCount.begin(),
739 aMLast = typesCount.end();
740 for( ; aMIt!=aMLast; aMIt++ )
741 typeCount.append( QString( "%1 %2" ).arg( aMIt.data() ).arg( typeName( aMIt.key() ) ) );
743 return typeCount.join( ", " );
746 //=======================================================================
749 //=======================================================================
750 QString& LightApp_Dialog::typeName( const int type )
752 return myTypeNames[ type ];
755 //=======================================================================
758 //=======================================================================
759 const QString& LightApp_Dialog::typeName( const int type ) const
761 return myTypeNames[ type ];
765 //=======================================================================
766 // name : activateObject
768 //=======================================================================
769 void LightApp_Dialog::activateObject( const int theId )
771 if ( myObjects.contains( theId ) && !myObjects[ theId ].myBtn->isOn() )
772 myObjects[ theId ].myBtn->toggle();
775 //=======================================================================
776 // name : deactivateAll
778 //=======================================================================
779 void LightApp_Dialog::deactivateAll()
781 ObjectMap::iterator anIt = myObjects.begin(),
782 aLast = myObjects.end();
783 for( ; anIt!=aLast; anIt++ )
785 QToolButton* btn = ( QToolButton* )anIt.data().myBtn;
790 //=======================================================================
791 // name : selectObject
793 //=======================================================================
794 void LightApp_Dialog::selectObject( const int id, const QString& name, const int type, const QString& selid, const bool update )
796 QStringList names; names.append( name );
797 TypesList types; types.append( type );
798 QStringList ids; ids.append( selid );
799 selectObject( id, names, types, ids, update );
802 //=======================================================================
803 // name : selectObject
805 //=======================================================================
806 void LightApp_Dialog::selectObject( const int id, const QStringList& _names, const TypesList& _types,
807 const QStringList& _ids, const bool update )
809 if( !myObjects.contains( id ) )
812 QStringList names = _names, ids = _ids;
813 TypesList types = _types;
815 filterTypes( id, names, types, ids );
817 Object& obj = myObjects[ id ];
819 obj.myEdit->setText( selectionDescription( names, types, obj.myNI ) );
824 emit selectionChanged( id );
827 //=======================================================================
828 // name : setReadOnly
830 //=======================================================================
831 void LightApp_Dialog::setReadOnly( const int id, const bool ro )
833 if( myObjects.contains( id ) )
834 myObjects[ id ].myEdit->setReadOnly( nameIndication( id )==ListOfNames || nameIndication( id )==OneName ? ro : true );
837 //=======================================================================
840 //=======================================================================
841 bool LightApp_Dialog::isReadOnly( const int id ) const
843 if( myObjects.contains( id ) )
844 return myObjects[ id ].myEdit->isReadOnly();
849 //=======================================================================
850 // name : onTextChanged
852 //=======================================================================
853 void LightApp_Dialog::onTextChanged( const QString& text )
860 if( sender() && sender()->inherits( "QLineEdit" ) )
862 QLineEdit* edit = ( QLineEdit* )sender();
864 ObjectMap::const_iterator anIt = myObjects.begin(),
865 aLast = myObjects.end();
866 for( ; anIt!=aLast; anIt++ )
867 if( anIt.data().myEdit == edit )
870 if( id>=0 && !isReadOnly( id ) )
872 QStringList list = QStringList::split( " ", text );
873 emit objectChanged( id, list );