-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventorysearch.cpp
153 lines (143 loc) · 5.17 KB
/
inventorysearch.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "inventorysearch.h"
#include "inventorysearchdialog.h"
#include "ui_inventorysearch.h"
#include "subseq.h"
using namespace std;
using json = nlohmann::json;
void _setIconForLabel(string image_name, QLabel* label) {
QPixmap icon(QString::fromStdString(image_name));
int w =32;
int h =32;
icon = icon.scaled(w,h, Qt::KeepAspectRatio);
label->setPixmap(icon);
}
InventorySearch::InventorySearch(json &dat,QWidget *parent) :
QWidget(parent),
data(dat),
ui(new Ui::InventorySearch)
{
ui->setupUi(this);
ui->exitButton->setFixedSize(QSize(32,32));
ui->exitButton->setIcon(QIcon(tr(":/exit")));
json items = data["item"];
json fluids = data["fluid"];
json modules = data["module"];
json ammo = data["ammo"];
json recipe = data["recipe"];
auto guns = {"combat-shotgun",
"flamethrower",
"pistol",
"rocket-launcher",
"shotgun",
"submachine-gun"};
set<string> forbidden = {"electric-energy-interface",
"simple-entity-with-force",
"simple-entity-with-owner",
"player-port",
"fast-loader",
"express-loader",
"loader",
"railgun",
"railgun-dart",
"coin",
"small-plane",
"solid-fuel-from-heavy-oil",
"solid-fuel-from-light-oil",
"solid-fuel-from-petroleum-gas",
"nuclear-fuel-reprocessing",
"kovarex-enrichment-process",
"heavy-oil-cracking",
"light-oil-cracking",
"coal-liquefaction",
"advanced-oil-processing",
"basic-oil-processing"};
set<string> include = {"solid-fuel"};
for (string item : include) {
itemNames.insert(item);
}
for (json::iterator it = recipe.begin(); it != recipe.end(); ++it) {
string key = it.key();
if (forbidden.find(key) == forbidden.end()) {
itemNames.insert(key);
}
}
for (json::iterator it = fluids.begin(); it != fluids.end(); ++it) {
string key = it.key();
if (forbidden.find(key) == forbidden.end()) {
itemNames.insert(key);
}
}
int size = itemNames.size();
for (int i = 0; i < size/10 +1;i++) {
ui->inventoryTable->insertRow(i);
}
for (int r = 0; r < ui->inventoryTable->rowCount(); r++) {
for (int c =0; c < ui->inventoryTable->columnCount();c++) {
auto icon = new QLabel(ui->inventoryTable);
ui->inventoryTable->setCellWidget(r,c,icon);
}
}
_setIconForLabel(":/search.png",ui->searchIcon);
loadAssets();
}
InventorySearch::~InventorySearch()
{
delete ui;
}
void InventorySearch::loadAssets(string key) {
for (char &c : key) {
if (c == ' ') {
c = '-';
}
c = tolower(c);
}
int width = ui->inventoryTable->columnCount();
int i = 0;
set<string> supersequences;
matchSubsequence(key,itemNames,supersequences);
for (auto name : supersequences) {
auto icon = static_cast<QLabel*>(ui->inventoryTable->cellWidget(i/width,i%width));
icon->setProperty("item-name",QString::fromStdString(name));
string file_path = ":/icons/"+name;
_setIconForLabel(file_path, icon);
icon->setToolTip(QString::fromStdString(name));
i++;
}
int total_cells = ui->inventoryTable->rowCount()*ui->inventoryTable->columnCount();
for (int j = i; j < total_cells; j++) {
auto icon = static_cast<QLabel*>(ui->inventoryTable->cellWidget(j/width,j%width));
icon->setPixmap(QPixmap());
icon->setToolTip("");
}
ui->inventoryTable->scrollToTop();
}
void InventorySearch::on_inventoryTable_activated(const QModelIndex &index)
{
string item = static_cast<QLabel*>(ui->inventoryTable->cellWidget(index.row(),index.column()))->property("item-name").toString().toStdString();
auto dialog = static_cast<InventorySearchDialog*>(this->parentWidget());
int r = dialog->row;
int c = dialog->col;
select_item(item,r,c);
dialog->hide();
}
void InventorySearch::on_lineSearch_textChanged(const QString &arg1)
{
loadAssets(arg1.toStdString());
}
void InventorySearch::on_inventoryTable_clicked(const QModelIndex &index)
{
string item = static_cast<QLabel*>(ui->inventoryTable->cellWidget(index.row(),index.column()))->property("item-name").toString().toStdString();
auto dialog = static_cast<InventorySearchDialog*>(this->parentWidget());
int r = dialog->row;
int c = dialog->col;
emit select_item(item,r,c);
dialog->hide();
}
void InventorySearch::reset() {
ui->lineSearch->setText(tr(""));
}
void InventorySearch::on_exitButton_clicked()
{
auto dialog = static_cast<InventorySearchDialog*>(this->parentWidget());
dialog->hide();
}