-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotewidgetdelegate.cpp
More file actions
418 lines (367 loc) · 15.2 KB
/
Copy pathnotewidgetdelegate.cpp
File metadata and controls
418 lines (367 loc) · 15.2 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include "notewidgetdelegate.h"
#include "noteview.h"
#include <QPainter>
#include <QEvent>
#include <QDebug>
#include <QApplication>
#include <QFontDatabase>
#include <QtMath>
#include "notemodel.h"
#include "widget.h"
NoteWidgetDelegate::NoteWidgetDelegate(QObject *parent)
: QStyledItemDelegate(parent),
m_titleFont(QStringLiteral(""), 14, 30), //标题字体
m_titleSelectedFont(QStringLiteral(""), 14), //
m_dateFont(QStringLiteral(""), 10), //日期字体
m_titleColor(255, 255, 255), //标题颜色
m_dateColor(255, 255, 255), //日期颜色
m_ActiveColor(218, 233, 239),
m_notActiveColor(175, 212, 228), //默认选中背景色
m_hoverColor(80, 80, 80), //悬停颜色
m_selectColor(43, 49, 60), //选中颜色43, 49, 60 40,40,40
m_applicationInactiveColor(207, 207, 207), //应用程序可见,但未选择显示在前面时背景色
m_separatorColor(221, 221, 221),
m_defaultColor(0, 0, 0),
m_noteColor(0, 0, 0), //便签头颜色
m_rowHeight(82), //item宽度
m_maxFrame(200),
m_rowRightOffset(0),
m_state(Normal),
m_isActive(false)
{
//QTimeLine拥有一个时间轴来控制时间动画和事件
//参数1为毫秒级 总运行时间0.3秒
m_timeLine = new QTimeLine(300, this);
//对QTImeLine的时间轴设置动画帧数,共200张静态画面图组成
//Qt默认40ms一帧,也就是一秒可以最多走25帧
m_timeLine->setFrameRange(0,m_maxFrame);
//时间轴走的时候,会不断发出frameChanged信号,setUpdateInterval控制多少时间发一次
m_timeLine->setUpdateInterval(10);
//设置你的时间变换曲线,即明确你的时间是先快后慢,还是先慢后快,或者线性
m_timeLine->setCurveShape(QTimeLine::EaseInCurve);
connect( m_timeLine, &QTimeLine::frameChanged, [this](){
emit sizeHintChanged(m_animatedIndex);
});
connect(m_timeLine, &QTimeLine::finished, [this](){
m_animatedIndex = QModelIndex();
m_state = Normal;
});
}
void NoteWidgetDelegate::setState(States NewState, QModelIndex index)
{
m_animatedIndex = index;
auto startAnimation = [this](QTimeLine::Direction diretion, int duration){
m_timeLine->setDirection(diretion);
m_timeLine->setDuration(duration);
m_timeLine->start();
};
switch ( NewState ){
case Insert:
startAnimation(QTimeLine::Forward, m_maxFrame);
break;
case Remove:
startAnimation(QTimeLine::Backward, m_maxFrame);
break;
case MoveOut:
startAnimation(QTimeLine::Backward, m_maxFrame);
break;
case MoveIn:
startAnimation(QTimeLine::Backward, m_maxFrame);
break;
case Normal:
m_animatedIndex = QModelIndex();
break;
}
m_state = NewState;
}
void NoteWidgetDelegate::setAnimationDuration(const int duration)
{
m_timeLine->setDuration(duration);
}
void NoteWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.rect.setWidth(option.rect.width() - m_rowRightOffset); //678
int currentFrame = m_timeLine->currentFrame(); //0
double rate = (currentFrame/(m_maxFrame * 1.0)); //0
double height = m_rowHeight * rate; //0
//默认Normal
switch(m_state){
case Insert:
case Remove:
case MoveOut:
if(index == m_animatedIndex){
opt.rect.setHeight(int(height));
opt.backgroundBrush.setColor(m_notActiveColor);
}
break;
case MoveIn:
if(index == m_animatedIndex){
opt.rect.setY(int(height));
}
break;
case Normal:
break;
}
//绘制第一层便签头背景
int m_noteColor{index.data(NoteModel::NoteColor).toInt()};
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
painter->setBrush(QBrush(intToQcolor(m_noteColor)));
painter->setPen(Qt::transparent);
opt.rect.setWidth(678);
opt.rect.setHeight(opt.rect.height() - 5);
{
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 7, 7);
painter->drawPath(painterPath);
}
//绘制第二层底色背景
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
if(sink == 0)
{
painter->setBrush(QColor(40,40,46));
}else{
painter->setBrush(QColor(245,245,245));
}
painter->setPen(Qt::transparent);
opt.rect.setWidth(678);
opt.rect.setHeight(opt.rect.height() - 0);
opt.rect.setLeft(opt.rect.left() + 5);
{
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 0, 0);
painter->drawPath(painterPath);
}
paintBackground(painter, opt, index);
paintLabels(painter, option, index);
}
QSize NoteWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSize result = QStyledItemDelegate::sizeHint(option, index); //QSize(0, 23)
if(index == m_animatedIndex){
if(m_state == MoveIn){
result.setHeight(m_rowHeight);
}else{
double rate = m_timeLine->currentFrame()/(m_maxFrame * 1.0);
double height = m_rowHeight * rate;
result.setHeight(int(height));
}
}else{
result.setHeight(m_rowHeight);
}
return result;
}
QTimeLine::State NoteWidgetDelegate::animationState()
{
return m_timeLine->state();
}
void NoteWidgetDelegate::paintBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
opt.rect.setWidth(option.rect.width() - m_rowRightOffset);
if((option.state & QStyle::State_Selected) == QStyle::State_Selected)
{
//应用程序是可见的,并被选择在前面。
if(qApp->applicationState() == Qt::ApplicationActive){ //返回应用程序的当前状态。
if(m_isActive){//用指定的画笔填充给定的矩形。
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
if(sink == 0){
painter->setBrush(QBrush(m_selectColor));
}else{
painter->setBrush(QBrush(QColor(255,255,255)));
}
painter->setPen(Qt::transparent);
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 0, 0);
painter->drawPath(painterPath);
//painter->fillRect(option.rect, QBrush(m_ActiveColor));//浅蓝
}else{
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
if(sink == 0){
painter->setBrush(QBrush(m_selectColor));
}else{
painter->setBrush(QBrush(QColor(255,255,255)));
} painter->setPen(Qt::transparent);
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 0, 0);
painter->drawPath(painterPath);
//painter->fillRect(option.rect, QBrush(m_notActiveColor));//深蓝
}
//应用程序可见,但未选择显示在前面
}else if(qApp->applicationState() == Qt::ApplicationInactive){
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
if(sink == 0){
painter->setBrush(QBrush(m_selectColor));
}else{
painter->setBrush(QBrush(QColor(255,255,255)));
}
//painter->setBrush(QBrush(m_defaultColor));
painter->setPen(Qt::transparent);
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 0, 0);
painter->drawPath(painterPath);
//painter->fillRect(option.rect, QBrush(m_defaultColor));
}
}
//鼠标悬停时颜色
//用于指示小部件是否在鼠标下。
else if((option.state & QStyle::State_MouseOver) == QStyle::State_MouseOver){
//qDebug() << "当前文件 :" << __FILE__ << "当前函数 :" << __FUNCTION__ << "当前行号 :" << __LINE__;
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
if(sink == 0){
painter->setBrush(QBrush(m_hoverColor));
}else{
painter->setBrush(QBrush(QColor(205,205,205)));
}
painter->setPen(Qt::transparent);
QPainterPath painterPath;
painterPath.addRoundedRect(opt.rect, 0, 0);
painter->drawPath(painterPath);
//painter->fillRect(option.rect, QBrush(m_hoverColor));//灰色
//当前item未选中 未悬停时颜色
}else if((index.row() != m_currentSelectedIndex.row() - 1)
&& (index.row() != m_hoveredIndex.row() - 1)){
painter->setRenderHint(QPainter::Antialiasing); // 反锯齿;
//painter->fillRect(option.rect, QBrush(m_defaultColor));//黑色
}
}
void NoteWidgetDelegate::paintLabels(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const int leftOffsetX = 20;
const int topOffsetY = 18; // 标题上方的空格
const int spaceY = 5; // 标题和日期之间的空格
QString title{index.data(NoteModel::NoteFullTitle).toString()};
QFont titleFont = (option.state & QStyle::State_Selected) == QStyle::State_Selected ? m_titleSelectedFont : m_titleFont;
QFontMetrics fmTitle(titleFont);
QRect fmRectTitle = fmTitle.boundingRect(title);
QString date = parseDateTime(index.data(NoteModel::NoteLastModificationDateTime).toDateTime());
QFontMetrics fmDate(m_dateFont);
QRect fmRectDate = fmDate.boundingRect(title);
double rowPosX = option.rect.x();
double rowPosY = option.rect.y();
double rowWidth = option.rect.width();
double titleRectPosX = rowPosX + leftOffsetX;
double titleRectPosY = rowPosY;
double titleRectWidth = rowWidth - 2.0 * leftOffsetX;
double titleRectHeight = fmRectTitle.height() + topOffsetY;
double dateRectPosX = rowPosX + leftOffsetX;
double dateRectPosY = rowPosY + fmRectTitle.height() + topOffsetY;
double dateRectWidth = rowWidth - 2.0 * leftOffsetX;
double dateRectHeight = fmRectDate.height() + spaceY;
double rowRate = m_timeLine->currentFrame()/(m_maxFrame * 1.0);
double currRowHeight = m_rowHeight * rowRate;
auto drawStr = [painter](double posX, double posY, double width, double height, QColor color, QFont font, QString str){
QRectF rect(posX, posY, width, height);
painter->setPen(color);
painter->setFont(font);
painter->drawText(rect, Qt::AlignBottom, str);
};
// 设置标题和日期字符串的边界矩形
if(index.row() == m_animatedIndex.row()){
if(m_state == MoveIn){
titleRectHeight = topOffsetY + fmRectTitle.height() + currRowHeight;
dateRectPosY = titleRectHeight;
dateRectHeight = fmRectDate.height() + spaceY;
}else{
if((fmRectTitle.height() + topOffsetY) >= ((1.0 - rowRate) * m_rowHeight)){
titleRectHeight = (fmRectTitle.height() + topOffsetY) - (1.0 - rowRate) * m_rowHeight;
}else{
titleRectHeight = 0;
double labelsSumHeight = fmRectTitle.height() + topOffsetY + fmRectDate.height() + spaceY;
double bottomSpace = m_rowHeight - labelsSumHeight;
if(currRowHeight > bottomSpace){
dateRectHeight = currRowHeight - bottomSpace;
}else{
dateRectHeight = 0;
}
}
dateRectPosY = titleRectHeight + rowPosY;
}
}
// 绘图标题和日期
title = fmTitle.elidedText(title, Qt::ElideRight, int(titleRectWidth));
if(sink == 1)
{
drawStr(titleRectPosX, titleRectPosY, titleRectWidth, titleRectHeight, QColor(0,0,0), titleFont, title);
drawStr(dateRectPosX, dateRectPosY, dateRectWidth, dateRectHeight, QColor(0,0,0), m_dateFont, date);
}else{
drawStr(titleRectPosX, titleRectPosY, titleRectWidth, titleRectHeight, QColor(244,244,244), titleFont, title);
drawStr(dateRectPosX, dateRectPosY, dateRectWidth, dateRectHeight, QColor(244,244,244), m_dateFont, date);
}
}
void NoteWidgetDelegate::paintSeparator(QPainter*painter, const QStyleOptionViewItem&option, const QModelIndex&index) const
{
Q_UNUSED(index)
painter->setPen(QPen(m_separatorColor));
const int leftOffsetX = 11;
int posX1 = option.rect.x() + leftOffsetX;
int posX2 = option.rect.x() + option.rect.width() - leftOffsetX - 1;
int posY = option.rect.y() + option.rect.height() - 1;
painter->drawLine(QPoint(posX1, posY),
QPoint(posX2, posY));
}
QString NoteWidgetDelegate::parseDateTime(const QDateTime &dateTime) const
{
QLocale usLocale = QLocale::system();
QString d;
auto currDateTime = QDateTime::currentDateTime();
if(dateTime.date() == currDateTime.date()){
d = tr("Today ");
d.append(usLocale.toString(dateTime.time(),"hh:mm"));
return d;
}else if(dateTime.daysTo(currDateTime) == 1){
d = tr("Yesterday ");
d.append(usLocale.toString(dateTime.time(),"hh:mm"));
return d;
}
// else if(dateTime.daysTo(currDateTime) >= 2 &&
// dateTime.daysTo(currDateTime) <= 7){
// return usLocale.toString(dateTime.date(), "dddd");
// }
return dateTime.toString("yyyy/MM/dd hh:mm");
}
void NoteWidgetDelegate::setActive(bool isActive)
{
m_isActive = isActive;
}
void NoteWidgetDelegate::setRowRightOffset(int rowRightOffset)
{
m_rowRightOffset = rowRightOffset;
}
void NoteWidgetDelegate::setHoveredIndex(const QModelIndex &hoveredIndex)
{
m_hoveredIndex = hoveredIndex;
}
void NoteWidgetDelegate::setCurrentSelectedIndex(const QModelIndex ¤tSelectedIndex)
{
m_currentSelectedIndex = currentSelectedIndex;
}
int NoteWidgetDelegate::qcolorToInt(const QColor &color) const
{
//将Color 从QColor 转换成 int
return (int)(((unsigned int)color.blue()<< 16) | (unsigned short)(((unsigned short)color.green()<< 8) | color.red()));
}
QColor NoteWidgetDelegate::intToQcolor(int &intColor) const
{
int red = intColor & 255;
int green = intColor >> 8 & 255;
int blue = intColor >> 16 & 255;
return QColor(red, green, blue);
}