2014-02-01 03:21:45 +05:30
|
|
|
#include "GroupView.h"
|
2013-12-24 16:17:30 +05:30
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QtMath>
|
|
|
|
#include <QMouseEvent>
|
2013-12-27 01:46:03 +05:30
|
|
|
#include <QListView>
|
|
|
|
#include <QPersistentModelIndex>
|
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
2014-07-12 16:19:07 +05:30
|
|
|
#include <QCache>
|
2013-12-27 03:10:26 +05:30
|
|
|
#include <QScrollBar>
|
2013-12-27 01:46:03 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
#include "Group.h"
|
2014-07-06 14:45:15 +05:30
|
|
|
#include "logger/QsLog.h"
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
template <typename T> bool listsIntersect(const QList<T> &l1, const QList<T> t2)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
for (auto &item : l1)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
if (t2.contains(item))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
GroupView::GroupView(QWidget *parent)
|
2014-02-02 14:56:38 +05:30
|
|
|
: QAbstractItemView(parent), m_leftMargin(5), m_rightMargin(5), m_bottomMargin(5),
|
2014-02-01 03:21:45 +05:30
|
|
|
m_categoryMargin(5) //, m_updatesDisabled(false), m_categoryEditor(0), m_editedCategory(0)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-02 14:56:38 +05:30
|
|
|
// setViewMode(IconMode);
|
2014-02-01 03:28:57 +05:30
|
|
|
// setMovement(Snap);
|
2013-12-24 16:17:30 +05:30
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
2014-02-02 14:56:38 +05:30
|
|
|
// setWordWrap(true);
|
2014-02-01 03:28:57 +05:30
|
|
|
// setDragDropMode(QListView::InternalMove);
|
2013-12-27 01:46:03 +05:30
|
|
|
setAcceptDrops(true);
|
2014-02-02 18:57:43 +05:30
|
|
|
m_spacing = 5;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
GroupView::~GroupView()
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups.clear();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2014-03-07 20:16:56 +05:30
|
|
|
void GroupView::setModel(QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
QAbstractItemView::setModel(model);
|
|
|
|
connect(model, &QAbstractItemModel::modelReset, this, &GroupView::modelReset);
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
|
2014-02-01 03:28:57 +05:30
|
|
|
const QVector<int> &roles)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-10 05:21:52 +05:30
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::rowsInserted(const QModelIndex &parent, int start, int end)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-10 05:21:52 +05:30
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-10 05:21:52 +05:30
|
|
|
scheduleDelayedItemsLayout();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::updateGeometries()
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-07-12 16:19:07 +05:30
|
|
|
geometryCache.clear();
|
2013-12-31 03:40:53 +05:30
|
|
|
int previousScroll = verticalScrollBar()->value();
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QMap<QString, Group *> cats;
|
2013-12-24 16:17:30 +05:30
|
|
|
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
const QString groupName =
|
|
|
|
model()->index(i, 0).data(GroupViewRoles::GroupRole).toString();
|
|
|
|
if (!cats.contains(groupName))
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
Group *old = this->category(groupName);
|
2013-12-24 16:17:30 +05:30
|
|
|
if (old)
|
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
cats.insert(groupName, new Group(old));
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
cats.insert(groupName, new Group(groupName, this));
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (m_editedCategory)
|
|
|
|
{
|
|
|
|
m_editedCategory = cats[m_editedCategory->text];
|
|
|
|
}*/
|
|
|
|
|
2014-02-04 05:10:10 +05:30
|
|
|
qDeleteAll(m_groups);
|
|
|
|
m_groups = cats.values();
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-04 05:10:10 +05:30
|
|
|
for (auto cat : m_groups)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
cat->update();
|
|
|
|
}
|
|
|
|
|
2014-02-04 05:10:10 +05:30
|
|
|
if (m_groups.isEmpty())
|
2013-12-27 03:10:26 +05:30
|
|
|
{
|
|
|
|
verticalScrollBar()->setRange(0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int totalHeight = 0;
|
2014-02-09 02:16:29 +05:30
|
|
|
// top margin
|
|
|
|
totalHeight += m_categoryMargin;
|
2014-02-10 01:34:00 +05:30
|
|
|
int itemScroll = 0;
|
2014-02-04 05:10:10 +05:30
|
|
|
for (auto category : m_groups)
|
2013-12-27 03:10:26 +05:30
|
|
|
{
|
2014-02-09 02:16:29 +05:30
|
|
|
category->m_verticalPosition = totalHeight;
|
2013-12-27 03:10:26 +05:30
|
|
|
totalHeight += category->totalHeight() + m_categoryMargin;
|
2014-02-10 01:34:00 +05:30
|
|
|
if(!itemScroll && category->totalHeight() != 0)
|
|
|
|
{
|
|
|
|
itemScroll = category->contentHeight() / category->numRows();
|
|
|
|
}
|
2013-12-27 03:10:26 +05:30
|
|
|
}
|
2014-02-10 01:34:00 +05:30
|
|
|
// do not divide by zero
|
|
|
|
if(itemScroll == 0)
|
|
|
|
itemScroll = 64;
|
|
|
|
|
2013-12-30 23:16:12 +05:30
|
|
|
totalHeight += m_bottomMargin;
|
2014-02-10 01:15:18 +05:30
|
|
|
verticalScrollBar()->setSingleStep ( itemScroll );
|
|
|
|
const int rowsPerPage = qMax ( viewport()->height() / itemScroll, 1 );
|
|
|
|
verticalScrollBar()->setPageStep ( rowsPerPage * itemScroll );
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
verticalScrollBar()->setRange(0, totalHeight - height());
|
2013-12-27 03:10:26 +05:30
|
|
|
}
|
|
|
|
|
2013-12-31 03:40:53 +05:30
|
|
|
verticalScrollBar()->setValue(qMin(previousScroll, verticalScrollBar()->maximum()));
|
|
|
|
|
2014-02-02 14:56:38 +05:30
|
|
|
viewport()->update();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2014-03-07 20:16:56 +05:30
|
|
|
void GroupView::modelReset()
|
|
|
|
{
|
|
|
|
scheduleDelayedItemsLayout();
|
|
|
|
executeDelayedItemsLayout();
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
bool GroupView::isIndexHidden(const QModelIndex &index) const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
Group *cat = category(index);
|
2013-12-24 16:17:30 +05:30
|
|
|
if (cat)
|
|
|
|
{
|
|
|
|
return cat->collapsed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
Group *GroupView::category(const QModelIndex &index) const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
return category(index.data(GroupViewRoles::GroupRole).toString());
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
Group *GroupView::category(const QString &cat) const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
for (auto group : m_groups)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-02 18:57:43 +05:30
|
|
|
if (group->text == cat)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-02 18:57:43 +05:30
|
|
|
return group;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
}
|
2014-02-02 18:57:43 +05:30
|
|
|
return nullptr;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
Group *GroupView::categoryAt(const QPoint &pos) const
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
for (auto group : m_groups)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
if(group->hitScan(pos) & Group::CheckboxHit)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-02 18:57:43 +05:30
|
|
|
return group;
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
2014-02-02 18:57:43 +05:30
|
|
|
return nullptr;
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
int GroupView::itemsPerRow() const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-02 18:57:43 +05:30
|
|
|
return qFloor((qreal)(contentWidth()) / (qreal)(itemWidth() + m_spacing));
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
int GroupView::contentWidth() const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
|
|
|
return width() - m_leftMargin - m_rightMargin;
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
int GroupView::itemWidth() const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
return itemDelegate()
|
|
|
|
->sizeHint(viewOptions(), model()->index(model()->rowCount() - 1, 0))
|
|
|
|
.width();
|
2013-12-30 23:15:40 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
int GroupView::categoryRowHeight(const QModelIndex &index) const
|
2013-12-30 23:15:40 +05:30
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
QModelIndexList indices;
|
|
|
|
int internalRow = categoryInternalPosition(index).second;
|
2014-02-01 03:21:45 +05:30
|
|
|
for (auto &i : category(index)->items())
|
2013-12-30 23:15:40 +05:30
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
if (categoryInternalPosition(i).second == internalRow)
|
2013-12-30 23:15:40 +05:30
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
indices.append(i);
|
2013-12-30 23:15:40 +05:30
|
|
|
}
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2013-12-30 23:15:40 +05:30
|
|
|
|
2013-12-31 21:56:36 +05:30
|
|
|
int largestHeight = 0;
|
2014-02-01 03:21:45 +05:30
|
|
|
for (auto &i : indices)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
largestHeight =
|
|
|
|
qMax(largestHeight, itemDelegate()->sizeHint(viewOptions(), i).height());
|
2013-12-30 23:15:40 +05:30
|
|
|
}
|
2014-02-05 06:04:50 +05:30
|
|
|
return largestHeight + m_spacing;
|
2013-12-30 23:15:40 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QPair<int, int> GroupView::categoryInternalPosition(const QModelIndex &index) const
|
2013-12-30 23:15:40 +05:30
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
QList<QModelIndex> indices = category(index)->items();
|
2013-12-30 23:15:40 +05:30
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
const int perRow = itemsPerRow();
|
|
|
|
for (int i = 0; i < indices.size(); ++i)
|
|
|
|
{
|
|
|
|
if (indices.at(i) == index)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++x;
|
|
|
|
if (x == perRow)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-30 23:15:40 +05:30
|
|
|
x = 0;
|
|
|
|
++y;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
}
|
2013-12-30 23:15:40 +05:30
|
|
|
return qMakePair(x, y);
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
int GroupView::categoryInternalRowTop(const QModelIndex &index) const
|
2013-12-30 23:16:12 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
Group *cat = category(index);
|
2013-12-31 21:56:36 +05:30
|
|
|
int categoryInternalRow = categoryInternalPosition(index).second;
|
|
|
|
int result = 0;
|
|
|
|
for (int i = 0; i < categoryInternalRow; ++i)
|
|
|
|
{
|
|
|
|
result += cat->rowHeights.at(i);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-02-01 03:28:57 +05:30
|
|
|
int GroupView::itemHeightForCategoryRow(const Group *category, const int internalRow) const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
for (auto &i : category->items())
|
2013-12-30 23:16:12 +05:30
|
|
|
{
|
|
|
|
QPair<int, int> pos = categoryInternalPosition(i);
|
|
|
|
if (pos.second == internalRow)
|
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
return categoryRowHeight(i);
|
2013-12-30 23:16:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::mousePressEvent(QMouseEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
// endCategoryEditor();
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-10 05:21:52 +05:30
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
|
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
2013-12-27 01:46:03 +05:30
|
|
|
|
|
|
|
m_pressedIndex = index;
|
|
|
|
m_pressedAlreadySelected = selectionModel()->isSelected(m_pressedIndex);
|
2014-02-10 05:21:52 +05:30
|
|
|
m_pressedPosition = geometryPos;
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-10 05:21:52 +05:30
|
|
|
m_pressedCategory = categoryAt(geometryPos);
|
2013-12-27 01:46:03 +05:30
|
|
|
if (m_pressedCategory)
|
|
|
|
{
|
|
|
|
setState(m_pressedCategory->collapsed ? ExpandingState : CollapsingState);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index.isValid() && (index.flags() & Qt::ItemIsEnabled))
|
|
|
|
{
|
|
|
|
// 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);
|
2014-02-10 05:21:52 +05:30
|
|
|
QRect rect(geometryPos, geometryPos);
|
2013-12-31 03:40:53 +05:30
|
|
|
setSelection(rect, QItemSelectionModel::ClearAndSelect);
|
2013-12-27 01:46:03 +05:30
|
|
|
|
|
|
|
// signal handlers may change the model
|
|
|
|
emit pressed(index);
|
2014-02-01 03:21:45 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
// Forces a finalize() even if mouse is pressed, but not on a item
|
|
|
|
selectionModel()->select(QModelIndex(), QItemSelectionModel::Select);
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::mouseMoveEvent(QMouseEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
QPoint topLeft;
|
2014-02-10 05:21:52 +05:30
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
2013-12-27 01:46:03 +05:30
|
|
|
|
|
|
|
if (state() == ExpandingState || state() == CollapsingState)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state() == DraggingState)
|
|
|
|
{
|
2013-12-30 23:16:12 +05:30
|
|
|
topLeft = m_pressedPosition - offset();
|
2013-12-27 01:46:03 +05:30
|
|
|
if ((topLeft - event->pos()).manhattanLength() > QApplication::startDragDistance())
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
m_pressedIndex = QModelIndex();
|
|
|
|
startDrag(model()->supportedDragActions());
|
|
|
|
setState(NoState);
|
|
|
|
stopAutoScroll();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
return;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
if (selectionMode() != SingleSelection)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-30 23:16:12 +05:30
|
|
|
topLeft = m_pressedPosition - offset();
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-10 05:21:52 +05:30
|
|
|
topLeft = geometryPos;
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
if (m_pressedIndex.isValid() && (state() != DragSelectingState) &&
|
|
|
|
(event->buttons() != Qt::NoButton) && !selectedIndexes().isEmpty())
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
setState(DraggingState);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event->buttons() & Qt::LeftButton) && selectionModel())
|
|
|
|
{
|
|
|
|
setState(DragSelectingState);
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-10 05:21:52 +05:30
|
|
|
setSelection(QRect(geometryPos, geometryPos), QItemSelectionModel::ClearAndSelect);
|
|
|
|
QModelIndex index = indexAt(visualPos);
|
2013-12-27 01:46:03 +05:30
|
|
|
|
|
|
|
// set at the end because it might scroll the view
|
2014-02-01 03:21:45 +05:30
|
|
|
if (index.isValid() && (index != selectionModel()->currentIndex()) &&
|
|
|
|
(index.flags() & Qt::ItemIsEnabled))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::mouseReleaseEvent(QMouseEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-10 05:21:52 +05:30
|
|
|
QPoint visualPos = event->pos();
|
|
|
|
QPoint geometryPos = event->pos() + offset();
|
|
|
|
QPersistentModelIndex index = indexAt(visualPos);
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
bool click = (index == m_pressedIndex && index.isValid()) ||
|
2014-02-10 05:21:52 +05:30
|
|
|
(m_pressedCategory && m_pressedCategory == categoryAt(geometryPos));
|
2013-12-27 01:46:03 +05:30
|
|
|
|
|
|
|
if (click && m_pressedCategory)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
if (state() == ExpandingState)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
m_pressedCategory->collapsed = false;
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
2013-12-24 16:17:30 +05:30
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
else if (state() == CollapsingState)
|
|
|
|
{
|
|
|
|
m_pressedCategory->collapsed = true;
|
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
|
|
|
|
|
|
|
|
setState(NoState);
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
if (click)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit clicked(index);
|
|
|
|
}
|
|
|
|
QStyleOptionViewItem option = viewOptions();
|
|
|
|
if (m_pressedAlreadySelected)
|
|
|
|
{
|
|
|
|
option.state |= QStyle::State_Selected;
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
if ((model()->flags(index) & Qt::ItemIsEnabled) &&
|
|
|
|
style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option, this))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
emit activated(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::mouseDoubleClickEvent(QMouseEvent *event)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
2014-02-01 03:21:45 +05:30
|
|
|
if (!index.isValid() || !(index.flags() & Qt::ItemIsEnabled) || (m_pressedIndex != index))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
QMouseEvent me(QEvent::MouseButtonPress, event->localPos(), event->windowPos(),
|
|
|
|
event->screenPos(), event->button(), event->buttons(),
|
|
|
|
event->modifiers());
|
2013-12-27 01:46:03 +05:30
|
|
|
mousePressEvent(&me);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// signal handlers may change the model
|
|
|
|
QPersistentModelIndex persistent = index;
|
|
|
|
emit doubleClicked(persistent);
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::paintEvent(QPaintEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-03-03 23:42:48 +05:30
|
|
|
executeDelayedItemsLayout();
|
|
|
|
|
2013-12-24 16:17:30 +05:30
|
|
|
QPainter painter(this->viewport());
|
2013-12-30 23:16:12 +05:30
|
|
|
|
2014-02-05 06:04:50 +05:30
|
|
|
QStyleOptionViewItemV4 option(viewOptions());
|
|
|
|
option.widget = this;
|
|
|
|
|
2014-02-09 02:16:29 +05:30
|
|
|
int wpWidth = viewport()->width();
|
|
|
|
option.rect.setWidth(wpWidth);
|
2014-02-04 05:10:10 +05:30
|
|
|
for (int i = 0; i < m_groups.size(); ++i)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
Group *category = m_groups.at(i);
|
2014-02-09 02:16:29 +05:30
|
|
|
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 16:17:30 +05:30
|
|
|
y += category->totalHeight() + m_categoryMargin;
|
2014-02-09 02:16:29 +05:30
|
|
|
option.rect = backup;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-02-01 03:28:57 +05:30
|
|
|
option.features |=
|
|
|
|
QStyleOptionViewItemV2::WrapText; // FIXME: what is the meaning of this anyway?
|
2013-12-31 03:40:53 +05:30
|
|
|
if (flags & Qt::ItemIsSelectable && selectionModel()->isSelected(index))
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
option.state |= selectionModel()->isSelected(index) ? QStyle::State_Selected
|
|
|
|
: QStyle::State_None;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
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-27 01:46:03 +05:30
|
|
|
|
2014-02-04 05:10:10 +05:30
|
|
|
/*
|
|
|
|
* Drop indicators for manual reordering...
|
|
|
|
*/
|
|
|
|
#if 0
|
2013-12-27 01:46:03 +05:30
|
|
|
if (!m_lastDragPosition.isNull())
|
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
QPair<Group *, int> pair = rowDropPos(m_lastDragPosition);
|
|
|
|
Group *category = pair.first;
|
2013-12-27 01:46:03 +05:30
|
|
|
int row = pair.second;
|
|
|
|
if (category)
|
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
int internalRow = row - category->firstItemIndex;
|
2013-12-27 01:46:03 +05:30
|
|
|
QLine line;
|
2013-12-31 21:56:36 +05:30
|
|
|
if (internalRow >= category->numItems())
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2013-12-31 21:56:36 +05:30
|
|
|
QRect toTheRightOfRect = visualRect(category->lastItem());
|
2013-12-27 01:46:03 +05:30
|
|
|
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-04 05:10:10 +05:30
|
|
|
#endif
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::resizeEvent(QResizeEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-02 14:56:38 +05:30
|
|
|
// QListView::resizeEvent(event);
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
// if (m_categoryEditor)
|
|
|
|
// {
|
2014-02-01 03:28:57 +05:30
|
|
|
// m_categoryEditor->resize(qMax(contentWidth() / 2,
|
2014-02-02 18:57:43 +05:30
|
|
|
// m_editedCategory->textRect.width()),
|
2014-02-01 03:28:57 +05:30
|
|
|
// m_categoryEditor->height());
|
2014-02-01 03:21:45 +05:30
|
|
|
// }
|
2013-12-24 16:17:30 +05:30
|
|
|
|
|
|
|
updateGeometries();
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::dragEnterEvent(QDragEnterEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-30 23:16:12 +05:30
|
|
|
m_lastDragPosition = event->pos() + offset();
|
2013-12-27 01:46:03 +05:30
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::dragMoveEvent(QDragMoveEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
if (!isDragEventAccepted(event))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-30 23:16:12 +05:30
|
|
|
m_lastDragPosition = event->pos() + offset();
|
2013-12-27 01:46:03 +05:30
|
|
|
viewport()->update();
|
|
|
|
event->accept();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::dragLeaveEvent(QDragLeaveEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
viewport()->update();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
void GroupView::dropEvent(QDropEvent *event)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
m_lastDragPosition = QPoint();
|
|
|
|
|
2013-12-24 16:17:30 +05:30
|
|
|
stopAutoScroll();
|
|
|
|
setState(NoState);
|
|
|
|
|
|
|
|
if (event->source() != this || !(event->possibleActions() & Qt::MoveAction))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QPair<Group *, int> dropPos = rowDropPos(event->pos() + offset());
|
|
|
|
const Group *category = dropPos.first;
|
2013-12-27 01:46:03 +05:30
|
|
|
const int row = dropPos.second;
|
|
|
|
|
|
|
|
if (row == -1)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
viewport()->update();
|
|
|
|
return;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
const QString categoryText = category->text;
|
|
|
|
if (model()->dropMimeData(event->mimeData(), Qt::MoveAction, row, 0, QModelIndex()))
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
model()->setData(model()->index(row, 0), categoryText,
|
2014-02-04 05:10:10 +05:30
|
|
|
GroupViewRoles::GroupRole);
|
2013-12-27 01:46:03 +05:30
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
updateGeometries();
|
|
|
|
viewport()->update();
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::startDrag(Qt::DropActions supportedActions)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
QModelIndexList indexes = selectionModel()->selectedIndexes();
|
|
|
|
if (indexes.count() > 0)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
QMimeData *data = model()->mimeData(indexes);
|
|
|
|
if (!data)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
return;
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
QRect rect;
|
|
|
|
QPixmap pixmap = renderToPixmap(indexes, &rect);
|
2014-02-02 21:27:00 +05:30
|
|
|
//rect.translate(offset());
|
2014-02-01 03:21:45 +05:30
|
|
|
// rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
|
2013-12-27 01:46:03 +05:30
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
drag->setMimeData(data);
|
|
|
|
Qt::DropAction defaultDropAction = Qt::IgnoreAction;
|
2014-02-01 03:21:45 +05:30
|
|
|
if (this->defaultDropAction() != Qt::IgnoreAction &&
|
|
|
|
(supportedActions & this->defaultDropAction()))
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
defaultDropAction = this->defaultDropAction();
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
const QItemSelection selection = selectionModel()->selection();
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
for (auto it = selection.constBegin(); it != selection.constEnd(); ++it)
|
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
QModelIndex parent = (*it).parent();
|
|
|
|
if ((*it).left() != 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((*it).right() != (model()->columnCount(parent) - 1))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int count = (*it).bottom() - (*it).top() + 1;
|
|
|
|
model()->removeRows((*it).top(), count, parent);
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QRect GroupView::visualRect(const QModelIndex &index) const
|
2014-02-02 18:57:43 +05:30
|
|
|
{
|
|
|
|
return geometryRect(index).translated(-offset());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect GroupView::geometryRect(const QModelIndex &index) const
|
2013-12-24 16:17:30 +05:30
|
|
|
{
|
|
|
|
if (!index.isValid() || isIndexHidden(index) || index.column() > 0)
|
|
|
|
{
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
2014-07-12 16:19:07 +05:30
|
|
|
int row = index.row();
|
|
|
|
if(geometryCache.contains(row))
|
|
|
|
{
|
|
|
|
return *geometryCache[row];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const Group *cat = category(index);
|
|
|
|
QPair<int, int> pos = categoryInternalPosition(index);
|
|
|
|
int x = pos.first;
|
|
|
|
// int y = pos.second;
|
2013-12-24 16:17:30 +05:30
|
|
|
|
2014-07-12 16:19:07 +05:30
|
|
|
QRect out;
|
|
|
|
out.setTop(cat->verticalPosition() + cat->headerHeight() + 5 + categoryInternalRowTop(index));
|
|
|
|
out.setLeft(m_spacing + x * (itemWidth() + m_spacing));
|
|
|
|
out.setSize(itemDelegate()->sizeHint(viewOptions(), index));
|
|
|
|
const_cast<QCache<int, QRect>&>(geometryCache).insert(row, new QRect(out));
|
|
|
|
return out;
|
|
|
|
}
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
2014-02-02 18:57:43 +05:30
|
|
|
|
2013-12-24 16:17:30 +05:30
|
|
|
/*
|
|
|
|
void CategorizedView::startCategoryEditor(Category *category)
|
|
|
|
{
|
|
|
|
if (m_categoryEditor != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_editedCategory = category;
|
|
|
|
m_categoryEditor = new QLineEdit(m_editedCategory->text, this);
|
|
|
|
QRect rect = m_editedCategory->textRect;
|
|
|
|
rect.setWidth(qMax(contentWidth() / 2, rect.width()));
|
|
|
|
m_categoryEditor->setGeometry(rect);
|
|
|
|
m_categoryEditor->show();
|
|
|
|
m_categoryEditor->setFocus();
|
2014-02-01 03:21:45 +05:30
|
|
|
connect(m_categoryEditor, &QLineEdit::returnPressed, this,
|
|
|
|
&CategorizedView::endCategoryEditor);
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void CategorizedView::endCategoryEditor()
|
|
|
|
{
|
|
|
|
if (m_categoryEditor == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_editedCategory->text = m_categoryEditor->text();
|
|
|
|
m_updatesDisabled = true;
|
|
|
|
foreach (const QModelIndex &index, itemsForCategory(m_editedCategory))
|
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
const_cast<QAbstractItemModel *>(index.model())->setData(index,
|
|
|
|
m_categoryEditor->text(), CategoryRole);
|
2013-12-24 16:17:30 +05:30
|
|
|
}
|
|
|
|
m_updatesDisabled = false;
|
|
|
|
delete m_categoryEditor;
|
|
|
|
m_categoryEditor = 0;
|
|
|
|
m_editedCategory = 0;
|
|
|
|
updateGeometries();
|
|
|
|
}
|
|
|
|
*/
|
2013-12-27 01:46:03 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QModelIndex GroupView::indexAt(const QPoint &point) const
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
2014-02-10 05:21:52 +05:30
|
|
|
if (visualRect(index).contains(point))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-02-10 05:21:52 +05:30
|
|
|
// FIXME: is rect supposed to be geometry or visual coords?
|
2014-02-01 03:21:45 +05:30
|
|
|
void GroupView::setSelection(const QRect &rect,
|
2014-02-01 03:28:57 +05:30
|
|
|
const QItemSelectionModel::SelectionFlags commands)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
for (int i = 0; i < model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
QModelIndex index = model()->index(i, 0);
|
2014-02-02 21:27:00 +05:30
|
|
|
QRect itemRect = geometryRect(index);
|
|
|
|
if (itemRect.intersects(rect))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2013-12-31 03:40:53 +05:30
|
|
|
selectionModel()->select(index, commands);
|
2014-02-02 21:27:00 +05:30
|
|
|
update(itemRect.translated(-offset()));
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
2014-02-02 21:27:00 +05:30
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QPixmap GroupView::renderToPixmap(const QModelIndexList &indices, QRect *r) const
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
Q_ASSERT(r);
|
2014-02-01 03:21:45 +05:30
|
|
|
auto paintPairs = draggablePaintPairs(indices, r);
|
2013-12-27 01:46:03 +05:30
|
|
|
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-02-01 03:21:45 +05:30
|
|
|
|
2014-02-01 03:28:57 +05:30
|
|
|
QList<QPair<QRect, QModelIndex>> GroupView::draggablePaintPairs(const QModelIndexList &indices,
|
|
|
|
QRect *r) const
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
Q_ASSERT(r);
|
|
|
|
QRect &rect = *r;
|
2014-02-01 03:21:45 +05:30
|
|
|
QList<QPair<QRect, QModelIndex>> ret;
|
|
|
|
for (int i = 0; i < indices.count(); ++i)
|
|
|
|
{
|
2013-12-27 01:46:03 +05:30
|
|
|
const QModelIndex &index = indices.at(i);
|
2014-02-02 18:57:43 +05:30
|
|
|
const QRect current = geometryRect(index);
|
2014-02-02 23:00:52 +05:30
|
|
|
ret += qMakePair(current, index);
|
|
|
|
rect |= current;
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
bool GroupView::isDragEventAccepted(QDropEvent *event)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
if (event->source() != this)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!listsIntersect(event->mimeData()->formats(), model()->mimeTypes()))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
if (!model()->canDropMimeData(event->mimeData(), event->dropAction(),
|
|
|
|
rowDropPos(event->pos()).second, 0, QModelIndex()))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
|
|
|
QPair<Group *, int> GroupView::rowDropPos(const QPoint &pos)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
// check that we aren't on a category header and calculate which category we're in
|
2014-02-01 03:21:45 +05:30
|
|
|
Group *category = 0;
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
int y = 0;
|
2014-02-04 05:10:10 +05:30
|
|
|
for (auto cat : m_groups)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
if (pos.y() > y && pos.y() < (y + cat->headerHeight()))
|
|
|
|
{
|
2014-02-10 01:44:34 +05:30
|
|
|
return qMakePair<Group*, int>(nullptr, -1);
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
y += cat->totalHeight() + m_categoryMargin;
|
|
|
|
if (pos.y() < y)
|
|
|
|
{
|
|
|
|
category = cat;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (category == 0)
|
|
|
|
{
|
2014-02-10 01:44:34 +05:30
|
|
|
return qMakePair<Group*, int>(nullptr, -1);
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:56:36 +05:30
|
|
|
QList<QModelIndex> indices = category->items();
|
2013-12-27 02:32:25 +05:30
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
// calculate the internal column
|
|
|
|
int internalColumn = -1;
|
|
|
|
{
|
2013-12-30 23:15:40 +05:30
|
|
|
const int itemWidth = this->itemWidth();
|
2013-12-30 23:16:12 +05:30
|
|
|
if (pos.x() >= (itemWidth * itemsPerRow()))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2013-12-30 23:16:12 +05:30
|
|
|
internalColumn = itemsPerRow();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
for (int i = 0, c = 0; i < contentWidth(); i += itemWidth + 10 /*spacing()*/, ++c)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-01 03:21:45 +05:30
|
|
|
if (pos.x() > (i - itemWidth / 2) && pos.x() <= (i + itemWidth / 2))
|
2013-12-30 23:16:12 +05:30
|
|
|
{
|
|
|
|
internalColumn = c;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if (internalColumn == -1)
|
|
|
|
{
|
2014-02-10 01:44:34 +05:30
|
|
|
return qMakePair<Group*, int>(nullptr, -1);
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the internal row
|
|
|
|
int internalRow = -1;
|
|
|
|
{
|
2013-12-30 23:15:40 +05:30
|
|
|
// FIXME rework the drag and drop code
|
2014-02-04 05:10:10 +05:30
|
|
|
const int top = category->verticalPosition();
|
2014-02-01 03:21:45 +05:30
|
|
|
for (int r = 0, h = top; r < category->numRows();
|
|
|
|
h += itemHeightForCategoryRow(category, r), ++r)
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2013-12-30 23:16:12 +05:30
|
|
|
if (pos.y() > h && pos.y() < (h + itemHeightForCategoryRow(category, r)))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
internalRow = r;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (internalRow == -1)
|
|
|
|
{
|
2014-02-10 01:44:34 +05:30
|
|
|
return qMakePair<Group*, int>(nullptr, -1);
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
2013-12-27 02:32:25 +05:30
|
|
|
// this happens if we're in the margin between a one category and another
|
|
|
|
// categories header
|
|
|
|
if (internalRow > (indices.size() / itemsPerRow()))
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
2014-02-10 01:44:34 +05:30
|
|
|
return qMakePair<Group*, int>(nullptr, -1);
|
2013-12-27 01:46:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 02:32:25 +05:30
|
|
|
// flaten the internalColumn/internalRow to one row
|
|
|
|
int categoryRow = internalRow * itemsPerRow() + internalColumn;
|
|
|
|
|
2013-12-27 01:46:03 +05:30
|
|
|
// this is used if we're past the last item
|
2013-12-30 23:16:12 +05:30
|
|
|
if (categoryRow >= indices.size())
|
2013-12-27 01:46:03 +05:30
|
|
|
{
|
|
|
|
return qMakePair(category, indices.last().row() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qMakePair(category, indices.at(categoryRow).row());
|
|
|
|
}
|
2013-12-30 23:16:12 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
QPoint GroupView::offset() const
|
2013-12-30 23:16:12 +05:30
|
|
|
{
|
|
|
|
return QPoint(horizontalOffset(), verticalOffset());
|
|
|
|
}
|
2014-02-02 18:57:43 +05:30
|
|
|
|
|
|
|
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-02-02 23:00:52 +05:30
|
|
|
QModelIndex GroupView::moveCursor(QAbstractItemView::CursorAction cursorAction,
|
|
|
|
Qt::KeyboardModifiers modifiers)
|
|
|
|
{
|
|
|
|
auto current = currentIndex();
|
|
|
|
if(!current.isValid())
|
|
|
|
{
|
2014-07-06 14:45:15 +05:30
|
|
|
QLOG_DEBUG() << "model row: invalid";
|
2014-02-02 23:00:52 +05:30
|
|
|
return current;
|
|
|
|
}
|
2014-07-06 14:45:15 +05:30
|
|
|
QLOG_DEBUG() << "model row: " << current.row();
|
2014-02-02 23:00:52 +05:30
|
|
|
auto cat = category(current);
|
2014-02-04 05:10:10 +05:30
|
|
|
int i = m_groups.indexOf(cat);
|
2014-02-02 23:00:52 +05:30
|
|
|
if(i >= 0)
|
|
|
|
{
|
|
|
|
// this is a pile of something foul
|
2014-02-04 05:10:10 +05:30
|
|
|
auto real_group = m_groups[i];
|
2014-02-02 23:00:52 +05:30
|
|
|
int beginning_row = 0;
|
2014-02-04 05:10:10 +05:30
|
|
|
for(auto group: m_groups)
|
2014-02-02 23:00:52 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
if(group == real_group)
|
2014-02-02 23:00:52 +05:30
|
|
|
break;
|
2014-02-04 05:10:10 +05:30
|
|
|
beginning_row += group->numRows();
|
2014-02-02 23:00:52 +05:30
|
|
|
}
|
2014-07-06 14:45:15 +05:30
|
|
|
QLOG_DEBUG() << "category: " << real_group->text;
|
2014-02-02 23:00:52 +05:30
|
|
|
QPair<int, int> pos = categoryInternalPosition(current);
|
|
|
|
int row = beginning_row + pos.second;
|
2014-07-06 14:45:15 +05:30
|
|
|
QLOG_DEBUG() << "row: " << row;
|
|
|
|
QLOG_DEBUG() << "column: " << pos.first;
|
2014-02-02 23:00:52 +05:30
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|