Salome HOME
bec4331438f90594061701dac213fd40060fa2fe
[modules/gui.git] / src / SVTK / SVTK_ComboAction.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME VTKViewer : build VTK viewer into Salome desktop
23 //  File   : 
24 //  Author : 
25 //  Module : SALOME
26 //  $Header: 
27 //
28 #include "SVTK_ComboAction.h"
29
30 #include <QComboBox>
31 #include <QHBoxLayout>
32
33 SVTK_ComboAction::SVTK_ComboAction( QObject* parent )
34   : QWidgetAction( parent )
35 {
36   myCurId = -1;
37 }
38
39 SVTK_ComboAction::SVTK_ComboAction( const QString& text, QObject* parent )
40   : QWidgetAction( parent )
41 {
42   setToolTip( text );
43   myCurId = -1;
44 }
45
46 SVTK_ComboAction::~SVTK_ComboAction()
47 {
48   myIcons.clear();
49 }
50
51 void SVTK_ComboAction::insertItem( const QIcon& icon, const int index )
52 {
53   if ( index < 0 || index > myIcons.size() )
54     myIcons.append( icon );
55   else
56     myIcons.insert( index, icon );
57
58   update();
59 }
60
61 void SVTK_ComboAction::clear()
62 {
63   myIcons.clear();
64   update();
65 }
66
67 void SVTK_ComboAction::setCurrentIndex( const int id )
68 {
69   if ( myCurId != id ) 
70   {
71     myCurId = id;
72     update();
73   }
74 }
75
76 int SVTK_ComboAction::currentIndex() const
77 {
78   return myCurId;
79 }
80
81 QWidget* SVTK_ComboAction::createWidget( QWidget* parent )
82 {
83   QWidget* w = 0;
84   if ( parent->inherits("QToolBar") )
85   {
86     w = new QWidget( parent );
87     QHBoxLayout* l = new QHBoxLayout( w );
88     l->setSpacing(0); l->setMargin(0);
89     QComboBox* combo = new QComboBox( w );
90     combo->setFocusPolicy( Qt::NoFocus );
91     combo->setSizeAdjustPolicy( QComboBox::AdjustToContents );
92     l->addSpacing( 3 );
93     l->addWidget( combo );
94     l->addSpacing( 3 );
95
96     updateCombo( combo );
97     connect( combo, SIGNAL( activated( int ) ), this, SIGNAL( triggered( int ) ) );
98   }
99   return w;
100 }
101
102 void SVTK_ComboAction::update()
103 {
104   QList<QWidget*> aList = createdWidgets();
105   for ( QList<QWidget*>::const_iterator it = aList.begin(); it != aList.end(); ++it )
106     updateCombo( qFindChild<QComboBox*>(*it) );
107 }
108
109 void SVTK_ComboAction::updateCombo( QComboBox* combo )
110 {
111   if ( !combo ) return;
112
113   combo->clear();
114
115   for ( QList<QIcon>::const_iterator it = myIcons.begin(); it != myIcons.end(); ++it )
116     combo->addItem( *it, "" );
117
118   if ( combo->count() > 0 ) {
119     if ( myCurId < 0 ) myCurId = 0;
120     combo->setCurrentIndex( myCurId );
121   }
122 }