TestControlPointsItemEvents.cxx 6.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*=========================================================================

  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
Allison Vacanti's avatar
Allison Vacanti committed
17
#include "vtkChartXY.h"
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
#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;
  }

44
  vtkTFCallback() = default;
45 46

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

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

Allison Vacanti's avatar
Allison Vacanti committed
84 85
  vtkNew<vtkChartXY> chart;
  chart->AddPlot(controlPoints);
86 87

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

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

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

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

  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
158
    "LeftButtonReleaseEvent 55 1 0 0 0 0 0\n" // must be followed by release
159 160 161 162 163 164 165 166 167 168 169
    ;

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

  return EXIT_SUCCESS;
}