Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
berryAbstractHandlerWithState.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include "berryState.h"
16 
17 namespace berry {
18 
19 void AbstractHandlerWithState::AddState(const QString& stateId, const SmartPointer<State>& state)
20 {
21  if (state.IsNull())
22  {
23  throw ctkInvalidArgumentException("Cannot add a null state");
24  }
25 
26  states.insert(stateId, state);
27  state->AddListener(this);
28  HandleStateChange(state, Object::Pointer(nullptr));
29 }
30 
32 {
33  QHash<QString,State::Pointer>::const_iterator i = states.find(stateId);
34  if (i != states.end()) return *i;
35  return State::Pointer(nullptr);
36 }
37 
39 {
40  return states.keys();
41 }
42 
43 void AbstractHandlerWithState::RemoveState(const QString& stateId)
44 {
45  if (stateId.isNull())
46  {
47  throw ctkInvalidArgumentException("Cannot remove a null state");
48  }
49 
50  QHash<QString,State::Pointer>::iterator i = states.find(stateId);
51  if (i != states.end())
52  {
53  (*i)->RemoveListener(this);
54  states.erase(i);
55  }
56 }
57 
58 }
Implements transparent reference counting.
virtual void HandleStateChange(const SmartPointer< State > &state, const SmartPointer< Object > &oldValue)=0
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:82
void RemoveState(const QString &stateId) override
QList< QString > GetStateIds() const override
SmartPointer< State > GetState(const QString &stateId) const override