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.
|
|
|
|
*/
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
#include "InstanceView.h"
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QtMath>
|
|
|
|
#include <QMouseEvent>
|
2013-12-26 20:16:03 +00:00
|
|
|
#include <QListView>
|
|
|
|
#include <QPersistentModelIndex>
|
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
2014-07-12 10:49:07 +00:00
|
|
|
#include <QCache>
|
2013-12-26 21:40:26 +00:00
|
|
|
#include <QScrollBar>
|
2019-07-21 19:12:05 +00:00
|
|
|
#include <QAccessible>
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2014-07-12 19:13:23 +00:00
|
|
|
#include "VisualGroup.h"
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2013-12-31 16:26:36 +00:00
|
|
|
|
2021-11-20 15:22:22 +00:00
|
|
|
#include <Application.h>
|
2021-10-25 21:51:42 +00:00
|
|
|
#include <InstanceList.h>
|
|
|
|
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
template <typename T> bool listsIntersect(const QList<T> &l1, const QList<T> t2)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
for (auto &item : l1)
|
|
|
|
{
|
|
|
|
if (t2.contains(item))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
InstanceView::InstanceView(QWidget *parent)
|
2018-07-15 12:51:05 +00:00
|
|
|
: QAbstractItemView(parent)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setAutoScroll(true);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
InstanceView::~InstanceView()
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups.clear();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::setModel(QAbstractItemModel *model)
|
2014-03-07 14:46:56 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QAbstractItemView::setModel(model);
|
2021-10-25 21:51:42 +00:00
|
|
|
connect(model, &QAbstractItemModel::modelReset, this, &InstanceView::modelReset);
|
|
|
|
connect(model, &QAbstractItemModel::rowsRemoved, this, &InstanceView::rowsRemoved);
|
2014-03-07 14:46:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::rowsInserted(const QModelIndex &parent, int start, int end)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::modelReset()
|
2016-12-18 23:34:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2016-12-18 23:34:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::rowsRemoved()
|
2016-12-18 23:34:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2016-12-18 23:34:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
|
2019-07-21 19:12:05 +00:00
|
|
|
{
|
|
|
|
QAbstractItemView::currentChanged(current, previous);
|
2021-10-25 21:51:42 +00:00
|
|
|
// TODO: for accessibility support, implement+register a factory, steal QAccessibleTable from Qt and return an instance of it for InstanceView.
|
2019-10-01 12:28:06 +00:00
|
|
|
#ifndef QT_NO_ACCESSIBILITY
|
2019-07-21 19:12:05 +00:00
|
|
|
if (QAccessible::isActive() && current.isValid()) {
|
|
|
|
QAccessibleEvent event(this, QAccessible::Focus);
|
|
|
|
event.setChild(current.row());
|
|
|
|
QAccessible::updateAccessibility(&event);
|
|
|
|
}
|
2019-10-01 12:28:06 +00:00
|
|
|
#endif /* !QT_NO_ACCESSIBILITY */
|
2019-07-21 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-10 07:51:24 +00:00
|
|
|
class LocaleString : public QString
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 12:51:05 +00:00
|
|
|
LocaleString(const char *s) : QString(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
LocaleString(const QString &s) : QString(s)
|
|
|
|
{
|
|
|
|
}
|
2014-11-10 07:51:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator<(const LocaleString &lhs, const LocaleString &rhs)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return (QString::localeAwareCompare(lhs, rhs) < 0);
|
2014-11-10 07:51:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::updateScrollbar()
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
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 - height());
|
|
|
|
}
|
|
|
|
|
|
|
|
verticalScrollBar()->setValue(qMin(previousScroll, verticalScrollBar()->maximum()));
|
2018-01-28 01:04:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::updateGeometries()
|
2018-01-28 01:04:47 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
geometryCache.clear();
|
|
|
|
|
|
|
|
QMap<LocaleString, VisualGroup *> cats;
|
|
|
|
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
const QString groupName = model()->index(i, 0).data(InstanceViewRoles::GroupRole).toString();
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!cats.contains(groupName))
|
|
|
|
{
|
|
|
|
VisualGroup *old = this->category(groupName);
|
|
|
|
if (old)
|
|
|
|
{
|
|
|
|
auto cat = new VisualGroup(old);
|
|
|
|
cats.insert(groupName, cat);
|
|
|
|
cat->update();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto cat = new VisualGroup(groupName, this);
|
2019-08-20 00:58:27 +00:00
|
|
|
if(fVisibility) {
|
|
|
|
cat->collapsed = fVisibility(groupName);
|
|
|
|
}
|
2018-07-15 12:51:05 +00:00
|
|
|
cats.insert(groupName, cat);
|
|
|
|
cat->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups = cats.values();
|
|
|
|
updateScrollbar();
|
|
|
|
viewport()->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
bool InstanceView::isIndexHidden(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
VisualGroup *cat = category(index);
|
|
|
|
if (cat)
|
|
|
|
{
|
|
|
|
return cat->collapsed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
VisualGroup *InstanceView::category(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
return category(index.data(InstanceViewRoles::GroupRole).toString());
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
VisualGroup *InstanceView::category(const QString &cat) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
for (auto group : m_groups)
|
|
|
|
{
|
|
|
|
if (group->text == cat)
|
|
|
|
{
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
VisualGroup *InstanceView::categoryAt(const QPoint &pos, VisualGroup::HitResults & result) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
for (auto group : m_groups)
|
|
|
|
{
|
|
|
|
result = group->hitScan(pos);
|
|
|
|
if(result != VisualGroup::NoHit)
|
|
|
|
{
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = VisualGroup::NoHit;
|
|
|
|
return nullptr;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QString InstanceView::groupNameAt(const QPoint &point)
|
2016-04-10 23:30:50 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
auto group = categoryAt(point + offset(), hitresult);
|
|
|
|
if(group && (hitresult & (VisualGroup::HeaderHit | VisualGroup::BodyHit)))
|
|
|
|
{
|
|
|
|
return group->text;
|
|
|
|
}
|
|
|
|
return QString();
|
2016-04-10 23:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::calculateItemsPerRow() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return qFloor((qreal)(contentWidth()) / (qreal)(itemWidth() + m_spacing));
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::contentWidth() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return width() - m_leftMargin - m_rightMargin;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::itemWidth() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return m_itemWidth;
|
2013-12-30 17:46:12 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::mousePressEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
2018-07-15 12:51:05 +00:00
|
|
|
|
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
|
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
|
|
|
|
|
|
|
m_pressedIndex = index;
|
|
|
|
m_pressedAlreadySelected = selectionModel()->isSelected(m_pressedIndex);
|
|
|
|
m_pressedPosition = geometryPos;
|
|
|
|
|
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
m_pressedCategory = categoryAt(geometryPos, hitresult);
|
|
|
|
if (m_pressedCategory && hitresult & VisualGroup::CheckboxHit)
|
|
|
|
{
|
|
|
|
setState(m_pressedCategory->collapsed ? ExpandingState : CollapsingState);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.isValid() && (index.flags() & Qt::ItemIsEnabled))
|
|
|
|
{
|
|
|
|
if(index != currentIndex())
|
|
|
|
{
|
|
|
|
// FIXME: better!
|
|
|
|
m_currentCursorColumn = -1;
|
|
|
|
}
|
|
|
|
// we disable scrollTo for mouse press so the item doesn't change position
|
|
|
|
// when the user is interacting with it (ie. clicking on it)
|
|
|
|
bool autoScroll = hasAutoScroll();
|
|
|
|
setAutoScroll(false);
|
|
|
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
|
|
|
|
|
|
|
|
setAutoScroll(autoScroll);
|
|
|
|
QRect rect(visualPos, visualPos);
|
|
|
|
setSelection(rect, QItemSelectionModel::ClearAndSelect);
|
|
|
|
|
|
|
|
// signal handlers may change the model
|
|
|
|
emit pressed(index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Forces a finalize() even if mouse is pressed, but not on a item
|
|
|
|
selectionModel()->select(QModelIndex(), QItemSelectionModel::Select);
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::mouseMoveEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QPoint topLeft;
|
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
|
|
|
|
if (state() == ExpandingState || state() == CollapsingState)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state() == DraggingState)
|
|
|
|
{
|
|
|
|
topLeft = m_pressedPosition - offset();
|
|
|
|
if ((topLeft - event->pos()).manhattanLength() > QApplication::startDragDistance())
|
|
|
|
{
|
|
|
|
m_pressedIndex = QModelIndex();
|
|
|
|
startDrag(model()->supportedDragActions());
|
|
|
|
setState(NoState);
|
|
|
|
stopAutoScroll();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectionMode() != SingleSelection)
|
|
|
|
{
|
|
|
|
topLeft = m_pressedPosition - offset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
topLeft = geometryPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pressedIndex.isValid() && (state() != DragSelectingState) &&
|
|
|
|
(event->buttons() != Qt::NoButton) && !selectedIndexes().isEmpty())
|
|
|
|
{
|
|
|
|
setState(DraggingState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event->buttons() & Qt::LeftButton) && selectionModel())
|
|
|
|
{
|
|
|
|
setState(DragSelectingState);
|
|
|
|
|
|
|
|
setSelection(QRect(visualPos, visualPos), QItemSelectionModel::ClearAndSelect);
|
|
|
|
QModelIndex index = indexAt(visualPos);
|
|
|
|
|
|
|
|
// set at the end because it might scroll the view
|
|
|
|
if (index.isValid() && (index != selectionModel()->currentIndex()) &&
|
|
|
|
(index.flags() & Qt::ItemIsEnabled))
|
|
|
|
{
|
|
|
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::mouseReleaseEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
|
|
|
|
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
|
|
|
|
bool click = (index == m_pressedIndex && index.isValid()) ||
|
|
|
|
(m_pressedCategory && m_pressedCategory == categoryAt(geometryPos, hitresult));
|
|
|
|
|
|
|
|
if (click && m_pressedCategory)
|
|
|
|
{
|
|
|
|
if (state() == ExpandingState)
|
|
|
|
{
|
|
|
|
m_pressedCategory->collapsed = false;
|
2019-08-20 00:58:27 +00:00
|
|
|
emit groupStateChanged(m_pressedCategory->text, false);
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2020-01-08 08:00:54 +00:00
|
|
|
m_pressedCategory = nullptr;
|
|
|
|
setState(NoState);
|
2018-07-15 12:51:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (state() == CollapsingState)
|
|
|
|
{
|
|
|
|
m_pressedCategory->collapsed = true;
|
2019-08-20 00:58:27 +00:00
|
|
|
emit groupStateChanged(m_pressedCategory->text, true);
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2020-01-08 08:00:54 +00:00
|
|
|
m_pressedCategory = nullptr;
|
|
|
|
setState(NoState);
|
2018-07-15 12:51:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
|
|
|
|
|
|
|
|
setState(NoState);
|
|
|
|
|
|
|
|
if (click)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit clicked(index);
|
|
|
|
}
|
|
|
|
QStyleOptionViewItem option = viewOptions();
|
|
|
|
if (m_pressedAlreadySelected)
|
|
|
|
{
|
|
|
|
option.state |= QStyle::State_Selected;
|
|
|
|
}
|
|
|
|
if ((model()->flags(index) & Qt::ItemIsEnabled) &&
|
|
|
|
style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
|
|
|
|
{
|
|
|
|
emit activated(index);
|
|
|
|
}
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::mouseDoubleClickEvent(QMouseEvent *event)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
if (!index.isValid() || !(index.flags() & Qt::ItemIsEnabled) || (m_pressedIndex != index))
|
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
QMouseEvent me(
|
|
|
|
QEvent::MouseButtonPress,
|
|
|
|
event->localPos(),
|
|
|
|
event->windowPos(),
|
|
|
|
event->screenPos(),
|
|
|
|
event->button(),
|
|
|
|
event->buttons(),
|
|
|
|
event->modifiers()
|
|
|
|
);
|
2018-07-15 12:51:05 +00:00
|
|
|
mousePressEvent(&me);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// signal handlers may change the model
|
|
|
|
QPersistentModelIndex persistent = index;
|
|
|
|
emit doubleClicked(persistent);
|
2018-11-22 01:02:53 +00:00
|
|
|
|
|
|
|
QStyleOptionViewItem option = viewOptions();
|
|
|
|
if ((model()->flags(index) & Qt::ItemIsEnabled) && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
|
|
|
|
{
|
|
|
|
emit activated(index);
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::paintEvent(QPaintEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
|
|
|
QPainter painter(this->viewport());
|
|
|
|
|
|
|
|
QStyleOptionViewItem option(viewOptions());
|
|
|
|
option.widget = this;
|
|
|
|
|
|
|
|
int wpWidth = viewport()->width();
|
|
|
|
option.rect.setWidth(wpWidth);
|
|
|
|
for (int i = 0; i < m_groups.size(); ++i)
|
|
|
|
{
|
|
|
|
VisualGroup *category = m_groups.at(i);
|
|
|
|
int y = category->verticalPosition();
|
|
|
|
y -= verticalOffset();
|
|
|
|
QRect backup = option.rect;
|
|
|
|
int height = category->totalHeight();
|
|
|
|
option.rect.setTop(y);
|
|
|
|
option.rect.setHeight(height);
|
|
|
|
option.rect.setLeft(m_leftMargin);
|
|
|
|
option.rect.setRight(wpWidth - m_rightMargin);
|
|
|
|
category->drawHeader(&painter, option);
|
|
|
|
y += category->totalHeight() + m_categoryMargin;
|
|
|
|
option.rect = backup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
const QModelIndex index = model()->index(i, 0);
|
|
|
|
if (isIndexHidden(index))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Qt::ItemFlags flags = index.flags();
|
|
|
|
option.rect = visualRect(index);
|
|
|
|
option.features |= QStyleOptionViewItem::WrapText;
|
|
|
|
if (flags & Qt::ItemIsSelectable && selectionModel()->isSelected(index))
|
|
|
|
{
|
|
|
|
option.state |= selectionModel()->isSelected(index) ? QStyle::State_Selected
|
|
|
|
: QStyle::State_None;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
option.state &= ~QStyle::State_Selected;
|
|
|
|
}
|
|
|
|
option.state |= (index == currentIndex()) ? QStyle::State_HasFocus : QStyle::State_None;
|
|
|
|
if (!(flags & Qt::ItemIsEnabled))
|
|
|
|
{
|
|
|
|
option.state &= ~QStyle::State_Enabled;
|
|
|
|
}
|
|
|
|
itemDelegate()->paint(&painter, option, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop indicators for manual reordering...
|
|
|
|
*/
|
2014-02-03 23:40:10 +00:00
|
|
|
#if 0
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!m_lastDragPosition.isNull())
|
|
|
|
{
|
|
|
|
QPair<Group *, int> pair = rowDropPos(m_lastDragPosition);
|
|
|
|
Group *category = pair.first;
|
|
|
|
int row = pair.second;
|
|
|
|
if (category)
|
|
|
|
{
|
|
|
|
int internalRow = row - category->firstItemIndex;
|
|
|
|
QLine line;
|
|
|
|
if (internalRow >= category->numItems())
|
|
|
|
{
|
|
|
|
QRect toTheRightOfRect = visualRect(category->lastItem());
|
|
|
|
line = QLine(toTheRightOfRect.topRight(), toTheRightOfRect.bottomRight());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QRect toTheLeftOfRect = visualRect(model()->index(row, 0));
|
|
|
|
line = QLine(toTheLeftOfRect.topLeft(), toTheLeftOfRect.bottomLeft());
|
|
|
|
}
|
|
|
|
painter.save();
|
|
|
|
painter.setPen(QPen(Qt::black, 3));
|
|
|
|
painter.drawLine(line);
|
|
|
|
painter.restore();
|
|
|
|
}
|
|
|
|
}
|
2014-02-03 23:40:10 +00:00
|
|
|
#endif
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::resizeEvent(QResizeEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
int newItemsPerRow = calculateItemsPerRow();
|
|
|
|
if(newItemsPerRow != m_currentItemsPerRow)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = -1;
|
|
|
|
m_currentItemsPerRow = newItemsPerRow;
|
|
|
|
updateGeometries();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateScrollbar();
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::dragEnterEvent(QDragEnterEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_lastDragPosition = event->pos() + offset();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::dragMoveEvent(QDragMoveEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_lastDragPosition = event->pos() + offset();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::dragLeaveEvent(QDragLeaveEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
viewport()->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::dropEvent(QDropEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
|
|
|
|
stopAutoScroll();
|
|
|
|
setState(NoState);
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
auto mimedata = event->mimeData();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
if (event->source() == this)
|
|
|
|
{
|
|
|
|
if(event->possibleActions() & Qt::MoveAction)
|
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
QPair<VisualGroup *, VisualGroup::HitResults> dropPos = rowDropPos(event->pos());
|
|
|
|
const VisualGroup *group = dropPos.first;
|
|
|
|
auto hitresult = dropPos.second;
|
2018-07-15 12:51:05 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
if (hitresult == VisualGroup::HitResult::NoHit)
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
viewport()->update();
|
|
|
|
return;
|
|
|
|
}
|
2021-10-25 21:51:42 +00:00
|
|
|
auto instanceId = QString::fromUtf8(mimedata->data("application/x-instanceid"));
|
2021-11-20 15:22:22 +00:00
|
|
|
auto instanceList = APPLICATION->instances().get();
|
2021-10-25 21:51:42 +00:00
|
|
|
instanceList->setInstanceGroup(instanceId, group->text);
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
2018-07-15 12:51:05 +00:00
|
|
|
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
}
|
2021-10-25 21:51:42 +00:00
|
|
|
return;
|
2018-07-15 12:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if the action is supported
|
|
|
|
if (!mimedata)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// files dropped from outside?
|
|
|
|
if (mimedata->hasUrls())
|
|
|
|
{
|
|
|
|
auto urls = mimedata->urls();
|
|
|
|
event->accept();
|
|
|
|
emit droppedURLs(urls);
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::startDrag(Qt::DropActions supportedActions)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QModelIndexList indexes = selectionModel()->selectedIndexes();
|
|
|
|
if(indexes.count() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMimeData *data = model()->mimeData(indexes);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QRect rect;
|
|
|
|
QPixmap pixmap = renderToPixmap(indexes, &rect);
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
drag->setMimeData(data);
|
2021-10-27 08:25:18 +00:00
|
|
|
drag->setHotSpot(m_pressedPosition - rect.topLeft());
|
2018-07-15 12:51:05 +00:00
|
|
|
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
|
2021-10-25 21:51:42 +00:00
|
|
|
if (this->defaultDropAction() != Qt::IgnoreAction && (supportedActions & this->defaultDropAction()))
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
defaultDropAction = this->defaultDropAction();
|
|
|
|
}
|
2021-10-25 21:51:42 +00:00
|
|
|
/*auto action = */
|
|
|
|
drag->exec(supportedActions, defaultDropAction);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QRect InstanceView::visualRect(const QModelIndex &index) const
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
const_cast<InstanceView*>(this)->executeDelayedItemsLayout();
|
2019-07-02 00:09:41 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
return geometryRect(index).translated(-offset());
|
2014-02-02 13:27:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QRect InstanceView::geometryRect(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
const_cast<InstanceView*>(this)->executeDelayedItemsLayout();
|
2019-07-02 00:09:41 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!index.isValid() || isIndexHidden(index) || index.column() > 0)
|
|
|
|
{
|
|
|
|
return QRect();
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
int row = index.row();
|
|
|
|
if(geometryCache.contains(row))
|
|
|
|
{
|
|
|
|
return *geometryCache[row];
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
const VisualGroup *cat = category(index);
|
|
|
|
QPair<int, int> pos = cat->positionOf(index);
|
|
|
|
int x = pos.first;
|
|
|
|
// int y = pos.second;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QRect out;
|
|
|
|
out.setTop(cat->verticalPosition() + cat->headerHeight() + 5 + cat->rowTopOf(index));
|
|
|
|
out.setLeft(m_spacing + x * (itemWidth() + m_spacing));
|
|
|
|
out.setSize(itemDelegate()->sizeHint(viewOptions(), index));
|
|
|
|
geometryCache.insert(row, new QRect(out));
|
|
|
|
return out;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QModelIndex InstanceView::indexAt(const QPoint &point) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
const_cast<InstanceView*>(this)->executeDelayedItemsLayout();
|
2016-10-26 16:12:33 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
|
|
|
if (visualRect(index).contains(point))
|
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::setSelection(const QRect &rect, const QItemSelectionModel::SelectionFlags commands)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2019-07-02 00:09:41 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
|
|
|
QRect itemRect = visualRect(index);
|
|
|
|
if (itemRect.intersects(rect))
|
|
|
|
{
|
|
|
|
selectionModel()->select(index, commands);
|
|
|
|
update(itemRect.translated(-offset()));
|
|
|
|
}
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QPixmap InstanceView::renderToPixmap(const QModelIndexList &indices, QRect *r) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Q_ASSERT(r);
|
|
|
|
auto paintPairs = draggablePaintPairs(indices, r);
|
|
|
|
if (paintPairs.isEmpty())
|
|
|
|
{
|
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
QPixmap pixmap(r->size());
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
QStyleOptionViewItem option = viewOptions();
|
|
|
|
option.state |= QStyle::State_Selected;
|
|
|
|
for (int j = 0; j < paintPairs.count(); ++j)
|
|
|
|
{
|
|
|
|
option.rect = paintPairs.at(j).first.translated(-r->topLeft());
|
|
|
|
const QModelIndex ¤t = paintPairs.at(j).second;
|
|
|
|
itemDelegate()->paint(&painter, option, current);
|
|
|
|
}
|
|
|
|
return pixmap;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QList<QPair<QRect, QModelIndex>> InstanceView::draggablePaintPairs(const QModelIndexList &indices, QRect *r) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
Q_ASSERT(r);
|
|
|
|
QRect &rect = *r;
|
|
|
|
QList<QPair<QRect, QModelIndex>> ret;
|
|
|
|
for (int i = 0; i < indices.count(); ++i)
|
|
|
|
{
|
|
|
|
const QModelIndex &index = indices.at(i);
|
|
|
|
const QRect current = geometryRect(index);
|
|
|
|
ret += qMakePair(current, index);
|
|
|
|
rect |= current;
|
|
|
|
}
|
|
|
|
return ret;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
bool InstanceView::isDragEventAccepted(QDropEvent *event)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return true;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QPair<VisualGroup *, VisualGroup::HitResults> InstanceView::rowDropPos(const QPoint &pos)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2021-10-25 21:51:42 +00:00
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
auto group = categoryAt(pos + offset(), hitresult);
|
|
|
|
return qMakePair<VisualGroup*, int>(group, hitresult);
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-30 17:46:12 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QPoint InstanceView::offset() const
|
2013-12-30 17:46:12 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return QPoint(horizontalOffset(), verticalOffset());
|
2013-12-30 17:46:12 +00:00
|
|
|
}
|
2014-02-02 13:27:43 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QRegion InstanceView::visualRegionForSelection(const QItemSelection &selection) const
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QRegion region;
|
|
|
|
for (auto &range : selection)
|
|
|
|
{
|
|
|
|
int start_row = range.top();
|
|
|
|
int end_row = range.bottom();
|
|
|
|
for (int row = start_row; row <= end_row; ++row)
|
|
|
|
{
|
|
|
|
int start_column = range.left();
|
|
|
|
int end_column = range.right();
|
|
|
|
for (int column = start_column; column <= end_column; ++column)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(row, column, rootIndex());
|
|
|
|
region += visualRect(index); // OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return region;
|
2014-02-02 13:27:43 +00:00
|
|
|
}
|
2014-07-12 19:13:23 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
QModelIndex InstanceView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
|
2018-07-15 12:51:05 +00:00
|
|
|
{
|
|
|
|
auto current = currentIndex();
|
|
|
|
if(!current.isValid())
|
|
|
|
{
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
auto cat = category(current);
|
|
|
|
int group_index = m_groups.indexOf(cat);
|
|
|
|
if(group_index < 0)
|
|
|
|
return current;
|
|
|
|
|
|
|
|
QPair<int, int> pos = cat->positionOf(current);
|
|
|
|
int column = pos.first;
|
|
|
|
int row = pos.second;
|
|
|
|
if(m_currentCursorColumn < 0)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = column;
|
|
|
|
}
|
|
|
|
switch(cursorAction)
|
|
|
|
{
|
|
|
|
case MoveUp:
|
|
|
|
{
|
|
|
|
if(row == 0)
|
|
|
|
{
|
|
|
|
int prevgroupindex = group_index-1;
|
|
|
|
while(prevgroupindex >= 0)
|
|
|
|
{
|
|
|
|
auto prevgroup = m_groups[prevgroupindex];
|
|
|
|
if(prevgroup->collapsed)
|
|
|
|
{
|
|
|
|
prevgroupindex--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int newRow = prevgroup->numRows() - 1;
|
|
|
|
int newRowSize = prevgroup->rows[newRow].size();
|
|
|
|
int newColumn = m_currentCursorColumn;
|
|
|
|
if (m_currentCursorColumn >= newRowSize)
|
|
|
|
{
|
|
|
|
newColumn = newRowSize - 1;
|
|
|
|
}
|
|
|
|
return prevgroup->rows[newRow][newColumn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int newRow = row - 1;
|
|
|
|
int newRowSize = cat->rows[newRow].size();
|
|
|
|
int newColumn = m_currentCursorColumn;
|
|
|
|
if (m_currentCursorColumn >= newRowSize)
|
|
|
|
{
|
|
|
|
newColumn = newRowSize - 1;
|
|
|
|
}
|
|
|
|
return cat->rows[newRow][newColumn];
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
case MoveDown:
|
|
|
|
{
|
|
|
|
if(row == cat->rows.size() - 1)
|
|
|
|
{
|
|
|
|
int nextgroupindex = group_index+1;
|
|
|
|
while (nextgroupindex < m_groups.size())
|
|
|
|
{
|
|
|
|
auto nextgroup = m_groups[nextgroupindex];
|
|
|
|
if(nextgroup->collapsed)
|
|
|
|
{
|
|
|
|
nextgroupindex++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int newRowSize = nextgroup->rows[0].size();
|
|
|
|
int newColumn = m_currentCursorColumn;
|
|
|
|
if (m_currentCursorColumn >= newRowSize)
|
|
|
|
{
|
|
|
|
newColumn = newRowSize - 1;
|
|
|
|
}
|
|
|
|
return nextgroup->rows[0][newColumn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int newRow = row + 1;
|
|
|
|
int newRowSize = cat->rows[newRow].size();
|
|
|
|
int newColumn = m_currentCursorColumn;
|
|
|
|
if (m_currentCursorColumn >= newRowSize)
|
|
|
|
{
|
|
|
|
newColumn = newRowSize - 1;
|
|
|
|
}
|
|
|
|
return cat->rows[newRow][newColumn];
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
case MoveLeft:
|
|
|
|
{
|
|
|
|
if(column > 0)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = column - 1;
|
|
|
|
return cat->rows[row][column - 1];
|
|
|
|
}
|
|
|
|
// TODO: moving to previous line
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
case MoveRight:
|
|
|
|
{
|
|
|
|
if(column < cat->rows[row].size() - 1)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = column + 1;
|
|
|
|
return cat->rows[row][column + 1];
|
|
|
|
}
|
|
|
|
// TODO: moving to next line
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
case MoveHome:
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = 0;
|
|
|
|
return cat->rows[row][0];
|
|
|
|
}
|
|
|
|
case MoveEnd:
|
|
|
|
{
|
|
|
|
auto last = cat->rows[row].size() - 1;
|
|
|
|
m_currentCursorColumn = last;
|
|
|
|
return cat->rows[row][last];
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return current;
|
2014-02-02 17:30:52 +00:00
|
|
|
}
|
2014-07-12 19:13:23 +00:00
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::horizontalOffset() const
|
2014-07-12 19:13:23 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return horizontalScrollBar()->value();
|
2014-07-12 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::verticalOffset() const
|
2014-07-12 19:13:23 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return verticalScrollBar()->value();
|
2014-07-12 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::scrollContentsBy(int dx, int dy)
|
2014-07-12 19:13:23 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
scrollDirtyRegion(dx, dy);
|
|
|
|
viewport()->scroll(dx, dy);
|
2014-07-12 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
void InstanceView::scrollTo(const QModelIndex &index, ScrollHint hint)
|
2014-07-12 19:13:23 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2014-07-12 19:13:23 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
const QRect rect = visualRect(index);
|
|
|
|
if (hint == EnsureVisible && viewport()->rect().contains(rect))
|
|
|
|
{
|
|
|
|
viewport()->update(rect);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-12 19:13:23 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
verticalScrollBar()->setValue(verticalScrollToValue(index, rect, hint));
|
2014-07-12 19:13:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:51:42 +00:00
|
|
|
int InstanceView::verticalScrollToValue(const QModelIndex &index, const QRect &rect, QListView::ScrollHint hint) const
|
2014-07-12 19:13:23 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
const QRect area = viewport()->rect();
|
|
|
|
const bool above = (hint == QListView::EnsureVisible && rect.top() < area.top());
|
|
|
|
const bool below = (hint == QListView::EnsureVisible && rect.bottom() > area.bottom());
|
|
|
|
|
|
|
|
int verticalValue = verticalScrollBar()->value();
|
|
|
|
QRect adjusted = rect.adjusted(-spacing(), -spacing(), spacing(), spacing());
|
|
|
|
if (hint == QListView::PositionAtTop || above)
|
|
|
|
verticalValue += adjusted.top();
|
|
|
|
else if (hint == QListView::PositionAtBottom || below)
|
|
|
|
verticalValue += qMin(adjusted.top(), adjusted.bottom() - area.height() + 1);
|
|
|
|
else if (hint == QListView::PositionAtCenter)
|
|
|
|
verticalValue += adjusted.top() - ((area.height() - adjusted.height()) / 2);
|
|
|
|
return verticalValue;
|
2014-07-12 19:13:23 +00:00
|
|
|
}
|