A security patch will be applied between 1pm - 1:30pm, (UTC-0400), today- Aug 5th. No downtime is expected, but the site could be slow during that time.

TestControlPointsItemEvents.cxx 6.66 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestVector.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.

=========================================================================*/

// Charts includes
#include "vtkContextInteractorStyle.h"
#include "vtkContextScene.h"
#include "vtkControlPointsItem.h"
#include "vtkColorTransferControlPointsItem.h"
#include "vtkColorTransferFunction.h"

// Common includes"
#include "vtkIdTypeArray.h"
#include "vtkInteractorEventRecorder.h"
#include "vtkNew.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"

// STD includes
#include <iostream>
#include <map>

//----------------------------------------------------------------------------
class vtkTFCallback : public vtkCommand
{
public:
  static vtkTFCallback *New()
  {
  return new vtkTFCallback;
  }

  vtkTFCallback()
  {
  }

  void Execute( vtkObject *caller, unsigned long event,
                void *vtkNotUsed(callData) )
  {
  vtkColorTransferFunction* self =
    reinterpret_cast< vtkColorTransferFunction* >( caller );
  if (!self)
    {
    return;
    }
  if (this->EventSpy.count(event) == 0)
    {
    this->EventSpy[event] = 0;
    }
  ++this->EventSpy[event];
  std::cout << "InvokedEvent: " << event << this->EventSpy[event] << std::endl;
  }
  std::map<unsigned long, int> EventSpy;
};

//----------------------------------------------------------------------------
int TestControlPointsItemEvents(int, char*[])
{
  vtkNew<vtkColorTransferFunction> transferFunction;
  transferFunction->AddHSVSegment(50.,0.,1.,1.,85.,0.3333,1.,1.);
  transferFunction->AddHSVSegment(85.,0.3333,1.,1.,170.,0.6666,1.,1.);
  transferFunction->AddHSVSegment(170.,0.6666,1.,1.,200.,0.,1.,1.);

  vtkNew<vtkTFCallback> cbk;
  transferFunction->AddObserver( vtkCommand::StartEvent, cbk.GetPointer() );
  transferFunction->AddObserver( vtkCommand::ModifiedEvent, cbk.GetPointer() );
  transferFunction->AddObserver( vtkCommand::EndEvent, cbk.GetPointer() );
  transferFunction->AddObserver( vtkCommand::StartInteractionEvent, cbk.GetPointer() );
  transferFunction->AddObserver( vtkCommand::InteractionEvent, cbk.GetPointer() );
  transferFunction->AddObserver( vtkCommand::EndInteractionEvent, cbk.GetPointer() );

  vtkNew<vtkColorTransferControlPointsItem> controlPoints;
  controlPoints->SetColorTransferFunction(transferFunction.GetPointer());

//  vtkNew<vtkChartXY> chart;
//  chart->AddPlot(controlPoints.GetPointer());

  vtkNew<vtkContextScene> scene;
  scene->AddItem(controlPoints.GetPointer());

  vtkNew<vtkContextInteractorStyle> interactorStyle;
  interactorStyle->SetScene(scene.GetPointer());

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetInteractorStyle(interactorStyle.GetPointer());

  vtkNew<vtkInteractorEventRecorder> recorder;
  recorder->SetInteractor(iren.GetPointer());
  recorder->ReadFromInputStringOn();

  // Add a point at (60, 0.5) and move it to (62, 0.5)
  const char addAndDragEvents[] =
  "# StreamVersion 1\n"
  "LeftButtonPressEvent 60 1 0 0 0 0 0\n"
  "MouseMoveEvent 62 1 0 0 0 0 0\n"
  "LeftButtonReleaseEvent 62 1 0 0 0 0 0\n"
  ;
  recorder->SetInputString(addAndDragEvents);
  recorder->Play();

  // 1 ModifiedEvent for adding a point
  // 1 ModifiedEvent for moving the point
  if (cbk->EventSpy[vtkCommand::ModifiedEvent] != 2 ||
      cbk->EventSpy[vtkCommand::StartInteractionEvent] != 1 ||
      cbk->EventSpy[vtkCommand::InteractionEvent] != 1 ||
      cbk->EventSpy[vtkCommand::EndInteractionEvent] != 1 ||
      cbk->EventSpy[vtkCommand::StartEvent] != 0 ||
      cbk->EventSpy[vtkCommand::EndEvent] != 0)
    {
    std::cerr << "Wrong number of fired events : "
              << cbk->EventSpy[vtkCommand::ModifiedEvent] << " "
              << cbk->EventSpy[vtkCommand::StartInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::InteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::EndInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::StartEvent] << " "
              << cbk->EventSpy[vtkCommand::EndEvent] << std::endl;
    return EXIT_FAILURE;
    }
  cbk->EventSpy.clear();
  // Move all the points to the right.
  controlPoints->MovePoints(vtkVector2f(5, 0.));
  // One ModifiedEvent for each moved point

  if (cbk->EventSpy[vtkCommand::ModifiedEvent] > controlPoints->GetNumberOfPoints() ||
      cbk->EventSpy[vtkCommand::StartInteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::InteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::EndInteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::StartEvent] != 1 ||
      cbk->EventSpy[vtkCommand::EndEvent] != 1)
    {
    std::cerr << "Wrong number of fired events : "
              << cbk->EventSpy[vtkCommand::ModifiedEvent] << " "
              << cbk->EventSpy[vtkCommand::StartInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::InteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::EndInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::StartEvent] << " "
              << cbk->EventSpy[vtkCommand::EndEvent] << std::endl;
    return EXIT_FAILURE;
    }

  cbk->EventSpy.clear();

  const char dblClickEvents[] =
    "# StreamVersion 1\n"
    "MouseMoveEvent 56 1 0 0 0 0 0\n" // shouldn't move the point
    "LeftButtonPressEvent 55 1 0 0 0 0 0\n" // select the first point
    "LeftButtonReleaseEvent 55 1 0 0 0 0 0\n"
    "LeftButtonPressEvent 55 1 0 0 0 1 0\n" // dbl click
Julien Finet's avatar
Julien Finet committed
159
    "LeftButtonReleaseEvent 55 1 0 0 0 0 0\n" // must be followed by release
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    ;

  recorder->SetInputString(dblClickEvents);
  recorder->Play();

  if (cbk->EventSpy[vtkCommand::ModifiedEvent] != 0 ||
      cbk->EventSpy[vtkCommand::StartInteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::InteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::EndInteractionEvent] != 0 ||
      cbk->EventSpy[vtkCommand::StartEvent] != 0 ||
      cbk->EventSpy[vtkCommand::EndEvent] != 0)
    {
    std::cerr << "Wrong number of fired events : "
              << cbk->EventSpy[vtkCommand::ModifiedEvent] << " "
              << cbk->EventSpy[vtkCommand::StartInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::InteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::EndInteractionEvent] << " "
              << cbk->EventSpy[vtkCommand::StartEvent] << " "
              << cbk->EventSpy[vtkCommand::EndEvent] << std::endl;
    return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}