-
Notifications
You must be signed in to change notification settings - Fork 0
/
modlistwidget.cpp
489 lines (425 loc) · 13.2 KB
/
modlistwidget.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2014 Jarno van der Kolk <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "modlistwidget.h"
#include "modmanager.h"
#include "modfilterwidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QScrollArea>
#include <QAction>
#include <QSettings>
ModListWidget::ModListWidget(QWidget* parent, QAction* modfilteraction, ModManager* manager, ModListWidget::mode_t mode)
: QWidget(parent), Manager(manager), Mode(mode), ListWidget(NULL), FilterWidget(NULL), CurrentStateFilter(StateInvalid), CurrentSort(SortInvalid), FilterCategoryComboBox(NULL)
{
#ifdef __APPLE__
QFont currentfont = font();
currentfont.setPointSize(currentfont.pointSize() - 2);
setFont(currentfont);
#endif
QVBoxLayout *mainLayout = new QVBoxLayout(this);
#ifdef __APPLE__
mainLayout->setMargin(0);
mainLayout->setSpacing(-1);
#endif
QWidget *sortFilterWidget = createSortFilterWidget(this);
mainLayout->addWidget(sortFilterWidget);
QScrollArea *modListScrollArea = createModListScrollArea(this);
mainLayout->addWidget(modListScrollArea);
FilterWidget = new ModFilterWidget(this);
connect(FilterWidget, SIGNAL(filterTextChanged(QString)), SLOT(filterTextChanged(QString)));
connect(FilterWidget, SIGNAL(visibilityChanged(bool)), modfilteraction, SLOT(setChecked(bool)));
connect(modfilteraction, SIGNAL(triggered(bool)), SLOT(showModFilter(bool)));
mainLayout->addWidget(FilterWidget);
QSettings settings("DeathByDenim", "PAMM");
switch(Mode)
{
case ModListWidget::ModeAvailable:
modfilteraction->setChecked(settings.value("view/modfilteravailable", false).toBool());
break;
case ModListWidget::ModeInstalled:
modfilteraction->setChecked(settings.value("view/modfilterinstalled", false).toBool());
break;
}
showModFilter(modfilteraction->isChecked());
connect(Manager, SIGNAL(newModInstalled()), SLOT(updateList()));
connect(Manager, SIGNAL(likeCountUpdated()), SLOT(maybeUpdateList()));
if(Mode == ModListWidget::ModeAvailable)
connect(Manager, SIGNAL(availableModsLoaded()), SLOT(updateList()));
}
ModListWidget::~ModListWidget()
{
}
QWidget* ModListWidget::createSortFilterWidget(QWidget* parent)
{
QSettings settings("DeathByDenim", "PAMM");
QWidget *sort_filter_widget = new QWidget(parent);
QHBoxLayout *mylayout = new QHBoxLayout(sort_filter_widget);
QLabel *filterLabel = new QLabel(sort_filter_widget);
filterLabel->setText(tr("SHOW:"));
filterLabel->setStyleSheet("QLabel { color: #008888 }");
mylayout->addWidget(filterLabel);
if(Mode == ModListWidget::ModeAvailable)
{
CurrentStateFilter = StateAll;
QComboBox *filterComboBox = new QComboBox(sort_filter_widget);
filterComboBox->addItem(tr("ALL"));
filterComboBox->addItem(tr("INSTALLED"));
filterComboBox->addItem(tr("REQUIRE UPDATE"));
filterComboBox->addItem(tr("NOT INSTALLED"));
connect(filterComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(filterIndexChanged(const QString &)));
filterComboBox->setCurrentIndex(settings.value(QString("filter/mode%1_filter").arg(Mode), 0).toInt());
mylayout->addWidget(filterComboBox);
}
FilterCategoryComboBox = new QComboBox(sort_filter_widget);
FilterCategoryComboBox->addItem(tr("ALL"));
connect(FilterCategoryComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(filterCategoryIndexChanged(QString)));
mylayout->addWidget(FilterCategoryComboBox);
mylayout->addStretch();
mylayout->addStrut(10);
QLabel *sortLabel = new QLabel(sort_filter_widget);
sortLabel->setText(tr("SORT:"));
sortLabel->setStyleSheet("QLabel { color: #008888 }");
mylayout->addWidget(sortLabel);
QComboBox *sortComboBox = new QComboBox(sort_filter_widget);
sortComboBox->addItem(tr("RANDOM"));
sortComboBox->addItem(tr("LAST UPDATED"));
sortComboBox->addItem(tr("TITLE"));
sortComboBox->addItem(tr("AUTHOR"));
sortComboBox->addItem(tr("BUILD"));
if(Mode == ModListWidget::ModeAvailable)
{
sortComboBox->addItem(tr("DOWNLOADS"));
sortComboBox->addItem(tr("LIKES"));
}
connect(sortComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(sortIndexChanged(const QString &)));
CurrentSort = SortRandom;
sortComboBox->setCurrentIndex(settings.value(QString("filter/mode%1_sort").arg(Mode), 0).toInt());
mylayout->addWidget(sortComboBox);
mylayout->addStretch();
return sort_filter_widget;
}
QScrollArea* ModListWidget::createModListScrollArea(QWidget* parent)
{
QScrollArea *scrollAreaInstalled = new QScrollArea(parent);
ListWidget = new QWidget(scrollAreaInstalled);
QVBoxLayout *modListLayout = new QVBoxLayout(ListWidget);
populateCategoryFilter();
populateModList(true);
QFont listWidgetFont = ListWidget->font();
listWidgetFont.setBold(false);
ListWidget->setFont(listWidgetFont);
#ifdef __APPLE__
listWidgetFont.setPointSize(listWidgetFont.pointSize() - 2);
#endif
scrollAreaInstalled->setWidget(ListWidget);
scrollAreaInstalled->setWidgetResizable(true);
return scrollAreaInstalled;
}
void ModListWidget::filterIndexChanged(const QString& text)
{
if(text == tr("ALL"))
{
CurrentStateFilter = StateAll;
}
else if(text == tr("INSTALLED"))
{
CurrentStateFilter = StateInstalled;
}
else if(text == tr("REQUIRE UPDATE"))
{
CurrentStateFilter = StateRequireUpdate;
}
else if(text == tr("NOT INSTALLED"))
{
CurrentStateFilter = StateNotInstalled;
}
else
CurrentStateFilter = StateInvalid;
QSettings settings("DeathByDenim", "PAMM");
QComboBox *cb = dynamic_cast<QComboBox *>(sender());
if(cb)
settings.setValue(QString("filter/mode%1_filter").arg(Mode), cb->currentIndex());
populateModList(true);
}
void ModListWidget::sortIndexChanged(const QString& text)
{
if(text == tr("RANDOM"))
{
CurrentSort = SortRandom;
}
else if(text == tr("LAST UPDATED"))
{
CurrentSort = SortLastUpdated;
}
else if(text == tr("TITLE"))
{
CurrentSort = SortTitle;
}
else if(text == tr("AUTHOR"))
{
CurrentSort = SortAuthor;
}
else if(text == tr("BUILD"))
{
CurrentSort = SortBuild;
}
else if(text == tr("DOWNLOADS"))
{
CurrentSort = SortDownloads;
}
else if(text == tr("LIKES"))
{
CurrentSort = SortLikes;
}
else
CurrentSort = SortInvalid;
QSettings settings("DeathByDenim", "PAMM");
QComboBox *cb = dynamic_cast<QComboBox *>(sender());
if(cb)
settings.setValue(QString("filter/mode%1_sort").arg(Mode), cb->currentIndex());
populateModList(true);
}
void ModListWidget::populateModList(bool do_sort)
{
if(!ListWidget)
return;
QList<Mod *> *mods;
switch(Mode)
{
case ModeInstalled:
mods = &Manager->installedMods;
break;
case ModeAvailable:
mods = &Manager->availableMods;
break;
default:
return;
}
QVBoxLayout *modListLayout = dynamic_cast<QVBoxLayout *>(ListWidget->layout());
if(modListLayout == NULL)
return;
setUpdatesEnabled(false);
// Clear the list first.
while (QLayoutItem* item = modListLayout->takeAt(0))
{
Mod *mod = dynamic_cast<Mod *>(item->widget());
if(mod)
mod->setParent(NULL);
else
delete item->widget();
delete item;
}
if(do_sort)
{
switch(CurrentSort)
{
case SortRandom:
qSort(mods->begin(), mods->end(), Mod::sortRandom);
break;
case SortLastUpdated:
qSort(mods->begin(), mods->end(), Mod::sortLastUpdated);
break;
case SortTitle:
qSort(mods->begin(), mods->end(), Mod::sortTitle);
break;
case SortAuthor:
qSort(mods->begin(), mods->end(), Mod::sortAuthor);
break;
case SortBuild:
qSort(mods->begin(), mods->end(), Mod::sortBuild);
break;
case SortDownloads:
qSort(mods->begin(), mods->end(), AvailableMod::sortDownloads);
break;
case SortLikes:
qSort(mods->begin(), mods->end(), AvailableMod::sortLikes);
break;
default:
break;
}
}
Q_ASSERT(CurrentSort != SortInvalid);
Q_ASSERT(CurrentStateFilter != StateInvalid || Mode != ModListWidget::ModeAvailable);
QList<Mod *> filteredmods;
if(Mode == ModListWidget::ModeInstalled || (Mode == ModListWidget::ModeAvailable && CurrentStateFilter == StateAll))
{
if(CurrentFilterText == "" && CurrentCategoryFilter == "")
{
filteredmods = *mods;
}
else
{
for(QList<Mod *>::const_iterator m = mods->constBegin(); m != mods->constEnd(); ++m)
{
if((*m)->textContains(CurrentFilterText))
{
if(CurrentCategoryFilter == "" || (*m)->category().contains(CurrentCategoryFilter, Qt::CaseInsensitive))
filteredmods.append(*m);
}
}
}
}
else if(Mode == ModListWidget::ModeAvailable)
{
for(QList<Mod *>::const_iterator m = mods->constBegin(); m != mods->constEnd(); ++m)
{
AvailableMod *am = dynamic_cast<AvailableMod *>(*m);
Q_ASSERT(am != NULL);
switch(CurrentStateFilter)
{
case StateInstalled:
if(am->state() != AvailableMod::installed)
continue;
break;
case StateNotInstalled:
if(am->state() != AvailableMod::notinstalled)
continue;
break;
case StateRequireUpdate:
if(am->state() != AvailableMod::updateavailable)
continue;
break;
default:
Q_ASSERT(false);
}
if(CurrentFilterText == "" || (*m)->textContains(CurrentFilterText))
{
if(CurrentCategoryFilter == "" || (*m)->category().contains(CurrentCategoryFilter, Qt::CaseInsensitive))
filteredmods.append(*m);
}
}
}
if(filteredmods.count() == 0)
{
QLabel *nomodsLabel = new QLabel(ListWidget);
nomodsLabel->setText(tr("No mods found"));
nomodsLabel->setAlignment(Qt::AlignHCenter);
nomodsLabel->setStyleSheet("QLabel {color: #ffffff}");
modListLayout->addWidget(nomodsLabel);
}
else
{
QLabel* clientmodheaderlabel;
QLabel* servermodheaderlabel;
clientmodheaderlabel = new QLabel("CLIENT MODS", ListWidget);
clientmodheaderlabel->setAlignment(Qt::AlignHCenter);
clientmodheaderlabel->setStyleSheet("QLabel { color: gray; }");
servermodheaderlabel = new QLabel("SERVER MODS", ListWidget);
servermodheaderlabel->setAlignment(Qt::AlignHCenter);
servermodheaderlabel->setStyleSheet("QLabel { color: gray; }");
QFont font = clientmodheaderlabel->font();
font.setPointSize(font.pointSize() + 8);
font.setBold(true);
clientmodheaderlabel->setFont(font);
servermodheaderlabel->setFont(font);
modListLayout->addWidget(clientmodheaderlabel);
bool firstservermod = true;
for(QList<Mod *>::const_iterator m = filteredmods.constBegin(); m != filteredmods.constEnd(); ++m)
{
if(firstservermod && (*m)->context() == Mod::server)
{
firstservermod = false;
modListLayout->addWidget(servermodheaderlabel);
}
(*m)->setParent(ListWidget);
modListLayout->addWidget(*m);
}
}
modListLayout->addStretch();
setUpdatesEnabled(true);
}
void ModListWidget::updateList()
{
populateCategoryFilter();
populateModList(true);
}
void ModListWidget::maybeUpdateList()
{
if(CurrentSort == SortLikes)
populateModList(true);
}
void ModListWidget::filterTextChanged(const QString& text)
{
CurrentFilterText = text.toLower();
populateModList(false);
}
void ModListWidget::showModFilter(bool checked)
{
FilterWidget->setVisible(checked);
FilterWidget->setFocus();
QAction *ac = dynamic_cast<QAction *>(sender());
if(ac == NULL)
return;
QSettings settings("DeathByDenim", "PAMM");
switch(Mode)
{
case ModListWidget::ModeAvailable:
settings.setValue("view/modfilteravailable", ac->isChecked());
break;
case ModListWidget::ModeInstalled:
settings.setValue("view/modfilterinstalled", ac->isChecked());
break;
}
}
void ModListWidget::filterCategoryIndexChanged(const QString& text)
{
if(text == tr("ALL"))
CurrentCategoryFilter = "";
else
CurrentCategoryFilter = text;
populateModList(false);
}
void ModListWidget::populateCategoryFilter()
{
QList<Mod *> *mods;
switch(Mode)
{
case ModeInstalled:
mods = &Manager->installedMods;
break;
case ModeAvailable:
mods = &Manager->availableMods;
break;
default:
return;
}
QStringList categories;
for(QList<Mod *>::const_iterator m = mods->constBegin(); m != mods->constEnd(); ++m)
{
categories += (*m)->category();
}
for(QStringList::iterator s = categories.begin(); s != categories.end(); ++s)
*s = s->toUpper();
categories.sort();
categories.removeDuplicates();
categories.push_front(tr("ALL"));
if(FilterCategoryComboBox)
{
disconnect(FilterCategoryComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(filterCategoryIndexChanged(QString)));
FilterCategoryComboBox->clear();
FilterCategoryComboBox->addItems(categories);
int index = FilterCategoryComboBox->findText(CurrentCategoryFilter);
if(index >= 0)
FilterCategoryComboBox->setCurrentIndex(index);
connect(FilterCategoryComboBox, SIGNAL(currentIndexChanged(const QString &)), SLOT(filterCategoryIndexChanged(QString)));
}
}
#include "modlistwidget.moc"