Minsky: 3.17.0
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 "ravelWrap.xcd"
36 #include "minsky_epilogue.h"
37 
38 #include <boost/locale.hpp>
39 
40 using namespace ecolab;
41 using ecolab::cairo::CairoSave;
42 using namespace std;
43 using namespace minsky;
44 using namespace boost::geometry;
45 
46 namespace
47 {
48  cairo::Surface dummySurf(cairo_image_surface_create(CAIRO_FORMAT_A1, 100,100));
49 }
50 
51 RenderVariable::RenderVariable(const VariableBase& var, cairo_t* cairo):
52  Pango(cairo? cairo: dummySurf.cairo()), var(var), cairo(cairo)
53 {
54  setFontSize(12);
56  {
57  try
58  {
59  auto val=var.engExp();
60  if (val.engExp==-3) val.engExp=0; //0.001-1.0
61  setMarkup(var.mantissa(val)+expMultiplier(val.engExp));
62  }
63  catch (const std::exception& ex)
64  {
65  setMarkup("0");
66  }
67  w=0.5*Pango::width();
68  h=0.5*Pango::height();
69  }
70  else
71  {
72  setMarkup(latexToPango(var.name()));
73  w=0.5*Pango::width();
74  h=0.5*Pango::height();
75  if (!var.ioVar())
76  { // add additional space for numerical display
77  w+=12;
78  h+=4;
79  }
80  }
81  hoffs=Pango::top();
82 }
83 
85 {
86  var.draw(cairo);
87 
88 }
89 
90 bool RenderVariable::inImage(float x, float y)
91 {
92  const float dx=x-var.x(), dy=y-var.y();
93  const float rx=dx*cos(var.rotation()*M_PI/180)-dy*sin(var.rotation()*M_PI/180);
94  const float ry=dy*cos(var.rotation()*M_PI/180)+dx*sin(var.rotation()*M_PI/180);
95  return rx>=-w && rx<=w && ry>=-h && ry <= h;
96 }
97 
99 {
100  if (auto vv=var.vValue())
101  {
102  vv->adjustSliderBounds();
103  assert(vv->sliderMin<vv->sliderMax);
104  return (w<0.5*var.iWidth()? 0.5*var.iWidth() : w)*(vv->value()-0.5*(vv->sliderMin+vv->sliderMax))/(vv->sliderMax-vv->sliderMin);
105  }
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:130
Expr cos(const Expr &x)
Definition: expr.h:136
virtual float x() const
Definition: item.cc:107
virtual float y() const
Definition: item.cc:114
STL namespace.
double handlePos() const
Definition: cairoItems.cc:98
virtual Type type() const =0
cairo::Surface dummySurf(cairo_image_surface_create(CAIRO_FORMAT_A1, 100, 100))
const VariableBase & var
Definition: cairoItems.h:36
float iWidth() const
Definition: item.h:214
bool ioVar() const override
indicates this is a group I/O variable
Definition: variable.cc:243
bool inImage(float x, float y)
return the boost geometry corresponding to this variable&#39;s shape
Definition: cairoItems.cc:90
EngNotation engExp() const
return formatted mantissa and exponent in engineering format
Definition: variable.h:214
virtual std::string name() const
variable displayed name
Definition: variable.cc:186
std::string mantissa(const EngNotation &e, int digits=3) const
Definition: variable.h:216
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:698
std::shared_ptr< VariableValue > vValue() const
variableValue associated with this. nullptr if not associated with a variableValue ...
Definition: variable.cc:146
void draw()
render the cairo image
Definition: cairoItems.cc:84
double rotation() const
Definition: item.h:208