This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sampleview.cpp
132 lines (117 loc) · 4.31 KB
/
sampleview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "sampleview.h"
#include <QTimer>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QBuffer>
#include <QByteArray>
#include <QUuid>
#include <QModelIndexList>
SampleView::SampleView(QWidget * parent)
: QTreeView(parent)
{
initView();
if (openSampleDB(SAMPLES_DB_PATH)) loadSamples(TRAIN_SAMPLE_DB_NAME);
else QMessageBox::warning(NULL, "", "cannot open sample database!");
//QTimer::singleShot(0, this, SLOT(loadSampleDB()));
}
SampleView::~SampleView()
{
sampleDB().close();
}
void SampleView::initView()
{
QStandardItemModel* model= new QStandardItemModel();
this->setModel(model);
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "0"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "1"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "2"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "3"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "4"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "5"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "6"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "7"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "8"));
rootList().append(new QStandardItem(QIcon(":/new/others/digital"), "9"));
QStandardItem *rootItem = model->invisibleRootItem();
rootItem->appendRows(rootList());
//QImage image(IMAGE_W, IMAGE_H, QImage::Format_RGB32);
//rootList()[0]->appendRow(new QStandardItem(QIcon(QPixmap().fromImage(image)), ""));
//rootList()[0]->appendRow(new QStandardItem(QIcon(QPixmap().fromImage(image)), ""));
//rootList()[0]->appendRow(new QStandardItem(QIcon(QPixmap().fromImage(image)), ""));
//rootList()[0]->appendRow(new QStandardItem(QIcon(QPixmap().fromImage(image)), ""));
}
bool SampleView::openSampleDB(QString const& filename)
{
QString dbName = tr("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=%1").arg(filename);
sampleDB(QSqlDatabase::addDatabase("QODBC"));
sampleDB().setDatabaseName(dbName);
return sampleDB().open();
}
bool SampleView::loadSamples(QString const& table)
{
QSqlQuery query(QObject::tr("SELECT * FROM %1").arg(table));
int id_field = query.record().indexOf("id");
int digit_field = query.record().indexOf("digit");
int image_field = query.record().indexOf("image");
// clear
for (int i = 0; i < 10; ++i)
rootList()[i]->removeRows(0, rootList()[i]->rowCount());
// insert items
QImage image;
while (query.next())
{
QString id_str = query.value(id_field).toString();
int digit = query.value(digit_field).toInt(NULL);
QByteArray data = query.value(image_field).toByteArray();
if (digit >= 0 && digit < 10)
{
image.loadFromData(data, "bmp");
QStandardItem* item = new QStandardItem(QIcon(QPixmap().fromImage(image)), "");
item->setData(id_str, Qt::UserRole + 1);
rootList()[digit]->appendRow(item);
}
}
return true;
}
bool SampleView::addSample(QString const& table, int digit, QImage const& image)
{
if (digit < 0 || digit > 9) return false;
// QImage => QBuffer => QByteArray
QByteArray data;
QBuffer buffer(&data);
image.save(&buffer, "bmp");
QSqlQuery query;
query.prepare(QObject::tr("INSERT INTO %1(id, digit, image) "
"VALUES(?, ?, ?)").arg(table));
query.addBindValue(QUuid::createUuid().toString());
query.addBindValue(digit);
query.addBindValue(data);
/*query.prepare(QObject::tr("INSERT INTO %1(id, digit, image) "
"VALUES(?, ?, ?)").arg(SAMPLE_DB_NAME));
query.bindValue(":id", QUuid::createUuid().toString());
query.bindValue(":digit", digit);
query.bindValue(":image", data);*/
return query.exec();
}
void SampleView::removeSample(QString const& table)
{
QModelIndexList list = this->selectedIndexes();
for (int i = 0; i < list.size(); ++i)
{
// gets uuid
QString id_str = list[i].data(Qt::UserRole + 1).toString();
if (!id_str.isEmpty())
{
// delete from database
QSqlQuery query;
query.prepare(QObject::tr("DELETE FROM %1 "
"WHERE id = ?").arg(table));
query.addBindValue(id_str);
if (query.exec())
{
// remove item
((QStandardItemModel*)(this->model()))->itemFromIndex(list[i].parent())->removeRow(list[i].row());
}
}
}
}