Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

qashmoney.cpp

Go to the documentation of this file.
00001 #include "qashmoney.h"
00002 #include "preferencedialogs.h"
00003 #include "memorydialog.h"
00004 
00005 #include <qheader.h>
00006 
00007 Budget *budget = new Budget ();
00008 Preferences *preferences = new Preferences ();
00009 Account *account = new Account ();
00010 Transaction *transaction = new Transaction ();
00011 Transfer *transfer = new Transfer ();
00012 Memory *memory = new Memory ();
00013 
00014 QashMoney::QashMoney () : QWidget ()
00015   {
00016     preferences->addPreferences ();
00017     preferences->initializeColumnPreferences ();
00018     preferences->initializeSortingPreferences ();
00019 
00020     // set the text in the upper part of the frame
00021     setCaption ( tr ( "QashMoney" ) );
00022 
00023     // Create new menubar for our mainwindow
00024     // and add menu items
00025     mainmenu = new QMenuBar ( this );
00026     mainmenu->setFrameStyle ( QFrame::PopupPanel | QFrame::Raised );
00027     preferencesmenu = new QPopupMenu ( this );
00028     utilitiesmenu = new QPopupMenu ( this );
00029     mainmenu->insertItem ( "Preferences", preferencesmenu );
00030     mainmenu->insertItem ( "Utilities", utilitiesmenu );
00031     preferencesmenu->insertItem ( "Date", this, SLOT ( displayDatePreferencesDialog() ) );
00032     preferencesmenu->insertItem ( "Account", this, SLOT ( displayAccountPreferencesDialog() ) );
00033     preferencesmenu->insertItem ( "Transaction", this, SLOT ( displayTransactionPreferencesDialog() ) );
00034     utilitiesmenu->insertItem ( "Memory", this, SLOT ( displayMemoryDialog() ) );
00035 
00036     // create the main tabwidget for displaying accounts and transactions
00037     maintabs = new QTabWidget ( this );
00038     tab = new QWidget ( this );
00039     tab_2 = new QWidget ( this );
00040     tab_3 = new QWidget ( this );
00041     maintabs->addTab ( tab, "Accounts" );
00042     maintabs->addTab ( tab_2, "Transactions" );
00043     maintabs->addTab ( tab_3, "Budgets" );
00044     tabheight = tab->height();
00045     maintabs->setTabEnabled ( tab_2, FALSE );
00046 
00047     // create a new account display object
00048     accountdisplay = new AccountDisplay ( maintabs );
00049     accountdisplay->setTabs ( tab_2, maintabs );
00050     connect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( setTransactionTab() ) );
00051 
00052     // set the connection to disable the one touch account viewing if we are transfering money
00053     connect ( accountdisplay->transferbutton, SIGNAL ( toggled(bool) ), this, SLOT ( toggleOneTouchViewing(bool) ) );
00054 
00055     // create a new transactiondisplay object
00056     transactiondisplay = new TransactionDisplay ( maintabs );
00057     transactiondisplay->hide();
00058 
00059     // create new budgetdisplay object
00060     budgetdisplay = new BudgetDisplay ( maintabs );
00061     budgetdisplay->hide();
00062 
00063     tabslayout = new QVBoxLayout ( maintabs, 4, 2 );
00064     tabslayout->addSpacing ( tabheight );
00065     tabslayout->addWidget ( accountdisplay );
00066     tabslayout->addWidget ( transactiondisplay );
00067     tabslayout->addWidget ( budgetdisplay );
00068 
00069     // connect a change in the maintabs with changing the tab display
00070     connect ( maintabs, SIGNAL ( currentChanged(QWidget*) ), this, SLOT ( changeTabDisplay() ) );
00071 
00072     // create layout that will contain the menubar and the maintabs
00073     layout = new QVBoxLayout ( this, 2, 2 );
00074     layout->setMenuBar ( mainmenu );
00075     layout->addWidget ( maintabs );
00076   }
00077 
00078 QashMoney::~QashMoney ()
00079   {
00080     delete budget;
00081     delete preferences;
00082     delete account;
00083     delete transaction;
00084     delete transfer;
00085     delete memory;
00086   }
00087 
00088 void QashMoney::changeTabDisplay ()
00089   {
00090     // if the user pressed the transactions tab, hide the account display
00091     // object and create a new transaction display
00092     if ( maintabs->currentPageIndex() == 1 )
00093       {
00094         // initialize variables
00095         bool children = FALSE;
00096 
00097         // hide the account display and define accountid
00098         int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();
00099 
00100         //remove all the columns from the transactiondisplay
00101         int columns = transactiondisplay->listview->columns();
00102         int counter;
00103         for ( counter = 0; counter <= columns; counter++ )
00104           transactiondisplay->listview->removeColumn ( 0 );
00105 
00106         // set the account name and account balance
00107         QString name = account->getAccountName ( accountid );
00108         QString balance = account->getAccountBalance ( accountid );
00109         transactiondisplay->name->setText ( name );
00110         transactiondisplay->balance->setText ( balance );
00111 
00112         // clear the limitbox
00113         transactiondisplay->limitbox->clear();
00114 
00115         // get parent account id
00116         int parentaccountid = account->getParentAccountID ( accountid );
00117 
00118         // add columns based on which account is selected
00119         // this first if determines if we selected a parent with no children or a child
00120         // in these cases, we add standard three columns for date, transaction, amount
00121         transactiondisplay->listview->addColumn ( "Date", 0 );
00122         transactiondisplay->listview->addColumn ( "Transaction", 0 );
00123         transactiondisplay->listview->addColumn ( "Amt", 0);
00124         transactiondisplay->listview->setColumnAlignment ( 2, Qt::AlignRight );
00125         transactiondisplay->listview->addColumn ( "", 0 );
00126 
00127         if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 ) // we selected a parent with children
00128           {
00129             // add an extra column for the account name for eac child transaction
00130             transactiondisplay->listview->addColumn ( "Acct", 0 );
00131             children = TRUE;
00132 
00133             // hide the new transaction button
00134             transactiondisplay->newtransaction->setEnabled ( FALSE );
00135           }
00136         else //we selected a parent without children or a child
00137           transactiondisplay->newtransaction->setEnabled ( TRUE );
00138 
00139         // disable the transactionid column so it can't be red
00140         transactiondisplay->listview->header()->setResizeEnabled ( FALSE, 3 );
00141 
00142         // set the accountid and children variables
00143         transactiondisplay->setChildren ( children );
00144         transactiondisplay->setAccountID ( accountid );
00145 
00146         setTransactionDisplayDate ();
00147 
00148         // display transactions
00149         transactiondisplay->listview->clear();
00150         QString displaytext = "%";
00151         displaytext.prepend ( transactiondisplay->limitbox->text() );
00152         if ( transaction->getNumberOfTransactions() > 0 )
00153           transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );
00154 
00155         // display transfers
00156         transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );
00157 
00158         // open a new preferences object and resize the transaction display columns
00159         // each column will have a different size based on whether we are looking at a child
00160         // account or children through a parent
00161         if ( parentaccountid != -1 || accountdisplay->listview->selectedItem()->childCount() == 0 ) // a parent with no children or a child - three columns
00162           {
00163             transactiondisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 3 ) ); // normal transaction date width
00164             transactiondisplay->listview->setColumnWidthMode ( 0, QListView::Manual );
00165             transactiondisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 4 ) ); // normal transaction name width
00166             transactiondisplay->listview->setColumnWidthMode ( 1, QListView::Manual );
00167             transactiondisplay->listview->setColumnWidth ( 2, preferences->getColumnPreference ( 5 ) ); // normal transaction amount width
00168             transactiondisplay->listview->setColumnWidthMode ( 2, QListView::Manual );
00169           }
00170         else
00171           {
00172             transactiondisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 6 ) ); // extended transaction date width
00173             transactiondisplay->listview->setColumnWidthMode ( 0, QListView::Manual );
00174             transactiondisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 7 ) ); // extended transaction name width
00175             transactiondisplay->listview->setColumnWidthMode ( 1, QListView::Manual );
00176             transactiondisplay->listview->setColumnWidth ( 2, preferences->getColumnPreference ( 8 ) ); // extended transaction amount width
00177             transactiondisplay->listview->setColumnWidthMode ( 2, QListView::Manual );
00178             transactiondisplay->listview->setColumnWidth ( 4, preferences->getColumnPreference ( 9 ) ); // transaction account width
00179             transactiondisplay->listview->setColumnWidthMode ( 4, QListView::Manual );
00180           }
00181         
00182         // pull the column sorting preference from the preferences table, and configure the listview accordingly
00183         int column = 0;
00184         int direction = 0;
00185         preferences->getSortingPreference ( 2, &column, &direction );
00186         transactiondisplay->listview->setSorting ( column, direction );
00187 
00188         // show the window
00189         transactiondisplay->show();
00190         // hide the account display and define accountid
00191         accountdisplay->hide();
00192         // hide the budget display
00193         budgetdisplay->hide();
00194       }
00195     else if ( maintabs->currentPageIndex() == 0 )
00196       {
00197         disableOneTouchViewing();
00198 
00199         // clear the account display selection
00200         accountdisplay->listview->clearSelection();
00201 
00202         // resize the account display columns
00203         accountdisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 1 ) );
00204         accountdisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 2 ) );
00205 
00206         // set sorting preference on account display columns
00207         int column = 0;
00208         int direction = 0;
00209         preferences->getSortingPreference ( 1, &column, &direction );
00210         accountdisplay->listview->setSorting ( column, direction );
00211 
00212         // display the accounts
00213         if ( account->getNumberOfAccounts() != 0 )
00214           account->displayAccounts ( accountdisplay->listview );
00215         maintabs->setTabEnabled ( tab_2, FALSE );
00216 
00217         // set the toggle button
00218         accountdisplay->setToggleButton ();
00219 
00220         // show the account display
00221         accountdisplay->show();
00222 
00223         // hide the transaction display
00224         transactiondisplay->hide();
00225 
00226         // hide the budget display
00227         budgetdisplay->hide();
00228 
00229 
00230         enableOneTouchViewing ();
00231       }
00232     else
00233       {
00234         budgetdisplay->displayLineItems();
00235         budgetdisplay->show();
00236         transactiondisplay->hide();
00237         accountdisplay->hide();
00238       }
00239   }
00240 
00241 void QashMoney::setTransactionTab ()
00242   {
00243     if ( accountdisplay->listview->selectedItem() == 0 )
00244       maintabs->setTabEnabled ( tab_2, FALSE );
00245     else
00246       maintabs->setTabEnabled ( tab_2, TRUE );
00247   }
00248 
00249 void QashMoney::displayDatePreferencesDialog ()
00250   {
00251     // this shows a dialog to set preferences for formatting the date
00252     DatePreferences *pd = new DatePreferences ( this );
00253     pd->exec ();
00254     if ( transactiondisplay->isVisible() )
00255       {
00256         // set the account id
00257         int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();
00258 
00259         // set children so we can let displayTransfers know if there are children for the selected account
00260         bool children;
00261         if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 )
00262           children = TRUE;
00263         else
00264           children = FALSE;
00265 
00266         // redisplay transactions if they are visible incorporating
00267         // any changes to the date format
00268         transactiondisplay->listview->clear();
00269         QString displaytext = "%";
00270         displaytext.prepend ( transactiondisplay->limitbox->text() );
00271 
00272         setTransactionDisplayDate();
00273         if ( transaction->getNumberOfTransactions() > 0 )
00274           transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );
00275 
00276         if ( transfer->getNumberOfTransfers() != 0 )
00277           transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );
00278       }
00279     else if ( accountdisplay->isVisible() )
00280       {
00281         accountdisplay->listview->clearSelection();
00282         maintabs->setTabEnabled ( tab_2, FALSE );
00283       }
00284     else
00285       budgetdisplay->updateBudgetInformation();
00286   }
00287 
00288 void QashMoney::displayTransactionPreferencesDialog ()
00289   {
00290     // display a dialog for setting preferences for transactions
00291     TransactionPreferences *td = new TransactionPreferences ( this );
00292     td->exec ();
00293     if ( transactiondisplay->isVisible() )
00294       {
00295         // set the account id
00296         int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();
00297 
00298         // set children so we can let displayTransfers know if there are children for the selected account
00299         bool children;
00300         if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 )
00301           children = TRUE;
00302         else
00303           children = FALSE;
00304 
00305         // redisplay transactions incorporating any transaction preference changes
00306         transactiondisplay->listview->clear();
00307         QString displaytext = "%";
00308         displaytext.prepend ( transactiondisplay->limitbox->text() );
00309 
00310         setTransactionDisplayDate();
00311         if ( transaction->getNumberOfTransactions() > 0 )
00312           transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );
00313 
00314         if ( transfer->getNumberOfTransfers() != 0 )
00315           transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );
00316       }
00317     else
00318       {
00319         accountdisplay->listview->clearSelection();
00320         maintabs->setTabEnabled ( tab_2, FALSE );
00321       }
00322   }
00323 
00324 void QashMoney::displayAccountPreferencesDialog ()
00325   {
00326     // display a dialog for setting preferences for accounts
00327     AccountPreferences *ap = new AccountPreferences ( this );
00328     ap->exec ();
00329 
00330     if ( accountdisplay->isVisible() && account->getNumberOfAccounts() != 0 )
00331       {
00332         accountdisplay->listview->clear();
00333         account->displayAccounts ( accountdisplay->listview );
00334         accountdisplay->listview->clearSelection();
00335         maintabs->setTabEnabled ( tab_2, FALSE );
00336       }
00337     changeTabDisplay();
00338   }
00339 
00340 void QashMoney::displayMemoryDialog ()
00341   {
00342     // opens a dialog to add, edit and delete memory items
00343     MemoryDialog *md = new MemoryDialog ();
00344     md->exec();
00345   }
00346 
00347 void QashMoney::showTransactions ()
00348   {
00349     maintabs->setCurrentPage ( 1 );
00350   }
00351 
00352 void QashMoney::enableOneTouchViewing ()
00353   {
00354     if ( preferences->getPreference ( 5 ) == 1 )
00355       connect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
00356     else
00357       disconnect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
00358   }
00359 
00360 void QashMoney::disableOneTouchViewing ()
00361   {
00362     disconnect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
00363   }
00364 
00365 void QashMoney::toggleOneTouchViewing ( bool state )
00366   {
00367     if ( state == TRUE )
00368       disableOneTouchViewing();
00369     else
00370       enableOneTouchViewing();
00371   }
00372 
00373 void QashMoney::setTransactionDisplayDate ()
00374   {
00375     // determine how many days of transactions to show
00376     int limittype = preferences->getPreference ( 7 );
00377     if ( limittype != 5 ) // set today's date if we are not showing all transactions
00378       {
00379         QDate today = QDate::currentDate ();
00380         switch ( limittype ) // if we are not showing all transactions
00381           {
00382             case 0:  // viewing two weeks
00383               newdate = today.addDays ( -14 );
00384               break;
00385             case 1:  // viewing one month
00386               newdate = today.addDays ( -30 );
00387               break;
00388             case 2: // three months
00389               newdate = today.addDays ( -90 );
00390               break;
00391             case 3: // six months
00392               newdate = today.addDays ( -180 );
00393               break;
00394             case 4: // one year
00395               newdate = today.addDays ( -365 );
00396               break;
00397           }
00398       }
00399     else
00400       newdate = QDate ( 1900, 1, 1 );
00401   }
00402 

Generated on Sat Nov 5 16:18:11 2005 for OPIE by  doxygen 1.4.2