2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2014-06-29 23:29:08 +05:30
|
|
|
*
|
|
|
|
* 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"
|
2014-07-13 02:32:52 +05:30
|
|
|
|
2014-06-29 23:29:08 +05:30
|
|
|
#include <QStackedLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
#include <QListView>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
2014-07-13 02:32:52 +05:30
|
|
|
#include "MultiMC.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "settings/SettingsObject.h"
|
|
|
|
#include "widgets/IconLabel.h"
|
|
|
|
#include "Platform.h"
|
2014-06-29 23:29:08 +05:30
|
|
|
#include "PageContainer_p.h"
|
2015-03-02 02:50:57 +05:30
|
|
|
#include <MultiMC.h>
|
2014-06-29 23:29:08 +05:30
|
|
|
|
|
|
|
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);
|
2014-07-12 21:28:23 +05:30
|
|
|
if (!page->shouldDisplay())
|
2014-06-29 23:29:08 +05:30
|
|
|
return false;
|
|
|
|
// Regular contents check, then check page-filter.
|
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
PageContainer::PageContainer(BasePageProviderPtr pageProvider, QString defaultId,
|
|
|
|
QWidget *parent)
|
|
|
|
: QWidget(parent)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
createUI();
|
|
|
|
m_model = new PageModel(this);
|
|
|
|
m_proxyModel = new PageEntryFilterModel(this);
|
|
|
|
int firstIndex = -1;
|
|
|
|
int counter = 0;
|
|
|
|
auto pages = pageProvider->getPages();
|
2014-07-12 21:28:23 +05:30
|
|
|
for (auto page : pages)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
page->stackIndex = m_pageStack->addWidget(dynamic_cast<QWidget *>(page));
|
|
|
|
page->listIndex = counter;
|
2015-05-29 05:52:02 +05:30
|
|
|
page->setParentContainer(this);
|
2014-06-29 23:29:08 +05:30
|
|
|
counter++;
|
2014-07-12 21:28:23 +05:30
|
|
|
if (firstIndex == -1)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
firstIndex = page->stackIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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->setModel(m_proxyModel);
|
2014-07-12 21:28:23 +05:30
|
|
|
connect(m_pageList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
|
|
|
|
this, SLOT(currentChanged(QModelIndex)));
|
2014-06-29 23:29:08 +05:30
|
|
|
m_pageStack->setStackingMode(QStackedLayout::StackOne);
|
|
|
|
m_pageList->setFocus();
|
2015-05-29 05:52:02 +05:30
|
|
|
selectPage(defaultId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageContainer::selectPage(QString pageId)
|
|
|
|
{
|
2014-06-29 23:29:08 +05:30
|
|
|
// now find what we want to have selected...
|
2015-05-29 05:52:02 +05:30
|
|
|
auto page = m_model->findPageEntryById(pageId);
|
2014-06-29 23:29:08 +05:30
|
|
|
QModelIndex index;
|
2014-07-12 21:28:23 +05:30
|
|
|
if (page)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
index = m_proxyModel->mapFromSource(m_model->index(page->listIndex));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-12 21:28:23 +05:30
|
|
|
index = m_proxyModel->index(0, 0);
|
2014-06-29 23:29:08 +05:30
|
|
|
}
|
2014-07-12 21:28:23 +05:30
|
|
|
if (index.isValid())
|
2015-05-29 05:52:02 +05:30
|
|
|
{
|
2014-06-29 23:29:08 +05:30
|
|
|
m_pageList->setCurrentIndex(index);
|
2015-05-29 05:52:02 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-06-29 23:29:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void PageContainer::createUI()
|
|
|
|
{
|
|
|
|
m_pageStack = new QStackedLayout;
|
|
|
|
m_filter = new QLineEdit;
|
|
|
|
m_pageList = new PageView;
|
|
|
|
m_header = new QLabel();
|
2014-07-12 21:28:23 +05:30
|
|
|
m_iconHeader = new IconLabel(this, QIcon(), QSize(24, 24));
|
2014-06-29 23:29:08 +05:30
|
|
|
|
|
|
|
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);
|
2014-07-21 03:17:46 +05:30
|
|
|
const int rightMargin = MMC->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
|
|
|
|
headerHLayout->addSpacerItem(
|
|
|
|
new QSpacerItem(rightMargin, 0, QSizePolicy::Fixed, QSizePolicy::Ignored));
|
2014-06-29 23:29:08 +05:30
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-30 05:32:57 +05:30
|
|
|
void PageContainer::addButtons(QLayout *buttons)
|
|
|
|
{
|
|
|
|
m_layout->addLayout(buttons, 2, 0, 1, 2);
|
|
|
|
}
|
|
|
|
|
2014-06-29 23:29:08 +05:30
|
|
|
void PageContainer::showPage(int row)
|
|
|
|
{
|
2014-07-12 21:28:23 +05:30
|
|
|
if (m_currentPage)
|
|
|
|
{
|
|
|
|
m_currentPage->closed();
|
|
|
|
}
|
|
|
|
if (row != -1)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
m_currentPage = m_model->pages().at(row);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_currentPage = nullptr;
|
|
|
|
}
|
2014-07-12 21:28:23 +05:30
|
|
|
if (m_currentPage)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
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());
|
2015-03-02 02:50:57 +05:30
|
|
|
m_iconHeader->setIcon(MMC->getThemedIcon("bug"));
|
2014-06-29 23:29:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageContainer::help()
|
|
|
|
{
|
2014-07-12 21:28:23 +05:30
|
|
|
if (m_currentPage)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
|
|
|
QString pageId = m_currentPage->helpPage();
|
2014-07-12 21:28:23 +05:30
|
|
|
if (pageId.isEmpty())
|
2014-06-29 23:29:08 +05:30
|
|
|
return;
|
|
|
|
QDesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/wiki/" + pageId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageContainer::currentChanged(const QModelIndex ¤t)
|
|
|
|
{
|
|
|
|
showPage(current.isValid() ? m_proxyModel->mapToSource(current).row() : -1);
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
bool PageContainer::requestClose(QCloseEvent *event)
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
2014-07-12 21:28:23 +05:30
|
|
|
for (auto page : m_model->pages())
|
2014-06-29 23:29:08 +05:30
|
|
|
{
|
2014-07-12 21:28:23 +05:30
|
|
|
if (!page->apply())
|
2014-06-29 23:29:08 +05:30
|
|
|
return false;
|
|
|
|
}
|
2014-07-13 02:32:52 +05:30
|
|
|
if (m_currentPage)
|
|
|
|
{
|
|
|
|
m_currentPage->closed();
|
|
|
|
}
|
2014-06-29 23:29:08 +05:30
|
|
|
return true;
|
|
|
|
}
|