Minsky
variablePane.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2022
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 #include "minsky.h"
21 #include "variablePane.h"
22 #include "cairoItems.h"
23 #include "pannableTab.rcd"
24 #include "pannableTab.xcd"
25 #include "variableValue.h"
26 #include "variablePane.rcd"
27 #include "variablePane.xcd"
28 #include "variableType.rcd"
29 #include "minsky_epilogue.h"
30 #include <stdexcept>
31 using namespace std;
32 
33 namespace minsky
34 {
35  VariablePaneCell::VariablePaneCell(const VariableValue& var):
36  var(var.type(), ':'+uqName(var.name))
37  {
38  const ecolab::cairo::Surface surf
39  (cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA,NULL));
40  cairo_move_to(surf.cairo(),0,0);
41  RenderVariable rv(*this->var, surf.cairo());
42  rv.draw();
43  double l,t;
44  cairo_recording_surface_ink_extents(surf.surface(),&l,&t,&m_width,&m_height);
45  }
46 
48  {
49  if (!cachedCairo || !var || var->type()==VariableType::undefined) return;
51  const ecolab::cairo::CairoSave cs(cachedCairo);
52  cairo_translate(cachedCairo,0.5*m_width,0.5*m_height);
53  rv.draw();
54  cairo_reset_clip(cachedCairo);
55  }
56 
58  {
59  if (var && var->type()!=VariableType::undefined)
60  minsky().canvas.addVariable(var->rawName(), var->type());
61  }
62 
64  {
65  if (var) return *var;
66  static const Variable<VariableType::undefined> undefined;
67  return undefined;
68  }
69 
70  VariablePaneCell& VariablePane::cell(unsigned row, unsigned col)
71  {
72  auto idx=row+m_numRows*col;
73  if (idx>=vars.size())
74  {
75  static VariablePaneCell emptyCell;
76  return emptyCell;
77  }
78  return vars[idx];
79  }
80 
81  void VariablePane::updateWithHeight(unsigned height)
82  {
83  const unsigned typicalHeight=38;
84  m_numRows=height/typicalHeight;
85  update();
86  }
87 
88 
90  {
91  if (!m_numRows) return;
92  vars.clear();
93  for (auto& v: cminsky().variableValues)
94  {
95  if (v.first.empty() || selection.contains(v.second->type())==0) continue; // ignore those filtered out
96  vars.emplace_back(*v.second);
97  }
98 
99  m_numCols=vars.size()/m_numRows+1;
100  const unsigned gridSize=m_numRows*m_numCols;
101  while (vars.size()<gridSize) vars.emplace_back();
102  if (surface.get()) surface->requestRedraw(); // TODO, plain requestRedraw doesn't work for Tk here...
103  }
104 
105  bool VariablePane::redraw(int, int, int, int)
106  {
107  if (surface.get()) {
108  cairo_t* cairo=surface->cairo();
109  for (auto& i: vars) i.reset(cairo);
110  cairo_identity_matrix(cairo);
112  }
113  return surface.get();
114  }
115 
116  void VariablePane::moveCursorTo(double x, double y)
117  {
118  if (surface.get())
119  {
120  cairo_identity_matrix(surface->cairo());
121  cairo_translate(surface->cairo(), x+offsx, y+offsy);
122  }
123  }
124 }
void update()
update variables from model, using previous height value
Definition: variablePane.cc:89
void addVariable(const std::string &name, VariableType::Type type)
item or wire obtained by get*At() calls
Definition: canvas.h:187
STL namespace.
std::string uqName(const std::string &name)
extract unqualified portion of name
Definition: valueId.cc:135
std::set< Type > selection
Definition: variablePane.h:59
const VariableBase & variable() const
Definition: variablePane.cc:63
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
bool redraw(int, int, int width, int height) override
Canvas canvas
Definition: minsky.h:256
const Minsky & cminsky()
const version to help in const correctness
Definition: minsky.h:549
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::VariablePaneBase)
void moveCursorTo(double, double) override
move current cursor to x,y
VariablePaneCell & cell(unsigned row, unsigned col) override
return reference to cell row, col. Reference is valid until next call to cell()
Definition: variablePane.cc:70
classdesc::Exclude< std::vector< VariablePaneCell > > vars
Definition: variablePane.h:54
void updateWithHeight(unsigned height)
update variables from model, given a window of height pixels
Definition: variablePane.cc:81
Minsky & minsky()
global minsky object
Definition: minskyTCL.cc:51
void draw()
render the cairo image
Definition: cairoItems.cc:83
void draw()
draw the grid
Definition: grid.cc:42