Minsky
godleyIcon.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
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 GODLEYICON_H
20 #define GODLEYICON_H
21 
22 #include "variable.h"
23 #include "godleyTable.h"
24 #include "godleyTableWindow.h"
25 #include "zoom.h"
26 #include "intrusiveMap.h"
27 #include "classdesc_access.h"
28 #include "SVGItem.h"
29 #include "group.h"
30 #include "valueId.h"
31 #include "variableValue.h"
32 
33 #include <map>
34 #include <cairo.h>
35 #include <cairo_base.h>
36 
37 namespace minsky
38 {
39  class GodleyTableEditor;
40 
41  class GodleyIcon: public ItemT<GodleyIcon>
42  {
43 
45  float flowMargin=0, stockMargin=0;
46  bool m_editorMode=false;
47  bool m_variableDisplay=false;
49  friend struct SchemaHelper;
50 
51 
52  void updateBB() {
53  auto wasSelected=selected;
54  selected=true; // ensure bounding box is set to the entire icon
55  bb.update(*this);
56  selected=wasSelected;
57  }
58 
59  double titleOffs() const {return !table.title.empty()? 15*zoomFactor(): 0;}
60 
61  public:
63 
65  GodleyIcon(const GodleyIcon&)=default;
66  ~GodleyIcon() {Item::removeControlledItems();}
67 
69  bool editorMode() const override {return m_editorMode;}
70  void toggleEditorMode() override;
71 
73  bool buttonDisplay() const;
74  void toggleButtons();
75 
76  bool variableDisplay() const {return m_variableDisplay;}
77  void toggleVariableDisplay();
78 
86 
88  void scaleIcon(float w, float h);
89 
91  float leftMargin() const {return variableDisplay()? flowMargin*scaleFactor()*zoomFactor(): 0;}
94 
95  void resize(const LassoBox&) override;
96  void removeControlledItems(GroupItems&) override;
97 
99  void setCell(int row, int col, const string& val);
101  void deleteRow(unsigned row);
104  {
106  };
107  void moveCell(const GodleyIcon::MoveCellArgs&);
109  std::map<string,double> flowSignature(unsigned col) const;
110 
111  typedef std::vector<VariablePtr> Variables;
112  const Variables& flowVars() const {return m_flowVars;}
113  const Variables& stockVars() const {return m_stockVars;}
114 
116  std::vector<Summary> summarise() const;
117 
119  void update();
120 
121  GodleyIcon* clone() const override {
122  auto r=new GodleyIcon(*this);
123  r->update();
124  r->group.reset();
125  return r;
126  }
127 
131  ItemPtr select(float x, float y) const override;
132  ClickType::Type clickType(float x, float y) const override;
133 
135  void draw(cairo_t* cairo) const override;
136 
138  std::string rowSum(int row) const;
139 
141  // TODO: this should be refactored to a more central location
142  std::string valueId(const std::string& x) const {return minsky::valueId(group.lock(), x);}
145  Units stockVarUnits(const std::string& stockName, bool check) const;
146 
147  std::string currency;
148  void setCurrency(const std::string& currency)
149  {this->currency=currency; for (auto& i: m_stockVars) i->setUnits(currency);}
150 
151  void insertControlled(Selection& selection) override;
152 
153  void onMouseDown(float, float) override;
154  void onMouseUp(float, float) override;
155  bool onMouseMotion(float, float) override;
156  bool onMouseOver(float, float) override;
157  void onMouseLeave() override;
158  bool onKeyPress(int, const std::string&, int) override;
159  bool inItem(float, float) const override;
160 
162  void destroyFrame() override {popup.destroyFrame();}
163 
165  bool wiresAttached() const;
166 
167  const GodleyIcon* godleyIconCast() const override {return this;}
168  GodleyIcon* godleyIconCast() override {return this;}
169 
170  private:
171  void updateVars(Variables& vars,
172  const vector<string>& varNames,
173  VariableBase::Type varType);
175  void positionVariables() const;
177 
179  float toEditorX(float) const;
180  float toEditorY(float) const;
181  };
182 }
183 
184 #ifdef CLASSDESC
185 // omit these, because weak/shared pointers cause problems, and its
186 // not needed anyway
187 #pragma omit pack minsky::GodleyIcon
188 #pragma omit unpack minsky::GodleyIcon
189 #endif
190 namespace classdesc_access
191 {
192  template <> struct access_pack<minsky::GodleyIcon>:
193  public classdesc::NullDescriptor<classdesc::pack_t> {};
194  template <> struct access_unpack<minsky::GodleyIcon>:
195  public classdesc::NullDescriptor<classdesc::unpack_t> {};
196 }
197 
198 #include "godleyIcon.cd"
199 #include "godleyIcon.xcd"
200 #endif
201 
bool editorMode() const override
indicate whether icon is in editor mode or icon mode
Definition: godleyIcon.h:69
void updateVars(Variables &vars, const vector< string > &varNames, VariableBase::Type varType)
Definition: godleyIcon.cc:94
void toggleEditorMode() override
Definition: godleyIcon.cc:155
std::vector< Summary > summarise() const
summary of variables in this godley table
Definition: godleyIcon.cc:271
represents items that have been selected
Definition: selection.h:32
GodleyIcon * godleyIconCast() override
Definition: godleyIcon.h:168
void toggleVariableDisplay()
Definition: godleyIcon.cc:149
bool wiresAttached() const
return true if any wires are attached to the variables
Definition: godleyIcon.cc:428
void adjustWidgets()
ensure button widgets are synced with current table data
double titleOffs() const
Definition: godleyIcon.h:59
bool buttonDisplay() const
enable/disable drawing buttons in table on canvas display
Definition: godleyIcon.cc:161
GodleyIcon * clone() const override
Definition: godleyIcon.h:121
Variables m_flowVars
Definition: godleyIcon.h:176
void scaleIcon(float w, float h)
scale icon until it&#39;s height or width matches h or w depending on which is minimum ...
Definition: godleyIcon.cc:171
virtual float x() const
Definition: item.cc:107
void moveCell(const GodleyIcon::MoveCellArgs &)
Definition: godleyIcon.cc:238
const GodleyIcon * godleyIconCast() const override
Definition: godleyIcon.h:167
virtual float y() const
Definition: item.cc:114
float iHeight() const
Definition: item.h:224
float toEditorY(float) const
convert mouse coordinates into editor coords
Definition: godleyIcon.cc:606
represents rectangular region of a lasso operation
Definition: lasso.h:28
void positionVariables() const
move contained variables to correct locations within icon
Definition: godleyIcon.cc:389
void resize(const LassoBox &) override
resize this item on the canvas
Definition: godleyIcon.cc:177
void setCell(int row, int col, const string &val)
set cell(row,col) with contents val
Definition: godleyIcon.cc:209
std::shared_ptr< Item > ItemPtr
Definition: item.h:57
float flowMargin
for placement of bank icon within complex
Definition: godleyIcon.h:45
std::string rowSum(int row) const
return the A-L-E row sum for row
Definition: godleyIcon.cc:522
const Variables & flowVars() const
Definition: godleyIcon.h:112
string valueId(const string &name)
construct a valueId from fully qualified name @ name should not be canonicalised
Definition: valueId.cc:75
bool onMouseOver(float, float) override
respond to mouse motion events (hover) without button pressed
Definition: godleyIcon.cc:621
BoundingBox bb
canvas bounding box.
Definition: item.h:195
CLASSDESC_ACCESS(GodleyIcon)
bool inItem(float, float) const override
Definition: godleyIcon.cc:89
virtual float zoomFactor() const
Definition: item.cc:121
move the contents of cell at (srcRow, srcCol) to (destRow, destCol).
Definition: godleyIcon.h:103
GodleyTable table
table data. Must be declared before editor
Definition: godleyIcon.h:80
void update(const Item &x)
Definition: item.cc:46
float leftMargin() const
left margin of bank icon with Godley icon
Definition: godleyIcon.h:91
bool variableDisplay() const
Definition: godleyIcon.h:76
static SVGRenderer svgRenderer
SVG icon to display when not in editor mode.
Definition: godleyIcon.h:62
void insertControlled(Selection &selection) override
Definition: godleyIcon.cc:581
std::string valueId(const std::string &x) const
returns valueid for variable reference in table
Definition: godleyIcon.h:142
ClickType::Type clickType(float x, float y) const override
returns the clicktype given a mouse click at x, y.
Definition: godleyIcon.cc:589
bool onMouseMotion(float, float) override
respond to mouse motion events with button pressed
Definition: godleyIcon.cc:615
GodleyTableWindow popup
for rendering the popup window
Definition: godleyIcon.h:84
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
float iWidth() const
Definition: item.h:217
Units stockVarUnits(const std::string &stockName, bool check) const
performs dimensional analysis on stock var column stockName
Definition: godleyIcon.cc:545
void update()
updates the variable lists with the Godley table
Definition: godleyIcon.cc:289
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
Variables m_stockVars
Definition: godleyIcon.h:176
std::map< string, double > flowSignature(unsigned col) const
flows, along with multipliers, appearing in col
Definition: godleyIcon.cc:258
bool selected
true if selected for cut, copy or group operation
Definition: noteBase.h:32
virtual float scaleFactor() const
factor by which item has been resized
Definition: item.cc:128
ItemPtr select(float x, float y) const override
returns the variable if point (x,y) is within a variable icon, null otherwise, indicating that the Go...
Definition: godleyIcon.cc:414
float toEditorX(float) const
convert mouse coordinates into editor coords
Definition: godleyIcon.cc:604
std::string title
Definition: godleyTable.h:60
std::vector< VariablePtr > Variables
Definition: godleyIcon.h:111
void draw(cairo_t *cairo) const override
draw icon to context
Definition: godleyIcon.cc:441
const Variables & stockVars() const
Definition: godleyIcon.h:113
void onMouseLeave() override
respond to mouse leave events (when mouse leaves item)
Definition: godleyIcon.cc:627
void removeControlledItems(GroupItems &) override
Definition: godleyIcon.cc:189
void setCurrency(const std::string &currency)
Definition: godleyIcon.h:148
void onMouseUp(float, float) override
respond to mouse up events
Definition: godleyIcon.cc:612
void adjustPopupWidgets()
Definition: godleyIcon.h:85
bool onKeyPress(int, const std::string &, int) override
respond to key press events
Definition: godleyIcon.cc:639
void onMouseDown(float, float) override
respond to mouse down events
Definition: godleyIcon.cc:609
Definition: group.tcl:84
GodleyTableEditor editor
rendering as a godley table
Definition: godleyIcon.h:82
float bottomMargin() const
bottom margin of bank icon with Godley icon
Definition: godleyIcon.h:93
std::string currency
Definition: godleyIcon.h:147
void destroyFrame() override
clean up popup window structures on window close
Definition: godleyIcon.h:162
void deleteRow(unsigned row)
delete row before row
Definition: godleyIcon.cc:225