view src/cache.cpp @ 22:92188f60323d default tip

Implement Masking function on Preview Dialog.
author pyon@macmini
date Sat, 04 Apr 2015 17:23:46 +0900
parents a2ad87cad48b
children
line wrap: on
line source

// Filename   : cache.cpp
// Last Change: 15-Dec-2014.
//

#include "cache.h"
#include "db.h"

// キャッシュ取得ダイアログ
CacheGetDialog::CacheGetDialog( 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( wxVERTICAL );
	
	m_dirPicker = new wxDirPickerCtrl( this, wxID_ANY, wxEmptyString, wxT("Select a directory") );
	bSizerTop->Add( m_dirPicker, 0, wxALL|wxEXPAND, 5 );
	
	wxBoxSizer* bSizerButton = new wxBoxSizer( wxHORIZONTAL );
	
	m_buttonGet = new wxButton( this, ID_GETCACHE, wxT("コピィ"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonGet, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_buttonCancel = new wxButton( this, wxID_CANCEL, wxT("閉じる"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonCancel, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( bSizerButton, 0, wxALIGN_RIGHT, 5 );
	
	this->SetSizer( bSizerTop );
	this->Layout();
	
	this->Centre( wxBOTH );
}

CacheGetDialog::~CacheGetDialog()
{
}

// Event Table
BEGIN_EVENT_TABLE( CacheGetDialog, wxDialog )
    EVT_BUTTON( ID_GETCACHE, CacheGetDialog::OnGetCache )
END_EVENT_TABLE()

// Event Handlers & Functions
void CacheGetDialog::OnGetCache( wxCommandEvent& WXUNUSED(event) )
{
    wxString fromdir = m_dirPicker->GetPath();

    wxString cachedir = wxGetCwd() + wxFILE_SEP_PATH + wxT("cache");

    for ( int i = 0; i < m_nocache.GetCount(); i++ ) {

        wxString year = m_nocache[i].Left( 4 );
        wxString month = m_nocache[i].Mid( 4, 2 );

        if ( month.IsSameAs(wxT("01")) || month.IsSameAs(wxT("02")) || month.IsSameAs(wxT("03")) ) {
            long y;
            year.ToLong( &y, 10 );
            y--;
            year = wxString::Format( wxT("%d"), y );
        }

        wxString from = fromdir  + wxFILE_SEP_PATH + year + wxFILE_SEP_PATH + m_nocache[i];
        wxString to   = cachedir + wxFILE_SEP_PATH + year + wxFILE_SEP_PATH + m_nocache[i];

        wxArrayString files;
        wxDir::GetAllFiles( from, &files, wxT("*.png"), wxDIR_DEFAULT );

        wxProgressDialog pd( wxT("進行状況"), wxT("処理開始..."), files.GetCount(), NULL, wxPD_APP_MODAL|wxPD_REMAINING_TIME|wxPD_AUTO_HIDE );
        pd.SetSize( wxSize( 320, 140 ) );

        for ( int j = 0; j < files.GetCount(); j++ ) {
            wxFileName fn( files[j] );

            wxString ccn = fn.GetPath().BeforeLast( wxFILE_SEP_PATH ).AfterLast( wxFILE_SEP_PATH );
            wxString hhs = fn.GetPath().AfterLast( wxFILE_SEP_PATH );
            wxString buf = to + wxFILE_SEP_PATH + ccn + wxFILE_SEP_PATH + hhs + wxFILE_SEP_PATH + fn.GetFullName();

            wxFileName td( buf );
            if ( !td.Exists() ) td.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );

            wxCopyFile( files[j], buf, true );
            if ( j % 5 == 0 ) wxSleep( 2 ); // ディスクの負荷軽減

            pd.Update( j, hhs + wxT("@") + ccn + wxT("@") + m_nocache[i] + wxT("を処理しました.") );
        }
    }

    wxMessageBox( wxT("Getting cache done."), wxT("Message"), wxOK|wxSTAY_ON_TOP );
    Close();
}

void CacheGetDialog::SetSyncDates( wxArrayString dates )
{
    m_nocache = dates;
}

// メインダイアログ
CacheDialog::CacheDialog( 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 );
	
	wxBoxSizer* bSizerTop = new wxBoxSizer( wxHORIZONTAL );
	
    //
	wxBoxSizer* bSizerList = new wxBoxSizer( wxVERTICAL );
	
	m_staticText = new wxStaticText( this, wxID_ANY, wxT("作成対象選択"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerList->Add( m_staticText, 0, wxALL, 5 );
	
	m_listCtrl = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT );
    wxListItem itemCol;
    itemCol.SetText( wxT("通番") );
    m_listCtrl->InsertColumn( 0, itemCol );
    m_listCtrl->SetColumnWidth( 0, 50 );
    itemCol.SetText( wxT("審査会年月日") );
    m_listCtrl->InsertColumn( 1, itemCol );
    m_listCtrl->SetColumnWidth( 1, 100 );
    itemCol.SetText( wxT("キャッシュ有無") );
    m_listCtrl->InsertColumn( 2, itemCol );
    m_listCtrl->SetColumnWidth( 2, 100 );
	bSizerList->Add( m_listCtrl, 1, wxALL|wxEXPAND, 5 );
	
    //
	wxBoxSizer* bSizerRange = new wxBoxSizer( wxHORIZONTAL );

	m_staticTextRange = new wxStaticText( this, wxID_ANY, wxT("表示範囲指定"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerRange->Add( m_staticTextRange, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );

	m_datePickerBgn = new wxDatePickerCtrl( this, ID_RGBGN, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN );
	bSizerRange->Add( m_datePickerBgn, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticTextBetween = new wxStaticText( this, wxID_ANY, wxT("~"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerRange->Add( m_staticTextBetween, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );

	m_datePickerEnd = new wxDatePickerCtrl( this, ID_RGEND, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN );
	bSizerRange->Add( m_datePickerEnd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerList->Add( bSizerRange, 0, wxALL|wxEXPAND, 5 );

	bSizerTop->Add( bSizerList, 1, wxEXPAND, 5 );

    //	
	wxBoxSizer* bSizerButton = new wxBoxSizer( wxVERTICAL );

	m_buttonCache = new wxButton( this, ID_MKCACHE, wxT("作成"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonCache, 0, wxALL, 5 );
	
	m_buttonGet = new wxButton( this, ID_GET, wxT("取得"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonGet, 0, wxALL, 5 );
	
	m_buttonClose = new wxButton( this, wxID_CANCEL, wxT("閉じる"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonClose, 0, wxALL, 5 );
	m_buttonClose->SetDefault(); 
	
	bSizerTop->Add( bSizerButton, 0, 0, 5 );
	
	
	this->SetSizer( bSizerTop );
	this->Layout();
	
	this->Centre( wxBOTH );
}

CacheDialog::~CacheDialog()
{
}

// Event Table
BEGIN_EVENT_TABLE( CacheDialog, wxDialog )
    EVT_DATE_CHANGED( ID_RGBGN, CacheDialog::OnDateChange )
    EVT_DATE_CHANGED( ID_RGEND, CacheDialog::OnDateChange )
    EVT_BUTTON( ID_MKCACHE,     CacheDialog::OnMakeCache  )
    EVT_BUTTON( ID_GET,         CacheDialog::OnGetCache   )
END_EVENT_TABLE()

// Event Handlers & Functions
void CacheDialog::Setting( wxString rootdir, int w, int h )
{
    m_rootdir = rootdir;
    m_width   = w;
    m_height  = h;

    m_datePickerBgn->SetValue( wxDateTime::Today() - wxDateSpan::Month() * 2 );
    m_datePickerEnd->SetValue( wxDateTime::Today() );
}

void CacheDialog::Listup( void )
{
    nocache.Clear();
    wxDateTime b = m_datePickerBgn->GetValue();
    wxDateTime e = m_datePickerEnd->GetValue();
    wxArrayString ccn = GetCcnDate();
    
    wxDateTime dt;
    wxRegEx reDate( wxT("(^20[0-9]{2})([0-1][0-9])([0-3][0-9])$") );
    m_listCtrl->DeleteAllItems();

    for ( int i = 0; i < ccn.GetCount(); i++ ) {

        dt.ParseFormat( ccn[i], wxT("%Y%m%d") );

        if ( dt.IsBetween( b, e ) ) {

            // cache exists ?
            wxString month = dt.Format( wxT("%m") );
            wxString year  = dt.Format( wxT("%Y") );

            if ( month.IsSameAs(wxT("01")) || month.IsSameAs(wxT("02")) || month.IsSameAs(wxT("03")) ) {
                long y;
                year.ToLong( &y, 10 );
                y--;
                year = wxString::Format( wxT("%d"), y );
            }
            wxString cache_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("cache") + wxFILE_SEP_PATH + year + wxFILE_SEP_PATH + dt.Format( wxT("%Y%m%d") );

            wxString cache_ok;
            wxFileName cd( cache_dir );
            if ( cd.Exists() ) 
                cache_ok = wxT("○");
            else
                nocache.Insert( dt.Format( wxT("%Y%m%d") ), 0 );

            m_listCtrl->InsertItem( i, -1 );
            m_listCtrl->SetItem( i, 0, wxString::Format( wxT("%d"), i + 1 ) , -1 ); // No
            reDate.Replace( &ccn[i], wxT("\\1-\\2-\\3") );
            m_listCtrl->SetItem( i, 1, ccn[i], -1 );
            if ( !cache_ok.IsEmpty() ) m_listCtrl->SetItem( i, 2, cache_ok, -1 );

            if ( i % 2 ) m_listCtrl->SetItemBackgroundColour( i, wxColour( wxT("WHEAT") ) );
        }
    }
}

void CacheDialog::OnDateChange( wxDateEvent& WXUNUSED(event) )
{
    Listup();
}

void CacheDialog::OnMakeCache( wxCommandEvent& WXUNUSED(event) )	
{
    if ( m_listCtrl->GetSelectedItemCount() < 1 ) {
        wxMessageBox( wxT("項目を少なくともひとつ選択してください.") );
        return;
    }
    wxString cachedir = wxGetCwd() + wxFILE_SEP_PATH + wxT("cache");

    long item = -1;
    bool done = false;
    for ( ;; ) {
        item = m_listCtrl->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
        if ( item == -1 ) break;

        wxString c = m_listCtrl->GetItemText( item, 2 );
        if ( c.IsEmpty() ) {
            wxString date = m_listCtrl->GetItemText( item, 1 );
            date.Replace( wxT("-"), wxEmptyString, true );

            wxArrayString path = GetPathesByDate( date );

            wxProgressDialog pd( wxT("進行状況"), wxT("処理開始..."), path.GetCount(), NULL, wxPD_APP_MODAL|wxPD_REMAINING_TIME|wxPD_AUTO_HIDE );
            pd.SetSize( wxSize( 320, 140 ) );

            for ( int i = 0; i < path.GetCount(); i++ ) {

                wxArrayString files;
                wxDir::GetAllFiles( path[i], &files, wxT("*.jpg"), wxDIR_DEFAULT );

                for ( int j = 0; j < files.GetCount(); j++ ) {
                    wxImage image( files[j], wxBITMAP_TYPE_JPEG );
                    wxImage output = image.Scale( m_width, m_height, wxIMAGE_QUALITY_HIGH );

                    wxString buf = files[j];
                    buf.Replace( m_rootdir, cachedir, false );
                    buf = buf.BeforeLast( '.' ) + wxT(".png");

                    wxFileName tf( buf );
                    if ( !tf.Exists() ) tf.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
                    output.SaveFile( buf, wxBITMAP_TYPE_PNG );
                }
                pd.Update( i, path[i] + wxT("を処理しました.") );
                wxSleep( 10 );
            }
            done = true;
        }
    }

    if ( done ) {
        wxMessageBox( wxT("cache updated."), wxT("Message"), wxOK|wxSTAY_ON_TOP );
        Listup();
    }
}

void CacheDialog::OnGetCache( wxCommandEvent& WXUNUSED(event) )
{
    CacheGetDialog* cd = new CacheGetDialog( this, wxID_ANY, wxT("キャッシュ取得"), wxDefaultPosition, wxSize( 500, 100 ), wxCAPTION|wxFRAME_NO_TASKBAR );
    cd->SetSyncDates( nocache );
    cd->ShowModal();
    Listup();
}