]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / ParametersQtDialog.cxx
1 #include <cpPlugins/Interface/ParametersQtDialog.h>
2
3 #include <limits>
4 #include <vector>
5
6 #include <cpPlugins/Interface/ParametersListWidget.h>
7 #include <QCheckBox>
8 #include <QDialog>
9 #include <QDialogButtonBox>
10 #include <QDoubleSpinBox>
11 #include <QHBoxLayout>
12 #include <QLabel>
13 #include <QLineEdit>
14 #include <QWidget>
15
16 // -------------------------------------------------------------------------
17 bool cpPlugins::Interface::
18 ParametersQtDialog(
19   Parameters& parameters, const std::string& title, QWidget* parent
20   )
21 {
22   // Create dialog with a simple layout
23   QDialog* dlg = new QDialog( parent );
24   dlg->setWindowFlags( Qt::FramelessWindowHint ); 
25   dlg->setWindowFlags( Qt::WindowTitleHint );
26   QGridLayout* gridLayout = new QGridLayout( dlg );
27   QVBoxLayout* verticalLayout = new QVBoxLayout( );
28
29   // Put a title
30   QLabel* dlg_title = new QLabel( dlg );
31   dlg_title->setText( title.c_str( ) );
32   verticalLayout->addWidget( dlg_title );
33
34   // Put values
35   std::vector< std::string > names = parameters.GetParameters( );
36   std::vector< std::string >::const_iterator nIt = names.begin( );
37   for( ; nIt != names.end( ); ++nIt )
38   {
39     Parameters::Type pt = parameters.GetParameterType( *nIt );
40
41     /* TODO
42        enum Type
43        {
44        Bool,
45        Index,
46        Point,
47        StringList,
48        BoolList,
49        IntList,
50        UintList,
51        IndexList,
52        PointList,
53        NoType
54        };
55     */
56     QWidget* w_input = NULL;
57     if( pt == Parameters::String )
58     {
59       QLineEdit* v_string = new QLineEdit( dlg );
60       v_string->setText( "Enter some text!!!" );
61       w_input = v_string;
62     }
63     else if( pt == Parameters::Bool )
64     {
65       QCheckBox* v_bool = new QCheckBox( dlg );
66       v_bool->setText( "[ON/OFF]" );
67       v_bool->setChecked( parameters.GetValueAsBool( *nIt ) );
68       w_input = v_bool;
69     }
70     else if( pt == Parameters::Uint )
71     {
72       QSpinBox* v_uint = new QSpinBox( dlg );
73       v_uint->setMinimum( 0 );
74       v_uint->setMaximum( std::numeric_limits< int >::max( ) );
75       v_uint->setValue( parameters.GetValueAsUint( *nIt ) );
76       w_input = v_uint;
77     }
78     else if( pt == Parameters::Int )
79     {
80       QSpinBox* v_int = new QSpinBox( dlg );
81       v_int->setMinimum( -std::numeric_limits< int >::max( ) );
82       v_int->setMaximum(  std::numeric_limits< int >::max( ) );
83       v_int->setValue( parameters.GetValueAsInt( *nIt ) );
84       w_input = v_int;
85     }
86     else if( pt == Parameters::Real )
87     {
88       QDoubleSpinBox* v_double = new QDoubleSpinBox( dlg );
89       v_double->setDecimals( 3 );
90       v_double->setMinimum( -std::numeric_limits< double >::max( ) );
91       v_double->setMaximum(  std::numeric_limits< double >::max( ) );
92       v_double->setValue( parameters.GetValueAsReal( *nIt ) );
93       w_input = v_double;
94     }
95     else if(
96       pt == Parameters::StringList ||
97       pt == Parameters::IntList ||
98       pt == Parameters::UintList ||
99       pt == Parameters::RealList
100       )
101     {
102       cpPlugins::Interface::ParametersListWidget* l_double =
103         new cpPlugins::Interface::ParametersListWidget( *nIt, dlg );
104       w_input = l_double;
105
106     } // fi
107
108     // Ok, a representation was created
109     if( w_input != NULL )
110     {
111       w_input->setObjectName( QString( nIt->c_str( ) ) );
112
113       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
114       QLabel* label = new QLabel( dlg );
115       label->setText( QString( nIt->c_str( ) ) );
116       horizontalLayout->addWidget( label );
117       horizontalLayout->addWidget( w_input );
118       verticalLayout->addLayout( horizontalLayout );
119
120     } // fi
121
122   } // rof
123
124   // Add buttons
125   QDialogButtonBox* bb = new QDialogButtonBox(
126     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
127     );
128   QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
129   QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
130   verticalLayout->addWidget( bb );
131   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
132
133   // Execute
134   QMetaObject::connectSlotsByName( dlg );
135   if( !( dlg->exec( ) ) )
136     return( false );
137
138   // Get values back
139   nIt = names.begin( );
140   for( ; nIt != names.end( ); ++nIt )
141   {
142     Parameters::Type pt = parameters.GetParameterType( *nIt );
143     if( pt == Parameters::String )
144     {
145       QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
146       if( v_string != NULL )
147         parameters.SetValueAsString( *nIt, v_string->text( ).toStdString( ) );
148     }
149     else if( pt == Parameters::Bool )
150     {
151       QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
152       if( v_bool != NULL )
153         parameters.SetValueAsBool( *nIt, v_bool->isChecked( ) );
154     }
155     else if( pt == Parameters::Uint )
156     {
157       QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
158       if( v_uint != NULL )
159         parameters.SetValueAsUint( *nIt, v_uint->value( ) );
160     }
161     else if( pt == Parameters::Int )
162     {
163       QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
164       if( v_int != NULL )
165         parameters.SetValueAsInt( *nIt, v_int->value( ) );
166     }
167     else if( pt == Parameters::Real )
168     {
169       QDoubleSpinBox* v_double =
170         dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
171       if( v_double != NULL )
172         parameters.SetValueAsReal( *nIt, v_double->value( ) );
173     }
174     else if(
175       pt == Parameters::StringList ||
176       pt == Parameters::IntList ||
177       pt == Parameters::UintList ||
178       pt == Parameters::RealList
179       )
180     {
181       cpPlugins::Interface::ParametersListWidget* l_double =
182         dlg->findChild< cpPlugins::Interface::ParametersListWidget* >(
183           nIt->c_str( )
184           );
185       if( l_double != NULL )
186       {
187         if( pt == Parameters::StringList )
188         {
189           std::vector< std::string > values = l_double->GetStringValues( );
190           for( int r = 0; r < values.size( ); ++r )
191             parameters.AddValueToStringList( *nIt, values[ r ] );
192         }
193         else if( pt == Parameters::IntList )
194         {
195           std::vector< int > values = l_double->GetIntValues( );
196           for( int r = 0; r < values.size( ); ++r )
197             parameters.AddValueToIntList( *nIt, values[ r ] );
198         }
199         else if( pt == Parameters::UintList )
200         {
201           std::vector< unsigned int > values = l_double->GetUintValues( );
202           for( int r = 0; r < values.size( ); ++r )
203             parameters.AddValueToUintList( *nIt, values[ r ] );
204         }
205         else if( pt == Parameters::RealList )
206         {
207           std::vector< double > values = l_double->GetDoubleValues( );
208           for( int r = 0; r < values.size( ); ++r )
209             parameters.AddValueToRealList( *nIt, values[ r ] );
210
211         } // fi
212
213       } // fi
214
215     } // fi
216
217   } // rof
218   return( true );
219 }
220
221 // eof - $RCSfile$