removed the buttons on the graph sides
[siggy:siggy.git] / src / desktop / mainwindowdesktop.cpp
1 #include <QHBoxLayout>
2 #include <QVBoxLayout>
3 #include <QDate>
4 #include <QAction>
5 #include <QMenuBar>
6 #include <QMessageBox>
7 #include "mainwindowdesktop.h"
8
9 MainWindowDesktop::MainWindowDesktop(QWidget *parent)
10         : MainWindow(parent)
11 {
12         month = NULL;
13
14         setWindowTitle(tr("Account: none selected"));
15
16         QVBoxLayout *vBox = new QVBoxLayout(this);
17
18         QMenuBar *menubar = new QMenuBar();
19         menubar->addAction(tr("Accounts"), this, SIGNAL(accountsSelected()));
20         menubar->addAction(tr("Items"), this, SIGNAL(conceptsSelected()));
21         budgetAction = menubar->addAction(tr("Budget"), this, SIGNAL(budgetSelected()));
22         expensesAction = menubar->addAction(tr("Expenses"), this, SIGNAL(expensesSelected()));
23         scheduledAction = menubar->addAction(tr("Scheduled"), this, SIGNAL(scheduledSelected()));
24         summaryAction = menubar->addAction(tr("Summary"), this, SIGNAL(summarySelected()));
25         budgetAction->setEnabled(false);
26         expensesAction->setEnabled(false);
27         scheduledAction->setEnabled(false);
28         summaryAction->setEnabled(false);
29         vBox->addWidget(menubar, 0);
30
31         dateLabel = new QLabel("");
32         vBox->addWidget(dateLabel, 0, Qt::AlignCenter);
33
34         graph = new ExpensesGraph();
35         vBox->addWidget(graph, 1);
36
37         QHBoxLayout *footer = new QHBoxLayout();
38         vBox->addLayout(footer, 0);
39
40         footer->addWidget(new QLabel(tr("Budget")), 0, Qt::AlignRight | Qt::AlignVCenter);
41         budgetLabel = new QLabel("");
42         footer->addWidget(budgetLabel, 1, Qt::AlignLeft | Qt::AlignVCenter);
43
44         footer->addWidget(new QLabel(tr("Expenses")), 0, Qt::AlignRight | Qt::AlignVCenter);
45         totalLabel = new QLabel("");
46         footer->addWidget(totalLabel, 1, Qt::AlignLeft | Qt::AlignVCenter);
47
48         footer->addWidget(new QLabel(tr("Difference")), 0, Qt::AlignRight | Qt::AlignVCenter);
49         savedLabel = new QLabel("");
50         footer->addWidget(savedLabel, 1, Qt::AlignLeft | Qt::AlignVCenter);
51 }
52
53 QSize MainWindowDesktop::minimumSizeHint() const
54 {
55         return QSize(800, 500);
56 }
57
58 QSize MainWindowDesktop::sizeHint() const
59 {
60         return QSize(800, 500);
61 }
62
63 void MainWindowDesktop::setMonthData(MonthData *data)
64 {
65         month = data;
66         /* set data to the graph as well */
67         graph->setModel(data);
68         connect(month, SIGNAL(expensesChanged()), this, SLOT(expensesChanged()));
69         connect(month, SIGNAL(budgetChanged()), this, SLOT(budgetChanged()));
70
71         updateAll();
72
73         budgetAction->setEnabled(true);
74         expensesAction->setEnabled(true);
75         scheduledAction->setEnabled(true);
76         summaryAction->setEnabled(true);
77 }
78
79 void MainWindowDesktop::updateAccountLabel()
80 {
81         setWindowTitle(tr("Account: %1").arg(month->account()->name()));
82 }
83
84 void MainWindowDesktop::expensesChanged()
85 {
86         updateTotalLabel();
87         updateSavedLabel();
88         updateGraph();
89 }
90
91 void MainWindowDesktop::budgetChanged()
92 {
93         updateBudgetLabel();
94         updateSavedLabel();
95         updateGraph();
96 }
97
98 void MainWindowDesktop::updateAll()
99 {
100         updateAccountLabel();
101         updateDateLabel();
102         updateBudgetLabel();
103         updateTotalLabel();
104         updateSavedLabel();
105         updateGraph();
106 }
107
108 void MainWindowDesktop::updateDateLabel()
109 {
110         QString label = QString("<b>%1 %2</b>").arg(QDate::longMonthName(month->month())).arg(month->year());
111         if (graph->accumulated()) {
112                 label.append(QString("   (%1)").arg(tr("accumulated view")));
113         } else {
114                 label.append(QString("   (%1)").arg(tr("daily view")));
115         }
116         dateLabel->setText(label);
117 }
118
119 void MainWindowDesktop::updateBudgetLabel()
120 {
121         budgetLabel->setNum(month->budget());
122 }
123
124 void MainWindowDesktop::updateTotalLabel()
125 {
126         totalLabel->setNum(month->totalExpense());
127 }
128
129 void MainWindowDesktop::updateSavedLabel()
130 {
131         double diff;
132
133         diff = month->budget() - month->totalExpense();
134
135         if (diff >= 0) {
136                 savedLabel->setText(QString("<font color='green'><b>+%1</b></font>").arg(diff));
137         } else {
138                 savedLabel->setText(QString("<font color='red'><b>%1</b></font>").arg(diff));
139         }
140 }
141
142 void MainWindowDesktop::updateGraph()
143 {
144         graph->repaint();
145 }
146
147 void MainWindowDesktop::mousePressEvent(QMouseEvent *event)
148 {
149         if (month != NULL) {
150                 graph->changeView();
151                 updateDateLabel();
152         }
153 }
154
155 void MainWindowDesktop::message(const QString& text)
156 {
157         QMessageBox msgBox(this);
158         msgBox.setText(text);
159         msgBox.exec();
160 }