summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/qashmoney.cpp
blob: 1455eb017151e363586a7027798fb18654dd653b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "qashmoney.h"
#include "preferencedialogs.h"
#include "memorydialog.h"

#include <qheader.h>

Budget *budget = new Budget ();
Preferences *preferences = new Preferences ();
Account *account = new Account ();
Transaction *transaction = new Transaction ();
Transfer *transfer = new Transfer ();
Memory *memory = new Memory ();

QashMoney::QashMoney () : QWidget ()
  {
    preferences->addPreferences ();
    preferences->initializeColumnPreferences ();
    preferences->initializeSortingPreferences ();

    // set the text in the upper part of the frame
    setCaption ( tr ( "QashMoney" ) );

    // Create new menubar for our mainwindow
    // and add menu items
    mainmenu = new QMenuBar ( this );
    mainmenu->setFrameStyle ( QFrame::PopupPanel | QFrame::Raised );
    preferencesmenu = new QPopupMenu ( this );
    utilitiesmenu = new QPopupMenu ( this );
    mainmenu->insertItem ( "Preferences", preferencesmenu );
    mainmenu->insertItem ( "Utilities", utilitiesmenu );
    preferencesmenu->insertItem ( "Date", this, SLOT ( displayDatePreferencesDialog() ) );
    preferencesmenu->insertItem ( "Account", this, SLOT ( displayAccountPreferencesDialog() ) );
    preferencesmenu->insertItem ( "Transaction", this, SLOT ( displayTransactionPreferencesDialog() ) );
    utilitiesmenu->insertItem ( "Memory", this, SLOT ( displayMemoryDialog() ) );

    // create the main tabwidget for displaying accounts and transactions
    maintabs = new QTabWidget ( this );
    tab = new QWidget ( this );
    tab_2 = new QWidget ( this );
    tab_3 = new QWidget ( this );
    maintabs->addTab ( tab, "Accounts" );
    maintabs->addTab ( tab_2, "Transactions" );
    maintabs->addTab ( tab_3, "Budgets" );
    tabheight = tab->height();
    maintabs->setTabEnabled ( tab_2, FALSE );

    // create a new account display object
    accountdisplay = new AccountDisplay ( maintabs );
    accountdisplay->setTabs ( tab_2, maintabs );
    connect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( setTransactionTab() ) );

    // set the connection to disable the one touch account viewing if we are transfering money
    connect ( accountdisplay->transferbutton, SIGNAL ( toggled(bool) ), this, SLOT ( toggleOneTouchViewing(bool) ) );

    // create a new transactiondisplay object
    transactiondisplay = new TransactionDisplay ( maintabs );
    transactiondisplay->hide();

    // create new budgetdisplay object
    budgetdisplay = new BudgetDisplay ( maintabs );
    budgetdisplay->hide();

    tabslayout = new QVBoxLayout ( maintabs, 4, 2 );
    tabslayout->addSpacing ( tabheight );
    tabslayout->addWidget ( accountdisplay );
    tabslayout->addWidget ( transactiondisplay );
    tabslayout->addWidget ( budgetdisplay );

    // connect a change in the maintabs with changing the tab display
    connect ( maintabs, SIGNAL ( currentChanged(QWidget*) ), this, SLOT ( changeTabDisplay() ) );

    // create layout that will contain the menubar and the maintabs
    layout = new QVBoxLayout ( this, 2, 2 );
    layout->setMenuBar ( mainmenu );
    layout->addWidget ( maintabs );
  }

QashMoney::~QashMoney ()
  {
    delete budget;
    delete preferences;
    delete account;
    delete transaction;
    delete transfer;
    delete memory;
  }

