OpenWalnut  1.5.0dev
WColumnPropertyHandler.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include "WColumnPropertyHandler.h"
32 
34  WPropertyGroup::SPtr properties,
36  m_protonData( protonData ),
37  m_properties( properties ),
38  m_dataUpdate( dataUpdate )
39 {
41 }
42 
44 {
46  this, boost::placeholders::_1 );
47 
48  m_columnSelectionGroup = m_properties->addPropertyGroup( "Select columns", "Select the columns which should be used" );
49 
50  std::list< WColumnPropertyHandler::NameDescriptionSearchTyp > names = WSingleSelectorName::getListOfSelectorContent();
51  for( WColumnPropertyHandler::NameDescriptionSearchTyp selectorElement : names )
52  {
53  std::string columnName = std::get< 0 >( selectorElement );
54 
56  std::map< WPropSelection, std::string >::value_type( addHeaderProperty( selectorElement, notifier ), columnName )
57  );
58  }
59 }
60 
62 {
63  m_externEventMethod = externEventMethod;
64 }
65 
67 {
68 }
69 
70 std::shared_ptr< WItemSelection > WColumnPropertyHandler::initializeSelectionItem( std::list< std::string > typeNames )
71 {
72  std::shared_ptr< WItemSelection > possibleSelectionsUsingTypes = WItemSelection::SPtr( new WItemSelection() );
73 
74  std::vector< std::string > header = m_protonData->getHeaderFromType( typeNames );
75 
76  for( std::vector<std::string>::iterator colName = header.begin(); colName != header.end(); colName++ )
77  {
78  possibleSelectionsUsingTypes->addItem( ItemType::create( *colName, *colName, "", NULL ) );
79  }
80 
81  possibleSelectionsUsingTypes->addItem( ItemType::create( "- no selection -", "- no selection -", "", NULL ) );
82 
83  return possibleSelectionsUsingTypes;
84 }
85 
86 int WColumnPropertyHandler::getFilterIndex( int index, std::list< std::string > typeName )
87 {
88  std::vector< std::string > headerToSearch = m_protonData->getCSVHeader()->at( 0 );
89 
90  std::string refheader = headerToSearch.at( index );
91 
92  std::vector< std::string > singleSelectorContent = m_protonData->getHeaderFromType( typeName );
93 
94  size_t indexCounter = 0;
95 
96  for( std::vector<std::string>::iterator colName = singleSelectorContent.begin(); colName != singleSelectorContent.end(); colName++ )
97  {
98  if( *colName == refheader )
99  {
100  return indexCounter;
101  }
102  indexCounter++;
103  }
104 
105  return -1;
106 }
107 
110 {
111  std::string columnName = std::get< 0 >( ndst );
112  std::string description = std::get< 1 >( ndst );
113  std::string defName = std::get< 2 >( ndst );
114  std::list< std::string > type = std::get< 3 >( ndst );
115 
116  int index = m_protonData->getColumnIndex( defName );
117 
118  int indexSingleSelector = index < 0 ? -1 : getFilterIndex( index, type );
119 
120  m_protonData->setStateIndex( columnName, index );
121 
122  std::shared_ptr< WItemSelection > possibleSelectionsUsingTypes = initializeSelectionItem( type );
123 
124  WItemSelector selector = index < 0 ? possibleSelectionsUsingTypes->getSelectorLast() :
125  possibleSelectionsUsingTypes->getSelector( indexSingleSelector );
126 
127  WPropSelection selection = m_columnSelectionGroup->addProperty(
128  columnName,
129  description,
130  selector,
131  notifier );
132 
135 
136  return selection;
137 }
138 
140 {
141  const WItemSelector* selector = NULL;
142  std::string columnName;
143 
144  for( PropMapEntry elem = mapPropSelectionsToString.begin();
145  elem != mapPropSelectionsToString.end();
146  elem++ )
147  {
148  if( property == elem->first )
149  {
150  selector = &elem->first->get( true );
151  columnName = elem->second;
152  break;
153  }
154  }
155 
156  if( selector != NULL )
157  {
158  std::string selectedValue = selector->at( 0 )->getAs< ItemType >()->getValue();
159 
160  m_protonData->setStateIndex( columnName, m_protonData->getColumnIndex( selectedValue ) );
161 
162  m_dataUpdate();
163  if( m_externEventMethod != NULL )
164  {
166  }
167  }
168 }
void propertyNotifier(WPropertyBase::SPtr property)
Event function when WItemSelection is triggered.
std::tuple< std::string, std::string, std::string, std::list< std::string > > NameDescriptionSearchTyp
tuple with 3 string as value
std::map< WPropSelection, std::string >::iterator PropMapEntry
Reresents an entry in a < WPropSelection, string > map.
WPropertyGroup::SPtr m_properties
A property variable that is generated by the WModul.
WPropSelection addHeaderProperty(WColumnPropertyHandler::NameDescriptionSearchTyp ndst, WPropertyBase::PropertyChangeNotifierType notifier)
Creates the individual WItemSelection.
std::shared_ptr< WItemSelection > initializeSelectionItem(std::list< std::string > typeName)
creates the content (options) of the WItemSelection
void updateProperty()
update current group property and subproperty
WPropGroup m_columnSelectionGroup
Property group for column selection.
WColumnPropertyHandler::CallbackPtr m_externEventMethod
A void function variable that can be use outside.
WColumnPropertyHandler::CallbackPtr m_dataUpdate
A function variable that reinitializes the WDataSets.
void createProperties()
creates the group property and the subproperty
std::map< WPropSelection, std::string > mapPropSelectionsToString
A map between WPropSelection items and column names as strings.
void setSelectionEventMethod(WColumnPropertyHandler::CallbackPtr externEventMethod)
setter to use a external function
WColumnPropertyHandler(WProtonData::SPtr protonData, WPropertyGroup::SPtr properties, WColumnPropertyHandler::CallbackPtr dataUpdate)
constructor
boost::function< void() > CallbackPtr
Function variables for updating the data.
int getFilterIndex(int index, std::list< std::string > typeName)
converts the index to the filtered index
WProtonData::SPtr m_protonData
Pointer to the content and header of the CSV.
A derivation of WItemSelection which can store a value of any type.
static SPtr create(T value, std::string name, std::string description="", const char **icon=NULL)
Create a instance of the item.
A class containing a list of named items.
std::shared_ptr< WItemSelection > SPtr
Convenience typedef for a std::shared_ptr< WItemSelection >
This class represents a subset of a WItemSelection.
Definition: WItemSelector.h:53
virtual const std::shared_ptr< WItemSelectionItem > at(size_t index) const
Gets the selected item with the given index.
boost::function< void(std::shared_ptr< WPropertyBase >)> PropertyChangeNotifierType
Signal signature emitted during set operations.
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
std::shared_ptr< WPropertyGroup > SPtr
shared pointer to object of this type
std::shared_ptr< WProtonData > SPtr
shared_ptr that points to itself
Definition: WProtonData.h:52
static std::list< WSingleSelectorName::NameDescriptionSearchTyp > getListOfSelectorContent()
getter
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.