summaryrefslogtreecommitdiffstats
path: root/application/widgets/PageContainer.cpp
blob: c8c2b57a5cc399bded77ed5bd898a9ad9fa7bf5e (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
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
/* Copyright 2013-2018 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "PageContainer.h"

#include <QStackedLayout>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QUrl>
#include <QStyledItemDelegate>
#include <QListView>
#include <QLineEdit>
#include <QLabel>
#include <QDialogButtonBox>
#include <QGridLayout>

#include "MultiMC.h"
#include "settings/SettingsObject.h"
#include "widgets/IconLabel.h"
#include "PageContainer_p.h"
#include <MultiMC.h>
#include <DesktopServices.h>

class PageEntryFilterModel : public QSortFilterProxyModel
{
public:
	explicit PageEntryFilterModel(QObject *parent = 0) : QSortFilterProxyModel(parent)
	{
	}

protected:
	bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
	{
		const QString pattern = filterRegExp().pattern();
		const auto model = static_cast<PageModel *>(sourceModel());
		const auto page = model->pages().at(sourceRow);
		if (!page->shouldDisplay())
			return false;
		// Regular contents check, then check page-filter.
		return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
	}
};

PageContainer::PageContainer(BasePageProvider *pageProvider, QString defaultId,
							 QWidget *parent)
	: QWidget(parent)
{
	createUI();
	m_model = new PageModel(this);
	m_proxyModel = new PageEntryFilterModel(this);
	int counter = 0;
	auto pages = pageProvider->getPages();
	for (auto page : pages)
	{
		page->stackIndex = m_pageStack->addWidget(dynamic_cast<QWidget *>(page));
		page->listIndex = counter;
		page->setParentContainer(this);
		counter++;
	}
	m_model->setPages(pages);

	m_proxyModel->setSourceModel(m_model);
	m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);

	m_pageList->setIconSize(QSize(pageIconSize, pageIconSize));
	m_pageList->setSelectionMode(QAbstractItemView::SingleSelection);
	m_pageList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
	m_pageList->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
	m_pageList->setModel(m_proxyModel);
	connect(m_pageList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
			this, SLOT(currentChanged(QModelIndex)));
	m_pageStack->setStackingMode(QStackedLayout::StackOne);
	m_pageList->setFocus();
	selectPage(defaultId);
}

bool PageContainer::selectPage(QString pageId)
{
	// now find what we want to have selected...
	auto page = m_model->findPageEntryById(pageId);
	QModelIndex index;
	if (page)
	{
		index = m_proxyModel->mapFromSource(m_model->index(page->listIndex));
	}
	if(!index.isValid())
	{
		index = m_proxyModel->index(0, 0);
	}
	if (index.isValid())
	{
		m_pageList->setCurrentIndex(index);
		return true;
	}
	return false;
}

void PageContainer::refreshContainer()
{
	m_proxyModel->invalidate();
	if(!m_currentPage->shouldDisplay())
	{
		auto index = m_proxyModel->index(0, 0);
		if(index.isValid())
		{
			m_pageList->setCurrentIndex(index);
		}
		else
		{
			// FIXME: unhandled corner case: what to do when there's no page to select?
		}
	}
}

void PageContainer::createUI()
{
	m_pageStack = new QStackedLayout;
	m_pageList = new PageView;
	m_header = new QLabel();
	m_iconHeader = new IconLabel(this, QIcon(), QSize(24, 24));

	QFont headerLabelFont = m_header->font();
	headerLabelFont.setBold(true);
	const int pointSize = headerLabelFont.pointSize();
	if (pointSize > 0)
		headerLabelFont.setPointSize(pointSize + 2);
	m_header->setFont(headerLabelFont);

	QHBoxLayout *headerHLayout = new QHBoxLayout;
	const int leftMargin = MMC->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
	headerHLayout->addSpacerItem(
		new QSpacerItem(leftMargin, 0, QSizePolicy::Fixed, QSizePolicy::Ignored));
	headerHLayout->addWidget(m_header);
	headerHLayout->addSpacerItem(
		new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
	headerHLayout->addWidget(m_iconHeader);
	const int rightMargin = MMC->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
	headerHLayout->addSpacerItem(
		new QSpacerItem(rightMargin, 0, QSizePolicy::Fixed, QSizePolicy::Ignored));

	m_pageStack->setMargin(0);
	m_pageStack->addWidget(new QWidget(this));

	m_layout = new QGridLayout;
	m_layout->addLayout(headerHLayout, 0, 1, 1, 1);
	m_layout->addWidget(m_pageList, 0, 0, 2, 1);
	m_layout->addLayout(m_pageStack, 1, 1, 1, 1);
	m_layout->setColumnStretch(1, 4);
	setLayout(m_layout);
}

void PageContainer::addButtons(QWidget *buttons)
{
	m_layout->addWidget(buttons, 2, 0, 1, 2);
}

void PageContainer::addButtons(QLayout *buttons)
{
	m_layout->addLayout(buttons, 2, 0, 1, 2);
}

void PageContainer::showPage(int row)
{
	if (m_currentPage)
	{
		m_currentPage->closed();
	}
	if (row != -1)
	{
		m_currentPage = m_model->pages().at(row);
	}
	else
	{
		m_currentPage = nullptr;
	}
	if (m_currentPage)
	{
		m_pageStack->setCurrentIndex(m_currentPage->stackIndex);
		m_header->setText(m_currentPage->displayName());
		m_iconHeader->setIcon(m_currentPage->icon());
		m_currentPage->opened();
	}
	else
	{
		m_pageStack->setCurrentIndex(0);
		m_header->setText(QString());
		m_iconHeader->setIcon(MMC->getThemedIcon("bug"));
	}
}

void PageContainer::help()
{
	if (m_currentPage)
	{
		QString pageId = m_currentPage->helpPage();
		if (pageId.isEmpty())
			return;
		DesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/wiki/" + pageId));
	}
}

void PageContainer::currentChanged(const QModelIndex &current)
{
	showPage(current.isValid() ? m_proxyModel->mapToSource(current).row() : -1);
}

bool PageContainer::prepareToClose()
{
	if(!saveAll())
	{
		return false;
	}
	if (m_currentPage)
	{
		m_currentPage->closed();
	}
	return true;
}

bool PageContainer::saveAll()
{
	for (auto page : m_model->pages())
	{
		if (!page->apply())
			return false;
	}
	return true;
}