diff src/myframe.cpp @ 11:9b0840b0be7e

cache done.
author pyon@macmini
date Sun, 23 Oct 2011 07:43:34 +0900
parents 1fda3a06c39b
children 6ed3b64ed39a
line wrap: on
line diff
--- a/src/myframe.cpp	Fri Oct 21 07:58:23 2011 +0900
+++ b/src/myframe.cpp	Sun Oct 23 07:43:34 2011 +0900
@@ -1,5 +1,5 @@
 // Filename   : myframe.cpp
-// Last Change: 21-Oct-2011.
+// Last Change: 23-Oct-2011.
 //
 
 #include "main.h"
@@ -7,7 +7,6 @@
 #include "param.h"
 #include "dndfile.h"
 #include "marksheet.h"
-#include "cache.h"
 
 // resources
 // the application icon (under Windows and OS/2 it is in resources and even
@@ -167,8 +166,7 @@
     m_statusBar->SetStatusText( wxEmptyString, 0 );
 	
 	this->Centre( wxBOTH );
-
-    Cache cache;
+    m_timer.SetOwner( this, ID_TIMER );
 }
 
 // destructor
@@ -178,6 +176,8 @@
 
 // Event Table
 BEGIN_EVENT_TABLE( MyFrame, wxFrame )
+    EVT_IDLE( MyFrame::OnIdle )
+    EVT_TIMER( ID_TIMER, MyFrame::OnTimer )
     EVT_SIZE( MyFrame::OnWinSize )
     EVT_MOVE( MyFrame::OnWinMove )
     EVT_MENU( ID_MENUITEMPARAM,   MyFrame::OnParam )
@@ -232,6 +232,9 @@
     }
     m_buttonMove->Enable(true);
 
+    // TODO.
+    // make 申請書ふぉるだ
+
     wxMessageBox(wxT("移動先フォルダ準備完了"));
     wxString cmd = wxT("explorer ") + to;
     wxExecute( cmd );
@@ -301,6 +304,7 @@
     wxString workdir = m_dirPickerWork->GetPath();
     wxDir dir( workdir );
     if ( !dir.IsOpened() ) return;
+    // start-up iamge
     wxString notfound = wxGetCwd() + wxFILE_SEP_PATH + wxT("image") + wxFILE_SEP_PATH + wxT("notfound.png");
     wxBitmap bmp = wxBitmap( notfound, wxBITMAP_TYPE_PNG );
     m_bitmapName->SetBitmap( bmp );
@@ -308,6 +312,7 @@
     bmp.LoadFile( notfound, wxBITMAP_TYPE_PNG );
     m_bitmapHhsno->SetBitmap( bmp );
 
+    // enum jpeg
     wxArrayString filenames;
     unsigned int n = dir.GetAllFiles( workdir, &filenames, wxT("*.jpg"), wxDIR_FILES );
 
@@ -319,6 +324,7 @@
     pd.SetSize( wxSize(320,140) );
     float z; long l;
     for ( int i=0; i<n; i++ ) {
+        /*
         wxFileName f( filenames[i] );
         wxString filename = f.GetFullName();
 
@@ -354,6 +360,7 @@
             }
         }
         m_listCtrlView->SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
+        */
 
         // write log
         pd.Update( i+1, wxT("画像認識中") );
@@ -365,6 +372,7 @@
 /* 画像を選択 */
 void MyFrame::GetImageInfo(wxListEvent& event)
 {
+    /*
     SetStatusText( wxEmptyString, 2 );
     int i = event.GetIndex();
 
@@ -377,6 +385,7 @@
         msg = wxT("perhaps marksheet !");
     }
     SetStatusText( wxString::Format(wxT("selected image : z = %f, l = %d ...")+msg, b, l ), 2 );
+    */
 }
 
 /* 画像のスクリーン表示*/
@@ -409,7 +418,76 @@
     pd->Destroy();
 }
 
+/* キャッシュ作成&更新 */
+void MyFrame::UpdateCache()
+{
+    wxString workdir = m_dirPickerWork->GetPath();
+    wxDir dir( workdir );
+    if ( !dir.IsOpened() ) return;
+    wxArrayString filenames;
+
+    wxListItem item;
+    wxString first;
+    float z; long l;
+    bool m;
+
+    wxGetApp().WriteLog( wxT("start updating cache") );
+    SetStatusText( wxT("rebuiling cache..."), 0 );
+
+    for ( CacheHash::iterator i=CH.begin(); i != CH.end(); ++i ) {
+        CacheItem* ci = new CacheItem;
+        ci = i->second;
+        ci->exists = false;
+    }
+    unsigned int n = dir.GetAllFiles( workdir, &filenames, wxT("*.jpg"), wxDIR_FILES );
+    for ( int i=0; i<n; i++ ) {
+        CacheItem* ci = new CacheItem;
+        if ( CH.count(filenames[i]) ) {
+            ci = CH[filenames[i]];
+            ci->exists = true;
+            continue;
+        }
+        ci->filename = filenames[i];
+        m = IsMarksheet( filenames[i], &z, &l );
+        wxImage image( filenames[i], wxBITMAP_TYPE_JPEG );
+        wxBitmap bmp( image.Scale( 160, 226, wxIMAGE_QUALITY_HIGH ) );
+        ci->marksheet = m;
+        ci->z         = z;
+        ci->l         = l;
+        ci->exists    = true;
+        ci->thumbnail = bmp;
+        ci->modtime   = wxEmptyString; // TODO.
+        CH[ci->filename] = ci;  // add hash
+        wxGetApp().WriteLog( filenames[i] + wxString::Format(wxT(" z=%f l=%d m=%d"), z, l, m ? 1 : 0 ) );
+    }
+    for ( CacheHash::iterator i=CH.begin(); i != CH.end(); ++i ) {
+        CacheItem* ci = new CacheItem;
+        ci = i->second;
+        if ( !ci->exists ) {
+            CH.erase(ci->filename);
+            wxGetApp().WriteLog( ci->filename + wxT(" removed.") );
+        }
+    }
+
+    SetStatusText( wxEmptyString, 0 );
+    m_timer.Start( 20*1000, wxTIMER_ONE_SHOT ); // restart
+}
+
 // 以下,定型もの
+void MyFrame::OnTimer(wxTimerEvent& event)
+{
+    UpdateCache();
+}
+/* アイドリング */
+void MyFrame::OnIdle(wxIdleEvent& event)
+{
+    if ( !m_timer.IsRunning() ) {
+        m_timer.Start( 20*1000, wxTIMER_ONE_SHOT );
+    }
+    event.RequestMore();
+    event.Skip();
+}
+
 /* アプリフォルダを開く */
 void MyFrame::OnOpenAppDir(wxCommandEvent& WXUNUSED(event))
 {