Minsky
wire.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2015
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 #ifndef WIRE_H
20 #define WIRE_H
21 
22 #include "noteBase.h"
23 #include "intrusiveMap.h"
24 
25 #include <error.h>
26 #include <arrays.h>
27 #include <TCL_obj_base.h>
28 #include <accessor.h>
29 #include "classdesc_access.h"
30 #include <cairo.h>
31 
32 namespace minsky
33 {
34  class Wire;
35  class Port;
36  class Group;
37  class Item;
38  struct Units;
39  using ecolab::error;
40 
41  struct WireAccessor: public ecolab::TCLAccessor<Wire, std::vector<float>> {WireAccessor();};
42 
43  class Wire: public NoteBase, public WireAccessor
44  {
46  friend struct SchemaHelper;
47 
48  // intermediate control point coords, relative to ports
49  std::vector<float> m_coords;
51  std::weak_ptr<Port> m_from, m_to;
52 
53  constexpr static float handleRadius=3;
54  mutable int unitsCtr=0;
55  mutable std::vector<std::pair<float,float>> cairoCoords;
56  public:
57 
58  Wire() {}
59  Wire(const std::weak_ptr<Port>& from, const std::weak_ptr<Port>& to,
60  const std::vector<float>& a_coords=std::vector<float>());
61  ~Wire();
62 
63  std::shared_ptr<Port> from() const {return m_from.lock();}
64  std::shared_ptr<Port> to() const {return m_to.lock();}
65 
67  void moveToPorts(const std::shared_ptr<Port>& from, const std::shared_ptr<Port>& to);
69  void storeCairoCoords(cairo_t* cairo) const;
70 
72  void draw(cairo_t* cairo, bool reverseArrow=false) const;
73 
75  std::vector<float> coords() const;
76  std::vector<float> coords(const std::vector<float>& coords);
77 
78 #undef near
79  bool near(float x, float y) const;
84  unsigned nearestHandle(float x, float y);
85  void insertHandle(unsigned position, float x, float y);
86  // For ticket 1092. Reinstate delete handle user interaction
87  void deleteHandle(float x, float y);
88  void editHandle(unsigned position, float x, float y);
89 
90  void straighten() {m_coords.clear();}
91 
93  bool visible() const;
95  void moveIntoGroup(Group& dest);
97  void split();
99  Units units(bool) const;
100  };
101 
102  typedef std::shared_ptr<Wire> WirePtr;
103  typedef std::vector<WirePtr> Wires;
104 
105 }
106 
107 #ifdef CLASSDESC
108 // omit these, because weak/shared pointers cause problems, and its
109 // not needed anyway
110 #pragma omit pack minsky::Wire
111 #pragma omit unpack minsky::Wire
112 #endif
113 namespace classdesc_access
114 {
115 template <> struct access_pack<minsky::Wire>:
116  public classdesc::NullDescriptor<classdesc::pack_t> {};
117 template <> struct access_unpack<minsky::Wire>:
118  public classdesc::NullDescriptor<classdesc::unpack_t> {};
119 }
120 #include "wire.cd"
121 #include "wire.xcd"
122 #endif
bool visible() const
whether this wire is visible or not
Definition: wire.cc:109
std::vector< WirePtr > Wires
Definition: wire.h:103
void straighten()
Definition: wire.h:90
std::shared_ptr< Port > from() const
Definition: wire.h:63
std::vector< std::pair< float, float > > cairoCoords
contains all the internal cairo coordinates used to draw a wire
Definition: wire.h:55
std::shared_ptr< Port > to() const
Definition: wire.h:64
static constexpr float handleRadius
Definition: wire.h:53
std::vector< float > m_coords
Definition: wire.h:49
Wire()
Definition: wire.h:58
std::shared_ptr< Wire > WirePtr
Definition: wire.h:102
void deleteHandle(float x, float y)
Definition: wire.cc:655
int unitsCtr
for detecting wiring loops in units()
Definition: wire.h:54
void insertHandle(unsigned position, float x, float y)
Definition: wire.cc:643
void storeCairoCoords(cairo_t *cairo) const
stash all the internal cairo coordinates along a wire
Definition: wire.cc:350
std::vector< float > coords() const
display coordinates
Definition: wire.cc:45
unsigned nearestHandle(float x, float y)
returns the index into the coordinate list if x,y is close to it. Otherwise inserts midpoints and ret...
Definition: wire.cc:603
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
CLASSDESC_ACCESS(Wire)
std::weak_ptr< Port > m_to
Definition: wire.h:51
void split()
splits wires crossing group boundaries
Definition: wire.cc:513
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
bool near(float x, float y) const
returns true if coordinates are near this wire
Definition: wire.cc:574
std::weak_ptr< Port > m_from
ports this wire connects
Definition: wire.h:51
void moveIntoGroup(Group &dest)
move this from its group into dest
Definition: wire.cc:131
Units units(bool) const
units (dimensional analysis) of data flowing across wire
Definition: wire.cc:542
void moveToPorts(const std::shared_ptr< Port > &from, const std::shared_ptr< Port > &to)
switch ports this wire links to
Definition: wire.cc:118
void draw(cairo_t *cairo, bool reverseArrow=false) const
draw this item into a cairo context
Definition: wire.cc:374
void editHandle(unsigned position, float x, float y)
Definition: wire.cc:674