-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatelistwidget.cpp
119 lines (95 loc) · 2.96 KB
/
templatelistwidget.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
#include "templatelistwidget.h"
TemplateListWidget::TemplateListWidget(QWidget *parent) :
QListWidget(parent)
{
setResizeMode(QListView::Adjust);
connect(this, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(updateGrid(QListWidgetItem*)));
}
void TemplateListWidget::updateGrid(QListWidgetItem *item)
{
int templateId = item->data(Qt::UserRole).toInt();
grid->setTemplateId(templateId);
grid->loadFromDB();
}
void TemplateListWidget::updateList(int id)
{
int countRecord = -1;
QSqlQuery query;
query.prepare("SELECT _preview FROM crossword.templates WHERE _id = ?;");
query.addBindValue(QVariant(id));
query.exec();
QSqlError le = query.lastError();
if (le.type() == QSqlError::NoError)
{
int previewNo = query.record().indexOf("_preview");
if (query.isActive() && query.isSelect())
countRecord = query.size();
else
countRecord = -1;
if (countRecord > 0)
{
QByteArray _ba2;
QBuffer buffer2(&_ba2);
query.next();
_ba2 = query.value(previewNo).toByteArray();
buffer2.setBuffer(&_ba2);
buffer2.open(QIODevice::ReadWrite | QIODevice::Unbuffered);
QImage *imagePreview = new QImage();
imagePreview->load(&buffer2, "PNG");
QListWidgetItem *lwi = NULL, *cur;
for (int i = 0; i < count(); i++)
{
cur = item(i);
if (cur->data(Qt::UserRole).toInt() == id)
lwi = cur;
}
if (lwi)
{
if (imagePreview)
{
QImage scaledPreview = imagePreview->scaled(sizeHint(), Qt::KeepAspectRatio, Qt::FastTransformation);
lwi->setData(Qt::DecorationRole, QVariant(scaledPreview));
}
}
else
{
lwi = new QListWidgetItem(this);
if (lwi)
{
if (imagePreview)
{
QImage scaledPreview = imagePreview->scaled(sizeHint(), Qt::KeepAspectRatio, Qt::FastTransformation);
lwi->setData(Qt::UserRole, QVariant(id));
lwi->setData(Qt::DecorationRole, QVariant(scaledPreview));
}
addItem(lwi);
}
}
buffer2.close();
}
}
else
qDebug() << "updateList: " << le.text();
}
void TemplateListWidget::setTemplateGrid(tableTemplateWidget *ttw)
{
grid = ttw;
connect(grid, SIGNAL(savedToDB(int)), this, SLOT(updateList(int)));
connect(grid, SIGNAL(deletedFromDB(int)), this, SLOT(deleteItem(int)));
}
void TemplateListWidget::deleteItem(int id)
{
QListWidgetItem *cur, *lwi;
int cnt = this->count();
for (int i = 0; i < cnt; i++)
{
cur = item(i);
if (cur->data(Qt::UserRole).toInt() == id)
{
lwi = takeItem(i);
if (lwi)
delete lwi;
return;
}
}
}