-
Notifications
You must be signed in to change notification settings - Fork 4
/
itemsearch.cpp
276 lines (242 loc) · 10.5 KB
/
itemsearch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <QMessageBox>
#include <QCloseEvent>
#include <QItemSelectionModel>
#include "qtconcurrentrun.h"
#include "itemsearch.h"
#include "tibiahandler.h"
#include "formathandler.h"
#include "itemmodel.h"
extern TibiaHandler g_tibiaHandler;
extern FormatHandler g_formatHandler;
ItemSearch::ItemSearch( QWidget *parent ) : QDialog( parent ), ui( new Ui::ItemSearchClass )
{
ui->setupUi( this );
ui->checkFilters->setChecked( false );
ui->checkCombinations->setChecked( false );
itemModel = new ItemModel( this );
ui->itemView->setModel( itemModel );
if( g_formatHandler.isLoaded() ) {
for( DatFormatMap::const_iterator it = g_formatHandler.getFormatsBegin(); it != g_formatHandler.getFormatsEnd(); it++ )
ui->comboVersions->insertItem( 0, it.value()->getName(), QVariant::fromValue( it.key() ) );
} else
ui->comboVersions->insertItem( 0, tr( "No Formats" ) );
QObject::connect( &searchWatcher, SIGNAL( finished() ), this, SLOT( onSearchComplete() ) );
toggleAdvanced();
}
ItemSearch::~ItemSearch( void )
{
if( itemModel ) {
itemModel->clear();
delete itemModel;
}
delete ui;
}
void ItemSearch::closeEvent( QCloseEvent *event )
{
if( searchWatcher.isRunning() ) {
QMessageBox::critical( this, tr( "Warning!" ), tr( "A search is in progress and cannot be canceled." ) );
event->ignore();
return;
}
event->accept();
}
void ItemSearch::on_checkAdvanced_stateChanged( int )
{
toggleAdvanced();
}
void ItemSearch::toggleAdvanced( void )
{
ui->checkAdvanced->isChecked() ? ui->itemAttributes->showValues( true ) : ui->itemAttributes->showValues( false );
}
void ItemSearch::on_comboVersions_currentIndexChanged( int index )
{
qint32 version = ui->comboVersions->itemData( index, Qt::UserRole ).toInt();
DatFormat *datFormat = g_formatHandler.getFormatByClient( version );
if( datFormat ) {
ui->itemAttributes->setFormat( datFormat );
}
}
quint32 ItemSearch::colorDominance( const QImage& image, quint8 colorIntensity ) const
{
quint16 colorConcentration[6];
for( quint8 i = 0; i < 6; i++ )
colorConcentration[i] = 0;
quint32 colorGrid[6];
colorGrid[0] = qRgb( 0xFF, 0x00, 0x00 ); // Red
colorGrid[1] = qRgb( 0x00, 0xFF, 0x00 ); // Green
colorGrid[2] = qRgb( 0x00, 0x00, 0xFF ); // Blue
colorGrid[3] = qRgb( 0xFF, 0xFF, 0x00 ); // Yellow
colorGrid[4] = qRgb( 0xFF, 0x00, 0xFF ); // Magenta
colorGrid[5] = qRgb( 0x00, 0xFF, 0xFF ); // Cyan
for ( qint32 x = 0; x < image.width(); x++ ) {
for ( qint32 y = 0; y < image.height(); y++ ) {
QRgb pixel = image.pixel( x, y );
quint8 Red = qRed( pixel );
quint8 Green = qGreen( pixel );
quint8 Blue = qBlue( pixel );
quint8 Alpha = qAlpha( pixel );
if( !( Red == 0xFF && Green == 0x00 && Blue == 0xFF ) && Alpha != 0x00 ) {
if( Red > Green && Red > Blue && ( Red - Green >= colorIntensity ) && ( Red - Blue >= colorIntensity ) )
colorConcentration[0]++;
if( Green > Red && Green > Blue && ( Green - Red >= colorIntensity ) && ( Green - Blue >= colorIntensity ) )
colorConcentration[1]++;
if( Blue > Green && Blue > Red && ( Blue - Green >= colorIntensity ) && ( Blue - Red >= colorIntensity ) )
colorConcentration[2]++;
if( Red > Blue && Green > Blue && ( Red - Blue >= colorIntensity ) && ( Green - Blue >= colorIntensity ) )
colorConcentration[3]++;
if( Red > Green && Blue > Green && ( Red - Green >= colorIntensity ) && ( Blue - Green >= colorIntensity ) )
colorConcentration[4]++;
if( Green > Red && Blue > Red && ( Green - Red >= colorIntensity ) && ( Blue - Red >= colorIntensity ) )
colorConcentration[5]++;
}
}
}
quint16 highest = 0;
quint8 index = 0;
for( quint8 i = 0; i < 6; i++ ) {
if( colorConcentration[i] > highest ) {
highest = colorConcentration[i];
index = i;
}
}
if( highest > 0 ) // Color with highest intensity/density
return colorGrid[index];
return qRgb( 0x00, 0x00, 0x00 );
}
void ItemSearch::on_buttonFind_clicked()
{
HeaderList headerSearch;
ui->itemAttributes->getHeaders( headerSearch );
PropertyList propertySearch;
ui->itemAttributes->getAttributes( propertySearch );
ItemFile *itemFile = g_tibiaHandler.getItemFile();
if( itemFile ) {
results.clear();
searchWatcher.setFuture( QtConcurrent::run( this, &ItemSearch::performSearch, headerSearch, propertySearch, itemFile ) );
g_tibiaHandler.getChaseWidget()->addOperation( USER_OPERATION_SEARCH );
time.start();
}
}
void ItemSearch::on_buttonSelect_clicked()
{
ItemList selectedItems = itemSelection( ui->itemView->selectionModel() );
if( !selectedItems.isEmpty() ) {
ItemList items, outfits, effects, projectiles;
foreach( TibiaItem* item, selectedItems ) {
switch( item->getParentType() ) {
case ITEM_PARENT_ITEMS:
items.push_back( item );
break;
case ITEM_PARENT_OUTFITS:
outfits.push_back( item );
break;
case ITEM_PARENT_EFFECTS:
effects.push_back( item );
break;
case ITEM_PARENT_PROJECTILES:
projectiles.push_back( item );
break;
}
}
emit selectItems( items, outfits, effects, projectiles );
}
}
ItemList ItemSearch::itemSelection( QItemSelectionModel *selectionModel )
{
ItemList items;
if( selectionModel ) {
QModelIndexList indexes = selectionModel->selectedIndexes();
if( !indexes.isEmpty() ) {
foreach( QModelIndex index, indexes ) {
if( index.isValid() ) {
TibiaItem *item = index.data( Qt::UserRole ).value<TibiaItem *>();
if( item )
items.push_back( item );
}
}
}
}
return items;
}
void ItemSearch::onSearchComplete( void )
{
if( itemModel )
itemModel->setItemList( results );
g_tibiaHandler.getChaseWidget()->removeOperation( USER_OPERATION_SEARCH );
g_tibiaHandler.getOutputWidget()->addLine( QColor( Qt::darkCyan ), tr( "Search Completed: ( %1 )" ).arg( QTime().addMSecs( time.elapsed() ).toString( tr( "hh:mm:ss.zzz" ) ) ) );
}
void ItemSearch::performSearch( const HeaderList& headerSearch, const PropertyList& propertySearch, ItemFile *itemFile )
{
if( !ui->checkFilters->isChecked() || !ui->filterItems->isChecked() ) {
foreach( TibiaItem* item, itemFile->getItems() ) {
if( verifyItem( headerSearch, propertySearch, item ) )
results.push_back( item );
}
}
if( !ui->checkFilters->isChecked() || !ui->filterOutfits->isChecked() ) {
foreach( TibiaItem* item, itemFile->getOutfits() ) {
if( verifyItem( headerSearch, propertySearch, item ) )
results.push_back( item );
}
}
if( !ui->checkFilters->isChecked() || !ui->filterEffects->isChecked() ) {
foreach( TibiaItem* item, itemFile->getEffects() ) {
if( verifyItem( headerSearch, propertySearch, item ) )
results.push_back( item );
}
}
if( !ui->checkFilters->isChecked() || !ui->filterProjectiles->isChecked() ) {
foreach( TibiaItem* item, itemFile->getProjectiles() ) {
if( verifyItem( headerSearch, propertySearch, item ) )
results.push_back( item );
}
}
}
bool ItemSearch::verifyItem( const HeaderList& headerSearch, const PropertyList& propertySearch, const TibiaItem *item ) const
{
if( ui->tabAttributeTypes->currentIndex() == 0 || ( ui->checkCombinations->isChecked() && ui->combineAttributes->isChecked() ) ) {
if( ui->checkAdvanced->isChecked() && ( !item->contains( headerSearch ) || !item->contains( propertySearch ) ) )
return false;
if( !ui->checkAdvanced->isChecked() && !item->contains( headerSearch ) )
return false;
}
if( ui->tabAttributeTypes->currentIndex() == 1 || ( ui->checkCombinations->isChecked() && ui->combineValues->isChecked() ) ) {
if( ui->buttonID->isChecked() ) {
if( g_tibiaHandler.getItemFile()->getItemById( ( quint32 )ui->valueID->value() ) != item )
return false;
} else if( ui->buttonProperty->isChecked() ) {
if( ui->checkWidth->isChecked() && ui->valueWidth->value() != item->getWidth() )
return false;
if( ui->checkHeight->isChecked() && ui->valueHeight->value() != item->getHeight() )
return false;
if( ui->checkCropsize->isChecked() && ui->valueCropsize->value() != item->getCropsize() )
return false;
if( ui->checkLayers->isChecked() && ui->valueLayers->value() != item->getLayers() )
return false;
if( ui->checkXDiv->isChecked() && ui->valueXDiv->value() != item->getXDiv() )
return false;
if( ui->checkYDiv->isChecked() && ui->valueYDiv->value() != item->getYDiv() )
return false;
if( ui->checkZDiv->isChecked() && ui->valueZDiv->value() != item->getZDiv() )
return false;
if( ui->checkAnimations->isChecked() && ui->valueAnimations->value() != item->getAnimation() )
return false;
}
}
if( ui->tabAttributeTypes->currentIndex() == 2 || ( ui->checkCombinations->isChecked() && ui->combineColors->isChecked() ) ) {
quint32 dominantColor = colorDominance( g_tibiaHandler.drawItem( item ), ui->spinIntensity->value() );
if( ui->radioColorRed->isChecked() && dominantColor != qRgb( 0xFF, 0x00, 0x00 ) )
return false;
if( ui->radioColorGreen->isChecked() && dominantColor != qRgb( 0x00, 0xFF, 0x00 ) )
return false;
if( ui->radioColorBlue->isChecked() && dominantColor != qRgb( 0x00, 0x00, 0xFF ) )
return false;
if( ui->radioColorYellow->isChecked() && dominantColor != qRgb( 0xFF, 0xFF, 0x00 ) )
return false;
if( ui->radioColorMagenta->isChecked() && dominantColor != qRgb( 0xFF, 0x00, 0xFF ) )
return false;
if( ui->radioColorCyan->isChecked() && dominantColor != qRgb( 0x00, 0xFF, 0xFF ) )
return false;
}
return true;
}