Back to index

salome-gui  6.5.0
SVTK_ComboAction.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  SALOME VTKViewer : build VTK viewer into Salome desktop
00021 //  File   : 
00022 //  Author : 
00023 
00024 #include "SVTK_ComboAction.h"
00025 
00026 #include <QComboBox>
00027 #include <QHBoxLayout>
00028 
00029 SVTK_ComboAction::SVTK_ComboAction( QObject* parent )
00030   : QWidgetAction( parent )
00031 {
00032   myCurId = -1;
00033 }
00034 
00035 SVTK_ComboAction::SVTK_ComboAction( const QString& text, QObject* parent )
00036   : QWidgetAction( parent )
00037 {
00038   setToolTip( text );
00039   myCurId = -1;
00040 }
00041 
00042 SVTK_ComboAction::~SVTK_ComboAction()
00043 {
00044   myIcons.clear();
00045 }
00046 
00047 void SVTK_ComboAction::insertItem( const QIcon& icon, const int index )
00048 {
00049   if ( index < 0 || index > myIcons.size() )
00050     myIcons.append( icon );
00051   else
00052     myIcons.insert( index, icon );
00053 
00054   update();
00055 }
00056 
00057 void SVTK_ComboAction::clear()
00058 {
00059   myIcons.clear();
00060   update();
00061 }
00062 
00063 void SVTK_ComboAction::setCurrentIndex( const int id )
00064 {
00065   if ( myCurId != id ) 
00066   {
00067     myCurId = id;
00068     update();
00069   }
00070 }
00071 
00072 int SVTK_ComboAction::currentIndex() const
00073 {
00074   return myCurId;
00075 }
00076 
00077 QWidget* SVTK_ComboAction::createWidget( QWidget* parent )
00078 {
00079   QWidget* w = 0;
00080   if ( parent->inherits("QToolBar") )
00081   {
00082     w = new QWidget( parent );
00083     QHBoxLayout* l = new QHBoxLayout( w );
00084     l->setSpacing(0); l->setMargin(0);
00085     QComboBox* combo = new QComboBox( w );
00086     combo->setFocusPolicy( Qt::NoFocus );
00087     combo->setSizeAdjustPolicy( QComboBox::AdjustToContents );
00088     l->addSpacing( 3 );
00089     l->addWidget( combo );
00090     l->addSpacing( 3 );
00091 
00092     updateCombo( combo );
00093     connect( combo, SIGNAL( activated( int ) ), this, SIGNAL( triggered( int ) ) );
00094   }
00095   return w;
00096 }
00097 
00098 void SVTK_ComboAction::update()
00099 {
00100   QList<QWidget*> aList = createdWidgets();
00101   for ( QList<QWidget*>::const_iterator it = aList.begin(); it != aList.end(); ++it )
00102     updateCombo( qFindChild<QComboBox*>(*it) );
00103 }
00104 
00105 void SVTK_ComboAction::updateCombo( QComboBox* combo )
00106 {
00107   if ( !combo ) return;
00108 
00109   combo->clear();
00110 
00111   for ( QList<QIcon>::const_iterator it = myIcons.begin(); it != myIcons.end(); ++it )
00112     combo->addItem( *it, "" );
00113 
00114   if ( combo->count() > 0 ) {
00115     if ( myCurId < 0 ) myCurId = 0;
00116     combo->setCurrentIndex( myCurId );
00117   }
00118 }