view src/rsearcher.cpp @ 1:eaa27e4ed5be

add client_ui.go
author pyon@macmini
date Mon, 01 Oct 2018 23:18:29 +0900
parents d3b8cd5aeb70
children 7fe3417cefc8
line wrap: on
line source

// Filename   : rsearcher.cpp
// Last Change: 2018-10-01 Mon 23:15:33.
//

#include "rsearcher.h"
#include "main.h"

/********************/
/** MySearchCtrl   **/
/********************/
MySearchCtrl::MySearchCtrl( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style )
    : wxSearchCtrl( parent, id, value, pos, size, style )
{
	m_parent = parent;
    SetMaxLength( 10 );
	#ifndef __WXMAC__
	ShowSearchButton( true );
	#endif
	ShowCancelButton( false );

    wxFont font( 12, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
    SetFont( font );
}
 
MySearchCtrl::~MySearchCtrl()
{
}

// Event Table
BEGIN_EVENT_TABLE( MySearchCtrl, wxSearchCtrl )
    EVT_CHAR( MySearchCtrl::OnKey )
END_EVENT_TABLE()

// Event Handlers & Functions
void MySearchCtrl::OnKey( wxKeyEvent& event )
{
    int kc = event.GetKeyCode();

    if ( kc == 13 ) {	// Enter
		// select all
		wxString s = GetValue();

		MainFrame* f = (MainFrame*)FindWindowById( ID_MAIN );
		f->Cmd( s );

        wxMessageBox( "Enter ed" );
        event.Skip();
        return;
    }

    if (  kc == 45 ) {   // Num-Key '-' as Backspace
        wxString t = GetStringSelection();
        if ( t.IsEmpty() ) {
            long p = GetInsertionPoint();
            if ( p > 0 ) Remove( p - 1, p );
        }
        else {
            Cut();
        }
        return;
    }

    if ( kc == WXK_ESCAPE ) {    // clear by ESC
        this->Clear();
        return;
    }

    event.Skip();
}

/********************/
/** MyStaticBitmap **/
/********************/
MyStaticBitmap::MyStaticBitmap( wxScrolledWindow *parent, wxWindowID id, const wxBitmap &label, const wxPoint &pos, const wxSize &size, long style, const wxString &name )
    : wxStaticBitmap( parent, id, label, pos, size, style, name )
{
	m_parent = parent;
    Connect( wxEVT_LEFT_DOWN,  wxMouseEventHandler( MyStaticBitmap::OnLeftDown ), NULL, this );
    Connect( wxEVT_LEFT_UP,    wxMouseEventHandler( MyStaticBitmap::OnLeftUp   ), NULL, this );
    Connect( wxEVT_MOTION,     wxMouseEventHandler( MyStaticBitmap::OnMotion   ), NULL, this );
    Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( MyStaticBitmap::OnWheel    ), NULL, this );
}

MyStaticBitmap::~MyStaticBitmap()
{
    Disconnect( wxEVT_LEFT_DOWN,  wxMouseEventHandler( MyStaticBitmap::OnLeftDown ), NULL, this );
    Disconnect( wxEVT_LEFT_UP,    wxMouseEventHandler( MyStaticBitmap::OnLeftUp   ), NULL, this );
    Disconnect( wxEVT_MOTION,     wxMouseEventHandler( MyStaticBitmap::OnMotion   ), NULL, this );
    Disconnect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( MyStaticBitmap::OnWheel    ), NULL, this );
}

// Event Handlers
void MyStaticBitmap::OnWheel( wxMouseEvent& event )
{
    if ( event.ControlDown() ) {
        if ( event.GetWheelRotation() < 0 ) {
            if ( m_zoom < 4 ) m_zoom++;
        }
        else {
            if ( m_zoom > 0 ) m_zoom--;
        }
        SetBitmap( m_bmp[ m_zoom ] );
        m_parent->SetScrollbars( 10, 10, m_bmp[ m_zoom ].GetWidth() / 10, m_bmp[ m_zoom ].GetHeight() / 10 );
        return;
    }
    event.Skip();
}

void MyStaticBitmap::OnLeftDown( wxMouseEvent& event )
{
    event.GetPosition( &m_dragx, &m_dragy );
    SetCursor( wxCursor( wxCURSOR_SIZING ) );
}

