2013-12-24 10:47:30 +00:00
|
|
|
#include "CategorizedView.h"
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QStandardItemModel>
|
2013-12-26 21:02:25 +00:00
|
|
|
#include <QPainter>
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
#include "CategorizedProxyModel.h"
|
2013-12-30 17:45:40 +00:00
|
|
|
#include "InstanceDelegate.h"
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
QPixmap icon(const Qt::GlobalColor color)
|
|
|
|
{
|
|
|
|
QPixmap p = QPixmap(32, 32);
|
|
|
|
p.fill(QColor(color));
|
|
|
|
return p;
|
|
|
|
}
|
2013-12-26 21:02:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
QStandardItem *createItem(const Qt::GlobalColor color, const QString &text, const QString &category)
|
|
|
|
{
|
|
|
|
QStandardItem *item = new QStandardItem;
|
|
|
|
item->setText(text);
|
|
|
|
item->setData(icon(color), Qt::DecorationRole);
|
|
|
|
item->setData(category, CategorizedView::CategoryRole);
|
|
|
|
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
|
|
|
return item;
|
|
|
|
}
|
2013-12-26 21:02:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-12-24 10:47:30 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QApplication a(argc, argv);
|
|
|
|
|
|
|
|
QStandardItemModel model;
|
|
|
|
model.setRowCount(10);
|
|
|
|
model.setColumnCount(1);
|
|
|
|
|
2013-12-30 17:45:40 +00:00
|
|
|
model.setItem(0, createItem(Qt::red, "Red is a color. Some more text. I'm out of ideas. 42. What's your name?", "Colorful"));
|
2013-12-24 10:47:30 +00:00
|
|
|
model.setItem(1, createItem(Qt::blue, "Blue", "Colorful"));
|
|
|
|
model.setItem(2, createItem(Qt::yellow, "Yellow", "Colorful"));
|
|
|
|
|
|
|
|
model.setItem(3, createItem(Qt::black, "Black", "Not Colorful"));
|
|
|
|
model.setItem(4, createItem(Qt::darkGray, "Dark Gray", "Not Colorful"));
|
|
|
|
model.setItem(5, createItem(Qt::gray, "Gray", "Not Colorful"));
|
|
|
|
model.setItem(6, createItem(Qt::lightGray, "Light Gray", "Not Colorful"));
|
|
|
|
model.setItem(7, createItem(Qt::white, "White", "Not Colorful"));
|
|
|
|
|
|
|
|
model.setItem(8, createItem(Qt::darkGreen, "Dark Green", ""));
|
|
|
|
model.setItem(9, createItem(Qt::green, "Green", ""));
|
|
|
|
|
2013-12-30 17:46:12 +00:00
|
|
|
for (int i = 0; i < 20; ++i)
|
2013-12-26 21:02:25 +00:00
|
|
|
{
|
|
|
|
model.setItem(i + 10, createItem(i+1, "Items 1-20"));
|
|
|
|
}
|
|
|
|
|
2013-12-24 10:47:30 +00:00
|
|
|
CategorizedProxyModel pModel;
|
|
|
|
pModel.setSourceModel(&model);
|
|
|
|
|
|
|
|
CategorizedView w;
|
2013-12-30 17:45:40 +00:00
|
|
|
w.setItemDelegate(new ListViewDelegate);
|
2013-12-24 10:47:30 +00:00
|
|
|
w.setModel(&pModel);
|
|
|
|
w.resize(640, 480);
|
|
|
|
w.show();
|
|
|
|
|
|
|
|
return a.exec();
|
|
|
|
}
|