2022-03-20 19:01:08 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2014-06-29 18:59:08 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* 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, version 3.
|
2014-06-29 18:59:08 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* 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.
|
2014-06-29 18:59:08 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
2014-06-29 18:59:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PageContainer.h"
|
2022-03-06 10:31:50 +00:00
|
|
|
#include "BuildConfig.h"
|
2021-11-22 02:55:16 +00:00
|
|
|
#include "PageContainer_p.h"
|
2014-07-12 22:02:52 +01:00
|
|
|
|
2014-06-29 18:59:08 +01:00
|
|
|
#include <QStackedLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
#include <QListView>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGridLayout>
|
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "settings/SettingsObject.h"
|
2021-11-22 02:55:16 +00:00
|
|
|
|
|
|
|
#include "ui/widgets/IconLabel.h"
|
|
|
|
|
|
|
|
#include "DesktopServices.h"
|
|
|
|
#include "Application.h"
|
2014-06-29 18:59:08 +01:00
|
|
|
|
|
|
|
class PageEntryFilterModel : public QSortFilterProxyModel
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 13:51:05 +01:00
|
|
|
explicit PageEntryFilterModel(QObject *parent = 0) : QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
}
|
2014-06-29 18:59:08 +01:00
|
|
|
|
|
|
|
protected:
|
2018-07-15 13:51:05 +01:00
|
|
|
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
|
|
|
{
|
2022-05-02 18:48:37 +01:00
|
|
|
const QString pattern = filterRegularExpression().pattern();
|
2018-07-15 13:51:05 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-06-29 18:59:08 +01:00
|
|
|
};
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
PageContainer::PageContainer(BasePageProvider *pageProvider, QString defaultId,
|
2018-07-15 13:51:05 +01:00
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
2014-06-29 18:59:08 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
createUI();
|
|
|
|
m_model = new PageModel(this);
|
|
|
|
m_proxyModel = new PageEntryFilterModel(this);
|
|
|
|
int counter = 0;
|
|
|
|
auto pages = pageProvider->getPages();
|
|
|
|
for (auto page : pages)
|
|
|
|
{
|
2023-05-28 10:15:39 +01:00
|
|
|
auto widget = dynamic_cast<QWidget *>(page);
|
|
|
|
widget->setParent(this);
|
|
|
|
page->stackIndex = m_pageStack->addWidget(widget);
|
2018-07-15 13:51:05 +01:00
|
|
|
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);
|
2015-05-29 01:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PageContainer::selectPage(QString pageId)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
// 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;
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
2022-10-14 18:09:41 +01:00
|
|
|
BasePage* PageContainer::getPage(QString pageId)
|
|
|
|
{
|
|
|
|
return m_model->findPageEntryById(pageId);
|
|
|
|
}
|
|
|
|
|
2016-11-06 23:18:27 +00:00
|
|
|
void PageContainer::refreshContainer()
|
2016-08-06 14:39:29 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
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?
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 14:39:29 +01:00
|
|
|
}
|
|
|
|
|
2014-06-29 18:59:08 +01:00
|
|
|
void PageContainer::createUI()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
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;
|
2021-11-20 15:22:22 +00:00
|
|
|
const int leftMargin = APPLICATION->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
|
2019-08-02 22:52:19 +01:00
|
|
|
headerHLayout->addSpacerItem(new QSpacerItem(leftMargin, 0, QSizePolicy::Fixed, QSizePolicy::Ignored));
|
2018-07-15 13:51:05 +01:00
|
|
|
headerHLayout->addWidget(m_header);
|
2019-08-02 22:52:19 +01:00
|
|
|
headerHLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
|
2018-07-15 13:51:05 +01:00
|
|
|
headerHLayout->addWidget(m_iconHeader);
|
2021-11-20 15:22:22 +00:00
|
|
|
const int rightMargin = APPLICATION->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
|
2019-08-02 22:52:19 +01:00
|
|
|
headerHLayout->addSpacerItem(new QSpacerItem(rightMargin, 0, QSizePolicy::Fixed, QSizePolicy::Ignored));
|
|
|
|
headerHLayout->setContentsMargins(0, 6, 0, 0);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-05-02 18:10:45 +01:00
|
|
|
m_pageStack->setContentsMargins(0, 0, 0, 0);
|
2018-07-15 13:51:05 +01:00
|
|
|
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);
|
2019-08-02 22:52:19 +01:00
|
|
|
m_layout->setContentsMargins(0,0,0,6);
|
2018-07-15 13:51:05 +01:00
|
|
|
setLayout(m_layout);
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:40:50 +00:00
|
|
|
void PageContainer::retranslate()
|
|
|
|
{
|
|
|
|
if (m_currentPage)
|
|
|
|
m_header->setText(m_currentPage->displayName());
|
2022-02-22 18:23:53 +00:00
|
|
|
|
|
|
|
for (auto page : m_model->pages())
|
|
|
|
page->retranslate();
|
2022-02-22 17:40:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 18:59:08 +01:00
|
|
|
void PageContainer::addButtons(QWidget *buttons)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_layout->addWidget(buttons, 2, 0, 1, 2);
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
2014-06-30 01:02:57 +01:00
|
|
|
void PageContainer::addButtons(QLayout *buttons)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_layout->addLayout(buttons, 2, 0, 1, 2);
|
2014-06-30 01:02:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-29 18:59:08 +01:00
|
|
|
void PageContainer::showPage(int row)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
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());
|
2021-11-20 15:22:22 +00:00
|
|
|
m_iconHeader->setIcon(APPLICATION->getThemedIcon("bug"));
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageContainer::help()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (m_currentPage)
|
|
|
|
{
|
|
|
|
QString pageId = m_currentPage->helpPage();
|
|
|
|
if (pageId.isEmpty())
|
|
|
|
return;
|
2022-03-06 10:31:50 +00:00
|
|
|
DesktopServices::openUrl(QUrl(BuildConfig.HELP_URL.arg(pageId)));
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageContainer::currentChanged(const QModelIndex ¤t)
|
|
|
|
{
|
2022-07-15 15:20:08 +01:00
|
|
|
int selected_index = current.isValid() ? m_proxyModel->mapToSource(current).row() : -1;
|
|
|
|
|
|
|
|
auto* selected = m_model->pages().at(selected_index);
|
|
|
|
auto* previous = m_currentPage;
|
|
|
|
|
|
|
|
emit selectedPageChanged(previous, selected);
|
|
|
|
|
|
|
|
showPage(selected_index);
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 02:36:29 +01:00
|
|
|
bool PageContainer::prepareToClose()
|
2014-06-29 18:59:08 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if(!saveAll())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (m_currentPage)
|
|
|
|
{
|
|
|
|
m_currentPage->closed();
|
|
|
|
}
|
|
|
|
return true;
|
2014-06-29 18:59:08 +01:00
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
|
|
|
|
bool PageContainer::saveAll()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
for (auto page : m_model->pages())
|
|
|
|
{
|
|
|
|
if (!page->apply())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
2022-02-22 17:40:50 +00:00
|
|
|
|
|
|
|
void PageContainer::changeEvent(QEvent* event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::LanguageChange) {
|
|
|
|
retranslate();
|
|
|
|
}
|
|
|
|
QWidget::changeEvent(event);
|
|
|
|
}
|