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

📄 plot.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 3 页
字号:

/*
        if (m_owner->m_current)
        {
            dc.SetPen( *wxLIGHT_GREY_PEN );
            int base_line = client_height - m_owner->m_current->GetOffsetY();
            dc.DrawLine( update_x-1, base_line-1, update_x+update_width+2, base_line-1 );
        }
*/

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

            if (curve == m_owner->GetCurrentCurve())
                dc.SetPen( curve->GetPenSelected() );
            else
                dc.SetPen( curve->GetPenNormal() );

            DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 );

            node = node->GetNext();
        }

        dc.SetPen( *wxRED_PEN );

        node = m_owner->m_onOffCurves.GetFirst();
        while (node)
        {
            wxPlotOnOffCurve *curve = (wxPlotOnOffCurve*) node->GetData();

            DrawOnOffCurve( &dc, curve, update_x-1, update_x+update_width+2 );

            node = node->GetNext();
        }

        upd ++;
    }
}

void wxPlotArea::ScrollWindow( int dx, int dy, const wxRect *rect )
{
    wxWindow::ScrollWindow( dx, dy, rect );
//    m_owner->m_xaxis->ScrollWindow( dx, 0 );
}

//-----------------------------------------------------------------------------
// wxPlotXAxisArea
//-----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxPlotXAxisArea, wxWindow)

BEGIN_EVENT_TABLE(wxPlotXAxisArea, wxWindow)
  EVT_PAINT(        wxPlotXAxisArea::OnPaint)
  EVT_LEFT_DOWN(    wxPlotXAxisArea::OnMouse)
END_EVENT_TABLE()

wxPlotXAxisArea::wxPlotXAxisArea( wxPlotWindow *parent )
        : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,40), 0, _T("plotxaxisarea") )
{
    m_owner = parent;

    SetBackgroundColour( *wxWHITE );
    SetFont( *wxSMALL_FONT );
}

void wxPlotXAxisArea::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() + view_x;
    wxCoord y = event.GetY() + view_y;

    /* TO DO: do something here */
    wxUnusedVar(x);
    wxUnusedVar(y);
}

void wxPlotXAxisArea::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 );

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

    double zoom = m_owner->GetZoom();

    double ups = m_owner->GetUnitsPerValue() / zoom;

    double start = view_x * ups;
    double end = (view_x + client_width) * ups;
    double range = end - start;

    int int_log_range = (int)floor( log10( range ) );
    double step = 1.0;
    if (int_log_range > 0)
    {
        for (int i = 0; i < int_log_range; i++)
           step *= 10;
    }
    if (int_log_range < 0)
    {
        for (int i = 0; i < -int_log_range; i++)
           step /= 10;
    }
    double lower = ceil(start / step) * step;
    double upper = floor(end / step) * step;

    // if too few values, shrink size
    if ((range/step) < 4)
    {
        step /= 2;
        if (lower-step > start) lower -= step;
        if (upper+step < end) upper += step;
    }

    // if still too few, again
    if ((range/step) < 4)
    {
        step /= 2;
        if (lower-step > start) lower -= step;
        if (upper+step < end) upper += step;
    }

    dc.SetBrush( *wxWHITE_BRUSH );
    dc.SetPen( *wxTRANSPARENT_PEN );
    dc.DrawRectangle( 4, 5, client_width-14, 10 );
    dc.DrawRectangle( 0, 20, client_width, 20 );
    dc.SetPen( *wxBLACK_PEN );

    double current = lower;
    while (current < upper+(step/2))
    {
        int x = (int)ceil((current-start) / range * (double)client_width) - 1;
        if ((x > 4) && (x < client_width-25))
        {
            dc.DrawLine( x, 5, x, 15 );
            wxString label;
            if (range < 50)
            {
                label.Printf( _T("%f"), current );
                while (label.Last() == _T('0'))
                    label.RemoveLast();
                if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
                    label.Append( _T('0') );
            }
            else
                label.Printf( _T("%d"), (int)floor(current) );
            dc.DrawText( label, x-4, 20 );
        }

        current += step;
    }

    dc.DrawLine( 0, 15, client_width-8, 15 );
    dc.DrawLine( client_width-4, 15, client_width-10, 10 );
    dc.DrawLine( client_width-4, 15, client_width-10, 20 );
}