void QashMoney::changeTabDisplay ()
  {
    // if the user pressed the transactions tab, hide the account display
    // object and create a new transaction display
    if ( maintabs->currentPageIndex() == 1 )
      {
        // initialize variables
        bool children = FALSE;

        // hide the account display and define accountid
        int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();

        //remove all the columns from the transactiondisplay
        int columns = transactiondisplay->listview->columns();
        int counter;
        for ( counter = 0; counter <= columns; counter++ )
          transactiondisplay->listview->removeColumn ( 0 );

        // set the account name and account balance
        QString name = account->getAccountName ( accountid );
        QString balance = account->getAccountBalance ( accountid );
        transactiondisplay->name->setText ( name );
        transactiondisplay->balance->setText ( balance );

        // clear the limitbox
        transactiondisplay->limitbox->clear();

        // get parent account id
        int parentaccountid = account->getParentAccountID ( accountid );

        // add columns based on which account is selected
        // this first if determines if we selected a parent with no children or a child
        // in these cases, we add standard three columns for date, transaction, amount
        transactiondisplay->listview->addColumn ( "Date", 0 );
        transactiondisplay->listview->addColumn ( "Transaction", 0 );
        transactiondisplay->listview->addColumn ( "Amt", 0);
        transactiondisplay->listview->setColumnAlignment ( 2, Qt::AlignRight );
        transactiondisplay->listview->addColumn ( "", 0 );

        if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 ) // we selected a parent with children
          {
            // add an extra column for the account name for eac child transaction
            transactiondisplay->listview->addColumn ( "Acct", 0 );
            children = TRUE;

            // hide the new transaction button
            transactiondisplay->newtransaction->setEnabled ( FALSE );
          }
        else //we selected a parent without children or a child
          transactiondisplay->newtransaction->setEnabled ( TRUE );

        // disable the transactionid column so it can't be red
        transactiondisplay->listview->header()->setResizeEnabled ( FALSE, 3 );

        // set the accountid and children variables
        transactiondisplay->setChildren ( children );
        transactiondisplay->setAccountID ( accountid );

        setTransactionDisplayDate ();

        // display transactions
        transactiondisplay->listview->clear();
        QString displaytext = "%";
        displaytext.prepend ( transactiondisplay->limitbox->text() );
        if ( transaction->getNumberOfTransactions() > 0 )
          transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );

        // display transfers
        transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );

        // open a new preferences object and resize the transaction display columns
        // each column will have a different size based on whether we are looking at a child
        // account or children through a parent
        if ( parentaccountid != -1 || accountdisplay->listview->selectedItem()->childCount() == 0 ) // a parent with no children or a child - three columns
          {
            transactiondisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 3 ) ); // normal transaction date width
            transactiondisplay->listview->setColumnWidthMode ( 0, QListView::Manual );
            transactiondisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 4 ) ); // normal transaction name width
            transactiondisplay->listview->setColumnWidthMode ( 1, QListView::Manual );
            transactiondisplay->listview->setColumnWidth ( 2, preferences->getColumnPreference ( 5 ) ); // normal transaction amount width
            transactiondisplay->listview->setColumnWidthMode ( 2, QListView::Manual );
          }
        else
          {
            transactiondisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 6 ) ); // extended transaction date width
            transactiondisplay->listview->setColumnWidthMode ( 0, QListView::Manual );
            transactiondisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 7 ) ); // extended transaction name width
            transactiondisplay->listview->setColumnWidthMode ( 1, QListView::Manual );
            transactiondisplay->listview->setColumnWidth ( 2, preferences->getColumnPreference ( 8 ) ); // extended transaction amount width
            transactiondisplay->listview->setColumnWidthMode ( 2, QListView::Manual );
            transactiondisplay->listview->setColumnWidth ( 4, preferences->getColumnPreference ( 9 ) ); // transaction account width
            transactiondisplay->listview->setColumnWidthMode ( 4, QListView::Manual );
          }
	
	// pull the column sorting preference from the preferences table, and configure the listview accordingly
	int column = 0;
	int direction = 0;
	preferences->getSortingPreference ( 2, &column, &direction );
	transactiondisplay->listview->setSorting ( column, direction );

        // show the window
        transactiondisplay->show();
        // hide the account display and define accountid
        accountdisplay->hide();
        // hide the budget display
        budgetdisplay->hide();
      }
    else if ( maintabs->currentPageIndex() == 0 )
      {
        disableOneTouchViewing();

        // clear the account display selection
        accountdisplay->listview->clearSelection();

        // resize the account display columns
        accountdisplay->listview->setColumnWidth ( 0, preferences->getColumnPreference ( 1 ) );
        accountdisplay->listview->setColumnWidth ( 1, preferences->getColumnPreference ( 2 ) );

	// set sorting preference on account display columns
	int column = 0;
	int direction = 0;
	preferences->getSortingPreference ( 1, &column, &direction );
	accountdisplay->listview->setSorting ( column, direction );

        // display the accounts
        if ( account->getNumberOfAccounts() != 0 )
          account->displayAccounts ( accountdisplay->listview );
        maintabs->setTabEnabled ( tab_2, FALSE );

        // set the toggle button
        accountdisplay->setToggleButton ();

        // show the account display
        accountdisplay->show();

        // hide the transaction display
        transactiondisplay->hide();

        // hide the budget display
        budgetdisplay->hide();


        enableOneTouchViewing ();
      }
    else
      {
        budgetdisplay->displayLineItems();
        budgetdisplay->show();
        transactiondisplay->hide();
        accountdisplay->hide();
      }
  }

void QashMoney::setTransactionTab ()
  {
    if ( accountdisplay->listview->selectedItem() == 0 )
      maintabs->setTabEnabled ( tab_2, FALSE );
    else
      maintabs->setTabEnabled ( tab_2, TRUE );
  }

