summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook/qcheckgraph.cpp
Unidiff
Diffstat (limited to 'noncore/apps/checkbook/qcheckgraph.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/checkbook/qcheckgraph.cpp258
1 files changed, 258 insertions, 0 deletions
diff --git a/noncore/apps/checkbook/qcheckgraph.cpp b/noncore/apps/checkbook/qcheckgraph.cpp
new file mode 100644
index 0000000..5b21ad8
--- a/dev/null
+++ b/noncore/apps/checkbook/qcheckgraph.cpp
@@ -0,0 +1,258 @@
1#include "qcheckgraph.h"
2
3#include <qpainter.h>
4#include <qmessagebox.h>
5#include <qfontmetrics.h>
6#include <qdir.h>
7#include <qfile.h>
8#include <qstringlist.h>
9#include <qdatetime.h>
10#include <qmenubar.h>
11#include <qpopupmenu.h>
12
13QCheckGraph::QCheckGraph(const QString filename)
14 : QCheckGraphBase()
15{
16 QMenuBar *bar = new QMenuBar(this);
17 bar->setMargin(0);
18 QPopupMenu *popup = new QPopupMenu;
19 popup->insertItem("&Save Graph...", this, SLOT(saveGraphAsPicture()));
20 bar->insertItem("&File", popup);
21
22 pixmapready = false;
23 config = new Config(filename, Config::File);
24 QString forresult = "";
25 QString forresult2 = "";
26 int i = 1;
27 for (; forresult != "Not Found"; i++)
28 {
29 config->setGroup(QString::number(i));
30 forresult = config->readEntry("Description", QString("Not Found"));
31 forresult2 = config->readEntry("Category", QString("Not Found"));
32 if (forresult != "Not Found")
33 {
34 if (list.contains(forresult2) == 0)
35 {
36 config->setGroup("Totals");
37 QString larger = config->readEntry("Spent", QString("no"));
38 QString smaller = config->readEntry(forresult2, QString("no"));
39 if (larger != "no" && smaller != "no")
40 {
41 list << forresult2;
42 QString percentage = calculator(smaller, larger, true);
43
44 // Here we calculate how many pixels tall it will be by multiplying it by 200.
45 QString pixels = calculator(percentage, QString("200"), false);
46
47 // This is done because it really doesn't need to have a decimal... just an int, and
48 // QString rounds doubles up to the nearest whole number in order to convert
49 // to an int (which is correct).
50 pixels = pixels.remove(pixels.find('.'), pixels.length());
51
52 if (pixels.toInt() <= 5)
53 {
54 pixels = "6";
55 }
56
57 list2 << pixels;
58 }
59 }
60 } else {
61 continue;
62 }
63 // We want to break this loop on the 7th (remember, starts at 0->6 == 7) item.
64 if (list.count() == 6)
65 {
66 break;
67 }
68 }
69 for (QStringList::Iterator it = list.begin(); it != list.end(); it++)
70 {
71 if ((*it).length() > 11)
72 {
73 (*it).truncate(8);
74 (*it).append("...");
75 }
76 }
77 graphPixmap();
78}
79
80void QCheckGraph::graphPixmap()
81{
82 pixmapready = false;
83 graph = QPixmap(240,250);
84 QPainter p;
85 p.begin(&graph);
86 p.fillRect(0, 0, 240, 300, QColor(255,255,255));
87 // Draw the graph lines
88
89 // Y
90 p.drawLine( 40, 50, 40, 252 );
91
92 // X
93 p.drawLine( 40, 252, 203, 252 );
94
95 // Y stepup lines
96 p.drawLine( 40, 50, 37, 50);
97 p.drawLine( 40, 70, 37, 70);
98 p.drawLine( 40, 90, 37, 90);
99 p.drawLine( 40, 110, 37, 110);
100 p.drawLine( 40, 130, 37, 130);
101 p.drawLine( 40, 150, 37, 150);
102 p.drawLine( 40, 170, 37, 170);
103 p.drawLine( 40, 190, 37, 190);
104 p.drawLine( 40, 210, 37, 210);
105 p.drawLine( 40, 230, 37, 230);
106 p.drawLine( 40, 245, 37, 245);
107
108
109 // Y stepup values
110 p.drawText((35 - p.fontMetrics().width("100")), (50 + (p.fontMetrics().height() / 2)), "100");
111 p.drawText((35 - p.fontMetrics().width("90")), (70 + (p.fontMetrics().height() / 2)), "90");
112 p.drawText((35 - p.fontMetrics().width("80")), (90 + (p.fontMetrics().height() / 2)), "80");
113 p.drawText((35 - p.fontMetrics().width("70")), (110 + (p.fontMetrics().height() / 2)), "70");
114 p.drawText((35 - p.fontMetrics().width("60")), (130 + (p.fontMetrics().height() / 2)), "60");
115 p.drawText((35 - p.fontMetrics().width("50")), (150 + (p.fontMetrics().height() / 2)), "50");
116 p.drawText((35 - p.fontMetrics().width("40")), (170 + (p.fontMetrics().height() / 2)), "40");
117 p.drawText((35 - p.fontMetrics().width("30")), (190 + (p.fontMetrics().height() / 2)), "30");
118 p.drawText((35 - p.fontMetrics().width("20")), (210 + (p.fontMetrics().height() / 2)), "20");
119 p.drawText((35 - p.fontMetrics().width("10")), (230 + (p.fontMetrics().height() / 2)), "10");
120 p.drawText((35 - p.fontMetrics().width("<10")), (245 + (p.fontMetrics().height() / 2)), "<10");
121
122 // Draw the axis[sic?] labels
123 QString ylabel = "Percentage";
124 int pixel = 100;
125 for (unsigned int i = 0; i != ylabel.length(); i++)
126 {
127 p.setBrush(QColor(0,0,0));
128 p.drawText(5,pixel, QString(ylabel[i]));
129 pixel = pixel + p.fontMetrics().height();
130 }
131 p.drawText(95, 265, "Category");
132
133 int i = 0;
134 // Hack: Using if()'s... switch was acting all wierd :{
135 QStringList::Iterator it2 = list2.begin();
136 for (QStringList::Iterator it = list.begin(); it != list.end(); it++)
137 {
138 qWarning(QString::number(i));
139 if (i ==0)
140 {
141 // For the color code:
142 p.setBrush(QColor(255,0,0));
143 p.drawRect(8,12, 8, 8);
144
145 // Now the text:
146 p.setBrush(QColor(0,0,0));
147 p.drawText(18,20, (*it));
148
149 // Last, but not least, we have the actual bar graph height.
150 p.setBrush(QColor(255,0,0));
151 p.drawRect(47, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
152 }
153 if (i ==1)
154 {
155 p.setBrush(QColor(255,255,0));
156 p.drawRect(78,12, 8, 8);
157
158 p.setBrush(QColor(0,0,0));
159 p.drawText(88,20, (*it));
160
161 p.setBrush(QColor(255,255,0));
162 p.drawRect(70, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
163 }
164 if (i==2)
165 {
166 p.setBrush(QColor(0,255,0));
167 p.drawRect(153,12, 8, 8);
168 p.setBrush(QColor(0,0,0));
169 p.drawText(163,20, (*it));
170
171 p.setBrush(QColor(0,255,0));
172 p.drawRect(98, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
173 }
174 if (i==3)
175 {
176 p.setBrush(QColor(89,12,54));
177 p.drawRect(8,27, 8, 8);
178 p.setBrush(QColor(0,0,0));
179 p.drawText(18,35, (*it));
180
181 p.setBrush(QColor(89,12,54));
182 p.drawRect(126, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
183 }
184 if (i==4)
185 {
186 p.setBrush(QColor(0,0,255));
187 p.drawRect(78,27, 8, 8);
188 p.setBrush(QColor(0,0,0));
189 p.drawText(88,35, (*it));
190 p.setBrush(QColor(0,0,255));
191 p.drawRect(154, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
192 }
193 if (i==5)
194 {
195 p.setBrush(QColor(100,40,0));
196 p.drawRect(153,27, 8, 8);
197 p.setBrush(QColor(0,0,0));
198 p.drawText(163,35, (*it));
199 p.setBrush(QColor(100,40,0));
200 p.drawRect(182, ((202 - (*it2).toInt()) + 50), 15, (*it2).toInt());
201 }
202 i++;
203 it2++;
204 }
205
206 p.end();
207 pixmapready = true;
208 graphWidget->setBackgroundPixmap(graph);
209}
210
211QString QCheckGraph::calculator(QString largervalue, QString smallervalue, bool divide)
212{
213 //largervalue = largervalue.remove(largervalue.find(".", 0, false), 1);
214 //smallervalue = smallervalue.remove(smallervalue.find(".", 0, false), 1);
215
216 double largercents = largervalue.toDouble();
217 double smallercents = smallervalue.toDouble();
218
219 double finalammount = 0;
220
221 if (divide == true)
222 {
223 finalammount = (largercents / smallercents);
224 } else {
225 finalammount = (largercents * smallercents);
226 }
227
228 qWarning(QString::number(finalammount));
229
230 return QString::number(finalammount);
231}
232
233/*void QCheckGraph::paintEvent(QPaintEvent *e)
234{
235 if (pixmapready == true)
236 {
237 bitBlt((QPaintDevice *)(graphWidget), 0, 0, graph, 0,0);
238 QWidget::paintEvent(e);
239 }
240}
241*/
242void QCheckGraph::saveGraphAsPicture()
243{
244 QString homedir = QDir::homeDirPath();
245 QDate current = QDate::currentDate();
246 QString datestring = QString::number(current.month());
247 datestring.append(QString::number(current.day()));
248 datestring.append(QString::number(current.year()));
249 QString filename = homedir;
250 filename.append("/Documents/graph");
251 filename.append(datestring);
252 filename.append(".png");
253 graph.save(filename, "PNG", 85);
254 QString graphmessage = "<qt>The graph was saved as graph";
255 graphmessage.append(datestring);
256 graphmessage.append(".png. You may access it by returning to your home screen and clicking on the \"Documents\" tab in the upper right hand screen</qt>");
257 QMessageBox::information(0, "File Saved", graphmessage);
258}