--- as_dataop.cpp 2002/11/22 21:51:58 1.5
+++ as_dataop.cpp 2002/11/24 23:11:56 1.6
@@ -4,6 +4,7 @@
#include "titrex.h" // Exception classes
#include "titabitem.h" // For class RtTableItem
+
//
// Convenience method to load accounting data from a file
//
@@ -16,7 +17,7 @@
}
else {
if (!Fileobj.open(IO_ReadOnly)) // Try to open file
- throw Genexcept("Could not open accounting 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
@@ -29,7 +30,7 @@
void Titraqform::loadData(QTextStream &Tstream)
{
bool bValid = true; // Used to warn on invalid accounting data
- int i = 0; // Iterator used in loop and also as a count
+ 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);
@@ -44,41 +45,113 @@
Temp = Tstream.readLine();
QTextStream Asline(&Temp, IO_ReadOnly); // Convert a single line now
- if (i % g_knBlocks == 0) // Add blocks of rows to optimize loading speed
+ 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(i, 0, Date);
+ 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(i, 4, new RtTableItem(m_pMaintable, QTableItem::WhenCurrent, Account));
+ 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(i, 3, Amount);
+ m_pMaintable->setText(nIter, 3, Amount);
else
bValid = false;
Remark = Asline.read(); // Copy the remark field
if (Remark != NULL)
- m_pMaintable->setText(i, 5, Remark);
+ m_pMaintable->setText(nIter, 5, Remark);
- i++; // The big increment
+ nIter++; // The big increment
}
m_pMaintable->setUpdatesEnabled(true); // Repaint all
- m_pMaintable->setNumRows(i - 1); // Trim unneeded rows
+ m_pMaintable->setNumRows(nIter - 1); // Trim unneeded rows
m_pRemark->setText(trUtf8("Loaded text goes here"));
m_pRemark->setEdited(false); // Reset widget
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 << trUtf8(" "); // Save date field text
+ else
+ bValid = false;
+
+ Tstream << trUtf8("user "); // Save user field text
+
+/* Tempfield = m_pMaintable->text(nIter, 1); // Load start field text
+ if (Tempfield != NULL)
+ Tstream << Tempfield << trUtf8(" "); // Save start field text
+ else
+ bValid = false;
+
+ Tempfield = m_pMaintable->text(nIter, 2); // Load end field text
+ if (Tempfield != NULL)
+ Tstream << Tempfield << trUtf8(" "); // Save end field text
+ else
+ bValid = false;*/
+
+ Tempfield = m_pMaintable->text(nIter, 4); // Load amount field text
+ if (Tempfield != NULL)
+ Tstream << Tempfield << trUtf8(" "); // Save amount field text
+ else
+ bValid = false;
+
+ Tempfield = m_pMaintable->text(nIter, 3); // Load acct field text
+ if (Tempfield != NULL)
+ Tstream << Tempfield << trUtf8(" "); // Save acct field text
+ else
+ bValid = false;
+
+ Tempfield = m_pMaintable->text(nIter, 5); // Load remark field text
+ if (Tempfield != NULL)
+ Tstream << Tempfield << trUtf8(" "); // Save remark field text
+ else
+ bValid = false;
+
+ Tstream << endl; // Append a newline
+ }
+}
|