Minsky
ravelWrap.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2018
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef RAVELWRAP_H
21 #define RAVELWRAP_H
22 
23 #include "operation.h"
24 #include "cairoRenderer.h"
25 #include "dynamicRavelCAPI.h"
26 #include "hypercube.h"
27 #include "handleLockInfo.h"
28 #include "renderNativeWindow.h"
29 #include "SVGItem.h"
30 
31 namespace minsky
32 {
33  using namespace civita;
34  class RavelLockGroup;
35 
36  class Ravel;
38  {
40  float width, height, scale=1;
41  float localX(float x) const;
42  float localY(float y) const;
43  bool redraw(int x0, int y0, int width, int height) override;
45  public:
46  RavelPopup(Ravel& ravel): ravel(ravel) {}
47  void mouseDown(float x, float y) override;
48  void mouseUp(float x, float y) override;
49  void mouseMove(float x, float y) override;
50  void mouseOver(float x, float y);
51  void mouseLeave();
52  bool keyPress(const EventInterface::KeyPressArgs&) override;
53  };
54 
55  class Ravel: public ItemT<Ravel, Operation<OperationType::ravel>>, public classdesc::Exclude<ravel::Ravel>
56  {
57  public:
58 
60  private:
61  using HandleState=ravel::HandleState;
63  const double moveX=0.5, moveY=0.5, moveSz=0.1;
64 
66  bool m_editorMode=false;
67  //std::string m_filename;
68  std::string explanation; // explanation of Ravel bits displayed as tooltip
69  ravel::HandleSort::Order previousOrder=ravel::HandleSort::forward;
70 
73  ravel::RavelState initState;
74 
75  friend struct SchemaHelper;
76  friend RavelPopup;
77 
78  std::vector<std::string> allSliceLabelsImpl(int axis, ravel::HandleSort::Order) const;
79 
80  ravel::Ravel wrappedRavel;
81  ravel::Op::ReductionOp m_nextReduction=ravel::Op::sum;
82  public:
85  bool flipped=false;
86  Ravel();
87  // copy operations needed for clone, but not really used for now
88  // define them as empty operations to prevent double frees if accidentally used
89  void operator=(const Ravel&) {}
90  Ravel(const Ravel& x): ItemT<Ravel, Operation<OperationType::ravel>>(x), popup(*this) {
91  applyState(x.getState());
92  }
93 
94  const Ravel* ravelCast() const override {return this;}
95  Ravel* ravelCast() override {return this;}
96 
97  double radius() const {return wrappedRavel.radius();}
98 
100  Dimensions axisDimensions;
101 
103  std::shared_ptr<RavelLockGroup> lockGroup;
104  void leaveLockGroup();
105  void broadcastStateToLockGroup() const;
106 
107  void draw(cairo_t* cairo) const override;
108  void resize(const LassoBox&) override;
109  bool inItem(float x, float y) const override;
110  void onMouseDown(float x, float y) override;
111  void onMouseUp(float x, float y) override;
112  bool onMouseMotion(float x, float y) override;
113  bool onMouseOver(float x, float y) override;
114  void onMouseLeave() override {wrappedRavel.onMouseLeave();}
116  Hypercube hypercube() const;
117  void populateHypercube(const Hypercube&);
119  unsigned maxRank() const {return wrappedRavel.numHandles();}
120  unsigned numHandles() const {return wrappedRavel.numHandles();}
122  size_t numSliceLabels(size_t axis) const {return wrappedRavel.numSliceLabels(axis);}
124  void setRank(unsigned r);
125  void adjustSlicer(int);
126  bool onKeyPress(int, const std::string&, int) override;
128  void redistributeHandles() {wrappedRavel.redistributeHandles();}
130  void nextReduction(ravel::Op::ReductionOp op) {m_nextReduction=op; wrappedRavel.nextReduction(op);}
132  void handleSetReduction(int handle, ravel::Op::ReductionOp op) {wrappedRavel.handleSetReduction(handle, op);}
134  int selectedHandle() const {return wrappedRavel.selectedHandle();}
137  void collapseAllHandles(bool collapse=true);
138 
140  bool displayFilterCaliper() const;
141  bool setDisplayFilterCaliper(bool x);
143  {return setDisplayFilterCaliper(!displayFilterCaliper());}
145 
147  std::vector<std::string> allSliceLabels() const;
149  std::vector<std::string> allSliceLabelsAxis(int axis) const;
151  std::vector<std::string> pickedSliceLabels() const;
152  std::vector<std::string> pickedSliceLabels(int axis) const;
154  void pickSliceLabels(int axis, const std::vector<std::string>& pick);
155 
157  std::vector<std::string> handleNames() const {
158  std::vector<std::string> r;
159  for (size_t i=0; i<wrappedRavel.numHandles(); ++i)
160  r.push_back(wrappedRavel.handleDescription(i));
161  return r;
162  }
163 
165  Dimension dimension(int handle) const;
166 
169  ravel::HandleSort::Order sortOrder() const;
170  ravel::HandleSort::Order setSortOrder(ravel::HandleSort::Order);
172 
173  void resortHandleIfDynamic();
174 
176  ravel::HandleSort::Order setHandleSortOrder(ravel::HandleSort::Order, int handle);
177 
179  bool handleSortableByValue() const;
182  void sortByValue(ravel::HandleSort::Order dir);
183 
185  std::string description() const;
186  void setDescription(const std::string&);
189  std::string handleDescription(int handle) const {return wrappedRavel.handleDescription(handle);}
191  void setHandleDescription(int handle, const std::string& description)
192  {wrappedRavel.setHandleDescription(handle,description);}
193 
195  Dimension::Type dimensionType() const;
196  Dimension::Type dimensionType(int) const;
197  std::string dimensionUnitsFormat() const;
198  std::string dimensionUnitsFormat(int) const;
200  void setDimension(Dimension::Type type,const std::string& units);
202  void setDimension(int handleIndex, Dimension::Type type,const std::string& units);
204 
206  ravel::RavelState getState() const {return wrappedRavel? wrappedRavel.getRavelState(): initState;}
209  void applyState(const ravel::RavelState& state);
210  void displayDelayedTooltip(float x, float y) override;
213  void exportAsCSV(const std::string& filename, bool tabular) const;
214 
215  Units units(bool) const override;
216 
218  bool editorMode() const override {return m_editorMode;}
219  void toggleEditorMode() override {m_editorMode=!m_editorMode;updateBoundingBox();}
220 
221  static std::vector<unsigned> lockGroupColours();
222  void joinLockGroup(unsigned);
223  };
224 
226  {
227  static unsigned nextColour;
228  unsigned m_colour;
229  std::vector<std::weak_ptr<Ravel>> m_ravels;
230  public:
231  RavelLockGroup() {m_colour=nextColour++;}
232  void addRavel(const std::weak_ptr<Ravel>& ravel);
233  const std::vector<std::weak_ptr<Ravel> >& ravels() const {return m_ravels;}
235  void initialBroadcast();
237  void broadcast(const Ravel& ravel);
239  unsigned colour() const {return m_colour;}
240  std::vector<HandleLockInfo> handleLockInfo;
241 
244  void validateLockHandleInfo();
245 
247  std::vector<std::string> allLockHandles() const;
248 
250  std::vector<std::string> ravelNames() const;
251 
253  std::vector<std::string> handleNames(size_t ravel_idx) const;
254 
256  void setLockHandles(const std::vector<std::string>& handles);
258  void addHandleInfo(const std::weak_ptr<Ravel>& ravel);
259  void removeFromGroup(const Ravel&);
260  };
261 }
262 
263 #include "ravelWrap.cd"
264 
265 #endif
266 
int selectedHandle() const
current handle mouse is over, or -1 if none
Definition: ravelWrap.h:134
RavelPopup(Ravel &ravel)
Definition: ravelWrap.h:46
unsigned colour() const
an identifying tag used to colour locked ravels on canvas
Definition: ravelWrap.h:239
std::vector< std::weak_ptr< Ravel > > m_ravels
Definition: ravelWrap.h:229
ravel::RavelState getState() const
get the current state of the Ravel
Definition: ravelWrap.h:206
void nextReduction(ravel::Op::ReductionOp op)
sets the type of the next reduction operation
Definition: ravelWrap.h:130
double radius() const
Definition: ravelWrap.h:97
bool toggleDisplayFilterCaliper()
Definition: ravelWrap.h:142
void redistributeHandles()
redistribute handles according to current state
Definition: ravelWrap.h:128
represents rectangular region of a lasso operation
Definition: lasso.h:28
static SVGRenderer svgRenderer
SVG icon to display when not in editor mode.
Definition: ravelWrap.h:83
ravel::Ravel wrappedRavel
Definition: ravelWrap.h:80
static unsigned nextColour
Definition: ravelWrap.h:227
ravel::RavelState initState
used entirely to defer persisted state data until after first load from a variable ...
Definition: ravelWrap.h:73
bool flipped(double rotation)
returns if the angle (in degrees) is in the second or third quadrant
Definition: geometry.h:102
bool editorMode() const override
indicate whether icon is in editor mode or icon mode
Definition: ravelWrap.h:218
void handleSetReduction(int handle, ravel::Op::ReductionOp op)
set the reduction type for handle
Definition: ravelWrap.h:132
mouseDownid x y X Y
Definition: godley.tcl:137
unsigned maxRank() const
Definition: ravelWrap.h:119
void onMouseLeave() override
respond to mouse leave events (when mouse leaves item)
Definition: ravelWrap.h:114
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
const Ravel * ravelCast() const override
Definition: ravelWrap.h:94
RavelPopup popup
popup Ravel control window
Definition: ravelWrap.h:84
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
#define CLASSDESC_ACCESS(type)
setDimension
Definition: ravel.tcl:181
std::vector< std::string > handleNames() const
return all handle names
Definition: ravelWrap.h:157
std::string handleDescription(int handle) const
return the description field for handle handle.
Definition: ravelWrap.h:189
std::shared_ptr< RavelLockGroup > lockGroup
group of ravels that move syncronously
Definition: ravelWrap.h:103
const std::vector< std::weak_ptr< Ravel > > & ravels() const
Definition: ravelWrap.h:233
void toggleEditorMode() override
Definition: ravelWrap.h:219
Ravel(const Ravel &x)
Definition: ravelWrap.h:90
size_t numSliceLabels(size_t axis) const
number of slice labels along axis axis
Definition: ravelWrap.h:122
void operator=(const Ravel &)
Definition: ravelWrap.h:89
std::vector< HandleLockInfo > handleLockInfo
Definition: ravelWrap.h:240
Ravel * ravelCast() override
Definition: ravelWrap.h:95
std::string explanation
Definition: ravelWrap.h:68
void setHandleDescription(int handle, const std::string &description)
set the description field for handle
Definition: ravelWrap.h:191
ravel::HandleState HandleState
Definition: ravelWrap.h:61
ravel::Op::ReductionOp ReductionOp
Definition: ravelWrap.cc:59
Dimensions axisDimensions
local override of axis dimensionality
Definition: ravelWrap.h:100
friend RavelPopup
Definition: ravelWrap.h:76
unsigned numHandles() const
Definition: ravelWrap.h:120