2021-08-14 03:32:43 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#include <QSplitter>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#define GROUP_COVER0 Qt::DecorationRole
|
|
|
|
#define GROUP_COVER1 Qt::UserRole + 3
|
|
|
|
#define GROUP_COVER2 Qt::UserRole + 4
|
2021-08-14 07:50:39 +00:00
|
|
|
#define GROUP_TITLE Qt::DisplayRole
|
|
|
|
#define GROUP_ITEMSCOUNT Qt::UserRole + 1
|
|
|
|
|
|
|
|
#define ITEM_ICON Qt::DecorationRole
|
|
|
|
#define ITEM_TITLE Qt::DisplayRole
|
2021-08-14 03:32:43 +00:00
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget* parent)
|
|
|
|
: QMainWindow(parent), groups_view(new QListView(this)),
|
|
|
|
groups_list(new QStandardItemModel(this)), items_view(new QListView(this)),
|
|
|
|
items_list(new QStandardItemModel(this))
|
|
|
|
{
|
|
|
|
groups_view->setModel(groups_list);
|
|
|
|
items_view->setModel(items_list);
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
auto splitter = new QSplitter(this);
|
|
|
|
setCentralWidget(splitter);
|
|
|
|
|
|
|
|
splitter->addWidget(groups_view);
|
|
|
|
splitter->addWidget(items_view);
|
|
|
|
|
|
|
|
groups_list->setHorizontalHeaderLabels(QStringList() << "数量"
|
|
|
|
<< "名称");
|
|
|
|
groups_view->setItemDelegate(new GroupDisplayDelegate);
|
|
|
|
groups_view->setMovement(QListView::Free);
|
|
|
|
init_groups_list(groups_list);
|
2021-08-14 07:50:39 +00:00
|
|
|
|
|
|
|
init_groups_list(items_list);
|
|
|
|
items_view->setItemDelegate(new ListIconDelegate());
|
2021-08-14 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::init_groups_list(QStandardItemModel* model)
|
|
|
|
{
|
|
|
|
model->removeRows(0, model->rowCount());
|
|
|
|
|
|
|
|
QRandomGenerator gen;
|
|
|
|
for (int idx = 0; idx < 200; ++idx) {
|
|
|
|
QList<QStandardItem*> row;
|
|
|
|
row << new QStandardItem("无名集合0");
|
|
|
|
row.last()->setData(gen.generate() % 100, GROUP_ITEMSCOUNT);
|
|
|
|
|
|
|
|
model->appendRow(row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupDisplayDelegate::GroupDisplayDelegate()
|
|
|
|
: default_cover(new QIcon(":/imgs/default/cover.jpg"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupDisplayDelegate::~GroupDisplayDelegate()
|
|
|
|
{
|
|
|
|
delete default_cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget*
|
|
|
|
GroupDisplayDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
auto font = option.font;
|
|
|
|
font.setPointSize(26);
|
|
|
|
auto ins = new QLineEdit(parent);
|
|
|
|
ins->setFont(font);
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GroupDisplayDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
static_cast<QLineEdit*>(editor)->setText(index.data().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GroupDisplayDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
model->setData(index, static_cast<QLineEdit*>(editor)->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto text_rect = [](const QRect& whole_rect, double font_height) -> QRectF {
|
|
|
|
return QRectF(whole_rect.left() + 145, whole_rect.top() + 20, whole_rect.width() - 165, font_height);
|
|
|
|
};
|
|
|
|
void
|
|
|
|
GroupDisplayDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
QStyleOptionViewItem new_ = option;
|
|
|
|
new_.font.setPointSize(26);
|
|
|
|
new_.fontMetrics = QFontMetrics(new_.font);
|
|
|
|
editor->setGeometry(text_rect(option.rect, new_.fontMetrics.height()).toRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GroupDisplayDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
drawBackground(painter, option, index);
|
|
|
|
|
|
|
|
auto cover0 = index.data(GROUP_COVER0);
|
|
|
|
if (cover0.isNull())
|
|
|
|
cover0 = default_cover->pixmap(QSize(100, 150));
|
2021-08-14 07:50:39 +00:00
|
|
|
else
|
|
|
|
cover0 = cover0.value<QIcon>().pixmap(QSize(100, 150));
|
2021-08-14 03:32:43 +00:00
|
|
|
|
|
|
|
auto cover1 = index.data(GROUP_COVER1);
|
|
|
|
if (cover1.isNull())
|
|
|
|
cover1 = default_cover->pixmap(QSize(100, 150));
|
2021-08-14 07:50:39 +00:00
|
|
|
else
|
|
|
|
cover0 = cover0.value<QIcon>().pixmap(QSize(100, 150));
|
2021-08-14 03:32:43 +00:00
|
|
|
|
|
|
|
auto cover2 = index.data(GROUP_COVER2);
|
|
|
|
if (cover2.isNull())
|
|
|
|
cover2 = default_cover->pixmap(QSize(100, 150));
|
2021-08-14 07:50:39 +00:00
|
|
|
else
|
|
|
|
cover0 = cover0.value<QIcon>().pixmap(QSize(100, 150));
|
2021-08-14 03:32:43 +00:00
|
|
|
|
|
|
|
auto valid_rect = option.rect - QMargins(15, 15, 15, 15);
|
|
|
|
|
|
|
|
auto pixmap1 = cover1.value<QPixmap>();
|
|
|
|
QRectF rect1(valid_rect.left() + 60, valid_rect.top() + 20, 54, 80);
|
|
|
|
painter->drawPixmap(rect1, pixmap1, QRectF(0, 0, 100, 150));
|
|
|
|
|
|
|
|
auto pixmap2 = cover2.value<QPixmap>();
|
|
|
|
QRectF rect2(valid_rect.left() + 34, valid_rect.top() + 10, 66, 100);
|
|
|
|
painter->drawPixmap(rect2, pixmap2, QRectF(0, 0, 100, 150));
|
|
|
|
|
|
|
|
auto pixmap0 = cover0.value<QPixmap>();
|
|
|
|
QRectF rect0(valid_rect.left(), valid_rect.top(), 80, 120);
|
|
|
|
painter->drawPixmap(rect0, pixmap0, QRectF(0, 0, 100, 150));
|
|
|
|
|
|
|
|
QStyleOptionViewItem new_copy = option;
|
|
|
|
new_copy.font.setPointSize(26);
|
|
|
|
new_copy.fontMetrics = QFontMetrics(new_copy.font);
|
|
|
|
drawDisplay(painter, new_copy, text_rect(option.rect, new_copy.fontMetrics.height()).toRect(), index.data().toString());
|
|
|
|
|
|
|
|
new_copy.font.setPointSize(18);
|
|
|
|
new_copy.fontMetrics = QFontMetrics(new_copy.font);
|
|
|
|
drawDisplay(painter, new_copy, new_copy.rect - QMargins(160, 60, 0, 0), QString("总藏书:%1").arg(index.data(GROUP_ITEMSCOUNT).toUInt()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
|
|
|
GroupDisplayDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
return QSize(400, 145);
|
|
|
|
}
|
2021-08-14 07:50:39 +00:00
|
|
|
|
|
|
|
ListIconDelegate::ListIconDelegate()
|
|
|
|
: default_cover(new QIcon(":/imgs/default/cover.jpg"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ListIconDelegate::~ListIconDelegate()
|
|
|
|
{
|
|
|
|
delete default_cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListIconDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
drawBackground(painter, option, index);
|
|
|
|
|
|
|
|
auto valid_rect = option.rect - QMargins(15, 15, 15, 15);
|
|
|
|
auto cover = index.data(ITEM_ICON);
|
|
|
|
if (!cover.isValid())
|
|
|
|
cover = default_cover->pixmap(100, 150);
|
|
|
|
else
|
|
|
|
cover = cover.value<QIcon>().pixmap(QSize(100, 150));
|
|
|
|
|
|
|
|
painter->drawPixmap(valid_rect, cover.value<QPixmap>());
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
|
|
|
ListIconDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
return QSize(100, 160);
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget*
|
|
|
|
ListIconDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
return new QLineEdit(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListIconDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
static_cast<QLineEdit*>(editor)->setText(index.data().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListIconDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
model->setData(index, static_cast<QLineEdit*>(editor)->text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListIconDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|