-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteList.cpp
More file actions
254 lines (217 loc) · 7.71 KB
/
Copy pathNoteList.cpp
File metadata and controls
254 lines (217 loc) · 7.71 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
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QTextDocument>
#include <QPainter>
#include "common_headers.h"
#include "NoteStg.h"
#include "NoteList.h"
///////////////////////////////////////////////////////////
bool QNoteList::_CTreeWidgetItem::operator<(const QTreeWidgetItem& other) const
{
//2014.4.14 Customize sorting of QTreeWidget
//http://www.qtforum.org/article/25281/customize-sorting-of-qtreewidget.html?s=e835cf0d2cc3ec9144ed5def1e3f9d904366b523#post89340
//A QTreeWidget is sorted alphabetically by default, how can I change this behavior, e.g to sort numerically instead?
//http://qt-project.org/faq/answer/a_qtreewidget_is_sorted_alphabetically_by_default_how_can_i_change_this_beh
int iSortCol = treeWidget()->sortColumn();
bool bLess=false;
switch(iSortCol){
case _COL_CREATED:
{
QVariant v1=data(iSortCol, Qt::UserRole);
QVariant v2=other.data(iSortCol, Qt::UserRole);
QDateTime d1 = v1.value<QDateTime>();
QDateTime d2 = v2.value<QDateTime>();
bLess = d1 < d2;
break;
}
default:
{
QString d1 = text(iSortCol);
QString d2 = other.text(iSortCol);
bLess = d1 < d2;
break;
}
}
return bLess;
}
///////////////////////////////////////////////////////////
QNoteList::QNoteList(_CNoteStg* pDb, QWidget* parent)
: QTreeWidget(parent)
, m_pStg(pDb)
, m_bBlockChkSignal(false)
{
QTreeWidget::setAlternatingRowColors(true);
QTreeWidget::setRootIsDecorated(false);
QTreeWidget::setTextElideMode(Qt::ElideRight);
QTreeWidget::setSortingEnabled(true);
{
_CTextSplitter vHeaders(_lc("List.Header.Notes", "Content|Created Time|Display|Read Only"), "|");
QTreeWidget::setColumnCount(vHeaders.size());
QTreeWidget::header()->setStretchLastSection(false);
QTreeWidget::header()->setSectionsClickable(true);
QTreeWidget::header()->setSortIndicatorShown(true);
QObject::connect(QTreeWidget::header(), SIGNAL(sectionClicked(int)), this, SLOT(onAttempToSortByColumn(int)));
// QTreeWidget::setHeaderLabels(vHeaders);
QTreeWidget::headerItem()->setText(0, vHeaders[_COL_CONTENT]);
QTreeWidget::headerItem()->setText(1, vHeaders[_COL_CREATED]);
QTreeWidget::headerItem()->setText(_COL_DISPLAY, "");
QTreeWidget::headerItem()->setIcon(_COL_DISPLAY, QIcon(":/images/ico_display.png"));
QTreeWidget::headerItem()->setToolTip(_COL_DISPLAY, vHeaders[_COL_DISPLAY]);
QTreeWidget::header()->setSectionResizeMode(_COL_DISPLAY, QHeaderView::Fixed);
QTreeWidget::headerItem()->setText(_COL_READONLY, "");
QTreeWidget::headerItem()->setIcon(_COL_READONLY, QIcon(":/images/ico_readonly.png"));
QTreeWidget::headerItem()->setToolTip(_COL_READONLY, vHeaders[_COL_READONLY]);
QTreeWidget::header()->setSectionResizeMode(_COL_READONLY, QHeaderView::Fixed);
}
QObject::connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(onNoteDisplayChecked(QTreeWidgetItem*,int)));
QObject::connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(onNoteReadOnlyChecked(QTreeWidgetItem*,int)));
loadNotes();
//2018.1.20 Sort after loading notes
QTreeWidget::sortByColumn(_COL_CREATED, Qt::DescendingOrder);
m_xCurrentSortState.m_iLogicalIndex = _COL_CREATED;
m_xCurrentSortState.m_iSortOrder = Qt::DescendingOrder;
}
int QNoteList::indexOfNote(const QString& sName) const
{
int nIndex = -1;
for(int i = 0; i < QTreeWidget::topLevelItemCount(); i++){
QTreeWidgetItem* pItem = QTreeWidget::topLevelItem(i);
if(pItem->data(_COL_CONTENT, Qt::UserRole).toString() == sName){
nIndex = i;
break;
}
}
return nIndex;
}
void QNoteList::addNote(const QString& sName)
{
_CTreeWidgetItem* pItem = new _CTreeWidgetItem(this);
pItem->setData(_COL_CONTENT, Qt::UserRole, QVariant(sName));
refreshNote(sName, false);
}
void QNoteList::removeNote(const QString& sName)
{
for(int i = 0; i < QTreeWidget::topLevelItemCount(); i++){
QTreeWidgetItem* pItem = QTreeWidget::topLevelItem(i);
if(pItem->data(_COL_CONTENT, Qt::UserRole).toString() == sName){
QTreeWidget::takeTopLevelItem(i);
break;
}
}
}
void QNoteList::refreshNote(const QString& sName, bool bAutoAddDel)
{
if(!m_pStg->exists(sName)){
if(bAutoAddDel) removeNote(sName);
}else{
int nIndex = indexOfNote(sName);
if(nIndex < 0){
if(bAutoAddDel) addNote(sName);
}else{
QTreeWidgetItem* pItem = QTreeWidget::topLevelItem(nIndex);
_CNoteStg::_CNote xNote; m_pStg->loadNote(sName, xNote);
QColor clPage = xNote.m_clPage;
QString sContent = xNote.m_sContent;
QString sPlain, sTxt;
{
QTextDocument xDoc;
xDoc.setHtml(sContent);
sPlain = xDoc.toPlainText();
{
sPlain.replace("\r\n", "\n");
_CTextSplitter v(sPlain, "\n");
_CTextSplitter v0("", "\n");
if(v.size() > 5){
v0 = v.mid(0, 4);
v0 << "...";
}else{
v0 = v;
}
sTxt = (QString)v0;
}
}
QDateTime tCreated = m_pStg->createdTime(sName);
QString sCreated = (QString)_CDateTimeSmart(tCreated);
pItem->setText(_COL_CONTENT, sTxt);
pItem->setText(_COL_CREATED, sCreated);
pItem->setForeground(_COL_CREATED, QBrush(Qt::gray));
pItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
pItem->setCheckState(_COL_DISPLAY, m_pStg->isTrash(sName) ? Qt::Unchecked : Qt::Checked);
pItem->setCheckState(_COL_READONLY, xNote.m_bReadOnly ? Qt::Checked : Qt::Unchecked);
pItem->setToolTip(_COL_CONTENT, sPlain);
pItem->setData(_COL_CREATED, Qt::UserRole, QVariant(tCreated));
QIcon xIcon;
{
QPixmap xPixmap(16, 16); xPixmap.fill(Qt::transparent);
QPainter xPainter(&xPixmap);
xPainter.setPen(QPen(QBrush(Qt::black), 1, Qt::SolidLine));
xPainter.setBrush(QBrush(clPage, Qt::SolidPattern));
xPainter.drawRect(0, 0, 15, 15);
xIcon = QIcon(xPixmap);
}
pItem->setIcon(_COL_CONTENT, xIcon);
}
}
}
void QNoteList::loadNotes()
{
_CTempValueChange<bool> _tvc(m_bBlockChkSignal, true); Q_UNUSED(_tvc);
QTreeWidget::clear();
std::vector<QString> v; m_pStg->listNotes(v, true);
Q_FOREACH(QString sName, v){
addNote(sName);
}
}
void QNoteList::resizeEvent(QResizeEvent* e)
{
QTreeWidget::resizeEvent(e);
QList<int> vSizes; vSizes << 0 << 200 << 30 << 30;
int nOccupiedSize = 0;
Q_FOREACH(int nSize, vSizes){
nOccupiedSize += nSize;
}
vSizes[0] = QTreeWidget::viewport()->width() - nOccupiedSize;
for(int i = 0; i < vSizes.size(); i++){
QTreeWidget::setColumnWidth(i, vSizes[i]);
}
}
void QNoteList::onNoteDisplayChecked(QTreeWidgetItem* pItem, int nCol)
{
if(!m_bBlockChkSignal){
if(nCol == _COL_DISPLAY){
QString sName = pItem->data(_COL_CONTENT, Qt::UserRole).toString();
emit noteVisibilityChanged(sName, pItem->checkState(_COL_DISPLAY) == Qt::Checked);
}
}
}
void QNoteList::onNoteReadOnlyChecked(QTreeWidgetItem* pItem, int nCol)
{
if(!m_bBlockChkSignal){
if(nCol == _COL_READONLY){
QString sNoteName = pItem->data(_COL_CONTENT, Qt::UserRole).toString();
emit noteReadOnlyChanged(sNoteName, pItem->checkState(_COL_READONLY) == Qt::Checked);
}
}
}
void QNoteList::onAttempToSortByColumn(int iLogicalIndex)
{
if(m_xCurrentSortState.m_iLogicalIndex != iLogicalIndex){
switch(iLogicalIndex){
case _COL_CONTENT:
m_xCurrentSortState.m_iSortOrder = Qt::AscendingOrder;
m_xCurrentSortState.m_iLogicalIndex = iLogicalIndex;
break;
case _COL_CREATED:
m_xCurrentSortState.m_iSortOrder = Qt::DescendingOrder;
m_xCurrentSortState.m_iLogicalIndex = iLogicalIndex;
break;
default:
break;
}
}else{
m_xCurrentSortState.m_iSortOrder = (m_xCurrentSortState.m_iSortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder;
}
QTreeWidget::header()->setSortIndicator(m_xCurrentSortState.m_iLogicalIndex, m_xCurrentSortState.m_iSortOrder);
QTreeWidget::sortByColumn(m_xCurrentSortState.m_iLogicalIndex, m_xCurrentSortState.m_iSortOrder);
}