]> 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 <QCheckBox>
7 #include <QDialog>
8 #include <QDialogButtonBox>
9 #include <QDoubleSpinBox>
10 #include <QHBoxLayout>
11 #include <QLabel>
12 #include <QLineEdit>
13 #include <QTableWidget>
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( pt == Parameters::RealList )
96     {
97       QStringList header;
98       header << nIt->c_str( );
99
100       QTableWidget* l_double = new QTableWidget( dlg );
101       l_double->setColumnCount( 1 );
102       l_double->setRowCount( 1 );
103       l_double->setHorizontalHeaderLabels( header );
104       l_double->setShowGrid( true );
105       l_double->setSelectionBehavior( QAbstractItemView::SelectRows );
106       l_double->setSelectionMode( QAbstractItemView::SingleSelection );
107
108       QObject::connect(
109         l_double, SIGNAL( cellDoubleClicked( int, int ) ),
110         l_double, SLOT( insertRow( int ) )
111         );
112
113       w_input = l_double;
114
115     } // fi
116
117     // Ok, a representation was created
118     if( w_input != NULL )
119     {
120       w_input->setObjectName( QString( nIt->c_str( ) ) );
121
122       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
123       QLabel* label = new QLabel( dlg );
124       label->setText( QString( nIt->c_str( ) ) );
125       horizontalLayout->addWidget( label );
126       horizontalLayout->addWidget( w_input );
127       verticalLayout->addLayout( horizontalLayout );
128
129     } // fi
130
131   } // rof
132
133   // Add buttons
134   QDialogButtonBox* bb = new QDialogButtonBox(
135     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
136     );
137   QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
138   QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
139   verticalLayout->addWidget( bb );
140   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
141
142   // Execute
143   QMetaObject::connectSlotsByName( dlg );
144   if( !( dlg->exec( ) ) )
145     return( false );
146
147   // Get values back
148   nIt = names.begin( );
149   for( ; nIt != names.end( ); ++nIt )
150   {
151     Parameters::Type pt = parameters.GetParameterType( *nIt );
152     if( pt == Parameters::String )
153     {
154       QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
155       if( v_string != NULL )
156         parameters.SetValueAsString( *nIt, v_string->text( ).toStdString( ) );
157     }
158     else if( pt == Parameters::Bool )
159     {
160       QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
161       if( v_bool != NULL )
162         parameters.SetValueAsBool( *nIt, v_bool->isChecked( ) );
163     }
164     else if( pt == Parameters::Uint )
165     {
166       QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
167       if( v_uint != NULL )
168         parameters.SetValueAsUint( *nIt, v_uint->value( ) );
169     }
170     else if( pt == Parameters::Int )
171     {
172       QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
173       if( v_int != NULL )
174         parameters.SetValueAsInt( *nIt, v_int->value( ) );
175     }
176     else if( pt == Parameters::Real )
177     {
178       QDoubleSpinBox* v_double =
179         dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
180       if( v_double != NULL )
181         parameters.SetValueAsReal( *nIt, v_double->value( ) );
182     }
183     else if( pt == Parameters::RealList )
184     {
185       QTableWidget* l_double =
186         dlg->findChild< QTableWidget* >( nIt->c_str( ) );
187       if( l_double != NULL )
188       {
189         for( int r = 0; r < l_double->rowCount( ); ++r )
190         {
191           double v =
192             std::atof(
193               l_double->item( r, 0 )->text( ).toStdString( ).c_str( )
194               );
195           parameters.AddValueToRealList( *nIt, v );
196
197         } // rof
198
199       } // fi
200
201     } // fi
202
203   } // rof
204   return( true );
205 }
206
207 // eof - $RCSfile$