as_reportpanel.cpp

Wed, 07 Jan 2009 18:49:25 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 07 Jan 2009 18:49:25 +0100
changeset 12
ceb4ba3d2d00
parent 3
c1941114ca88
child 14
1c58588f9cb6
permissions
-rw-r--r--

Bump copyright year number.

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

mercurial