Minsky
cairoItems.cc
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 // Implementations of canvas items representing operations and variables.
20 
21 // invert the display of the power operator so that y is on top, and x
22 // below, for ticket #327
23 #define DISPLAY_POW_UPSIDE_DOWN
24 
25 #define BOOST_GEOMETRY_DISABLE_DEPRECATED_03_WARNING
26 #include <boost/geometry/geometry.hpp>
27 
28 #include "minsky.h"
29 
30 #include "cairoItems.h"
31 #include "operation.h"
32 #include "latexMarkup.h"
33 #include <arrays.h>
34 #include <pango.h>
35 #include "minsky_epilogue.h"
36 
37 #include <boost/locale.hpp>
38 
39 using namespace ecolab;
40 using ecolab::cairo::CairoSave;
41 using namespace std;
42 using namespace minsky;
43 using namespace boost::geometry;
44 
45 namespace
46 {
47  cairo::Surface dummySurf(cairo_image_surface_create(CAIRO_FORMAT_A1, 100,100));
48 }
49 
50 RenderVariable::RenderVariable(const VariableBase& var, cairo_t* cairo):
51  Pango(cairo? cairo: dummySurf.cairo()), var(var), cairo(cairo)
52 {
53  setFontSize(12);
55  {
56  try
57  {
58  auto val=var.engExp();
59  if (val.engExp==-3) val.engExp=0; //0.001-1.0
60  setMarkup(var.mantissa(val)+expMultiplier(val.engExp));
61  }
62  catch (const std::exception& ex)
63  {
64  setMarkup("0");
65  }
66  w=0.5*Pango::width();
67  h=0.5*Pango::height();
68  }
69  else
70  {
71  setMarkup(latexToPango(var.name()));
72  w=0.5*Pango::width();
73  h=0.5*Pango::height();
74  if (!var.ioVar())
75  { // add additional space for numerical display
76  w+=12;
77  h+=4;
78  }
79  }
80  hoffs=Pango::top();
81 }
82 
84 {
85  var.draw(cairo);
86 
87 }
88 
89 bool RenderVariable::inImage(float x, float y)
90 {
91  const float dx=x-var.x(), dy=y-var.y();
92  const float rx=dx*cos(var.rotation()*M_PI/180)-dy*sin(var.rotation()*M_PI/180);
93  const float ry=dy*cos(var.rotation()*M_PI/180)+dx*sin(var.rotation()*M_PI/180);
94  return rx>=-w && rx<=w && ry>=-h && ry <= h;
95 }
96 
98 {
99  if (auto vv=var.vValue())
100  {
101  vv->adjustSliderBounds();
102  assert(vv->sliderMin<vv->sliderMax);
103  return (w<0.5*var.iWidth()? 0.5*var.iWidth() : w)*(vv->value()-0.5*(vv->sliderMin+vv->sliderMax))/(vv->sliderMax-vv->sliderMin);
104  }
105  else
106  return 0;
107 }
108 
110 (cairo_t* cairo, double x, double y, const cairo::Colour& col, double angle)
111 {
112  const CairoSave cs(cairo);
113  cairo_new_path(cairo);
114  cairo_set_source_rgba(cairo,col.r,col.g,col.b,col.a);
115  cairo_translate(cairo,x,y);
116  cairo_rotate(cairo, angle);
117  cairo_move_to(cairo,10,0);
118  cairo_line_to(cairo,0,-3);
119  cairo_line_to(cairo,0,3);
120  cairo_fill(cairo);
121 }
122 
#define M_PI
some useful geometry types, defined from boost::geometry
Definition: geometry.h:29
std::string expMultiplier(int exp)
std::string latexToPango(const char *s)
Definition: latexMarkup.h:30
Expr sin(const Expr &x)
Definition: expr.h:131
Expr cos(const Expr &x)
Definition: expr.h:137
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
STL namespace.
double handlePos() const
Definition: cairoItems.cc:97
virtual Type type() const =0
cairo::Surface dummySurf(cairo_image_surface_create(CAIRO_FORMAT_A1, 100, 100))
const VariableBase & var
Definition: cairoItems.h:36
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
bool ioVar() const override
indicates this is a group I/O variable
Definition: variable.cc:258
bool inImage(float x, float y)
return the boost geometry corresponding to this variable&#39;s shape
Definition: cairoItems.cc:89
EngNotation engExp() const
return formatted mantissa and exponent in engineering format
Definition: variable.h:227
virtual std::string name() const
variable displayed name
Definition: variable.cc:201
std::string mantissa(const EngNotation &e, int digits=3) const
Definition: variable.h:229
void drawTriangle(cairo_t *cairo, double x, double y, const ecolab::cairo::Colour &col, double angle=0)
void draw(cairo_t *) const override
Definition: variable.cc:704
std::shared_ptr< VariableValue > vValue() const
variableValue associated with this. nullptr if not associated with a variableValue ...
Definition: variable.cc:161
void draw()
render the cairo image
Definition: cairoItems.cc:83
double rotation() const
Definition: item.h:211