summaryrefslogtreecommitdiff
path: root/iup/srcpplot/iupPPlotInteraction.h
blob: eacf7909d50c561b3555a17ff9998f97871b490c (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***************************************************************************
 *                                                                         *
 *   Copyright notice:                                                     *
 *                                                                         *
 *   This is free Pier ware. You may do whatever you want with this code.  *
 *   You may cont(r)act me by email: pierphil@xs4all.nl                    *
 *                                                                         *
 ***************************************************************************/

#ifndef __PPLOTINTERACTION_H__
#define __PPLOTINTERACTION_H__

#include "iupPPlot.h"

class PModifierKeys {
public:
  enum {
    kShift=1,
    kControl=2,
    kAlt=4
  };
  PModifierKeys (int inModifierKeys=0):mModifierKeys (inModifierKeys) {};


  bool IsShiftKeyDown () const {return mModifierKeys & kShift? true: false;};
  bool IsControlKeyDown () const {return mModifierKeys & kControl? true: false;};
  bool IsAltKeyDown () const {return mModifierKeys & kAlt? true: false;};
  bool IsOnlyShiftKeyDown () const {return mModifierKeys == kShift? true: false;};
  bool IsOnlyControlKeyDown () const {return mModifierKeys == kControl? true: false;};
  bool IsOnlyAltKeyDown () const {return mModifierKeys == kAlt? true: false;};

  bool HasModifierKeys () const {return mModifierKeys != 0;};

  void SetModifierKeys (int inModifierKeys) {mModifierKeys = inModifierKeys;};
private:
  int mModifierKeys;// values like kShift | kAlt
};

class PMouseEvent: public PModifierKeys {
public:
  enum EType {
    kNone,
    kDown,
    kUp,
    kMove
  };

  PMouseEvent (int inX=0, int inY=0, EType inType=kNone, int inModifierKeys=0);

  int mX;
  int mY;


  EType mType;

  bool IsNone () const {return mType == kNone;};
  bool IsMouseDown () const {return mType == kDown;};
  bool IsMouseUp () const {return mType == kUp;};
  bool IsMouseMove () const {return mType == kMove;};
};

class PKeyEvent: public PModifierKeys {
public:

  enum EKey {
    kNone,
    kArrowUp,
    kArrowDown,
    kArrowLeft,
    kArrowRight,
    kDelete,
    kChar
  };

  PKeyEvent (EKey inKey=kNone, int inRepeatCount=0, int inModifierKeys=0, char inChar=0);


  bool IsNone () const {return mKey == kNone;};
  bool IsArrowUp () const {return mKey == kArrowUp;};
  bool IsArrowDown () const {return mKey == kArrowDown;};
  bool IsArrowLeft () const {return mKey == kArrowLeft;};
  bool IsArrowRight () const {return mKey == kArrowRight;};
  bool IsDelete () const {return mKey == kDelete;};
  bool IsChar () const {return mKey == kChar;};

  int GetRepeatCount () const {return mRepeatCount;};
  char GetChar () const {return mChar;};
protected:
  EKey mKey;
  char mChar;

  int mRepeatCount;
};


class PPlotInteraction {
 public:

  typedef vector<PPlotInteraction *>tList;

  PPlotInteraction (PPlot &inPPlot);

  virtual bool HandleMouseEvent (const PMouseEvent &inEvent)=0;
  virtual bool HandleKeyEvent (const PKeyEvent &inEvent) {return false;};

  void SetEnabled (bool inBool) {mIsEnabled = inBool;};
  bool IsEnabled () const {return mIsEnabled;};
protected:
  PPlot &mPPlot;
  bool mIsEnabled;
};

class PAxisInfo {
public:
  typedef vector<AxisSetup> tList;
  AxisSetup mXAxisSetup;
  AxisSetup mYAxisSetup;
};

class PZoomInteraction: public PPlotInteraction, public PDrawer {
public:

  enum EZoomMode {
    kZoom_Region,
    kZoom_X,
    kZoom_Y
  };

  PZoomInteraction (PPlot &inPPlot);

  virtual bool HandleMouseEvent (const PMouseEvent &inEvent);
  virtual bool HandleKeyEvent (const PKeyEvent &inEvent);

  void DoZoomIn (float inX1, float inX2, float inY1, float inY2);
  void DoZoomOut (float inY1 = -1, float inY2 = -1);
  bool CanZoomOut () { return !mZoomHistory.empty (); };
  int  GetZoomStackSize () { return mZoomHistory.size (); };

  stack<PAxisInfo> mZoomHistory;
  EZoomMode mZoomMode;

  bool IsZoomRegion () const {return mZoomMode == kZoom_Region;};
  bool IsZoomX () const {return mZoomMode == kZoom_X;};
  bool IsZoomY () const {return mZoomMode == kZoom_Y;};
protected:
  void StoreCurrentAxisSetup ();
  virtual bool Draw (Painter &inPainter);
  bool CheckRange (float inFloat1, float inFloat2);

  void DoZoomIn ();

  bool mDragging;
  int mX1;
  int mY1;
  int mX2;
  int mY2;

};

class PlotDataIncrementerBounds {
public:
  PlotDataIncrementerBounds ();

  bool CheckBounds (float inValue) const;

  bool mLowerBoundEnabled;
  float mLowerBound;
  bool mUpperBoundEnabled;
  float mUpperBound;
};

class PlotDataIncrementer {
public:

  // all are none are incremented
  bool Increment (const vector<float> &inIncrementList, vector<float *> &inData, const PlotDataIncrementerBounds &inGlobalBounds, const vector<PlotDataIncrementerBounds> &inBoundList) const;

protected:
  bool Impl_Increment (const vector<float> &inIncrementList, vector<float *> &inData, const PlotDataIncrementerBounds &inGlobalBounds, const vector<PlotDataIncrementerBounds> &inBoundList, bool inDontChange) const;
};

class PSelectionInteractionListener {
public:
  virtual void HandlePSelectionInteraction ()=0;
};

class PSelectionInteraction: public PPlotInteraction, public PCalculator {
public:

  enum ECommand {
    kNone,
    kPointwiseSelection,
    kGlobalSelection,
    kSelectAll
  };

  PSelectionInteraction (PPlot &inPPlot);

  virtual bool HandleKeyEvent (const PKeyEvent &inEvent);
  virtual bool HandleMouseEvent (const PMouseEvent &inEvent);
  virtual bool Calculate (Painter &inPainter, PPlot& inPPlot);

  void SetCommand (ECommand inCommand, const PKeyEvent &inKeyEvent, const PMouseEvent &inMouseEvent);
  void SetListener (PSelectionInteractionListener *inListener) {mListener = inListener;};
protected:
  PSelectionInteractionListener *mListener;

  virtual bool SelectNotify(int inIndex, int inSampleIndex, PlotDataBase *inXData, PlotDataBase *inYData, bool inSelect) {return true;}
  void UpdateSelection (int inIndex, int inSampleIndex, PlotDataBase *inXData, PlotDataBase *inYData, bool inHit, PlotDataSelection *inPlotDataSelection);
  void HandleGlobalInteraction (int inIndex, PlotDataBase *inXData, PlotDataBase *inYData, bool inHit, long inNearestPointIndex, DataDrawerBase *inDataDrawer, PlotDataSelection *inPlotDataSelection);
  void HandlePointwiseInteraction (int inIndex, PlotDataBase *inXData, PlotDataBase *inYData, bool inHit, long inNearestPointIndex, DataDrawerBase *inDataDrawer, PlotDataSelection *inPlotDataSelection);
  float CalculateDistanceToPlot (const PlotDataBase *inXData, const PlotDataBase *inYData, long &outNearestPointIndex);
  void SelectAll (int inIndex, PlotDataBase *inXData, PlotDataBase *inYData, PlotDataSelection *inPlotDataSelection);
//  int mX;
//  int mY;
  ECommand mCommand;
  PMouseEvent mMouseEvent;
  PKeyEvent mKeyEvent;
};


class PKeySelectionInteraction: public PPlotInteraction, public PCalculator {
public:
  PKeySelectionInteraction (PPlot &inPPlot);

  virtual bool Calculate (Painter &inPainter, PPlot& inPPlot);

protected:

  bool mCalculate;
};

class PEditInteractionListener {
public:
  virtual void HandlePEditInteraction ()=0;
};

class PEditInteraction: public PPlotInteraction, public PCalculator {
public:
  PEditInteraction (PPlot &inPPlot);

  virtual bool HandleMouseEvent (const PMouseEvent &inEvent) {return false;};
  virtual bool HandleKeyEvent (const PKeyEvent &inEvent);
  virtual bool ShouldCalculate () const {return mCalculate;};
  virtual bool Calculate (Painter &inPainter, PPlot& inPPlot);

  virtual bool Impl_HandleKeyEvent (const PKeyEvent &inEvent)=0;
  virtual bool Impl_Calculate (Painter &inPainter, PPlot& inPPlot)=0;
  void SetListener (PEditInteractionListener *inListener) {mListener = inListener;};
protected:
  PEditInteractionListener *mListener;
  PKeyEvent mKeyEvent;
private:
  bool mCalculate;
};

class PVerticalCursorInteraction: public PEditInteraction {
public:

  PVerticalCursorInteraction (PPlot &inPPlot);

  virtual bool Impl_HandleKeyEvent (const PKeyEvent &inEvent);
  virtual bool Impl_Calculate (Painter &inPainter, PPlot& inPPlot);

  PlotDataIncrementerBounds mGlobalBounds;
protected:
  void HandleVerticalCursorKey (const PlotDataSelection *inPlotDataSelection, PlotDataBase *inYData);
};

class PDeleteInteraction: public PEditInteraction {
public:

  PDeleteInteraction (PPlot &inPPlot);

  virtual bool Impl_HandleKeyEvent (const PKeyEvent &inEvent);
  virtual bool Impl_Calculate (Painter &inPainter, PPlot& inPPlot);

protected:
  void HandleDeleteKey (PlotDataBase *inXData, PlotDataBase *inYData, PlotDataSelection *inPlotDataSelection, int inIndex);
  virtual bool DeleteNotify(int inIndex, int inSampleIndex, PlotDataBase *inXData, PlotDataBase *inYData) {return true;}
};

class PCrosshairInteractionListener {
public:
  virtual void HandleCrosshair (int inIndex, int inPlotCount, float inX, float inY)=0;
};

class PCrosshairInteraction: public PPlotInteraction, public PDrawer  {
public:
  PCrosshairInteraction (PPlot &inPPlot);

  void SetListener (PCrosshairInteractionListener *inListener) {mListener = inListener;};
protected:
  virtual bool HandleMouseEvent (const PMouseEvent &inEvent);
  virtual bool Draw (Painter &inPainter);

  bool GetCrossPoint (const PlotDataBase *inXData, const PlotDataBase *inYData, float &outY);
  bool mActive;
  int mX;
  PCrosshairInteractionListener *mListener;
};

class InteractionContainer {
public:
  InteractionContainer (){};
  virtual ~InteractionContainer (){};

  bool HandleMouseEvent (const PMouseEvent &inEvent);
  bool HandleKeyEvent (const PKeyEvent &inEvent);

  void AddInteraction (PPlotInteraction &inInteraction){mList.push_back(&inInteraction);};

protected:
  PPlotInteraction::tList mList;
};

class DefaultInteractionContainer: public InteractionContainer {
public:
  DefaultInteractionContainer (PPlot &inPPlot);

  PZoomInteraction mZoomInteraction;
  PSelectionInteraction mSelectionInteraction;
  PVerticalCursorInteraction mVerticalCursorInteraction;
  PDeleteInteraction mDeleteInteraction;
  PCrosshairInteraction mCrosshairInteraction;
};

#endif