author | allenforsythe <allenforsythe> | 2003-05-04 22:02:48 (UTC) |
---|---|---|
committer | allenforsythe <allenforsythe> | 2003-05-04 22:02:48 (UTC) |
commit | 4593e3cf4eca4867e34b3220007c24523cf642b6 (patch) (unidiff) | |
tree | bd8071834dec2a5008b1b270c7953f5cfd65754d /noncore/apps/qashmoney/budgetdisplay.cpp | |
parent | 2894d0b14c0b3efa3ce259214b5aa597a6abfed1 (diff) | |
download | opie-4593e3cf4eca4867e34b3220007c24523cf642b6.zip opie-4593e3cf4eca4867e34b3220007c24523cf642b6.tar.gz opie-4593e3cf4eca4867e34b3220007c24523cf642b6.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/apps/qashmoney/budgetdisplay.cpp') (more/less context) (show whitespace changes)
-rwxr-xr-x | noncore/apps/qashmoney/budgetdisplay.cpp | 408 |
1 files changed, 408 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/budgetdisplay.cpp b/noncore/apps/qashmoney/budgetdisplay.cpp new file mode 100755 index 0000000..09f62b2 --- a/dev/null +++ b/noncore/apps/qashmoney/budgetdisplay.cpp | |||
@@ -0,0 +1,408 @@ | |||
1 | #include <qmessagebox.h> | ||
2 | #include <qheader.h> | ||
3 | #include <qfont.h> | ||
4 | #include <sqlite.h> | ||
5 | |||
6 | #include "budgetdisplay.h" | ||
7 | #include "budget.h" | ||
8 | #include "newaccount.h" | ||
9 | #include "datepicker.h" | ||
10 | #include "preferences.h" | ||
11 | #include "transaction.h" | ||
12 | |||
13 | extern Preferences *preferences; | ||
14 | extern Budget *budget; | ||
15 | extern Transaction *transaction; | ||
16 | |||
17 | BudgetDisplay::BudgetDisplay ( QWidget *parent ) : QWidget ( parent ) | ||
18 | { | ||
19 | QFont font = this->font(); | ||
20 | font.setWeight ( QFont::Bold ); | ||
21 | |||
22 | //set the default date to today | ||
23 | newDate = QDate::currentDate (); | ||
24 | year = newDate.year(); | ||
25 | month = newDate.month(); | ||
26 | day = newDate.day(); | ||
27 | datelabel = preferences->getDate ( year, month ); | ||
28 | |||
29 | setCaption ( "Budget" ); | ||
30 | |||
31 | firstline = new QHBox ( this ); | ||
32 | firstline->setSpacing ( 2 ); | ||
33 | secondline = new QHBox ( this ); | ||
34 | secondline->setSpacing ( 10 ); | ||
35 | |||
36 | menu = new QPEMenuBar ( this ); | ||
37 | menu->setFrameStyle ( QFrame::Box | QFrame::Sunken ); | ||
38 | budgetmenu = new QPopupMenu ( this ); | ||
39 | lineitemsmenu = new QPopupMenu ( this ); | ||
40 | datemenu = new QPopupMenu ( this ); | ||
41 | menu->insertItem ( "Budget", budgetmenu ); | ||
42 | menu->insertItem ( "Line Item", lineitemsmenu ); | ||
43 | menu->insertItem ( "Date", datemenu ); | ||
44 | budgetmenu->insertItem ( "New", this, SLOT ( newBudget () ), 0, 1 ); | ||
45 | budgetmenu->insertItem ( "Edit", this, SLOT ( editBudget () ), 0, 2 ); | ||
46 | budgetmenu->insertItem ( "Delete", this, SLOT ( deleteBudget () ), 0, 3 ); | ||
47 | lineitemsmenu->insertItem ( "New", this, SLOT ( newLineItem () ), 0, 1 ); | ||
48 | lineitemsmenu->insertItem ( "Edit", this, SLOT ( editLineItem () ), 0, 2 ); | ||
49 | lineitemsmenu->insertItem ( "Delete", this, SLOT ( deleteLineItem () ), 0, 3 ); | ||
50 | datemenu->insertItem ( "Change", this, SLOT ( showCalendar() ) ); | ||
51 | |||
52 | budgetbox = new QComboBox ( firstline ); | ||
53 | connect ( budgetbox, SIGNAL ( activated ( int ) ), this, SLOT ( setCurrentBudget ( int ) ) ); | ||
54 | |||
55 | budgetview = new QComboBox ( firstline ); | ||
56 | budgetview->insertItem ( "Month" ); | ||
57 | budgetview->insertItem ( "Year" ); | ||
58 | connect ( budgetview, SIGNAL ( activated ( int ) ), this, SLOT ( setCurrentView ( int ) ) ); | ||
59 | |||
60 | budgeted = new QLabel ( secondline ); | ||
61 | budgeted->setFont ( font ); | ||
62 | actual = new QLabel ( secondline ); | ||
63 | actual->setFont ( font ); | ||
64 | date = new QLabel ( secondline ); | ||
65 | date->setFont ( font ); | ||
66 | |||
67 | listview = new QListView ( this ); | ||
68 | listview->setAllColumnsShowFocus ( TRUE ); | ||
69 | listview->setShowSortIndicator ( TRUE ); | ||
70 | listview->setRootIsDecorated ( TRUE ); | ||
71 | listview->setMultiSelection ( FALSE ); | ||
72 | listview->addColumn ( "Line Item", preferences->getColumnPreference ( 13 ) ); // column id 13 | ||
73 | listview->addColumn ( "Budget", preferences->getColumnPreference ( 14 ) ); // column id 14 | ||
74 | listview->addColumn ( "Actual", preferences->getColumnPreference ( 15 ) ); // column id 15 | ||
75 | listview->addColumn ( "", 0 ); // line item ids | ||
76 | listview->setColumnWidthMode ( 0, QListView::Manual ); | ||
77 | listview->setColumnWidthMode ( 1, QListView::Manual ); | ||
78 | listview->setColumnWidthMode ( 2, QListView::Manual ); | ||
79 | listview->setColumnAlignment ( 1, Qt::AlignRight ); | ||
80 | listview->setColumnAlignment ( 2, Qt::AlignRight ); | ||
81 | listview->setColumnWidthMode ( 3, QListView::Manual ); | ||
82 | |||
83 | listview->header()->setTracking ( FALSE ); | ||
84 | connect ( listview->header(), SIGNAL ( sizeChange ( int, int, int ) ), this, SLOT ( saveColumnSize ( int, int, int ) ) ); | ||
85 | |||
86 | displayBudgetNames(); | ||
87 | |||
88 | layout = new QVBoxLayout ( this, 2, 2 ); | ||
89 | layout->setMenuBar ( menu ); | ||
90 | layout->addWidget ( firstline ); | ||
91 | layout->addWidget ( secondline ); | ||
92 | layout->addWidget ( listview ); | ||
93 | } | ||
94 | |||
95 | void BudgetDisplay::deleteBudget () | ||
96 | { | ||
97 | listview->clear(); | ||
98 | transaction->clearBudgetIDs ( currentbudget ); | ||
99 | budget->deleteBudget ( currentbudget ); | ||
100 | if ( budgetbox->count() != 0 ) | ||
101 | displayBudgetNames(); | ||
102 | checkBudgets(); | ||
103 | } | ||
104 | |||
105 | void BudgetDisplay::saveColumnSize ( int column, int oldsize, int newsize ) | ||
106 | { | ||
107 | switch ( column ) | ||
108 | { | ||
109 | case 0: | ||
110 | preferences->changeColumnPreference ( 13, newsize ); | ||
111 | break; | ||
112 | case 1: | ||
113 | preferences->changeColumnPreference ( 14, newsize ); | ||
114 | break; | ||
115 | case 2: | ||
116 | preferences->changeColumnPreference ( 15, newsize ); | ||
117 | break; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | int BudgetDisplay::getIDColumn () | ||
122 | { | ||
123 | int counter; | ||
124 | int columns = listview->columns(); | ||
125 | for ( counter = 0; counter <= columns; counter++ ) | ||
126 | if ( listview->header()->label ( counter ).length() == 0 ) | ||
127 | return counter; | ||
128 | } | ||
129 | |||
130 | void BudgetDisplay::newBudget () | ||
131 | { | ||
132 | constructBudgetWindow(); | ||
133 | int response = nb->exec(); | ||
134 | if ( response == 1 ) | ||
135 | { | ||
136 | // open a new budget object | ||
137 | int addedbudget = budget->addBudget ( budgetname->text(), 0, description->text(), currencybox->currencybox->currentText(), day, month, year, day, month, year, 0 ); | ||
138 | transaction->clearBudgetIDs ( addedbudget ); | ||
139 | displayBudgetNames(); | ||
140 | } | ||
141 | checkBudgets(); | ||
142 | } | ||
143 | |||
144 | void BudgetDisplay::constructBudgetWindow () | ||
145 | { | ||
146 | //construct and format the new budget window | ||
147 | nb = new QDialog ( this, 0, TRUE ); | ||
148 | nb->setCaption ( "Budget" ); | ||
149 | QLabel *namelabel = new QLabel ( "Budget Name", nb ); | ||
150 | budgetname = new QLineEdit ( nb ); | ||
151 | QLabel *descriptionlabel = new QLabel ( "Description", nb ); | ||
152 | description = new QLineEdit ( nb ); | ||
153 | currencybox = new Currency ( nb ); | ||
154 | QBoxLayout *layout = new QVBoxLayout ( nb, 2, 2 ); | ||
155 | layout->addWidget ( namelabel ); | ||
156 | layout->addWidget ( budgetname ); | ||
157 | layout->addWidget ( descriptionlabel ); | ||
158 | layout->addWidget ( description ); | ||
159 | layout->addWidget ( currencybox ); | ||
160 | } | ||
161 | |||
162 | void BudgetDisplay::displayBudgetNames () | ||
163 | { | ||
164 | budgetbox->clear(); | ||
165 | if ( budget->getNumberOfBudgets() != 0 ) | ||
166 | { | ||
167 | ids = budget->getBudgetIDs(); | ||
168 | for ( QStringList::Iterator it = ids->begin(); it != ids->end(); ++it ) | ||
169 | { | ||
170 | QString flag = "/opt/QtPalmtop/pics/flags/"; | ||
171 | flag.append ( budget->getCurrency ( (*it).toInt() ) ); | ||
172 | flag.append ( ".png" ); | ||
173 | budgetbox->insertItem ( QPixmap ( flag ), budget->getBudgetName ( (*it).toInt() ) ); | ||
174 | } | ||
175 | setCurrentBudget ( 0 ); | ||
176 | } | ||
177 | else | ||
178 | checkBudgets(); | ||
179 | } | ||
180 | |||
181 | void BudgetDisplay::setCurrentBudget ( int index ) | ||
182 | { | ||
183 | currentbudget = ( ids->operator[] ( index ).toInt() ); | ||
184 | displayLineItems(); | ||
185 | } | ||
186 | |||
187 | void BudgetDisplay::setCurrentView ( int index ) | ||
188 | { | ||
189 | displayLineItems(); | ||
190 | } | ||
191 | |||
192 | void BudgetDisplay::showCalendar () | ||
193 | { | ||
194 | // create new calendar object and show it | ||
195 | DatePicker *dp = new DatePicker ( QDate ( year, month, day ) ); | ||
196 | dp->daylabel->hide(); | ||
197 | dp->daybox->hide(); | ||
198 | if ( budgetview->currentItem() == 1 ) | ||
199 | { | ||
200 | dp->monthlabel->hide(); | ||
201 | dp->monthbox->hide(); | ||
202 | } | ||
203 | dp->setMaximumWidth ( ( int ) ( this->size().width() * 0.9 ) ); | ||
204 | |||
205 | int response = dp->exec(); | ||
206 | if ( response == 1 ) | ||
207 | { | ||
208 | // Set date integers | ||
209 | year = dp->getYear(); | ||
210 | if ( budgetview->currentItem() == 0 ) | ||
211 | month = dp->getMonth(); | ||
212 | else | ||
213 | month = newDate.month(); | ||
214 | datelabel = preferences->getDate ( year, month ); | ||
215 | displayLineItems(); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | void BudgetDisplay::newLineItem () | ||
220 | { | ||
221 | //construct and format the new line item window | ||
222 | constructLineItemWindow (); | ||
223 | |||
224 | int response = newlineitem->exec(); | ||
225 | if ( response == 1 ) | ||
226 | { | ||
227 | float amount; | ||
228 | if ( lineitemtime->currentItem() == 0 ) | ||
229 | amount = lineitemamount->text().toFloat(); | ||
230 | else if ( lineitemtime->currentItem() == 1 ) | ||
231 | amount = lineitemamount->text().toFloat() * 12; | ||
232 | else | ||
233 | amount = lineitemamount->text().toFloat() * 52; | ||
234 | int lineitemadded = budget->addLineItem ( currentbudget, lineitemname->text(), amount, lineitemtime->currentItem() ); | ||
235 | transaction->clearBudgetIDs ( currentbudget, lineitemadded ); | ||
236 | displayLineItems(); | ||
237 | } | ||
238 | checkBudgets(); | ||
239 | } | ||
240 | |||
241 | void BudgetDisplay::constructLineItemWindow () | ||
242 | { | ||
243 | //construct and format the new budget window | ||
244 | newlineitem = new QDialog ( this, 0, TRUE ); | ||
245 | newlineitem->setCaption ( "Line Item" ); | ||
246 | QLabel *namelabel = new QLabel ( "Line Item Name", newlineitem ); | ||
247 | lineitemname = new QLineEdit ( newlineitem ); | ||
248 | QLabel *budgetamountlabel = new QLabel ( "Budget Amount", newlineitem ); | ||
249 | lineitemamount = new QLineEdit ( newlineitem ); | ||
250 | QLabel *lineitemtimelabel = new QLabel ( "Per:", newlineitem ); | ||
251 | lineitemtime = new QComboBox ( newlineitem ); | ||
252 | lineitemtime->insertItem ( "Year" ); // type 0 | ||
253 | lineitemtime->insertItem ( "Month" ); // type 1 | ||
254 | lineitemtime->insertItem ( "Week" ); // type 2 | ||
255 | QBoxLayout *layout = new QVBoxLayout ( newlineitem, 2, 2 ); | ||
256 | layout->addWidget ( namelabel ); | ||
257 | layout->addWidget ( lineitemname ); | ||
258 | layout->addWidget ( budgetamountlabel ); | ||
259 | layout->addWidget ( lineitemamount ); | ||
260 | layout->addWidget ( lineitemtimelabel ); | ||
261 | layout->addWidget ( lineitemtime ); | ||
262 | } | ||
263 | |||
264 | void BudgetDisplay::deleteLineItem () | ||
265 | { | ||
266 | if ( listview->selectedItem() != 0 ) | ||
267 | { | ||
268 | int lineitemid = listview->selectedItem()->text ( getIDColumn() ).toInt(); | ||
269 | transaction->clearBudgetIDs ( currentbudget, lineitemid ); | ||
270 | budget->deleteLineItem ( currentbudget, lineitemid ); | ||
271 | displayBudgetNames(); | ||
272 | } | ||
273 | else | ||
274 | QMessageBox::warning ( this, "QashMoney", "Please select a line item to delete." ); | ||
275 | checkBudgets(); | ||
276 | } | ||
277 | |||
278 | void BudgetDisplay::displayLineItems () | ||
279 | { | ||
280 | listview->clear(); | ||
281 | if ( budget->getNumberOfBudgets() != 0 ) | ||
282 | { | ||
283 | QString budgettable = budgetbox->currentText(); | ||
284 | budgettable.append ( QString::number ( currentbudget ) ); | ||
285 | budget->displayLineItems ( currentbudget, listview, month, year, budgetview->currentItem() ); | ||
286 | totalactual = transaction->getActualTotal ( currentbudget, year, month, budgetview->currentItem() ); | ||
287 | totalbudget = budget->getBudgetTotal ( currentbudget, budgetview->currentItem() ); | ||
288 | updateBudgetInformation(); | ||
289 | } | ||
290 | } | ||
291 | |||
292 | void BudgetDisplay::checkBudgets () | ||
293 | { | ||
294 | if ( budget->getNumberOfBudgets() == 0 ) | ||
295 | { | ||
296 | budgetview->setEnabled ( FALSE ); | ||
297 | budgetmenu->setItemEnabled ( 2, FALSE ); | ||
298 | budgetmenu->setItemEnabled ( 3, FALSE ); | ||
299 | lineitemsmenu->setItemEnabled ( 1, FALSE ); | ||
300 | lineitemsmenu->setItemEnabled ( 2, FALSE ); | ||
301 | lineitemsmenu->setItemEnabled ( 3, FALSE ); | ||
302 | } | ||
303 | else | ||
304 | { | ||
305 | budgetview->setEnabled ( TRUE ); | ||
306 | budgetmenu->setItemEnabled ( 2, TRUE ); | ||
307 | budgetmenu->setItemEnabled ( 3, TRUE ); | ||
308 | lineitemsmenu->setItemEnabled ( 1, TRUE ); | ||
309 | lineitemsmenu->setItemEnabled ( 2, FALSE ); | ||
310 | lineitemsmenu->setItemEnabled ( 3, FALSE ); | ||
311 | |||
312 | if ( budget->getNumberOfLineItems ( currentbudget ) != 0 ) | ||
313 | { | ||
314 | lineitemsmenu->setItemEnabled ( 2, TRUE ); | ||
315 | lineitemsmenu->setItemEnabled ( 3, TRUE ); | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | |||
320 | void BudgetDisplay::updateBudgetInformation () | ||
321 | { | ||
322 | if ( budgetview->currentItem() == 0 ) | ||
323 | { | ||
324 | datelabel = preferences->getDate ( year, month ); | ||
325 | datelabel.prepend ( "Date: " ); | ||
326 | date->setText ( datelabel ); | ||
327 | } | ||
328 | else | ||
329 | date->setText ( QString::number ( year ) ); | ||
330 | |||
331 | QString budget = "Budget: "; | ||
332 | budget.append ( totalbudget ); | ||
333 | budgeted->setText ( budget ); | ||
334 | |||
335 | QString actualamount = "Actual: "; | ||
336 | actualamount.append ( totalactual ); | ||
337 | actual->setText ( actualamount ); | ||
338 | } | ||
339 | |||
340 | void BudgetDisplay::editBudget () | ||
341 | { | ||
342 | constructBudgetWindow(); | ||
343 | |||
344 | //set the title | ||
345 | budgetname->setText ( budget->getBudgetName ( currentbudget ) ); | ||
346 | //set the description | ||
347 | description->setText ( budget->getBudgetDescription ( currentbudget ) ); | ||
348 | // retrieve the two character currency code then | ||
349 | // go through the currencty box and find the code | ||
350 | //set the currency box to that index number | ||
351 | int count = currencybox->currencybox->count(); | ||
352 | QString code = budget->getCurrency ( currentbudget ); | ||
353 | for ( int counter = 0; count - 1; counter++ ) | ||
354 | { | ||
355 | if ( QString::compare (currencybox->currencybox->text ( counter ), code ) == 0 ) | ||
356 | { | ||
357 | currencybox->currencybox->setCurrentItem ( counter ); | ||
358 | break; | ||
359 | } | ||
360 | } | ||
361 | int response = nb->exec(); | ||
362 | if ( response == 1 ) | ||
363 | { | ||
364 | budget->updateBudget ( budgetname->text(), description->text(), currencybox->currencybox->currentText(), currentbudget ); | ||
365 | displayBudgetNames(); | ||
366 | } | ||
367 | } | ||
368 | |||
369 | void BudgetDisplay::editLineItem () | ||
370 | { | ||
371 | if ( listview->selectedItem() != 0 ) | ||
372 | { | ||
373 | constructLineItemWindow(); | ||
374 | |||
375 | // set the line item name | ||
376 | lineitemname->setText ( listview->selectedItem()->text( 0 ) ); | ||
377 | |||
378 | // set the line item time combobox | ||
379 | int lineitemtype = budget->getLineItemTime ( currentbudget, listview->selectedItem()->text ( 3 ).toInt() ); | ||
380 | lineitemtime->setCurrentItem ( lineitemtype ); | ||
381 | |||
382 | // set the line item amount | ||
383 | float amount = budget->getLineItemAmount ( currentbudget, listview->selectedItem()->text ( 3 ).toInt() ); | ||
384 | if ( lineitemtype == 1 ) | ||
385 | amount = amount / 12; | ||
386 | else if ( lineitemtype == 2 ) | ||
387 | amount = amount / 52; | ||
388 | lineitemamount->setText ( QString::number ( amount ) ); | ||
389 | |||
390 | int response = newlineitem->exec(); | ||
391 | if ( response == 1 ) | ||
392 | { | ||
393 | float amount; | ||
394 | if ( lineitemtime->currentItem() == 0 ) | ||
395 | amount = lineitemamount->text().toFloat(); | ||
396 | else if ( lineitemtime->currentItem() == 1 ) | ||
397 | amount = lineitemamount->text().toFloat() * 12; | ||
398 | else | ||
399 | amount = lineitemamount->text().toFloat() * 52; | ||
400 | budget->updateLineItem ( lineitemname->text(), amount, lineitemtime->currentItem(), currentbudget, listview->selectedItem()->text ( 3 ).toInt() ); | ||
401 | displayLineItems(); | ||
402 | } | ||
403 | } | ||
404 | else | ||
405 | QMessageBox::warning ( this, "QashMoney", "Please select a line item to edit." ); | ||
406 | } | ||
407 | |||
408 | |||