/
maintabpageform.cpp
305 lines (230 loc) · 9.83 KB
/
maintabpageform.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "maintabpageform.h"
#include "ui_maintabpageform.h"
#include <torchlight2stashconverter.h>
#include <QMimeData>
#include <QFileInfo>
#include <QDir>
MainTabPageForm::MainTabPageForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainTabPageForm)
{
ui->setupUi(this);
mTorchlight2Stash = NULL;
mStashItemsTable = NULL;
mInfiniteStashTreeViewModel = NULL;
mIsLoading = false;
connect(ui->Torchlight2SharedStashListWidget,
SIGNAL(itemAdded(QListWidgetItem*)), this,
SLOT(OnTorchlight2SharedStashItemAdded(QListWidgetItem*)));
connect(ui->Torchlight2SharedStashListWidget,
SIGNAL(itemsAboutToBeRemoved(QList<QListWidgetItem*>)), this,
SLOT(OnTorchlight2SharedStashItemsRemoved(QList<QListWidgetItem*>)));
// connect(ui->InfiniteStashItemsTreeView,
// SIGNAL(itemDropped(QDropEvent*)), this,
// SLOT(OnItemDroppedOnInfiniteStash(QDropEvent*)));
connect(ui->Torchlight2SharedStashListWidget,
SIGNAL(dragEntered(QDragEnterEvent*)),
this,
SLOT(OnItemDraggedToTorchlight2SharedStash(QDragEnterEvent*)));
connect(ui->Torchlight2SharedStashListWidget,
SIGNAL(itemDropped(QDropEvent*)), this,
SLOT(OnItemDroppedOnTorchlight2SharedStash(QDropEvent*)));
connect(ui->Torchlight2SharedStashListWidget, SIGNAL(currentRowChanged(int)),
this, SLOT(OnTorchlight2StashItemActivated(int)));
}
MainTabPageForm::~MainTabPageForm()
{
delete ui;
}
void MainTabPageForm::SetInfiniteStashStandardItemModel(InfiniteStashStandardItemModel* inModel)
{
mInfiniteStashTreeViewModel = inModel;
if (mInfiniteStashTreeViewModel != NULL)
{
ui->InfiniteStashItemsTreeView->setModel(mInfiniteStashTreeViewModel);
}
}
void MainTabPageForm::Options(OptionCollection *inOptions)
{
mOptions = inOptions;
if (mOptions != NULL)
{
QString torchlight2StashFile = mOptions->Get(OptionKeys::Torchlight2SharedStashFile);
QString infiniteStashFolder = mOptions->Get(OptionKeys::StashManagerFolder);
ui->StashItemDetailsGraphicsView->InfiniteStashLocation(infiniteStashFolder);
FillSharedStashList(torchlight2StashFile);
}
}
void MainTabPageForm::FillSharedStashList(QString inTorchlight2SharedStashFile)
{
mIsLoading = true;
ui->Torchlight2SharedStashListWidget->clear();
if (mTorchlight2Stash != NULL)
delete mTorchlight2Stash;
QByteArray decryptedBytes;
QFile stashFile(inTorchlight2SharedStashFile);
if (stashFile.open(QIODevice::ReadOnly))
{
if (Torchlight2StashConverter::DescrambleFile(stashFile.readAll(), decryptedBytes))
{
stashFile.close();
QDir stashManagerFolder(mOptions->Get(OptionKeys::StashManagerFolder));
QFileInfo path(inTorchlight2SharedStashFile);
QString absolutePath = stashManagerFolder.absolutePath();
QString fileName = path.fileName();
fileName = "decrypted_" + fileName;
QFile decryptedFile(absolutePath + "/" + fileName);
if (decryptedFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
decryptedFile.write(decryptedBytes);
decryptedFile.close();
}
mTorchlight2Stash = new Torchlight2Stash(decryptedBytes);
QString maxNumberOfItems = mOptions->Get(OptionKeys::MaxNumberOfItemsPerStashTab);
mTorchlight2Stash->MaximumNumberOfItemsPerTab(maxNumberOfItems.toInt());
QList<Torchlight2Item> itemsInStash = mTorchlight2Stash->StashItems();
// QList<QString> keys = itemsInStash.keys();
for (qint32 i = 0; i < itemsInStash.count(); ++i)
{
QString itemName = itemsInStash[i].Name();
itemName.replace(":", "");
QFile nextItem(absolutePath + "/" + QString::number(i) + itemName + ".dat");
if (nextItem.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
nextItem.write(itemsInStash[i].Bytes());
nextItem.close();
}
QListWidgetItem* item = new QListWidgetItem(itemsInStash[i].Name());
// item->setFlags(item->flags() ^ Qt::ItemIsDropEnabled);
QVariant itemData = QVariant::fromValue(itemsInStash[i].Bytes());
// QVariant itemData(*itemsInStash[keys[i]]);
//first user role is the items data
item->setData(Qt::UserRole, itemData);
//second user role is a marker so we know that this is an item from the stash
item->setData(Qt::UserRole + 1, InfiniteStashStandardItemModel::StashItemType);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
ui->Torchlight2SharedStashListWidget->addItem(item);
}
}
}
mIsLoading = false;
}
void MainTabPageForm::OnTorchlight2SharedStashItemAdded(QListWidgetItem* item)
{
qint32 count = ui->Torchlight2SharedStashListWidget->count();
ui->SharedStashItemCountLabel->setText(QString::number(count));
if (!mIsLoading && mStashItemsTable != NULL)
{
QVariant itemId = item->data(Qt::UserRole);
StashItem itemData = mStashItemsTable->GetStashItem(itemId.toLongLong());
if (mStashItemsTable->DeleteStashItem(itemId.toLongLong()))
{
}
}
}
void MainTabPageForm::OnTorchlight2SharedStashItemsRemoved(QList<QListWidgetItem*> items)
{
qint32 count = ui->Torchlight2SharedStashListWidget->count() - items.count();
ui->SharedStashItemCountLabel->setText(QString::number(count));
for (int i = 0; i < items.length(); ++i)
{
if (mStashItemsTable != NULL)
{
StashItem itemData;
itemData.item = items[i]->data(Qt::UserRole).toByteArray();
itemData.itemId = mStashItemsTable->AddStashItem(itemData);
if (itemData.itemId > 0)
{
items[i]->setData(Qt::UserRole, itemData.itemId);
}
}
}
}
void MainTabPageForm::OnTorchlight2SharedStashFileChanged(QString fileLocation)
{
FillSharedStashList(fileLocation);
}
void MainTabPageForm::OnMaximumNumberOfItemsPerTabChanged(qint32 newMax)
{
if (mTorchlight2Stash != NULL)
mTorchlight2Stash->MaximumNumberOfItemsPerTab(newMax);
}
void MainTabPageForm::OnItemDroppedOnTorchlight2SharedStash(QDropEvent *event)
{
if (event->source() == ui->InfiniteStashItemsTreeView)
{
QListWidgetItem* itemDropped = ui->Torchlight2SharedStashListWidget->GetDroppedItem(event->pos());
StashItem itemData = mStashItemsTable->GetStashItem(itemDropped->data(Qt::UserRole).toLongLong());
Torchlight2Item stashItem(itemData.item);
mTorchlight2Stash->AddItemToStash(stashItem);
QByteArray outputBytes;
if (Torchlight2StashConverter::ScrambleFile(mTorchlight2Stash->Bytes(), outputBytes))
{
QString stashFilePath = mOptions->Get(OptionKeys::Torchlight2SharedStashFile);
QFile stashFile(stashFilePath);
if (stashFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qint64 numBytesWritten = stashFile.write(outputBytes);
stashFile.close();
if (numBytesWritten == outputBytes.length())
{
if (mStashItemsTable->DeleteStashItem(itemData.itemId))
{
itemDropped->setData(Qt::UserRole, itemData.item);
}
}
}
}
}
}
/*
* We capture this event in order to prevent groups from being dragged onto the
* Torchlight 2 stash list.
*
*/
void MainTabPageForm::OnItemDraggedToTorchlight2SharedStash(QDragEnterEvent* event)
{
bool ignoreAction = false;
if (event->source() == ui->InfiniteStashItemsTreeView)
{
QByteArray modelList = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream inputStream(&modelList, QIODevice::ReadOnly);
while (!inputStream.atEnd())
{
int row;
int col;
QMap<int, QVariant> roleDataMap;
inputStream >> row >> col >> roleDataMap;
QVariant itemType = roleDataMap[Qt::UserRole + 1];
if (itemType.toInt() == InfiniteStashStandardItemModel::GroupItemType)
{
ignoreAction = true;
}
else if (itemType.toInt() == InfiniteStashStandardItemModel::StashItemType)
{
QVariant itemIdVariant = roleDataMap[Qt::UserRole];
if (!itemIdVariant.isNull() && itemIdVariant.isValid() &&
itemIdVariant.type() == QVariant::LongLong)
{
StashItem itemDetails = mStashItemsTable->GetStashItem(itemIdVariant.toLongLong());
Torchlight2Item item(itemDetails.item);
if (!mTorchlight2Stash->CanItemBeAdded(item))
{
ignoreAction = true;
}
}
}
}
}
if (ignoreAction)
{
event->ignore();
event->setDropAction(Qt::IgnoreAction);
}
}
void MainTabPageForm::OnTorchlight2StashItemActivated(int row)
{
QListWidgetItem* widgetItem = ui->Torchlight2SharedStashListWidget->item(row);
Torchlight2Item item(widgetItem->data(Qt::UserRole).toByteArray());
ui->StashItemDetailsGraphicsView->SetItemToDisplay(item);
}