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