view src/bprint.cpp @ 12:52958cd4a073

Implement Force Mask button in Batch Print Mode.
author pyon@macmini
date Sun, 18 May 2014 19:49:15 +0900
parents 4967d1e2b30c
children bbd65edf71d4
line wrap: on
line source

// Filename   : bprint.cpp
// Last Change: 16-May-2014.
//

#include "bprint.h"
#include "marksheet.h"
#include "dndfile.h"
#include "db.h"

FrameBatchPrint::FrameBatchPrint( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
    : wxDialog( parent, id, title, pos, size, style )
{
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
    this->SetBackgroundColour( wxColour( wxT("WHEAT") ) );
	
	wxBoxSizer* bSizerTop = new wxBoxSizer( wxHORIZONTAL );
	
	m_grid = new wxGrid( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
	
	// Grid
	m_grid->CreateGrid( 25, 4 );
	m_grid->EnableEditing( true );
	m_grid->EnableGridLines( true );
	m_grid->EnableDragGridSize( false );
	m_grid->SetMargins( 0, 0 );
	
	// Columns
	m_grid->EnableDragColMove( false );
	m_grid->EnableDragColSize( true );
	m_grid->SetColLabelSize( 30 );
	m_grid->SetColLabelValue( 0, wxT("被保険者番号") );
	m_grid->SetColLabelValue( 1, wxT("氏名") );
	m_grid->SetColLabelValue( 2, wxT("最新フォルダ") );
	m_grid->SetColLabelValue( 3, wxT("ステータス") );
	m_grid->SetColLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
    m_grid->SetColSize( 0, 100 );
    m_grid->SetColSize( 1, 100 );
    m_grid->SetColSize( 2, 220 );
    m_grid->SetColSize( 3,  90 );
    m_grid->ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_ALWAYS );
	
	// Rows
	m_grid->EnableDragRowSize( true );
	m_grid->SetRowLabelSize( 30 );
	m_grid->SetRowLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
	
	// Cell Defaults
	m_grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_CENTRE );
	bSizerTop->Add( m_grid, 1, wxALL|wxEXPAND, 5 );
	
	wxBoxSizer* bSizerButton = new wxBoxSizer( wxVERTICAL );
	
	bSizerButton->Add( 0, 20, 0, 0, 5 );

	m_buttonClear = new wxButton( this, ID_BPCLEAR, wxT("クリア"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonClear, 0, wxALL, 5 );
	
	m_buttonMaskPrint = new wxButton( this, ID_BPFFMP, wxT("FFMP"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonMaskPrint, 0, wxALL, 5 );

	m_buttonPrint = new wxButton( this, ID_BPPRINT, wxT("一括印刷"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonPrint, 0, wxALL, 5 );

	m_buttonLdLog = new wxButton( this, ID_BPLDLOG, wxT("一括印刷ログ"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonLdLog, 0, wxALL, 5 );
	
	bSizerButton->Add( 0, 20, 0, 0, 5 );
	
	bSizerButton->Add( 0, 20, 0, 0, 5 );

	m_buttonClose = new wxButton( this, wxID_CANCEL, wxT("閉じる"), wxDefaultPosition, wxDefaultSize, 0 );
	m_buttonClose->SetDefault(); 
	bSizerButton->Add( m_buttonClose, 0, wxALL, 5 );
	
	bSizerTop->Add( bSizerButton, 0, wxEXPAND, 5 );
	
	this->SetSizer( bSizerTop );
	this->Layout();
	
	this->Centre( wxBOTH );

    this->SetDropTarget( new DnDFile( m_grid ) );
    SetGridReadOnly();
}

FrameBatchPrint::~FrameBatchPrint()
{
}

// Event Table
BEGIN_EVENT_TABLE( FrameBatchPrint, wxDialog )
    EVT_GRID_CELL_CHANGING( FrameBatchPrint::OnInput )
    EVT_BUTTON( ID_BPCLEAR, FrameBatchPrint::OnClear )
    EVT_BUTTON( ID_BPFFMP,  FrameBatchPrint::OnMaskPrint )
    EVT_BUTTON( ID_BPPRINT, FrameBatchPrint::OnPrint )
    EVT_BUTTON( ID_BPLDLOG, FrameBatchPrint::OnLoadLog )
END_EVENT_TABLE()

// Event Handlers & Functions
/* 氏名などを表示 */ 
void FrameBatchPrint::OnInput( wxGridEvent& event )
{
    wxString hhsno = event.GetString();
    int r = event.GetRow();
    for ( int c = 1; c < m_grid->GetNumberCols(); c++ )
        m_grid->SetCellValue( r, c, wxEmptyString );

    wxArrayString info = wxSplit( GetHhsInfoByHhsNo( hhsno ), '_', '\\' );
    wxArrayString path = GetPathByHhsNo( hhsno );

    if ( info.IsEmpty() ) info.Add( wxEmptyString );
    if ( path.IsEmpty() ) {
        wxMessageBox( wxT("ファイルがありません.") );
        path.Add( wxEmptyString );
    }

    m_grid->SetCellValue( r, 1, info[0] );
    m_grid->SetCellValue( r, 2, path[0] );    
}

/* グリッドをクリア */
void FrameBatchPrint::OnClear( wxCommandEvent& WXUNUSED(event) )
{
    m_grid->ClearGrid();
}

/* 一括印刷処理 */
void FrameBatchPrint::OnPrint( wxCommandEvent& WXUNUSED(event) )
{
    wxPrintDialogData pd;
    wxPrinter p( &pd );
    p.PrintDialog( NULL );
    wxArrayString log;

    for ( int r = 0; r < m_grid->GetNumberRows(); r++ ) {
        wxString path = m_grid->GetCellValue( r, 2 );

        // 印刷用の html を作成
        if ( path.IsEmpty() ) continue;

        wxDir dir( path );
        if ( !dir.IsOpened() ) return;

        wxString html;
        html = wxT("<html><body>\n");

        wxString file;
        bool cout = dir.GetFirst( &file, wxT("*.jpg"), wxDIR_FILES );
        bool notyet_mask = true;
        int n = 0;
        wxString tmpdir = wxGetCwd() + wxFILE_SEP_PATH + wxT("tmp") + wxFILE_SEP_PATH;
        while ( cout ) {
            file = path + wxFILE_SEP_PATH + file;
            file.Replace( wxFILE_SEP_PATH, wxT("/") );
            wxString tmpjpg = wxString::Format( wxT("%stmp%d.jpg"), tmpdir, n );
        
            if ( notyet_mask && IsMarksheet( file, m_zmin, m_zmax, m_lmin, m_lmax ) ) {  // マークシート表面をマスクする
                wxImage img_org( file, wxBITMAP_TYPE_JPEG );
                int ver = GetMarksheetVersion( file );
                if ( ver == 2 ) {
                    img_org.SetRGB( m_mask1, 255, 255, 255 );   // cm name
                    img_org.SetRGB( m_mask2, 255, 255, 255 );   // cm no.
                    img_org.SetRGB( m_mask3, 255, 255, 255 );   // barcode
                }
                else {  // 古いマークシート ver == 1
                    img_org.SetRGB( m_mask1old, 255, 255, 255 );   // cm name
                    img_org.SetRGB( m_mask2old, 255, 255, 255 );   // cm no.
                    img_org.SetRGB( m_mask3old, 255, 255, 255 );   // barcode
                }
                img_org.SaveFile( tmpjpg );
                notyet_mask = false;
            }
            else {
                wxCopyFile( file, tmpjpg, true );
            }
            html = html + wxT("<img src=\"") + tmpjpg + wxT("\" width=\"750\" height=\"1060\"/>");
            cout = dir.GetNext( &file );
            n++;
        }
        html = html + wxT("</body></html>");

        // start printing
        wxHtmlPrintout hpout( wxT("Searcher03") );
        hpout.SetMargins( 0, 0, 0, 0, 0 );

        hpout.SetHtmlText( html, wxEmptyString, false );
        p.Print( NULL, &hpout, false );

        wxString pst = wxT("処理済");
        wxString logmsg = path;
        if ( notyet_mask ) {
            pst.Append( wxT(" 非マスク") );
            logmsg.Append( wxT(" not mask") );
        }
        m_grid->SetCellValue( r, 3, pst );
        log.Add( logmsg );
    }
    WriteLog( log );
}

/* 一枚目強制マスク印刷 */
void FrameBatchPrint::OnMaskPrint( wxCommandEvent& WXUNUSED(event) )
{
    wxArrayInt rows = m_grid->GetSelectedRows();
    if ( rows.GetCount() != 1 ) {
        wxMessageDialog md( this, wxT("1件のみ選択してください."), wxT("Message"), wxOK|wxICON_INFORMATION );
        md.ShowModal();
        return;
    }

    wxString path = m_grid->GetCellValue( rows[0], 2 );
    wxDir dir( path );
    if ( !dir.IsOpened() ) return;

    wxPrintDialogData pd;
    wxPrinter p( &pd );
    p.PrintDialog( NULL );

    // 印刷用の html を作成
    wxString file;
    if ( dir.GetFirst( &file, wxT("*.jpg"), wxDIR_FILES ) ) {
        wxString tmpdir = wxGetCwd() + wxFILE_SEP_PATH + wxT("tmp") + wxFILE_SEP_PATH;
        file = path + wxFILE_SEP_PATH + file;
        file.Replace( wxFILE_SEP_PATH, wxT("/") );
        wxString tmpjpg = wxString::Format( wxT("%stmp.jpg"), tmpdir );
        
        wxImage img_org( file, wxBITMAP_TYPE_JPEG );
        img_org.SetRGB( m_mask1, 255, 255, 255 );   // cm name
        img_org.SetRGB( m_mask2, 255, 255, 255 );   // cm no.
        img_org.SetRGB( m_mask3, 255, 255, 255 );   // barcode
        img_org.SaveFile( tmpjpg );

        wxString html;
        html = wxT("<html><body>\n");
        html = html + wxT("<img src=\"") + tmpjpg + wxT("\" width=\"750\" height=\"1060\"/>");
        html = html + wxT("</body></html>");

        // start printing
        wxHtmlPrintout hpout( wxT("Searcher03") );
        hpout.SetMargins( 0, 0, 0, 0, 0 );
        hpout.SetHtmlText( html, wxEmptyString, false );
        p.Print( NULL, &hpout, false );
    }
}

/* 入力禁止 */
void FrameBatchPrint::SetGridReadOnly( void )
{
    for ( int r = 0; r < m_grid->GetNumberRows(); r++ )
        for ( int c = 1; c < m_grid->GetNumberCols(); c++ )
            m_grid->SetReadOnly( r, c, true );
}

/* 印刷ログ表示 */
void FrameBatchPrint::OnLoadLog( wxCommandEvent& WXUNUSED(event) )
{
    wxFileDialog fd( this, wxT("Choose a file"), wxT("log"), wxEmptyString, wxT("log files (*.log)|bp_*.log"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );
    if ( fd.ShowModal() == wxID_CANCEL )
        return;

    m_grid->ClearGrid();

    wxTextFile logfile;
    logfile.Open( fd.GetPath() );

    //
    int d = logfile.GetLineCount() - m_grid->GetNumberRows();
    if ( d > 0 )
        m_grid->AppendRows( d, true );

    wxArrayString hhsno;
    wxString hhs, path, stat;
    for ( int n = 0; n < logfile.GetLineCount(); n++ ) {
        hhs  = logfile[n].AfterLast( wxFILE_SEP_PATH );
        stat = hhs.AfterFirst( ' ' );
        hhs  = hhs.BeforeFirst( ' ' );
        path = logfile[n];
        if ( !stat.IsEmpty() ) {
            path = path.BeforeFirst( ' ' );
        }
        m_grid->SetCellValue( n, 0, hhs );
        m_grid->SetCellValue( n, 2, path );
        m_grid->SetCellValue( n, 3, stat );
        hhsno.Add( hhs );
    }
    for ( int n = 0; n < hhsno.GetCount(); n++ ) {
        wxArrayString info = wxSplit( GetHhsInfoByHhsNo( hhsno[n] ), '_', '\\' );
        m_grid->SetCellValue( n, 1, info[0] );
    }
    logfile.Close();
}

/* 印刷ログ出力 */
void FrameBatchPrint::WriteLog( wxArrayString logline )
{
    wxDateTime now = wxDateTime::Now();
    wxString log_file = wxGetCwd() + wxFILE_SEP_PATH + wxT("log") + wxFILE_SEP_PATH + wxT("bp_") + now.Format( wxT("%Y%m%d%H%M%S.log") );
    wxTextFile file( log_file );
    file.Create();
    wxTextFile logfile;
    logfile.Open( log_file );

    for ( int n = 0; n < logline.GetCount(); n++ ) {
        logfile.AddLine( logline[n] );
    }
    logfile.Write();
    logfile.Close();
}