diff --git a/Core/Code/Controllers/mitkFocusManager.cpp b/Core/Code/Controllers/mitkFocusManager.cpp index a28510a022..decc262252 100755 --- a/Core/Code/Controllers/mitkFocusManager.cpp +++ b/Core/Code/Controllers/mitkFocusManager.cpp @@ -1,143 +1,160 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkFocusManager.h" mitk::FocusManager::FocusManager() { m_Loop = true;//default m_FocusList.clear(); m_FocElement = NULL; } mitk::FocusManager::~FocusManager() { } bool mitk::FocusManager::AddElement(FocusElement* element) { // Try find mitk::FocusManager::FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),element); if (position != m_FocusList.end()) return false; m_FocusList.push_back(element); if (m_FocElement.GetPointer() == NULL) m_FocElement = element; return true; } bool mitk::FocusManager::RemoveElement(FocusElement* element) { if (element == m_FocElement) { this->GoToNext(); } // Try to find mitk::FocusManager::FocusListIterator position = std::find(m_FocusList.begin(), m_FocusList.end(), element); if (position == m_FocusList.end()) { return false; } m_FocusList.erase(position); + if (m_FocusList.empty()) + { + m_FocElement = NULL; + } return true; } mitk::FocusManager::FocusElement* mitk::FocusManager::GetFocused() const { return m_FocElement.GetPointer(); } bool mitk::FocusManager::SetFocused(FocusElement* element) { if (m_FocElement == element) return true; FocusListIterator position = std::find(m_FocusList.begin(),m_FocusList.end(),element); if (position == m_FocusList.end())//not found return false; m_FocElement = *position; ((const itk::Object*)this)->InvokeEvent(FocusEvent()); return true; } bool mitk::FocusManager::IsLast() { return (m_FocElement == m_FocusList.back()); } bool mitk::FocusManager::IsFirst() { return (m_FocElement == m_FocusList.front()); } const mitk::FocusManager::FocusElement* mitk::FocusManager::GetFirst() const { return (m_FocusList.front()).GetPointer(); } const mitk::FocusManager::FocusElement* mitk::FocusManager::GetLast() const { return (m_FocusList.back()).GetPointer(); } bool mitk::FocusManager::GoToNext() { + if (m_FocusList.empty()) + { + return false; + } + //find the m_FocElement FocusListIterator position = std::find(m_FocusList.begin(), m_FocusList.end(), m_FocElement); if (position == m_FocusList.end()) { return false; } - for (FocusListIterator nextPosition = position + 1; nextPosition != position; ++nextPosition) + if (m_FocusList.size() == 1) + { + return true; + } + + FocusListIterator nextPosition = position + 1; + while(nextPosition != position) { if (nextPosition == m_FocusList.end()) { if (!m_Loop) { return false; } nextPosition = m_FocusList.begin(); } FocusElement* focusElement = *nextPosition; if (focusElement->GetSizeX() > 0 && focusElement->GetSizeY() > 0) { m_FocElement = focusElement; return true; } + ++nextPosition; } + m_FocElement = NULL; return false; } //##Documentation //## returns an iterator, that points to the //## beginning of the list mitk::FocusManager::FocusListIterator mitk::FocusManager::GetIter() { return m_FocusList.begin(); } void mitk::FocusManager::SetLoop(bool loop) { m_Loop = loop; } diff --git a/Core/Code/Testing/mitkFocusManagerTest.cpp b/Core/Code/Testing/mitkFocusManagerTest.cpp index 1900f5446f..a2fe828de7 100644 --- a/Core/Code/Testing/mitkFocusManagerTest.cpp +++ b/Core/Code/Testing/mitkFocusManagerTest.cpp @@ -1,91 +1,94 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkFocusManager.h" #include "mitkTestingMacros.h" #include "mitkVtkPropRenderer.h" #include "mitkGlobalInteraction.h" int mitkFocusManagerTest(int /*argc*/, char* /*argv*/[]) { MITK_TEST_BEGIN("FocusManager"); // Global interaction must(!) be initialized if used mitk::GlobalInteraction::GetInstance()->Initialize("global"); mitk::RenderingManager::Pointer rm = mitk::RenderingManager::GetInstance(); //building up necessary objects vtkRenderWindow* renderWindow = vtkRenderWindow::New(); mitk::VtkPropRenderer::Pointer element1 = mitk::VtkPropRenderer::New( "renderer1", renderWindow, rm ); mitk::VtkPropRenderer::Pointer element2 = mitk::VtkPropRenderer::New( "renderer2", renderWindow, rm ); mitk::VtkPropRenderer::Pointer element3 = mitk::VtkPropRenderer::New( "renderer3", renderWindow, rm ); + element1->InitSize(100, 100); + element2->InitSize(100, 100); + element3->InitSize(100, 100); //the FocusManager itself mitk::FocusManager::Pointer focusManager = mitk::FocusManager::New(); //testing MITK_TEST_CONDITION_REQUIRED(focusManager.IsNotNull(), "Testing Instatiation"); MITK_TEST_CONDITION_REQUIRED(element1.IsNotNull(), "Testing Instatiation of an element"); MITK_TEST_CONDITION_REQUIRED(focusManager->AddElement(element1), "Testing addition of an element"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element1, "Testing if the added element is focused on"); MITK_TEST_CONDITION_REQUIRED(focusManager->RemoveElement(element1), "Testing removing of an element"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == NULL, "Testing focused on an empty list"); MITK_TEST_CONDITION_REQUIRED(focusManager->AddElement(element1), "Testing addition of an element; Elements in list: 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->AddElement(element2), "Testing addition of a second element; Elements in list: 1 2"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element1, "Testing if the added element still is element 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->AddElement(element3), "Testing addition of a third element; Elements in list: 1 2 3"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element1, "Testing if the added element still is element 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->SetFocused(element1), "Testing setting focused to element 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element1, "focus on element 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->SetFocused(element2), "Testing setting focused to element 2"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element2, "focus on element 2"); MITK_TEST_CONDITION_REQUIRED(focusManager->SetFocused(element3), "Testing setting focused to element 3"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element3, "focus on element 3"); MITK_TEST_CONDITION_REQUIRED(focusManager->RemoveElement(element1), "Testing removing first element; Elements in list: 2 3"); - MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element2, "Testing if focused element is the one behind the deleted one: 2"); + MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element3, "Testing if focused element is still element 3"); MITK_TEST_CONDITION_REQUIRED(focusManager->AddElement(element1), "Testing addition of an element again; Elements in list: 2 3 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->RemoveElement(element3), "Testing removing element 3; Elements in list: 2 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element1, "Testing if focused element is the one behind the deleted one: 1"); MITK_TEST_CONDITION_REQUIRED(focusManager->RemoveElement(element1), "Testing removing last element 1; Elements in list: 2 "); MITK_TEST_CONDITION_REQUIRED(focusManager->GetFocused() == element2, "Testing if focused element is 2"); MITK_TEST_CONDITION_REQUIRED(!focusManager->RemoveElement(element1), "Testing removing same element"); MITK_TEST_CONDITION_REQUIRED(focusManager->RemoveElement(element2), "Testing removing last element in list (2)"); MITK_TEST_CONDITION_REQUIRED(!focusManager->RemoveElement(element3), "Testing removing from empty list "); MITK_TEST_CONDITION_REQUIRED(!focusManager->RemoveElement(element2), "Testing removing from empty list with different object"); MITK_TEST_CONDITION_REQUIRED(!focusManager->RemoveElement(element1), "Testing removing from empty list with different object again"); focusManager = NULL; //TODO: test IsLast() IsFirst() GetFirst() GetLast() GoToNext() GetIter() SetLoop(bool loop) //Delete renderWindo correctly renderWindow->Delete(); MITK_TEST_END(); }