Fixing several d&d bugs
This commit is contained in:
parent
acbbdf319a
commit
53db8edb85
@ -872,6 +872,8 @@ QPair<CategorizedView::Category *, int> CategorizedView::rowDropPos(const QPoint
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<QModelIndex> indices = itemsForCategory(category);
|
||||||
|
|
||||||
// calculate the internal column
|
// calculate the internal column
|
||||||
int internalColumn = -1;
|
int internalColumn = -1;
|
||||||
{
|
{
|
||||||
@ -912,26 +914,20 @@ QPair<CategorizedView::Category *, int> CategorizedView::rowDropPos(const QPoint
|
|||||||
{
|
{
|
||||||
return qMakePair(nullptr, -1);
|
return qMakePair(nullptr, -1);
|
||||||
}
|
}
|
||||||
|
// this happens if we're in the margin between a one category and another
|
||||||
|
// categories header
|
||||||
|
if (internalRow > (indices.size() / itemsPerRow()))
|
||||||
|
{
|
||||||
|
return qMakePair(nullptr, -1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QModelIndex> indices = itemsForCategory(category);
|
|
||||||
|
|
||||||
// flaten the internalColumn/internalRow to one row
|
// flaten the internalColumn/internalRow to one row
|
||||||
int categoryRow = 0;
|
int categoryRow = internalRow * itemsPerRow() + internalColumn;
|
||||||
{
|
|
||||||
for (int i = 0; i < internalRow; ++i)
|
|
||||||
{
|
|
||||||
if ((i + 1) >= internalRow)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
categoryRow += itemsPerRow();
|
|
||||||
}
|
|
||||||
categoryRow += internalColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is used if we're past the last item
|
// this is used if we're past the last item
|
||||||
if (internalColumn >= qMin(itemsPerRow(), indices.size()))
|
int numItemsInLastRow = indices.size() % itemsPerRow();
|
||||||
|
if (internalColumn >= numItemsInLastRow)
|
||||||
{
|
{
|
||||||
return qMakePair(category, indices.last().row() + 1);
|
return qMakePair(category, indices.last().row() + 1);
|
||||||
}
|
}
|
||||||
|
27
main.cpp
27
main.cpp
@ -1,6 +1,7 @@
|
|||||||
#include "CategorizedView.h"
|
#include "CategorizedView.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
#include "CategorizedProxyModel.h"
|
#include "CategorizedProxyModel.h"
|
||||||
|
|
||||||
@ -10,6 +11,18 @@ QPixmap icon(const Qt::GlobalColor color)
|
|||||||
p.fill(QColor(color));
|
p.fill(QColor(color));
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
QPixmap icon(const int number)
|
||||||
|
{
|
||||||
|
QPixmap p = icon(Qt::white);
|
||||||
|
QPainter painter(&p);
|
||||||
|
QFont font = painter.font();
|
||||||
|
font.setBold(true);
|
||||||
|
font.setPixelSize(28);
|
||||||
|
painter.setFont(font);
|
||||||
|
painter.drawText(QRect(QPoint(0, 0), p.size()), Qt::AlignVCenter | Qt::AlignHCenter, QString::number(number));
|
||||||
|
painter.end();
|
||||||
|
return p;
|
||||||
|
}
|
||||||
QStandardItem *createItem(const Qt::GlobalColor color, const QString &text, const QString &category)
|
QStandardItem *createItem(const Qt::GlobalColor color, const QString &text, const QString &category)
|
||||||
{
|
{
|
||||||
QStandardItem *item = new QStandardItem;
|
QStandardItem *item = new QStandardItem;
|
||||||
@ -19,6 +32,15 @@ QStandardItem *createItem(const Qt::GlobalColor color, const QString &text, cons
|
|||||||
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
QStandardItem *createItem(const int index, const QString &category)
|
||||||
|
{
|
||||||
|
QStandardItem *item = new QStandardItem;
|
||||||
|
item->setText(QString("Item #%1").arg(index));
|
||||||
|
item->setData(icon(index), Qt::DecorationRole);
|
||||||
|
item->setData(category, CategorizedView::CategoryRole);
|
||||||
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -41,6 +63,11 @@ int main(int argc, char *argv[])
|
|||||||
model.setItem(8, createItem(Qt::darkGreen, "Dark Green", ""));
|
model.setItem(8, createItem(Qt::darkGreen, "Dark Green", ""));
|
||||||
model.setItem(9, createItem(Qt::green, "Green", ""));
|
model.setItem(9, createItem(Qt::green, "Green", ""));
|
||||||
|
|
||||||
|
for (int i = 0; i < 21; ++i)
|
||||||
|
{
|
||||||
|
model.setItem(i + 10, createItem(i+1, "Items 1-20"));
|
||||||
|
}
|
||||||
|
|
||||||
CategorizedProxyModel pModel;
|
CategorizedProxyModel pModel;
|
||||||
pModel.setSourceModel(&model);
|
pModel.setSourceModel(&model);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user