Skip to content

Commit c139b82

Browse files
author
Doug Beney
committed
EFFICIENT note list!
1 parent 3160140 commit c139b82

File tree

9 files changed

+171
-228
lines changed

9 files changed

+171
-228
lines changed

VibratoNotes-Desktop.pro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ SOURCES += \
5252
$$PWD/ui/note_edittags.cpp \
5353
$$PWD/src/models/items/listitemwithid.cpp \
5454
$$PWD/src/models/sortfilter/notelistproxymodel.cpp \
55-
$$PWD/src/models/items/notelistitemwidget.cpp \
5655
$$PWD/ui/edittags.cpp \
5756
$$PWD/src/models/views/customtreeview.cpp \
5857
$$PWD/ui/trashitem.cpp \
5958
$$PWD/src/ui-managers/notelist-views/trashview.cpp \
6059
$$PWD/src/ui-managers/notelist-views/genericview.cpp \
6160
$$PWD/ui/notebook_editparent.cpp \
62-
$$PWD/src/sql/sqlmanager.cpp
61+
$$PWD/src/sql/sqlmanager.cpp \
62+
src/models/delegates/noteitemdelegate.cpp
6363

6464
HEADERS += \
6565
$$PWD/src/meta/info/appconfig.h \
@@ -91,14 +91,14 @@ HEADERS += \
9191
$$PWD/ui/note_edittags.h \
9292
$$PWD/src/models/items/listitemwithid.h \
9393
$$PWD/src/models/sortfilter/notelistproxymodel.h \
94-
$$PWD/src/models/items/notelistitemwidget.h \
9594
$$PWD/ui/edittags.h \
9695
$$PWD/src/models/views/customtreeview.h \
9796
$$PWD/ui/trashitem.h \
9897
$$PWD/src/ui-managers/notelist-views/trashview.h \
9998
$$PWD/src/ui-managers/notelist-views/genericview.h \
10099
$$PWD/ui/notebook_editparent.h \
101-
$$PWD/src/sql/sqlmanager.h
100+
$$PWD/src/sql/sqlmanager.h \
101+
src/models/delegates/noteitemdelegate.h
102102

103103
INCLUDEPATH += $$PWD/include
104104
INCLUDEPATH += $$PWD/src/models/views # Location of customlistview
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "noteitemdelegate.h"
2+
#include <QPainter>
3+
#include <QDebug>
4+
#include <QApplication>
5+
#include <QMouseEvent>
6+
#include <QAbstractItemModel>
7+
#include "../items/notelistitem.h"
8+
9+
NoteItemDelegate::NoteItemDelegate(QListView *view, QSortFilterProxyModel *proxyModel) :
10+
m_view(view),
11+
m_proxyModel(proxyModel)
12+
{
13+
14+
}
15+
16+
QRect NoteItemDelegate::getStarRect(const QStyleOptionViewItem &option) const
17+
{
18+
return QRect(option.rect.x()+option.rect.width()-35,
19+
option.rect.y()+option.rect.height()/2-12,
20+
25,25);
21+
}
22+
23+
24+
bool NoteItemDelegate::editorEvent(QEvent *event,
25+
QAbstractItemModel *model,
26+
const QStyleOptionViewItem &option,
27+
const QModelIndex &index)
28+
{
29+
if (event->type() == QEvent::MouseButtonRelease)
30+
{
31+
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
32+
if(mouseEvent->button() == Qt::LeftButton)
33+
{
34+
QRect checkboxRect = getStarRect(option);
35+
QPoint mousePoint = mouseEvent->pos();
36+
if ( checkboxRect.contains(mousePoint) ) {
37+
QModelIndex realIndex = m_proxyModel->mapToSource(index);
38+
NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() );
39+
item->note()->setFavorited( !item->note()->favorited() );
40+
return true;
41+
}
42+
else
43+
m_view->setCurrentIndex(index);
44+
}
45+
}
46+
47+
return QStyledItemDelegate::editorEvent(event, model, option, index);
48+
}
49+
50+
void NoteItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
51+
{
52+
if (index.column() == 0 ) {
53+
QModelIndex realIndex = m_proxyModel->mapToSource(index);
54+
NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() );
55+
56+
// Set the background color
57+
QBrush background =
58+
(option.state & QStyle::State_Selected) ?
59+
option.palette.highlight() :
60+
option.palette.base();
61+
painter->fillRect(option.rect, background);
62+
63+
if (option.state & QStyle::State_Selected)
64+
painter->setPen(option.palette.highlightedText().color());
65+
else
66+
painter->setPen(option.palette.text().color());
67+
68+
QRect titleRect = option.rect;
69+
titleRect.setX(titleRect.x()+5);
70+
titleRect.setWidth(titleRect.x()-5);
71+
72+
QFont font=painter->font() ;
73+
font.setPointSize(10);
74+
75+
// Title
76+
font.setWeight(QFont::Bold);
77+
painter->setFont(font);
78+
painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+23), item->note()->title());
79+
80+
// Date
81+
font.setWeight(QFont::Normal);
82+
painter->setFont(font);
83+
painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+43), item->note()->date_created_str());
84+
85+
86+
// The Excerpt
87+
QRect excerptRect = option.rect;
88+
excerptRect.setX( excerptRect.x()+10 );
89+
excerptRect.setY( excerptRect.y()+50 );
90+
excerptRect.setWidth( excerptRect.width()-50 );
91+
92+
QString excerpt = item->note()->text();
93+
if (excerpt.length() > 50) {
94+
excerpt = excerpt.mid(0, 50) + "...";
95+
}
96+
excerpt.replace("\n", " ");
97+
painter->drawText(excerptRect, excerpt);
98+
99+
painter->fillRect(QRect(
100+
option.rect.x() + option.rect.width() - 60,
101+
option.rect.y(),
102+
60,
103+
option.rect.height()), background);
104+
105+
QIcon favoriteIcon;
106+
if (item->note()->favorited())
107+
favoriteIcon = QIcon::fromTheme("vibrato-draw-star-solid");
108+
else
109+
favoriteIcon = QIcon::fromTheme("vibrato-draw-star");
110+
111+
favoriteIcon.paint(painter, getStarRect(option));
112+
113+
} else {
114+
QStyledItemDelegate::paint(painter, option, index);
115+
}
116+
}
117+
118+
QSize NoteItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
119+
{
120+
return QSize(200,100);
121+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef NOTEITEMDELEGATE_H
2+
#define NOTEITEMDELEGATE_H
3+
#include <QStyledItemDelegate>
4+
#include <QListView>
5+
#include <QSortFilterProxyModel>
6+
7+
class NoteItemDelegate : public QStyledItemDelegate
8+
{
9+
public:
10+
NoteItemDelegate(QListView *view, QSortFilterProxyModel *proxyModel);
11+
12+
QRect getStarRect(const QStyleOptionViewItem &option) const;
13+
14+
bool editorEvent(QEvent *event,
15+
QAbstractItemModel *model,
16+
const QStyleOptionViewItem &option,
17+
const QModelIndex &index) override;
18+
19+
void paint(QPainter *painter, const QStyleOptionViewItem &option,
20+
const QModelIndex &index) const override;
21+
22+
QSize sizeHint(const QStyleOptionViewItem &option,
23+
const QModelIndex &index) const override;
24+
25+
private:
26+
bool checked = false;
27+
QListView *m_view;
28+
QSortFilterProxyModel *m_proxyModel;
29+
};
30+
31+
#endif // MYDELEGATE_H

src/models/items/notelistitemwidget.cpp

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/models/items/notelistitemwidget.h

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)