This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzlistview.cpp
151 lines (142 loc) · 5.13 KB
/
zlistview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "zlistview.h"
#include "editor.h"
#include <DGuiApplicationHelper>
#include <DPlatformTheme>
#include <DPushButton>
#include <DThemeManager>
#include <QDebug>
#include <QtWidgets>
DGUI_USE_NAMESPACE
DTK_USE_NAMESPACE
ZListView::ZListView(QWidget* parent)
: QListView(parent)
{
setDragEnabled(false);
setBatchSize(100);
setLayoutMode(QListView::Batched);
setFlow(QListView::TopToBottom);
setWrapping(false);
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setUniformItemSizes(true);
setSpacing(2);
viewport()->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
setItemDelegate(new ItemDelegate(this));
connect(this, &QListView::clicked, [this](const QModelIndex& cur) { emit currentIndexChanged(cur); });
}
QList<QModelIndex> ZListView::selection() const
{
return selectedIndexes();
}
QList<ZNote> ZListView::selectionNotes() const
{
auto indexes = selectedIndexes();
QList<ZNote> rt;
for (auto i : indexes)
rt.push_back(i.data(Qt::UserRole).value<ZNote>());
return rt;
}
void ZListView::clearSelectionExt()
{
if (!selectionModel()->hasSelection())
return;
int front = selectedIndexes().front().row(), back = selectedIndexes().back().row();
// qDebug()<<front<<back<<model()->rowCount();
clearSelection();
if (model()->rowCount() > back + 1)
setCurrentIndex(model()->index(back + 1, 0));
else if (front != 0)
setCurrentIndex(model()->index(front - 1, 0));
else
emit listEmptied();
}
void ZListView::setCurrentIndex(const QModelIndex& cur)
{
// if(cur.isValid()&&cur.model()==model())qDebug()<<"call ZListView::setCurrentIndex"<<cur;
if (selection() == QList<QModelIndex>({ cur }))
return;
clearSelection();
selectionModel()->setCurrentIndex(cur, QItemSelectionModel::SelectCurrent);
emit currentIndexChanged(cur);
}
void ZListView::setNoBackground(bool b)
{
if (!b)
return;
viewport()->setAutoFillBackground(false);
setFrameShape(QFrame::NoFrame);
}
ItemDelegate::ItemDelegate(QWidget* parent)
: QStyledItemDelegate(parent)
{
}
void ItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
// qDebug() << "paint" << index << option << index;
if (!index.isValid())
return;
painter->save();
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QRect rect(option.rect);
rect.adjust(5, 5, -5, -5);
QPainterPath path;
path.addRoundRect(QRectF(rect), 30);
painter->setOpacity(1);
if (option.state.testFlag(QStyle::State_Selected))
painter->fillPath(path, option.palette.dark());
else if (option.state.testFlag(QStyle::State_MouseOver))
painter->fillPath(path, option.palette.midlight());
else
painter->fillPath(path, option.palette.base());
if (option.state.testFlag(QStyle::State_Selected)) {
QRect borderRect(rect);
borderRect.adjust(-3, -3, 3, 3);
QPainterPath borderPath;
borderPath.addRoundRect(QRectF(borderRect), 33);
painter->setPen(QPen(option.palette.highlight(), 2));
painter->drawPath(borderPath);
}
auto data = index.data(Qt::UserRole).value<ZNote>();
rect.adjust(5, 5, -5, -5);
QRect mainRect(rect), infoRect(rect);
mainRect.adjust(0, 0, 0, -20);
infoRect.adjust(0, 40, 0, 0);
//start drawing text
painter->setPen(QPen(option.palette.text(), 1));
//draw abstract
drawMultilineElidedText(painter, QRectF(mainRect), data.getAbstract().simplified());
//draw other info
auto font = painter->font();
font.setItalic(true);
font.setPointSize(8);
painter->setFont(font);
painter->setPen(QPen(Qt::gray, 1));
painter->drawText(QRectF(infoRect), QString("%1 %2 %3 %4").arg(QObject::tr("上次修改"), data.getUpdateTime(), QObject::tr("创建于"), data.getCreateTime()));
painter->restore();
}
QSize ItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
return QSize(option.rect.width(), 80);
}
void ItemDelegate::drawMultilineElidedText(QPainter* painter, const QRectF& rect, const QString& str) const
{
QTextLayout layout(str, painter->font());
QTextOption option;
option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
layout.setTextOption(option);
layout.beginLayout();
for (int lineSpacing = painter->fontMetrics().lineSpacing(), ascent = painter->fontMetrics().ascent(), descent = painter->fontMetrics().descent(), y = 0;;) {
QTextLine line = layout.createLine();
if (!line.isValid())
break;
line.setLineWidth(rect.width());
if (rect.height() > y + lineSpacing + ascent) {
line.draw(painter, QPointF(rect.x(), rect.y() + y));
y += lineSpacing;
} else {
QString lastLine = painter->fontMetrics().elidedText(str.mid(line.textStart()), Qt::ElideRight, rect.width());
painter->drawText(QPointF(rect.x(), rect.y() + y + ascent), lastLine);
break;
}
}
}