"Fossies" - the Fresh Open Source Software Archive

Member "fityk-1.3.1/wxgui/plot.h" (13 May 2016, 6354 Bytes) of package /linux/misc/fityk-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "plot.h" see the Fossies "Dox" file reference documentation.

    1 // This file is part of fityk program. Copyright 2001-2013 Marcin Wojdyr
    2 // Licence: GNU General Public License ver. 2+
    3 
    4 #ifndef FITYK_WX_PLOT_H_
    5 #define FITYK_WX_PLOT_H_
    6 
    7 #include <limits.h>
    8 #include <vector>
    9 #include <wx/config.h>
   10 #include "fityk/fityk.h" // Point
   11 #include "fityk/common.h" // iround()
   12 #include "uplot.h" // BufferedPanel
   13 #include "cmn.h" // compatibility with wx2.8 (defined wxPenStyle, etc.)
   14 
   15 // convention: lowercase coordinates of point are real values,
   16 // and uppercase ones are coordinates of point on screen (integers).
   17 
   18 // INT_MIN, given as coordinate, is invalid value, means "cancel drawing"
   19 
   20 namespace fityk { class Model; class Data; struct Rect; }
   21 using fityk::Point;
   22 class wxPoint2DDouble;
   23 
   24 inline int get_pixel_width(wxDC const& dc)
   25 {
   26       int w;
   27       dc.GetClippingBox(NULL, NULL, &w, NULL);
   28       //if (w != 0) printf("Clipping On (width)\n");
   29       return w != 0 ? w : dc.GetSize().GetWidth();
   30 }
   31 inline int get_pixel_height(wxDC const& dc)
   32 {
   33       int h;
   34       dc.GetClippingBox(NULL, NULL, NULL, &h);
   35       //if (h != 0) printf("Clipping On (height)\n");
   36       return h != 0 ? h : dc.GetSize().GetHeight();
   37 }
   38 
   39 
   40 
   41 /// convertion between pixels and logical values
   42 class Scale
   43 {
   44 public:
   45     double scale, origin;
   46     bool logarithm, reversed;
   47 
   48     Scale() : scale(1.), origin(0.), logarithm(false), reversed(false) {}
   49 
   50     /// value -> pixel
   51     int px(double val) const { return iround(px_d(val)); }
   52     inline double px_d(double val) const;
   53     /// pixel -> value
   54     inline double val(int px) const;
   55 
   56     // Returns the same value as val(), but rounded in a smart way,
   57     // so when the number is formatted with "%.12g", it is not too long.
   58     double valr(int px) const;
   59 
   60     // set scale using minimum and maximum logical values and width/height
   61     // of the screen in pixels.
   62     // In case of y scale, where pixel=0 is at the top, m and M are switched
   63     void set(double m, double M, int pixels);
   64 
   65 private:
   66     static int inf_px(double t) { return t > 0 ? SHRT_MAX : SHRT_MIN; }
   67 };
   68 
   69 
   70 double Scale::px_d(double val) const
   71 {
   72     if (logarithm) {
   73         if (val <= 0)
   74             return inf_px(-scale);
   75         val = log(val);
   76     }
   77     double t = (val - origin) * scale;
   78     return fabs(t) < SHRT_MAX ? t : (double) inf_px(t);
   79 }
   80 
   81 double Scale::val(int px) const
   82 {
   83     double a = px / scale + origin;
   84     return logarithm ? exp(a) : a;
   85 }
   86 
   87 
   88 
   89 class Overlay
   90 {
   91 public:
   92     enum Mode
   93     {
   94         kNone,
   95         kRect,
   96         kLinearDraft,
   97         kPeakDraft,
   98         kSigmoidDraft,
   99         kFunction,
  100         kCrossHair,
  101         kVLine,
  102         kHLine,
  103         kVRange,
  104         kHRange
  105     };
  106     Overlay(BufferedPanel *panel) : panel_(panel), mode_(kNone),
  107                                     color_(192,192,192) {}
  108     void start_mode(Mode m, int x1, int y1) { mode_=m; x1_=x2_=x1; y1_=y2_=y1; }
  109     void switch_mode(Mode m) { mode_=m; }
  110     void change_pos(int x2,int y2)
  111         { x2_=x2; y2_=y2; if (mode_!=kNone) draw_overlay(); }
  112     void draw_overlay();
  113     void draw_lines(int n, wxPoint points[]);
  114     Mode mode() const { return mode_; }
  115     void bg_color_updated(const wxColour& bg);
  116 
  117 private:
  118     BufferedPanel *panel_;
  119     Mode mode_;
  120     wxColour color_;
  121     int x1_, x2_, y1_, y2_;
  122 };
  123 
  124 
  125 
  126 /// This class has no instances, MainPlot and AuxPlot are derived from it
  127 /// It knows how to draw on wxDC. Note that wxDC:SetClippingRegion() should be
  128 /// used together with wxDC::SetDeviceOrigin(). Clipping box is used only in
  129 /// get_pixel_width() and get_pixel_height() functions.
  130 /// When plotting a curve, values in each x from 0 to get_pixel_width() is
  131 /// calculated.
  132 
  133 class FPlot : public BufferedPanel
  134 {
  135 public:
  136     FPlot(wxWindow *parent);
  137     virtual ~FPlot();
  138 
  139     void set_font(wxDC &dc, wxFont const& font);
  140     void set_scale(int pixel_width, int pixel_height);
  141     int get_special_point_at_pointer(wxMouseEvent& event);
  142     Scale const& get_x_scale() const { return xs; }
  143     Scale const& get_y_scale() const { return ys; }
  144     virtual void save_settings(wxConfigBase *cf) const;
  145     virtual void read_settings(wxConfigBase *cf);
  146     void set_magnification(int m) { pen_width = m > 0 ? m : 1; }
  147     void draw_vertical_lines_on_overlay(int X1, int X2);
  148     virtual void set_bg_color(wxColour const& c);
  149 
  150 protected:
  151     Scale xs, ys;
  152     Overlay overlay;
  153 
  154     // properties stored in config
  155     int pen_width;
  156     wxColour activeDataCol, inactiveDataCol, xAxisCol;
  157     wxFont ticsFont;
  158     int point_radius;
  159     bool line_between_points;
  160     bool draw_sigma;
  161     bool x_axis_visible, y_axis_visible, xtics_visible, ytics_visible,
  162          xminor_tics_visible, yminor_tics_visible;
  163     bool x_grid, y_grid;
  164     int x_max_tics, y_max_tics, x_tic_size, y_tic_size;
  165 
  166     int downX, downY;
  167     std::vector<wxPoint> special_points; //used to mark positions of peak tops
  168     wxWindow *esc_source_; // temporary source of OnKeyDown() events
  169 
  170     void draw_xtics (wxDC& dc, fityk::Rect const& v, bool set_pen=true);
  171     void draw_ytics (wxDC& dc, fityk::Rect const &v, bool set_pen=true);
  172     double get_max_abs_y(double (*compute_y)(std::vector<Point>::const_iterator,
  173                                              fityk::Model const*),
  174                          std::vector<Point>::const_iterator first,
  175                          std::vector<Point>::const_iterator last,
  176                          fityk::Model const* model);
  177     void draw_data (wxDC& dc,
  178                     double (*compute_y)(std::vector<Point>::const_iterator,
  179                                         fityk::Model const*),
  180                     fityk::Data const* data,
  181                     fityk::Model const* model,
  182                     wxColour const& color = wxNullColour,
  183                     wxColour const& inactive_color = wxNullColour,
  184                     int Y_offset = 0,
  185                     bool cumulative=false);
  186 
  187     // if connect is true: connect, otherwise disconnect;
  188     // connect to the currently focused window and handle wxEVT_KEY_DOWN 
  189     // event: if Esc is pressed call cancel_action().
  190     void connect_esc_to_cancel(bool connect);
  191 
  192     virtual void cancel_action() {}
  193     // handler used by connect_esc_to_cancel()
  194     void OnKeyDown(wxKeyEvent& event);
  195 private:
  196     void draw_data_by_activity(wxDC& dc, wxPoint2DDouble *pp,
  197                                const std::vector<bool>& aa, bool state);
  198 };
  199 
  200 #endif
  201