Public Member Functions | Protected Member Functions | Protected Attributes

QmitkDataStorageListModel Class Reference

#include <QmitkDataStorageListModel.h>

Collaboration diagram for QmitkDataStorageListModel:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 QmitkDataStorageListModel (mitk::DataStorage::Pointer dataStorage=0, mitk::NodePredicateBase *pred=0, QObject *parent=0)
 ~QmitkDataStorageListModel ()
void SetDataStorage (mitk::DataStorage::Pointer dataStorage)
mitk::DataStorage::Pointer GetDataStorage () const
void SetPredicate (mitk::NodePredicateBase *pred)
mitk::NodePredicateBaseGetPredicate () const
std::vector< mitk::DataNode * > GetDataNodes () const
mitk::DataNode::Pointer getNode (const QModelIndex &index) const
Qt::ItemFlags flags (const QModelIndex &index) const
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
int rowCount (const QModelIndex &parent=QModelIndex()) const
virtual void NodeAdded (const mitk::DataNode *node)
virtual void NodeRemoved (const mitk::DataNode *node)
virtual void OnModified (const itk::Object *caller, const itk::EventObject &event)
 Called when a itk::Object that is hold as a member variable was modified in order to react to it.
virtual void OnDelete (const itk::Object *caller, const itk::EventObject &event)
 Called when a itk::Object that is hold as a member variable is about to be deleted in order to react to it.

Protected Member Functions

void reset ()
 Resets the whole model. Get all nodes matching the predicate from the data storage.

Protected Attributes

mitk::NodePredicateBase::Pointer m_NodePredicate
mitk::DataStoragem_DataStorage
unsigned long m_DataStorageDeleteObserverTag
 Holds the tag of the datastorage-delete observer.
std::vector< mitk::DataNode * > m_DataNodes
std::vector< unsigned long > m_DataNodesModifiedObserversTags
 Holds the tags of the node-modified observers.
bool m_BlockEvents

Detailed Description

Definition at line 35 of file QmitkDataStorageListModel.h.


Constructor & Destructor Documentation

QmitkDataStorageListModel::QmitkDataStorageListModel ( mitk::DataStorage::Pointer  dataStorage = 0,
mitk::NodePredicateBase pred = 0,
QObject *  parent = 0 
)

The NodePredicate is owned by the model

Definition at line 28 of file QmitkDataStorageListModel.cpp.

References SetDataStorage(), and SetPredicate().

 : QAbstractListModel(parent), m_NodePredicate(0), m_DataStorage(0), m_BlockEvents(false)
{  
  this->SetPredicate(pred);
  this->SetDataStorage(dataStorage);
}
QmitkDataStorageListModel::~QmitkDataStorageListModel (  )

Definition at line 36 of file QmitkDataStorageListModel.cpp.

References m_NodePredicate, and SetDataStorage().

{
  // set data storage to 0 so that event listener get removed
  this->SetDataStorage(0);

  if (m_NodePredicate) delete m_NodePredicate;
}

Member Function Documentation

QVariant QmitkDataStorageListModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const

Definition at line 93 of file QmitkDataStorageListModel.cpp.

References mitk::DataNode::GetName(), and m_DataNodes.

{
  if(index.isValid())
  {
    switch ( role )
    {
      case Qt::DisplayRole:
      {
        const mitk::DataNode* node = m_DataNodes.at(index.row());
        std::string name = node->GetName();
        return QVariant(QString::fromStdString(name));
      }
      break;

    }
  } // index.isValid()

  return QVariant();
}
Qt::ItemFlags QmitkDataStorageListModel::flags ( const QModelIndex &  index ) const

Definition at line 88 of file QmitkDataStorageListModel.cpp.

