summaryrefslogtreecommitdiffstats
path: root/application/pages/modplatform/FTBPage.cpp
blob: 8c8d8a7d54030a9e0b6f4c655bc3057689a033c9 (plain)
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
#include "FTBPage.h"
#include "ui_FTBPage.h"

#include "MultiMC.h"
#include "FolderInstanceProvider.h"
#include "dialogs/CustomMessageBox.h"
#include "dialogs/NewInstanceDialog.h"
#include "modplatform/ftb/FtbPackFetchTask.h"
#include "modplatform/ftb/FtbPackInstallTask.h"
#include <FtbListModel.h>

FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent)
	: QWidget(parent), dialog(dialog), ui(new Ui::FTBPage)
{
	ftbFetchTask = new FtbPackFetchTask();

	ui->setupUi(this);
	ui->tabWidget->tabBar()->hide();

	{
		publicFilterModel = new FtbFilterModel(this);
		publicListModel = new FtbListModel(this);
		publicFilterModel->setSourceModel(publicListModel);

		ui->publicPackList->setModel(publicFilterModel);
		ui->publicPackList->setSortingEnabled(true);
		ui->publicPackList->header()->hide();
		ui->publicPackList->setIndentation(0);

		for(int i = 0; i < publicFilterModel->getAvailableSortings().size(); i++)
		{
			ui->sortByBox->addItem(publicFilterModel->getAvailableSortings().keys().at(i));
		}

		ui->sortByBox->setCurrentText(publicFilterModel->translateCurrentSorting());
	}

	{
		thirdPartyFilterModel = new FtbFilterModel(this);
		thirdPartyModel = new FtbListModel(this);
		thirdPartyFilterModel->setSourceModel(thirdPartyModel);

		ui->thirdPartyPackList->setModel(thirdPartyFilterModel);
		ui->thirdPartyPackList->setSortingEnabled(true);
		ui->thirdPartyPackList->header()->hide();
		ui->thirdPartyPackList->setIndentation(0);
		thirdPartyFilterModel->setSorting(publicFilterModel->getCurrentSorting());
	}

	connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FTBPage::onSortingSelectionChanged);
	connect(ui->packVersionSelection, &QComboBox::currentTextChanged, this, &FTBPage::onVersionSelectionItemChanged);

	connect(ui->publicPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPublicPackSelectionChanged);
	connect(ui->thirdPartyPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onThirdPartyPackSelectionChanged);

	connect(ui->ftbTabWidget, &QTabWidget::currentChanged, this, &FTBPage::onTabChanged);

	ui->modpackInfo->setOpenExternalLinks(true);

	ui->publicPackList->selectionModel()->reset();
	ui->thirdPartyPackList->selectionModel()->reset();
}

FTBPage::~FTBPage()
{
	delete ui;
	if(ftbFetchTask) {
		ftbFetchTask->deleteLater();
	}
}

bool FTBPage::shouldDisplay() const
{
	return true;
}

void FTBPage::openedImpl()
{
	if(!initialized)
	{
		connect(ftbFetchTask, &FtbPackFetchTask::finished, this, &FTBPage::ftbPackDataDownloadSuccessfully);
		connect(ftbFetchTask, &FtbPackFetchTask::failed, this, &FTBPage::ftbPackDataDownloadFailed);
		ftbFetchTask->fetch();
		initialized = true;
	}
	suggestCurrent();
}

void FTBPage::suggestCurrent()
{
	if(isOpened)
	{
		if(!selected.broken)
		{
			dialog->setSuggestedPack(selected.name, new FtbPackInstallTask(selected, selectedVersion));
		}
		else
		{
			dialog->setSuggestedPack();
		}
	}
}

void FTBPage::ftbPackDataDownloadSuccessfully(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks)
{
	publicListModel->fill(publicPacks);
	thirdPartyModel->fill(thirdPartyPacks);
}

void FTBPage::ftbPackDataDownloadFailed(QString reason)
{
	//TODO: Display the error
}

void FTBPage::onPublicPackSelectionChanged(QModelIndex first, QModelIndex second)
{
	onPackSelectionChanged(first, second, publicFilterModel);
}

void FTBPage::onThirdPartyPackSelectionChanged(QModelIndex first, QModelIndex second)
{
	onPackSelectionChanged(first, second, thirdPartyFilterModel);
}

void FTBPage::onPackSelectionChanged(QModelIndex now, QModelIndex prev, FtbFilterModel *model)
{
	ui->packVersionSelection->clear();
	FtbModpack selectedPack = model->data(now, Qt::UserRole).value<FtbModpack>();

	ui->modpackInfo->setHtml("Pack by <b>" + selectedPack.author + "</b>" + "<br>Minecraft " + selectedPack.mcVersion + "<br>"
				"<br>" + selectedPack.description + "<ul><li>" + selectedPack.mods.replace(";", "</li><li>") + "</li></ul>");

	bool currentAdded = false;

	for(int i = 0; i < selectedPack.oldVersions.size(); i++)
	{
		if(selectedPack.currentVersion == selectedPack.oldVersions.at(i))
		{
			currentAdded = true;
		}
		ui->packVersionSelection->addItem(selectedPack.oldVersions.at(i));
	}

	if(!currentAdded)
	{
		ui->packVersionSelection->addItem(selectedPack.currentVersion);
	}

	selected = selectedPack;
	suggestCurrent();
}

void FTBPage::onVersionSelectionItemChanged(QString data)
{
	if(data.isNull() || data.isEmpty())
	{
		selectedVersion = "";
		return;
	}

	selectedVersion = data;
}

FtbModpack FTBPage::getSelectedModpack()
{
	return selected;
}

QString FTBPage::getSelectedVersion()
{
	return selectedVersion;
}

void FTBPage::onSortingSelectionChanged(QString data)
{
	FtbFilterModel::Sorting toSet = publicFilterModel->getAvailableSortings().value(data);
	publicFilterModel->setSorting(toSet);
	thirdPartyFilterModel->setSorting(toSet);
}

void FTBPage::onTabChanged(int tab)
{
	ui->publicPackList->selectionModel()->reset();
	ui->thirdPartyPackList->selectionModel()->reset();
}