//-----------------------------------------------------------------------------
// wxPlotYAxisArea
//-----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxPlotYAxisArea, wxWindow)

BEGIN_EVENT_TABLE(wxPlotYAxisArea, wxWindow)
  EVT_PAINT(        wxPlotYAxisArea::OnPaint)
  EVT_LEFT_DOWN(    wxPlotYAxisArea::OnMouse)
END_EVENT_TABLE()

wxPlotYAxisArea::wxPlotYAxisArea( wxPlotWindow *parent )
        : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord), 0, _T("plotyaxisarea") )
{
    m_owner = parent;

    SetBackgroundColour( *wxWHITE );
    SetFont( *wxSMALL_FONT );
}

void wxPlotYAxisArea::OnMouse( wxMouseEvent &WXUNUSED(event) )
{
    /* do something here */
}

void wxPlotYAxisArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
{
    wxPaintDC dc( this );

    wxPlotCurve *curve = m_owner->GetCurrentCurve();

    if (!curve) return;

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


    double range = curve->GetEndY() - curve->GetStartY();
    double offset = ((double) curve->GetOffsetY() / (double)client_height ) * range;
    double start = curve->GetStartY() - offset;
    double end = curve->GetEndY() - offset;

    int int_log_range = (int)floor( log10( range ) );
    double step = 1.0;
    if (int_log_range > 0)
    {
        for (int i = 0; i < int_log_range; i++)
           step *= 10;
    }
    if (int_log_range < 0)
    {
        for (int i = 0; i < -int_log_range; i++)
           step /= 10;
    }
    double lower = ceil(start / step) * step;
    double upper = floor(end / step) * step;

    // if too few values, shrink size
    if ((range/step) < 4)
    {
        step /= 2;
        if (lower-step > start) lower -= step;
        if (upper+step < end) upper += step;
    }

    // if still too few, again
    if ((range/step) < 4)
    {
        step /= 2;
        if (lower-step > start) lower -= step;
        if (upper+step < end) upper += step;
    }

    dc.SetPen( *wxBLACK_PEN );

    double current = lower;
    while (current < upper+(step/2))
    {
        int y = (int)((curve->GetEndY()-current) / range * (double)client_height) - 1;
        y -= curve->GetOffsetY();
        if ((y > 10) && (y < client_height-7))
        {
            dc.DrawLine( client_width-15, y, client_width-7, y );
            wxString label;
            if (range < 50)
            {
                label.Printf( _T("%f"), current );
                while (label.Last() == _T('0'))
                    label.RemoveLast();
                if ((label.Last() == _T('.')) || (label.Last() == _T(',')))
                    label.Append( _T('0') );
            }
            else
                label.Printf( _T("%d"), (int)floor(current) );
            dc.DrawText( label, 5, y-7 );
        }

        current += step;
    }

    dc.DrawLine( client_width-15, 6, client_width-15, client_height );
    dc.DrawLine( client_width-15, 2, client_width-20, 8 );
    dc.DrawLine( client_width-15, 2, client_width-10, 8 );
}

//-----------------------------------------------------------------------------
// wxPlotWindow
//-----------------------------------------------------------------------------

#define  ID_ENLARGE       1000
#define  ID_SHRINK        1002

#define  ID_MOVE_UP       1006
#define  ID_MOVE_DOWN     1007

#define  ID_ZOOM_IN       1010
#define  ID_ZOOM_OUT      1011


IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow)

BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow)
    EVT_BUTTON(  ID_MOVE_UP,     wxPlotWindow::OnMoveUp)
    EVT_BUTTON(  ID_MOVE_DOWN,   wxPlotWindow::OnMoveDown)

    EVT_BUTTON(  ID_ENLARGE,  wxPlotWindow::OnEnlarge)
    EVT_BUTTON(  ID_SHRINK,   wxPlotWindow::OnShrink)

    EVT_BUTTON(  ID_ZOOM_IN,     wxPlotWindow::OnZoomIn)
    EVT_BUTTON(  ID_ZOOM_OUT,    wxPlotWindow::OnZoomOut)

    EVT_SCROLLWIN( wxPlotWindow::OnScroll2)
