notify_implementation_example.cpp

This small example demonstrates how the class ReadNotification could be used to provide feedback to a user interface for lengthy read operations.

// this is an example code snipped that shows a possible implementation for
// a notification object.

#include <VCL.h>
#include <IBK_stopwatch.h>

#include "delphin5_output.h"

class NotifyUI : public DELPHIN5_OUTPUT_FILE_FILTERS::ReadNotification {
  public:
    // Constructor, takes a pointer to a TMemo field and the
    // number of the line where we are adding dots.
    NotifyUI(TMemo * m, unsigned int line_num) : memo(m), li(line_num) {}
    
    // called from the read functions
    virtual void notify() {
        // check that at least 500 ms have elapsed since last notification call
        if (watch.difference() < 500)  return;
        // restart stop watch
        watch.start();
        // as an example we just add a '.' at the line in the memo field
        AnsiString text = memo->Lines->Strings[li];
        memo->Lines->Strings[li] = text + ".";
        // make sure the stuff is actually shown by the GUI
        Application->ProcessMessages();
    }

    TMemo * memo;    // the memo field
    unsigned int li; // the line index

    IBK::StopWatch watch; // a handy stopwatch
};
//---------------------------------------------------------------------------



... other code ...

// and here is the example how to add this to a read function

for (int i=0; i<=num_files; ++i) {
    AnsiString fname = m_files[i];

    memo->Lines->Add("Converting '" + ExtractFileName(fname) + "'...");
    Application->ProcessMessages();

    // now we create our notification object
    NotifyUI n(memo, i);
        
    // try to read output file
    Delphin5OutputData outdata;
    string err_msg;
    // when we call "read" here, pass a pointer to the notification object
    if (!outdata.read(fname.c_str(), err_msg, &n)) { 
        MessageDlg("Cannot read output file '"+fname+"'!", mtError, TMsgDlgButtons() << mbOK, 0);
        continue;
    }
    
    // ... do something with the read data
    
} // end for

//---------------------------------------------------------------------------

Generated on Tue Mar 24 18:12:02 2009 for Delphin 5 Outputs Library by  doxygen 1.5.5