void QashMoney::displayDatePreferencesDialog ()
  {
    // this shows a dialog to set preferences for formatting the date
    DatePreferences *pd = new DatePreferences ( this );
    pd->exec ();
    if ( transactiondisplay->isVisible() )
      {
        // set the account id
        int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();

        // set children so we can let displayTransfers know if there are children for the selected account
        bool children;
        if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 )
          children = TRUE;
        else
          children = FALSE;

        // redisplay transactions if they are visible incorporating
        // any changes to the date format
        transactiondisplay->listview->clear();
        QString displaytext = "%";
        displaytext.prepend ( transactiondisplay->limitbox->text() );

        setTransactionDisplayDate();
        if ( transaction->getNumberOfTransactions() > 0 )
          transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );

        if ( transfer->getNumberOfTransfers() != 0 )
          transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );
      }
    else if ( accountdisplay->isVisible() )
      {
        accountdisplay->listview->clearSelection();
        maintabs->setTabEnabled ( tab_2, FALSE );
      }
    else
      budgetdisplay->updateBudgetInformation();
  }

void QashMoney::displayTransactionPreferencesDialog ()
  {
    // display a dialog for setting preferences for transactions
    TransactionPreferences *td = new TransactionPreferences ( this );
    td->exec ();
    if ( transactiondisplay->isVisible() )
      {
        // set the account id
        int accountid = accountdisplay->listview->selectedItem()->text ( accountdisplay->getIDColumn() ).toInt();

        // set children so we can let displayTransfers know if there are children for the selected account
        bool children;
        if ( accountdisplay->listview->selectedItem()->parent() == 0 && accountdisplay->listview->selectedItem()->childCount() != 0 )
          children = TRUE;
        else
          children = FALSE;

        // redisplay transactions incorporating any transaction preference changes
        transactiondisplay->listview->clear();
        QString displaytext = "%";
        displaytext.prepend ( transactiondisplay->limitbox->text() );

        setTransactionDisplayDate();
        if ( transaction->getNumberOfTransactions() > 0 )
          transaction->displayTransactions ( transactiondisplay->listview, accountid, children, displaytext, newdate );

        if ( transfer->getNumberOfTransfers() != 0 )
          transfer->displayTransfers ( transactiondisplay->listview, accountid, children, newdate );
      }
    else
      {
        accountdisplay->listview->clearSelection();
        maintabs->setTabEnabled ( tab_2, FALSE );
      }
  }

void QashMoney::displayAccountPreferencesDialog ()
  {
    // display a dialog for setting preferences for accounts
    AccountPreferences *ap = new AccountPreferences ( this );
    ap->exec ();

    if ( accountdisplay->isVisible() && account->getNumberOfAccounts() != 0 )
      {
        accountdisplay->listview->clear();
        account->displayAccounts ( accountdisplay->listview );
        accountdisplay->listview->clearSelection();
        maintabs->setTabEnabled ( tab_2, FALSE );
      }
    changeTabDisplay();
  }

void QashMoney::displayMemoryDialog ()
  {
    // opens a dialog to add, edit and delete memory items
    MemoryDialog *md = new MemoryDialog ();
    md->exec();
  }

void QashMoney::showTransactions ()
  {
    maintabs->setCurrentPage ( 1 );
  }

void QashMoney::enableOneTouchViewing ()
  {
    if ( preferences->getPreference ( 5 ) == 1 )
      connect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
    else
      disconnect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
  }

void QashMoney::disableOneTouchViewing ()
  {
    disconnect ( accountdisplay->listview, SIGNAL ( selectionChanged() ), this, SLOT ( showTransactions() ) );
  }

void QashMoney::toggleOneTouchViewing ( bool state )
  {
    if ( state == TRUE )
      disableOneTouchViewing();
    else
      enableOneTouchViewing();
  }

void QashMoney::setTransactionDisplayDate ()
  {
    // determine how many days of transactions to show
    int limittype = preferences->getPreference ( 7 );
    if ( limittype != 5 ) // set today's date if we are not showing all transactions
      {
        QDate today = QDate::currentDate ();
        switch ( limittype ) // if we are not showing all transactions
          {
            case 0:  // viewing two weeks
              newdate = today.addDays ( -14 );
              break;
            case 1:  // viewing one month
              newdate = today.addDays ( -30 );
              break;
            case 2: // three months
              newdate = today.addDays ( -90 );
              break;
            case 3: // six months
              newdate = today.addDays ( -180 );
              break;
            case 4: // one year
              newdate = today.addDays ( -365 );
              break;
          }
      }
    else
      newdate = QDate ( 1900, 1, 1 );
  }