as_reportpanel.cpp

Thu, 06 Aug 2009 13:21:30 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 06 Aug 2009 13:21:30 +0200
changeset 15
0e0eb7c91312
parent 14
1c58588f9cb6
permissions
-rw-r--r--

Remove seemingly declarations unnecessary according to Qt 4.5.2 headers.

     1 //
     2 //  OSSP asgui - Accounting system graphical user interface
     3 //  Copyright (c) 2002-2009 The OSSP Project (http://www.ossp.org/)
     4 //  Copyright (c) 2002-2009 Ralf S. Engelschall <rse@engelschall.com>
     5 //  Copyright (c) 2002-2009 Michael Schloh von Bennewitz <michael@schloh.com>
     6 //  Copyright (c) 2002-2009 Cable & Wireless Telecommunications Services GmbH
     7 //
     8 //  This file is part of OSSP asgui, an accounting system graphical user
     9 //  interface which can be found at http://asgui.europalab.com/.
    10 //
    11 //  Permission to use, copy, modify, and distribute this software for
    12 //  any purpose with or without fee is hereby granted, provided that
    13 //  the above copyright notice and this permission notice appear in all
    14 //  copies.
    15 //
    16 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    17 //  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    18 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    19 //  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    20 //  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    21 //  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    22 //  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    23 //  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    24 //  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    25 //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    26 //  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    27 //  SUCH DAMAGE.
    28 //
    29 //  as_reportpanel.cpp: ISO C++ implementation
    30 //
    32 #include <qvariant.h>
    33 #include <qpushbutton.h>
    34 #include <q3textedit.h>
    35 #include <qtoolbutton.h>
    36 #include <q3buttongroup.h>
    37 #include <q3popupmenu.h>
    38 #include <qlayout.h>
    39 #include <qtooltip.h>
    40 #include <q3whatsthis.h>
    41 #include <q3textstream.h>
    42 #include <q3filedialog.h>
    43 #include <qprinter.h>
    44 #include <q3paintdevicemetrics.h>
    45 #include <qpainter.h>
    46 #include <qfile.h>
    47 #include <qdir.h>
    49 //Added by qt3to4:
    50 #include <Q3HBoxLayout>
    51 #include <Q3Frame>
    52 #include <Q3VBoxLayout>
    54 #include "as_reportpanel.h"
    55 #include "as_numdial.h"
    56 #include "as_table.h"
    57 #include "as_pref.h"
    58 #include "as_generic.h"
    59 #include "as_const.h"
    60 #include "as_except.h"
    62 // Version information
    63 #define _AS_VERSION_CPP_AS_HEADER_
    64 #include "as_version.cpp"
    65 #undef  _AS_VERSION_CPP_AS_HEADER_
    68 namespace AS {
    70 //
    71 // Constructs a Reportpanel as a child of 'pParent', with the 
    72 // name 'kszName' and widget flags set to 'Flags'.
    73 //
    74 // The dialog will by default be modal, unless you set 'bModal' to
    75 // false to construct a modeless dialog.
    76 //
    77 Reportpanel::Reportpanel(TiTable *pTable, Preferences *pPreferences,
    78                          QWidget *pParent, const char *kszName,
    79                          bool bModal, Qt::WFlags Flags)
    80                          : QDialog(pParent, kszName, bModal, Flags)
    81 {
    82     // Boilerplate code to initialize the panel
    83     if (!kszName)
    84         this->setName("Reportpanel");
    86     // Make panel resizeable
    87     this->setSizeGripEnabled(true);
    88     this->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)5,
    89         (QSizePolicy::SizeType)5, 0, 0, this->sizePolicy().hasHeightForWidth()));
    91     // Store matrix and prefs members
    92     m_pReptable = pTable;
    93     m_pReprefs  = pPreferences;
    94     m_pPrinter  = new QPrinter;
    96     // Preset number of weeks and months to report
    97     m_nWeeks = m_pReprefs->getNumber(TITRAQ_PREFREPORTWEEKS, TITRAQ_DEFREPORTWEEKS);
    98     m_nMonths = m_pReprefs->getNumber(TITRAQ_PREFREPORTMONTHS, TITRAQ_DEFREPORTMONTHS);
   100     // Build panel using already constructed widgets and layouts
   101     m_pFormlay = new Q3VBoxLayout(this, 11, 6, "Formlayout");
   102     m_pToolay = new Q3HBoxLayout(0, 0, 6, "Toolbuttonlay"); 
   103     m_pPushlay = new Q3HBoxLayout(0, 0, 6, "Pushbuttonlay"); 
   105     // Groupbox and its text display
   106     m_pBrowser = new Q3TextEdit(this, "Reportbrowser");
   107     m_pBrowser->setTextFormat(Qt::PlainText);
   108     m_pBrowser->setReadOnly(true);
   109     m_pBrowser->setFont(QFont("Courier", 10));
   110     m_pFormlay->addWidget(m_pBrowser);
   111     m_pFormlay->addSpacing(6);
   112     m_pFormlay->addLayout(m_pToolay);
   113     m_pFormlay->addSpacing(6);
   114     m_pFormlay->addLayout(m_pPushlay);
   116     // Tool button suite
   117     m_pWeekmonthgroup = new Q3ButtonGroup(this, "Weekmonthgroup");
   118     m_pWeekmonthgroup->setColumnLayout(0, Qt::Horizontal);
   119     m_pWeekmonthgroup->layout()->setSpacing(11);
   120     m_pWeekmonthgroup->layout()->setMargin(0);
   121     m_pWeekmonthlay = new Q3HBoxLayout(m_pWeekmonthgroup->layout());
   122     //m_pWeekmonthgroup->setFrameShape(QFrame::NoFrame);
   123     m_pWeekmonthgroup->setExclusive(true);
   124     m_pWeeklybutt = new QToolButton(m_pWeekmonthgroup, "Weeklybutton");
   125 //    m_pWeeklybutt->setPaletteBackgroundColor(QColor(198, 196, 186));
   126     m_pWeeklybutt->setFocusPolicy(Qt::TabFocus);
   127     m_pWeeklybutt->setCursor(QCursor(Qt::PointingHandCursor));
   128     m_pWeeklybutt->setToggleButton(true);
   129     m_pMonthlybutt = new QToolButton(m_pWeekmonthgroup, "Monthlybutton");
   130 //    m_pMonthlybutt->setPaletteBackgroundColor(QColor(198, 196, 186));
   131     m_pMonthlybutt->setFocusPolicy(Qt::TabFocus);
   132     m_pMonthlybutt->setCursor(QCursor(Qt::PointingHandCursor));
   133     m_pMonthlybutt->setToggleButton(true);
   135     // Popup for number of weeks
   136     m_pWeekpop = new Q3PopupMenu(this);
   137     if (m_pWeekpop == NULL) // Sanity check
   138         throw Genexcept("Weekly toolbutton popup creation failed.");
   139     m_pWeekpop->insertItem(trUtf8("One week"), this, SLOT(reportWeeks(int)));
   140     m_pWeekpop->insertItem(trUtf8("Two weeks"), this, SLOT(reportWeeks(int)));
   141     m_pWeekpop->insertItem(trUtf8("Three weeks"), this, SLOT(reportWeeks(int)));
   142     m_pWeekpop->insertItem(trUtf8("Four weeks"), this, SLOT(reportWeeks(int)));
   143     m_pWeekpop->insertItem(trUtf8("N... weeks"), this, SLOT(reportWeeks(int)));
   144     m_pWeekpop->setCheckable(true);
   145     m_pWeeklybutt->setPopup(m_pWeekpop);
   146     m_pWeeklybutt->setPopupDelay(TITRAQ_POPUPMSECS);
   148     { // Initialize check button correctly
   149         int nPosition = (m_nWeeks < m_pWeekpop->count()) ? m_nWeeks : m_pWeekpop->count();
   150         m_pWeekpop->setItemChecked(m_pWeekpop->idAt(--nPosition), true);
   151     }
   153     // Popup for number of months
   154     m_pMonthpop = new Q3PopupMenu(this);
   155     if (m_pMonthpop == NULL) // Sanity check
   156         throw Genexcept("Monthly toolbutton popup creation failed.");
   157     m_pMonthpop->insertItem(trUtf8("One month"), this, SLOT(reportMonths(int)));
   158     m_pMonthpop->insertItem(trUtf8("Two months"), this, SLOT(reportMonths(int)));
   159     m_pMonthpop->insertItem(trUtf8("Three months"), this, SLOT(reportMonths(int)));
   160     m_pMonthpop->insertItem(trUtf8("Four months"), this, SLOT(reportMonths(int)));
   161     m_pMonthpop->insertItem(trUtf8("N... months"), this, SLOT(reportMonths(int)));
   162     m_pMonthpop->setCheckable(true);
   163     m_pMonthlybutt->setPopup(m_pMonthpop);
   164     m_pMonthlybutt->setPopupDelay(TITRAQ_POPUPMSECS);
   166     { // Initialize check button correctly
   167         int nPosition = (m_nMonths < m_pMonthpop->count()) ? m_nMonths : m_pMonthpop->count();
   168         m_pMonthpop->setItemChecked(m_pMonthpop->idAt(--nPosition), true);
   169     }
   171     // Add our tool buttons
   172     m_pWeekmonthlay->addWidget(m_pWeeklybutt);
   173     m_pWeekmonthlay->addWidget(m_pMonthlybutt);
   174     m_pToolay->addWidget(m_pWeekmonthgroup);
   176     // Push button suite
   177     m_pSavebutt = new QPushButton(this, "Savebutton");
   178     m_pSavebutt->setPaletteBackgroundColor(QColor(202, 194, 182));
   179     m_pSavebutt->setCursor(QCursor(Qt::PointingHandCursor));
   180     m_pPrintbutt = new QPushButton(this, "Printbutton");
   181     m_pPrintbutt->setPaletteBackgroundColor(QColor(198, 196, 186));
   182     m_pPrintbutt->setCursor(QCursor(Qt::PointingHandCursor));
   183 #ifdef QT_NO_PRINTER
   184     m_pPrintbutt->setEnabled(false);
   185 #endif
   186     m_pDismissbutt = new QPushButton(this, "Dismissbutton");
   187     m_pDismissbutt->setPaletteBackgroundColor(QColor(198, 196, 186));
   188     m_pDismissbutt->setCursor(QCursor(Qt::PointingHandCursor));
   189     m_pDismissbutt->setDefault(true);
   190     m_pPushlay->addWidget(m_pSavebutt);
   191     m_pPushlay->addWidget(m_pPrintbutt);
   192     m_pPushlay->addWidget(m_pDismissbutt);
   194     // Connect signals to slots, accept() and reject() are Qt implicit
   195     connect(m_pWeeklybutt, SIGNAL(clicked(void)), SLOT(reportWeeks(void)));
   196     connect(m_pMonthlybutt, SIGNAL(clicked(void)), SLOT(reportMonths(void)));
   197     connect(m_pSavebutt, SIGNAL(clicked(void)), SLOT(saveReport(void)));
   198     connect(m_pPrintbutt, SIGNAL(clicked(void)), SLOT(printReport(void)));
   199     connect(m_pDismissbutt, SIGNAL(clicked(void)), SLOT(accept(void)));
   200     this->resize(QSize(464, 332).expandedTo(minimumSizeHint()));
   201     this->textChange();
   202 }
   204 //
   205 // Overload QDialog::exec(), and generate a report before executing modally
   206 //
   207 int Reportpanel::exec(void)
   208 {
   209     int nRet = 0;
   210     int nPeriod = m_pReprefs->getNumber(TITRAQ_PREFREPORTYPE, TITRAQ_DEFREPORTYPE);
   212     switch (nPeriod) {
   213     case TITRAQ_REPORTWEEK:
   214         this->reportWeeks(1);
   215         m_pWeekmonthgroup->setButton(TITRAQ_REPORTWEEK);
   216         break;
   217     case TITRAQ_REPORTMONTH:
   218         this->reportMonths(1);
   219         m_pWeekmonthgroup->setButton(TITRAQ_REPORTMONTH);
   220         break;
   221     default:
   222         throw Genexcept("Reportpanel: Modal event loop entered with no report period.");
   223         break;
   224     }
   226     nRet = QDialog::exec(); // Blast off
   228     // Before losing scope, the preferred report period
   229     m_pReprefs->setNumber(TITRAQ_PREFREPORTYPE,
   230         m_pWeekmonthgroup->id(m_pWeekmonthgroup->selected()));
   231     m_pReprefs->setNumber(TITRAQ_PREFREPORTWEEKS, m_nWeeks);
   232     m_pReprefs->setNumber(TITRAQ_PREFREPORTMONTHS, m_nMonths);
   233     return nRet;
   234 }
   236 //
   237 // Makes a new weekly report of so many weeks
   238 //
   239 void Reportpanel::reportWeeks(int nMenuid)
   240 {
   241     // Range of weeks to report
   242     int nFirstweek, nLastweek;
   244     // Menu index, first item is always 0
   245     int nIndex = m_pWeekpop->indexOf(nMenuid);      // Corresponds to weeks
   246     int nLastid = 0;                                // Last menu id selected
   248     if (m_nWeeks < m_pWeekpop->count())
   249         nLastid = m_pWeekpop->idAt(m_nWeeks - 1);   // Probably not last item
   250     else
   251         nLastid = m_pWeekpop->idAt(m_pWeekpop->count() - 1); // Last item selected
   253     // Set the button in case from a menu selection
   254     m_pWeekmonthgroup->setButton(TITRAQ_REPORTWEEK);
   256     // Update m_nWeeks only if user prefers a different number
   257     if (nIndex >= 0) {
   258         // User selected N... to indicate an arbitrary number
   259         if (nIndex == m_pWeekpop->count() - 1) {
   260             Numdial Weeksinput;
   261             Weeksinput.setNum(m_nWeeks);
   262             if (Weeksinput.exec() == QDialog::Accepted) {
   263                 m_nWeeks = Weeksinput.getNum();
   264                 m_pWeekpop->setItemChecked(nLastid, false);
   265                 m_pWeekpop->setItemChecked(nMenuid, true);
   266             }
   267             else
   268                 return;
   269         }
   270         else { // User selected a preset menu item such as '2 weeks'
   271             m_nWeeks = nIndex + 1;
   272             m_pWeekpop->setItemChecked(nLastid, false);
   273             m_pWeekpop->setItemChecked(nMenuid, true);
   274         }
   275     }
   277     // Clear data window
   278     m_pBrowser->setUpdatesEnabled(false);
   279     m_pBrowser->clear();
   281     // Determine first and last week numbers, then write header
   282     nLastweek = QDate::currentDate().weekNumber();
   283     if (m_nWeeks > 1) {
   284         nFirstweek = QDate::currentDate().addDays(m_nWeeks * -7).weekNumber() + 1;
   285         this->writeHeader(nFirstweek, nLastweek);
   286     }
   287     else
   288         this->writeHeader(nLastweek);
   290     // Write new contents to data window
   291     m_pBrowser->append(this->getWeektotals(QDate::currentDate(), m_nWeeks));
   292     if (m_pReprefs->getBool(TITRAQ_PREFDETAILON, TITRAQ_DEFDETAILON))
   293         m_pBrowser->append(this->getWeekdetails(QDate::currentDate(), m_nWeeks));
   294     this->writeFooter();
   295     m_pBrowser->setCursorPosition(0, 0);
   296     m_pBrowser->ensureCursorVisible();
   297     m_pBrowser->setUpdatesEnabled(true);
   298     m_pBrowser->repaint(false);
   299 }
   301 //
   302 // Makes a new monthly report of so many months
   303 //
   304 void Reportpanel::reportMonths(int nMenuid)
   305 {
   306     // Range of months to report
   307     QString Firstmonth, Lastmonth;
   309     // Menu index, first item is always 0
   310     int nIndex = m_pMonthpop->indexOf(nMenuid);     // Corresponds to months
   311     int nLastid = 0;                                // Last menu id selected
   313     if (m_nMonths < m_pMonthpop->count())
   314         nLastid = m_pMonthpop->idAt(m_nMonths - 1); // Probably not last item
   315     else
   316         nLastid = m_pMonthpop->idAt(m_pMonthpop->count() - 1); // Last item selected
   318     // Set the button in case from a menu selection
   319     m_pWeekmonthgroup->setButton(TITRAQ_REPORTMONTH);
   321     // Update m_nMonths only if user prefers a different number
   322     if (nIndex >= 0) {
   323         // User selected N... to indicate an arbitrary number
   324         if (nIndex == m_pMonthpop->count() - 1) {
   325             Numdial Monthsinput;
   326             Monthsinput.setNum(m_nMonths);
   327             if (Monthsinput.exec() == QDialog::Accepted) {
   328                 m_nMonths = Monthsinput.getNum();
   329                 m_pMonthpop->setItemChecked(nLastid, false);
   330                 m_pMonthpop->setItemChecked(nMenuid, true);
   331             }
   332             else
   333                 return;
   334         }
   335         else { // User selected a preset menu item such as '2 months'
   336             m_nMonths = nIndex + 1;
   337             m_pMonthpop->setItemChecked(nLastid, false);
   338             m_pMonthpop->setItemChecked(nMenuid, true);
   339         }
   340     }
   342     // Clear data window
   343     m_pBrowser->setUpdatesEnabled(false);
   344     m_pBrowser->clear();
   346     // Determine first and last month names, then write header
   347     Lastmonth = QDate::longMonthName(QDate::currentDate().month());
   348     if (m_nMonths > 1) {
   349         int nMonth = (QDate::currentDate().addMonths(m_nMonths * -1).month() + 12) % 12 + 1;
   350         Firstmonth = QDate::longMonthName(nMonth);
   351         this->writeHeader(Firstmonth, Lastmonth);
   352     }
   353     else
   354         this->writeHeader(Lastmonth);
   356     // Write new contents to data window
   357     m_pBrowser->append(this->getMonthtotals(QDate::currentDate(), m_nMonths));
   358     if (m_pReprefs->getBool(TITRAQ_PREFDETAILON, TITRAQ_DEFDETAILON)) {
   359         m_pBrowser->append(this->getMonthdetails(QDate::currentDate(), m_nMonths));
   360     }
   361     this->writeFooter();
   362     m_pBrowser->setCursorPosition(0, 0);
   363     m_pBrowser->ensureCursorVisible();
   364     m_pBrowser->setUpdatesEnabled(true);
   365     m_pBrowser->repaint(false);
   366 }
   368 //
   369 // Writes a report header to the display window
   370 //
   371 void Reportpanel::writeHeader(int nWeeknum)
   372 {
   373     QString Header;
   374     Header = QString("Accounting System ");
   375     Header += QString(asgui_version.v_short);
   376     Header += trUtf8("\nLocal report, username '");
   377     Header += m_pReprefs->getString(TITRAQ_PREFUSER, TITRAQ_DEFUSER);
   378     Header += QString("'\n");
   379     Header += trUtf8("Date ");
   380     Header += QDate::currentDate().toString(Qt::ISODate);
   381     Header += trUtf8(", Time ");
   382     Header += QTime::currentTime().toString(Qt::ISODate);
   383     Header += trUtf8("\nReporting for week %1...").arg(nWeeknum);
   384     Header += QString("\n\n");
   385     m_pBrowser->setText(Header);
   386 }
   388 //
   389 // Writes a report header to the display window
   390 //
   391 void Reportpanel::writeHeader(QString Month)
   392 {
   393     QString Header;
   394     Header = QString("Accounting System ");
   395     Header += QString(asgui_version.v_short);
   396     Header += trUtf8("\nLocal report, username '");
   397     Header += m_pReprefs->getString(TITRAQ_PREFUSER, TITRAQ_DEFUSER);
   398     Header += QString("'\n");
   399     Header += trUtf8("Date ");
   400     Header += QDate::currentDate().toString(Qt::ISODate);
   401     Header += trUtf8(", Time ");
   402     Header += QTime::currentTime().toString(Qt::ISODate);
   403     Header += trUtf8("\nReporting for month %1...").arg(Month);
   404     Header += QString("\n\n");
   405     m_pBrowser->setText(Header);
   406 }
   408 //
   409 // Writes a report header to the display window
   410 //
   411 void Reportpanel::writeHeader(int nFirst, int nLast)
   412 {
   413     QString Header;
   414     Header = QString("Accounting System ");
   415     Header += QString(asgui_version.v_short);
   416     Header += trUtf8("\nLocal report, username '");
   417     Header += m_pReprefs->getString(TITRAQ_PREFUSER, TITRAQ_DEFUSER);
   418     Header += QString("'\n");
   419     Header += trUtf8("Date ");
   420     Header += QDate::currentDate().toString(Qt::ISODate);
   421     Header += trUtf8(", Time ");
   422     Header += QTime::currentTime().toString(Qt::ISODate);
   423     Header += trUtf8("\nReporting for weeks %1 through %2...").arg(nFirst).arg(nLast);
   424     Header += QString("\n\n");
   425     m_pBrowser->setText(Header);
   426 }
   428 //
   429 // Writes a report header to the display window
   430 //
   431 void Reportpanel::writeHeader(QString First, QString Last)
   432 {
   433     QString Header;
   434     Header = QString("Accounting System ");
   435     Header += QString(asgui_version.v_short);
   436     Header += trUtf8("\nLocal report, username '");
   437     Header += m_pReprefs->getString(TITRAQ_PREFUSER, TITRAQ_DEFUSER);
   438     Header += QString("'\n");
   439     Header += trUtf8("Date ");
   440     Header += QDate::currentDate().toString(Qt::ISODate);
   441     Header += trUtf8(", Time ");
   442     Header += QTime::currentTime().toString(Qt::ISODate);
   443     Header += trUtf8("\nReporting months %1 through %2...").arg(First).arg(Last);
   444     Header += QString("\n\n");
   445     m_pBrowser->setText(Header);
   446 }
   448 //
   449 // Writes a report footer to the display window
   450 //
   451 void Reportpanel::writeFooter(void)
   452 {
   453     if (m_pReprefs->getBool(TITRAQ_PREFSIGNATON, TITRAQ_DEFSIGNATON)) {
   454         m_pBrowser->append(trUtf8("\n\nPrint name ________________________________________\n"));
   455         m_pBrowser->append(trUtf8("\n\nSignature  ________________________________________"));
   456     }
   457 }
   459 //
   460 // Returns one week of details data
   461 //
   462 QString Reportpanel::getWeekdetails(QDate Dayref, int nWeeks)
   463 {
   464     QString Data;       // Output string
   465     QString Tempstring; // Temp churning
   466     int nRows = m_pReptable->numRows();
   468     // Find range boundaries
   469     Q_ASSERT(nWeeks > 0);
   470     QDate Tempdate;
   471     QDate Firstday;
   472     QDate Lastday;
   473     Firstday = Dayref.addDays(Dayref.dayOfWeek() * -1 + 1);
   474     Firstday = Firstday.addDays((nWeeks - 1) * -7);
   475     Lastday  = Firstday.addDays(7 * nWeeks);
   477     // Write some quick header text to the outgoing string
   478     Data  = trUtf8("\nDate       Hours Account details\n");
   479     Data += QString("---------- ----- ----------------------------------------\n");
   481     // Build the long week data string
   482     for (int nIter = nRows - 1; nIter >= 0; nIter--) {
   483         // Work on this tuple only if it within the reporting range
   484         Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   485         Tempdate = QDate::fromString(m_pReptable->text(nIter, TITRAQ_IDXDATE), Qt::ISODate);
   486         if (!Tempstring.isEmpty() && Firstday <= Tempdate && Tempdate < Lastday) {
   487             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXDATE);
   488             if (!Tempstring.isEmpty())
   489                 Data += Tempstring;
   490             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXAMOUNT);
   491             if (!Tempstring.isEmpty())
   492                 Data += QString(TITRAQ_SEPARATORTOK) + Tempstring;
   493             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   494             if (!Tempstring.isEmpty())
   495                 Data += QString(TITRAQ_SEPARATORTOK) + Tempstring;
   496             Data += QString("\n"); // Finish off line
   497         }
   498     }
   500     return Data;
   501 }
   503 //
   504 // Returns one week of totals data
   505 //
   506 QString Reportpanel::getWeektotals(QDate Dayref, int nWeeks)
   507 {
   508     using namespace std;                            // Needed for maps and hash
   509     map<string, string> Hashtasks;                  // Hashtable to store tasks
   510     multimap<string, string> Hashhours;             // Hashtable to store hours
   511     map<string, string>::iterator Taskiter;         // Hashtable task iterator
   512     multimap<string, string>::iterator Houriter;    // Hashtable hour iterator
   514     QString Totals;                         // Data totals to return for writing
   515     QString Tempstring;                     // Temporary processing string
   516     QDate   Tempdate;                       // Temporary processing date
   517     int     nRows = m_pReptable->numRows();
   519     // Find range boundaries
   520     Q_ASSERT(nWeeks > 0);
   521     QDate Firstday;
   522     QDate Lastday;
   523     Firstday = Dayref.addDays(Dayref.dayOfWeek() * -1 + 1);
   524     Firstday = Firstday.addDays((nWeeks - 1) * -7);
   525     Lastday  = Firstday.addDays(7 * nWeeks);
   527     // Parse the table for interesting values
   528     for (int nIter = 0; nIter < nRows; nIter++) {
   529         // Work on this tuple only if it within the reporting range
   530         Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   531         Tempdate = QDate::fromString(m_pReptable->text(nIter, TITRAQ_IDXDATE), Qt::ISODate);
   532         if (!Tempstring.isEmpty() && Firstday <= Tempdate && Tempdate < Lastday) {
   533             string Convstring = Tempstring.toStdString(); // Convert to string (can't cast?)
   534             QTime Intable = QTime::fromString(m_pReptable->text(nIter, TITRAQ_IDXAMOUNT), Qt::ISODate);
   535             string Gethashed = Hashtasks[Convstring];
   536             int nTothours = QString::fromStdString(Gethashed).section(':', 0, 0).toInt() + Intable.hour();;
   537             int nTotminut = QString::fromStdString(Gethashed).section(':', 1, 1).toInt() + Intable.minute();
   538             nTothours += nTotminut / 60;
   539             nTotminut %= 60;
   540             QString Hours = QString::number(nTothours).rightJustify(3, ' ');
   541             QString Mins  = QString::number(nTotminut).rightJustify(2, '0');
   542             string Timestring = Hours.toStdString() + ':' + Mins.toStdString(); // Convert to string (can't cast?)
   543             Hashtasks[Convstring] = Timestring;     // Finally store in hashtable
   544         }
   545     }
   547     // Reverse copy the tasks hashtable to both sort and index by amount key
   548     for (Taskiter = Hashtasks.begin(); Taskiter != Hashtasks.end(); Taskiter++)
   549         Hashhours.insert(pair<string, string>(Taskiter->second, Taskiter->first));
   551     // Write the actual data totals to the outgoing string in reverse order
   552     for (Houriter = Hashhours.begin(); Houriter != Hashhours.end(); Houriter++)
   553         Totals = QString::fromStdString(Houriter->first + ' ' + Houriter->second + '\n' + Totals.toStdString());
   555     // Write some quick header text to the outgoing string in reverse order
   556     Totals = QString("------ ----------------------------------------\n") + Totals;
   557     Totals = trUtf8("Total  Account summary\n") + Totals;
   559     return Totals; // The outgoing string... its finally gone, let's go to bed
   560 }
   562 //
   563 // Returns one month of details data
   564 //
   565 QString Reportpanel::getMonthdetails(QDate Dayref, int nMonths)
   566 {
   567     QString Data;       // Output string
   568     QString Tempstring; // Temp churning
   569     QDate Tempdate;     // For comparing
   570     QDate Firstday;     // Minimum boundary
   571     QDate Lastday;      // Maximum boundary
   572     int nRows = m_pReptable->numRows();
   574     // Find range boundaries
   575     Q_ASSERT(nMonths > 0);
   576     int nYear = Dayref.year();
   577     int nMonth = (Dayref.addMonths(nMonths * -1).month() + 12) % 12 + 1;
   578     if (nMonth > Dayref.month())
   579         nYear--;
   580     Firstday = QDate(nYear, nMonth, 1);
   581     Lastday  = QDate(Dayref.year(), Dayref.month(), Dayref.daysInMonth());
   583     // Write some quick header text to the outgoing string
   584     Data  = trUtf8("\nDate       Hours Account details\n");
   585     Data += QString("---------- ----- ----------------------------------------\n");
   587     // Build the long week data string
   588     for (int nIter = nRows - 1; nIter >= 0; nIter--) {
   589         // Work on this tuple only if it within the reporting range
   590         Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   591         Tempdate = QDate::fromString(m_pReptable->text(nIter, TITRAQ_IDXDATE), Qt::ISODate);
   592         if (!Tempstring.isEmpty() && Firstday <= Tempdate && Tempdate < Lastday) {
   593             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXDATE);
   594             if (!Tempstring.isEmpty())
   595                 Data += Tempstring;
   596             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXAMOUNT);
   597             if (!Tempstring.isEmpty())
   598                 Data += QString(TITRAQ_SEPARATORTOK) + Tempstring;
   599             Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   600             if (!Tempstring.isEmpty())
   601                 Data += QString(TITRAQ_SEPARATORTOK) + Tempstring;
   602             Data += trUtf8("\n"); // Finish off line
   603         }
   604     }
   606     return Data;
   607 }
   609 //
   610 // Returns one month of totals data
   611 //
   612 QString Reportpanel::getMonthtotals(QDate Dayref, int nMonths)
   613 {
   614     using namespace std;                            // Needed for maps and hash
   615     map<string, string> Hashtasks;                  // Hashtable to store tasks
   616     multimap<string, string> Hashhours;             // Hashtable to store hours
   617     map<string, string>::iterator Taskiter;         // Hashtable task iterator
   618     multimap<string, string>::iterator Houriter;    // Hashtable hour iterator
   620     QString Totals;                         // Data totals to return for writing
   621     QString Tempstring;                     // Temporary processing string
   622     QDate   Tempdate;                       // Temporary processing date
   623     int     nRows = m_pReptable->numRows();
   625     // Find range boundaries
   626     Q_ASSERT(nMonths > 0);
   627     int nYear = Dayref.year();
   628     int nMonth = (Dayref.addMonths(nMonths * -1).month() + 12) % 12 + 1;
   629     if (nMonth > Dayref.month())
   630         nYear--;
   631     QDate Firstday = QDate(nYear, nMonth, 1);
   632     QDate Lastday  = QDate(Dayref.year(), Dayref.month(), Dayref.daysInMonth());
   634     // Parse the table for interesting values
   635     for (int nIter = 0; nIter < nRows; nIter++) {
   636         // Work on this tuple only if it within the reporting range
   637         Tempstring = m_pReptable->text(nIter, TITRAQ_IDXTASK);
   638         Tempdate = QDate::fromString(m_pReptable->text(nIter, TITRAQ_IDXDATE), Qt::ISODate);
   639         if (!Tempstring.isEmpty() && Firstday <= Tempdate && Tempdate < Lastday) {
   640             string Convstring = Tempstring.toStdString();  // Convert to string (can't cast?)
   641             QTime Intable = QTime::fromString(m_pReptable->text(nIter, TITRAQ_IDXAMOUNT), Qt::ISODate);
   642             string Gethashed = Hashtasks[Convstring];
   643             int nTothours = QString::fromStdString(Gethashed).section(':', 0, 0).toInt() + Intable.hour();;
   644             int nTotminut = QString::fromStdString(Gethashed).section(':', 1, 1).toInt() + Intable.minute();
   645             nTothours += nTotminut / 60;
   646             nTotminut %= 60;
   647             QString Hours = QString::number(nTothours).rightJustify(3, ' ');
   648             QString Mins  = QString::number(nTotminut).rightJustify(2, '0');
   649             QString Timestring = Hours + ':' + Mins; // Convert to string (can't cast?)
   650             Hashtasks[Convstring] = Timestring.toStdString();  // Finally store in hashtable
   651         }
   652     }
   654     // Reverse copy the tasks hashtable to both sort and index by amount key
   655     for (Taskiter = Hashtasks.begin(); Taskiter != Hashtasks.end(); Taskiter++)
   656         Hashhours.insert(pair<string, string>(Taskiter->second, Taskiter->first));
   658     // Write the actual data totals to the outgoing string in reverse order
   659     for (Houriter = Hashhours.begin(); Houriter != Hashhours.end(); Houriter++)
   660         Totals = QString::fromStdString(Houriter->first + ' ' + Houriter->second + '\n' + Totals.toStdString());
   662     // Write some quick header text to the outgoing string in reverse order
   663     Totals = QString("------ ----------------------------------------\n") + Totals;
   664     Totals = trUtf8("Total  Account summary\n") + Totals;
   666     return Totals; // The outgoing string... its finally gone, let's go to bed
   667 }
   669 //
   670 // Saves the currently displayed local report
   671 //
   672 void Reportpanel::saveReport(void)
   673 {
   674     int nResult = 0;        // For checking user's answer
   675     QFile Report;           // The resulting report file
   676     QString Filestring;     // The user chosen file name to save
   677     QString Openas = m_pReprefs->getString(TITRAQ_PREFHOME, TITRAQ_DEFHOME);
   679     // And then get the name of the selected file to save to
   680     Filestring = Q3FileDialog::getSaveFileName(Openas, trUtf8("Text files (*.txt);;All Files (*)"), this, trUtf8("ChooserDialog"), trUtf8("Choose a file to save"), NULL, false);
   681     if (!Filestring.isEmpty()) {
   682         if (QFile::exists(Filestring)) {
   683             nResult = QMessageBox::warning(this, QString(TITRAQ_APPTITLE)
   684                     + ' ' + asgui_version.v_short, trUtf8(TITRAQ_OVERWRITE),
   685                       trUtf8("&Yes"), trUtf8("&No"), NULL, 1, 1);
   686             if (nResult = QMessageBox::Ok) {                // Overwrite a file
   687                 Report.setName(Filestring);                 // User wished name
   688                 Report.open(QIODevice::WriteOnly | QIODevice::Truncate);    // Open report file
   689                 Q3TextStream Outstream(&Report);             // Convert to stream
   690                 Outstream << m_pBrowser->text();            // Write the data
   691                 Report.close();                             // Finish by closing
   692             }
   693         }
   694         else { // User gave a file name, and there was no preexisting file there
   695             Report.setName(Filestring);                 // User wished name
   696             Report.open(QIODevice::WriteOnly | QIODevice::Truncate);    // Open report file
   697             Q3TextStream Outstream(&Report);             // Convert to stream
   698             Outstream << m_pBrowser->text();            // Write the data
   699             Report.close();                             // Finish by closing
   700         }
   701     }
   702     // Otherwise, user did not select a valid file and push okay button
   703 }
   705 //
   706 // Prints the currently displayed local report
   707 //
   708 void Reportpanel::printReport(void)
   709 {
   710 #ifndef QT_NO_PRINTER
   711     if (m_pPrinter->setup(this)) {      // Opens printer dialog
   712         m_pPrinter->setFullPage(true);  // Set our own margins
   713         QPainter Paint;                 // Our painter (for pages)
   714         Paint.begin(m_pPrinter);        // Paint on printer
   715         Paint.setFont(m_pBrowser->font());
   716         QFontMetrics Fontmetrics = Paint.fontMetrics();
   717         Q3PaintDeviceMetrics Devmetrics(m_pPrinter); // Need width/height of printer surface
   718         const int knMargin = Devmetrics.logicalDpiX() / 2;  // Half-inch margin
   719         int nYpos   = knMargin;                             // Y position for each line
   720         int nPageno = 1;                                    // The starting page number
   722         for (int nIter = 0; nIter < m_pBrowser->lines() && !m_pPrinter->aborted(); nIter++) {
   723             // See if there is some space on this page to paint on
   724             if (nYpos + Fontmetrics.lineSpacing() > Devmetrics.height() - knMargin) {
   725 //                QString Printmsg(trUtf8("Printing page "))
   726 //                    + QString::number(++nPageno) + QString("...");
   727 //                m_pStatus()->message(Printmsg);             // Not in scope (crap)
   728                 if (!m_pPrinter->newPage()) // Start new page
   729                     break;                  // Some error
   730                 nYpos = knMargin;           // Back to top of page
   731             }
   732             Paint.drawText(knMargin, nYpos, Devmetrics.width() - 2 * knMargin,
   733                 Fontmetrics.lineSpacing(), Qt::ExpandTabs, m_pBrowser->text(nIter));
   734             nYpos += Fontmetrics.lineSpacing();
   735         }
   736         Paint.end(); // Send job to printer
   737 //        m_pStatus()->message(trUtf8("Printing completed"), 2000); // Not in scope
   738     }
   739 //    m_pStatusBar()->message(trUtf8("Printing completed"), 2000);  // Not in scope
   740 #endif
   741 }
   743 //
   744 // Sets the strings of the subwidgets using the current language
   745 //
   746 void Reportpanel::textChange(void)
   747 {
   748     this->setCaption(trUtf8("AS local report", "Local report using weekly or monthly data."));
   750     // Top level push buttons associated with accept and save slots
   751     m_pSavebutt->setText(trUtf8("Save As...", "Comment for Savebutton"));
   752     QToolTip::add(m_pSavebutt, trUtf8("Saves the report text", "Comment for tooltip Savebutton"));
   753     Q3WhatsThis::add(m_pSavebutt, trUtf8("The save button saves the report text to a file", "Comment for whatsThis Savebutton"));
   754     m_pPrintbutt->setText(trUtf8("Print...", "Comment for Printbutton"));
   755     QToolTip::add(m_pPrintbutt, trUtf8("Print the report text", "Comment for tooltip Printbutton"));
   756     Q3WhatsThis::add(m_pPrintbutt, trUtf8("The print button prints the report text to a file", "Comment for whatsThis Printbutton"));
   757     m_pDismissbutt->setText(trUtf8("Dismiss", "Comment for Dismissbutton"));
   758     QToolTip::add(m_pDismissbutt, trUtf8("Closes the report panel", "Comment for tooltip Dismissbutton"));
   759     Q3WhatsThis::add(m_pDismissbutt, trUtf8("The dismiss button dismisses the report panel", "Comment for whatsThis Dismissbutton"));
   761     // Inner tool buttons for new local report generation
   762     m_pWeeklybutt->setText(trUtf8("Weekly", "Comment for Weeklybutt"));
   763     QToolTip::add(m_pWeeklybutt, trUtf8("Hold down for options", "Comment for tooltip Weeklybutt"));
   764     Q3WhatsThis::add(m_pWeeklybutt, trUtf8("The Weekly button generates a new weekly report.\nHold this button down to specify many weeks.", "Comment for whatsThis Weeklybutt"));
   765     m_pMonthlybutt->setText(trUtf8("Monthly", "Comment for Monthlybutt"));
   766     QToolTip::add(m_pMonthlybutt, trUtf8("Hold down for options", "Comment for tooltip Monthlybutt"));
   767     Q3WhatsThis::add(m_pMonthlybutt, trUtf8("The Monthly button makes a new monthly report.\nHold this button down to specify how many months.", "Comment for whatsThis Monthlybutt"));
   768 }
   769 } // namespace AS

mercurial