1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// This file is part of fityk program. Copyright 2001-2013 Marcin Wojdyr
// Licence: GNU General Public License ver. 2+
// (It is also part of xyconvert and can be distributed under LGPL2.1)
/// utilities for making plot (BufferedPanel, scale_tics_step())
#include <wx/wx.h>
#include <wx/dcgraph.h>
#include "uplot.h"
//#include "cmn.h"
#if !defined(XYCONVERT) && !defined(STANDALONE_POWDIFPAT)
#include "frame.h" // frame->antialias()
inline bool antialias() { return frame->antialias(); }
#else
inline bool antialias() { return false; }
#endif
using namespace std;
BufferedPanel::BufferedPanel(wxWindow *parent)
: wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize,
wxNO_BORDER|wxFULL_REPAINT_ON_RESIZE),
support_antialiasing_(true), dirty_(true)
{
SetBackgroundStyle(wxBG_STYLE_CUSTOM);
Connect(wxEVT_IDLE, wxIdleEventHandler(BufferedPanel::OnIdle));
}
void BufferedPanel::redraw_now()
{
dirty_ = true;
Refresh(false);
Update();
}
void BufferedPanel::gc_draw(wxMemoryDC& dc)
{
if (antialias() && support_antialiasing_) {
wxGCDC gdc(dc);
#ifdef __WXMSW__
// this is necessary only for PNG output on Windows,
// together with explicit 32-bit bitmap depth in
// PlotPane::prepare_bitmap_for_export()
gdc.SetBackground(wxBrush(bg_color_));
gdc.Clear();
#endif
draw(gdc);
} else
draw(dc);
}
wxBitmap BufferedPanel::draw_on_bitmap(int w, int h, int depth)
{
wxBitmap bmp = wxBitmap(w, h, depth);
memory_dc_.SelectObject(bmp);
memory_dc_.SetBackground(wxBrush(bg_color_));
memory_dc_.Clear();
gc_draw(memory_dc_);
if (buffer_.Ok()) {
memory_dc_.SelectObject(buffer_);
memory_dc_.SetBackground(wxBrush(bg_color_));
}
return bmp;
}
void BufferedPanel::update_buffer_and_blit()
{
//cout << "BufferedPanel::update_buffer_and_blit()" << endl;
wxPaintDC pdc(this);
// check size
wxCoord w, h;
pdc.GetSize(&w, &h);
if (w == 0 || h == 0)
return;
if (!buffer_.Ok() || w != buffer_.GetWidth() || h != buffer_.GetHeight()) {
memory_dc_.SelectObject(wxNullBitmap);
buffer_ = wxBitmap(w, h);
memory_dc_.SelectObject(buffer_);
memory_dc_.SetBackground(wxBrush(bg_color_));
dirty_ = true;
}
// update the buffer if necessary
if (dirty_) {
memory_dc_.SetLogicalFunction(wxCOPY);
memory_dc_.Clear();
gc_draw(memory_dc_);
// This condition is almost always true. It was added because on
// wxGTK 2.8 with some window managers, after loading a data file
// the next paint event had the update region set to the area
// of the file dialog, and the rest of the plot remained not updated.
if (GetUpdateRegion().GetBox().GetSize() == pdc.GetSize())
dirty_ = false;
}
blit(pdc);
}
void BufferedPanel::blit(wxDC& dc)
{
dc.Blit(0, 0, buffer_.GetWidth(), buffer_.GetHeight(), &memory_dc_, 0, 0);
}
void BufferedPanel::OnIdle(wxIdleEvent&)
{
if (dirty_)
Refresh(false);
}
void BufferedPanel::set_bg_color(wxColour const& c)
{
bg_color_ = c;
if (memory_dc_.IsOk())
memory_dc_.SetBackground(wxBrush(c));
}
PlotWithTics::PlotWithTics(wxWindow* parent)
: BufferedPanel(parent)
{
Connect(GetId(), wxEVT_PAINT, wxPaintEventHandler(PlotWithTics::OnPaint));
}
void PlotWithTics::draw_tics(wxDC &dc, double x_min, double x_max,
double y_min, double y_max)
{
// prepare scaling
double const margin = 0.1;
double dx = x_max - x_min;
double dy = y_max - y_min;
int W = dc.GetSize().GetWidth();
int H = dc.GetSize().GetHeight();
xScale = (1 - 1.2 * margin) * W / dx;
yScale = - (1 - 1.2 * margin) * H / dy;
xOffset = - x_min * xScale + margin * W;
yOffset = H - y_min * yScale - margin * H;
// draw scale
dc.SetPen(*wxWHITE_PEN);
dc.SetTextForeground(*wxWHITE);
dc.SetFont(*wxSMALL_FONT);
// ... horizontal
vector<double> minors;
vector<double> tics = scale_tics_step(x_min, x_max, 4, minors);
for (vector<double>::const_iterator i = tics.begin(); i != tics.end(); ++i){
int X = getX(*i);
wxString label = format_label(*i, x_max - x_min);
wxCoord tw, th;
dc.GetTextExtent (label, &tw, &th);
int Y = dc.DeviceToLogicalY(H - th - 2);
dc.DrawText (label, X - tw/2, Y + 1);
dc.DrawLine (X, Y, X, Y - 4);
}
// ... vertical
tics = scale_tics_step(y_min, y_max, 4, minors);
for (vector<double>::const_iterator i = tics.begin(); i != tics.end(); ++i){
int Y = getY(*i);
wxString label = format_label(*i, y_max - y_min);
wxCoord tw, th;
dc.GetTextExtent (label, &tw, &th);
dc.DrawText (label, dc.DeviceToLogicalX(5), Y - th/2);
dc.DrawLine (dc.DeviceToLogicalX(0), Y, dc.DeviceToLogicalX(4), Y);
}
}
void PlotWithTics::draw_axis_labels(wxDC& dc, string const& x_name,
string const& y_name)
{
int W = dc.GetSize().GetWidth();
int H = dc.GetSize().GetHeight();
if (!x_name.empty()) {
wxCoord tw, th;
dc.GetTextExtent(x_name, &tw, &th);
dc.DrawText(x_name, (W - tw)/2, 2);
}
if (!y_name.empty()) {
wxCoord tw, th;
dc.GetTextExtent(y_name, &tw, &th);
dc.DrawRotatedText(y_name, W - 2, (H - tw)/2, 270);
}
}
vector<double> scale_tics_step (double beg, double end, int max_tics,
vector<double> &minors, bool logarithm)
{
vector<double> result;
minors.clear();
if (beg >= end || max_tics <= 0)
return result;
if (logarithm) {
if (beg <= 0)
beg = 1e-1;
if (end <= beg)
end = 2*beg;
double min_logstep = (log10(end/beg)) / max_tics;
bool with_2_5 = (min_logstep < log10(2.));
double logstep = ceil(min_logstep);
double step0 = pow(10, logstep * ceil(log10(beg) / logstep));
for (int i = 2; i <= 9; ++i) {
double v = step0/10. * i;
if (v > beg && v < end) {
if (with_2_5 && (i == 2 || i == 5))
result.push_back(v);
else
minors.push_back(v);
}
}
for (double t = step0; t < end; t *= pow(10,logstep)) {
result.push_back(t);
for (int i = 2; i <= 9; ++i) {
double v = t * i;
if (v > beg && v < end) {
if (with_2_5 && (i == 2 || i == 5))
result.push_back(v);
else
minors.push_back(v);
}
}
}
} else { // !logarithm
double min_step = (end - beg) / max_tics;
double s = pow(10, floor (log10 (min_step)));
int minor_div = 5; //ratio of major-step to minor-step
// now s <= min_step
if (s >= min_step)
;
else if (s * 2 >= min_step) {
s *= 2;
minor_div = 2;
} else if (s * 2.5 >= min_step)
s *= 2.5;
else if (s * 5 >= min_step)
s *= 5;
else
s *= 10;
for (double t = s * floor(beg / s); t < end; t += s) {
if (t > beg) {
// make sure that 0 is not displayed as e.g. -2.7893e-17
if (fabs(t) < 1e-6 * fabs(min_step))
t = 0.;
result.push_back(t);
}
for (int i = 1; i < minor_div; ++i) {
double v = t + s * i / minor_div;
if (v > beg && v < end)
minors.push_back(v);
}
}
}
return result;
}
|