2014-06-01 23:49:53 +01:00
|
|
|
/* Copyright 2014 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 "PageDialog.h"
|
|
|
|
#include "gui/Platform.h"
|
|
|
|
#include <QStackedLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSortFilterProxyModel>
|
2014-06-09 23:46:05 +01:00
|
|
|
#include <QUrl>
|
2014-06-01 23:49:53 +01:00
|
|
|
#include "MultiMC.h"
|
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
#include <QListView>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGridLayout>
|
2014-06-09 23:46:05 +01:00
|
|
|
#include <QDesktopServices>
|
2014-06-03 00:34:44 +01:00
|
|
|
#include <settingsobject.h>
|
2014-06-01 23:49:53 +01:00
|
|
|
|
|
|
|
#include "PageDialog_p.h"
|
|
|
|
#include <gui/widgets/IconLabel.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);
|
2014-06-08 19:11:09 +01:00
|
|
|
if(!page->shouldDisplay())
|
|
|
|
return false;
|
|
|
|
// Regular contents check, then check page-filter.
|
|
|
|
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
|
2014-06-01 23:49:53 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-09 00:29:18 +01:00
|
|
|
PageDialog::PageDialog(BasePageProviderPtr pageProvider, QString defaultId, QWidget *parent) : QDialog(parent)
|
2014-06-01 23:49:53 +01:00
|
|
|
{
|
|
|
|
MultiMCPlatform::fixWM_CLASS(this);
|
|
|
|
createUI();
|
|
|
|
setWindowTitle(pageProvider->dialogTitle());
|
2014-06-03 00:34:44 +01:00
|
|
|
restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("PagedGeometry").toByteArray()));
|
2014-06-01 23:49:53 +01:00
|
|
|
|
|
|
|
m_model = new PageModel(this);
|
|
|
|
m_proxyModel = new PageEntryFilterModel(this);
|
|
|
|
int firstIndex = -1;
|
2014-06-09 00:29:18 +01:00
|
|
|
int counter = 0;
|
2014-06-01 23:49:53 +01:00
|
|
|
auto pages = pageProvider->getPages();
|
|
|
|
for(auto page: pages)
|
|
|
|
{
|
2014-06-09 00:29:18 +01:00
|
|
|
page->stackIndex = m_pageStack->addWidget(dynamic_cast<QWidget *>(page));
|
|
|
|
page->listIndex = counter;
|
|
|
|
counter++;
|
2014-06-01 23:49:53 +01:00
|
|
|
if(firstIndex == -1)
|
|
|
|
{
|
2014-06-09 00:29:18 +01:00
|
|
|
firstIndex = page->stackIndex;
|
2014-06-01 23:49:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
connect(m_pageList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
|
|
|
|
this, SLOT(currentChanged(QModelIndex)));
|
|
|
|
m_pageStack->setStackingMode(QStackedLayout::StackOne);
|
|
|
|
m_pageList->setFocus();
|
2014-06-09 00:29:18 +01:00
|
|
|
// now find what we want to have selected...
|
|
|
|
auto page = m_model->findPageEntryById(defaultId);
|
|
|
|
QModelIndex index;
|
|
|
|
if(page)
|
|
|
|
{
|
|
|
|
index = m_proxyModel->mapFromSource(m_model->index(page->listIndex));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
index = m_proxyModel->index(0,0);
|
|
|
|
}
|
|
|
|
if(index.isValid())
|
|
|
|
m_pageList->setCurrentIndex(index);
|
2014-06-01 23:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageDialog::createUI()
|
|
|
|
{
|
|
|
|
m_pageStack = new QStackedLayout;
|
|
|
|
m_filter = new QLineEdit;
|
|
|
|
m_pageList = new PageView;
|
|
|
|
m_header = new QLabel();
|
2014-06-08 19:11:09 +01:00
|
|
|
m_iconHeader = new IconLabel(this, QIcon(), QSize(24,24));
|
2014-06-01 23:49:53 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
m_pageStack->setMargin(0);
|
|
|
|
m_pageStack->addWidget(new QWidget(this));
|
|
|
|
|
2014-06-03 00:34:44 +01:00
|
|
|
QDialogButtonBox *buttons =
|
2014-06-09 23:46:05 +01:00
|
|
|
new QDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Close);
|
|
|
|
buttons->button(QDialogButtonBox::Close)->setDefault(true);
|
|
|
|
connect(buttons->button(QDialogButtonBox::Close), SIGNAL(clicked()), this, SLOT(close()));
|
|
|
|
connect(buttons->button(QDialogButtonBox::Help), SIGNAL(clicked()), this, SLOT(help()));
|
2014-06-01 23:49:53 +01:00
|
|
|
|
|
|
|
QGridLayout *mainGridLayout = new QGridLayout;
|
|
|
|
mainGridLayout->addLayout(headerHLayout, 0, 1, 1, 1);
|
|
|
|
mainGridLayout->addWidget(m_pageList, 0, 0, 2, 1);
|
|
|
|
mainGridLayout->addLayout(m_pageStack, 1, 1, 1, 1);
|
2014-06-09 23:46:05 +01:00
|
|
|
mainGridLayout->addWidget(buttons, 2, 0, 1, 2);
|
2014-06-01 23:49:53 +01:00
|
|
|
mainGridLayout->setColumnStretch(1, 4);
|
|
|
|
setLayout(mainGridLayout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageDialog::showPage(int row)
|
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
if(row != -1)
|
|
|
|
{
|
|
|
|
m_currentPage = m_model->pages().at(row);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_currentPage = nullptr;
|
|
|
|
}
|
|
|
|
if(m_currentPage)
|
2014-06-01 23:49:53 +01:00
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
m_pageStack->setCurrentIndex(m_currentPage->stackIndex);
|
|
|
|
m_header->setText(m_currentPage->displayName());
|
|
|
|
m_iconHeader->setIcon(m_currentPage->icon());
|
2014-06-01 23:49:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pageStack->setCurrentIndex(0);
|
|
|
|
m_header->setText(QString());
|
|
|
|
m_iconHeader->setIcon(QIcon::fromTheme("bug"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 23:46:05 +01:00
|
|
|
void PageDialog::help()
|
2014-06-03 00:34:44 +01:00
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
if(m_currentPage)
|
2014-06-03 00:34:44 +01:00
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
QString pageId = m_currentPage->helpPage();
|
|
|
|
if(pageId.isEmpty())
|
|
|
|
return;
|
|
|
|
QDesktopServices::openUrl(QUrl("https://github.com/MultiMC/MultiMC5/wiki/" + pageId));
|
2014-06-03 00:34:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 23:46:05 +01:00
|
|
|
void PageDialog::currentChanged(const QModelIndex ¤t)
|
2014-06-03 00:34:44 +01:00
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
showPage(current.isValid() ? m_proxyModel->mapToSource(current).row() : -1);
|
2014-06-03 00:34:44 +01:00
|
|
|
}
|
|
|
|
|
2014-06-09 23:46:05 +01:00
|
|
|
void PageDialog::closeEvent(QCloseEvent * event)
|
2014-06-01 23:49:53 +01:00
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
bool accepted = true;
|
2014-06-03 00:34:44 +01:00
|
|
|
for(auto page: m_model->pages())
|
|
|
|
{
|
2014-06-09 23:46:05 +01:00
|
|
|
accepted &= page->apply();
|
|
|
|
}
|
|
|
|
if(accepted)
|
|
|
|
{
|
|
|
|
MMC->settings()->set("PagedGeometry", saveGeometry().toBase64());
|
|
|
|
QDialog::closeEvent(event);
|
2014-06-03 00:34:44 +01:00
|
|
|
}
|
2014-06-01 23:49:53 +01:00
|
|
|
}
|