END_EVENT_TABLE()

wxPlotWindow::wxPlotWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
        : wxScrolledWindow( parent, id, pos, size, flag, _T("plotcanvas") ),
          m_titleStaticText( NULL )
{
    m_xUnitsPerValue = 1.0;
    m_xZoom = 1.0;

    m_enlargeAroundWindowCentre = false;
    m_scrollOnThumbRelease = false;

    m_area = new wxPlotArea( this );
    wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL );

    if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ALL) != 0)
    {
        wxBoxSizer *buttonlist = new wxBoxSizer( wxVERTICAL );
        if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ENLARGE) != 0)
        {
            buttonlist->Add( new wxBitmapButton( this, ID_ENLARGE, *GetEnlargeBitmap() ), 0, wxEXPAND|wxALL, 2 );
            buttonlist->Add( new wxBitmapButton( this, ID_SHRINK, *GetShrinkBitmap() ), 0, wxEXPAND|wxALL, 2 );
            buttonlist->Add( 20,10, 0 );
        }
        if ((GetWindowStyleFlag() & wxPLOT_BUTTON_MOVE) != 0)
        {
            buttonlist->Add( new wxBitmapButton( this, ID_MOVE_UP, *GetUpBitmap() ), 0, wxEXPAND|wxALL, 2 );
            buttonlist->Add( new wxBitmapButton( this, ID_MOVE_DOWN, *GetDownBitmap() ), 0, wxEXPAND|wxALL, 2 );
            buttonlist->Add( 20,10, 0 );
        }
        if ((GetWindowStyleFlag() & wxPLOT_BUTTON_ZOOM) != 0)
        {
            buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_IN, *GetZoomInBitmap() ), 0, wxEXPAND|wxALL, 2 );
            buttonlist->Add( new wxBitmapButton( this, ID_ZOOM_OUT, *GetZoomOutBitmap() ), 0, wxEXPAND|wxALL, 2 );
        }
        mainsizer->Add( buttonlist, 0, wxEXPAND|wxALL, 4 );
    }

    wxBoxSizer *plotsizer = new wxBoxSizer( wxHORIZONTAL );

    //Add sizer to hold the title and plot.
    //Title to be added later.
    m_plotAndTitleSizer = new wxBoxSizer( wxVERTICAL );
    m_plotAndTitleSizer->Add( plotsizer, 1, wxEXPAND | wxTOP, 10 );

    if ((GetWindowStyleFlag() & wxPLOT_Y_AXIS) != 0)
    {
        m_yaxis = new wxPlotYAxisArea( this );

        wxBoxSizer *vert1 = new wxBoxSizer( wxVERTICAL );
        plotsizer->Add( vert1, 1, wxEXPAND|wxTOP,10 );
        vert1->Add( m_yaxis, 1 );
        if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
            vert1->Add( 60, 40 );
    }
    else
    {
        m_yaxis = (wxPlotYAxisArea*) NULL;
    }

    if ((GetWindowStyleFlag() & wxPLOT_X_AXIS) != 0)
    {
        m_xaxis = new wxPlotXAxisArea( this );

        wxBoxSizer *vert2 = new wxBoxSizer( wxVERTICAL );
        plotsizer->Add( vert2, 5, wxEXPAND);
        vert2->Add( m_area, 1, wxEXPAND|wxTOP,10 );
        vert2->Add( m_xaxis, 0, wxEXPAND );
    }
    else
    {
        plotsizer->Add( m_area, 1, wxEXPAND );
        m_xaxis = (wxPlotXAxisArea*) NULL;
    }

    mainsizer->Add( m_plotAndTitleSizer, 1, wxEXPAND );

    SetSizerAndFit( mainsizer );

    SetTargetWindow( m_area );

    SetBackgroundColour( *wxWHITE );

    m_current = (wxPlotCurve*) NULL;
}

wxPlotWindow::~wxPlotWindow()
{
}

void wxPlotWindow::Add( wxPlotCurve *curve )
{

⌨️ 快捷键说明

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