⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plot.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Name:        plot.cpp
// Purpose:     wxPlotWindow
// Author:      Robert Roebling
// Modified by:
// Created:     12/01/2000
// RCS-ID:      $Id: plot.cpp,v 1.15 2005/12/17 19:23:04 VZ Exp $
// Copyright:   (c) Robert Roebling
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/object.h"
#include "wx/font.h"
#include "wx/colour.h"
#include "wx/settings.h"
#include "wx/sizer.h"
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/dcclient.h"
#include "wx/stattext.h"
#endif

#include "wx/plot/plot.h"
#include "wx/bmpbuttn.h"
#include "wx/module.h"

#include <math.h>

// ----------------------------------------------------------------------------
// XPMs
// ----------------------------------------------------------------------------

#if !defined(__WXMSW__) && !defined(__WXPM__)
    #include "wx/plot/plot_enl.xpm"
    #include "wx/plot/plot_shr.xpm"
    #include "wx/plot/plot_zin.xpm"
    #include "wx/plot/plot_zot.xpm"
    #include "wx/plot/plot_up.xpm"
    #include "wx/plot/plot_dwn.xpm"
#endif

//----------------------------------------------------------------------------
// event types
//----------------------------------------------------------------------------

DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGING)
DEFINE_EVENT_TYPE(wxEVT_PLOT_SEL_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_CLICKED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_DOUBLECLICKED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_IN)
DEFINE_EVENT_TYPE(wxEVT_PLOT_ZOOM_OUT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATING)
DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CREATED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGING)
DEFINE_EVENT_TYPE(wxEVT_PLOT_VALUE_SEL_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATING)
DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CREATED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGING)
DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_SEL_CHANGED)
DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_X_LABEL_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_END_X_LABEL_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_Y_LABEL_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_END_Y_LABEL_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_BEGIN_TITLE_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_END_TITLE_EDIT)
DEFINE_EVENT_TYPE(wxEVT_PLOT_AREA_CREATE)

//----------------------------------------------------------------------------
// accessor functions for the bitmaps (may return NULL, check for it!)
//----------------------------------------------------------------------------

static wxBitmap *GetEnlargeBitmap();
static wxBitmap *GetShrinkBitmap();
static wxBitmap *GetZoomInBitmap();
static wxBitmap *GetZoomOutBitmap();
static wxBitmap *GetUpBitmap();
static wxBitmap *GetDownBitmap();

//-----------------------------------------------------------------------------
// consts
//-----------------------------------------------------------------------------

#define wxPLOT_SCROLL_STEP  30

//-----------------------------------------------------------------------------
// wxPlotEvent
//-----------------------------------------------------------------------------

wxPlotEvent::wxPlotEvent( wxEventType commandType, int id )
    : wxNotifyEvent( commandType, id )
{
    m_curve = (wxPlotCurve*) NULL;
    m_zoom = 1.0;
    m_position = 0;
}

//-----------------------------------------------------------------------------
// wxPlotCurve
//-----------------------------------------------------------------------------

IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve, wxObject)

wxPlotCurve::wxPlotCurve( int offsetY, double startY, double endY )
: m_penNormal(*wxGREY_PEN), m_penSelected(*wxBLACK_PEN)
{
    m_offsetY = offsetY;
    m_startY = startY;
    m_endY = endY;
}

//-----------------------------------------------------------------------------
// wxPlotOnOffCurve
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(wxPlotOnOffCurve, wxObject)

#include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY(wxArrayPlotOnOff);

wxPlotOnOffCurve::wxPlotOnOffCurve( int offsetY )
{
    m_offsetY = offsetY;
    m_minX = -1;
    m_maxX = -1;
}

void wxPlotOnOffCurve::Add( wxInt32 on, wxInt32 off, void *clientData )
{
    wxASSERT_MSG( on > 0, _T("plot index < 0") );
    wxASSERT( on <= off );

    if (m_minX == -1)
        m_minX = on;
    if (off > m_maxX)
        m_maxX = off;

    wxPlotOnOff *v = new wxPlotOnOff;
    v->m_on = on;
    v->m_off = off;
    v->m_clientData = clientData;
    m_marks.Add( v );
}

