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