Fix groupview issues

* indexAt was using the wrong coordinate system
* model events now trigger a delayed layout update instead of immediate.
This commit is contained in:
Petr Mrázek 2014-02-10 00:51:52 +01:00
parent 2f0275c194
commit 5a0e7877b0
2 changed files with 26 additions and 27 deletions

View File

@ -48,26 +48,16 @@ GroupView::~GroupView()
void GroupView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, void GroupView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVector<int> &roles) const QVector<int> &roles)
{ {
/* scheduleDelayedItemsLayout();
if (roles.contains(GroupViewRoles::GroupRole) || roles.contains(Qt::DisplayRole))
{
*/
updateGeometries();
/*
}
*/
viewport()->update();
} }
void GroupView::rowsInserted(const QModelIndex &parent, int start, int end) void GroupView::rowsInserted(const QModelIndex &parent, int start, int end)
{ {
updateGeometries(); scheduleDelayedItemsLayout();
viewport()->update();
} }
void GroupView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) void GroupView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{ {
updateGeometries(); scheduleDelayedItemsLayout();
viewport()->update();
} }
void GroupView::updateGeometries() void GroupView::updateGeometries()
@ -274,15 +264,17 @@ void GroupView::mousePressEvent(QMouseEvent *event)
{ {
// endCategoryEditor(); // endCategoryEditor();
QPoint pos = event->pos() + offset(); QPoint visualPos = event->pos();
QPersistentModelIndex index = indexAt(pos); QPoint geometryPos = event->pos() + offset();
QPersistentModelIndex index = indexAt(visualPos);
m_pressedIndex = index; m_pressedIndex = index;
m_pressedAlreadySelected = selectionModel()->isSelected(m_pressedIndex); m_pressedAlreadySelected = selectionModel()->isSelected(m_pressedIndex);
QItemSelectionModel::SelectionFlags selection_flags = selectionCommand(index, event); QItemSelectionModel::SelectionFlags selection_flags = selectionCommand(index, event);
m_pressedPosition = pos; m_pressedPosition = geometryPos;
m_pressedCategory = categoryAt(m_pressedPosition); m_pressedCategory = categoryAt(geometryPos);
if (m_pressedCategory) if (m_pressedCategory)
{ {
setState(m_pressedCategory->collapsed ? ExpandingState : CollapsingState); setState(m_pressedCategory->collapsed ? ExpandingState : CollapsingState);
@ -298,7 +290,7 @@ void GroupView::mousePressEvent(QMouseEvent *event)
setAutoScroll(false); setAutoScroll(false);
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
setAutoScroll(autoScroll); setAutoScroll(autoScroll);
QRect rect(m_pressedPosition, pos); QRect rect(geometryPos, geometryPos);
setSelection(rect, QItemSelectionModel::ClearAndSelect); setSelection(rect, QItemSelectionModel::ClearAndSelect);
// signal handlers may change the model // signal handlers may change the model
@ -314,7 +306,8 @@ void GroupView::mousePressEvent(QMouseEvent *event)
void GroupView::mouseMoveEvent(QMouseEvent *event) void GroupView::mouseMoveEvent(QMouseEvent *event)
{ {
QPoint topLeft; QPoint topLeft;
QPoint pos = event->pos() + offset(); QPoint visualPos = event->pos();
QPoint geometryPos = event->pos() + offset();
if (state() == ExpandingState || state() == CollapsingState) if (state() == ExpandingState || state() == CollapsingState)
{ {
@ -340,7 +333,7 @@ void GroupView::mouseMoveEvent(QMouseEvent *event)
} }
else else
{ {
topLeft = pos; topLeft = geometryPos;
} }
if (m_pressedIndex.isValid() && (state() != DragSelectingState) && if (m_pressedIndex.isValid() && (state() != DragSelectingState) &&
@ -354,8 +347,8 @@ void GroupView::mouseMoveEvent(QMouseEvent *event)
{ {
setState(DragSelectingState); setState(DragSelectingState);
setSelection(QRect(pos, pos), QItemSelectionModel::ClearAndSelect); setSelection(QRect(geometryPos, geometryPos), QItemSelectionModel::ClearAndSelect);
QModelIndex index = indexAt(pos); QModelIndex index = indexAt(visualPos);
// set at the end because it might scroll the view // set at the end because it might scroll the view
if (index.isValid() && (index != selectionModel()->currentIndex()) && if (index.isValid() && (index != selectionModel()->currentIndex()) &&
@ -368,11 +361,12 @@ void GroupView::mouseMoveEvent(QMouseEvent *event)
void GroupView::mouseReleaseEvent(QMouseEvent *event) void GroupView::mouseReleaseEvent(QMouseEvent *event)
{ {
QPoint pos = event->pos() + offset(); QPoint visualPos = event->pos();
QPersistentModelIndex index = indexAt(pos); QPoint geometryPos = event->pos() + offset();
QPersistentModelIndex index = indexAt(visualPos);
bool click = (index == m_pressedIndex && index.isValid()) || bool click = (index == m_pressedIndex && index.isValid()) ||
(m_pressedCategory && m_pressedCategory == categoryAt(pos)); (m_pressedCategory && m_pressedCategory == categoryAt(geometryPos));
if (click && m_pressedCategory) if (click && m_pressedCategory)
{ {
@ -708,7 +702,7 @@ QModelIndex GroupView::indexAt(const QPoint &point) const
for (int i = 0; i < model()->rowCount(); ++i) for (int i = 0; i < model()->rowCount(); ++i)
{ {
QModelIndex index = model()->index(i, 0); QModelIndex index = model()->index(i, 0);
if (geometryRect(index).contains(point)) if (visualRect(index).contains(point))
{ {
return index; return index;
} }
@ -716,6 +710,7 @@ QModelIndex GroupView::indexAt(const QPoint &point) const
return QModelIndex(); return QModelIndex();
} }
// FIXME: is rect supposed to be geometry or visual coords?
void GroupView::setSelection(const QRect &rect, void GroupView::setSelection(const QRect &rect,
const QItemSelectionModel::SelectionFlags commands) const QItemSelectionModel::SelectionFlags commands)
{ {

View File

@ -24,9 +24,12 @@ public:
GroupView(QWidget *parent = 0); GroupView(QWidget *parent = 0);
~GroupView(); ~GroupView();
/// return geometry rectangle occupied by the specified model item
QRect geometryRect(const QModelIndex &index) const; QRect geometryRect(const QModelIndex &index) const;
/// return visual rectangle occupied by the specified model item
virtual QRect visualRect(const QModelIndex &index) const override; virtual QRect visualRect(const QModelIndex &index) const override;
QModelIndex indexAt(const QPoint &point) const; /// get the model index at the specified visual point
virtual QModelIndex indexAt(const QPoint &point) const override;
void setSelection(const QRect &rect, void setSelection(const QRect &rect,
const QItemSelectionModel::SelectionFlags commands) override; const QItemSelectionModel::SelectionFlags commands) override;
@ -114,6 +117,7 @@ private slots:
void endCategoryEditor();*/ void endCategoryEditor();*/
private: /* variables */ private: /* variables */
/// point where the currently active mouse action started in geometry coordinates
QPoint m_pressedPosition; QPoint m_pressedPosition;
QPersistentModelIndex m_pressedIndex; QPersistentModelIndex m_pressedIndex;
bool m_pressedAlreadySelected; bool m_pressedAlreadySelected;