Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkPropertiesTableModel.h"
00019
00020
00021 #include "mitkStringProperty.h"
00022 #include "mitkColorProperty.h"
00023 #include "mitkProperties.h"
00024 #include "mitkEnumerationProperty.h"
00025 #include "mitkRenderingManager.h"
00026 #include "QmitkCustomVariants.h"
00027
00028
00029 #include <itkCommand.h>
00030 #include <QColor>
00031 #include <QBrush>
00032 #include <QStringList>
00033
00034
00035 QmitkPropertiesTableModel::QmitkPropertiesTableModel(QObject* parent, mitk::PropertyList::Pointer _PropertyList)
00036 : QAbstractTableModel(parent)
00037 , m_PropertyList(0)
00038 , m_BlockEvents(false)
00039 , m_SortDescending(false)
00040 , m_FilterKeyWord("")
00041 {
00042 this->SetPropertyList(_PropertyList);
00043 }
00044
00045 QmitkPropertiesTableModel::~QmitkPropertiesTableModel()
00046 {
00047
00048 this->SetPropertyList(0);
00049 }
00050
00051
00052 mitk::PropertyList::Pointer QmitkPropertiesTableModel::GetPropertyList() const
00053 {
00054 return m_PropertyList.GetPointer();
00055 }
00056
00057 Qt::ItemFlags QmitkPropertiesTableModel::flags(const QModelIndex& index) const
00058 {
00059
00060 Qt::ItemFlags flags = QAbstractItemModel::flags(index);
00061
00062 if (index.column() == PROPERTY_VALUE_COLUMN)
00063 {
00064
00065 if(index.data(Qt::EditRole).isValid())
00066 flags |= Qt::ItemIsEditable;
00067
00068 if(index.data(Qt::CheckStateRole).isValid())
00069 flags |= Qt::ItemIsUserCheckable;
00070 }
00071
00072 if (index.column() == PROPERTY_ACTIVE_COLUMN)
00073 {
00074 flags |= Qt::ItemIsUserCheckable;
00075 }
00076
00077 return flags;
00078 }
00079
00080 QVariant QmitkPropertiesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
00081 {
00082 if (role != Qt::DisplayRole)
00083 return QVariant();
00084
00085 if (orientation == Qt::Horizontal) {
00086 switch (section)
00087 {
00088 case PROPERTY_NAME_COLUMN:
00089 return tr("Name");
00090
00091 case PROPERTY_VALUE_COLUMN:
00092 return tr("Value");
00093
00094 case PROPERTY_ACTIVE_COLUMN:
00095 return tr("Active");
00096
00097 default:
00098 return QVariant();
00099 }
00100 }
00101
00102 return QVariant();
00103 }
00104
00105
00106 QVariant QmitkPropertiesTableModel::data(const QModelIndex& index, int role) const
00107 {
00108
00109 QVariant data;
00110
00111 if(!index.isValid() || m_SelectedProperties.empty() || index.row() > (int)(m_SelectedProperties.size()-1))
00112 return data;
00113
00114
00115 if(index.column() == PROPERTY_NAME_COLUMN)
00116 {
00117 if(role == Qt::DisplayRole)
00118 data = QString::fromStdString(m_SelectedProperties[index.row()].first);
00119 }
00120
00121 else if(index.column() == PROPERTY_VALUE_COLUMN)
00122 {
00123 mitk::BaseProperty* baseProp = m_SelectedProperties[index.row()].second.first;
00124
00125 if (const mitk::ColorProperty* colorProp
00126 = dynamic_cast<const mitk::ColorProperty*>(baseProp))
00127 {
00128 mitk::Color col = colorProp->GetColor();
00129 QColor qcol((int)(col.GetRed() * 255), (int)(col.GetGreen() * 255),(int)( col.GetBlue() * 255));
00130 if(role == Qt::DisplayRole)
00131 data.setValue<QColor>(qcol);
00132 else if(role == Qt::EditRole)
00133 data.setValue<QColor>(qcol);
00134 }
00135
00136 else if(mitk::BoolProperty* boolProp = dynamic_cast<mitk::BoolProperty*>(baseProp))
00137 {
00138 if(role == Qt::CheckStateRole)
00139 data = boolProp->GetValue() ? Qt::Checked : Qt::Unchecked;
00140 }
00141
00142 else if (mitk::StringProperty* stringProp = dynamic_cast<mitk::StringProperty*>(baseProp))
00143 {
00144 if(role == Qt::DisplayRole)
00145 data.setValue<QString>(QString::fromStdString(stringProp->GetValue()));
00146 else if(role == Qt::EditRole)
00147 data.setValue<QString>(QString::fromStdString(stringProp->GetValue()));
00148
00149 }
00150
00151 else if (mitk::IntProperty* intProp = dynamic_cast<mitk::IntProperty*>(baseProp))
00152 {
00153 if(role == Qt::DisplayRole)
00154 data.setValue<int>(intProp->GetValue());
00155 else if(role == Qt::EditRole)
00156 data.setValue<int>(intProp->GetValue());
00157 }
00158
00159 else if (mitk::FloatProperty* floatProp = dynamic_cast<mitk::FloatProperty*>(baseProp))
00160 {
00161 if(role == Qt::DisplayRole)
00162 data.setValue<float>(floatProp->GetValue());
00163 else if(role == Qt::EditRole)
00164 data.setValue<float>(floatProp->GetValue());
00165
00166 }
00167
00168 else if (mitk::EnumerationProperty* enumerationProp = dynamic_cast<mitk::EnumerationProperty*>(baseProp))
00169 {
00170 if(role == Qt::DisplayRole)
00171 data.setValue<QString>(QString::fromStdString(baseProp->GetValueAsString()));
00172 else if(role == Qt::EditRole)
00173 {
00174 QStringList values;
00175 for(mitk::EnumerationProperty::EnumConstIterator it=enumerationProp->Begin(); it!=enumerationProp->End()
00176 ; it++)
00177 {
00178 values << QString::fromStdString(it->second);
00179 }
00180 data.setValue<QStringList>(values);
00181 }
00182 }
00183
00184 else
00185 {
00186 if(role == Qt::DisplayRole)
00187 data.setValue<QString>(QString::fromStdString(m_SelectedProperties[index.row()].second.first->GetValueAsString()));
00188 }
00189 }
00190
00191
00192 else if(index.column() == PROPERTY_ACTIVE_COLUMN)
00193 {
00194 if (role == Qt::CheckStateRole)
00195 data = (m_SelectedProperties[index.row()].second.second) ? Qt::Checked : Qt::Unchecked;
00196 }
00197
00198 return data;
00199 }
00200
00201 int QmitkPropertiesTableModel::rowCount(const QModelIndex& ) const
00202 {
00203
00204 return m_SelectedProperties.size();
00205 }
00206
00207 int QmitkPropertiesTableModel::columnCount(const QModelIndex & )const
00208 {
00209 return 3;
00210 }
00211
00212
00213 void QmitkPropertiesTableModel::SetPropertyList( mitk::PropertyList* _PropertyList )
00214 {
00215
00216 if(m_PropertyList.GetPointer() != _PropertyList)
00217 {
00218
00219 if(m_PropertyList.IsNotNull())
00220 {
00221 m_PropertyList.ObjectDelete.RemoveListener
00222 (mitk::MessageDelegate1<QmitkPropertiesTableModel
00223 , const itk::Object*>( this, &QmitkPropertiesTableModel::PropertyListDelete ));
00224 }
00225
00226
00227 m_PropertyList = _PropertyList;
00228
00229 if(m_PropertyList.IsNotNull())
00230 {
00231 m_PropertyList.ObjectDelete.AddListener
00232 (mitk::MessageDelegate1<QmitkPropertiesTableModel
00233 , const itk::Object*>( this, &QmitkPropertiesTableModel::PropertyListDelete ));
00234 }
00235 this->Reset();
00236 }
00237 }
00238
00239 void QmitkPropertiesTableModel::PropertyListDelete( const itk::Object * )
00240 {
00241 if(!m_BlockEvents)
00242 {
00243 m_BlockEvents = true;
00244 this->Reset();
00245 m_BlockEvents = false;
00246 }
00247 }
00248
00249 void QmitkPropertiesTableModel::PropertyModified( const itk::Object *caller, const itk::EventObject & )
00250 {
00251 if(!m_BlockEvents)
00252 {
00253 m_BlockEvents = true;
00254 int row = this->FindProperty(dynamic_cast<const mitk::BaseProperty*>(caller));
00255
00256 QModelIndex indexOfChangedProperty = index(row, 1);
00257
00258 emit dataChanged(indexOfChangedProperty, indexOfChangedProperty);
00259 m_BlockEvents = false;
00260 }
00261 }
00262
00263 void QmitkPropertiesTableModel::PropertyDelete( const itk::Object *caller, const itk::EventObject & )
00264 {
00265 if(!m_BlockEvents)
00266 {
00267 m_BlockEvents = true;
00268 int row = this->FindProperty(dynamic_cast<const mitk::BaseProperty*>(caller));
00269 if(row >= 0)
00270 this->Reset();
00271 m_BlockEvents = false;
00272 }
00273 }
00274
00275 bool QmitkPropertiesTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
00276 {
00277 if (index.isValid() && !m_SelectedProperties.empty() && index.row() < (int)(m_SelectedProperties.size())
00278 && (role == Qt::EditRole || Qt::CheckStateRole))
00279 {
00280
00281 m_BlockEvents = true;
00282
00283
00284 if(index.column() == PROPERTY_VALUE_COLUMN)
00285 {
00286 mitk::BaseProperty* baseProp = m_SelectedProperties[index.row()].second.first;
00287
00288 if (mitk::ColorProperty* colorProp
00289 = dynamic_cast<mitk::ColorProperty*>(baseProp))
00290 {
00291 QColor qcolor = value.value<QColor>();
00292 if(!qcolor.isValid())
00293 return false;
00294
00295 mitk::Color col = colorProp->GetColor();
00296 col.SetRed(qcolor.red() / 255.0);
00297 col.SetGreen(qcolor.green() / 255.0);
00298 col.SetBlue(qcolor.blue() / 255.0);
00299 colorProp->SetColor(col);
00300 m_PropertyList->InvokeEvent(itk::ModifiedEvent());
00301 m_PropertyList->Modified();
00302
00303 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00304 }
00305
00306 else if(mitk::BoolProperty* boolProp = dynamic_cast<mitk::BoolProperty*>(baseProp))
00307 {
00308 boolProp->SetValue(value.toInt() == Qt::Checked ? true : false);
00309 m_PropertyList->InvokeEvent(itk::ModifiedEvent());
00310 m_PropertyList->Modified();
00311
00312 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00313 }
00314
00315 else if (mitk::StringProperty* stringProp = dynamic_cast<mitk::StringProperty*>(baseProp))
00316 {
00317 stringProp->SetValue((value.value<QString>()).toStdString());
00318 m_PropertyList->InvokeEvent(itk::ModifiedEvent());
00319 m_PropertyList->Modified();
00320
00321 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00322 }
00323
00324 else if (mitk::IntProperty* intProp = dynamic_cast<mitk::IntProperty*>(baseProp))
00325 {
00326 int intValue = value.value<int>();
00327 if (intValue != intProp->GetValue())
00328 {
00329 intProp->SetValue(intValue);
00330 m_PropertyList->InvokeEvent(itk::ModifiedEvent());
00331 m_PropertyList->Modified();
00332
00333 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00334 }
00335 }
00336
00337 else if (mitk::FloatProperty* floatProp = dynamic_cast<mitk::FloatProperty*>(baseProp))
00338 {
00339 float floatValue = value.value<float>();
00340 if (floatValue != floatProp->GetValue())
00341 {
00342 floatProp->SetValue(floatValue);
00343 m_PropertyList->InvokeEvent(itk::ModifiedEvent());
00344 m_PropertyList->Modified();
00345
00346 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00347 }
00348 }
00349
00350 else if (mitk::EnumerationProperty* enumerationProp = dynamic_cast<mitk::EnumerationProperty*>(baseProp))
00351 {
00352 std::string activatedItem = value.value<QString>().toStdString();
00353 if ( activatedItem != enumerationProp->GetValueAsString() )
00354 {
00355 if ( enumerationProp->IsValidEnumerationValue( activatedItem ) )
00356 {
00357 enumerationProp->SetValue( activatedItem );
00358 m_PropertyList->InvokeEvent( itk::ModifiedEvent() );
00359 m_PropertyList->Modified();
00360
00361 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00362 }
00363 }
00364 }
00365 }
00366
00367
00368 else if(index.column() == PROPERTY_ACTIVE_COLUMN)
00369 {
00370 bool active = value.toInt() == Qt::Checked;
00371 std::string propertyName = m_SelectedProperties[index.row()].first;
00372
00373 m_PropertyList->SetEnabled(propertyName, active);
00374 m_SelectedProperties[index.row()].second.second = active;
00375
00376 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00377 }
00378
00379
00380 m_BlockEvents = false;
00381 emit dataChanged(index, index);
00382 return true;
00383 }
00384
00385 return false;
00386 }
00387
00388
00389 void QmitkPropertiesTableModel::sort( int column, Qt::SortOrder order )
00390 {
00391 bool sortDescending = (order == Qt::DescendingOrder) ? true: false;
00392
00393
00394 if(sortDescending != m_SortDescending)
00395 {
00396 m_SortDescending = sortDescending;
00397
00398 PropertyDataSetCompareFunction::CompareCriteria _CompareCriteria
00399 = PropertyDataSetCompareFunction::CompareByName;
00400
00401 PropertyDataSetCompareFunction::CompareOperator _CompareOperator
00402 = m_SortDescending ? PropertyDataSetCompareFunction::Greater: PropertyDataSetCompareFunction::Less;
00403
00404 if(column == PROPERTY_VALUE_COLUMN)
00405 _CompareCriteria = PropertyDataSetCompareFunction::CompareByValue;
00406
00407 else if(column == PROPERTY_ACTIVE_COLUMN)
00408 _CompareCriteria = PropertyDataSetCompareFunction::CompareByActivity;
00409
00410
00411 PropertyDataSetCompareFunction compareFunc(_CompareCriteria, _CompareOperator);
00412 std::sort(m_SelectedProperties.begin(), m_SelectedProperties.end(), compareFunc);
00413
00414 QAbstractTableModel::reset();
00415
00416 }
00417 }
00418
00419
00420 int QmitkPropertiesTableModel::FindProperty( const mitk::BaseProperty* _Property ) const
00421 {
00422 int row = -1;
00423
00424 if(_Property)
00425 {
00426
00427 std::vector<PropertyDataSet >::const_iterator propertyIterator;
00428
00429 for( propertyIterator=m_SelectedProperties.begin(); propertyIterator!=m_SelectedProperties.end()
00430 ; propertyIterator++)
00431 {
00432 if(propertyIterator->second.first == _Property)
00433 break;
00434 }
00435
00436 if(propertyIterator != m_SelectedProperties.end())
00437 row = std::distance(m_SelectedProperties.begin(), propertyIterator);
00438 }
00439
00440 return row;
00441 }
00442
00443
00444 void QmitkPropertiesTableModel::AddSelectedProperty( PropertyDataSet& _PropertyDataSet )
00445 {
00446
00447 itk::MemberCommand<QmitkPropertiesTableModel>::Pointer _PropertyDataSetModifiedCommand =
00448 itk::MemberCommand<QmitkPropertiesTableModel>::New();
00449 _PropertyDataSetModifiedCommand->SetCallbackFunction(this, &QmitkPropertiesTableModel::PropertyModified);
00450 m_PropertyModifiedObserverTags.push_back(_PropertyDataSet.second.first->AddObserver(itk::ModifiedEvent(), _PropertyDataSetModifiedCommand));
00451
00452
00453 itk::MemberCommand<QmitkPropertiesTableModel>::Pointer _PropertyDataSetDeleteCommand =
00454 itk::MemberCommand<QmitkPropertiesTableModel>::New();
00455 _PropertyDataSetDeleteCommand->SetCallbackFunction(this, &QmitkPropertiesTableModel::PropertyDelete);
00456 m_PropertyDeleteObserverTags.push_back(_PropertyDataSet.second.first->AddObserver(itk::DeleteEvent(), _PropertyDataSetDeleteCommand));
00457
00458
00459 m_SelectedProperties.push_back(_PropertyDataSet);
00460 }
00461
00462 void QmitkPropertiesTableModel::RemoveSelectedProperty( unsigned int _Index )
00463 {
00464 PropertyDataSet& _PropertyDataSet = m_SelectedProperties.at(_Index);
00465
00466
00467 _PropertyDataSet.second.first->RemoveObserver(m_PropertyModifiedObserverTags[_Index]);
00468 m_PropertyModifiedObserverTags.erase(m_PropertyModifiedObserverTags.begin()+_Index);
00469
00470 _PropertyDataSet.second.first->RemoveObserver(m_PropertyDeleteObserverTags[_Index]);
00471 m_PropertyDeleteObserverTags.erase(m_PropertyDeleteObserverTags.begin()+_Index);
00472
00473 m_SelectedProperties.erase(m_SelectedProperties.begin()+_Index);
00474 }
00475
00476 void QmitkPropertiesTableModel::Reset()
00477 {
00478
00479 while(!m_SelectedProperties.empty())
00480 {
00481 this->RemoveSelectedProperty(m_SelectedProperties.size()-1);
00482 }
00483
00484 std::vector<PropertyDataSet> allPredicates;
00485 if(m_PropertyList.IsNotNull())
00486 {
00487
00488 for(mitk::PropertyList::PropertyMap::const_iterator it=m_PropertyList->GetMap()->begin()
00489 ; it!=m_PropertyList->GetMap()->end()
00490 ; it++)
00491 {
00492 allPredicates.push_back(*it);
00493 }
00494 }
00495
00496 if(!m_FilterKeyWord.empty())
00497 {
00498 std::vector<PropertyDataSet> subSelection;
00499
00500 for(std::vector<PropertyDataSet>::iterator it=allPredicates.begin()
00501 ; it!=allPredicates.end()
00502 ; it++)
00503 {
00504
00505 if((*it).first.find(m_FilterKeyWord) != std::string::npos)
00506 subSelection.push_back((*it));
00507 }
00508 allPredicates.clear();
00509 allPredicates = subSelection;
00510 }
00511
00512 PropertyDataSet tmpPropertyDataSet;
00513
00514 for(std::vector<PropertyDataSet>::iterator it=allPredicates.begin()
00515 ; it!=allPredicates.end()
00516 ; it++)
00517 {
00518 tmpPropertyDataSet = *it;
00519 this->AddSelectedProperty(tmpPropertyDataSet);
00520 }
00521
00522
00523 this->sort(m_SortDescending);
00524
00525
00526 QAbstractTableModel::reset();
00527 }
00528
00529 void QmitkPropertiesTableModel::SetFilterPropertiesKeyWord( std::string _FilterKeyWord )
00530 {
00531 m_FilterKeyWord = _FilterKeyWord;
00532 this->Reset();
00533 }
00534
00535 QmitkPropertiesTableModel::PropertyDataSetCompareFunction::PropertyDataSetCompareFunction( CompareCriteria _CompareCriteria
00536 , CompareOperator _CompareOperator )
00537 : m_CompareCriteria(_CompareCriteria)
00538 , m_CompareOperator(_CompareOperator)
00539 {
00540 }
00541
00542 bool QmitkPropertiesTableModel::PropertyDataSetCompareFunction::operator()
00543 ( const PropertyDataSet& _Left
00544 , const PropertyDataSet& _Right ) const
00545 {
00546 switch(m_CompareCriteria)
00547 {
00548 case CompareByValue:
00549 if(m_CompareOperator == Less)
00550 return (_Left.second.first->GetValueAsString() < _Right.second.first->GetValueAsString());
00551 else
00552 return (_Left.second.first->GetValueAsString() > _Right.second.first->GetValueAsString());
00553 break;
00554
00555 case CompareByActivity:
00556 if(m_CompareOperator == Less)
00557 return (_Left.second.second < _Right.second.second);
00558 else
00559 return (_Left.second.second > _Right.second.second);
00560 break;
00561
00562
00563 default:
00564 if(m_CompareOperator == Less)
00565 return (_Left.first < _Right.first);
00566 else
00567 return (_Left.first > _Right.first);
00568 break;
00569 }
00570 }
00571 QmitkPropertiesTableModel::PropertyListElementFilterFunction::PropertyListElementFilterFunction( const std::string& _FilterKeyWord )
00572 : m_FilterKeyWord(_FilterKeyWord)
00573 {
00574
00575 }
00576
00577 bool QmitkPropertiesTableModel::PropertyListElementFilterFunction::operator()( const PropertyDataSet& _Elem ) const
00578 {
00579 if(m_FilterKeyWord.empty())
00580 return true;
00581 return (_Elem.first.find(m_FilterKeyWord) == 0);
00582 }