TestControlPointsItemEvents.cxx 6.5 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
/*=========================================================================

  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,
48
                void *vtkNotUsed(callData) ) override
49
50
51
52
  {
  vtkColorTransferFunction* self =
    reinterpret_cast< vtkColorTransferFunction* >( caller );
  if (!self)
53
  {
54
    return;
55
  }
56
  if (this->EventSpy.count(event) == 0)
57
  {
58
    this->EventSpy[event] = 0;
59
  }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  ++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;
75
76
77
78
79
80
  transferFunction->AddObserver( vtkCommand::StartEvent, cbk );
  transferFunction->AddObserver( vtkCommand::ModifiedEvent, cbk );
  transferFunction->AddObserver( vtkCommand::EndEvent, cbk );
  transferFunction->AddObserver( vtkCommand::StartInteractionEvent, cbk );
  transferFunction->AddObserver( vtkCommand::InteractionEvent, cbk );
  transferFunction->AddObserver( vtkCommand::EndInteractionEvent, cbk );
81
82

  vtkNew<vtkColorTransferControlPointsItem> controlPoints;
83
  controlPoints->SetColorTransferFunction(transferFunction);
84
85

//  vtkNew<vtkChartXY> chart;
86
//  chart->AddPlot(controlPoints);
87
88

  vtkNew<vtkContextScene> scene;
89
  scene->AddItem(controlPoints);
90
91

  vtkNew<vtkContextInteractorStyle> interactorStyle;
92
  interactorStyle->SetScene(scene);
93
94

  vtkNew<vtkRenderWindowInteractor> iren;
95
  iren->SetInteractorStyle(interactorStyle);
96
97

  vtkNew<vtkInteractorEventRecorder> recorder;
98
  recorder->SetInteractor(iren);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  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 ||
117
118
      cbk->EventSpy[vtkCommand::StartEvent] != 2 ||
      cbk->EventSpy[vtkCommand::EndEvent] != 2)
119
  {
120
121
122
123
124
125
126
127
    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;
128
  }
129
130
131
132
133
134
135
136
137
138
139
  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)
140
  {
141
142
143
144
145
146
147
148
    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;
149
  }
150
151
152
153
154
155
156
157
158

  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
    ;

  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)
171
  {
172
173
174
175
176
177
178
179
    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;
180
  }
181
182
183

  return EXIT_SUCCESS;
}