]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersQtDialog.cxx
9f6aadcdbc910fd960cbcb233ff19cc9b0b64c64
[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;
38   parameters->GetNames( names );
39   std::vector< std::string >::const_iterator nIt = names.begin( );
40   for( ; nIt != names.end( ); ++nIt )
41   {
42     Parameters::Type pt = parameters->GetType( *nIt );
43
44     /* TODO
45        enum Type
46        {
47        Bool,
48        Index,
49        Point,
50        StringList,
51        BoolList,
52        IntList,
53        UintList,
54        IndexList,
55        PointList,
56        NoType
57        };
58     */
59     QWidget* w_input = NULL;
60     if( pt == Parameters::String )
61     {
62       QLineEdit* v_string = new QLineEdit( dlg );
63       v_string->setText( "Enter some text!!!" );
64       w_input = v_string;
65     }
66     else if( pt == Parameters::Bool )
67     {
68       QCheckBox* v_bool = new QCheckBox( dlg );
69       v_bool->setText( "[ON/OFF]" );
70       v_bool->setChecked( parameters->GetBool( *nIt ) );
71       w_input = v_bool;
72     }
73     else if( pt == Parameters::Uint )
74     {
75       QSpinBox* v_uint = new QSpinBox( dlg );
76       v_uint->setMinimum( 0 );
77       v_uint->setMaximum( std::numeric_limits< int >::max( ) );
78       v_uint->setValue( parameters->GetUint( *nIt ) );
79       w_input = v_uint;
80     }
81     else if( pt == Parameters::Int )
82     {
83       QSpinBox* v_int = new QSpinBox( dlg );
84       v_int->setMinimum( -std::numeric_limits< int >::max( ) );
85       v_int->setMaximum(  std::numeric_limits< int >::max( ) );
86       v_int->setValue( parameters->GetInt( *nIt ) );
87       w_input = v_int;
88     }
89     else if( pt == Parameters::Real )
90     {
91       QDoubleSpinBox* v_double = new QDoubleSpinBox( dlg );
92       v_double->setDecimals( 3 );
93       v_double->setMinimum( -std::numeric_limits< double >::max( ) );
94       v_double->setMaximum(  std::numeric_limits< double >::max( ) );
95       v_double->setValue( parameters->GetReal( *nIt ) );
96       w_input = v_double;
97     }
98     else if(
99       pt == Parameters::StringList ||
100       pt == Parameters::IntList ||
101       pt == Parameters::UintList ||
102       pt == Parameters::RealList
103       )
104     {
105       cpPlugins::Interface::ParametersListWidget* l_double =
106         new cpPlugins::Interface::ParametersListWidget( *nIt, dlg );
107       w_input = l_double;
108
109     } // fi
110
111     // Ok, a representation was created
112     if( w_input != NULL )
113     {
114       w_input->setObjectName( QString( nIt->c_str( ) ) );
115
116       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
117       QLabel* label = new QLabel( dlg );
118       label->setText( QString( nIt->c_str( ) ) );
119       horizontalLayout->addWidget( label );
120       horizontalLayout->addWidget( w_input );
121       verticalLayout->addLayout( horizontalLayout );
122
123     } // fi
124
125   } // rof
126
127   // Add buttons
128   QDialogButtonBox* bb = new QDialogButtonBox(
129     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
130     );
131   QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
132   QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
133   verticalLayout->addWidget( bb );
134   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
135
136   // Execute
137   QMetaObject::connectSlotsByName( dlg );
138   if( !( dlg->exec( ) ) )
139     return( false );
140
141   // Get values back
142   nIt = names.begin( );
143   for( ; nIt != names.end( ); ++nIt )
144   {
145     Parameters::Type pt = parameters->GetType( *nIt );
146     if( pt == Parameters::String )
147     {
148       QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
149       if( v_string != NULL )
150         parameters->SetString( *nIt, v_string->text( ).toStdString( ) );
151     }
152     else if( pt == Parameters::Bool )
153     {
154       QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
155       if( v_bool != NULL )
156         parameters->SetBool( *nIt, v_bool->isChecked( ) );
157     }
158     else if( pt == Parameters::Uint )
159     {
160       QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
161       if( v_uint != NULL )
162         parameters->SetUint( *nIt, v_uint->value( ) );
163     }
164     else if( pt == Parameters::Int )
165     {
166       QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
167       if( v_int != NULL )
168         parameters->SetInt( *nIt, v_int->value( ) );
169     }
170     else if( pt == Parameters::Real )
171     {
172       QDoubleSpinBox* v_double =
173         dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
174       if( v_double != NULL )
175         parameters->SetReal( *nIt, v_double->value( ) );
176     }
177     else if(
178       pt == Parameters::StringList ||
179       pt == Parameters::IntList ||
180       pt == Parameters::UintList ||
181       pt == Parameters::RealList
182       )
183     {
184       cpPlugins::Interface::ParametersListWidget* l_double =
185         dlg->findChild< cpPlugins::Interface::ParametersListWidget* >(
186           nIt->c_str( )
187           );
188       if( l_double != NULL )
189       {
190         if( pt == Parameters::StringList )
191         {
192           std::vector< std::string > values = l_double->GetStringValues( );
193           for( int r = 0; r < values.size( ); ++r )
194             parameters->AddToStringList( *nIt, values[ r ] );
195         }
196         else if( pt == Parameters::IntList )
197         {
198           std::vector< int > values = l_double->GetIntValues( );
199           for( int r = 0; r < values.size( ); ++r )
200             parameters->AddToIntList( *nIt, values[ r ] );
201         }
202         else if( pt == Parameters::UintList )
203         {
204           std::vector< unsigned int > values = l_double->GetUintValues( );
205           for( int r = 0; r < values.size( ); ++r )
206             parameters->AddToUintList( *nIt, values[ r ] );
207         }
208         else if( pt == Parameters::RealList )
209         {
210           std::vector< double > values = l_double->GetDoubleValues( );
211           for( int r = 0; r < values.size( ); ++r )
212             parameters->AddToRealList( *nIt, values[ r ] );
213
214         } // fi
215
216       } // fi
217
218     } // fi
219
220   } // rof
221   return( true );
222 }
223
224 #endif // cpPlugins_Interface_QT4
225
226 // eof - $RCSfile$