2021-01-18 07:28:54 +00:00
|
|
|
/* Copyright 2013-2021 MultiMC Contributors
|
2017-05-22 21:42:47 +00:00
|
|
|
*
|
|
|
|
* 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-01-31 21:51:45 +00:00
|
|
|
#pragma once
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
#include <QListView>
|
|
|
|
#include <QLineEdit>
|
2014-02-02 13:27:43 +00:00
|
|
|
#include <QScrollBar>
|
2014-07-12 10:49:07 +00:00
|
|
|
#include <QCache>
|
2016-04-10 23:30:50 +00:00
|
|
|
#include "VisualGroup.h"
|
2019-08-20 00:58:27 +00:00
|
|
|
#include <functional>
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
struct InstanceViewRoles
|
2013-12-30 22:39:10 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
GroupRole = Qt::UserRole,
|
|
|
|
ProgressValueRole,
|
|
|
|
ProgressMaximumRole
|
|
|
|
};
|
2013-12-30 22:39:10 +00:00
|
|
|
};
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
class InstanceView : public QAbstractItemView
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Q_OBJECT
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
public:
|
2021-10-25 21:51:42 +00:00
|
|
|
InstanceView(QWidget *parent = 0);
|
|
|
|
~InstanceView();
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
void setModel(QAbstractItemModel *model) override;
|
2014-03-07 14:46:56 +00:00
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
using visibilityFunction = std::function<bool(const QString &)>;
|
|
|
|
void setSourceOfGroupCollapseStatus(visibilityFunction f) {
|
|
|
|
fVisibility = f;
|
|
|
|
}
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
/// return geometry rectangle occupied by the specified model item
|
|
|
|
QRect geometryRect(const QModelIndex &index) const;
|
|
|
|
/// return visual rectangle occupied by the specified model item
|
|
|
|
virtual QRect visualRect(const QModelIndex &index) const override;
|
|
|
|
/// get the model index at the specified visual point
|
|
|
|
virtual QModelIndex indexAt(const QPoint &point) const override;
|
|
|
|
QString groupNameAt(const QPoint &point);
|
2019-08-20 00:58:27 +00:00
|
|
|
void setSelection(const QRect &rect, const QItemSelectionModel::SelectionFlags commands) override;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
virtual int horizontalOffset() const override;
|
|
|
|
virtual int verticalOffset() const override;
|
|
|
|
virtual void scrollContentsBy(int dx, int dy) override;
|
|
|
|
virtual void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override;
|
2014-02-02 09:26:38 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
|
2014-02-02 09:26:38 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
virtual QRegion visualRegionForSelection(const QItemSelection &selection) const override;
|
2014-02-02 09:26:38 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
int spacing() const
|
|
|
|
{
|
|
|
|
return m_spacing;
|
|
|
|
};
|
2015-09-15 22:21:50 +00:00
|
|
|
|
|
|
|
public slots:
|
2018-07-15 12:51:05 +00:00
|
|
|
virtual void updateGeometries() override;
|
2015-09-15 22:21:50 +00:00
|
|
|
|
|
|
|
protected slots:
|
2021-10-25 21:51:42 +00:00
|
|
|
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) override;
|
2018-07-15 12:51:05 +00:00
|
|
|
virtual void rowsInserted(const QModelIndex &parent, int start, int end) override;
|
|
|
|
virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override;
|
|
|
|
void modelReset();
|
|
|
|
void rowsRemoved();
|
2019-07-21 19:12:05 +00:00
|
|
|
void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2017-04-22 04:11:26 +00:00
|
|
|
signals:
|
2018-07-15 12:51:05 +00:00
|
|
|
void droppedURLs(QList<QUrl> urls);
|
2019-08-20 00:58:27 +00:00
|
|
|
void groupStateChanged(QString group, bool collapsed);
|
2017-04-22 04:11:26 +00:00
|
|
|
|
2013-12-24 10:47:30 +00:00
|
|
|
protected:
|
2021-10-25 21:51:42 +00:00
|
|
|
bool isIndexHidden(const QModelIndex &index) const override;
|
2018-07-15 12:51:05 +00:00
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *event) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
|
|
|
void mouseDoubleClickEvent(QMouseEvent *event) override;
|
|
|
|
void paintEvent(QPaintEvent *event) override;
|
|
|
|
void resizeEvent(QResizeEvent *event) override;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
void dragEnterEvent(QDragEnterEvent *event) override;
|
|
|
|
void dragMoveEvent(QDragMoveEvent *event) override;
|
|
|
|
void dragLeaveEvent(QDragLeaveEvent *event) override;
|
|
|
|
void dropEvent(QDropEvent *event) override;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
void startDrag(Qt::DropActions supportedActions) override;
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
void updateScrollbar();
|
2018-01-28 01:04:47 +00:00
|
|
|
|
2013-12-24 10:47:30 +00:00
|
|
|
private:
|
2018-07-15 12:51:05 +00:00
|
|
|
friend struct VisualGroup;
|
|
|
|
QList<VisualGroup *> m_groups;
|
|
|
|
|
2019-08-20 00:58:27 +00:00
|
|
|
visibilityFunction fVisibility;
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
// geometry
|
|
|
|
int m_leftMargin = 5;
|
|
|
|
int m_rightMargin = 5;
|
|
|
|
int m_bottomMargin = 5;
|
|
|
|
int m_categoryMargin = 5;
|
|
|
|
int m_spacing = 5;
|
|
|
|
int m_itemWidth = 100;
|
|
|
|
int m_currentItemsPerRow = -1;
|
|
|
|
int m_currentCursorColumn= -1;
|
|
|
|
mutable QCache<int, QRect> geometryCache;
|
|
|
|
|
|
|
|
// point where the currently active mouse action started in geometry coordinates
|
|
|
|
QPoint m_pressedPosition;
|
|
|
|
QPersistentModelIndex m_pressedIndex;
|
|
|
|
bool m_pressedAlreadySelected;
|
|
|
|
VisualGroup *m_pressedCategory;
|
|
|
|
QItemSelectionModel::SelectionFlag m_ctrlDragSelectionFlag;
|
|
|
|
QPoint m_lastDragPosition;
|
|
|
|
|
|
|
|
VisualGroup *category(const QModelIndex &index) const;
|
|
|
|
VisualGroup *category(const QString &cat) const;
|
|
|
|
VisualGroup *categoryAt(const QPoint &pos, VisualGroup::HitResults & result) const;
|
|
|
|
|
|
|
|
int itemsPerRow() const
|
|
|
|
{
|
|
|
|
return m_currentItemsPerRow;
|
|
|
|
};
|
|
|
|
int contentWidth() const;
|
2014-07-12 19:13:23 +00:00
|
|
|
|
|
|
|
private: /* methods */
|
2018-07-15 12:51:05 +00:00
|
|
|
int itemWidth() const;
|
|
|
|
int calculateItemsPerRow() const;
|
2021-10-25 21:51:42 +00:00
|
|
|
int verticalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const;
|
2018-07-15 12:51:05 +00:00
|
|
|
QPixmap renderToPixmap(const QModelIndexList &indices, QRect *r) const;
|
2021-10-25 21:51:42 +00:00
|
|
|
QList<QPair<QRect, QModelIndex>> draggablePaintPairs(const QModelIndexList &indices, QRect *r) const;
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
bool isDragEventAccepted(QDropEvent *event);
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QPair<VisualGroup *, VisualGroup::HitResults> rowDropPos(const QPoint &pos);
|
2013-12-30 17:46:12 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QPoint offset() const;
|
2013-12-24 10:47:30 +00:00
|
|
|
};
|