]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ParametersListWidget.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / ParametersListWidget.cxx
1 #include <cpPlugins/Interface/ParametersListWidget.h>
2 #include <cpPlugins/Interface/ui_ParametersListWidget.h>
3
4 #include <set>
5
6 // -------------------------------------------------------------------------
7 cpPlugins::Interface::ParametersListWidget::
8 ParametersListWidget( const std::string& list_name, QWidget* parent )
9   : QWidget( parent ),
10     m_UI( new Ui::ParametersListWidget )
11 {
12   this->m_UI->setupUi( this );
13
14   // Configure table
15   QTableWidget* table = this->m_UI->ValuesTable;
16   table->setColumnCount( 1 );
17   table->setRowCount( 1 );
18
19   QStringList header;
20   header << list_name.c_str( );
21   table->setHorizontalHeaderLabels( header );
22   table->setShowGrid( true );
23   table->setSelectionBehavior( QAbstractItemView::SelectRows );
24
25   // Connect signals
26   QObject::connect(
27     this->m_UI->AddValueButton, SIGNAL( clicked( ) ),
28     this, SLOT( _changeValuesCount( ) )
29     );
30   QObject::connect(
31     this->m_UI->RemoveValueButton, SIGNAL( clicked( ) ),
32     this, SLOT( _changeValuesCount( ) )
33     );
34 }
35
36 // -------------------------------------------------------------------------
37 cpPlugins::Interface::ParametersListWidget::
38 ~ParametersListWidget( )
39 {
40   delete this->m_UI;
41 }
42
43 // -------------------------------------------------------------------------
44 std::vector< std::string > cpPlugins::Interface::ParametersListWidget::
45 GetStringValues( ) const
46 {
47   std::vector< std::string > values;
48
49   QTableWidget* table = this->m_UI->ValuesTable;
50   for( int i = 0; i < table->rowCount( ); ++i )
51     values.push_back( table->item( i, 0 )->text( ).toStdString( ) );
52   return( values );
53 }
54
55 // -------------------------------------------------------------------------
56 std::vector< int > cpPlugins::Interface::ParametersListWidget::
57 GetIntValues( ) const
58 {
59   std::vector< int > values;
60
61   QTableWidget* table = this->m_UI->ValuesTable;
62   for( int i = 0; i < table->rowCount( ); ++i )
63   {
64     const char* text = table->item( i, 0 )->text( ).toStdString( ).c_str( );
65     char* ptr = 0;
66     double v = std::strtod( text, &ptr );
67     if( *ptr == '\0' && ptr != text )
68       values.push_back( int( v ) );
69     
70   } // rof
71   return( values );
72 }
73
74 // -------------------------------------------------------------------------
75 std::vector< unsigned int > cpPlugins::Interface::ParametersListWidget::
76 GetUintValues( ) const
77 {
78   std::vector< unsigned int > values;
79
80   QTableWidget* table = this->m_UI->ValuesTable;
81   for( int i = 0; i < table->rowCount( ); ++i )
82   {
83     const char* text = table->item( i, 0 )->text( ).toStdString( ).c_str( );
84     char* ptr = 0;
85     double v = std::strtod( text, &ptr );
86     if( *ptr == '\0' && ptr != text && v >= double( 0 ) )
87       values.push_back( ( unsigned int )( v ) );
88
89   } // rof
90   return( values );
91 }
92
93 // -------------------------------------------------------------------------
94 std::vector< double > cpPlugins::Interface::ParametersListWidget::
95 GetDoubleValues( ) const
96 {
97   std::vector< double > values;
98
99   QTableWidget* table = this->m_UI->ValuesTable;
100   for( int i = 0; i < table->rowCount( ); ++i )
101   {
102     const char* text = table->item( i, 0 )->text( ).toStdString( ).c_str( );
103     char* ptr = 0;
104     double v = std::strtod( text, &ptr );
105     if( *ptr == '\0' && ptr != text )
106       values.push_back( v );
107     
108   } // rof
109   return( values );
110 }
111
112 // -------------------------------------------------------------------------
113 void cpPlugins::Interface::ParametersListWidget::
114 _changeValuesCount( )
115 {
116   QPushButton* btn = dynamic_cast< QPushButton* >( this->sender( ) );
117   if( btn == NULL )
118     return;
119   QTableWidget* table = this->m_UI->ValuesTable;
120
121   if( btn == this->m_UI->RemoveValueButton )
122   {
123     std::set< int > to_delete;
124     QList< QTableWidgetItem* > lst = table->selectedItems( );
125     QList< QTableWidgetItem* >::iterator i = lst.begin( );
126     for( ; i != lst.end( ); ++i )
127       to_delete.insert( ( *i )->row( ) );
128
129     std::set< int >::const_reverse_iterator d = to_delete.rbegin( );
130     for( ; d != to_delete.rend( ); ++d )
131       table->removeRow( *d );
132   }
133   else
134     table->insertRow( table->rowCount( ) );
135 }
136
137 // eof - $RCSfile$