2017-05-22 22:42:47 +01:00
|
|
|
/* Copyright 2013-2017 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.
|
|
|
|
*/
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
#include "GroupView.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 11:49:07 +01:00
|
|
|
#include <QCache>
|
2013-12-26 21:40:26 +00:00
|
|
|
#include <QScrollBar>
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
#include "VisualGroup.h"
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2013-12-31 16:26:36 +00:00
|
|
|
|
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
|
|
|
{
|
2014-01-31 21:51:45 +00:00
|
|
|
for (auto &item : l1)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
if (t2.contains(item))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
GroupView::GroupView(QWidget *parent)
|
2014-07-12 20:13:23 +01:00
|
|
|
: QAbstractItemView(parent)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
2013-12-26 20:16:03 +00:00
|
|
|
setAcceptDrops(true);
|
2014-07-12 20:13:23 +01:00
|
|
|
setAutoScroll(true);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
GroupView::~GroupView()
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-03 23:40:10 +00:00
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups.clear();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 14:46:56 +00:00
|
|
|
void GroupView::setModel(QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
QAbstractItemView::setModel(model);
|
|
|
|
connect(model, &QAbstractItemModel::modelReset, this, &GroupView::modelReset);
|
2016-12-18 23:34:03 +00:00
|
|
|
connect(model, &QAbstractItemModel::rowsRemoved, this, &GroupView::rowsRemoved);
|
2014-03-07 14:46:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
|
2014-01-31 21:58:57 +00:00
|
|
|
const QVector<int> &roles)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-09 23:51:52 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::rowsInserted(const QModelIndex &parent, int start, int end)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-09 23:51:52 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-09 23:51:52 +00:00
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 23:34:03 +00:00
|
|
|
void GroupView::modelReset()
|
|
|
|
{
|
|
|
|
scheduleDelayedItemsLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupView::rowsRemoved()
|
|
|
|
{
|
|
|
|
scheduleDelayedItemsLayout();
|
|
|
|
}
|
|
|
|
|
2014-11-10 07:51:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::updateGeometries()
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 11:49:07 +01:00
|
|
|
geometryCache.clear();
|
2013-12-30 22:10:53 +00:00
|
|
|
int previousScroll = verticalScrollBar()->value();
|
|
|
|
|
2014-11-10 07:51:24 +00:00
|
|
|
QMap<LocaleString, VisualGroup *> cats;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
2016-12-18 23:34:03 +00:00
|
|
|
const QString groupName = model()->index(i, 0).data(GroupViewRoles::GroupRole).toString();
|
2014-02-03 23:40:10 +00:00
|
|
|
if (!cats.contains(groupName))
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup *old = this->category(groupName);
|
2013-12-24 10:47:30 +00:00
|
|
|
if (old)
|
|
|
|
{
|
2016-12-18 23:34:03 +00:00
|
|
|
auto cat = new VisualGroup(old);
|
|
|
|
cats.insert(groupName, cat);
|
|
|
|
cat->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-18 23:34:03 +00:00
|
|
|
auto cat = new VisualGroup(groupName, this);
|
|
|
|
cats.insert(groupName, cat);
|
|
|
|
cat->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 23:40:10 +00:00
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups = cats.values();
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-02-03 23:40:10 +00:00
|
|
|
if (m_groups.isEmpty())
|
2013-12-26 21:40:26 +00:00
|
|
|
{
|
|
|
|
verticalScrollBar()->setRange(0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int totalHeight = 0;
|
2014-02-08 20:46:29 +00:00
|
|
|
// top margin
|
|
|
|
totalHeight += m_categoryMargin;
|
2014-02-09 20:04:00 +00:00
|
|
|
int itemScroll = 0;
|
2014-02-03 23:40:10 +00:00
|
|
|
for (auto category : m_groups)
|
2013-12-26 21:40:26 +00:00
|
|
|
{
|
2014-02-08 20:46:29 +00:00
|
|
|
category->m_verticalPosition = totalHeight;
|
2013-12-26 21:40:26 +00:00
|
|
|
totalHeight += category->totalHeight() + m_categoryMargin;
|
2014-02-09 20:04:00 +00:00
|
|
|
if(!itemScroll && category->totalHeight() != 0)
|
|
|
|
{
|
|
|
|
itemScroll = category->contentHeight() / category->numRows();
|
|
|
|
}
|
2013-12-26 21:40:26 +00:00
|
|
|
}
|
2014-02-09 20:04:00 +00:00
|
|
|
// do not divide by zero
|
|
|
|
if(itemScroll == 0)
|
|
|
|
itemScroll = 64;
|
|
|
|
|
2013-12-30 17:46:12 +00:00
|
|
|
totalHeight += m_bottomMargin;
|
2014-02-09 19:45:18 +00:00
|
|
|
verticalScrollBar()->setSingleStep ( itemScroll );
|
|
|
|
const int rowsPerPage = qMax ( viewport()->height() / itemScroll, 1 );
|
|
|
|
verticalScrollBar()->setPageStep ( rowsPerPage * itemScroll );
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
verticalScrollBar()->setRange(0, totalHeight - height());
|
2013-12-26 21:40:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 22:10:53 +00:00
|
|
|
verticalScrollBar()->setValue(qMin(previousScroll, verticalScrollBar()->maximum()));
|
|
|
|
|
2014-02-02 09:26:38 +00:00
|
|
|
viewport()->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
bool GroupView::isIndexHidden(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup *cat = category(index);
|
2013-12-24 10:47:30 +00:00
|
|
|
if (cat)
|
|
|
|
{
|
|
|
|
return cat->collapsed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup *GroupView::category(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-03 23:40:10 +00:00
|
|
|
return category(index.data(GroupViewRoles::GroupRole).toString());
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup *GroupView::category(const QString &cat) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-03 23:40:10 +00:00
|
|
|
for (auto group : m_groups)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-02 13:27:43 +00:00
|
|
|
if (group->text == cat)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-02 13:27:43 +00:00
|
|
|
return group;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-02 13:27:43 +00:00
|
|
|
return nullptr;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2016-04-11 00:30:50 +01:00
|
|
|
VisualGroup *GroupView::categoryAt(const QPoint &pos, VisualGroup::HitResults & result) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2014-02-03 23:40:10 +00:00
|
|
|
for (auto group : m_groups)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2016-04-11 00:30:50 +01:00
|
|
|
result = group->hitScan(pos);
|
|
|
|
if(result != VisualGroup::NoHit)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2014-02-02 13:27:43 +00:00
|
|
|
return group;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-11 00:30:50 +01:00
|
|
|
result = VisualGroup::NoHit;
|
2014-02-02 13:27:43 +00:00
|
|
|
return nullptr;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2016-04-11 00:30:50 +01:00
|
|
|
QString GroupView::groupNameAt(const QPoint &point)
|
|
|
|
{
|
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
auto group = categoryAt(point + offset(), hitresult);
|
|
|
|
if(group && (hitresult & (VisualGroup::HeaderHit | VisualGroup::BodyHit)))
|
|
|
|
{
|
|
|
|
return group->text;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int GroupView::calculateItemsPerRow() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-02 13:27:43 +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
|
|
|
|
|
|
|
int GroupView::contentWidth() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
|
|
|
return width() - m_leftMargin - m_rightMargin;
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
int GroupView::itemWidth() const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
return m_itemWidth;
|
2013-12-30 17:46:12 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::mousePressEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-01-31 21:51:45 +00:00
|
|
|
// endCategoryEditor();
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-02-09 23:51:52 +00:00
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
2015-09-15 23:21:50 +01:00
|
|
|
|
2014-02-09 23:51:52 +00:00
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
2013-12-26 20:16:03 +00:00
|
|
|
|
|
|
|
m_pressedIndex = index;
|
|
|
|
m_pressedAlreadySelected = selectionModel()->isSelected(m_pressedIndex);
|
2014-02-09 23:51:52 +00:00
|
|
|
m_pressedPosition = geometryPos;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2016-04-11 00:30:50 +01:00
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
m_pressedCategory = categoryAt(geometryPos, hitresult);
|
|
|
|
if (m_pressedCategory && hitresult & VisualGroup::CheckboxHit)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
setState(m_pressedCategory->collapsed ? ExpandingState : CollapsingState);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.isValid() && (index.flags() & Qt::ItemIsEnabled))
|
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
if(index != currentIndex())
|
|
|
|
{
|
|
|
|
// FIXME: better!
|
|
|
|
m_currentCursorColumn = -1;
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
// 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);
|
2014-07-12 20:13:23 +01:00
|
|
|
|
2013-12-26 20:16:03 +00:00
|
|
|
setAutoScroll(autoScroll);
|
2014-07-12 20:13:23 +01:00
|
|
|
QRect rect(visualPos, visualPos);
|
2013-12-30 22:10:53 +00:00
|
|
|
setSelection(rect, QItemSelectionModel::ClearAndSelect);
|
2013-12-26 20:16:03 +00:00
|
|
|
|
|
|
|
// signal handlers may change the model
|
|
|
|
emit pressed(index);
|
2014-01-31 21:51:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
void GroupView::mouseMoveEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
QPoint topLeft;
|
2014-02-09 23:51:52 +00:00
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
2013-12-26 20:16:03 +00:00
|
|
|
|
|
|
|
if (state() == ExpandingState || state() == CollapsingState)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state() == DraggingState)
|
|
|
|
{
|
2013-12-30 17:46:12 +00:00
|
|
|
topLeft = m_pressedPosition - offset();
|
2013-12-26 20:16:03 +00:00
|
|
|
if ((topLeft - event->pos()).manhattanLength() > QApplication::startDragDistance())
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
m_pressedIndex = QModelIndex();
|
|
|
|
startDrag(model()->supportedDragActions());
|
|
|
|
setState(NoState);
|
|
|
|
stopAutoScroll();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
return;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 20:16:03 +00:00
|
|
|
if (selectionMode() != SingleSelection)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-30 17:46:12 +00:00
|
|
|
topLeft = m_pressedPosition - offset();
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-09 23:51:52 +00:00
|
|
|
topLeft = geometryPos;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
if (m_pressedIndex.isValid() && (state() != DragSelectingState) &&
|
|
|
|
(event->buttons() != Qt::NoButton) && !selectedIndexes().isEmpty())
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
setState(DraggingState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event->buttons() & Qt::LeftButton) && selectionModel())
|
|
|
|
{
|
|
|
|
setState(DragSelectingState);
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
setSelection(QRect(visualPos, visualPos), QItemSelectionModel::ClearAndSelect);
|
2014-02-09 23:51:52 +00:00
|
|
|
QModelIndex index = indexAt(visualPos);
|
2013-12-26 20:16:03 +00:00
|
|
|
|
|
|
|
// set at the end because it might scroll the view
|
2014-01-31 21:51:45 +00:00
|
|
|
if (index.isValid() && (index != selectionModel()->currentIndex()) &&
|
|
|
|
(index.flags() & Qt::ItemIsEnabled))
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::mouseReleaseEvent(QMouseEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-02-09 23:51:52 +00:00
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2016-04-11 00:30:50 +01:00
|
|
|
VisualGroup::HitResults hitresult;
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
bool click = (index == m_pressedIndex && index.isValid()) ||
|
2016-04-11 00:30:50 +01:00
|
|
|
(m_pressedCategory && m_pressedCategory == categoryAt(geometryPos, hitresult));
|
2013-12-26 20:16:03 +00:00
|
|
|
|
|
|
|
if (click && m_pressedCategory)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
if (state() == ExpandingState)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
m_pressedCategory->collapsed = false;
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
else if (state() == CollapsingState)
|
|
|
|
{
|
|
|
|
m_pressedCategory->collapsed = true;
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
|
|
|
|
|
|
|
|
setState(NoState);
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2013-12-26 20:16:03 +00:00
|
|
|
if (click)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit clicked(index);
|
|
|
|
}
|
|
|
|
QStyleOptionViewItem option = viewOptions();
|
|
|
|
if (m_pressedAlreadySelected)
|
|
|
|
{
|
|
|
|
option.state |= QStyle::State_Selected;
|
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
if ((model()->flags(index) & Qt::ItemIsEnabled) &&
|
|
|
|
style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
emit activated(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::mouseDoubleClickEvent(QMouseEvent *event)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
2014-01-31 21:51:45 +00:00
|
|
|
if (!index.isValid() || !(index.flags() & Qt::ItemIsEnabled) || (m_pressedIndex != index))
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2014-01-31 21:51:45 +00:00
|
|
|
QMouseEvent me(QEvent::MouseButtonPress, event->localPos(), event->windowPos(),
|
|
|
|
event->screenPos(), event->button(), event->buttons(),
|
|
|
|
event->modifiers());
|
2013-12-26 20:16:03 +00:00
|
|
|
mousePressEvent(&me);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// signal handlers may change the model
|
|
|
|
QPersistentModelIndex persistent = index;
|
|
|
|
emit doubleClicked(persistent);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::paintEvent(QPaintEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-03-03 18:12:48 +00:00
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2013-12-24 10:47:30 +00:00
|
|
|
QPainter painter(this->viewport());
|
2013-12-30 17:46:12 +00:00
|
|
|
|
2017-05-21 19:20:37 +01:00
|
|
|
QStyleOptionViewItem option(viewOptions());
|
2014-02-05 00:34:50 +00:00
|
|
|
option.widget = this;
|
|
|
|
|
2014-02-08 20:46:29 +00:00
|
|
|
int wpWidth = viewport()->width();
|
|
|
|
option.rect.setWidth(wpWidth);
|
2014-02-03 23:40:10 +00:00
|
|
|
for (int i = 0; i < m_groups.size(); ++i)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup *category = m_groups.at(i);
|
2014-02-08 20:46:29 +00:00
|
|
|
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);
|
2013-12-24 10:47:30 +00:00
|
|
|
y += category->totalHeight() + m_categoryMargin;
|
2014-02-08 20:46:29 +00:00
|
|
|
option.rect = backup;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-05-21 19:20:37 +01:00
|
|
|
option.features |= QStyleOptionViewItem::WrapText;
|
2013-12-30 22:10:53 +00:00
|
|
|
if (flags & Qt::ItemIsSelectable && selectionModel()->isSelected(index))
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-01-31 21:51:45 +00:00
|
|
|
option.state |= selectionModel()->isSelected(index) ? QStyle::State_Selected
|
|
|
|
: QStyle::State_None;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2014-02-03 23:40:10 +00:00
|
|
|
/*
|
|
|
|
* Drop indicators for manual reordering...
|
|
|
|
*/
|
|
|
|
#if 0
|
2013-12-26 20:16:03 +00:00
|
|
|
if (!m_lastDragPosition.isNull())
|
|
|
|
{
|
2014-01-31 21:51:45 +00:00
|
|
|
QPair<Group *, int> pair = rowDropPos(m_lastDragPosition);
|
|
|
|
Group *category = pair.first;
|
2013-12-26 20:16:03 +00:00
|
|
|
int row = pair.second;
|
|
|
|
if (category)
|
|
|
|
{
|
2014-02-03 23:40:10 +00:00
|
|
|
int internalRow = row - category->firstItemIndex;
|
2013-12-26 20:16:03 +00:00
|
|
|
QLine line;
|
2013-12-31 16:26:36 +00:00
|
|
|
if (internalRow >= category->numItems())
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2013-12-31 16:26:36 +00:00
|
|
|
QRect toTheRightOfRect = visualRect(category->lastItem());
|
2013-12-26 20:16:03 +00:00
|
|
|
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
|
|
|
|
|
|
|
void GroupView::resizeEvent(QResizeEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
int newItemsPerRow = calculateItemsPerRow();
|
|
|
|
if(newItemsPerRow != m_currentItemsPerRow)
|
|
|
|
{
|
2014-07-12 22:27:32 +01:00
|
|
|
m_currentCursorColumn = -1;
|
2014-07-12 20:13:23 +01:00
|
|
|
m_currentItemsPerRow = newItemsPerRow;
|
|
|
|
updateGeometries();
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::dragEnterEvent(QDragEnterEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-30 17:46:12 +00:00
|
|
|
m_lastDragPosition = event->pos() + offset();
|
2013-12-26 20:16:03 +00:00
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::dragMoveEvent(QDragMoveEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-30 17:46:12 +00:00
|
|
|
m_lastDragPosition = event->pos() + offset();
|
2013-12-26 20:16:03 +00:00
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::dragLeaveEvent(QDragLeaveEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
viewport()->update();
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::dropEvent(QDropEvent *event)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
|
2013-12-24 10:47:30 +00:00
|
|
|
stopAutoScroll();
|
|
|
|
setState(NoState);
|
|
|
|
|
2017-04-22 05:11:26 +01:00
|
|
|
if (event->source() == this)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2017-04-22 05:11:26 +01:00
|
|
|
if(event->possibleActions() & Qt::MoveAction)
|
|
|
|
{
|
|
|
|
QPair<VisualGroup *, int> dropPos = rowDropPos(event->pos() + offset());
|
|
|
|
const VisualGroup *category = dropPos.first;
|
|
|
|
const int row = dropPos.second;
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2017-04-22 05:11:26 +01:00
|
|
|
if (row == -1)
|
|
|
|
{
|
|
|
|
viewport()->update();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2017-04-22 05:11:26 +01:00
|
|
|
const QString categoryText = category->text;
|
|
|
|
if (model()->dropMimeData(event->mimeData(), Qt::MoveAction, row, 0, QModelIndex()))
|
|
|
|
{
|
|
|
|
model()->setData(model()->index(row, 0), categoryText,
|
|
|
|
GroupViewRoles::GroupRole);
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto mimedata = event->mimeData();
|
|
|
|
|
|
|
|
// check if the action is supported
|
|
|
|
if (!mimedata)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
return;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 05:11:26 +01:00
|
|
|
// files dropped from outside?
|
|
|
|
if (mimedata->hasUrls())
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2017-04-22 05:11:26 +01:00
|
|
|
auto urls = mimedata->urls();
|
2017-04-23 15:50:48 +01:00
|
|
|
event->accept();
|
2017-04-22 05:11:26 +01:00
|
|
|
emit droppedURLs(urls);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
void GroupView::startDrag(Qt::DropActions supportedActions)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
QModelIndexList indexes = selectionModel()->selectedIndexes();
|
2014-07-12 20:13:23 +01:00
|
|
|
if(indexes.count() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMimeData *data = model()->mimeData(indexes);
|
|
|
|
if (!data)
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QRect rect;
|
|
|
|
QPixmap pixmap = renderToPixmap(indexes, &rect);
|
|
|
|
//rect.translate(offset());
|
|
|
|
// rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
drag->setMimeData(data);
|
|
|
|
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
|
|
|
|
if (this->defaultDropAction() != Qt::IgnoreAction &&
|
|
|
|
(supportedActions & this->defaultDropAction()))
|
|
|
|
{
|
|
|
|
defaultDropAction = this->defaultDropAction();
|
|
|
|
}
|
|
|
|
if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
|
|
|
|
{
|
|
|
|
const QItemSelection selection = selectionModel()->selection();
|
2013-12-26 20:16:03 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
for (auto it = selection.constBegin(); it != selection.constEnd(); ++it)
|
|
|
|
{
|
|
|
|
QModelIndex parent = (*it).parent();
|
|
|
|
if ((*it).left() != 0)
|
2014-01-31 21:51:45 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((*it).right() != (model()->columnCount(parent) - 1))
|
|
|
|
{
|
|
|
|
continue;
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
int count = (*it).bottom() - (*it).top() + 1;
|
|
|
|
model()->removeRows((*it).top(), count, parent);
|
2013-12-24 10:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
QRect GroupView::visualRect(const QModelIndex &index) const
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
|
|
|
return geometryRect(index).translated(-offset());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect GroupView::geometryRect(const QModelIndex &index) const
|
2013-12-24 10:47:30 +00:00
|
|
|
{
|
|
|
|
if (!index.isValid() || isIndexHidden(index) || index.column() > 0)
|
|
|
|
{
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
2014-07-12 11:49:07 +01:00
|
|
|
int row = index.row();
|
|
|
|
if(geometryCache.contains(row))
|
|
|
|
{
|
|
|
|
return *geometryCache[row];
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
2014-07-12 20:13:23 +01: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
|
|
|
|
2014-07-12 20:13:23 +01: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
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
QModelIndex GroupView::indexAt(const QPoint &point) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2016-10-26 17:12:33 +01:00
|
|
|
const_cast<GroupView*>(this)->executeDelayedItemsLayout();
|
|
|
|
|
2013-12-26 20:16:03 +00:00
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
2014-02-09 23:51:52 +00:00
|
|
|
if (visualRect(index).contains(point))
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
|
|
|
void GroupView::setSelection(const QRect &rect,
|
2014-01-31 21:58:57 +00:00
|
|
|
const QItemSelectionModel::SelectionFlags commands)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
2014-07-12 20:13:23 +01:00
|
|
|
QRect itemRect = visualRect(index);
|
2014-02-02 15:57:00 +00:00
|
|
|
if (itemRect.intersects(rect))
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2013-12-30 22:10:53 +00:00
|
|
|
selectionModel()->select(index, commands);
|
2014-02-02 15:57:00 +00:00
|
|
|
update(itemRect.translated(-offset()));
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
QPixmap GroupView::renderToPixmap(const QModelIndexList &indices, QRect *r) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(r);
|
2014-01-31 21:51:45 +00:00
|
|
|
auto paintPairs = draggablePaintPairs(indices, r);
|
2013-12-26 20:16:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-01-31 21:58:57 +00:00
|
|
|
QList<QPair<QRect, QModelIndex>> GroupView::draggablePaintPairs(const QModelIndexList &indices,
|
|
|
|
QRect *r) const
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(r);
|
|
|
|
QRect &rect = *r;
|
2014-01-31 21:51:45 +00:00
|
|
|
QList<QPair<QRect, QModelIndex>> ret;
|
|
|
|
for (int i = 0; i < indices.count(); ++i)
|
|
|
|
{
|
2013-12-26 20:16:03 +00:00
|
|
|
const QModelIndex &index = indices.at(i);
|
2014-02-02 13:27:43 +00:00
|
|
|
const QRect current = geometryRect(index);
|
2014-02-02 17:30:52 +00:00
|
|
|
ret += qMakePair(current, index);
|
|
|
|
rect |= current;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
bool GroupView::isDragEventAccepted(QDropEvent *event)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2017-04-22 05:11:26 +01:00
|
|
|
return true;
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
QPair<VisualGroup *, int> GroupView::rowDropPos(const QPoint &pos)
|
2013-12-26 20:16:03 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
return qMakePair<VisualGroup*, int>(nullptr, -1);
|
2013-12-26 20:16:03 +00:00
|
|
|
}
|
2013-12-30 17:46:12 +00:00
|
|
|
|
2014-01-31 21:51:45 +00:00
|
|
|
QPoint GroupView::offset() const
|
2013-12-30 17:46:12 +00:00
|
|
|
{
|
|
|
|
return QPoint(horizontalOffset(), verticalOffset());
|
|
|
|
}
|
2014-02-02 13:27:43 +00:00
|
|
|
|
|
|
|
QRegion GroupView::visualRegionForSelection(const QItemSelection &selection) const
|
|
|
|
{
|
|
|
|
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-07-12 20:13:23 +01:00
|
|
|
|
2014-02-02 17:30:52 +00:00
|
|
|
QModelIndex GroupView::moveCursor(QAbstractItemView::CursorAction cursorAction,
|
|
|
|
Qt::KeyboardModifiers modifiers)
|
|
|
|
{
|
|
|
|
auto current = currentIndex();
|
|
|
|
if(!current.isValid())
|
|
|
|
{
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
auto cat = category(current);
|
2014-07-12 20:13:23 +01:00
|
|
|
int group_index = m_groups.indexOf(cat);
|
|
|
|
if(group_index < 0)
|
|
|
|
return current;
|
|
|
|
|
|
|
|
auto real_group = m_groups[group_index];
|
|
|
|
int beginning_row = 0;
|
|
|
|
for(auto group: m_groups)
|
|
|
|
{
|
|
|
|
if(group == real_group)
|
|
|
|
break;
|
|
|
|
beginning_row += group->numRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPair<int, int> pos = cat->positionOf(current);
|
|
|
|
int column = pos.first;
|
|
|
|
int row = pos.second;
|
|
|
|
if(m_currentCursorColumn < 0)
|
2014-02-02 17:30:52 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
m_currentCursorColumn = column;
|
|
|
|
}
|
|
|
|
switch(cursorAction)
|
|
|
|
{
|
|
|
|
case MoveUp:
|
2014-02-02 17:30:52 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
if(row == 0)
|
|
|
|
{
|
2014-07-12 22:27:32 +01:00
|
|
|
int prevgroupindex = group_index-1;
|
|
|
|
while(prevgroupindex >= 0)
|
2014-07-12 20:13:23 +01:00
|
|
|
{
|
2014-07-12 22:27:32 +01:00
|
|
|
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];
|
2014-07-12 20:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
}
|
2014-07-12 22:27:32 +01:00
|
|
|
return current;
|
2014-02-02 17:30:52 +00:00
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
case MoveDown:
|
|
|
|
{
|
|
|
|
if(row == cat->rows.size() - 1)
|
|
|
|
{
|
2014-07-12 22:27:32 +01:00
|
|
|
int nextgroupindex = group_index+1;
|
|
|
|
while (nextgroupindex < m_groups.size())
|
2014-07-12 20:13:23 +01:00
|
|
|
{
|
2014-07-12 22:27:32 +01:00
|
|
|
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];
|
2014-07-12 20:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
}
|
2014-07-12 22:27:32 +01:00
|
|
|
return current;
|
2014-07-12 20:13:23 +01:00
|
|
|
}
|
|
|
|
case MoveLeft:
|
|
|
|
{
|
|
|
|
if(column > 0)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = column - 1;
|
|
|
|
return cat->rows[row][column - 1];
|
|
|
|
}
|
2014-07-12 22:27:32 +01:00
|
|
|
// TODO: moving to previous line
|
2014-07-12 20:13:23 +01:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
case MoveRight:
|
|
|
|
{
|
|
|
|
if(column < cat->rows[row].size() - 1)
|
|
|
|
{
|
|
|
|
m_currentCursorColumn = column + 1;
|
|
|
|
return cat->rows[row][column + 1];
|
|
|
|
}
|
2014-07-12 22:27:32 +01:00
|
|
|
// TODO: moving to next line
|
2014-07-12 20:13:23 +01:00
|
|
|
return current;
|
|
|
|
}
|
2014-07-12 22:27:32 +01:00
|
|
|
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];
|
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
default:
|
|
|
|
break;
|
2014-02-02 17:30:52 +00:00
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
|
|
|
|
int GroupView::horizontalOffset() const
|
|
|
|
{
|
|
|
|
return horizontalScrollBar()->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GroupView::verticalOffset() const
|
|
|
|
{
|
|
|
|
return verticalScrollBar()->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupView::scrollContentsBy(int dx, int dy)
|
|
|
|
{
|
|
|
|
scrollDirtyRegion(dx, dy);
|
|
|
|
viewport()->scroll(dx, dy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupView::scrollTo(const QModelIndex &index, ScrollHint hint)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QRect rect = visualRect(index);
|
|
|
|
if (hint == EnsureVisible && viewport()->rect().contains(rect))
|
|
|
|
{
|
|
|
|
viewport()->update(rect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verticalScrollBar()->setValue(verticalScrollToValue(index, rect, hint));
|
|
|
|
}
|
|
|
|
|
|
|
|
int GroupView::verticalScrollToValue(const QModelIndex &index, const QRect &rect,
|
|
|
|
QListView::ScrollHint hint) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|