Skip to content
Permalink
Browse files

EFFICIENT note list!

  • Loading branch information...
DougBeney committed Jan 21, 2019
1 parent 3160140 commit c139b8244d496e7000bd0bf11ae026b77cecdccc
@@ -52,14 +52,14 @@ SOURCES += \
$$PWD/ui/note_edittags.cpp \
$$PWD/src/models/items/listitemwithid.cpp \
$$PWD/src/models/sortfilter/notelistproxymodel.cpp \
$$PWD/src/models/items/notelistitemwidget.cpp \
$$PWD/ui/edittags.cpp \
$$PWD/src/models/views/customtreeview.cpp \
$$PWD/ui/trashitem.cpp \
$$PWD/src/ui-managers/notelist-views/trashview.cpp \
$$PWD/src/ui-managers/notelist-views/genericview.cpp \
$$PWD/ui/notebook_editparent.cpp \
$$PWD/src/sql/sqlmanager.cpp
$$PWD/src/sql/sqlmanager.cpp \
src/models/delegates/noteitemdelegate.cpp

HEADERS += \
$$PWD/src/meta/info/appconfig.h \
@@ -91,14 +91,14 @@ HEADERS += \
$$PWD/ui/note_edittags.h \
$$PWD/src/models/items/listitemwithid.h \
$$PWD/src/models/sortfilter/notelistproxymodel.h \
$$PWD/src/models/items/notelistitemwidget.h \
$$PWD/ui/edittags.h \
$$PWD/src/models/views/customtreeview.h \
$$PWD/ui/trashitem.h \
$$PWD/src/ui-managers/notelist-views/trashview.h \
$$PWD/src/ui-managers/notelist-views/genericview.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/src/models/views # Location of customlistview
@@ -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);
}
@@ -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

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit c139b82

Please sign in to comment.
You can’t perform that action at this time.