size_t wxPlotOnOffCurve::GetCount()
{
    return m_marks.GetCount();
}

wxInt32 wxPlotOnOffCurve::GetOn( size_t index )
{
    wxPlotOnOff *v = &m_marks.Item( index );
    return v->m_on;
}

wxInt32 wxPlotOnOffCurve::GetOff( size_t index )
{
    wxPlotOnOff *v = &m_marks.Item( index );
    return v->m_off;
}

void* wxPlotOnOffCurve::GetClientData( size_t index )
{
    wxPlotOnOff *v = &m_marks.Item( index );
    return v->m_clientData;
}

wxPlotOnOff *wxPlotOnOffCurve::GetAt( size_t index )
{
    return &m_marks.Item( index );
}

void wxPlotOnOffCurve::DrawOnLine( wxDC &dc, wxCoord y, wxCoord start, wxCoord end, void *WXUNUSED(clientData) )
{
    dc.DrawLine( start, y, start, y-30 );
    dc.DrawLine( start, y-30, end, y-30 );
    dc.DrawLine( end, y-30, end, y );
}

void wxPlotOnOffCurve::DrawOffLine( wxDC &dc, wxCoord y, wxCoord start, wxCoord end )
{
    dc.DrawLine( start, y, end, y );
}

//-----------------------------------------------------------------------------
// wxPlotArea
//-----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxPlotArea, wxWindow)

BEGIN_EVENT_TABLE(wxPlotArea, wxWindow)
    EVT_PAINT(        wxPlotArea::OnPaint)
    EVT_LEFT_DOWN(    wxPlotArea::OnMouse)
    EVT_LEFT_DCLICK(  wxPlotArea::OnMouse)
END_EVENT_TABLE()

wxPlotArea::wxPlotArea( wxPlotWindow *parent )
        : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, _T("plotarea") )
{
    m_owner = parent;

    m_zooming = false;

    SetBackgroundColour( *wxWHITE );
}

void wxPlotArea::OnMouse( wxMouseEvent &event )
{
    int client_width;
    int client_height;
    GetClientSize( &client_width, &client_height);
    int view_x;
    int view_y;
    m_owner->GetViewStart( &view_x, &view_y );
    view_x *= wxPLOT_SCROLL_STEP;
    view_y *= wxPLOT_SCROLL_STEP;

    wxCoord x = event.GetX();
    wxCoord y = event.GetY();
    x += view_x;
    y += view_y;

    wxList::compatibility_iterator node = m_owner->m_curves.GetFirst();
    while (node)
    {
        wxPlotCurve *curve = (wxPlotCurve*)node->GetData();

        double double_client_height = (double)client_height;
        double range = curve->GetEndY() - curve->GetStartY();
        double end = curve->GetEndY();
        wxCoord offset_y = curve->GetOffsetY();

        double dy = (end - curve->GetY( (wxInt32)(x/m_owner->GetZoom()) )) / range;
        wxCoord curve_y = (wxCoord)(dy * double_client_height) - offset_y - 1;

        if ((y-curve_y < 4) && (y-curve_y > -4))
        {
            wxPlotEvent event1( event.ButtonDClick() ? wxEVT_PLOT_DOUBLECLICKED : wxEVT_PLOT_CLICKED, m_owner->GetId() );
            event1.SetEventObject( m_owner );
            event1.SetZoom( m_owner->GetZoom() );
            event1.SetCurve( curve );
            event1.SetPosition( (int)floor(x/m_owner->GetZoom()) );
            m_owner->GetEventHandler()->ProcessEvent( event1 );

            if (curve != m_owner->GetCurrentCurve())
            {
                wxPlotEvent event2( wxEVT_PLOT_SEL_CHANGING, m_owner->GetId() );
                event2.SetEventObject( m_owner );
                event2.SetZoom( m_owner->GetZoom() );
                event2.SetCurve( curve );
                if (!m_owner->GetEventHandler()->ProcessEvent( event2 ) || event2.IsAllowed())
                {
                    m_owner->SetCurrentCurve( curve );
                }
            }
            return;
        }

        node = node->GetNext();
    }
}

