/* 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.
*/
#include "InstanceView.h"
#include <QPainter>
#include <QApplication>
#include <QtMath>
#include <QMouseEvent>
#include <QListView>
#include <QPersistentModelIndex>
#include <QDrag>
#include <QMimeData>
#include <QCache>
#include <QScrollBar>
#include <QAccessible>
#include "VisualGroup.h"
#include <QDebug>
#include <Application.h>
#include <InstanceList.h>
template <typename T> bool listsIntersect(const QList<T> &l1, const QList<T> t2)
{
for (auto &item : l1)
{
if (t2.contains(item))
{
return true;
}
}
return false;
}
InstanceView::InstanceView(QWidget *parent)
: QAbstractItemView(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setAcceptDrops(true);
setAutoScroll(true);
}
InstanceView::~InstanceView()
{
qDeleteAll(m_groups);
m_groups.clear();
}
void InstanceView::setModel(QAbstractItemModel *model)
{
QAbstractItemView::setModel(model);
connect(model, &QAbstractItemModel::modelReset, this, &InstanceView::modelReset);
connect(model, &QAbstractItemModel::rowsRemoved, this, &InstanceView::rowsRemoved);
}
void InstanceView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
scheduleDelayedItemsLayout();
}
void InstanceView::rowsInserted(const QModelIndex &parent, int start, int end)
{
scheduleDelayedItemsLayout();
}
void InstanceView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
scheduleDelayedItemsLayout();
}
void InstanceView::modelReset()
{
scheduleDelayedItemsLayout();
}
void InstanceView::rowsRemoved()
{
scheduleDelayedItemsLayout();
}
void InstanceView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
{
QAbstractItemView::currentChanged(current, previous);
// TODO: for accessibility support, implement+register a factory, steal QAccessibleTable from Qt and return an instance of it for InstanceView.
#ifndef QT_NO_ACCESSIBILITY
if (QAccessible::isActive() && current.isValid()) {
QAccessibleEvent event(this, QAccessible::Focus);
event.setChild(current.row());
QAccessible::updateAccessibility(&event);
}
#endif /* !QT_NO_ACCESSIBILITY */
}
class LocaleString : public QString
{
public:
LocaleString(const char *s) : QString(s)
{
}
LocaleString(const QString &s) : QString(s)
{
}
};
inline bool operator<(const LocaleString &lhs, const LocaleString &rhs)
{
return (QString::localeAwareCompare(lhs, rhs) < 0);
}
void InstanceView::updateScrollbar()
{
int previousScroll = verticalScrollBar()->value();
if (m_groups.isEmpty())
{
verticalScrollBar()->setRange(0, 0);
}
else
{
int totalHeight = 0;
// top margin
totalHeight += m_categoryMargin;
int itemScroll = 0;
for (auto category : m_groups)
{
category->m_verticalPosition = totalHeight;
totalHeight += category->totalHeight() + m_categoryMargin;
if(!itemScroll && category->totalHeight() != 0)
{
itemScroll = category->contentHeight() / category->numRows();
}
}
// do not divide by zero
if(itemScroll == 0)
itemScroll = 64;
totalHeight += m_bottomMargin;
verticalScrollBar()->setSingleStep ( itemScroll );
const int rowsPerPage = qMax ( viewport()->height() / itemScroll, 1 );
verticalScrollBar()->setPageStep ( rowsPerPage * itemScroll );
verticalScrollBar()->setRange(0, totalHeight -