Minsky: 3.17.0
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 "classdesc_access.h"
28 #include <cairo.h>
29 
30 namespace minsky
31 {
32  class Wire;
33  class Port;
34  class Group;
35  class Item;
36  struct Units;
37  using ecolab::error;
38 
39  class Wire: public NoteBase
40  {
42  friend struct SchemaHelper;
43 
44  // intermediate control point coords, relative to ports
45  std::vector<float> m_coords;
47  std::weak_ptr<Port> m_from, m_to;
48 
49  constexpr static float handleRadius=3;
50  mutable int unitsCtr=0;
51  mutable std::vector<std::pair<float,float>> cairoCoords;
52  public:
53 
54  Wire() {}
55  Wire(const std::weak_ptr<Port>& from, const std::weak_ptr<Port>& to,
56  const std::vector<float>& a_coords=std::vector<float>());
57  ~Wire();
58 
59  std::shared_ptr<Port> from() const {return m_from.lock();}
60  std::shared_ptr<Port> to() const {return m_to.lock();}
61 
63  void moveToPorts(const std::shared_ptr<Port>& from, const std::shared_ptr<Port>& to);
65  void storeCairoCoords(cairo_t* cairo) const;
66 
68  void draw(cairo_t* cairo, bool reverseArrow=false) const;
69 
71  std::vector<float> coords() const;
72  std::vector<float> coords(const std::vector<float>& coords);
73 
74 #undef near
75  bool near(float x, float y) const;
80  unsigned nearestHandle(float x, float y);
81  void insertHandle(unsigned position, float x, float y);
82  // For ticket 1092. Reinstate delete handle user interaction
83  void deleteHandle(float x, float y);
84  void editHandle(unsigned position, float x, float y);
85 
86  void straighten() {m_coords.clear();}
87 
89  bool visible() const;
91  void moveIntoGroup(Group& dest);
93  void split();
95  Units units(bool) const;
96  };
97 
98  typedef std::shared_ptr<Wire> WirePtr;
99  typedef std::vector<WirePtr> Wires;
100 
101 }
102 
103 #ifdef CLASSDESC
104 // omit these, because weak/shared pointers cause problems, and its
105 // not needed anyway
106 #pragma omit pack minsky::Wire
107 #pragma omit unpack minsky::Wire
108 #endif
109 namespace classdesc_access
110 {
111 template <> struct access_pack<minsky::Wire>:
112  public classdesc::NullDescriptor<classdesc::pack_t> {};
113 template <> struct access_unpack<minsky::Wire>:
114  public classdesc::NullDescriptor<classdesc::unpack_t> {};
115 }
116 #include "wire.cd"
117 #include "wire.xcd"
118 #endif
bool visible() const
whether this wire is visible or not
Definition: wire.cc:105
std::vector< WirePtr > Wires
Definition: wire.h:99
void straighten()
Definition: wire.h:86
std::shared_ptr< Port > from() const
Definition: wire.h:59
std::vector< std::pair< float, float > > cairoCoords
contains all the internal cairo coordinates used to draw a wire
Definition: wire.h:51
std::shared_ptr< Port > to() const
Definition: wire.h:60
static constexpr float handleRadius
Definition: wire.h:49
std::vector< float > m_coords
Definition: wire.h:45
Wire()
Definition: wire.h:54
std::shared_ptr< Wire > WirePtr
Definition: wire.h:98
void deleteHandle(float x, float y)
Definition: wire.cc:651
int unitsCtr
for detecting wiring loops in units()
Definition: wire.h:50
void insertHandle(unsigned position, float x, float y)
Definition: wire.cc:639
void storeCairoCoords(cairo_t *cairo) const
stash all the internal cairo coordinates along a wire
Definition: wire.cc:346
std::vector< float > coords() const
display coordinates
Definition: wire.cc:41
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:599
CLASSDESC_ACCESS(Wire)
std::weak_ptr< Port > m_to
Definition: wire.h:47
void split()
splits wires crossing group boundaries
Definition: wire.cc:509
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:570
std::weak_ptr< Port > m_from
ports this wire connects
Definition: wire.h:47
void moveIntoGroup(Group &dest)
move this from its group into dest
Definition: wire.cc:127
Units units(bool) const
units (dimensional analysis) of data flowing across wire
Definition: wire.cc:538
void moveToPorts(const std::shared_ptr< Port > &from, const std::shared_ptr< Port > &to)
switch ports this wire links to
Definition: wire.cc:114
void draw(cairo_t *cairo, bool reverseArrow=false) const
draw this item into a cairo context
Definition: wire.cc:370
void editHandle(unsigned position, float x, float y)
Definition: wire.cc:670