void wxPlotArea::DeleteCurve( wxPlotCurve *curve, int from, int to )
{
    wxClientDC dc(this);
    m_owner->PrepareDC( dc );
    dc.SetPen( *wxWHITE_PEN );
    DrawCurve( &dc, curve, from, to );
}

void wxPlotArea::DrawCurve( wxDC *dc, wxPlotCurve *curve, int from, int to )
{
    int view_x;
    int view_y;
    m_owner->GetViewStart( &view_x, &view_y );
    view_x *= wxPLOT_SCROLL_STEP;

    if (from == -1)
        from = view_x;

    int client_width;
    int client_height;
    GetClientSize( &client_width, &client_height);

    if (to == -1)
        to = view_x + client_width;

    double zoom = m_owner->GetZoom();

    int start_x = wxMax( from, (int)floor(curve->GetStartX()*zoom) );
    int end_x = wxMin( to, (int)floor(curve->GetEndX()*zoom) );

    start_x = wxMax( view_x, start_x );
    end_x = wxMin( view_x + client_width, end_x );

    end_x++;

    double double_client_height = (double)client_height;
    double range = curve->GetEndY() - curve->GetStartY();
    double end = curve->GetEndY();
    wxCoord offset_y = curve->GetOffsetY();

    wxCoord last_y=0;
    for (int x = start_x; x < end_x; x++)
    {
        double dy = (end - curve->GetY( (wxInt32)(x/zoom) )) / range;
        wxCoord y = (wxCoord)(dy * double_client_height) - offset_y - 1;

        if (x != start_x)
           dc->DrawLine( x-1, last_y, x, y );

        last_y = y;
    }
}

void wxPlotArea::DrawOnOffCurve( wxDC *dc, wxPlotOnOffCurve *curve, int from, int to )
{
    int view_x;
    int view_y;
    m_owner->GetViewStart( &view_x, &view_y );
    view_x *= wxPLOT_SCROLL_STEP;

    if (from == -1)
        from = view_x;

    int client_width;
    int client_height;
    GetClientSize( &client_width, &client_height);

    if (to == -1)
        to = view_x + client_width;

    double zoom = m_owner->GetZoom();

    int start_x = wxMax( from, (int)floor(curve->GetStartX()*zoom) );
    int end_x = wxMin( to, (int)floor(curve->GetEndX()*zoom) );

    start_x = wxMax( view_x, start_x );
    end_x = wxMin( view_x + client_width, end_x );

    end_x++;

    wxCoord offset_y = curve->GetOffsetY();
    wxCoord last_off = -5;

    if (curve->GetCount() == 0)
        return;

    for (size_t index = 0; index < curve->GetCount(); index++)
    {
        wxPlotOnOff *p = curve->GetAt( index );

        wxCoord on = (wxCoord)(p->m_on*zoom);
        wxCoord off = (wxCoord)(p->m_off*zoom);

        if (end_x < on)
        {
            curve->DrawOffLine( *dc, client_height-offset_y, last_off, on );
            break;
        }

        if (off >= start_x)
        {
            curve->DrawOffLine( *dc, client_height-offset_y, last_off, on );
            curve->DrawOnLine( *dc, client_height-offset_y, on, off, p->m_clientData );
        }
        last_off = off;
    }

    wxPlotOnOff *p = curve->GetAt( curve->GetCount()-1 );
    wxCoord off = (wxCoord)(p->m_off*zoom);
    if (off < end_x)
        curve->DrawOffLine( *dc, client_height-offset_y, off, to );
}

void wxPlotArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
{
    int view_x;
    int view_y;
    m_owner->GetViewStart( &view_x, &view_y );
    view_x *= wxPLOT_SCROLL_STEP;
    view_y *= wxPLOT_SCROLL_STEP;

    wxPaintDC dc( this );
    m_owner->PrepareDC( dc );

    wxRegionIterator upd( GetUpdateRegion() );

    while (upd)
    {
        int update_x = upd.GetX() + view_x;
        int update_width = upd.GetWidth();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -