1 // File: SalomeApp_ResourceEditor.cxx
3 // Author: Alexander Sladkov
6 #include "SalomeApp_ResourceEditor.h"
11 #include <qcombobox.h>
12 #include <qlineedit.h>
13 #include <qcheckbox.h>
14 #include <qvalidator.h>
15 #include <qcolordialog.h>
17 #include <qwidgetstack.h>
18 #include <qobjectlist.h>
20 #include <QtxDblSpinBox.h>
21 #include <SUIT_OverrideCursor.h>
26 Class: SalomeApp_ResourceEditor::ListItem
27 Descr: GUI implementation of resources list item.
30 SalomeApp_ResourceEditor::ListItem::ListItem( SalomeApp_ResourceEditor* ed, QWidget* parent )
33 mypName = new QLabel( parent );
34 mypList = new QComboBox( false, parent );
37 SalomeApp_ResourceEditor::ListItem::~ListItem()
41 void SalomeApp_ResourceEditor::ListItem::Set( const QString& sect, const QString& key,
42 const int type, const QString& label )
44 Item::Set( sect, key, type, label );
45 mypName->setText( label );
48 void SalomeApp_ResourceEditor::ListItem::SetList( const QValueList<int>& ids,
49 const QStringList& names )
54 for ( uint i = 0; i < QMIN( ids.count(), names.count() ); i++ )
56 mypList->insertItem( *names.at( i ) );
57 myIndex.insert( mypList->count() - 1, *ids.at( i ) );
61 void SalomeApp_ResourceEditor::ListItem::Store()
65 QString sect = Get( key, type, label );
67 if ( !sect.isEmpty() )
70 int iVal, index = mypList->currentItem();
71 if ( GetInteger( sect, key, iVal ) && myIndex.contains( index ) )
72 SetInteger( sect, key, myIndex[index] );
73 else if ( GetString( sect, key, sVal ) )
74 SetString( sect, key, mypList->text( index ) );
78 void SalomeApp_ResourceEditor::ListItem::Retrieve()
82 QString sect = Get( key, type, label );
85 if ( !sect.isEmpty() )
89 if ( GetInteger( sect, key, iVal ) )
91 for ( QMap<int, int>::ConstIterator it = myIndex.begin(); it != myIndex.end() && index == -1; ++it )
92 if ( it.data() == iVal )
95 else if ( GetString( sect, key, sVal ) )
97 for ( int i = 0; i < (int)mypList->count() && index == -1; i++ )
98 if ( mypList->text( i ) == sVal )
102 mypList->setCurrentItem( index );
106 Class: SalomeApp_ResourceEditor::ColorItem
107 Descr: GUI implementation of resources color item.
110 SalomeApp_ResourceEditor::ColorItem::ColorItem( SalomeApp_ResourceEditor* ed, QWidget* parent )
113 mypName = new QLabel( parent );
114 mypColor = new ColorButton( parent );
115 mypColor->setAutoDefault( false );
118 SalomeApp_ResourceEditor::ColorItem::~ColorItem()
122 void SalomeApp_ResourceEditor::ColorItem::Set( const QString& sect, const QString& key,
123 const int type, const QString& label )
125 Item::Set( sect, key, type, label );
126 mypName->setText( label );
129 void SalomeApp_ResourceEditor::ColorItem::Store()
133 QString sect = Get( key, type, label );
135 if ( !sect.isEmpty() )
136 SetColor( sect, key, mypColor->paletteBackgroundColor() );
139 void SalomeApp_ResourceEditor::ColorItem::Retrieve()
144 QString sect = Get( key, type, label );
146 if ( !sect.isEmpty() && GetColor( sect, key, color ) )
147 mypColor->setPaletteBackgroundColor( color );
151 Class: SalomeApp_ResourceEditor::StateItem
152 Descr: GUI implementation of resources bool item.
155 SalomeApp_ResourceEditor::StateItem::StateItem( SalomeApp_ResourceEditor* ed, QWidget* parent )
158 mypName = new QLabel( parent );
159 mypState = new QCheckBox( parent );
162 SalomeApp_ResourceEditor::StateItem::~StateItem()
166 void SalomeApp_ResourceEditor::StateItem::Set( const QString& sect, const QString& key,
167 const int type, const QString& label )
169 Item::Set( sect, key, type, label );
170 mypName->setText( label );
173 void SalomeApp_ResourceEditor::StateItem::Store()
177 QString sect = Get( key, type, label );
178 if ( !sect.isEmpty() )
179 SetBoolean( sect, key, mypState->isChecked() );
182 void SalomeApp_ResourceEditor::StateItem::Retrieve()
187 QString sect = Get( key, type, label );
188 if ( !sect.isEmpty() && GetBoolean( sect, key, state ) )
189 mypState->setChecked( state );
193 Class: SalomeApp_ResourceEditor::StringItem
194 Descr: GUI implementation of resources string item.
197 SalomeApp_ResourceEditor::StringItem::StringItem( SalomeApp_ResourceEditor* ed, QWidget* parent )
200 mypName = new QLabel( parent );
201 mypString = new QLineEdit( parent );
204 SalomeApp_ResourceEditor::StringItem::~StringItem()
208 void SalomeApp_ResourceEditor::StringItem::Set( const QString& sect, const QString& key,
209 const int type, const QString& label )
211 Item::Set( sect, key, type, label );
212 mypName->setText( label );
215 void SalomeApp_ResourceEditor::StringItem::Store()
219 QString sect = Get( key, type, label );
220 if ( !sect.isEmpty() )
221 SetString( sect, key, mypString->text() );
224 void SalomeApp_ResourceEditor::StringItem::Retrieve()
227 QString val, key, label;
228 QString sect = Get( key, type, label );
229 if ( !sect.isEmpty() && GetString( sect, key, val ) )
230 mypString->setText( val );
234 Class: SalomeApp_ResourceEditor::DoubleSpinItem
235 Descr: GUI implementation of resources string item.
238 SalomeApp_ResourceEditor::DoubleSpinItem::DoubleSpinItem
239 ( SalomeApp_ResourceEditor* ed, QWidget* parent )
242 mypName = new QLabel( parent );
243 mypDouble = new QtxDblSpinBox( parent, "spinboxdouble" );
244 mypDouble->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
247 SalomeApp_ResourceEditor::DoubleSpinItem::~DoubleSpinItem()
251 void SalomeApp_ResourceEditor::DoubleSpinItem::Set( const QString& sect, const QString& key,
252 const int type, const QString& label )
254 Item::Set( sect, key, type, label );
255 mypName->setText( label );
256 mypDouble->setName( label ); // for debug purposes..
259 void SalomeApp_ResourceEditor::DoubleSpinItem::SetRange( const double min, const double max,
260 const double prec, const double step )
262 mypDouble->setMinValue( min );
263 mypDouble->setMaxValue( max );
264 mypDouble->setPrecision( prec );
266 mypDouble->setSteps( step, step*5 );
269 void SalomeApp_ResourceEditor::DoubleSpinItem::Store()
273 QString sect = Get( key, type, label );
274 if ( !sect.isEmpty() )
275 SetDouble( sect, key, mypDouble->value() );
278 void SalomeApp_ResourceEditor::DoubleSpinItem::Retrieve()
283 QString sect = Get( key, type, label );
284 if ( sect.isEmpty() && GetDouble( sect, key, val ) )
285 mypDouble->setValue( val );
289 Class: SalomeApp_ResourceEditor::DoubleEditItem
290 Descr: GUI implementation of resources string item.
293 SalomeApp_ResourceEditor::DoubleEditItem::DoubleEditItem
294 ( SalomeApp_ResourceEditor* ed, QWidget* parent )
297 mypName = new QLabel( parent );
298 mypDouble = new QLineEdit( parent );
299 mypDouble->setValidator( new QDoubleValidator( mypDouble ) );
302 SalomeApp_ResourceEditor::DoubleEditItem::~DoubleEditItem()
306 void SalomeApp_ResourceEditor::DoubleEditItem::Set( const QString& sect, const QString& key,
307 const int type, const QString& label )
309 Item::Set( sect, key, type, label );
310 mypName->setText( label );
313 void SalomeApp_ResourceEditor::DoubleEditItem::Store()
317 QString sect = Get( key, type, label );
318 if ( !sect.isEmpty() )
319 SetDouble( sect, key, mypDouble->text().toDouble() );
322 void SalomeApp_ResourceEditor::DoubleEditItem::Retrieve()
327 QString sect = Get( key, type, label );
328 if ( sect.isEmpty() && GetDouble( sect, key, val ) )
329 mypDouble->setText( QString().setNum( val, 'g', 16 ) );
333 Class: SalomeApp_ResourceEditor::IntegerSpinItem
334 Descr: GUI implementation of resources integer item.
337 SalomeApp_ResourceEditor::IntegerSpinItem::IntegerSpinItem
338 ( SalomeApp_ResourceEditor* ed, QWidget* parent )
341 mypName = new QLabel( parent );
342 mypInteger = new QSpinBox( parent );
345 SalomeApp_ResourceEditor::IntegerSpinItem::~IntegerSpinItem()
349 void SalomeApp_ResourceEditor::IntegerSpinItem::Set( const QString& sect, const QString& key,
350 const int type, const QString& label )
352 Item::Set( sect, key, type, label );
353 mypName->setText( label );
356 void SalomeApp_ResourceEditor::IntegerSpinItem::SetRange( const double min, const double max,
357 const double, const double step )
359 mypInteger->setMinValue( (int)min );
360 mypInteger->setMaxValue( (int)max );
361 int aStep = (int)step;
363 mypInteger->setLineStep( aStep );
366 void SalomeApp_ResourceEditor::IntegerSpinItem::Store()
370 QString sect = Get( key, type, label );
371 if ( !sect.isEmpty() )
372 SetInteger( sect, key, mypInteger->value() );
375 void SalomeApp_ResourceEditor::IntegerSpinItem::Retrieve()
379 QString sect = Get( key, type, label );
380 if ( !sect.isEmpty() && GetInteger( sect, key, val ) )
381 mypInteger->setValue( val );
385 Class: SalomeApp_ResourceEditor::IntegerEditItem
386 Descr: GUI implementation of resources integer item.
389 SalomeApp_ResourceEditor::IntegerEditItem::IntegerEditItem
390 ( SalomeApp_ResourceEditor* ed, QWidget* parent )
393 mypName = new QLabel( parent );
394 mypInteger = new QLineEdit( parent );
395 mypInteger->setValidator( new QIntValidator( mypInteger ) );
398 SalomeApp_ResourceEditor::IntegerEditItem::~IntegerEditItem()
402 void SalomeApp_ResourceEditor::IntegerEditItem::Set( const QString& sect, const QString& key,
403 const int type, const QString& label )
405 Item::Set( sect, key, type, label );
406 mypName->setText( label );
409 void SalomeApp_ResourceEditor::IntegerEditItem::Store()
413 QString sect = Get( key, type, label );
414 if ( !sect.isEmpty() )
415 SetInteger( sect, key, mypInteger->text().toInt() );
418 void SalomeApp_ResourceEditor::IntegerEditItem::Retrieve()
422 QString sect = Get( key, type, label );
423 if ( !sect.isEmpty() && GetInteger( sect, key, val ) )
424 mypInteger->setText( QString().setNum( val ) );
428 Class: SalomeApp_ResourceEditor::Spacing
429 Descr: GUI implementation of resources spacer.
432 SalomeApp_ResourceEditor::Spacing::Spacing( SalomeApp_ResourceEditor* ed, QWidget* parent )
435 new QLabel( parent );
436 new QLabel( parent );
439 SalomeApp_ResourceEditor::Spacing::~Spacing()
443 void SalomeApp_ResourceEditor::Spacing::Store()
447 void SalomeApp_ResourceEditor::Spacing::Retrieve()
452 Class: SalomeApp_ResourceEditor::PrefGroup
453 Descr: GUI implementation of resources group.
456 SalomeApp_ResourceEditor::PrefGroup::PrefGroup( const QString& name,
457 SalomeApp_ResourceEditor* ed,
459 : QGroupBox( 4, Qt::Horizontal, name, parent ),
465 SalomeApp_ResourceEditor::PrefGroup::~PrefGroup()
469 void SalomeApp_ResourceEditor::PrefGroup::SetColumns( const int cols )
471 setColumns( 2 * cols );
474 void SalomeApp_ResourceEditor::PrefGroup::SetTitle( const QString& title )
479 SalomeApp_ResourceEditor::Item* SalomeApp_ResourceEditor::PrefGroup::createItem( const int type, const QString& label )
486 item = new ColorItem( myEditor, this );
489 item = new StateItem( myEditor, this );
492 item = new StringItem( myEditor, this );
495 item = new ListItem( myEditor, this );
498 item = new DoubleSpinItem( myEditor, this );
501 item = new IntegerSpinItem( myEditor, this );
504 item = new DoubleEditItem( myEditor, this );
507 item = new IntegerEditItem( myEditor, this );
510 item = new Spacing( myEditor, this );
518 Class: SalomeApp_ResourceEditor::PrefTab
519 Descr: GUI implementation of resources tab.
522 SalomeApp_ResourceEditor::PrefTab::PrefTab( const QString& name,
523 SalomeApp_ResourceEditor* ed,
527 mypTabWidget( parent ),
530 QVBoxLayout* main = new QVBoxLayout( this );
531 mypMainFrame = new QGroupBox( 1, Qt::Horizontal, "", this );
532 mypMainFrame->setFrameStyle( QFrame::NoFrame );
533 mypMainFrame->setInsideMargin( 5 );
534 main->addWidget( mypMainFrame );
537 SalomeApp_ResourceEditor::PrefTab::~PrefTab()
541 void SalomeApp_ResourceEditor::PrefTab::SetTitle( const QString& title )
546 mypTabWidget->setTabLabel( this, title );
549 void SalomeApp_ResourceEditor::PrefTab::adjustLabels()
551 QObjectList* labels = queryList( "QLabel" );
555 for ( QObjectListIt it1( *labels ); it1.current(); ++it1 )
557 if ( it1.current()->isWidgetType() )
559 QWidget* wid = (QWidget*)it1.current();
560 w = QMAX( w, wid->sizeHint().width() );
563 for ( QObjectListIt it2( *labels ); it2.current(); ++it2 )
565 if ( it2.current()->isWidgetType() )
567 QWidget* wid = (QWidget*)it2.current();
568 wid->setMinimumWidth( w );
575 SalomeApp_ResourceEditor::Group* SalomeApp_ResourceEditor::PrefTab::createGroup( const QString& name )
577 return new PrefGroup( name, myEditor, mypMainFrame );
582 Class: SalomeApp_ResourceEditor::PrefCategory
583 Descr: GUI implementation of preferences category.
586 SalomeApp_ResourceEditor::PrefCategory::PrefCategory( const QString& name,
587 SalomeApp_ResourceEditor* ed,
589 QWidgetStack* parent )
590 : QTabWidget( parent ),
592 mypListBox( listBox ),
593 mypWidgetStack( parent ),
598 SalomeApp_ResourceEditor::PrefCategory::~PrefCategory()
602 void SalomeApp_ResourceEditor::PrefCategory::SetTitle( const QString& title )
604 if ( !mypWidgetStack || !mypListBox )
607 int id = mypWidgetStack->id( this );
611 //int idx = mypListBox->index( mypListBox->selectedItem() );
612 int idx = mypListBox->currentItem();
614 mypListBox->changeItem( title, id );
615 mypListBox->setSelected( idx, true );
618 void SalomeApp_ResourceEditor::PrefCategory::adjustLabels()
620 for ( int i = 0; i < count(); i++ )
622 QWidget* wid = page( i );
625 PrefTab* tab = (PrefTab*)wid;
631 SalomeApp_ResourceEditor::Tab* SalomeApp_ResourceEditor::PrefCategory::createTab( const QString& name )
633 PrefTab* aTab = new PrefTab( name, myEditor, this );
634 addTab( aTab, name );
638 //=======================================================================
639 //function : SalomeApp_ResourceEditor
641 //=======================================================================
643 SalomeApp_ResourceEditor::SalomeApp_ResourceEditor( QtxResourceMgr* theMgr,
645 : QtxDialog( theParent, 0, true, false, Standard | Apply ),
646 QtxResourceEditor( theMgr )
648 setCaption( tr( "CAPTION" ) );
650 QHBoxLayout* main = new QHBoxLayout( mainFrame(), 5, 5 );
652 mypCatList = new QListBox( mainFrame() );
653 mypCatStack = new QWidgetStack( mainFrame() );
655 main->addWidget( mypCatList );
656 main->addWidget( mypCatStack );
658 mypCatList->setSelectionMode( QListBox::Single );
660 //int defId = addUserButton( tr( "DEFAULT" ) );
661 //QButton* defButton = userButton( defId );
664 // setUserButtonPosition( Right, defId );
666 setTabOrder( button( OK ), button( Cancel ),
667 /*defButton,*/ button( Help ), 0 );
669 connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
670 connect( this, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
671 connect( mypCatList, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
674 connect( defButton, SIGNAL( clicked() ), this, SLOT( onDefault() ) );*/
676 setFocusProxy( mypCatList );
679 //=======================================================================
680 //function : ~SalomeApp_ResourceEditor
682 //=======================================================================
684 SalomeApp_ResourceEditor::~SalomeApp_ResourceEditor()
688 //=======================================================================
691 //=======================================================================
693 void SalomeApp_ResourceEditor::show()
695 SUIT_OverrideCursor c;
699 for ( QPtrListIterator<Category> it( categories() ); it.current(); ++it )
701 PrefCategory* aCat = (PrefCategory*)it.current();
702 aCat->adjustLabels();
705 Update(); // update all tabs, groups, items.
713 //=======================================================================
716 //=======================================================================
718 void SalomeApp_ResourceEditor::accept()
720 SUIT_OverrideCursor c;
728 //=======================================================================
731 //=======================================================================
733 void SalomeApp_ResourceEditor::reject()
735 SUIT_OverrideCursor c;
743 //=======================================================================
746 //=======================================================================
748 void SalomeApp_ResourceEditor::onHelp()
750 /*LH3DGui_Desktop* desk = LH3DGui_Desktop::getDesktop();
753 LH3DGui_Helper* helper = desk->getHelper();
755 helper->ShowPage( "preferences" );
759 //=======================================================================
762 //=======================================================================
764 void SalomeApp_ResourceEditor::onApply()
766 SUIT_OverrideCursor c;
772 //=======================================================================
773 //function : onSelectionChanged
775 //=======================================================================
777 void SalomeApp_ResourceEditor::onSelectionChanged()
779 //int index = mypCatList->index( mypCatList->selectedItem() );
780 int index = mypCatList->currentItem();
784 mypCatStack->raiseWidget( index );
787 //=======================================================================
788 //function : updateViewer
790 //=======================================================================
792 void SalomeApp_ResourceEditor::updateViewer()
794 /* LH3DGui_Desktop* desktop = LH3DGui_Desktop::getDesktop();
798 LH3DGui_Modeler* active = desktop->getActiveModeler();
802 active->Update( LH3DGui_Desktop::Viewer3d );
803 if ( desktop->getViewer3d() )
804 desktop->getViewer3d()->update();*/
807 //=======================================================================
808 //function : createCategory
810 //=======================================================================
812 SalomeApp_ResourceEditor::Category* SalomeApp_ResourceEditor::createCategory( const QString& name )
814 PrefCategory* aCat = new PrefCategory( name, this, mypCatList, mypCatStack );
815 mypCatList->insertItem( name );
816 int id = mypCatList->count() - 1;
817 mypCatStack->addWidget( aCat, id );
819 if ( !mypCatList->currentItem() )
820 mypCatList->setSelected( 0, true );
825 //=======================================================================
826 //function : setTabOrder
828 //=======================================================================
830 void SalomeApp_ResourceEditor::setTabOrder( QObject* first, ... )
833 va_start( objs, first );
837 QObject* cur = first;
840 objList.append( cur );
841 cur = va_arg( objs, QObject* );
844 setTabOrder( objList );
847 //=======================================================================
848 //function : setTabOrder
850 //=======================================================================
852 void SalomeApp_ResourceEditor::setTabOrder( const QObjectList& objs )
854 QPtrList<QWidget> widgets;
856 for ( QObjectListIt it( objs ); it.current(); ++it )
860 if ( cur->isWidgetType() )
863 widgets.append( wid );
866 if ( widgets.count() < 2 )
869 QWidget* prev = widgets.first();
870 for ( QWidget* next = widgets.at( 1 ); next; next = widgets.next() )
872 QWidget::setTabOrder( prev, next );
880 ColorButton::ColorButton( QWidget* parent )
881 : QPushButton( parent )
883 connect( this, SIGNAL( clicked() ), this, SLOT( onColor() ) );
886 ColorButton::~ColorButton()
890 void ColorButton::onColor()
894 QColor c = QColorDialog::getColor( paletteBackgroundColor(), this );
896 setPaletteBackgroundColor( c );