Skip to content

Commit

Permalink
EFFICIENT note list!
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Beney committed Jan 21, 2019
1 parent 3160140 commit c139b82
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 228 deletions.
8 changes: 4 additions & 4 deletions VibratoNotes-Desktop.pro
Expand Up @@ -52,14 +52,14 @@ SOURCES += \
$$PWD/ui/note_edittags.cpp \ $$PWD/ui/note_edittags.cpp \
$$PWD/src/models/items/listitemwithid.cpp \ $$PWD/src/models/items/listitemwithid.cpp \
$$PWD/src/models/sortfilter/notelistproxymodel.cpp \ $$PWD/src/models/sortfilter/notelistproxymodel.cpp \
$$PWD/src/models/items/notelistitemwidget.cpp \
$$PWD/ui/edittags.cpp \ $$PWD/ui/edittags.cpp \
$$PWD/src/models/views/customtreeview.cpp \ $$PWD/src/models/views/customtreeview.cpp \
$$PWD/ui/trashitem.cpp \ $$PWD/ui/trashitem.cpp \
$$PWD/src/ui-managers/notelist-views/trashview.cpp \ $$PWD/src/ui-managers/notelist-views/trashview.cpp \
$$PWD/src/ui-managers/notelist-views/genericview.cpp \ $$PWD/src/ui-managers/notelist-views/genericview.cpp \
$$PWD/ui/notebook_editparent.cpp \ $$PWD/ui/notebook_editparent.cpp \
$$PWD/src/sql/sqlmanager.cpp $$PWD/src/sql/sqlmanager.cpp \
src/models/delegates/noteitemdelegate.cpp


HEADERS += \ HEADERS += \
$$PWD/src/meta/info/appconfig.h \ $$PWD/src/meta/info/appconfig.h \
Expand Down Expand Up @@ -91,14 +91,14 @@ HEADERS += \
$$PWD/ui/note_edittags.h \ $$PWD/ui/note_edittags.h \
$$PWD/src/models/items/listitemwithid.h \ $$PWD/src/models/items/listitemwithid.h \
$$PWD/src/models/sortfilter/notelistproxymodel.h \ $$PWD/src/models/sortfilter/notelistproxymodel.h \
$$PWD/src/models/items/notelistitemwidget.h \
$$PWD/ui/edittags.h \ $$PWD/ui/edittags.h \
$$PWD/src/models/views/customtreeview.h \ $$PWD/src/models/views/customtreeview.h \
$$PWD/ui/trashitem.h \ $$PWD/ui/trashitem.h \
$$PWD/src/ui-managers/notelist-views/trashview.h \ $$PWD/src/ui-managers/notelist-views/trashview.h \
$$PWD/src/ui-managers/notelist-views/genericview.h \ $$PWD/src/ui-managers/notelist-views/genericview.h \
$$PWD/ui/notebook_editparent.h \ $$PWD/ui/notebook_editparent.h \
$$PWD/src/sql/sqlmanager.h $$PWD/src/sql/sqlmanager.h \
src/models/delegates/noteitemdelegate.h


INCLUDEPATH += $$PWD/include INCLUDEPATH += $$PWD/include
INCLUDEPATH += $$PWD/src/models/views # Location of customlistview INCLUDEPATH += $$PWD/src/models/views # Location of customlistview
Expand Down
121 changes: 121 additions & 0 deletions src/models/delegates/noteitemdelegate.cpp
@@ -0,0 +1,121 @@
#include "noteitemdelegate.h"
#include <QPainter>
#include <QDebug>
#include <QApplication>
#include <QMouseEvent>
#include <QAbstractItemModel>
#include "../items/notelistitem.h"

NoteItemDelegate::NoteItemDelegate(QListView *view, QSortFilterProxyModel *proxyModel) :
m_view(view),
m_proxyModel(proxyModel)
{

}

QRect NoteItemDelegate::getStarRect(const QStyleOptionViewItem &option) const
{
return QRect(option.rect.x()+option.rect.width()-35,
option.rect.y()+option.rect.height()/2-12,
25,25);
}


bool NoteItemDelegate::editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index)
{
if (event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton)
{
QRect checkboxRect = getStarRect(option);
QPoint mousePoint = mouseEvent->pos();
if ( checkboxRect.contains(mousePoint) ) {
QModelIndex realIndex = m_proxyModel->mapToSource(index);
NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() );
item->note()->setFavorited( !item->note()->favorited() );
return true;
}
else
m_view->setCurrentIndex(index);
}
}

return QStyledItemDelegate::editorEvent(event, model, option, index);
}

void NoteItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 0 ) {
QModelIndex realIndex = m_proxyModel->mapToSource(index);
NoteListItem *item = static_cast<NoteListItem*>( realIndex.internalPointer() );

// Set the background color
QBrush background =
(option.state & QStyle::State_Selected) ?
option.palette.highlight() :
option.palette.base();
painter->fillRect(option.rect, background);

if (option.state & QStyle::State_Selected)
painter->setPen(option.palette.highlightedText().color());
else
painter->setPen(option.palette.text().color());

QRect titleRect = option.rect;
titleRect.setX(titleRect.x()+5);
titleRect.setWidth(titleRect.x()-5);

QFont font=painter->font() ;
font.setPointSize(10);

// Title
font.setWeight(QFont::Bold);
painter->setFont(font);
painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+23), item->note()->title());

// Date
font.setWeight(QFont::Normal);
painter->setFont(font);
painter->drawText(QPoint(option.rect.x()+10, option.rect.y()+43), item->note()->date_created_str());


// The Excerpt
QRect excerptRect = option.rect;
excerptRect.setX( excerptRect.x()+10 );
excerptRect.setY( excerptRect.y()+50 );
excerptRect.setWidth( excerptRect.width()-50 );

QString excerpt = item->note()->text();
if (excerpt.length() > 50) {
excerpt = excerpt.mid(0, 50) + "...";
}
excerpt.replace("\n", " ");
painter->drawText(excerptRect, excerpt);

painter->fillRect(QRect(
option.rect.x() + option.rect.width() - 60,
option.rect.y(),
60,
option.rect.height()), background);

QIcon favoriteIcon;
if (item->note()->favorited())
favoriteIcon = QIcon::fromTheme("vibrato-draw-star-solid");
else
favoriteIcon = QIcon::fromTheme("vibrato-draw-star");

favoriteIcon.paint(painter, getStarRect(option));

} else {
QStyledItemDelegate::paint(painter, option, index);
}
}

QSize NoteItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(200,100);
}
31 changes: 31 additions & 0 deletions src/models/delegates/noteitemdelegate.h
@@ -0,0 +1,31 @@
#ifndef NOTEITEMDELEGATE_H
#define NOTEITEMDELEGATE_H
#include <QStyledItemDelegate>
#include <QListView>
#include <QSortFilterProxyModel>

class NoteItemDelegate : public QStyledItemDelegate
{
public:
NoteItemDelegate(QListView *view, QSortFilterProxyModel *proxyModel);

QRect getStarRect(const QStyleOptionViewItem &option) const;

bool editorEvent(QEvent *event,
QAbstractItemModel *model,
const QStyleOptionViewItem &option,
const QModelIndex &index) override;

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;

QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;

private:
bool checked = false;
QListView *m_view;
QSortFilterProxyModel *m_proxyModel;
};

#endif // MYDELEGATE_H
122 changes: 0 additions & 122 deletions src/models/items/notelistitemwidget.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions src/models/items/notelistitemwidget.h

This file was deleted.

0 comments on commit c139b82

Please sign in to comment.