author | John Kern <johnk@symbian.org> |
Wed, 24 Mar 2010 14:11:30 -0700 | |
changeset 4 | a154e00a4448 |
parent 3 | e6d1a78b6db9 |
child 7 | 7ee47a65f1ad |
permissions | -rw-r--r-- |
3
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
1 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
2 |
#include <QDesktopServices> |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
3 |
#include <QMessageBox> |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
4 |
#include <QtGlobal> |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
5 |
#include <QtSql> |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
6 |
#include "Fishes.h" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
7 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
8 |
Fishes::Fishes(QObject *parent) : QObject(parent) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
9 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
10 |
QSqlDatabase db; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
11 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
12 |
// Find QSLite driver |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
13 |
db = QSqlDatabase::addDatabase("QSQLITE"); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
14 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
15 |
QString dbFile = QDesktopServices::storageLocation(QDesktopServices::DataLocation) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
16 |
+ '/' // Qt Universal file separator |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
17 |
+ "seafood.db"; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
18 |
QFile f(dbFile); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
19 |
std::string errString(dbFile.toStdString()); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
20 |
if (!f.exists()) { |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
21 |
qWarning("db not found "); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
22 |
} else { |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
23 |
qWarning("found db "); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
24 |
qWarning(dbFile.toStdString().c_str()); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
25 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
26 |
db.setDatabaseName(dbFile); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
27 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
28 |
// Open databasee |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
29 |
if(!db.open()) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
30 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
31 |
std::string errCode = db.lastError().databaseText().toStdString(); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
32 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
33 |
qWarning("DB: failed to open."); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
34 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
35 |
this->bestFish << "Abalone (farmed)" << "Anchovy, European" << "Barramundi U.S." |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
36 |
<< "Capelin, smelt roe (Iceland)" << "Catfish (U.S.)" << "Clam, softshell" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
37 |
<< "Clams (farmed)" << "Cod, AK (longline)" << "Crab, Dungeness" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
38 |
<< "Crab, stone" << "Mussels" << "Oysters (farmed)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
39 |
<< "Sablefish/ Black Cod (Alaska, Canada)" << "Salmon, wild (Alaska)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
40 |
<< "Sardines, Pacific (U.S.)" << "Shrimp, pink (Oregon)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
41 |
<< "Trout, rainbow (farmed)" << "Tuna, albacore (U.S., Canada)"; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
42 |
this->okFish << "Clams (wild)" << "Cod, Pacific (trawl)" << "Crab, Snow" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
43 |
<< "Flounder/sole (Pacific)" << "Lobster, American/Maine" << "Scallops, sea (U.S., Canada)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
44 |
<< "Shrimp (U.S. wild)" << "Squid" << "Tilapia (Latin America)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
45 |
<< "Tuna, canned light"; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
46 |
this->worstFish << "Chilean sea bass" << "Grouper" << "Orange roughy" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
47 |
<< "Rockfish (trawl)" << "Salmon, farmed or Atlantic" << "Shark" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
48 |
<< "Swordfish (imported)" << "Tilefish (Gulf of Mexico/South Atlantic)" |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
49 |
<< "Tuna, bigeye/yellowfin" << "Tuna, bluefin"; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
50 |
} else { |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
51 |
this->populate(EPresentBest); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
52 |
this->populate(EPresentOK); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
53 |
this->populate(EPresentWorst); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
54 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
55 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
56 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
57 |
void Fishes::populate(TCATEGORIES cat) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
58 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
59 |
QSqlQuery query; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
60 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
61 |
query.prepare("SELECT name FROM fish where category = :category "); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
62 |
query.bindValue(":category",cat); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
63 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
64 |
if (!query.exec()) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
65 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
66 |
QString errCode = "failed to populate " + query.lastError().text(); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
67 |
qWarning(errCode.toStdString().c_str()); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
68 |
//qFatal("Failed to add fish."); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
69 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
70 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
71 |
while (query.next()){ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
72 |
switch (cat) |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
73 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
74 |
case EPresentBest: |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
75 |
this->bestFish << query.value(0).toString(); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
76 |
break; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
77 |
case EPresentWorst: |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
78 |
this->worstFish << query.value(0).toString(); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
79 |
break; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
80 |
case EPresentOK: |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
81 |
this->okFish << query.value(0).toString(); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
82 |
break; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
83 |
default: |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
84 |
qWarning("this can't happen."); |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
85 |
break; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
86 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
87 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
88 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
89 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
90 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
91 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
92 |
const QStringList Fishes::GetBest() |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
93 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
94 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
95 |
return bestFish; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
96 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
97 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
98 |
const QStringList Fishes::GetOK() |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
99 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
100 |
return okFish; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
101 |
} |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
102 |
|
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
103 |
const QStringList Fishes::GetWorst() |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
104 |
{ |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
105 |
return worstFish; |
e6d1a78b6db9
wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff
changeset
|
106 |
} |