{
  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
std::vector< mitk::DataNode * > QmitkDataStorageListModel::GetDataNodes (  ) const

Definition at line 123 of file QmitkDataStorageListModel.cpp.

References m_DataNodes.

{
  return m_DataNodes;
}
mitk::DataStorage::Pointer QmitkDataStorageListModel::GetDataStorage (  ) const

Definition at line 128 of file QmitkDataStorageListModel.cpp.

References m_DataStorage.

{
  return m_DataStorage;
}
mitk::DataNode::Pointer QmitkDataStorageListModel::getNode ( const QModelIndex &  index ) const

Definition at line 264 of file QmitkDataStorageListModel.cpp.

References m_DataNodes.

{
  mitk::DataNode::Pointer node;

  if(index.isValid())
  {
    node = m_DataNodes.at(index.row());
  }

  return node;
}
mitk::NodePredicateBase * QmitkDataStorageListModel::GetPredicate (  ) const

Definition at line 141 of file QmitkDataStorageListModel.cpp.

References m_NodePredicate.

{
  return m_NodePredicate;
}
QVariant QmitkDataStorageListModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const

Definition at line 113 of file QmitkDataStorageListModel.cpp.

{
  return QVariant("Nodes");
}
void QmitkDataStorageListModel::NodeAdded ( const mitk::DataNode node ) [virtual]

Called when a DataStorage Add Event was thrown. May be reimplemented by deriving classes.

Definition at line 186 of file QmitkDataStorageListModel.cpp.

References m_BlockEvents, m_DataNodes, and m_NodePredicate.

Referenced by SetDataStorage().

{
  // garantuee no recursions when a new node event is thrown
  if(!m_BlockEvents)
  {
    m_BlockEvents = true;

    // check if node should be added to the model
    bool addNode = true;
    if(m_NodePredicate && !m_NodePredicate->CheckNode(node))
      addNode = false;

    if(addNode)
    {
      beginInsertRows(QModelIndex(), m_DataNodes.size(), m_DataNodes.size());
      //reset();
      m_DataNodes.push_back(const_cast<mitk::DataNode*>(node));
      endInsertRows();
    }

    m_BlockEvents = false;
  }
}
void QmitkDataStorageListModel::NodeRemoved ( const mitk::DataNode node ) [virtual]

Called when a DataStorage Remove Event was thrown. May be reimplemented by deriving classes.

Definition at line 210 of file QmitkDataStorageListModel.cpp.

References m_BlockEvents, and m_DataNodes.

Referenced by SetDataStorage().

{
  // garantuee no recursions when a new node event is thrown
  if(!m_BlockEvents)
  {
    m_BlockEvents = true;

    int row = -1;
    //bool removeNode = false;    
    // check if node is contained in current list, if yes: reset model
    for (std::vector<mitk::DataNode*>::const_iterator nodeIt = m_DataNodes.begin()
      ; nodeIt != m_DataNodes.end(); nodeIt++)  // for each node
    {
      row++;
      if( (*nodeIt) == node )
      {
        // node found, remove it
        beginRemoveRows(QModelIndex(), row, row);
        m_DataNodes.erase(std::find(m_DataNodes.begin(), m_DataNodes.end(), (*nodeIt)));
        endRemoveRows();
        break;
      }
    }

    m_BlockEvents = false;
  }
}
void QmitkDataStorageListModel::OnDelete ( const itk::Object *  caller,
const itk::EventObject &  event 
) [virtual]

Called when a itk::Object that is hold as a member variable is about to be deleted in order to react to it.

Definition at line 252 of file QmitkDataStorageListModel.cpp.

References m_BlockEvents, and SetDataStorage().

Referenced by SetDataStorage().

{
  if(m_BlockEvents) return;

  const mitk::DataStorage* dataStorage = dynamic_cast<const mitk::DataStorage*>(caller);
  if(dataStorage)
  {
    // set datastorage to 0 -> empty model
    this->SetDataStorage(0);
  }
}
void QmitkDataStorageListModel::OnModified ( const itk::Object *  caller,
const itk::EventObject &  event 
) [virtual]

Called when a itk::Object that is hold as a member variable was modified in order to react to it.

Definition at line 238 of file QmitkDataStorageListModel.cpp.

References QuadProgPP::distance(), m_BlockEvents, and m_DataNodes.

Referenced by reset().

{
  if(m_BlockEvents) return;

  const mitk::DataNode* modifiedNode = dynamic_cast<const mitk::DataNode*>(caller);
  if(modifiedNode)
  {
    int row = std::distance(std::find(m_DataNodes.begin(), m_DataNodes.end(), modifiedNode), m_DataNodes.end());
    QModelIndex indexOfChangedProperty = index(row, 1);

    emit dataChanged(indexOfChangedProperty, indexOfChangedProperty);
  }
}
void QmitkDataStorageListModel::reset (  ) [protected]

Resets the whole model. Get all nodes matching the predicate from the data storage.

Definition at line 146 of file QmitkDataStorageListModel.cpp.

References mitk::DataStorage::GetAll(), mitk::DataStorage::GetSubset(), m_DataNodes, m_DataNodesModifiedObserversTags, m_DataStorage, m_NodePredicate, and OnModified().

Referenced by SetDataStorage(), and SetPredicate().

{
  if(m_DataStorage != 0)
  {    
    mitk::DataStorage::SetOfObjects::ConstPointer setOfObjects;

    if (m_NodePredicate)
      setOfObjects = m_DataStorage->GetSubset(m_NodePredicate);
    else
      setOfObjects = m_DataStorage->GetAll();   

    // remove all observes
    unsigned int i = 0;
    for(std::vector<mitk::DataNode*>::iterator it=m_DataNodes.begin()
      ; it!=m_DataNodes.end()
      ; ++it, ++i)
    {
      (*it)->RemoveObserver(m_DataNodesModifiedObserversTags[i]);     
    }

    // clear vector with nodes
    m_DataNodesModifiedObserversTags.clear();
    m_DataNodes.clear();
    itk::MemberCommand<QmitkDataStorageListModel>::Pointer modifiedCommand;
    // copy all selected nodes the vector
    for (mitk::DataStorage::SetOfObjects::ConstIterator nodeIt = setOfObjects->Begin()
      ; nodeIt != setOfObjects->End(); ++nodeIt, ++i)  // for each node
    {
      // add modified observer
      modifiedCommand = itk::MemberCommand<QmitkDataStorageListModel>::New();
      modifiedCommand->SetCallbackFunction(this, &QmitkDataStorageListModel::OnModified);
      m_DataNodesModifiedObserversTags.push_back( m_DataStorage->AddObserver(itk::ModifiedEvent(), modifiedCommand) );
      m_DataNodes.push_back( nodeIt.Value().GetPointer());

    } // for

  } // m_DataStorage != 0

} // reset()
int QmitkDataStorageListModel::rowCount ( const QModelIndex &  parent = QModelIndex() ) const

Definition at line 118 of file QmitkDataStorageListModel.cpp.

References m_DataNodes.

{
  return m_DataNodes.size();
}
void QmitkDataStorageListModel::SetDataStorage ( mitk::DataStorage::Pointer  dataStorage )

Definition at line 44 of file QmitkDataStorageListModel.cpp.

References mitk::DataStorage::AddNodeEvent, m_DataStorage, m_DataStorageDeleteObserverTag, NodeAdded(), NodeRemoved(), OnDelete(), mitk::DataStorage::RemoveNodeEvent, and reset().

Referenced by OnDelete(), QmitkDataStorageListModel(), and ~QmitkDataStorageListModel().

{
  if( m_DataStorage != dataStorage)
  {
    // remove old listeners
    if(m_DataStorage != 0)
    {
      this->m_DataStorage->AddNodeEvent.RemoveListener( mitk::MessageDelegate1<QmitkDataStorageListModel
        , const mitk::DataNode*>( this, &QmitkDataStorageListModel::NodeAdded ) );

      this->m_DataStorage->RemoveNodeEvent.RemoveListener( mitk::MessageDelegate1<QmitkDataStorageListModel
        , const mitk::DataNode*>( this, &QmitkDataStorageListModel::NodeRemoved ) );

      // remove delete observer
      m_DataStorage->RemoveObserver(m_DataStorageDeleteObserverTag);
      // this is good coding style ! reset variables whenever they are not used anymore.
      m_DataStorageDeleteObserverTag = 0;
    }

    m_DataStorage = dataStorage;

    // remove event listeners
    if(m_DataStorage != 0)
    {
      // subscribe for node added/removed events
      this->m_DataStorage->AddNodeEvent.AddListener( mitk::MessageDelegate1<QmitkDataStorageListModel
        , const mitk::DataNode*>( this, &QmitkDataStorageListModel::NodeAdded ) );

      this->m_DataStorage->RemoveNodeEvent.AddListener( mitk::MessageDelegate1<QmitkDataStorageListModel
        , const mitk::DataNode*>( this, &QmitkDataStorageListModel::NodeRemoved ) );

      // add itk delete listener on datastorage
      itk::MemberCommand<QmitkDataStorageListModel>::Pointer deleteCommand =
        itk::MemberCommand<QmitkDataStorageListModel>::New();
      deleteCommand->SetCallbackFunction(this, &QmitkDataStorageListModel::OnDelete);
      // add observer
      m_DataStorageDeleteObserverTag = m_DataStorage->AddObserver(itk::DeleteEvent(), deleteCommand);
    }

    // reset model
    reset();
  }
}
void QmitkDataStorageListModel::SetPredicate ( mitk::NodePredicateBase pred )

Definition at line 134 of file QmitkDataStorageListModel.cpp.

References m_NodePredicate, and reset().

Referenced by QmitkDataStorageListModel().


Member Data Documentation

Saves if this model is currently working on events to prevent endless event loops.

Definition at line 122 of file QmitkDataStorageListModel.h.

Referenced by NodeAdded(), NodeRemoved(), OnDelete(), and OnModified().

Holds all selected Nodes. Dont hold smart pointer as we are in a GUI class.

Definition at line 112 of file QmitkDataStorageListModel.h.

Referenced by data(), GetDataNodes(), getNode(), NodeAdded(), NodeRemoved(), OnModified(), reset(), and rowCount().

std::vector<unsigned long> QmitkDataStorageListModel::m_DataNodesModifiedObserversTags [protected]

Holds the tags of the node-modified observers.

Definition at line 117 of file QmitkDataStorageListModel.h.

Referenced by reset().

Pointer to the DataStorage from which the nodes are selected (remember: in BlueBerry there might be more than one DataStorage).

Definition at line 102 of file QmitkDataStorageListModel.h.

Referenced by GetDataStorage(), reset(), and SetDataStorage().

Holds the tag of the datastorage-delete observer.

Definition at line 107 of file QmitkDataStorageListModel.h.

Referenced by SetDataStorage().

Holds the predicate that defines this SubSet of Nodes. If m_Predicate is NULL all Nodes will be selected. *Attention: this class owns the predicate and deletes it*

Definition at line 96 of file QmitkDataStorageListModel.h.

Referenced by GetPredicate(), NodeAdded(), reset(), SetPredicate(), and ~QmitkDataStorageListModel().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines