]> 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 <QWidget>
14
15 // -------------------------------------------------------------------------
16 bool cpPlugins::Interface::
17 ParametersQtDialog(
18   Parameters& parameters, const std::string& title, QWidget* parent
19   )
20 {
21   // Create dialog with a simple layout
22   QDialog* dlg = new QDialog( parent );
23   dlg->setWindowFlags( Qt::FramelessWindowHint ); 
24   dlg->setWindowFlags( Qt::WindowTitleHint );
25   QGridLayout* gridLayout = new QGridLayout( dlg );
26   QVBoxLayout* verticalLayout = new QVBoxLayout( );
27
28   // Put a title
29   QLabel* dlg_title = new QLabel( dlg );
30   dlg_title->setText( title.c_str( ) );
31   verticalLayout->addWidget( dlg_title );
32
33   // Put values
34   std::vector< std::string > names = parameters.GetParameters( );
35   std::vector< std::string >::const_iterator nIt = names.begin( );
36   for( ; nIt != names.end( ); ++nIt )
37   {
38     Parameters::Type pt = parameters.GetParameterType( *nIt );
39
40     /* TODO
41        enum Type
42        {
43        Bool,
44        Index,
45        Point,
46        StringList,
47        BoolList,
48        IntList,
49        UintList,
50        RealList,
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     } // fi
96
97     // Ok, a representation was created
98     if( w_input != NULL )
99     {
100       w_input->setObjectName( QString( nIt->c_str( ) ) );
101
102       QHBoxLayout* horizontalLayout = new QHBoxLayout( );
103       QLabel* label = new QLabel( dlg );
104       label->setText( QString( nIt->c_str( ) ) );
105       horizontalLayout->addWidget( label );
106       horizontalLayout->addWidget( w_input );
107       verticalLayout->addLayout( horizontalLayout );
108
109     } // fi
110
111   } // rof
112
113   // Add buttons
114   QDialogButtonBox* bb = new QDialogButtonBox(
115     QDialogButtonBox::Ok | QDialogButtonBox::Cancel
116     );
117   QObject::connect( bb, SIGNAL( accepted( ) ), dlg, SLOT( accept( ) ) );
118   QObject::connect( bb, SIGNAL( rejected( ) ), dlg, SLOT( reject( ) ) );
119   verticalLayout->addWidget( bb );
120   gridLayout->addLayout( verticalLayout, 0, 0, 1, 1 );
121
122   // Execute
123   QMetaObject::connectSlotsByName( dlg );
124   if( !( dlg->exec( ) ) )
125     return( false );
126
127   // Get values back
128   nIt = names.begin( );
129   for( ; nIt != names.end( ); ++nIt )
130   {
131     Parameters::Type pt = parameters.GetParameterType( *nIt );
132     if( pt == Parameters::String )
133     {
134       QLineEdit* v_string = dlg->findChild< QLineEdit* >( nIt->c_str( ) );
135       if( v_string != NULL )
136         parameters.SetValueAsString( *nIt, v_string->text( ).toStdString( ) );
137     }
138     else if( pt == Parameters::Bool )
139     {
140       QCheckBox* v_bool = dlg->findChild< QCheckBox* >( nIt->c_str( ) );
141       if( v_bool != NULL )
142         parameters.SetValueAsBool( *nIt, v_bool->isChecked( ) );
143     }
144     else if( pt == Parameters::Uint )
145     {
146       QSpinBox* v_uint = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
147       if( v_uint != NULL )
148         parameters.SetValueAsUint( *nIt, v_uint->value( ) );
149     }
150     else if( pt == Parameters::Int )
151     {
152       QSpinBox* v_int = dlg->findChild< QSpinBox* >( nIt->c_str( ) );
153       if( v_int != NULL )
154         parameters.SetValueAsInt( *nIt, v_int->value( ) );
155     }
156     else if( pt == Parameters::Real )
157     {
158       QDoubleSpinBox* v_double =
159         dlg->findChild< QDoubleSpinBox* >( nIt->c_str( ) );
160       if( v_double != NULL )
161         parameters.SetValueAsReal( *nIt, v_double->value( ) );
162
163     } // fi
164
165   } // rof
166   return( true );
167 }
168
169 // eof - $RCSfile$