1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkObserverMediator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkObserverMediator.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkObjectFactory.h"
#include "vtkPriorityQueue.h"
#include "vtkInteractorObserver.h"
#include <map>
vtkStandardNewMacro(vtkObserverMediator);
// PIMPL the map representing the observer (key) to cursor request
// (data). Note that only observers who are requesting non-default cursors
// are placed into the map.
// Comparison functor based on observer priorities (for the purpose of sorting
// the cursor request in the map).
struct vtkObserverCompare
{
bool operator()(vtkInteractorObserver* w1, vtkInteractorObserver* w2) const
{
float p1 = w1->GetPriority();
float p2 = w2->GetPriority();
if ( p1 < p2 )
{
return true;
}
else if ( p1 == p2 )
{
if ( w1 < w2 )
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
};
// The important feature of the map is that it sorts data (based on the functor above).
class vtkObserverMap : public std::map<vtkInteractorObserver*,int,vtkObserverCompare>
{
public:
vtkObserverMap() : std::map<vtkInteractorObserver*,int,vtkObserverCompare>() {}
};
typedef vtkObserverMap::iterator ObserverMapIterator;
//----------------------------------------------------------------------------
vtkObserverMediator::vtkObserverMediator()
{
this->Interactor = NULL;
this->ObserverMap = new vtkObserverMap;
this->CurrentObserver = NULL;
this->CurrentCursorShape = VTK_CURSOR_DEFAULT;
}
//----------------------------------------------------------------------------
vtkObserverMediator::~vtkObserverMediator()
{
delete this->ObserverMap;
}
//----------------------------------------------------------------------------
void vtkObserverMediator::SetInteractor(vtkRenderWindowInteractor* i)
{
this->Interactor = i;
}
//----------------------------------------------------------------------------
// This mediation process works by keeping track of non-default cursor
// requests.
// Ties are broken based on widget priority (hence the priority queue).
int vtkObserverMediator::RequestCursorShape(vtkInteractorObserver *w, int requestedShape)
{
if ( !this->Interactor || !w )
{
return 0;
}
// First remove previous requests from the map. Note we have to use our own
// special version of find() because the sorting of the map using the function
// vtkObserverCompare() screws up the usual find().
ObserverMapIterator iter = this->ObserverMap->begin();
for ( ; iter != this->ObserverMap->end(); ++iter )
{
if ( (*iter).first == w )
{
this->ObserverMap->erase(iter);
break;
}
}
// Now see whether we have to set to the default cursor, or add the latest request.
if ( this->ObserverMap->empty() && requestedShape == VTK_CURSOR_DEFAULT &&
this->CurrentCursorShape != VTK_CURSOR_DEFAULT )
{
this->Interactor->GetRenderWindow()->SetCurrentCursor(VTK_CURSOR_DEFAULT);
this->CurrentCursorShape = VTK_CURSOR_DEFAULT;
return 1;
}
else if ( requestedShape != VTK_CURSOR_DEFAULT )
{
(*this->ObserverMap)[w] = requestedShape;
// Find the highest priority and set that
if ( ! this->ObserverMap->empty() )
{
iter = this->ObserverMap->end();
--iter; //this is the observer with the highest priority
// Have to set the current cursor repeatedly or it reverts back to default
// (at least on windows it does).
this->Interactor->GetRenderWindow()->SetCurrentCursor((*iter).second);
if ( this->CurrentCursorShape != (*iter).second )
{
this->CurrentCursorShape = (*iter).second;
return 1;
}
}
}
return 0;
}
//----------------------------------------------------------------------------
void vtkObserverMediator::RemoveAllCursorShapeRequests(vtkInteractorObserver *w)
{
if (w)
{
ObserverMapIterator iter = this->ObserverMap->begin();
for ( ; iter != this->ObserverMap->end(); ++iter )
{
if ( (*iter).first == w )
{
this->ObserverMap->erase(iter);
break;
}
}
}
}
//----------------------------------------------------------------------------
void vtkObserverMediator::PrintSelf(ostream& os, vtkIndent indent)
{
//Superclass typedef defined in vtkTypeMacro() found in vtkSetGet.h
this->Superclass::PrintSelf(os,indent);
os << indent << "Render Window Interactor: ";
if ( this->Interactor )
{
os << this->Interactor << "\n";
}
else
{
os << "(None)\n";
}
}
|