Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Doug Beney
committed
Jan 21, 2019
1 parent
3160140
commit c139b82
Showing
9 changed files
with
171 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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.