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