void MyStaticBitmap::OnLeftUp( wxMouseEvent& WXUNUSED(event) )
{
    SetCursor( wxCursor( wxCURSOR_ARROW ) );
}

void MyStaticBitmap::OnMotion( wxMouseEvent& event )
{
    if ( event.Dragging() ) {
        int xv, yv, x, y;
        m_parent->GetViewStart( &xv, &yv );

        event.GetPosition( &x, &y );

        int xa = abs( x - m_dragx );
        int ya = abs( y - m_dragy );
        int xs = x - m_dragx < 0 ? -1 : 1;
        int ys = y - m_dragy < 0 ? -1 : 1;

        /* handai dakedo sumu-zu
        m_parent->Scroll( xv + xs * log10( xa + 1 ), yv + ys * log10( ya + 1 ) );
        */
        m_parent->Scroll( xv + xs * log10( xa + 1 ), yv + ys * log10( ya + 1 ) );

        m_dragx = x; m_dragy = y;
    }
}

// Functions

/********************/
/** Main Frame     **/
/********************/
MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) 
	: wxFrame( parent, id, title, pos, size, style )
{
	CreateControls();
}

MainFrame::~MainFrame()
{
}

// Event Table
BEGIN_EVENT_TABLE( MainFrame, wxFrame )
    EVT_DATAVIEW_ITEM_ACTIVATED( ID_LIST, MainFrame::OnItemDClicked )
    EVT_NOTEBOOK_PAGE_CHANGED( ID_NBOOK, MainFrame::OnNBookChanged )
	EVT_BUTTON( ID_TEST, MainFrame::OnTestButton )
	EVT_CLOSE( MainFrame::SaveConfig )
    //EVT_IDLE( MainFrame::OnIdle )
END_EVENT_TABLE()


// Event Handler
void MainFrame::OnItemDClicked( wxDataViewEvent& WXUNUSED(event) )
{
    wxMessageBox( "dcli" );
    int r = m_dataViewListCtrl->GetSelectedRow();
    wxString no = m_dataViewListCtrl->GetTextValue( r, 0 );

    LoadBitmaps();
}

void MainFrame::OnNBookChanged( wxBookCtrlEvent& WXUNUSED(event) )
{
    for ( int i = 0; i < m_notebook->GetPageCount(); i++ ) {
        m_notebook->SetPageImage( i, 1 );
    }
    m_notebook->SetPageImage( m_notebook->GetSelection(), 0 );
}

/*
void MainFrame::OnItemSelected( wxDataViewEvent& event )
{
    dclick or select ?
}
*/


/*
void MainFrame::OnIdle( wxIdleEvent& WXUNUSED(event) )
{
}

*/
void MainFrame::SaveConfig( wxCloseEvent& WXUNUSED(event) )
{
    if ( !IsIconized() && !IsMaximized() ) {
        wxGetApp().rect = this->GetRect();
    }
    Destroy();
}

void MainFrame::OnTestButton( wxCommandEvent& WXUNUSED(event) )
{
    /* ok
    Cmd( m_searchCtrl->GetValue() );
    Cmd( wxT( "0100012345" ) );
    */

    wxBitmap bmp( wxT(".cache/01_1"), wxBITMAP_TYPE_JPEG );
    int width  = bmp.GetWidth();
    int height = bmp.GetHeight();
    wxImage img = bmp.ConvertToImage();

    int ww, wh;
    m_scrolledWindow6->GetSize( &ww, &wh );

    float w = ww;
    float h = w * height / width;
    m_staticBitmap6->SetBitmap( wxBitmap( img.Scale( w, h, wxIMAGE_QUALITY_HIGH ) ) );
    m_scrolledWindow6->SetScrollbars( 10, 10, w / 10, h / 10 );

    for ( int i = 0; i < 5; i++ ) {
        w *= 1.1;
        h *= 1.1;
        //m_staticBitmap6->SetImage( i, wxBitmap( img.Scale( w, h, wxIMAGE_QUALITY_HIGH ) ) );
    }
}

