ossp-pkg/as/as-gui/as_dataop.cpp
1.8
#include <qregexp.h> // Portable regular expressions
#include "titraq.h" // Main classes
#include "titrex.h" // Exception classes
#include "titabitem.h" // For class RtTableItem
//
// Convenience method to load accounting data from a file
//
void Titraqform::loadData(QFile &Fileobj)
{
if (Fileobj.isOpen()) { // Check state of file
Fileobj.flush(); // Begin processing file cleanly
QTextStream Asentry(&Fileobj); // Convert data to stream
this->loadData(Asentry); // Pass off to do the real work
}
else {
if (!Fileobj.open(IO_ReadOnly)) // Try to open file
throw Genexcept("Could not read open accounting file.");
QTextStream Asentry(&Fileobj); // Convert data to stream
this->loadData(Asentry); // Pass off to do the real work
Fileobj.close(); // Finish fileop by closing
}
}
//
// Load accounting data from a stream
//
void Titraqform::loadData(QTextStream &Tstream)
{
bool bValid = true; // Used to warn on invalid accounting data
int nIter = 0; // Iterator used in loop and also as a count
// Optimize viewing by repainting cells only once after processing
m_pMaintable->setUpdatesEnabled(false);
// Set the table text by linewise reading from the input stream
// and parsing date, time, account, and other columns out of it
while (!Tstream.atEnd()) {
QString Date, Account, Amount, Remark; // Fields of a valid AS file
QString Temp; // Used for linewise editing
while (Temp.isEmpty() && !Tstream.atEnd()) // Strip out extra line feed
Temp = Tstream.readLine();
QTextStream Asline(&Temp, IO_ReadOnly); // Convert a single line now
if (nIter % g_knBlocks == 0) // Add blocks of rows to optimize loading
m_pMaintable->setNumRows(m_pMaintable->numRows() + g_knBlocks);
Asline >> Date; // Copy the date field
if (Date != NULL)
m_pMaintable->setText(nIter, 0, Date);
else
bValid = false;
Asline >> Account; // Copy to the bit bucket
Asline >> Account; // Copy the account field
if (Account != NULL) {
m_pMaintable->setItem(nIter, 4, new RtTableItem(m_pMaintable, QTableItem::WhenCurrent, Account));
}
else
bValid = false;
Asline >> Amount; // Copy the amount field
if (Amount != NULL)
m_pMaintable->setText(nIter, 3, Amount);
else
bValid = false;
Remark = Asline.read(); // Copy the remark field
if (Remark != NULL)
m_pMaintable->setText(nIter, 5, Remark);
nIter++; // The big increment
}
m_pMaintable->setUpdatesEnabled(true); // Update and repaint
m_pMaintable->setNumRows(nIter - 1); // Trim excess rows
m_pMaintable->setCurrentCell(nIter - 1, 0); // Move focus to last row
if (!bValid)
throw Genexcept("Warning, invalid accounting data.");
}
//
// Convenience method to save accounting data to a file
//
void Titraqform::saveData(QFile &Fileobj)
{
if (Fileobj.isOpen()) { // Check state of file
Fileobj.flush(); // Begin processing file cleanly
QTextStream Asentry(&Fileobj); // Convert data to stream
this->saveData(Asentry); // Pass off to do the real work
}
else {
if (!Fileobj.open(IO_WriteOnly)) // Try to open file
throw Genexcept("Could not write open accounting file.");
QTextStream Asentry(&Fileobj); // Convert data to stream
this->saveData(Asentry); // Pass off to do the real work
Fileobj.close(); // Finish fileop by closing
}
}
//
// Save accounting data to a stream
//
void Titraqform::saveData(QTextStream &Tstream)
{
const int nRows = m_pMaintable->numRows(); // Max rows used in loop
QString Tempfield; // Current field string
bool bValid = true; // Warn on invalid data
// Linewise save from the main table date, time, account, and others
for (int nIter = 0; nIter <= nRows; nIter++) {
Tempfield = m_pMaintable->text(nIter, 0); // Load date field text
if (Tempfield != NULL)
Tstream << Tempfield; // Save date field text
else
bValid = false;
Tstream << trUtf8(" ") << trUtf8("user"); // Save user field text
Tempfield = m_pMaintable->text(nIter, 1); // Load start field text
if (Tempfield != NULL)
Tstream << trUtf8(" ") << Tempfield; // Save start field text
/* else
bValid = false;*/
Tempfield = m_pMaintable->text(nIter, 2); // Load end field text
if (Tempfield != NULL)
Tstream << trUtf8(" ") << Tempfield; // Save end field text
/* else
bValid = false;*/
Tempfield = m_pMaintable->text(nIter, 4); // Load amount field text
if (Tempfield != NULL)
Tstream << trUtf8(" ") << Tempfield; // Save amount field text
else
bValid = false;
Tempfield = m_pMaintable->text(nIter, 3); // Load acct field text
if (Tempfield != NULL)
Tstream << trUtf8(" ") << Tempfield; // Save acct field text
else
bValid = false;
Tempfield = m_pMaintable->text(nIter, 5); // Load remark field text
if (Tempfield != NULL)
Tstream << trUtf8(" ") << Tempfield; // Save remark field text
else
bValid = false;
Tstream << endl; // Append a newline
}
}