as_reportpanel.cpp

Fri, 28 Nov 2008 11:21:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 28 Nov 2008 11:21:08 +0100
changeset 1
d64aaa7d146f
child 3
c1941114ca88
permissions
-rw-r--r--

Fork project trunk at version 0.7.7 to maintain for future usage.
The original Cable & Wireless development team no longer exists.
The OSSP CVS repository is unavailable (cvs co ossp-pkg/as fails),
however http://cvs.ossp.org/dirview?d=ossp-pkg/as/as-gui is working.

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

mercurial