// Functions
void MainFrame::CreateControls( void )
{
    this->SetIcon( wxIcon( wxT( "sample" ) ) );
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
	//this->SetBackgroundColour( wxColour( 0, 150, 230 ) );

	wxBoxSizer* bSizerTop = new wxBoxSizer( wxHORIZONTAL );
	
    // Left
	wxImageList* imgList = new wxImageList( 16, 16, false, 1 );
    wxBitmap bmp( wxT( "image/blue.png" ), wxBITMAP_TYPE_PNG );
	imgList->Add( bmp, wxNullBitmap );
    bmp.LoadFile( wxT( "image/water.png" ), wxBITMAP_TYPE_PNG );
	imgList->Add( bmp, wxNullBitmap );

	m_notebook = new wxNotebook( this, ID_NBOOK, wxDefaultPosition, wxDefaultSize, 0 );
	m_notebook->SetImageList( imgList );

	m_scrolledWindow1 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow1->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow1, wxT( "Image-01" ), false, 0 );

	m_scrolledWindow2 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow2->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow2, wxT( "Image-02" ), false, 0 );

	m_scrolledWindow3 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow3->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow3, wxT( "Image-03" ), false, 0 );

	m_scrolledWindow4 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow4->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow4, wxT( "Image-04" ), false, 0 );

	m_scrolledWindow5 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow5->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow5, wxT( "Image-05" ), false, 0 );
	
	m_scrolledWindow6 = new wxScrolledWindow( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHSCROLL|wxVSCROLL );
	m_scrolledWindow6->SetScrollRate( 5, 5 );
	m_notebook->AddPage( m_scrolledWindow6, wxT( "Image-06" ), false, 0 );
	
	bSizerTop->Add( m_notebook, 1, wxEXPAND|wxALL, 5 );
	
    // Right
	wxBoxSizer* bSizerRight = new wxBoxSizer( wxVERTICAL );
	
	m_searchCtrl = new MySearchCtrl( this, ID_SEARCH, wxEmptyString, wxDefaultPosition, wxSize( -1, 24 ), wxTE_PROCESS_ENTER );
	bSizerRight->Add( m_searchCtrl, 0, wxALL, 5 );
	m_searchCtrl->SetFocus();
	
	m_textCtrlName = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 80, -1 ), 0 );
	m_textCtrlName->SetBackgroundColour( wxColour( 180, 210, 240 ) );
	bSizerRight->Add( m_textCtrlName, 0, wxALL, 5 );
	
	m_textCtrlAddr = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 120, -1 ), 0 );
	m_textCtrlAddr->SetBackgroundColour( wxColour( 180, 210, 240 ) );
	bSizerRight->Add( m_textCtrlAddr, 0, wxALL|wxEXPAND, 5 );
	
	m_dataViewListCtrl = new wxDataViewListCtrl( this, ID_LIST, wxDefaultPosition, wxDefaultSize, wxDV_ROW_LINES|wxDV_SINGLE );
	m_dataViewListColumnNo   = m_dataViewListCtrl->AppendTextColumn( wxT( "No" ),   wxDATAVIEW_CELL_INERT,  30, wxALIGN_RIGHT,  wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE );
	m_dataViewListColumnDate = m_dataViewListCtrl->AppendTextColumn( wxT( "Date" ), wxDATAVIEW_CELL_INERT, 120, wxALIGN_CENTER, wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE );

	bSizerRight->Add( m_dataViewListCtrl, 1, wxALL|wxEXPAND, 5 );
	
	m_textCtrlLog = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	bSizerRight->Add( m_textCtrlLog, 1, wxALL|wxEXPAND, 5 );

	m_slider = new wxSlider( this, ID_SLDR, 1, 1, 5, wxDefaultPosition, wxSize( -1,200 ), wxSL_AUTOTICKS|wxSL_INVERSE|wxSL_LABELS|wxSL_VERTICAL );
	bSizerRight->Add( m_slider, 0, wxALL, 5 );
	
	m_buttonPrint = new wxButton( this, ID_PRINT, wxT( "Print" ), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerRight->Add( m_buttonPrint, 0, wxALL, 5 );
	
	m_buttonTest = new wxButton( this, ID_TEST, wxT( "Test" ), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerRight->Add( m_buttonTest, 0, wxALL, 5 );
	
	bSizerTop->Add( bSizerRight, 0, wxEXPAND, 5 );
	
	this->SetSizer( bSizerTop );
	this->Layout();
	
	//this->Centre( wxBOTH );

    m_staticBitmap1 = new MyStaticBitmap( m_scrolledWindow1, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString );
    m_staticBitmap2 = new MyStaticBitmap( m_scrolledWindow2, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString );
    m_staticBitmap3 = new MyStaticBitmap( m_scrolledWindow3, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString );
    m_staticBitmap4 = new MyStaticBitmap( m_scrolledWindow4, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString );
    m_staticBitmap5 = new MyStaticBitmap( m_scrolledWindow5, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, 0, wxEmptyString );
}

void MainFrame::Cmd( wxString cmd )
{
	m_dataViewListCtrl->DeleteAllItems();

    if ( cmd.Cmp( wxT( "q" ) ) == 0 || cmd.Cmp( wxT( "9" ) ) == 0 ) {
        Close();
    }

    if ( cmd.Cmp( wxT( "." ) ) == 0 ) {
        wxString appdir = wxGetCwd();
        wxString execmd = wxT( "explorer " ) + appdir;
        wxExecute( execmd );
        return;
    }

    wxRegEx reHhs( wxT( "^0[1238][0-9]{8}$" ) );
    if ( reHhs.Matches( cmd ) ) {
        wxArrayString output, errors;
        cmd = wxT( "./rsearcher -q " ) + cmd;
        wxExecute( cmd, output, errors, wxEXEC_SYNC, NULL ); // ok

        if ( output.GetCount() > 0 ) {
            m_textCtrlName->SetValue( output[0] );
            m_textCtrlAddr->SetValue( output[2] );
            for ( int i = 5, n = 1; i < output.GetCount(); i++, n++ ) {
                wxVector<wxVariant> data;
                data.push_back( wxString::Format( wxT( "%02d" ), n ) );
                data.push_back( output[i] );
                m_dataViewListCtrl->AppendItem( data );
                data.clear();
            }
        }
    
        if ( errors.GetCount() > 0 ) {
            wxMessageBox( errors[0], wxT( "Error" ) );
        }
    }
}

void MainFrame::LoadBitmap( wxScrolledWindow* sc, wxStaticBitmap* sb, wxString file )
{
    wxBitmap bmp( file, wxBITMAP_TYPE_JPEG );
    int width  = bmp.GetWidth();
    int height = bmp.GetHeight();
    wxImage img = bmp.ConvertToImage();

    int ww, wh;
    sc->GetSize( &ww, &wh );

    float w = ww - 50;
    float h = w * height / width;
    sb->SetBitmap( wxBitmap( img.Scale( w, h, wxIMAGE_QUALITY_HIGH ) ) );
    sc->SetScrollbars( 10, 10, w / 10, h / 10 );

    for ( int i = 0; i < 5; i++ ) {
        w *= 1.1;
        h *= 1.1;
        //sb->SetImage( i, wxBitmap( img.Scale( w, h, wxIMAGE_QUALITY_HIGH ) ) );
    }
}

void MainFrame::LoadBitmaps( void )
{
    int date = 0;
    LoadBitmap( m_scrolledWindow1, m_staticBitmap1, wxString::Format( ".cache/%08d_1", date ) );
    LoadBitmap( m_scrolledWindow2, m_staticBitmap2, wxString::Format( ".cache/%08d_2", date ) );
    LoadBitmap( m_scrolledWindow3, m_staticBitmap3, wxString::Format( ".cache/%08d_3", date ) );
    LoadBitmap( m_scrolledWindow4, m_staticBitmap4, wxString::Format( ".cache/%08d_4", date ) );
    LoadBitmap( m_scrolledWindow5, m_staticBitmap5, wxString::Format( ".cache/%08d_5", date ) );
}

void MainFrame::GetImage( wxString hhs, wxString no )
{
    // http get
}

void MainFrame::InDevelop( bool flag )
{
    if ( !flag ) return;
    LoadBitmaps();
    
	m_slider->Enable( false );
	m_slider->Show( false );

	m_buttonPrint->Enable( false );
	m_buttonPrint->Show( false );

	bool tb = false;
	m_buttonTest->Enable( tb );
	m_buttonTest->Show( tb );

	m_searchCtrl->Enable( false );
}