diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2013-12-26 22:02:25 +0100 |
---|---|---|
committer | Jan Dalheimer <jan@dalheimer.de> | 2013-12-26 22:02:25 +0100 |
commit | 53db8edb851917809209e4473eef2a66651d6047 (patch) | |
tree | e968580dc51ad9616e3ae518909771ca6568ac87 | |
parent | acbbdf319a7378a4029965a52222e7a84c33253f (diff) | |
download | MultiMC-53db8edb851917809209e4473eef2a66651d6047.tar MultiMC-53db8edb851917809209e4473eef2a66651d6047.tar.gz MultiMC-53db8edb851917809209e4473eef2a66651d6047.tar.lz MultiMC-53db8edb851917809209e4473eef2a66651d6047.tar.xz MultiMC-53db8edb851917809209e4473eef2a66651d6047.zip |
Fixing several d&d bugs
-rw-r--r-- | CategorizedView.cpp | 26 | ||||
-rw-r--r-- | main.cpp | 27 |
2 files changed, 38 insertions, 15 deletions
diff --git a/CategorizedView.cpp b/CategorizedView.cpp index 780674eb..1860f095 100644 --- a/CategorizedView.cpp +++ b/CategorizedView.cpp @@ -872,6 +872,8 @@ QPair<CategorizedView::Category *, int> CategorizedView::rowDropPos(const QPoint } } + QList<QModelIndex> indices = itemsForCategory(category); + // calculate the internal column int internalColumn = -1; { @@ -912,26 +914,20 @@ QPair<CategorizedView::Category *, int> CategorizedView::rowDropPos(const QPoint { return qMakePair(nullptr, -1); } - } - - QList<QModelIndex> indices = itemsForCategory(category); - - // flaten the internalColumn/internalRow to one row - int categoryRow = 0; - { - for (int i = 0; i < internalRow; ++i) + // this happens if we're in the margin between a one category and another + // categories header + if (internalRow > (indices.size() / itemsPerRow())) { - if ((i + 1) >= internalRow) - { - break; - } - categoryRow += itemsPerRow(); + return qMakePair(nullptr, -1); } - categoryRow += internalColumn; } + // flaten the internalColumn/internalRow to one row + int categoryRow = internalRow * itemsPerRow() + internalColumn; + // 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); } @@ -1,6 +1,7 @@ #include "CategorizedView.h" #include <QApplication> #include <QStandardItemModel> +#include <QPainter> #include "CategorizedProxyModel.h" @@ -10,6 +11,18 @@ QPixmap icon(const Qt::GlobalColor color) p.fill(QColor(color)); 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 *item = new QStandardItem; @@ -19,6 +32,15 @@ QStandardItem *createItem(const Qt::GlobalColor color, const QString &text, cons item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); 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[]) { @@ -41,6 +63,11 @@ int main(int argc, char *argv[]) model.setItem(8, createItem(Qt::darkGreen, "Dark 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; pModel.setSourceModel(&model); |